aboutsummaryrefslogtreecommitdiffstats
path: root/libemail-engine/mail-ops.c
blob: df90596e530efbc60c1d9fd94cb7eb5e4ea97df2 (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
/*
 * mail-ops.c: callbacks for the mail toolbar/menus
 *
 * 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/>
 *
 *
 * Authors:
 *      Dan Winship <danw@ximian.com>
 *      Jeffrey Stedfast <fejj@ximian.com>
 *      Peter Williams <peterw@ximian.com>
 *      Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <errno.h>

#include <glib/gstdio.h>
#include <glib/gi18n.h>

#include <libedataserver/libedataserver.h>

#include <libemail-engine/mail-mt.h>

#include "e-mail-utils.h"
#include "mail-ops.h"
#include "mail-tools.h"

#include "e-mail-folder-utils.h"
#include "e-mail-session.h"
#include "e-mail-session-utils.h"

#define w(x)
#define d(x)

/* XXX Make this a preprocessor definition. */
const gchar *x_mailer = "Evolution " VERSION SUB_VERSION " " VERSION_COMMENT;

/* used for both just filtering a folder + uid's, and for filtering a whole folder */
/* used both for fetching mail, and for filtering mail */
struct _filter_mail_msg {
    MailMsg base;

    EMailSession *session;
    CamelFolder *source_folder; /* where they come from */
    GPtrArray *source_uids;     /* uids to copy, or NULL == copy all */
    CamelUIDCache *cache;       /* UID cache if we are to cache
                     * the uids, NULL otherwise */
    CamelFilterDriver *driver;
    gint delete;            /* delete messages after filtering? */
    CamelFolder *destination;   /* default destination for any
                     * messages, NULL for none */
};

/* since fetching also filters, we subclass the data here */
struct _fetch_mail_msg {
    struct _filter_mail_msg fmsg;

    CamelStore *store;
    GCancellable *cancellable;  /* we have our own cancellation
                     * struct, the other should be empty */
    gint keep;          /* keep on server? */

    MailProviderFetchLockFunc provider_lock;
    MailProviderFetchUnlockFunc provider_unlock;
    MailProviderFetchInboxFunc provider_fetch_inbox;

    void (*done)(gpointer data);
    gpointer data;
};

static gchar *
em_filter_folder_element_desc (struct _filter_mail_msg *m)
{
    return g_strdup (_("Filtering Selected Messages"));
}

/* filter a folder, or a subset thereof, uses source_folder/source_uids */
/* this is shared with fetch_mail */
static gboolean
em_filter_folder_element_exec (struct _filter_mail_msg *m,
                               GCancellable *cancellable,
                               GError **error)
{
    CamelFolder *folder;
    GPtrArray *uids, *folder_uids = NULL;
    gboolean success = TRUE;
    GError *local_error = NULL;

    folder = m->source_folder;

    if (folder == NULL || camel_folder_get_message_count (folder) == 0)
        return success;

    if (m->destination) {
        camel_folder_freeze (m->destination);
        camel_filter_driver_set_default_folder (m->driver, m->destination);
    }

    camel_folder_freeze (folder);

    if (m->source_uids)
        uids = m->source_uids;
    else
        folder_uids = uids = camel_folder_get_uids (folder);

    success = camel_filter_driver_filter_folder (
        m->driver, folder, m->cache, uids, m->delete,
        cancellable, &local_error) == 0;
    camel_filter_driver_flush (m->driver, &local_error);

    if (folder_uids)
        camel_folder_free_uids (folder, folder_uids);

    /* sync our source folder */
    if (!m->cache && !local_error)
        camel_folder_synchronize_sync (
            folder, FALSE, cancellable, &local_error);
    camel_folder_thaw (folder);

    if (m->destination)
        camel_folder_thaw (m->destination);

    /* this may thaw/unref source folders, do it here so we dont do
     * it in the main thread see also fetch_mail_fetch () below */
    g_object_unref (m->driver);
    m->driver = NULL;

    if (g_error_matches (local_error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_URL_INVALID) ||
        g_error_matches (local_error, CAMEL_FOLDER_ERROR, CAMEL_FOLDER_ERROR_INVALID)) {
        g_set_error (
            error, local_error->domain, local_error->code,
            _("Failed to filter selected messages. One reason can be that folder "
            "location set in one or more filters is invalid. Please check your "
            "filters in Edit->Message Filters.\n"
            "Original error was: %s"), local_error->message);
        g_clear_error (&local_error);
    } else if (local_error)
        g_propagate_error (error, local_error);

    return success;
}

static void
em_filter_folder_element_done (struct _filter_mail_msg *m)
{
}

static void
em_filter_folder_element_free (struct _filter_mail_msg *m)
{
    mail_session_flush_filter_log (m->session);

    if (m->session)
        g_object_unref (m->session);

    if (m->source_folder)
        g_object_unref (m->source_folder);

    if (m->source_uids)
        g_ptr_array_unref (m->source_uids);

    if (m->destination)
        g_object_unref (m->destination);

    if (m->driver)
        g_object_unref (m->driver);
}

static MailMsgInfo em_filter_folder_element_info = {
    sizeof (struct _filter_mail_msg),
    (MailMsgDescFunc) em_filter_folder_element_desc,
    (MailMsgExecFunc) em_filter_folder_element_exec,
    (MailMsgDoneFunc) em_filter_folder_element_done,
    (MailMsgFreeFunc) em_filter_folder_element_free
};

void
mail_filter_folder (EMailSession *session,
                    CamelFolder *source_folder,
                    GPtrArray *uids,
                    const gchar *type,
                    gboolean notify)
{
    struct _filter_mail_msg *m;

    m = mail_msg_new (&em_filter_folder_element_info);
    m->session = g_object_ref (session);
    m->source_folder = g_object_ref (source_folder);
    m->source_uids = g_ptr_array_ref (uids);
    m->cache = NULL;
    m->delete = FALSE;

    m->driver = camel_session_get_filter_driver (
        CAMEL_SESSION (session), type, NULL);

    if (!notify) {
        /* FIXME: have a #define NOTIFY_FILTER_NAME macro? */
        /* the filter name has to stay in sync
         * with mail-session::get_filter_driver */
        camel_filter_driver_remove_rule_by_name (
            m->driver, "new-mail-notification");
    }

    mail_msg_unordered_push (m);
}

/* ********************************************************************** */

static gchar *
fetch_mail_desc (struct _fetch_mail_msg *m)
{
    return g_strdup_printf (
        _("Fetching mail from '%s'"),
        camel_service_get_display_name (CAMEL_SERVICE (m->store)));
}

static void
fetch_mail_exec (struct _fetch_mail_msg *m,
                 GCancellable *cancellable,
                 GError **error)
{
    struct _filter_mail_msg *fm = (struct _filter_mail_msg *) m;
    GObjectClass *class;
    CamelFolder *folder = NULL;
    CamelProvider *provider;
    CamelService *service;
    CamelSession *session;
    CamelSettings *settings;
    CamelStore *parent_store;
    CamelUIDCache *cache = NULL;
    CamelURL *url;
    gboolean keep = TRUE;
    gboolean delete_fetched;
    gboolean is_local_delivery = FALSE;
    const gchar *uid = NULL;
    const gchar *data_dir;
    gchar *cachename;
    gint i;

    service = CAMEL_SERVICE (m->store);
    session = camel_service_ref_session (service);
    provider = camel_service_get_provider (service);

    if (provider && (provider->flags & CAMEL_PROVIDER_IS_REMOTE) != 0 &&
        !camel_session_get_online (session))
        goto exit;

    fm->destination = e_mail_session_get_local_folder (
        E_MAIL_SESSION (session), E_MAIL_LOCAL_FOLDER_LOCAL_INBOX);
    if (fm->destination == NULL)
        goto exit;
    g_object_ref (fm->destination);

    uid = camel_service_get_uid (service);
    settings = camel_service_ref_settings (service);

    /* XXX This is a POP3-specific setting. */
    class = G_OBJECT_GET_CLASS (settings);
    if (g_object_class_find_property (class, "keep-on-server") != NULL)
        g_object_get (settings, "keep-on-server", &keep, NULL);

    g_object_unref (settings);

    /* Just for readability. */
    delete_fetched = !keep;

    url = camel_service_new_camel_url (service);
    is_local_delivery = em_utils_is_local_delivery_mbox_file (url);

    if (is_local_delivery) {
        gchar *path;
        gchar *url_string;

        path = mail_tool_do_movemail (m->store, error);
        url_string = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);

        if (path && (!error || !*error)) {
            camel_folder_freeze (fm->destination);
            camel_filter_driver_set_default_folder (
                fm->driver, fm->destination);
            camel_filter_driver_filter_mbox (
                fm->driver, path, url_string,
                cancellable, error);
            camel_folder_thaw (fm->destination);

            if (!error || !*error)
                g_unlink (path);
        }

        g_free (path);
        g_free (url_string);
    } else {
        uid = camel_service_get_uid (service);
        if (m->provider_lock)
            m->provider_lock (uid);

        folder = fm->source_folder =
            e_mail_session_get_inbox_sync (
                fm->session, uid, cancellable, error);
    }

    camel_url_free (url);

    if (folder == NULL)
        goto exit;

    parent_store = camel_folder_get_parent_store (folder);

    service = CAMEL_SERVICE (parent_store);
    data_dir = camel_service_get_user_data_dir (service);

    cachename = g_build_filename (data_dir, "uid-cache", NULL);
    cache = camel_uid_cache_new (cachename);
    g_free (cachename);

    if (cache) {
        GPtrArray *folder_uids, *cache_uids, *uids;
        GError *local_error = NULL;

        if (m->provider_fetch_inbox) {
            g_object_unref (fm->destination);
            fm->destination = m->provider_fetch_inbox (uid, cancellable, &local_error);
            if (fm->destination == NULL)
                goto exit;
            g_object_ref (fm->destination);
        }

        if (!local_error && !g_cancellable_is_cancelled (cancellable)) {
            folder_uids = camel_folder_get_uids (folder);
            cache_uids = camel_uid_cache_get_new_uids (cache, folder_uids);

            if (cache_uids) {
                gboolean success;

                /* need to copy this, sigh */
                fm->source_uids = uids = g_ptr_array_new ();
                g_ptr_array_set_size (uids, cache_uids->len);

                /* Reverse it so that we fetch the latest as first, while fetching POP  */
                for (i = 0; i < cache_uids->len; i++) {
                    uids->pdata[cache_uids->len - i - 1] = g_strdup (cache_uids->pdata[i]);
                }

                fm->cache = cache;

                success = em_filter_folder_element_exec (fm, cancellable, &local_error);

                /* need to uncancel so writes/etc. don't fail */
                if (g_cancellable_is_cancelled (m->cancellable))
                    g_cancellable_reset (m->cancellable);

                if (!success) {
                    GPtrArray *uncached_uids;
                    GHashTable *uncached_hash;

                    uncached_uids = camel_folder_get_uncached_uids (folder, cache_uids, NULL);
                    uncached_hash = g_hash_table_new (g_str_hash, g_str_equal);

                    for (i = 0; uncached_uids && i < uncached_uids->len; i++) {
                        g_hash_table_insert (uncached_hash, uncached_uids->pdata[i], uncached_uids->pdata[i]);
                    }

                    /* re-enter known UIDs, thus they are not
                     * re-fetched next time */
                    for (i = 0; i < cache_uids->len; i++) {
                        /* skip uncached UIDs */
                        if (!g_hash_table_lookup (uncached_hash, cache_uids->pdata[i]))
                            camel_uid_cache_save_uid (cache, cache_uids->pdata[i]);
                    }

                    g_hash_table_destroy (uncached_hash);
                    camel_folder_free_uids (folder, uncached_uids);
                }

                /* save the cache of uids that we've just downloaded */
                camel_uid_cache_save (cache);

                camel_uid_cache_free_uids (cache_uids);
            }

            if (delete_fetched && !local_error) {
                /* not keep on server - just delete all
                 * the actual messages on the server */
                for (i = 0; i < folder_uids->len; i++) {
                    camel_folder_delete_message (
                        folder, folder_uids->pdata[i]);
                }
            }

            if ((delete_fetched || cache_uids) && !local_error) {
                /* expunge messages (downloaded so far) */
                /* FIXME Not passing a GCancellable or GError here. */
                camel_folder_synchronize_sync (
                    folder, delete_fetched, NULL, NULL);
            }

            camel_folder_free_uids (folder, folder_uids);
        }

        camel_uid_cache_destroy (cache);

        if (local_error)
            g_propagate_error (error, local_error);
    } else {
        em_filter_folder_element_exec (fm, cancellable, error);
    }

    /* we unref the source folder here since we
     * may now block in finalize (we try to
     * disconnect cleanly) */
    g_object_unref (fm->source_folder);
    fm->source_folder = NULL;

exit:
    if (!is_local_delivery && m->provider_unlock)
        m->provider_unlock (uid);

    /* we unref this here as it may have more work to do (syncing
     * folders and whatnot) before we are really done */
    /* should this be cancellable too? (i.e. above unregister above) */
    if (fm->driver) {
        g_object_unref (fm->driver);
        fm->driver = NULL;
    }

    /* also disconnect if not a local delivery mbox;
     * there is no need to keep the connection alive forever */
    if (!is_local_delivery) {
        gboolean was_cancelled;

        was_cancelled = g_cancellable_is_cancelled (cancellable);

        /* pity, but otherwise it doesn't disconnect */
        if (was_cancelled)
            g_cancellable_reset (cancellable);

        camel_service_disconnect_sync (
            service, !was_cancelled, cancellable, NULL);
    }

    g_object_unref (session);
}

static void
fetch_mail_done (struct _fetch_mail_msg *m)
{
    if (m->done)
        m->done (m->data);
}

static void
fetch_mail_free (struct _fetch_mail_msg *m)
{
    if (m->store != NULL)
        g_object_unref (m->store);

    if (m->cancellable != NULL)
        g_object_unref (m->cancellable);

    em_filter_folder_element_free ((struct _filter_mail_msg *) m);
}

static MailMsgInfo fetch_mail_info = {
    sizeof (struct _fetch_mail_msg),
    (MailMsgDescFunc) fetch_mail_desc,
    (MailMsgExecFunc) fetch_mail_exec,
    (MailMsgDoneFunc) fetch_mail_done,
    (MailMsgFreeFunc) fetch_mail_free
};

/* ouch, a 'do everything' interface ... */
void
mail_fetch_mail (CamelStore *store,
                 const gchar *type,
                 MailProviderFetchLockFunc lock_func,
                 MailProviderFetchUnlockFunc unlock_func,
                 MailProviderFetchInboxFunc fetch_inbox_func,
                 GCancellable *cancellable,
                 CamelFilterGetFolderFunc get_folder,
                 gpointer get_data,
                 CamelFilterStatusFunc *status,
                 gpointer status_data,
                 void (*done)(gpointer data),
                 gpointer data)
{
    struct _fetch_mail_msg *m;
    struct _filter_mail_msg *fm;
    CamelSession *session;

    g_return_if_fail (CAMEL_IS_STORE (store));

    session = camel_service_ref_session (CAMEL_SERVICE (store));

    m = mail_msg_new (&fetch_mail_info);
    fm = (struct _filter_mail_msg *) m;
    fm->session = g_object_ref (session);
    m->store = g_object_ref (store);
    fm->cache = NULL;
    if (cancellable)
        m->cancellable = g_object_ref (cancellable);
    m->done = done;
    m->data = data;

    m->provider_lock = lock_func;
    m->provider_unlock = unlock_func;
    m->provider_fetch_inbox = fetch_inbox_func;

    fm->driver = camel_session_get_filter_driver (session, type, NULL);
    camel_filter_driver_set_folder_func (fm->driver, get_folder, get_data);
    if (status)
        camel_filter_driver_set_status_func (fm->driver, status, status_data);

    mail_msg_unordered_push (m);

    g_object_unref (session);
}

/* ********************************************************************** */
/* sending stuff */
/* ** SEND MAIL *********************************************************** */

static const gchar *normal_recipients[] = {
    CAMEL_RECIPIENT_TYPE_TO,
    CAMEL_RECIPIENT_TYPE_CC,
    CAMEL_RECIPIENT_TYPE_BCC
};

static const gchar *resent_recipients[] = {
    CAMEL_RECIPIENT_TYPE_RESENT_TO,
    CAMEL_RECIPIENT_TYPE_RESENT_CC,
    CAMEL_RECIPIENT_TYPE_RESENT_BCC
};

struct _send_queue_msg {
    MailMsg base;

    EMailSession *session;
    CamelFolder *queue;
    CamelTransport *transport;

    CamelFilterDriver *driver;

    /* we use camelfilterstatusfunc, even though its not the filter doing it */
    CamelFilterStatusFunc *status;
    gpointer status_data;

    void (*done)(gpointer data);
    gpointer data;
};

static void report_status       (struct _send_queue_msg *m,
                     enum camel_filter_status_t status,
                     gint pc,
                     const gchar *desc,
                     ...);

/* send 1 message to a specific transport */
static void
mail_send_message (struct _send_queue_msg *m,
                   CamelFolder *queue,
                   const gchar *uid,
                   CamelTransport *transport,
                   CamelFilterDriver *driver,
                   GCancellable *cancellable,
                   GError **error)
{
    CamelService *service;
    const CamelInternetAddress *iaddr;
    CamelAddress *from, *recipients;
    CamelMessageInfo *info = NULL;
    CamelProvider *provider = NULL;
    const gchar *resent_from;
    CamelFolder *folder = NULL;
    GString *err = NULL;
    struct _camel_header_raw *xev, *header;
    CamelMimeMessage *message;
    gint i;
    GError *local_error = NULL;

    message = camel_folder_get_message_sync (
        queue, uid, cancellable, error);
    if (!message)
        return;

    camel_medium_set_header (CAMEL_MEDIUM (message), "X-Mailer", x_mailer);

    /* Do this before removing "X-Evolution" headers. */
    service = e_mail_session_ref_transport_for_message (
        m->session, message);
    if (service != NULL)
        provider = camel_service_get_provider (service);

    err = g_string_new ("");
    xev = mail_tool_remove_xevolution_headers (message);

    if (CAMEL_IS_TRANSPORT (service)) {
        const gchar *tuid;

        /* Let the dialog know the right account it is using. */
        tuid = camel_service_get_uid (CAMEL_SERVICE (transport));
        report_status (m, CAMEL_FILTER_STATUS_ACTION, 0, tuid);
    }

    /* Check for email sending */
    from = (CamelAddress *) camel_internet_address_new ();
    resent_from = camel_medium_get_header (
        CAMEL_MEDIUM (message), "Resent-From");
    if (resent_from != NULL) {
        camel_address_decode (from, resent_from);
    } else {
        iaddr = camel_mime_message_get_from (message);
        camel_address_copy (from, CAMEL_ADDRESS (iaddr));
    }

    recipients = (CamelAddress *) camel_internet_address_new ();
    for (i = 0; i < 3; i++) {
        const gchar *type;

        if (resent_from != NULL)
            type = resent_recipients[i];
        else
            type = normal_recipients[i];
        iaddr = camel_mime_message_get_recipients (message, type);
        camel_address_cat (recipients, CAMEL_ADDRESS (iaddr));
    }

    if (camel_address_length (recipients) > 0) {
        if (provider && (provider->flags & CAMEL_PROVIDER_IS_REMOTE) != 0 &&
            !camel_session_get_online (CAMEL_SESSION (m->session))) {
            /* silently ignore */
            goto exit;
        }

        if (!camel_service_connect_sync (
            service, cancellable, error))
            goto exit;

        /* expand, or remove empty, group addresses */
        em_utils_expand_groups (CAMEL_INTERNET_ADDRESS (recipients));

        if (!camel_transport_send_to_sync (
            CAMEL_TRANSPORT (service), message,
            from, recipients, cancellable, error))
            goto exit;
    }

    /* Now check for posting, failures are ignored */
    info = camel_message_info_new (NULL);
    camel_message_info_set_flags (info, CAMEL_MESSAGE_SEEN, ~0);

    for (header = xev; header; header = header->next) {
        gchar *uri;

        if (strcmp (header->name, "X-Evolution-PostTo") != 0)
            continue;

        /* TODO: don't lose errors */

        uri = g_strstrip (g_strdup (header->value));
        /* FIXME Not passing a GCancellable or GError here. */
        folder = e_mail_session_uri_to_folder_sync (
            m->session, uri, 0, NULL, NULL);
        if (folder != NULL) {
            /* FIXME Not passing a GCancellable or GError here. */
            camel_folder_append_message_sync (
                folder, message, info, NULL, NULL, NULL);
            g_object_unref (folder);
            folder = NULL;
        }
        g_free (uri);
    }

    /* post process */
    mail_tool_restore_xevolution_headers (message, xev);

    if (driver) {
        camel_filter_driver_filter_message (
            driver, message, info, NULL, NULL,
            NULL, "", cancellable, &local_error);

        if (local_error != NULL) {
            if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                goto exit;

            /* sending mail, filtering failed */
            if (g_error_matches (local_error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_URL_INVALID) ||
                g_error_matches (local_error, CAMEL_FOLDER_ERROR, CAMEL_FOLDER_ERROR_INVALID)) {
                g_string_append_printf (
                    err,
                    _("Failed to apply outgoing filters. One reason can be that folder "
                    "location set in one or more filters is invalid. Please check your "
                    "filters in Edit->Message Filters.\n"
                    "Original error was: %s"), local_error->message);
            } else {
                g_string_append_printf (
                    err, _("Failed to apply outgoing filters: %s"),
                    local_error->message);
            }

            g_clear_error (&local_error);
        }
    }

    if (provider == NULL
        || !(provider->flags & CAMEL_PROVIDER_DISABLE_SENT_FOLDER)) {
        CamelFolder *local_sent_folder;
        GError *local_error = NULL;

        local_sent_folder = e_mail_session_get_local_folder (
            m->session, E_MAIL_LOCAL_FOLDER_SENT);

        folder = e_mail_session_get_fcc_for_message_sync (
            m->session, message, cancellable, &local_error);

        /* Sanity check. */
        g_return_if_fail (
            ((folder == NULL) && (local_error != NULL)) ||
            ((folder != NULL) && (local_error == NULL)));

        if (local_error == NULL)
            camel_folder_append_message_sync (
                folder, message, info, NULL,
                cancellable, &local_error);

        if (g_error_matches (
            local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
            goto exit;

        if (local_error != NULL && folder != local_sent_folder) {

            if (folder != NULL) {
                const gchar *description;

                description =
                    camel_folder_get_description (folder);
                if (err->len > 0)
                    g_string_append (err, "\n\n");
                g_string_append_printf (
                    err,
                    _("Failed to append to %s: %s\n"
                    "Appending to local 'Sent' folder instead."),
                    description,
                    local_error->message);

                g_object_unref (folder);
            }

            g_clear_error (&local_error);
            folder = g_object_ref (local_sent_folder);

            camel_folder_append_message_sync (
                folder, message, info, NULL,
                cancellable, &local_error);

            if (g_error_matches (
                local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                goto exit;

            if (local_error != NULL) {
                if (err->len > 0)
                    g_string_append (err, "\n\n");
                g_string_append_printf (
                    err,
                    _("Failed to append to "
                    "local 'Sent' folder: %s"),
                    local_error->message);
            }
        }
    }

    if (local_error == NULL) {
        /* Mark the draft message for deletion, if present. */
        e_mail_session_handle_draft_headers_sync (
            m->session, message, cancellable, &local_error);
        if (local_error != NULL) {
            g_warning (
                "%s: Failed to handle draft headers: %s",
                G_STRFUNC, local_error->message);
            g_clear_error (&local_error);
        }

        /* Set flags on the original source message, if present.
         * Source message refers to the message being forwarded
         * or replied to. */
        e_mail_session_handle_source_headers_sync (
            m->session, message, cancellable, &local_error);
        if (local_error != NULL) {
            g_warning (
                "%s: Failed to handle source headers: %s",
                G_STRFUNC, local_error->message);
            g_clear_error (&local_error);
        }
    }

    if (local_error == NULL) {
        camel_folder_set_message_flags (
            queue, uid, CAMEL_MESSAGE_DELETED |
            CAMEL_MESSAGE_SEEN, ~0);
        /* Sync it to disk, since if it crashes in between,
         * we keep sending it again on next start. */
        /* FIXME Not passing a GCancellable or GError here. */
        camel_folder_synchronize_sync (queue, FALSE, NULL, NULL);
    }

    if (err->len > 0) {
        /* set the culmulative exception report */
        g_set_error (
            &local_error, CAMEL_ERROR,
            CAMEL_ERROR_GENERIC, "%s", err->str);
    }

exit:
    if (local_error != NULL)
        g_propagate_error (error, local_error);

    /* FIXME Not passing a GCancellable or GError here. */
    if (folder != NULL) {
        camel_folder_synchronize_sync (folder, FALSE, NULL, NULL);
        g_object_unref (folder);
    }

    if (info != NULL)
        camel_message_info_unref (info);

    if (service != NULL)
        g_object_unref (service);

    g_object_unref (recipients);
    g_object_unref (from);
    camel_header_raw_clear (&xev);
    g_string_free (err, TRUE);
    g_object_unref (message);
}

/* ** SEND MAIL QUEUE ***************************************************** */

static void
report_status (struct _send_queue_msg *m,
               enum camel_filter_status_t status,
               gint pc,
               const gchar *desc,
               ...)
{
    va_list ap;
    gchar *str;

    if (m->status) {
        va_start (ap, desc);
        str = g_strdup_vprintf (desc, ap);
        va_end (ap);
        m->status (m->driver, status, pc, str, m->status_data);
        g_free (str);
    }
}

static void
send_queue_exec (struct _send_queue_msg *m,
                 GCancellable *cancellable,
                 GError **error)
{
    CamelFolder *sent_folder;
    GPtrArray *uids, *send_uids = NULL;
    gint i, j;
    GError *local_error = NULL;

    d (printf ("sending queue\n"));

    sent_folder =
        e_mail_session_get_local_folder (
        m->session, E_MAIL_LOCAL_FOLDER_SENT);

    if (!(uids = camel_folder_get_uids (m->queue)))
        return;

    send_uids = g_ptr_array_sized_new (uids->len);
    for (i = 0, j = 0; i < uids->len; i++) {
        CamelMessageInfo *info;

        info = camel_folder_get_message_info (m->queue, uids->pdata[i]);
        if (info) {
            if ((camel_message_info_flags (info) & CAMEL_MESSAGE_DELETED) == 0)
                send_uids->pdata[j++] = uids->pdata[i];
            camel_message_info_unref (info);
        }
    }

    send_uids->len = j;
    if (send_uids->len == 0) {
        /* nothing to send */
        camel_folder_free_uids (m->queue, uids);
        g_ptr_array_free (send_uids, TRUE);
        return;
    }

    camel_operation_push_message (cancellable, _("Sending message"));

    /* NB: This code somewhat abuses the 'exception' stuff.  Apart from
     *     fatal problems, it is also used as a mechanism to accumualte
     *     warning messages and present them back to the user. */

    for (i = 0, j = 0; i < send_uids->len; i++) {
        gint pc = (100 * i) / send_uids->len;

        report_status (
            m, CAMEL_FILTER_STATUS_START, pc,
            _("Sending message %d of %d"), i + 1,
            send_uids->len);

        camel_operation_progress (
            cancellable, (i + 1) * 100 / send_uids->len);

        mail_send_message (
            m, m->queue, send_uids->pdata[i], m->transport,
            m->driver, cancellable, &local_error);
        if (local_error != NULL) {
            if (!g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
                /* merge exceptions into one */
                if (m->base.error != NULL) {
                    gchar *old_message;

                    old_message = g_strdup (
                        m->base.error->message);
                    g_clear_error (&m->base.error);
                    g_set_error (
                        &m->base.error, CAMEL_ERROR,
                        CAMEL_ERROR_GENERIC,
                        "%s\n\n%s", old_message,
                        local_error->message);
                    g_free (old_message);

                    g_clear_error (&local_error);
                } else {
                    g_propagate_error (&m->base.error, local_error);
                    local_error = NULL;
                }

                /* keep track of the number of failures */
                j++;
            } else {
                /* transfer the USER_CANCEL error to the
                 * async op exception and then break */
                g_propagate_error (&m->base.error, local_error);
                local_error = NULL;
                break;
            }
        }
    }

    j += (send_uids->len - i);

    if (j > 0)
        report_status (
            m, CAMEL_FILTER_STATUS_END, 100,
            /* Translators: The string is distinguished by total
             * count of messages to be sent.  Failed messages is
             * always more than zero. */
            ngettext (
                "Failed to send a message",
                "Failed to send %d of %d messages",
                send_uids->len),
            j, send_uids->len);
    else if (g_error_matches (
            m->base.error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        report_status (m, CAMEL_FILTER_STATUS_END, 100, _("Canceled."));
    else
        report_status (m, CAMEL_FILTER_STATUS_END, 100, _("Complete."));

    if (m->driver) {
        g_object_unref (m->driver);
        m->driver = NULL;
    }

    camel_folder_free_uids (m->queue, uids);
    g_ptr_array_free (send_uids, TRUE);

    /* FIXME Not passing a GCancellable or GError here. */
    if (j <= 0 && m->base.error == NULL)
        camel_folder_synchronize_sync (m->queue, TRUE, NULL, NULL);

    /* FIXME Not passing a GCancellable or GError here. */
    if (sent_folder)
        camel_folder_synchronize_sync (sent_folder, FALSE, NULL, NULL);

    camel_operation_pop_message (cancellable);
}

static void
send_queue_done (struct _send_queue_msg *m)
{
    if (m->done)
        m->done (m->data);
}

static gchar *
send_queue_desc (struct _send_queue_msg *m)
{
    return g_strdup (_("Sending message"));
}

static void
send_queue_free (struct _send_queue_msg *m)
{
    if (m->session != NULL)
        g_object_unref (m->session);
    if (m->driver != NULL)
        g_object_unref (m->driver);
    if (m->transport != NULL)
        g_object_unref (m->transport);
    g_object_unref (m->queue);
}

static MailMsgInfo send_queue_info = {
    sizeof (struct _send_queue_msg),
    (MailMsgDescFunc) send_queue_desc,
    (MailMsgExecFunc) send_queue_exec,
    (MailMsgDoneFunc) send_queue_done,
    (MailMsgFreeFunc) send_queue_free
};

/* same interface as fetch_mail, just 'cause i'm lazy today
 * (and we need to run it from the same spot?) */
void
mail_send_queue (EMailSession *session,
                 CamelFolder *queue,
                 CamelTransport *transport,
                 const gchar *type,
                 GCancellable *cancellable,
                 CamelFilterGetFolderFunc get_folder,
                 gpointer get_data,
                 CamelFilterStatusFunc *status,
                 gpointer status_data,
                 void (*done)(gpointer data),
                 gpointer data)
{
    struct _send_queue_msg *m;

    g_return_if_fail (E_IS_MAIL_SESSION (session));

    m = mail_msg_new (&send_queue_info);
    m->session = g_object_ref (session);
    m->queue = g_object_ref (queue);
    m->transport = g_object_ref (transport);
    if (G_IS_CANCELLABLE (cancellable))
        m->base.cancellable = g_object_ref (cancellable);
    m->status = status;
    m->status_data = status_data;
    m->done = done;
    m->data = data;

    m->driver = camel_session_get_filter_driver (
        CAMEL_SESSION (session), type, NULL);
    camel_filter_driver_set_folder_func (m->driver, get_folder, get_data);

    mail_msg_unordered_push (m);
}

/* ** TRANSFER MESSAGES **************************************************** */

struct _transfer_msg {
    MailMsg base;

    EMailSession *session;
    CamelFolder *source;
    GPtrArray *uids;
    gboolean delete;
    gchar *dest_uri;
    guint32 dest_flags;

    void (*done)(gboolean ok, gpointer data);
    gpointer data;
};

static gchar *
transfer_messages_desc (struct _transfer_msg *m)
{
    return g_strdup_printf (
        m->delete ?
            _("Moving messages to '%s'") :
            _("Copying messages to '%s'"),
        m->dest_uri);

}

static void
transfer_messages_exec (struct _transfer_msg *m,
                        GCancellable *cancellable,
                        GError **error)
{
    CamelFolder *dest;

    dest = e_mail_session_uri_to_folder_sync (
        m->session, m->dest_uri, m->dest_flags,
        cancellable, error);
    if (dest == NULL)
        return;

    if (dest == m->source) {
        g_object_unref (dest);
        /* no-op */
        return;
    }

    camel_folder_freeze (m->source);
    camel_folder_freeze (dest);

    camel_folder_transfer_messages_to_sync (
        m->source, m->uids, dest, m->delete, NULL,
        cancellable, error);

    /* make sure all deleted messages are marked as seen */

    if (m->delete) {
        gint i;

        for (i = 0; i < m->uids->len; i++)
            camel_folder_set_message_flags (
                m->source, m->uids->pdata[i],
                CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);
    }

    camel_folder_thaw (m->source);
    camel_folder_thaw (dest);

    /* FIXME Not passing a GCancellable or GError here. */
    camel_folder_synchronize_sync (dest, FALSE, NULL, NULL);
    g_object_unref (dest);
}

static void
transfer_messages_done (struct _transfer_msg *m)
{
    if (m->done)
        m->done (m->base.error == NULL, m->data);
}

static void
transfer_messages_free (struct _transfer_msg *m)
{
    g_object_unref (m->session);
    g_object_unref (m->source);
    g_free (m->dest_uri);
    g_ptr_array_unref (m->uids);
}

static MailMsgInfo transfer_messages_info = {
    sizeof (struct _transfer_msg),
    (MailMsgDescFunc) transfer_messages_desc,
    (MailMsgExecFunc) transfer_messages_exec,
    (MailMsgDoneFunc) transfer_messages_done,
    (MailMsgFreeFunc) transfer_messages_free
};

void
mail_transfer_messages (EMailSession *session,
                        CamelFolder *source,
                        GPtrArray *uids,
                        gboolean delete_from_source,
                        const gchar *dest_uri,
                        guint32 dest_flags,
                        void (*done) (gboolean ok,
                                      gpointer data),
                        gpointer data)
{
    struct _transfer_msg *m;

    g_return_if_fail (CAMEL_IS_FOLDER (source));
    g_return_if_fail (uids != NULL);
    g_return_if_fail (dest_uri != NULL);

    m = mail_msg_new (&transfer_messages_info);
    m->session = g_object_ref (session);
    m->source = g_object_ref (source);
    m->uids = g_ptr_array_ref (uids);
    m->delete = delete_from_source;
    m->dest_uri = g_strdup (dest_uri);
    m->dest_flags = dest_flags;
    m->done = done;
    m->data = data;

    mail_msg_slow_ordered_push (m);
}

/* ** SYNC FOLDER ********************************************************* */

struct _sync_folder_msg {
    MailMsg base;

    CamelFolder *folder;
    gboolean test_for_expunge;
    void (*done) (CamelFolder *folder, gpointer data);
    gpointer data;
};

static gchar *
sync_folder_desc (struct _sync_folder_msg *m)
{
    return g_strdup_printf (
        _("Storing folder '%s'"),
        camel_folder_get_full_name (m->folder));
}

static void
sync_folder_exec (struct _sync_folder_msg *m,
                  GCancellable *cancellable,
                  GError **error)
{
    gboolean expunge = FALSE;

    if (m->test_for_expunge) {
        GSettings *settings;
        gboolean delete_junk;

        settings = g_settings_new ("org.gnome.evolution.mail");

        expunge = g_settings_get_boolean (settings, "trash-empty-on-exit") &&
              g_settings_get_int (settings, "trash-empty-on-exit-days") == -1;
        delete_junk = g_settings_get_boolean (settings, "junk-empty-on-exit") &&
                  g_settings_get_int (settings, "junk-empty-on-exit-days") == -1;

        g_object_unref (settings);

        /* delete junk first, if requested */
        if (delete_junk) {
            CamelStore *store;
            CamelFolder *folder;

            store = camel_folder_get_parent_store (m->folder);
            folder = camel_store_get_junk_folder_sync (store, cancellable, error);
            if (folder != NULL) {
                GPtrArray *uids;
                guint32 flags;
                guint32 mask;
                guint ii;

                uids = camel_folder_get_uids (folder);
                flags = mask = CAMEL_MESSAGE_DELETED | CAMEL_MESSAGE_SEEN;

                camel_folder_freeze (folder);

                for (ii = 0; ii < uids->len && !g_cancellable_is_cancelled (cancellable); ii++) {
                    const gchar *uid = uids->pdata[ii];
                    camel_folder_set_message_flags (folder, uid, flags, mask);
                }

                camel_folder_thaw (folder);
                camel_folder_free_uids (folder, uids);

                g_object_unref (folder);

                if (g_cancellable_set_error_if_cancelled (cancellable, error))
                    return;
            }

            /* error should be set already, from the get_junk_folder_sync() call */
            if (g_cancellable_is_cancelled (cancellable))
                return;
        }
    }

    camel_folder_synchronize_sync (m->folder, expunge, cancellable, error);
}

static void
sync_folder_done (struct _sync_folder_msg *m)
{
    if (m->done)
        m->done (m->folder, m->data);
}

static void
sync_folder_free (struct _sync_folder_msg *m)
{
    if (m->folder)
        g_object_unref (m->folder);
}

static MailMsgInfo sync_folder_info = {
    sizeof (struct _sync_folder_msg),
    (MailMsgDescFunc) sync_folder_desc,
    (MailMsgExecFunc) sync_folder_exec,
    (MailMsgDoneFunc) sync_folder_done,
    (MailMsgFreeFunc) sync_folder_free
};

void
mail_sync_folder (CamelFolder *folder,
                  gboolean test_for_expunge,
                  void (*done) (CamelFolder *folder,
                                gpointer data),
                  gpointer data)
{
    struct _sync_folder_msg *m;

    m = mail_msg_new (&sync_folder_info);
    m->folder = g_object_ref (folder);
    m->test_for_expunge = test_for_expunge;
    m->data = data;
    m->done = done;

    mail_msg_slow_ordered_push (m);
}

/* ** SYNC STORE ********************************************************* */

struct _sync_store_msg {
    MailMsg base;

    CamelStore *store;
    gint expunge;
    void (*done) (CamelStore *store, gpointer data);
    gpointer data;
};

static gchar *
sync_store_desc (struct _sync_store_msg *m)
{
    CamelURL *url;
    gchar *uri, *res;

    url = camel_service_new_camel_url (CAMEL_SERVICE (m->store));
    uri = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
    camel_url_free (url);

    res = g_strdup_printf (
        m->expunge ?
        _("Expunging and storing account '%s'") :
        _("Storing account '%s'"),
        uri);
    g_free (uri);

    return res;
}

static void
sync_store_exec (struct _sync_store_msg *m,
                 GCancellable *cancellable,
                 GError **error)
{
    camel_store_synchronize_sync (
        m->store, m->expunge,
        cancellable, error);
}

static void
sync_store_done (struct _sync_store_msg *m)
{
    if (m->done)
        m->done (m->store, m->data);
}

static void
sync_store_free (struct _sync_store_msg *m)
{
    g_object_unref (m->store);
}

static MailMsgInfo sync_store_info = {
    sizeof (struct _sync_store_msg),
    (MailMsgDescFunc) sync_store_desc,
    (MailMsgExecFunc) sync_store_exec,
    (MailMsgDoneFunc) sync_store_done,
    (MailMsgFreeFunc) sync_store_free
};

void
mail_sync_store (CamelStore *store,
                 gint expunge,
                 void (*done) (CamelStore *store,
                               gpointer data),
                 gpointer data)
{
    struct _sync_store_msg *m;

    m = mail_msg_new (&sync_store_info);
    m->store = g_object_ref (store);
    m->expunge = expunge;
    m->data = data;
    m->done = done;

    mail_msg_slow_ordered_push (m);
}

/* ******************************************************************************** */

struct _empty_trash_msg {
    MailMsg base;

    CamelStore *store;
};

static gchar *
empty_trash_desc (struct _empty_trash_msg *m)
{
    CamelService *service;
    const gchar *display_name;

    service = CAMEL_SERVICE (m->store);
    display_name = camel_service_get_display_name (service);

    return g_strdup_printf (
        _("Emptying trash in '%s'"), display_name);
}

static void
empty_trash_exec (struct _empty_trash_msg *m,
                  GCancellable *cancellable,
                  GError **error)
{
    CamelService *service;
    CamelFolder *trash;

    service = CAMEL_SERVICE (m->store);

    if (!camel_service_connect_sync (service, cancellable, error))
        return;

    trash = camel_store_get_trash_folder_sync (
        m->store, cancellable, error);

    if (trash != NULL) {
        e_mail_folder_expunge_sync (trash, cancellable, error);
        g_object_unref (trash);
    }
}

static void
empty_trash_done (struct _empty_trash_msg *m)
{
}

static void
empty_trash_free (struct _empty_trash_msg *m)
{
    if (m->store)
        g_object_unref (m->store);
}

static MailMsgInfo empty_trash_info = {
    sizeof (struct _empty_trash_msg),
    (MailMsgDescFunc) empty_trash_desc,
    (MailMsgExecFunc) empty_trash_exec,
    (MailMsgDoneFunc) empty_trash_done,
    (MailMsgFreeFunc) empty_trash_free
};

void
mail_empty_trash (CamelStore *store)
{
    struct _empty_trash_msg *m;

    g_return_if_fail (CAMEL_IS_STORE (store));

    m = mail_msg_new (&empty_trash_info);
    m->store = g_object_ref (store);

    mail_msg_slow_ordered_push (m);
}

/* ** Execute Shell Command ************************************************ */

void
mail_execute_shell_command (CamelFilterDriver *driver,
                            gint argc,
                            gchar **argv,
                            gpointer data)
{
    if (argc <= 0)
        return;

    g_spawn_async (NULL, argv, NULL, 0, NULL, data, NULL, NULL);
}