aboutsummaryrefslogtreecommitdiffstats
path: root/composer/e-composer-actions.c
blob: a1fe1cafd4d1adc2f24f0436fa5730f2d987015a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>  
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 */

#include "e-composer-actions.h"
#include "e-composer-private.h"

#include <errno.h>
#include <fcntl.h>
#include <e-util/e-error.h>
#include <mail/em-event.h>

#include "misc/e-charset-picker.h"

static void
action_attach_cb (GtkAction *action,
                  EMsgComposer *composer)
{
    EAttachmentView *view;
    EAttachmentStore *store;

    view = e_msg_composer_get_attachment_view (composer);
    store = e_attachment_view_get_store (view);

    e_attachment_store_run_load_dialog (store, GTK_WINDOW (composer));
}

static void
action_charset_cb (GtkRadioAction *action,
                   GtkRadioAction *current,
                   EMsgComposer *composer)
{
    const gchar *charset;

    if (action != current)
        return;

    charset = g_object_get_data (G_OBJECT (action), "charset");

    g_free (composer->priv->charset);
    composer->priv->charset = g_strdup (charset);
}

static void
action_close_cb (GtkAction *action,
                 EMsgComposer *composer)
{
    GtkhtmlEditor *editor;
    EComposerHeaderTable *table;
    GtkWidget *widget;
    const gchar *subject;
    gint response;

    editor = GTKHTML_EDITOR (composer);
    widget = GTK_WIDGET (composer);

    if (!gtkhtml_editor_get_changed (editor) ||
        e_msg_composer_is_exiting (composer)) {
        gtk_widget_destroy (widget);
        return;
    }

    gdk_window_raise (widget->window);

    table = e_msg_composer_get_header_table (composer);
    subject = e_composer_header_table_get_subject (table);

    if (subject == NULL || *subject == '\0')
        subject = _("Untitled Message");

    response = e_error_run (
        GTK_WINDOW (composer),
        "mail-composer:exit-unsaved",
        subject, NULL);

    switch (response) {
        case GTK_RESPONSE_YES:
            gtk_widget_hide (widget);
            gtk_action_activate (ACTION (SAVE_DRAFT));
            break;

        case GTK_RESPONSE_NO:
            gtk_widget_destroy (widget);
            break;

        case GTK_RESPONSE_CANCEL:
            break;
    }
}

static void
action_pgp_encrypt_cb (GtkToggleAction *action,
                       EMsgComposer *composer)
{
    GtkhtmlEditor *editor;

    editor = GTKHTML_EDITOR (composer);
    gtkhtml_editor_set_changed (editor, TRUE);
}

static void
action_pgp_sign_cb (GtkToggleAction *action,
                    EMsgComposer *composer)
{
    GtkhtmlEditor *editor;

    editor = GTKHTML_EDITOR (composer);
    gtkhtml_editor_set_changed (editor, TRUE);
}

static void
action_print_cb (GtkAction *action,
                 EMsgComposer *composer)
{
    GtkPrintOperationAction print_action;

    print_action = GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG;
    e_msg_composer_print (composer, print_action);
}

static void
action_print_preview_cb (GtkAction *action,
                         EMsgComposer *composer)
{
    GtkPrintOperationAction print_action;

    print_action = GTK_PRINT_OPERATION_ACTION_PREVIEW;
    e_msg_composer_print (composer, print_action);
}

static void
action_save_cb (GtkAction *action,
                EMsgComposer *composer)
{
    GtkhtmlEditor *editor = GTKHTML_EDITOR (composer);
    const gchar *filename;
    gint fd;
    GError *error = NULL;

    filename = gtkhtml_editor_get_filename (editor);
    if (filename == NULL) {
        gtk_action_activate (ACTION (SAVE_AS));
        return;
    }

    /* Check if the file already exists and we can create it. */
    fd = g_open (filename, O_RDONLY | O_CREAT | O_EXCL, 0777);
    if (fd < 0) {
        gint errno_saved = errno;

        if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
            gint response;

            response = e_error_run (
                GTK_WINDOW (composer),
                E_ERROR_ASK_FILE_EXISTS_OVERWRITE,
                filename, NULL);
            if (response != GTK_RESPONSE_OK)
                return;
        } else {
            e_error_run (
                GTK_WINDOW (composer),
                E_ERROR_NO_SAVE_FILE, filename,
                g_strerror (errno_saved), NULL);
            return;
        }
    } else
        close (fd);

    if (!gtkhtml_editor_save (editor, filename, TRUE, &error)) {
        e_error_run (
            GTK_WINDOW (composer),
            E_ERROR_NO_SAVE_FILE,
            filename, error->message, NULL);
        g_error_free (error);
        return;
    }

    gtkhtml_editor_run_command (GTKHTML_EDITOR (composer), "saved");
    e_composer_autosave_set_saved (composer, FALSE);
}

static void
action_save_as_cb (GtkAction *action,
                   EMsgComposer *composer)
{
    GtkWidget *dialog;
    gchar *filename;
    gint response;

    dialog = gtk_file_chooser_dialog_new (
        _("Save as..."), GTK_WINDOW (composer),
        GTK_FILE_CHOOSER_ACTION_SAVE,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        GTK_STOCK_SAVE, GTK_RESPONSE_OK,
        NULL);

    gtk_dialog_set_default_response (
        GTK_DIALOG (dialog), GTK_RESPONSE_OK);
    gtk_file_chooser_set_local_only (
        GTK_FILE_CHOOSER (dialog), FALSE);
    gtk_window_set_icon_name (
        GTK_WINDOW (dialog), "mail-message-new");

    response = gtkhtml_editor_file_chooser_dialog_run (
        GTKHTML_EDITOR (composer), dialog);

    if (response != GTK_RESPONSE_OK)
        goto exit;

    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
    gtkhtml_editor_set_filename (GTKHTML_EDITOR (composer), filename);
    g_free (filename);

    gtk_action_activate (ACTION (SAVE));

exit:
    gtk_widget_destroy (dialog);
}

static void
action_save_draft_cb (GtkAction *action,
                      EMsgComposer *composer)
{
    e_msg_composer_save_draft (composer);
}

static void
action_send_cb (GtkAction *action,
                EMsgComposer *composer)
{
    e_msg_composer_send (composer);
}

static void
action_send_options_cb (GtkAction *action,
                        EMsgComposer *composer)
{
    EMEvent *event = em_event_peek ();
    EMEventTargetComposer *target;

    target = em_event_target_new_composer (
        event, composer, EM_EVENT_COMPOSER_SEND_OPTION);
    e_msg_composer_set_send_options (composer, FALSE);

    e_event_emit (
        (EEvent *) event,
        "composer.selectsendoption",
        (EEventTarget *) target);

    if (!composer->priv->send_invoked)
        e_error_run (
            GTK_WINDOW (composer),
            "mail-composer:send-options-support", NULL);
}

static void
action_new_message_cb (GtkAction *action,
                       EMsgComposer *composer)
{
    EMsgComposer *new_composer;

    new_composer = e_msg_composer_new ();
    gtk_widget_show (GTK_WIDGET (new_composer));
}

static void
action_smime_encrypt_cb (GtkToggleAction *action,
                         EMsgComposer *composer)
{
    GtkhtmlEditor *editor;

    editor = GTKHTML_EDITOR (composer);
    gtkhtml_editor_set_changed (editor, TRUE);
}

static void
action_smime_sign_cb (GtkToggleAction *action,
                      EMsgComposer *composer)
{
    GtkhtmlEditor *editor;

    editor = GTKHTML_EDITOR (composer);
    gtkhtml_editor_set_changed (editor, TRUE);
}

static void
action_view_bcc_cb (GtkToggleAction *action,
                    EMsgComposer *composer)
{
    EComposerHeaderTable *table;
    gboolean active;

    table = e_msg_composer_get_header_table (composer);
    active = gtk_toggle_action_get_active (action);

    e_composer_header_table_set_header_visible (
        table, E_COMPOSER_HEADER_BCC, active);
}

static void
action_view_cc_cb (GtkToggleAction *action,
                   EMsgComposer *composer)
{
    EComposerHeaderTable *table;
    gboolean active;

    table = e_msg_composer_get_header_table (composer);
    active = gtk_toggle_action_get_active (action);

    e_composer_header_table_set_header_visible (
        table, E_COMPOSER_HEADER_CC, active);
}

static void
action_view_from_cb (GtkToggleAction *action,
                     EMsgComposer *composer)
{
    EComposerHeaderTable *table;
    gboolean active;

    table = e_msg_composer_get_header_table (composer);
    active = gtk_toggle_action_get_active (action);

    e_composer_header_table_set_header_visible (
        table, E_COMPOSER_HEADER_FROM, active);
}

static void
action_view_post_to_cb (GtkToggleAction *action,
                        EMsgComposer *composer)
{
    EComposerHeaderTable *table;
    gboolean active;

    table = e_msg_composer_get_header_table (composer);
    active = gtk_toggle_action_get_active (action);

    e_composer_header_table_set_header_visible (
        table, E_COMPOSER_HEADER_POST_TO, active);
}

static void
action_view_reply_to_cb (GtkToggleAction *action,
                         EMsgComposer *composer)
{
    EComposerHeaderTable *table;
    gboolean active;

    table = e_msg_composer_get_header_table (composer);
    active = gtk_toggle_action_get_active (action);

    e_composer_header_table_set_header_visible (
        table, E_COMPOSER_HEADER_REPLY_TO, active);
}

static void
action_view_subject_cb (GtkToggleAction *action,
                        EMsgComposer *composer)
{
    EComposerHeaderTable *table;
    gboolean active;

    table = e_msg_composer_get_header_table (composer);
    active = gtk_toggle_action_get_active (action);

    e_composer_header_table_set_header_visible (
        table, E_COMPOSER_HEADER_SUBJECT, active);
}

static void
action_view_to_cb (GtkToggleAction *action,
                   EMsgComposer *composer)
{
    EComposerHeaderTable *table;
    gboolean active;

    table = e_msg_composer_get_header_table (composer);
    active = gtk_toggle_action_get_active (action);

    e_composer_header_table_set_header_visible (
        table, E_COMPOSER_HEADER_TO, active);
}

static GtkActionEntry entries[] = {

    { "attach",
          "mail-attachment",
      N_("_Attachment..."),
      "<Control>m",
      N_("Attach a file"),
      G_CALLBACK (action_attach_cb) },

    { "close",
      GTK_STOCK_CLOSE,
      N_("_Close"),
      "<Control>w",
      N_("Close the current file"),
      G_CALLBACK (action_close_cb) },

    { "print",
      GTK_STOCK_PRINT,
      N_("_Print..."),
      "<Control>p",
      NULL,
      G_CALLBACK (action_print_cb) },

    { "print-preview",
      GTK_STOCK_PRINT_PREVIEW,
      N_("Print Pre_view"),
      "<Shift><Control>p",
      NULL,
      G_CALLBACK (action_print_preview_cb) },

    { "save",
      GTK_STOCK_SAVE,
      N_("_Save"),
      "<Control>s",
      N_("Save the current file"),
      G_CALLBACK (action_save_cb) },

    { "save-as",
      GTK_STOCK_SAVE_AS,
      N_("Save _As..."),
      NULL,
      N_("Save the current file with a different name"),
      G_CALLBACK (action_save_as_cb) },

    { "save-draft",
      GTK_STOCK_SAVE,
      N_("Save as _Draft"),
      "<Shift><Control>s",
      N_("Save as draft"),
      G_CALLBACK (action_save_draft_cb) },

    { "send",
      "mail-send",
      N_("S_end"),
      "<Control>Return",
      N_("Send this message"),
      G_CALLBACK (action_send_cb) },

    { "send-options",
      NULL,
      N_("_Send Options"),
      NULL,
      N_("Insert Send options"),
      G_CALLBACK (action_send_options_cb) },

    { "new-message",
      "mail-message-new",
      N_("New _Message"),
      "<Control>n",
      N_("Open New Message window"),
      G_CALLBACK (action_new_message_cb) },

    /* Menus */

    { "charset-menu",
      NULL,
      N_("Character _Encoding"),
      NULL,
      NULL,
      NULL },

    { "security-menu",
      NULL,
      N_("_Security"),
      NULL,
      NULL,
      NULL }
};

static GtkToggleActionEntry toggle_entries[] = {

    { "pgp-encrypt",
      NULL,
      N_("PGP _Encrypt"),
      NULL,
      N_("Encrypt this message with PGP"),
      G_CALLBACK (action_pgp_encrypt_cb),
      FALSE },

    { "pgp-sign",
      NULL,
      N_("PGP _Sign"),
      NULL,
      N_("Sign this message with your PGP key"),
      G_CALLBACK (action_pgp_sign_cb),
      FALSE },

    { "prioritize-message",
      NULL,
      N_("_Prioritize Message"),
      NULL,
      N_("Set the message priority to high"),
      NULL,  /* no callback */
      FALSE },

    { "request-read-receipt",
      NULL,
      N_("Re_quest Read Receipt"),
      NULL,
      N_("Get delivery notification when your message is read"),
      NULL,  /* no callback */
      FALSE },

    { "smime-encrypt",
      NULL,
      N_("S/MIME En_crypt"),
      NULL,
      N_("Encrypt this message with your S/MIME Encryption Certificate"),
      G_CALLBACK (action_smime_encrypt_cb),
      FALSE },

    { "smime-sign",
      NULL,
      N_("S/MIME Sig_n"),
      NULL,
      N_("Sign this message with your S/MIME Signature Certificate"),
      G_CALLBACK (action_smime_sign_cb),
      FALSE },

    { "view-bcc",
      NULL,
      N_("_Bcc Field"),
      NULL,
      N_("Toggles whether the BCC field is displayed"),
      G_CALLBACK (action_view_bcc_cb),
      FALSE },

    { "view-cc",
      NULL,
      N_("_Cc Field"),
      NULL,
      N_("Toggles whether the CC field is displayed"),
      G_CALLBACK (action_view_cc_cb),
      FALSE },

    { "view-from",
      NULL,
      N_("_From Field"),
      NULL,
      N_("Toggles whether the From chooser is displayed"),
      G_CALLBACK (action_view_from_cb),
      FALSE },

    { "view-post-to",
      NULL,
      N_("_Post-To Field"),
      NULL,
      N_("Toggles whether the Post-To field is displayed"),
      G_CALLBACK (action_view_post_to_cb),
      FALSE },

    { "view-reply-to",
      NULL,
      N_("_Reply-To Field"),
      NULL,
      N_("Toggles whether the Reply-To field is displayed"),
      G_CALLBACK (action_view_reply_to_cb),
      FALSE },

    { "view-subject",
      NULL,
      N_("_Subject Field"),
      NULL,
      N_("Toggles whether the Subject field is displayed"),
      G_CALLBACK (action_view_subject_cb),
      FALSE },

    { "view-to",
      NULL,
      N_("_To Field"),
      NULL,
      N_("Toggles whether the To field is displayed"),
      G_CALLBACK (action_view_to_cb),
      FALSE }
};

void
e_composer_actions_init (EMsgComposer *composer)
{
    GtkActionGroup *action_group;
    GtkUIManager *ui_manager;
    gboolean visible;

    g_return_if_fail (E_IS_MSG_COMPOSER (composer));

    ui_manager = gtkhtml_editor_get_ui_manager (GTKHTML_EDITOR (composer));

    /* Composer Actions */
    action_group = composer->priv->composer_actions;
    gtk_action_group_set_translation_domain (
        action_group, GETTEXT_PACKAGE);
    gtk_action_group_add_actions (
        action_group, entries,
        G_N_ELEMENTS (entries), composer);
    gtk_action_group_add_toggle_actions (
        action_group, toggle_entries,
        G_N_ELEMENTS (toggle_entries), composer);
    gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);

    /* Character Set Actions */
    action_group = composer->priv->charset_actions;
    gtk_action_group_set_translation_domain (
        action_group, GETTEXT_PACKAGE);
    e_charset_add_radio_actions (
        action_group, "charset-", composer->priv->charset,
        G_CALLBACK (action_charset_cb), composer);
    gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);

    /* Fine Tuning */

    g_object_set (
        ACTION (ATTACH), "short-label", _("Attach"), NULL);

    g_object_set (
        ACTION (SAVE_DRAFT), "short-label", _("Save Draft"), NULL);

#if defined (HAVE_NSS)
    visible = TRUE;
#else
    visible = FALSE;
#endif

    gtk_action_set_visible (ACTION (SMIME_ENCRYPT), visible);
    gtk_action_set_visible (ACTION (SMIME_SIGN), visible);
}