aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-ops.c
blob: 5cc426d2adcfa41d8627bced83e6fccb081bc0c3 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* mail-ops.c: callbacks for the mail toolbar/menus */

/* 
 * Authors: Dan Winship <danw@helixcode.com>
 *          Jeffrey Stedfast <fejj@helixcode.com>
 *          Peter Williams <peterw@helixcode.com>
 *
 * Copyright 2000 Helix Code, Inc. (http://www.helixcode.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <config.h>
#include <ctype.h>
#include <errno.h>
#include <gnome.h>
#include <libgnomeprint/gnome-print-master.h>
#include <libgnomeprint/gnome-print-master-preview.h>
#include "mail.h"
#include "mail-threads.h"
#include "folder-browser.h"
#include "e-util/e-setup.h"
#include "filter/filter-editor.h"
#include "filter/filter-driver.h"
#include "widgets/e-table/e-table.h"
#include "mail-local.h"

/* FIXME: is there another way to do this? */
#include "Evolution.h"
#include "evolution-storage.h"

#include "evolution-shell-client.h"

#ifndef HAVE_MKSTEMP
#include <fcntl.h>
#include <sys/stat.h>
#endif

struct post_send_data {
    CamelFolder *folder;
    const char *uid;
    guint32 flags;
};

typedef struct rfm_s { 
    FolderBrowser *fb; 
    MailConfigService *source;
} rfm_t;

typedef struct rsm_s {
    EMsgComposer *composer;
    CamelTransport *transport;
    CamelMimeMessage *message;
    const char *subject;
    char *from;
    struct post_send_data *psd;
    gboolean ok;
} rsm_t;

static void
real_fetch_mail( gpointer user_data );

static void
real_send_mail( gpointer user_data );

static void
cleanup_send_mail( gpointer userdata );

static void
mail_exception_dialog (char *head, CamelException *ex, gpointer widget)
{
    char *msg;
    GtkWindow *window =
        GTK_WINDOW (gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW));

    msg = g_strdup_printf ("%s:\n%s", head,
                   camel_exception_get_description (ex));
    gnome_error_dialog_parented (msg, window);
    g_free (msg);
}

#ifdef USE_BROKEN_THREADS
static void
async_mail_exception_dialog (char *head, CamelException *ex, gpointer unused )
{
    mail_op_error( "%s: %s", head, camel_exception_get_description( ex ) );
}
#else
#define async_mail_exception_dialog mail_exception_dialog
#endif

static gboolean
check_configured (void)
{
    if (mail_config_is_configured ())
        return TRUE;
    
    mail_config_druid ();

    return mail_config_is_configured ();
}

static void
select_first_unread (CamelFolder *folder, int type, gpointer data)
{
    FolderBrowser *fb = data;
    ETable *table = E_TABLE_SCROLLED (fb->message_list->etable)->table;
    int mrow;

    mrow = e_table_view_to_model_row (table, 0);
    message_list_select (fb->message_list, mrow, MESSAGE_LIST_SELECT_NEXT,
                 0, CAMEL_MESSAGE_SEEN);
}

static CamelFolder *
filter_get_folder(FilterDriver *fd, const char *uri, void *data)
{
    return mail_uri_to_folder(uri);
}

static void
fetch_remote_mail (CamelFolder *source, CamelFolder *dest,
           gboolean keep_on_server, FolderBrowser *fb,
           CamelException *ex)
{
    CamelUIDCache *cache;
    GPtrArray *uids;
    int i;

    uids = camel_folder_get_uids (source);
    if (keep_on_server) {
        GPtrArray *new_uids;
        char *url, *p, *filename;

        url = camel_url_to_string (
            CAMEL_SERVICE (source->parent_store)->url, FALSE);
        for (p = url; *p; p++) {
            if (!isascii ((unsigned char)*p) ||
                strchr (" /'\"`&();|<>${}!", *p))
                *p = '_';
        }
        filename = g_strdup_printf ("%s/config/cache-%s",
                        evolution_dir, url);
        g_free (url);

        cache = camel_uid_cache_new (filename);
        g_free (filename);
        if (cache) {
            new_uids = camel_uid_cache_get_new_uids (cache, uids);
            camel_folder_free_uids (source, uids);
            uids = new_uids;
        } else {
            async_mail_exception_dialog ("Could not read UID "
                             "cache file. You may "
                             "receive duplicate "
                             "messages.", NULL, fb);
        }
    } else
        cache = NULL;

    printf ("got %d new messages in source\n", uids->len);
    for (i = 0; i < uids->len; i++) {
        CamelMimeMessage *msg;

        msg = camel_folder_get_message (source, uids->pdata[i], ex);
        if (camel_exception_is_set (ex)) {
            async_mail_exception_dialog ("Unable to get message",
                             ex, fb);
            goto done;
        }

        /* Append with flags = 0 since this is a new message */
        camel_folder_append_message (dest, msg, 0, ex);
        if (camel_exception_is_set (ex)) {
            async_mail_exception_dialog ("Unable to write message",
                             ex, fb);
            gtk_object_unref (GTK_OBJECT (msg));
            goto done;
        }

        if (!cache)
            camel_folder_delete_message (source, uids->pdata[i]);
        gtk_object_unref (GTK_OBJECT (msg));
    }

    camel_folder_sync (source, TRUE, ex);

 done:
    if (cache) {
        camel_uid_cache_free_uids (uids);
        if (!camel_exception_is_set (ex))
            camel_uid_cache_save (cache);
        camel_uid_cache_destroy (cache);
    } else
        camel_folder_free_uids (source, uids);
}

void
real_fetch_mail (gpointer user_data)
{
    rfm_t *info;
    FolderBrowser *fb = NULL;
    CamelException *ex;
    CamelStore *store = NULL, *dest_store = NULL;
    CamelFolder *folder = NULL, *dest_folder = NULL;
    char *url = NULL, *dest_url;
    FilterContext *fc = NULL;
    FilterDriver *driver = NULL;
    char *userrules, *systemrules;
    char *tmp_mbox = NULL, *source;
    guint handler_id = 0;
    struct stat st;
    gboolean keep;

    info = (rfm_t *) user_data;
    fb = info->fb;
    url = info->source->url;
    keep = info->source->keep_on_server;
    
    /* If using IMAP, don't do anything... */
    if (!strncmp (url, "imap:", 5))
        return;

    ex = camel_exception_new ();

    dest_url = g_strdup_printf ("mbox://%s/local/Inbox", evolution_dir);
    dest_store = camel_session_get_store (session, dest_url, ex);
    g_free (dest_url);
    if (!dest_store) {
        async_mail_exception_dialog ("Unable to get new mail", ex, fb);
        goto cleanup;
    }
    
    dest_folder = camel_store_get_folder (dest_store, "mbox", FALSE, ex);
    if (!dest_folder) {
        async_mail_exception_dialog ("Unable to get new mail", ex, fb);
        goto cleanup;
    }

    tmp_mbox = g_strdup_printf ("%s/local/Inbox/movemail", evolution_dir);

    /* If fetching mail from an mbox store, safely copy it to a
     * temporary store first.
     */
    if (!strncmp (url, "mbox:", 5)) {
        int tmpfd;

        tmpfd = open (tmp_mbox, O_RDWR | O_CREAT | O_APPEND, 0660);

        if (tmpfd == -1) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          "Couldn't create temporary "
                          "mbox: %s", g_strerror (errno));
            async_mail_exception_dialog ("Unable to move mail", ex, fb );
            goto cleanup;
        }
        close (tmpfd);

        /* Skip over "mbox:" plus host part (if any) of url. */
        source = url + 5;
        if (!strncmp (source, "//", 2))
            source = strchr (source + 2, '/');

        camel_movemail (source, tmp_mbox, ex);
        if (camel_exception_is_set (ex)) {
            async_mail_exception_dialog ("Unable to move mail",
                             ex, fb);
            goto cleanup;
        }

        if (stat (tmp_mbox, &st) == -1 || st.st_size == 0) {
            gnome_ok_dialog ("No new messages.");
            goto cleanup;
        }

        folder = camel_store_get_folder (dest_store, "movemail",
                         FALSE, ex);
        if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
            async_mail_exception_dialog ("Unable to move mail", ex, fb);
            goto cleanup;
        }
    } else {
        CamelFolder *sourcefolder;

        store = camel_session_get_store(session, url, ex);
        if (!store) {
            async_mail_exception_dialog("Unable to get new mail", ex, fb);
            goto cleanup;
        }

        camel_service_connect(CAMEL_SERVICE (store), ex);
        if (camel_exception_get_id(ex) != CAMEL_EXCEPTION_NONE) {
            if (camel_exception_get_id(ex) != CAMEL_EXCEPTION_USER_CANCEL)
                async_mail_exception_dialog("Unable to get new mail", ex, fb);
            goto cleanup;
        }

        sourcefolder = camel_store_get_folder(store, "inbox", FALSE, ex);
        if (camel_exception_get_id(ex) != CAMEL_EXCEPTION_NONE) {
            async_mail_exception_dialog("Unable to get new mail", ex, fb);
            goto cleanup;
        }

        /* can we perform filtering on this source? */
        if (!(sourcefolder->has_summary_capability
              && sourcefolder->has_search_capability)) {
            folder = camel_store_get_folder (dest_store,
                             "movemail", TRUE, ex);
            if (camel_exception_is_set (ex)) {
                async_mail_exception_dialog ("Unable to move mail", ex, fb);
                goto cleanup;
            }

            fetch_remote_mail (sourcefolder, folder, keep, fb, ex);
            gtk_object_unref (GTK_OBJECT (sourcefolder));
            if (camel_exception_is_set (ex))
                goto cleanup;
        } else {
            folder = sourcefolder;
        }
    }

    if (camel_folder_get_message_count (folder) == 0) {
        gnome_ok_dialog ("No new messages.");
        goto cleanup;
    } else if (camel_exception_is_set (ex)) {
        async_mail_exception_dialog ("Unable to get new mail", ex, fb);
        goto cleanup;
    }

    folder_browser_clear_search (fb);

    /* apply filtering rules to this inbox */
    fc = filter_context_new();
    userrules = g_strdup_printf("%s/filters.xml", evolution_dir);
    systemrules = g_strdup_printf("%s/evolution/filtertypes.xml", EVOLUTION_DATADIR);
    rule_context_load((RuleContext *)fc, systemrules, userrules);
    g_free (userrules);
    g_free (systemrules);

    driver = filter_driver_new(fc, filter_get_folder, 0);

    /* Attach a handler to the destination folder to select the first unread
     * message iff it changes and iff it's the folder being viewed.
     */
    if (dest_folder == fb->folder)
        handler_id = gtk_signal_connect (GTK_OBJECT (dest_folder), "folder_changed",
                         GTK_SIGNAL_FUNC (select_first_unread), fb);

    if (filter_driver_run(driver, folder, dest_folder) == -1) {
        async_mail_exception_dialog ("Unable to get new mail", ex, fb);
        goto cleanup;
    }

    if (dest_folder == fb->folder)
        gtk_signal_disconnect (GTK_OBJECT (dest_folder), handler_id);

 cleanup:
    if (stat (tmp_mbox, &st) == 0 && st.st_size == 0)
        unlink (tmp_mbox); /* FIXME: should use camel to do this */
    g_free (tmp_mbox);

    if (driver)
        gtk_object_unref((GtkObject *)driver);
    if (fc)
        gtk_object_unref((GtkObject *)fc);

    if (folder) {
        camel_folder_sync (folder, TRUE, ex);
        gtk_object_unref (GTK_OBJECT (folder));
    }

    if (dest_folder) {
        camel_folder_sync (dest_folder, TRUE, ex);
        gtk_object_unref (GTK_OBJECT (dest_folder));
    }

    if (store) {
        camel_service_disconnect (CAMEL_SERVICE (store), ex);
        gtk_object_unref (GTK_OBJECT (store));
    }

    if (dest_store && dest_store != fb->folder->parent_store) {
        camel_service_disconnect (CAMEL_SERVICE (dest_store), ex);
        gtk_object_unref (GTK_OBJECT (dest_store));
    }
    camel_exception_free (ex);
}

void
fetch_mail (GtkWidget *button, gpointer user_data)
{
    MailConfigService *source;
    rfm_t *info;

    if (!check_configured ())
        return;

    source = mail_config_get_default_source ();
    if (!source || !source->url) {
        GtkWidget *win = gtk_widget_get_ancestor (GTK_WIDGET (user_data),
                              GTK_TYPE_WINDOW);

        gnome_error_dialog_parented ("You have no remote mail source "
                         "configured", GTK_WINDOW (win));
        return;
    }

    /* This must be dynamically allocated so as not to be clobbered
     * when we return. Actually, making it static in the whole file
     * would probably work.
     */

    info = g_new (rfm_t, 1);
    info->fb = FOLDER_BROWSER (user_data);
    info->source = source;

#ifdef USE_BROKEN_THREADS
    mail_operation_try (_("Fetching mail"), real_fetch_mail, NULL, info);
#else
    real_fetch_mail (info);
#endif
}

static gboolean
ask_confirm_for_empty_subject (EMsgComposer *composer)
{
    GtkWidget *message_box;
    int button;

    message_box = gnome_message_box_new (_("This message has no subject.\nReally send?"),
                         GNOME_MESSAGE_BOX_QUESTION,
                         GNOME_STOCK_BUTTON_YES, GNOME_STOCK_BUTTON_NO,
                         NULL);

    button = gnome_dialog_run_and_close (GNOME_DIALOG (message_box));

    if (button == 0)
        return TRUE;
    else
        return FALSE;
}

static void
set_x_mailer_header (CamelMedium *medium)
{
    char *mailer_string;

    mailer_string = g_strdup_printf ("Evolution %s (Developer Preview)", VERSION);

    camel_medium_add_header (medium, "X-Mailer", mailer_string);

    g_free (mailer_string);
}

static void
real_send_mail (gpointer user_data)
{
    rsm_t *info = (rsm_t *) user_data;
    EMsgComposer *composer = NULL;
    CamelTransport *transport = NULL;
    CamelException *ex = NULL;
    CamelMimeMessage *message = NULL;
    const char *subject = NULL;
    char *from = NULL;
    struct post_send_data *psd = NULL;

#ifdef USE_BROKEN_THREADS
    mail_op_hide_progressbar ();
    mail_op_set_message ("Connecting to transport...");
#endif

    ex = camel_exception_new ();
    composer = info->composer;
    transport = info->transport;
    message = info->message;
    subject = info->subject;
    from = info->from;
    psd = info->psd;

    set_x_mailer_header (CAMEL_MEDIUM (message));

    camel_mime_message_set_from (message, from);
    camel_mime_message_set_date (message, CAMEL_MESSAGE_DATE_CURRENT, 0);

    camel_service_connect (CAMEL_SERVICE (transport), ex);

#ifdef USE_BROKEN_THREADS
    mail_op_set_message ("Connected. Sending...");
#endif

    if (!camel_exception_is_set (ex))
        camel_transport_send (transport, CAMEL_MEDIUM (message), ex);

    if (!camel_exception_is_set (ex)) {
#ifdef USE_BROKEN_THREADS
        mail_op_set_message ("Sent. Disconnecting...");
#endif      
        camel_service_disconnect (CAMEL_SERVICE (transport), ex);
    }

    if (camel_exception_is_set (ex)) {
        async_mail_exception_dialog ("Could not send message", ex, composer);
        info->ok = FALSE;
    } else {
        if (psd) {
            camel_folder_set_message_flags (psd->folder, psd->uid,
                            psd->flags, psd->flags);
        }
        info->ok = TRUE;

    }

    camel_exception_free (ex);
}

static void
cleanup_send_mail (gpointer userdata)
{
    rsm_t *info = (rsm_t *) userdata;
    
    if (info->ok) {
        gtk_object_destroy (GTK_OBJECT (info->composer));
    }

    gtk_object_unref (GTK_OBJECT (info->message));
    g_free (info);
}

static void
composer_send_cb (EMsgComposer *composer, gpointer data)
{
    const MailConfigIdentity *id = NULL; 
    static CamelTransport *transport = NULL;
    struct post_send_data *psd = data;
    rsm_t *info;
    static char *from = NULL;
    const char *subject;
    CamelException *ex;
    CamelMimeMessage *message;
    char *name, *addr;

    ex = camel_exception_new ();

    id = mail_config_get_default_identity ();
    
    if (!check_configured() || !id) {
        GtkWidget *message;

        message = gnome_warning_dialog_parented (_("You need to configure an identity\n"
                               "before you can send mail."),
                             GTK_WINDOW (gtk_widget_get_ancestor (GTK_WIDGET (composer),
                                                  GTK_TYPE_WINDOW)));
        gnome_dialog_run_and_close (GNOME_DIALOG (message));
        return;
    }

    if (!from) {
        CamelInternetAddress *ciaddr;

        g_assert (id);
        
        name = id->name;
        g_assert (name);

        addr = id->address;
        g_assert (addr);

        ciaddr = camel_internet_address_new ();
        camel_internet_address_add (ciaddr, name, addr);

        from = camel_address_encode (CAMEL_ADDRESS (ciaddr));
    }

    if (!transport) {
        MailConfigService *t;
        char *url;

        t = mail_config_get_transport ();
        url = t->url;
        g_assert (url);

        transport = camel_session_get_transport (session, url, ex);
        if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) {
            mail_exception_dialog ("Could not load mail transport",
                           ex, composer);
            camel_exception_free (ex);
            return;
        }
    }

    message = e_msg_composer_get_message (composer);

    subject = camel_mime_message_get_subject (message);
    if (!subject || !*subject) {
        if (!ask_confirm_for_empty_subject (composer)) {
            gtk_object_unref (GTK_OBJECT (message));
            return;
        }
    }

    info = g_new0 (rsm_t, 1);
    info->composer = composer;
    info->transport = transport;
    info->message = message;
    info->subject = subject;
    info->from = from;
    info->psd = psd;

#ifdef USE_BROKEN_THREADS
    mail_operation_try ("Send Message", real_send_mail, cleanup_send_mail, info);
#else
    real_send_mail (info);
    cleanup_send_mail (info);
#endif
}

static void
free_psd (GtkWidget *composer, gpointer user_data)
{
    struct post_send_data *psd = user_data;

    gtk_object_unref (GTK_OBJECT (psd->folder));
    g_free (psd);
}

static GtkWidget *
create_msg_composer (const char *url)
{
    MailConfigIdentity *id;
    gboolean send_html;
    gchar *sig_file = NULL;
    GtkWidget *composer_widget;

    id = mail_config_get_default_identity ();
    send_html = mail_config_send_html ();
    
    if (id) {
        sig_file = id->sig;
    }
    
    if (url != NULL)
        composer_widget = e_msg_composer_new_from_url (url);
    else
        composer_widget = e_msg_composer_new_with_sig_file (sig_file);

    e_msg_composer_set_send_html (E_MSG_COMPOSER (composer_widget), 
                      send_html);

    return composer_widget;
}

void
compose_msg (GtkWidget *widget, gpointer user_data)
{
    GtkWidget *composer;
    
    if (!check_configured ())
        return;
    
    composer = create_msg_composer (NULL);
    
    gtk_signal_connect (GTK_OBJECT (composer), "send",
                GTK_SIGNAL_FUNC (composer_send_cb), NULL);
    gtk_widget_show (composer);
}

/* Send according to a mailto (RFC 2368) URL. */
void
send_to_url (const char *url)
{
    GtkWidget *composer;

    if (!check_configured ())
        return;

    composer = create_msg_composer (url);

    gtk_signal_connect (GTK_OBJECT (composer), "send",
                GTK_SIGNAL_FUNC (composer_send_cb), NULL);
    gtk_widget_show (composer);
}   

static void
reply (FolderBrowser *fb, gboolean to_all)
{
    EMsgComposer *composer;
    struct post_send_data *psd;

    if (!check_configured () || !fb->message_list->cursor_uid ||
        !fb->mail_display->current_message)
        return;

    psd = g_new (struct post_send_data, 1);
    psd->folder = fb->folder;
    gtk_object_ref (GTK_OBJECT (psd->folder));
    psd->uid = fb->message_list->cursor_uid;
    psd->flags = CAMEL_MESSAGE_ANSWERED;

    composer = mail_generate_reply (fb->mail_display->current_message, to_all);

    gtk_signal_connect (GTK_OBJECT (composer), "send",
                GTK_SIGNAL_FUNC (composer_send_cb), psd); 
    gtk_signal_connect (GTK_OBJECT (composer), "destroy",
                GTK_SIGNAL_FUNC (free_psd), psd); 

    gtk_widget_show (GTK_WIDGET (composer));    
}

void
reply_to_sender (GtkWidget *widget, gpointer user_data)
{
    reply (FOLDER_BROWSER (user_data), FALSE);
}

void
reply_to_all (GtkWidget *widget, gpointer user_data)
{
    reply (FOLDER_BROWSER (user_data), TRUE);
}

static void
attach_msg (MessageList *ml, const char *uid, gpointer data)
{
    EMsgComposer *composer = data;
    CamelMimeMessage *message;
    CamelMimePart *part;
    const char *subject;
    char *desc;
    
    message = camel_folder_get_message (ml->folder, uid, NULL);
    if (!message)
        return;
    subject = camel_mime_message_get_subject (message);
    if (subject)
        desc = g_strdup_printf ("Forwarded message - %s", subject);
    else
        desc = g_strdup ("Forwarded message");
    
    part = camel_mime_part_new ();
    camel_mime_part_set_disposition (part, "inline");
    camel_mime_part_set_description (part, desc);
    camel_medium_set_content_object (CAMEL_MEDIUM (part),
                     CAMEL_DATA_WRAPPER (message));
    camel_mime_part_set_content_type (part, "message/rfc822");
    
    e_msg_composer_attach (composer, part);
    
    gtk_object_unref (GTK_OBJECT (part));
    gtk_object_unref (GTK_OBJECT (message));
    g_free (desc);
}

void
forward_msg (GtkWidget *widget, gpointer user_data)
{
    FolderBrowser *fb = FOLDER_BROWSER (user_data);
    EMsgComposer *composer;
    CamelMimeMessage *cursor_msg;
    const char *from, *subject;
    char *fwd_subj;
    
    cursor_msg = fb->mail_display->current_message;
    if (!check_configured () || !cursor_msg)
        return;

    composer = E_MSG_COMPOSER (create_msg_composer (NULL));
    message_list_foreach (fb->message_list, attach_msg, composer);

    from = camel_mime_message_get_from (cursor_msg);
    subject = camel_mime_message_get_subject (cursor_msg);
    if (from) {
        if (subject && *subject) {
            fwd_subj = g_strdup_printf ("[%s] %s", from, subject);
        } else {
            fwd_subj = g_strdup_printf ("[%s] (forwarded message)",
                            from);
        }
    } else {
        fwd_subj = NULL;
    }

    e_msg_composer_set_headers (composer, NULL, NULL, NULL, fwd_subj);
    g_free (fwd_subj);

    gtk_signal_connect (GTK_OBJECT (composer), "send",
                GTK_SIGNAL_FUNC (composer_send_cb), NULL);

    gtk_widget_show (GTK_WIDGET (composer));    
}

struct move_data {
    CamelFolder *source, *dest;
    CamelException *ex;
};

static void
real_move_msg (MessageList *ml, const char *uid, gpointer user_data)
{
    struct move_data *rfd = user_data;

    if (camel_exception_is_set (rfd->ex))
        return;

    camel_folder_move_message_to (rfd->source, uid, rfd->dest, rfd->ex);
}

void
move_msg (GtkWidget *widget, gpointer user_data)
{
    FolderBrowser *fb = user_data;
    MessageList *ml = fb->message_list;
    char *uri, *physical, *path;
    struct move_data rfd;
    const char *allowed_types[] = { "mail", NULL };
    extern EvolutionShellClient *global_shell_client;
    static char *last = NULL;

    if (!last)
        last = g_strdup ("");

    evolution_shell_client_user_select_folder  (global_shell_client,
                            _("Move message(s) to"),
                            last, allowed_types, &uri, &physical);
    if (!uri)
        return;

    path = strchr (uri, '/');
    if (path && strcmp (last, path) != 0) {
        g_free (last);
        last = g_strdup (path);
    }
    g_free (uri);

    rfd.source = ml->folder;
    rfd.dest = mail_uri_to_folder (physical);
    g_free (physical);
    if (!rfd.dest)
        return;
    rfd.ex = camel_exception_new ();
    
    message_list_foreach (ml, real_move_msg, &rfd);
    gtk_object_unref (GTK_OBJECT (rfd.dest));
    
    if (camel_exception_is_set (rfd.ex))
        mail_exception_dialog ("Could not move message", rfd.ex, fb);
    camel_exception_free (rfd.ex);
}

void
mark_all_seen (BonoboUIHandler *uih, void *user_data, const char *path)
{
    FolderBrowser *fb = FOLDER_BROWSER(user_data);
    MessageList *ml = fb->message_list;
    GPtrArray *uids;
    int i;

    uids = camel_folder_get_uids (ml->folder);
    for (i = 0; i < uids->len; i++) {
        camel_folder_set_message_flags (ml->folder, uids->pdata[i],
                        CAMEL_MESSAGE_SEEN,
                        CAMEL_MESSAGE_SEEN);
    }
}

static void
real_edit_msg (MessageList *ml, const char *uid, gpointer user_data)
{
    CamelException *ex = user_data;
    CamelMimeMessage *msg;
    GtkWidget *composer;
    
    if (camel_exception_is_set (ex))
        return;
    
    msg = camel_folder_get_message (ml->folder, uid, ex);
    
    composer = e_msg_composer_new_with_message (msg);
    gtk_signal_connect (GTK_OBJECT (composer), "send",
                GTK_SIGNAL_FUNC (composer_send_cb), NULL);
    gtk_widget_show (composer);
}

void
edit_msg (GtkWidget *widget, gpointer user_data)
{
    FolderBrowser *fb = FOLDER_BROWSER (user_data);
    MessageList *ml = fb->message_list;
    CamelException ex;
    extern CamelFolder *drafts_folder;
    
    camel_exception_init (&ex);
    
    if (fb->folder != drafts_folder) {
        camel_exception_setv (&ex, CAMEL_EXCEPTION_FOLDER_INSUFFICIENT_PERMISSION,
                      "FIXME: some error message about not being in the Drafts folder...");
        mail_exception_dialog ("Could not open message for editing", &ex, fb);
        return;
    }
    
    message_list_foreach (ml, real_edit_msg, &ex);
    if (camel_exception_is_set (&ex)) {
        mail_exception_dialog ("Could not open message for editing", &ex, fb);
        camel_exception_clear (&ex);
        return;
    }
}

void
edit_message (BonoboUIHandler *uih, void *user_data, const char *path)
{
    edit_msg (NULL, user_data);
}

static void
real_delete_msg (MessageList *ml, const char *uid, gpointer user_data)
{
    CamelException *ex = user_data;
    guint32 flags;

    if (camel_exception_is_set (ex))
        return;

    /* Toggle the deleted flag without touching other flags. */
    flags = camel_folder_get_message_flags (ml->folder, uid);
    camel_folder_set_message_flags (ml->folder, uid,
                    CAMEL_MESSAGE_DELETED, ~flags);
}

void
delete_msg (GtkWidget *button, gpointer user_data)
{
    FolderBrowser *fb = user_data;
    MessageList *ml = fb->message_list;
    CamelException ex;

    camel_exception_init (&ex);
    message_list_foreach (ml, real_delete_msg, &ex);
    if (camel_exception_is_set (&ex)) {
        mail_exception_dialog ("Could not toggle deleted flag",
                       &ex, fb);
        camel_exception_clear (&ex);
        return;
    }
}

static void real_expunge_folder (gpointer user_data)
{
    FolderBrowser *fb = FOLDER_BROWSER (user_data);
    CamelException ex;

    e_table_model_pre_change(fb->message_list->table_model);

#ifdef USE_BROKEN_THREADS
    mail_op_hide_progressbar ();
    mail_op_set_message ("Expunging %s...", fb->message_list->folder->full_name);
#endif

    camel_exception_init (&ex);

    camel_folder_expunge (fb->message_list->folder, &ex);

    /* FIXME: is there a better way to force an update? */
    /* FIXME: Folder should raise a signal to say its contents has changed ... */
    e_table_model_changed (fb->message_list->table_model);

    if (camel_exception_get_id (&ex) != CAMEL_EXCEPTION_NONE) {
        async_mail_exception_dialog ("Unable to expunge deleted messages", &ex, fb);
    }
}

void
expunge_folder (BonoboUIHandler *uih, void *user_data, const char *path)
{
    FolderBrowser *fb = FOLDER_BROWSER(user_data);

    if (fb->message_list->folder) {
#ifdef USE_BROKEN_THREADS
        mail_operation_try ("Expunge Folder", real_expunge_folder, NULL, fb);
#else
        real_expunge_folder (fb);
#endif
    }
}

static void
filter_druid_clicked(GtkWidget *w, int button, FolderBrowser *fb)
{
    FilterContext *fc;

    if (button == 0) {
        char *user;

        fc = gtk_object_get_data((GtkObject *)w, "context");
        user = g_strdup_printf("%s/filters.xml", evolution_dir);
        rule_context_save((RuleContext *)fc, user);
        g_free(user);
    }
    
    if (button != -1) {
        gnome_dialog_close((GnomeDialog *)w);
    }
}

void
filter_edit (BonoboUIHandler *uih, void *user_data, const char *path)
{
    FolderBrowser *fb = FOLDER_BROWSER (user_data);
    FilterContext *fc;
    char *user, *system;
    GtkWidget *w;

    fc = filter_context_new();
    user = g_strdup_printf("%s/filters.xml", evolution_dir);
    system = g_strdup_printf("%s/evolution/filtertypes.xml", EVOLUTION_DATADIR);
    rule_context_load((RuleContext *)fc, system, user);
    g_free(user);
    g_free(system);
    w = filter_editor_construct(fc);
    gtk_object_set_data_full((GtkObject *)w, "context", fc, (GtkDestroyNotify)gtk_object_unref);
    gtk_signal_connect((GtkObject *)w, "clicked", filter_druid_clicked, fb);
    gtk_widget_show(w);
}

void
vfolder_edit_vfolders (BonoboUIHandler *uih, void *user_data, const char *path)
{
    void vfolder_edit(void);

    vfolder_edit();
}

void
providers_config (BonoboUIHandler *uih, void *user_data, const char *path)
{
    mail_config();
}

void
print_msg (GtkWidget *button, gpointer user_data)
{
    FolderBrowser *fb = user_data;
    GnomePrintMaster *print_master;
    GnomePrintContext *print_context;
    GtkWidget *preview;

    print_master = gnome_print_master_new ();

    print_context = gnome_print_master_get_context (print_master);
    gtk_html_print (fb->mail_display->html, print_context);

    preview = GTK_WIDGET (gnome_print_master_preview_new (
        print_master, "Mail Print Preview"));
    gtk_widget_show (preview);

    gtk_object_unref (GTK_OBJECT (print_master));
}

void
configure_folder(BonoboUIHandler *uih, void *user_data, const char *path)
{
    FolderBrowser *fb = FOLDER_BROWSER(user_data);

    local_reconfigure_folder(fb);
}


struct view_msg_data {
    FolderBrowser *fb;
    CamelException *ex;
};

static void
real_view_msg (MessageList *ml, const char *uid, gpointer user_data)
{
    struct view_msg_data *data = user_data;
    FolderBrowser *fb;
    CamelMimeMessage *msg;
    GtkWidget *view;
    
    if (camel_exception_is_set (data->ex))
        return;
    
    msg = camel_folder_get_message (ml->folder, uid, data->ex);
    
    fb = FOLDER_BROWSER (folder_browser_new ());
    folder_browser_set_uri (fb, data->fb->uri);
    
    fb->message_list->cursor_uid = uid;
    fb->mail_display->current_message = msg;
    
    view = mail_view_create (fb);
    
    gtk_widget_show (view);
}

void
view_msg (GtkWidget *widget, gpointer user_data)
{
    struct view_msg_data data;
    FolderBrowser *fb = user_data;
    FolderBrowser *folder_browser;
    CamelException ex;
    MessageList *ml;
    
    camel_exception_init (&ex);
    
    data.fb = fb;
    data.ex = &ex;
    
    ml = fb->message_list;
    message_list_foreach (ml, real_view_msg, &data);
    if (camel_exception_is_set (&ex)) {
        mail_exception_dialog ("Could not open message for viewing", &ex, fb);
        camel_exception_clear (&ex);
        return;
    }
}

void
view_message (BonoboUIHandler *uih, void *user_data, const char *path)
{
    view_msg (NULL, user_data);
}