aboutsummaryrefslogtreecommitdiffstats
path: root/modules/mail/e-mail-shell-view-private.c
blob: a7309904bdc22c445a1ff8ea283582badfca1235 (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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
/*
 * e-mail-shell-view-private.c
 *
 * 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)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-mail-shell-view-private.h"

#include "widgets/menus/gal-view-factory-etable.h"
#include "widgets/misc/e-menu-tool-button.h"

#include "e-util/e-util-private.h"

typedef struct _AsyncContext AsyncContext;

struct _AsyncContext {
    EActivity *activity;
    EMailReader *reader;
    EShellView *shell_view;
};

static void
async_context_free (AsyncContext *context)
{
    if (context->activity != NULL)
        g_object_unref (context->activity);

    if (context->reader != NULL)
        g_object_unref (context->reader);

    if (context->shell_view != NULL)
        g_object_unref (context->shell_view);

    g_slice_free (AsyncContext, context);
}

static void
mail_shell_view_got_folder_cb (CamelStore *store,
                               GAsyncResult *result,
                               AsyncContext *context)
{
    EAlertSink *alert_sink;
    CamelFolder *folder;
    GError *error = NULL;

    alert_sink = e_activity_get_alert_sink (context->activity);

    folder = camel_store_get_folder_finish (store, result, &error);

    if (e_activity_handle_cancellation (context->activity, error)) {
        g_warn_if_fail (folder == NULL);
        async_context_free (context);
        g_error_free (error);
        return;

    } else if (error != NULL) {
        g_warn_if_fail (folder == NULL);
        e_alert_submit (
            alert_sink, "mail:folder-open",
            error->message, NULL);
        async_context_free (context);
        g_error_free (error);
        return;
    }

    e_mail_reader_set_folder (context->reader, folder);
    e_shell_view_update_actions (context->shell_view);

    g_object_unref (folder);

    async_context_free (context);
}

static void
mail_shell_view_folder_tree_selected_cb (EMailShellView *mail_shell_view,
                                         CamelStore *store,
                                         const gchar *folder_name,
                                         CamelFolderInfoFlags flags,
                                         EMFolderTree *folder_tree)
{
    EMailShellContent *mail_shell_content;
    EShellView *shell_view;
    EMailReader *reader;
    EMailView *mail_view;
    GCancellable *cancellable;
    AsyncContext *context;
    EActivity *activity;

    shell_view = E_SHELL_VIEW (mail_shell_view);

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    reader = E_MAIL_READER (mail_view);

    /* Cancel any unfinished open folder operations. */
    if (mail_shell_view->priv->opening_folder != NULL) {
        g_cancellable_cancel (mail_shell_view->priv->opening_folder);
        g_object_unref (mail_shell_view->priv->opening_folder);
        mail_shell_view->priv->opening_folder = NULL;
    }

    /* If we are to clear the message list, do so immediately. */
    if ((flags & CAMEL_FOLDER_NOSELECT) || folder_name == NULL) {
        e_mail_reader_set_folder (reader, NULL);
        e_shell_view_update_actions (shell_view);
        return;
    }

    g_warn_if_fail (CAMEL_IS_STORE (store));

    /* Open the selected folder asynchronously. */

    activity = e_mail_reader_new_activity (reader);
    cancellable = e_activity_get_cancellable (activity);
    mail_shell_view->priv->opening_folder = g_object_ref (cancellable);

    context = g_slice_new0 (AsyncContext);
    context->activity = activity;
    context->reader = g_object_ref (reader);
    context->shell_view = g_object_ref (shell_view);

    camel_store_get_folder (
        store, folder_name, 0, G_PRIORITY_DEFAULT, cancellable,
        (GAsyncReadyCallback) mail_shell_view_got_folder_cb, context);
}

static gboolean
mail_shell_view_folder_tree_key_press_event_cb (EMailShellView *mail_shell_view,
                                                GdkEventKey *event)
{
    EMailShellContent *mail_shell_content;
    EMailView *mail_view;
    gboolean handled = FALSE;

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    if ((event->state & GDK_CONTROL_MASK) != 0)
        goto ctrl;

    /* <keyval> alone */
    switch (event->keyval) {
        case GDK_KEY_period:
        case GDK_KEY_comma:
        case GDK_KEY_bracketleft:
        case GDK_KEY_bracketright:
            goto emit;

        default:
            goto exit;
    }

ctrl:
    /* Ctrl + <keyval> */
    switch (event->keyval) {
        case GDK_KEY_period:
        case GDK_KEY_comma:
            goto emit;

        default:
            goto exit;
    }

    /* All branches jump past this. */
    g_return_val_if_reached (FALSE);

emit:
    /* Forward the key press to the EMailReader interface. */
    g_signal_emit_by_name (mail_view, "key-press-event", event, &handled);

exit:
    return handled;
}

static void
mail_shell_view_folder_tree_selection_done_cb (EMailShellView *mail_shell_view,
                                               GtkWidget *menu)
{
    EMailShellContent *mail_shell_content;
    EMailShellSidebar *mail_shell_sidebar;
    EMFolderTree *folder_tree;
    GtkWidget *message_list;
    EMailReader *reader;
    EMailView *mail_view;
    CamelFolder *folder;
    gchar *list_uri;
    gchar *tree_uri;

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    mail_shell_sidebar = mail_shell_view->priv->mail_shell_sidebar;
    folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar);

    reader = E_MAIL_READER (mail_view);
    message_list = e_mail_reader_get_message_list (reader);

    /* Don't use e_mail_reader_get_folder() here.  The fact that the
     * method gets the folder from the message list is supposed to be
     * a hidden implementation detail, and we want to explicitly get
     * the folder URI from the message list here. */
    folder = MESSAGE_LIST (message_list)->folder;
    if (folder)
        list_uri = e_mail_folder_uri_from_folder (folder);
    else
        list_uri = NULL;
    tree_uri = em_folder_tree_get_selected_uri (folder_tree);

    /* If the folder tree and message list disagree on the current
     * folder, reset the folder tree to match the message list. */
    if (list_uri && g_strcmp0 (tree_uri, list_uri) != 0)
        em_folder_tree_set_selected (folder_tree, list_uri, FALSE);

    g_free (list_uri);
    g_free (tree_uri);

    /* Disconnect from the "selection-done" signal. */
    g_signal_handlers_disconnect_by_func (
        menu, mail_shell_view_folder_tree_selection_done_cb,
        mail_shell_view);
}

static void
mail_shell_view_folder_tree_popup_event_cb (EShellView *shell_view,
                                            GdkEventButton *event)
{
    GtkWidget *menu;
    const gchar *widget_path;

    widget_path = "/mail-folder-popup";
    menu = e_shell_view_show_popup_menu (shell_view, widget_path, event);

    g_signal_connect_object (
        menu, "selection-done",
        G_CALLBACK (mail_shell_view_folder_tree_selection_done_cb),
        shell_view, G_CONNECT_SWAPPED);
}

static gboolean
mail_shell_view_key_press_event_cb (EMailShellView *mail_shell_view,
                                    GdkEventKey *event)
{
    EShellView *shell_view;
    EShellWindow *shell_window;
    GtkAction *action;

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_window = e_shell_view_get_shell_window (shell_view);

    if ((event->state & GDK_CONTROL_MASK) != 0)
        return FALSE;

    switch (event->keyval) {
        case GDK_KEY_space:
            action = ACTION (MAIL_SMART_FORWARD);
            break;

        case GDK_KEY_BackSpace:
            action = ACTION (MAIL_SMART_BACKWARD);
            break;

        default:
            return FALSE;
    }

    gtk_action_activate (action);

    return TRUE;
}

static gboolean
mail_shell_view_message_list_key_press_cb (EMailShellView *mail_shell_view,
                                           gint row,
                                           ETreePath path,
                                           gint col,
                                           GdkEvent *event)
{
    return mail_shell_view_key_press_event_cb (
        mail_shell_view, &event->key);
}

static gboolean
mail_shell_view_message_list_popup_menu_cb (EShellView *shell_view)
{
    const gchar *widget_path;

    widget_path = "/mail-message-popup";
    e_shell_view_show_popup_menu (shell_view, widget_path, NULL);

    return TRUE;
}

static gboolean
mail_shell_view_message_list_right_click_cb (EShellView *shell_view,
                                             gint row,
                                             ETreePath path,
                                             gint col,
                                             GdkEventButton *event)
{
    const gchar *widget_path;

    widget_path = "/mail-message-popup";
    e_shell_view_show_popup_menu (shell_view, widget_path, event);

    return TRUE;
}

static gboolean
mail_shell_view_popup_event_cb (EMailShellView *mail_shell_view,
                                GdkEventButton *event,
                                const gchar *uri)
{
    EMailShellContent *mail_shell_content;
    EMFormatHTML *formatter;
    EShellView *shell_view;
    EMailReader *reader;
    EMailView *mail_view;
    EWebView *web_view;
    GtkMenu *menu;

    if (uri != NULL)
        return FALSE;

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    reader = E_MAIL_READER (mail_view);
    formatter = e_mail_reader_get_formatter (reader);
    web_view = em_format_html_get_web_view (formatter);

    if (e_web_view_get_cursor_image (web_view) != NULL)
        return FALSE;

    menu = e_mail_reader_get_popup_menu (reader);
    shell_view = E_SHELL_VIEW (mail_shell_view);
    e_shell_view_update_actions (shell_view);

    if (event == NULL)
        gtk_menu_popup (
            menu, NULL, NULL, NULL, NULL,
            0, gtk_get_current_event_time ());
    else
        gtk_menu_popup (
            menu, NULL, NULL, NULL, NULL,
            event->button, event->time);

    return TRUE;
}

static void
mail_shell_view_scroll_cb (EMailShellView *mail_shell_view,
                           GtkOrientation orientation,
                           GtkScrollType scroll_type,
                           gfloat position,
                           GtkHTML *html)
{
    EShell *shell;
    EShellView *shell_view;
    EShellWindow *shell_window;
    EShellSettings *shell_settings;
    EMailShellContent *mail_shell_content;
    EMailReader *reader;
    EMailView *mail_view;
    EWebView *web_view;
    GtkWidget *message_list;
    gboolean magic_spacebar;

    web_view = E_WEB_VIEW (html);

    if (html->binding_handled || e_web_view_get_caret_mode (web_view))
        return;

    if (orientation != GTK_ORIENTATION_VERTICAL)
        return;

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_window = e_shell_view_get_shell_window (shell_view);
    shell = e_shell_window_get_shell (shell_window);
    shell_settings = e_shell_get_shell_settings (shell);

    magic_spacebar = e_shell_settings_get_boolean (
        shell_settings, "mail-magic-spacebar");

    if (!magic_spacebar)
        return;

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    reader = E_MAIL_READER (mail_view);
    message_list = e_mail_reader_get_message_list (reader);

    if (scroll_type == GTK_SCROLL_PAGE_FORWARD)
        message_list_select (
            MESSAGE_LIST (message_list),
            MESSAGE_LIST_SELECT_NEXT,
            0, CAMEL_MESSAGE_SEEN);
    else
        message_list_select (
            MESSAGE_LIST (message_list),
            MESSAGE_LIST_SELECT_PREVIOUS,
            0, CAMEL_MESSAGE_SEEN);
}

static void
mail_shell_view_reader_changed_cb (EMailShellView *mail_shell_view,
                                   EMailReader *reader)
{
    GtkWidget *message_list;
    EMFormatHTML *formatter;
    EWebView *web_view;
    EShellView *shell_view;
    EShellTaskbar *shell_taskbar;

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_taskbar = e_shell_view_get_shell_taskbar (shell_view);

    formatter = e_mail_reader_get_formatter (reader);
    message_list = e_mail_reader_get_message_list (reader);
    web_view = em_format_html_get_web_view (formatter);

    e_shell_view_update_actions (E_SHELL_VIEW (mail_shell_view));
    e_mail_shell_view_update_sidebar (mail_shell_view);

    /* Connect if its not connected already */
    if (g_signal_handler_find (
        message_list, G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
        mail_shell_view_message_list_key_press_cb, NULL))
        return;

    g_signal_connect_object (
        message_list, "key-press",
        G_CALLBACK (mail_shell_view_message_list_key_press_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        message_list, "popup-menu",
        G_CALLBACK (mail_shell_view_message_list_popup_menu_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        message_list, "right-click",
        G_CALLBACK (mail_shell_view_message_list_right_click_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "key-press-event",
        G_CALLBACK (mail_shell_view_key_press_event_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "popup-event",
        G_CALLBACK (mail_shell_view_popup_event_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "scroll",
        G_CALLBACK (mail_shell_view_scroll_cb),
        mail_shell_view,
        G_CONNECT_AFTER | G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "status-message",
        G_CALLBACK (e_shell_taskbar_set_message),
        shell_taskbar, G_CONNECT_SWAPPED);
}

static void
mail_shell_view_reader_update_actions_cb (EMailReader *reader,
                                          guint32 state,
                                          EMailShellView *mail_shell_view)
{
    EMailShellContent *mail_shell_content;

    g_return_if_fail (mail_shell_view != NULL);
    g_return_if_fail (mail_shell_view->priv != NULL);

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    e_mail_reader_update_actions (E_MAIL_READER (mail_shell_content), state);
}

static void
mail_shell_view_prepare_for_quit_done_cb (CamelFolder *folder,
                                          gpointer user_data)
{
    g_object_unref (E_ACTIVITY (user_data));
}

static void
mail_shell_view_prepare_for_quit_cb (EMailShellView *mail_shell_view,
                                     EActivity *activity)
{
    EMailShellContent *mail_shell_content;
    CamelFolder *folder;
    EMailReader *reader;
    EMailView *mail_view;
    GtkWidget *message_list;

    /* If we got here, it means the application is shutting down
     * and this is the last EMailShellView instance.  Synchronize
     * the currently selected folder before we terminate. */

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    reader = E_MAIL_READER (mail_view);
    folder = e_mail_reader_get_folder (reader);
    message_list = e_mail_reader_get_message_list (reader);

    message_list_save_state (MESSAGE_LIST (message_list));

    if (folder == NULL)
        return;

    mail_sync_folder (
        folder,
        mail_shell_view_prepare_for_quit_done_cb,
        g_object_ref (activity));
}

static void
mail_shell_view_load_view_collection (EShellViewClass *shell_view_class)
{
    GalViewCollection *collection;
    GalViewFactory *factory;
    ETableSpecification *spec;
    const gchar *base_dir;
    gchar *filename;

    collection = shell_view_class->view_collection;

    base_dir = EVOLUTION_ETSPECDIR;
    spec = e_table_specification_new ();
    filename = g_build_filename (base_dir, ETSPEC_FILENAME, NULL);
    if (!e_table_specification_load_from_file (spec, filename))
        g_critical ("Unable to load ETable specification file "
                "for mail");
    g_free (filename);

    factory = gal_view_factory_etable_new (spec);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);
    g_object_unref (spec);

    gal_view_collection_load (collection);
}

static void
mail_shell_view_notify_view_id_cb (EMailShellView *mail_shell_view)
{
    EMailShellContent *mail_shell_content;
    GalViewInstance *view_instance;
    EMailView *mail_view;
    const gchar *view_id;

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    view_instance = e_mail_view_get_view_instance (mail_view);
    view_id = e_shell_view_get_view_id (E_SHELL_VIEW (mail_shell_view));

    /* A NULL view ID implies we're in a custom view.  But you can
     * only get to a custom view via the "Define Views" dialog, which
     * would have already modified the view instance appropriately.
     * Furthermore, there's no way to refer to a custom view by ID
     * anyway, since custom views have no IDs. */
    if (view_id == NULL)
        return;

    gal_view_instance_set_current_view_id (view_instance, view_id);
}

void
e_mail_shell_view_private_init (EMailShellView *mail_shell_view,
                                EShellViewClass *shell_view_class)
{
    if (!gal_view_collection_loaded (shell_view_class->view_collection))
        mail_shell_view_load_view_collection (shell_view_class);

    g_signal_connect (
        mail_shell_view, "notify::view-id",
        G_CALLBACK (mail_shell_view_notify_view_id_cb), NULL);
}

void
e_mail_shell_view_private_constructed (EMailShellView *mail_shell_view)
{
    EMailShellViewPrivate *priv = mail_shell_view->priv;
    EMailShellContent *mail_shell_content;
    EMailShellSidebar *mail_shell_sidebar;
    EShell *shell;
    EShellView *shell_view;
    EShellBackend *shell_backend;
    EShellContent *shell_content;
    EShellSettings *shell_settings;
    EShellSidebar *shell_sidebar;
    EShellTaskbar *shell_taskbar;
    EShellWindow *shell_window;
    EShellSearchbar *searchbar;
    EMFormatHTML *formatter;
    EMFolderTree *folder_tree;
    EActionComboBox *combo_box;
    ERuleContext *context;
    EFilterRule *rule = NULL;
    GtkTreeSelection *selection;
    GtkTreeModel *tree_model;
    GtkUIManager *ui_manager;
    GtkWidget *message_list;
    EMailReader *reader;
    EMailView *mail_view;
    EWebView *web_view;
    const gchar *source;
    guint merge_id;
    gint ii = 0;

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_backend = e_shell_view_get_shell_backend (shell_view);
    shell_content = e_shell_view_get_shell_content (shell_view);
    shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);
    shell_taskbar = e_shell_view_get_shell_taskbar (shell_view);
    shell_window = e_shell_view_get_shell_window (shell_view);

    ui_manager = e_shell_window_get_ui_manager (shell_window);

    shell = e_shell_window_get_shell (shell_window);
    shell_settings = e_shell_get_shell_settings (shell);

    tree_model = e_shell_settings_get_object (
        shell_settings, "mail-label-list-store");

    e_shell_window_add_action_group (shell_window, "mail");
    e_shell_window_add_action_group (shell_window, "mail-filter");
    e_shell_window_add_action_group (shell_window, "mail-label");
    e_shell_window_add_action_group (shell_window, "search-folders");

    merge_id = gtk_ui_manager_new_merge_id (ui_manager);
    priv->label_merge_id = merge_id;

    /* Cache these to avoid lots of awkward casting. */
    priv->mail_shell_backend = g_object_ref (shell_backend);
    priv->mail_shell_content = g_object_ref (shell_content);
    priv->mail_shell_sidebar = g_object_ref (shell_sidebar);

    mail_shell_sidebar = E_MAIL_SHELL_SIDEBAR (shell_sidebar);
    folder_tree = e_mail_shell_sidebar_get_folder_tree (mail_shell_sidebar);
    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (folder_tree));

    mail_shell_content = E_MAIL_SHELL_CONTENT (shell_content);
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);
    searchbar = e_mail_shell_content_get_searchbar (mail_shell_content);
    combo_box = e_shell_searchbar_get_scope_combo_box (searchbar);

    reader = E_MAIL_READER (shell_content);
    formatter = e_mail_reader_get_formatter (reader);
    message_list = e_mail_reader_get_message_list (reader);

    em_folder_tree_set_selectable_widget (folder_tree, message_list);

    /* The folder tree and scope combo box are both insensitive
     * when searching beyond the currently selected folder. */
    g_object_bind_property (
        folder_tree, "sensitive",
        combo_box, "sensitive",
        G_BINDING_BIDIRECTIONAL |
        G_BINDING_SYNC_CREATE);

    web_view = em_format_html_get_web_view (formatter);

    g_signal_connect_object (
        folder_tree, "folder-selected",
        G_CALLBACK (mail_shell_view_folder_tree_selected_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        folder_tree, "key-press-event",
        G_CALLBACK (mail_shell_view_folder_tree_key_press_event_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        folder_tree, "popup-event",
        G_CALLBACK (mail_shell_view_folder_tree_popup_event_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        message_list, "key-press",
        G_CALLBACK (mail_shell_view_message_list_key_press_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        message_list, "popup-menu",
        G_CALLBACK (mail_shell_view_message_list_popup_menu_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        message_list, "right-click",
        G_CALLBACK (mail_shell_view_message_list_right_click_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        reader, "changed",
        G_CALLBACK (mail_shell_view_reader_changed_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        mail_view, "update-actions",
        G_CALLBACK (mail_shell_view_reader_update_actions_cb),
        mail_shell_view, 0);

    g_signal_connect_object (
        reader, "folder-loaded",
        G_CALLBACK (e_mail_view_update_view_instance),
        mail_view, G_CONNECT_SWAPPED);

    /* Use the same callback as "changed". */
    g_signal_connect_object (
        reader, "folder-loaded",
        G_CALLBACK (mail_shell_view_reader_changed_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        reader, "folder-loaded",
        G_CALLBACK (e_mail_shell_view_restore_state),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        tree_model, "row-changed",
        G_CALLBACK (e_mail_shell_view_update_search_filter),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        tree_model, "row-deleted",
        G_CALLBACK (e_mail_shell_view_update_search_filter),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        tree_model, "row-inserted",
        G_CALLBACK (e_mail_shell_view_update_search_filter),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "key-press-event",
        G_CALLBACK (mail_shell_view_key_press_event_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "popup-event",
        G_CALLBACK (mail_shell_view_popup_event_cb),
        mail_shell_view, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "scroll",
        G_CALLBACK (mail_shell_view_scroll_cb),
        mail_shell_view,
        G_CONNECT_AFTER | G_CONNECT_SWAPPED);

    g_signal_connect_object (
        web_view, "status-message",
        G_CALLBACK (e_shell_taskbar_set_message),
        shell_taskbar, G_CONNECT_SWAPPED);

    g_signal_connect_object (
        mail_shell_view, "toggled",
        G_CALLBACK (e_mail_shell_view_update_send_receive_menus),
        mail_shell_view, G_CONNECT_AFTER | G_CONNECT_SWAPPED);

    /* Need to keep the handler ID so we can disconnect it in
     * dispose().  The shell outlives us and we don't want it
     * invoking callbacks on finalized shell views. */
    priv->prepare_for_quit_handler_id =
        g_signal_connect_object (
            shell, "prepare-for-quit",
            G_CALLBACK (mail_shell_view_prepare_for_quit_cb),
            mail_shell_view, G_CONNECT_SWAPPED);

    e_mail_reader_init (reader, TRUE, FALSE);
    e_mail_shell_view_actions_init (mail_shell_view);
    e_mail_shell_view_update_search_filter (mail_shell_view);

    /* This binding must come after e_mail_reader_init(). */
    g_object_bind_property (
        shell_content, "group-by-threads",
        mail_view, "group-by-threads",
        G_BINDING_BIDIRECTIONAL |
        G_BINDING_SYNC_CREATE);

    /* Populate built-in rules for search entry popup menu.
     * Keep the assertions, please.  If the conditions aren't
     * met we're going to crash anyway, just more mysteriously. */
    context = E_SHELL_VIEW_GET_CLASS (shell_view)->search_context;
    source = E_FILTER_SOURCE_DEMAND;
    while ((rule = e_rule_context_next_rule (context, rule, source))) {
        if (!rule->system)
            continue;
        g_assert (ii < MAIL_NUM_SEARCH_RULES);
        priv->search_rules[ii++] = g_object_ref (rule);
    }
    g_assert (ii == MAIL_NUM_SEARCH_RULES);

    /* Now that we're all set up, simulate selecting a folder. */
    g_signal_emit_by_name (selection, "changed");
}

void
e_mail_shell_view_private_dispose (EMailShellView *mail_shell_view)
{
    EMailShellViewPrivate *priv = mail_shell_view->priv;
    gint ii;

    /* XXX It's a little awkward to have to dig up the
     *     shell this late in the game.  Should we just
     *     keep a direct reference to it?  Not sure. */
    if (priv->prepare_for_quit_handler_id > 0) {
        EShellBackend *shell_backend;
        EShell *shell;

        shell_backend = E_SHELL_BACKEND (priv->mail_shell_backend);
        shell = e_shell_backend_get_shell (shell_backend);

        g_signal_handler_disconnect (
            shell, priv->prepare_for_quit_handler_id);
        priv->prepare_for_quit_handler_id = 0;
    }

    DISPOSE (priv->mail_shell_backend);
    DISPOSE (priv->mail_shell_content);
    DISPOSE (priv->mail_shell_sidebar);

    for (ii = 0; ii < MAIL_NUM_SEARCH_RULES; ii++)
        DISPOSE (priv->search_rules[ii]);

    if (priv->opening_folder != NULL) {
        g_cancellable_cancel (priv->opening_folder);
        g_object_unref (priv->opening_folder);
        priv->opening_folder = NULL;
    }

    if (priv->search_account_all != NULL) {
        g_object_unref (priv->search_account_all);
        priv->search_account_all = NULL;
    }

    if (priv->search_account_current != NULL) {
        g_object_unref (priv->search_account_current);
        priv->search_account_current = NULL;
    }

    if (priv->search_account_cancel != NULL) {
        g_object_unref (priv->search_account_cancel);
        priv->search_account_cancel = NULL;
    }
}

void
e_mail_shell_view_private_finalize (EMailShellView *mail_shell_view)
{
    /* XXX Nothing to do? */
}

void
e_mail_shell_view_restore_state (EMailShellView *mail_shell_view)
{
    EMailShellContent *mail_shell_content;
    EShellSearchbar *searchbar;
    EMailReader *reader;
    EMailView *mail_view;
    CamelFolder *folder;
    CamelVeeFolder *vee_folder;
    const gchar *old_state_group;
    gchar *folder_uri;
    gchar *new_state_group;

    /* XXX Move this to EMailShellContent. */

    g_return_if_fail (E_IS_MAIL_SHELL_VIEW (mail_shell_view));

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);
    searchbar = e_mail_shell_content_get_searchbar (mail_shell_content);

    reader = E_MAIL_READER (mail_view);
    folder = e_mail_reader_get_folder (reader);

    if (folder == NULL)
        return;

    /* Do not restore state if we're running a "Current Account"
     * or "All Accounts" search, since we don't want the search
     * criteria to be destroyed in those cases. */

    vee_folder = mail_shell_view->priv->search_account_all;
    if (vee_folder != NULL && folder == CAMEL_FOLDER (vee_folder))
        return;

    vee_folder = mail_shell_view->priv->search_account_current;
    if (vee_folder != NULL && folder == CAMEL_FOLDER (vee_folder))
        return;

    folder_uri = e_mail_folder_uri_from_folder (folder);
    new_state_group = g_strdup_printf ("Folder %s", folder_uri);
    old_state_group = e_shell_searchbar_get_state_group (searchbar);
    g_free (folder_uri);

    /* Avoid loading search state unnecessarily. */
    if (g_strcmp0 (new_state_group, old_state_group) != 0) {
        e_shell_searchbar_set_state_group (searchbar, new_state_group);
        e_shell_searchbar_load_state (searchbar);
    }

    g_free (new_state_group);
}

void
e_mail_shell_view_update_sidebar (EMailShellView *mail_shell_view)
{
    EMailShellContent *mail_shell_content;
    EShellSidebar *shell_sidebar;
    EShellView *shell_view;
    EMailReader *reader;
    EMailView *mail_view;
    CamelStore *local_store;
    CamelStore *parent_store;
    CamelFolder *folder;
    GPtrArray *uids;
    GString *buffer;
    const gchar *display_name;
    const gchar *folder_name;
    gchar *title;
    guint32 num_deleted;
    guint32 num_junked;
    guint32 num_junked_not_deleted;
    guint32 num_unread;
    guint32 num_visible;

    g_return_if_fail (E_IS_MAIL_SHELL_VIEW (mail_shell_view));

    mail_shell_content = mail_shell_view->priv->mail_shell_content;
    mail_view = e_mail_shell_content_get_mail_view (mail_shell_content);

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);

    reader = E_MAIL_READER (mail_view);
    folder = e_mail_reader_get_folder (reader);

    local_store = e_mail_local_get_store ();

    /* If no folder is selected, reset the sidebar banners
     * to their default values and stop. */
    if (folder == NULL) {
        GtkAction *action;
        gchar *label;

        action = e_shell_view_get_action (shell_view);

        g_object_get (action, "label", &label, NULL);
        e_shell_sidebar_set_secondary_text (shell_sidebar, NULL);
        e_shell_view_set_title (shell_view, label);
        g_free (label);

        return;
    }

    folder_name = camel_folder_get_display_name (folder);
    parent_store = camel_folder_get_parent_store (folder);

    num_deleted = folder->summary->deleted_count;
    num_junked = folder->summary->junk_count;
    num_junked_not_deleted = folder->summary->junk_not_deleted_count;
    num_unread = folder->summary->unread_count;
    num_visible = folder->summary->visible_count;

    buffer = g_string_sized_new (256);
    uids = e_mail_reader_get_selected_uids (reader);

    if (uids->len > 1)
        g_string_append_printf (
            buffer, ngettext ("%d selected, ", "%d selected, ",
            uids->len), uids->len);

    if (CAMEL_IS_VTRASH_FOLDER (folder)) {
        CamelVTrashFolder *trash_folder;

        trash_folder = (CamelVTrashFolder *) folder;

        /* "Trash" folder */
        if (trash_folder->type == CAMEL_VTRASH_FOLDER_TRASH)
            g_string_append_printf (
                buffer, ngettext ("%d deleted",
                "%d deleted", num_deleted), num_deleted);

        /* "Junk" folder (hide deleted messages) */
        else if (e_mail_reader_get_hide_deleted (reader))
            g_string_append_printf (
                buffer, ngettext ("%d junk",
                "%d junk", num_junked_not_deleted),
                num_junked_not_deleted);

        /* "Junk" folder (show deleted messages) */
        else
            g_string_append_printf (
                buffer, ngettext ("%d junk", "%d junk",
                num_junked), num_junked);

    /* "Drafts" folder */
    } else if (em_utils_folder_is_drafts (folder)) {
        g_string_append_printf (
            buffer, ngettext ("%d draft", "%d drafts",
            num_visible), num_visible);

    /* "Outbox" folder */
    } else if (em_utils_folder_is_outbox (folder)) {
        g_string_append_printf (
            buffer, ngettext ("%d unsent", "%d unsent",
            num_visible), num_visible);

    /* "Sent" folder */
    } else if (em_utils_folder_is_sent (folder)) {
        g_string_append_printf (
            buffer, ngettext ("%d sent", "%d sent",
            num_visible), num_visible);

    /* Normal folder */
    } else {
        if (!e_mail_reader_get_hide_deleted (reader))
            num_visible +=
                num_deleted - num_junked +
                num_junked_not_deleted;

        if (num_unread > 0 && uids->len <= 1)
            g_string_append_printf (
                buffer, ngettext ("%d unread, ",
                "%d unread, ", num_unread), num_unread);
        g_string_append_printf (
            buffer, ngettext ("%d total", "%d total",
            num_visible), num_visible);
    }

    em_utils_uids_free (uids);

    /* Choose a suitable folder name for displaying. */
    display_name = folder_name;
    if (parent_store == local_store) {
        if (strcmp (folder_name, "Drafts") == 0)
            display_name = _("Drafts");
        else if (strcmp (folder_name, "Inbox") == 0)
            display_name = _("Inbox");
        else if (strcmp (folder_name, "Outbox") == 0)
            display_name = _("Outbox");
        else if (strcmp (folder_name, "Sent") == 0)
            display_name = _("Sent");
        else if (strcmp (folder_name, "Templates") == 0)
            display_name = _("Templates");
        else if (strcmp (folder_name, "Trash") == 0)
            display_name = _("Trash");
    }
    if (strcmp (folder_name, "INBOX") == 0)
        display_name = _("Inbox");

    title = g_strdup_printf ("%s (%s)", display_name, buffer->str);
    e_shell_sidebar_set_secondary_text (shell_sidebar, buffer->str);
    e_shell_view_set_title (shell_view, title);
    g_free (title);

    g_string_free (buffer, TRUE);
}

void
e_mail_shell_view_send_receive (EMailShellView *mail_shell_view,
                                EMailSendReceiveMode mode,
                                const gchar *account_uid)
{
    EMailSession *session;
    EShellView *shell_view;
    EShellWindow *shell_window;
    EShellBackend *shell_backend;

    g_return_if_fail (mail_shell_view != NULL);

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_window = e_shell_view_get_shell_window (shell_view);
    shell_backend = e_shell_view_get_shell_backend (shell_view);

    session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));

    em_utils_clear_get_password_canceled_accounts_flag ();

    if (!account_uid) {
        switch (mode) {
        case E_MAIL_SEND_RECEIVE_BOTH:
            mail_send_receive (GTK_WINDOW (shell_window), session);
            break;
        case E_MAIL_SEND_RECEIVE_RECEIVE:
            mail_receive (GTK_WINDOW (shell_window), session);
            break;
        case E_MAIL_SEND_RECEIVE_SEND:
            mail_send (session);
            break;
        }
    } else {
        /* allow only receive on individual accounts */
        EAccount *account;

        account = e_get_account_by_uid (account_uid);
        g_return_if_fail (account != NULL);

        if (account->enabled && account->source != NULL)
            mail_receive_account (session, account);
    }
}

static GtkMenuItem *
send_receive_find_account_menu_item (GtkMenuShell *menu,
                                     EAccount *account)
{
    GList *children, *child;

    g_return_val_if_fail (menu != NULL, NULL);
    g_return_val_if_fail (account != NULL, NULL);
    g_return_val_if_fail (account->uid != NULL, NULL);

    children = gtk_container_get_children (GTK_CONTAINER (menu));

    for (child = children; child != NULL; child = child->next) {
        GObject *obj = child->data;
        const gchar *uid;

        if (!obj)
            continue;

        uid = g_object_get_data (obj, "e-account-uid");
        if (!uid)
            continue;

        if (g_strcmp0 (uid, account->uid) == 0) {
            g_list_free (children);

            return GTK_MENU_ITEM (obj);
        }
    }

    g_list_free (children);

    return NULL;
}

static gint
send_receive_get_account_index (EAccount *account)
{
    gint res;
    EAccountList *accounts;
    EIterator *iterator;

    g_return_val_if_fail (account != NULL, -1);

    accounts = e_get_account_list ();
    g_return_val_if_fail (accounts != NULL, -1);

    res = 0;
    for (iterator = e_list_get_iterator (E_LIST (accounts));
         e_iterator_is_valid (iterator);
         e_iterator_next (iterator)) {
        EAccount *candidate;
        const gchar *name;

        candidate = (EAccount *) e_iterator_get (iterator);

        if (candidate == NULL)
            continue;

        if (!candidate->enabled)
            continue;

        if (candidate->source == NULL)
            continue;

        if (candidate->source->url == NULL)
            continue;

        if (*candidate->source->url == '\0')
            continue;

        name = e_account_get_string (candidate, E_ACCOUNT_NAME);
        if (name == NULL || *name == '\0')
            continue;

        if (candidate->uid == NULL)
            continue;

        if (*candidate->uid == '\0')
            continue;

        if (g_strcmp0 (candidate->uid, account->uid) == 0) {
            g_object_unref (iterator);
            return res;
        }

        res++;
    }

    g_object_unref (iterator);

    return -1;
}

static void
send_receive_account_item_activate_cb (GtkMenuItem *item,
                                       GtkMenuShell *menu)
{
    EMailShellView *mail_shell_view;
    const gchar *account_uid;

    g_return_if_fail (item != NULL);
    g_return_if_fail (menu != NULL);

    mail_shell_view = g_object_get_data (G_OBJECT (menu), "mail-shell-view");
    g_return_if_fail (mail_shell_view != NULL);

    account_uid = g_object_get_data (G_OBJECT (item), "e-account-uid");
    g_return_if_fail (account_uid != NULL);

    e_mail_shell_view_send_receive (
        mail_shell_view, E_MAIL_SEND_RECEIVE_RECEIVE, account_uid);
}

static void
send_receive_add_to_menu (GtkMenuShell *menu,
                          EAccount *account,
                          gint insert_index)
{
    const gchar *name;
    GtkWidget *item;

    g_return_if_fail (menu != NULL);
    g_return_if_fail (account != NULL);

    if (send_receive_find_account_menu_item (menu, account) != NULL)
        return;

    if (account->source == NULL)
        return;

    if (account->source->url == NULL)
        return;

    if (*account->source->url == '\0')
        return;

    name = e_account_get_string (account, E_ACCOUNT_NAME);
    if (name == NULL || *name == '\0')
        return;

    if (account->uid == NULL)
        return;

    if (*account->uid == '\0')
        return;

    item = gtk_menu_item_new_with_label (name);
    gtk_widget_show (item);
    g_object_set_data_full (
        G_OBJECT (item), "e-account-uid",
        g_strdup (account->uid), g_free);
    g_signal_connect (
        item, "activate",
        G_CALLBACK (send_receive_account_item_activate_cb), menu);

    /* it's index between accounts, not in the menu */
    if (insert_index < 0)
        gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
    else
        gtk_menu_shell_insert (
            GTK_MENU_SHELL (menu), item, insert_index + 4);
}

static void
send_receive_remove_from_menu (GtkMenuShell *menu,
                               EAccount *account)
{
    GtkMenuItem *item;

    g_return_if_fail (menu != NULL);
    g_return_if_fail (account != NULL);

    item = send_receive_find_account_menu_item (menu, account);
    if (item == NULL)
        return;

    gtk_container_remove (GTK_CONTAINER (menu), GTK_WIDGET (item));
}

static void
send_receive_menu_account_added_cb (EAccountList *list,
                                    EAccount *account,
                                    GtkMenuShell *menu)
{
    g_return_if_fail (account != NULL);
    g_return_if_fail (menu != NULL);

    if (account->enabled)
        send_receive_add_to_menu (
            menu, account,
            send_receive_get_account_index (account));
}

static void
send_receive_menu_account_changed_cb (EAccountList *list,
                                      EAccount *account,
                                      GtkMenuShell *menu)
{
    g_return_if_fail (account != NULL);
    g_return_if_fail (menu != NULL);

    if (account->enabled) {
        GtkMenuItem *item;

        item = send_receive_find_account_menu_item (menu, account);

        if (item) {
            if (account->source == NULL ||
                account->source->url == NULL ||
                *account->source->url == '\0') {
                send_receive_remove_from_menu (menu, account);
            } else {
                const gchar *name;

                name = e_account_get_string (
                    account, E_ACCOUNT_NAME);
                if (name != NULL && *name != '\0')
                    gtk_menu_item_set_label (item, name);
            }
        } else {
            send_receive_add_to_menu (
                menu, account,
                send_receive_get_account_index (account));
        }
    } else {
        send_receive_remove_from_menu (menu, account);
    }
}

static void
send_receive_menu_account_removed_cb (EAccountList *list,
                                      EAccount *account,
                                      GtkMenuShell *menu)
{
    g_return_if_fail (account != NULL);
    g_return_if_fail (menu != NULL);

    send_receive_remove_from_menu (menu, account);
}

static void
menu_weak_ref_cb (gpointer accounts,
                  GObject *where_the_object_was)
{
    g_return_if_fail (accounts != NULL);

    g_signal_handlers_disconnect_matched (
        accounts, G_SIGNAL_MATCH_DATA,
        0, 0, NULL, NULL, where_the_object_was);
}

static GtkWidget *
create_send_receive_submenu (EMailShellView *mail_shell_view)
{
    EShellView *shell_view;
    EShellWindow *shell_window;
    EAccountList *accounts;
    GtkWidget *menu;
    GtkAccelGroup *accel_group;
    GtkUIManager *ui_manager;
    GtkAction *action;

    g_return_val_if_fail (mail_shell_view != NULL, NULL);

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_window = e_shell_view_get_shell_window (shell_view);

    accounts = e_get_account_list ();
    menu = gtk_menu_new ();
    ui_manager = e_shell_window_get_ui_manager (shell_window);
    accel_group = gtk_ui_manager_get_accel_group (ui_manager);

    action = e_shell_window_get_action (shell_window, "mail-send-receive");
    gtk_action_set_accel_group (action, accel_group);
    gtk_menu_shell_append (
        GTK_MENU_SHELL (menu),
        gtk_action_create_menu_item (action));

    action = e_shell_window_get_action (
        shell_window, "mail-send-receive-receive-all");
    gtk_action_set_accel_group (action, accel_group);
    gtk_menu_shell_append (
        GTK_MENU_SHELL (menu),
        gtk_action_create_menu_item (action));

    action = e_shell_window_get_action (
        shell_window, "mail-send-receive-send-all");
    gtk_action_set_accel_group (action, accel_group);
    gtk_menu_shell_append (
        GTK_MENU_SHELL (menu),
        gtk_action_create_menu_item (action));

    gtk_menu_shell_append (
        GTK_MENU_SHELL (menu),
        gtk_separator_menu_item_new ());

    if (accounts) {
        EIterator *iterator;

        for (iterator = e_list_get_iterator (E_LIST (accounts));
             e_iterator_is_valid (iterator);
             e_iterator_next (iterator)) {
            EAccount *account;

            account = (EAccount *) e_iterator_get (iterator);

            if (account == NULL)
                continue;

            if (!account->enabled)
                continue;

            send_receive_add_to_menu (
                GTK_MENU_SHELL (menu), account, -1);
        }

        g_object_unref (iterator);

        g_signal_connect (
            accounts, "account-added",
            G_CALLBACK (send_receive_menu_account_added_cb), menu);
        g_signal_connect (
            accounts, "account-changed",
            G_CALLBACK (send_receive_menu_account_changed_cb), menu);
        g_signal_connect (
            accounts, "account-removed",
            G_CALLBACK (send_receive_menu_account_removed_cb), menu);

        g_object_weak_ref (
            G_OBJECT (menu), menu_weak_ref_cb, accounts);
    }

    gtk_widget_show_all (menu);

    g_object_set_data (G_OBJECT (menu), "mail-shell-view", mail_shell_view);

    return menu;
}

void
e_mail_shell_view_update_send_receive_menus (EMailShellView *mail_shell_view)
{
    EMailShellViewPrivate *priv;
    EShellWindow *shell_window;
    EShellView *shell_view;
    GtkWidget *widget;
    const gchar *widget_path;

    g_return_if_fail (E_IS_MAIL_SHELL_VIEW (mail_shell_view));

    priv = mail_shell_view->priv;

    shell_view = E_SHELL_VIEW (mail_shell_view);
    shell_window = e_shell_view_get_shell_window (shell_view);

    if (!e_shell_view_is_active (shell_view)) {
        if (priv->send_receive_tool_item) {
            GtkWidget *toolbar;

            toolbar = e_shell_window_get_managed_widget (
                shell_window, "/main-toolbar");
            g_return_if_fail (toolbar != NULL);

            gtk_container_remove (
                GTK_CONTAINER (toolbar),
                GTK_WIDGET (priv->send_receive_tool_item));
            gtk_container_remove (
                GTK_CONTAINER (toolbar),
                GTK_WIDGET (priv->send_receive_tool_separator));

            priv->send_receive_tool_item = NULL;
            priv->send_receive_tool_separator = NULL;
        }

        return;
    }

    widget_path =
        "/main-menu/file-menu"
        "/mail-send-receiver/mail-send-receive-submenu";
    widget = e_shell_window_get_managed_widget (shell_window, widget_path);
    if (widget != NULL)
        gtk_menu_item_set_submenu (
            GTK_MENU_ITEM (widget),
            create_send_receive_submenu (mail_shell_view));

    if (!priv->send_receive_tool_item) {
        GtkWidget *toolbar;
        GtkToolItem *tool_item;
        gint index;

        toolbar = e_shell_window_get_managed_widget (
            shell_window, "/main-toolbar");
        g_return_if_fail (toolbar != NULL);

        widget_path =
            "/main-toolbar/toolbar-actions/mail-send-receiver";
        widget = e_shell_window_get_managed_widget (
            shell_window, widget_path);
        g_return_if_fail (widget != NULL);

        index = gtk_toolbar_get_item_index (
            GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (widget));

        tool_item = gtk_separator_tool_item_new ();
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), tool_item, index);
        gtk_widget_show (GTK_WIDGET (tool_item));
        priv->send_receive_tool_separator = tool_item;

        tool_item = GTK_TOOL_ITEM (
            e_menu_tool_button_new (_("Send / Receive")));
        gtk_tool_item_set_is_important (tool_item, TRUE);
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), tool_item, index);
        gtk_widget_show (GTK_WIDGET (tool_item));
        priv->send_receive_tool_item = tool_item;

        g_object_bind_property (
            ACTION (MAIL_SEND_RECEIVE), "sensitive",
            tool_item, "sensitive",
            G_BINDING_SYNC_CREATE);
    }

    if (priv->send_receive_tool_item)
        gtk_menu_tool_button_set_menu (
            GTK_MENU_TOOL_BUTTON (priv->send_receive_tool_item),
            create_send_receive_submenu (mail_shell_view));
}