aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-mt.c
blob: 4dba37c7e3ea82719b0b5eb565983d257c8ec405 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include <config.h>

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>

#include <glib.h>

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

#include <libedataserver/e-msgport.h>
#include <libedataserver/e-data-server-util.h>

#include <camel/camel-url.h>
#include <camel/camel-operation.h>

#include "misc/e-gui-utils.h"
#include "e-util/e-error.h"
#include "e-util/e-icon-factory.h"

#include "e-activity-handler.h"

#include "mail-config.h"
#include "mail-component.h"
#include "mail-session.h"
#include "mail-mt.h"

/*#define MALLOC_CHECK*/
#define LOG_OPS
#define LOG_LOCKS
#define d(x)

static void set_stop(int sensitive);
static void mail_operation_status(struct _CamelOperation *op, const char *what, int pc, void *data);

#ifdef LOG_LOCKS
#define MAIL_MT_LOCK(x) (log_locks?fprintf(log, "%" G_GINT64_MODIFIER "x: lock " # x "\n", e_util_pthread_id(pthread_self())):0, pthread_mutex_lock(&x))
#define MAIL_MT_UNLOCK(x) (log_locks?fprintf(log, "%" G_GINT64_MODIFIER "x: unlock " # x "\n", e_util_pthread_id(pthread_self())): 0, pthread_mutex_unlock(&x))
#else
#define MAIL_MT_LOCK(x) pthread_mutex_lock(&x)
#define MAIL_MT_UNLOCK(x) pthread_mutex_unlock(&x)
#endif

/* background operation status stuff */
struct _mail_msg_priv {
    int activity_state; /* sigh sigh sigh, we need to keep track of the state external to the
                   pointer itself for locking/race conditions */
    int activity_id;
    GtkWidget *error;
    gboolean cancelable;
};

static GdkPixbuf *progress_icon = NULL;

/* mail_msg stuff */
#ifdef LOG_OPS
static FILE *log;
static int log_ops, log_locks, log_init;
#endif

static unsigned int mail_msg_seq; /* sequence number of each message */
static GHashTable *mail_msg_active_table; /* table of active messages, must hold mail_msg_lock to access */
static pthread_mutex_t mail_msg_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t mail_msg_cond = PTHREAD_COND_INITIALIZER;

pthread_t mail_gui_thread;

MailAsyncEvent *mail_async_event;

static void mail_msg_destroy(EThread *e, EMsg *msg, void *data);

void mail_msg_set_cancelable (struct _mail_msg *msg, gboolean status)
{
    msg->priv->cancelable = status;
}

void *mail_msg_new(mail_msg_op_t *ops, EMsgPort *reply_port, size_t size)
{
    struct _mail_msg *msg;

    MAIL_MT_LOCK(mail_msg_lock);

#if defined(LOG_OPS) || defined(LOG_LOCKS)
    if (!log_init) {
        time_t now = time(NULL);

        log_init = TRUE;
        log_ops = getenv("EVOLUTION_MAIL_LOG_OPS") != NULL;
        log_locks = getenv("EVOLUTION_MAIL_LOG_LOCKS") != NULL;
        if (log_ops || log_locks) {
            log = fopen("evolution-mail-ops.log", "w+");
            if (log) {
                setvbuf(log, NULL, _IOLBF, 0);
                fprintf(log, "Started evolution-mail: %s\n", ctime(&now));
                g_warning("Logging mail operations to evolution-mail-ops.log");

                if (log_ops)
                    fprintf(log, "Logging async operations\n");

                if (log_locks) {
                    fprintf(log, "Logging lock operations, mail_gui_thread = %" G_GINT64_MODIFIER "x\n\n", e_util_pthread_id(mail_gui_thread));
                    fprintf(log, "%" G_GINT64_MODIFIER "x: lock mail_msg_lock\n", e_util_pthread_id(pthread_self()));
                }
            } else {
                g_warning ("Could not open log file: %s", strerror(errno));
                log_ops = log_locks = FALSE;
            }
        }
    }
#endif
    msg = g_malloc0(size);
    msg->ops = ops;
    msg->seq = mail_msg_seq++;
    msg->msg.reply_port = reply_port;
    msg->cancel = camel_operation_new(mail_operation_status, GINT_TO_POINTER(msg->seq));
    camel_exception_init(&msg->ex);
    msg->priv = g_malloc0(sizeof(*msg->priv));
    msg->priv->cancelable = TRUE;

    g_hash_table_insert(mail_msg_active_table, GINT_TO_POINTER(msg->seq), msg);

    d(printf("New message %p\n", msg));

#ifdef LOG_OPS
    if (log_ops)
        fprintf(log, "%p: New\n", msg);
#endif
    MAIL_MT_UNLOCK(mail_msg_lock);

    return msg;
}


static void end_event_callback (CamelObject *o, void *event_data, void *error)
{
    EActivityHandler *activity_handler = mail_component_peek_activity_handler (mail_component_peek ());
    guint activity_id = GPOINTER_TO_INT (event_data);

    if (!error) {
        e_activity_handler_operation_finished (activity_handler, activity_id);
    } else {
        d(printf("Yahooooo, we got it nonintrusively\n"));
        e_activity_handler_operation_set_error (activity_handler, activity_id, error);

    }
}


#ifdef MALLOC_CHECK
#include <mcheck.h>

static void
checkmem(void *p)
{
    if (p) {
        int status = mprobe(p);

        switch (status) {
        case MCHECK_HEAD:
            printf("Memory underrun at %p\n", p);
            abort();
        case MCHECK_TAIL:
            printf("Memory overrun at %p\n", p);
            abort();
        case MCHECK_FREE:
            printf("Double free %p\n", p);
            abort();
        }
    }
}
#endif

void mail_msg_free(void *msg)
{
    struct _mail_msg *m = msg;
    int activity_id;
    GtkWidget *error = NULL;

#ifdef MALLOC_CHECK
    checkmem(m);
    checkmem(m->cancel);
    checkmem(m->priv);
#endif
    d(printf("Free message %p\n", msg));

    if (m->ops->destroy_msg)
        m->ops->destroy_msg(m);

    MAIL_MT_LOCK(mail_msg_lock);

#ifdef LOG_OPS
    if (log_ops)
        fprintf(log, "%p: Free  (exception `%s')\n", msg,
            camel_exception_get_description(&m->ex)?camel_exception_get_description(&m->ex):"None");
#endif
    g_hash_table_remove(mail_msg_active_table, GINT_TO_POINTER(m->seq));
    pthread_cond_broadcast(&mail_msg_cond);

    /* We need to make sure we dont lose a reference here YUCK YUCK */
    /* This is tightly integrated with the code in do_op_status,
       as it closely relates to the CamelOperation setup in msg_new() above */
    if (m->priv->activity_state == 1) {
        m->priv->activity_state = 3; /* tell the other thread
                          * to free it itself (yuck yuck) */
        MAIL_MT_UNLOCK(mail_msg_lock);
        return;
    } else {
        activity_id = m->priv->activity_id;
        error = m->priv->error;
        if (error && !activity_id) {
            e_activity_handler_make_error (mail_component_peek_activity_handler (mail_component_peek ()), "mail",
                            g_object_get_data ((GObject *) error, "primary"), error);
            printf("Making error\n");
        }

    }

    MAIL_MT_UNLOCK(mail_msg_lock);

    if (m->cancel) {
        camel_operation_mute(m->cancel);
        camel_operation_unref(m->cancel);
    }

    camel_exception_clear(&m->ex);
    /*g_free(m->priv->what);*/
    g_free(m->priv);
    g_free(m);

    if (activity_id != 0)
        mail_async_event_emit(mail_async_event, MAIL_ASYNC_GUI, (MailAsyncFunc) end_event_callback,
                      NULL, GINT_TO_POINTER (activity_id), error);
}

/* hash table of ops->dialogue of active errors */
static GHashTable *active_errors = NULL;

static void error_destroy(GtkObject *o, void *data)
{
    g_hash_table_remove(active_errors, data);
}

static void error_response(GtkObject *o, int button, void *data)
{
    gtk_widget_destroy((GtkWidget *)o);
}

void mail_msg_check_error(void *msg)
{
    struct _mail_msg *m = msg;
    char *what;
    GtkDialog *gd;

#ifdef MALLOC_CHECK
    checkmem(m);
    checkmem(m->cancel);
    checkmem(m->priv);
#endif

    /* don't report any errors if we are not in interactive mode */
    if (!mail_session_get_interactive ())
        return;

    if (!camel_exception_is_set(&m->ex)
        || m->ex.id == CAMEL_EXCEPTION_USER_CANCEL
        || m->ex.id == CAMEL_EXCEPTION_FOLDER_INVALID_UID)
        return;

    if (active_errors == NULL)
        active_errors = g_hash_table_new(NULL, NULL);

    /* check to see if we have dialogue already running for this operation */
    /* we key on the operation pointer, which is at least accurate enough
       for the operation type, although it could be on a different object. */
    if (g_hash_table_lookup(active_errors, m->ops)) {
        g_warning("Error occurred while existing dialogue active:\n%s", camel_exception_get_description(&m->ex));
        return;
    }

    if (m->ops->describe_msg
        && (what = m->ops->describe_msg(m, FALSE))) {
        gd = (GtkDialog *)e_error_new(NULL, "mail:async-error", what, camel_exception_get_description(&m->ex), NULL);
        g_free(what);
    } else
        gd = (GtkDialog *)e_error_new(NULL, "mail:async-error-nodescribe", camel_exception_get_description(&m->ex), NULL);

    g_hash_table_insert(active_errors, m->ops, gd);
    g_signal_connect(gd, "response", G_CALLBACK(error_response), m->ops);
    g_signal_connect(gd, "destroy", G_CALLBACK(error_destroy), m->ops);
    if (m->priv->cancelable)
        m->priv->error = (GtkWidget *) gd;
    else
        gtk_widget_show((GtkWidget *)gd);
}

void mail_msg_cancel(unsigned int msgid)
{
    struct _mail_msg *m;

    MAIL_MT_LOCK(mail_msg_lock);
    m = g_hash_table_lookup(mail_msg_active_table, GINT_TO_POINTER(msgid));

    if (m && m->cancel)
        camel_operation_cancel(m->cancel);

    MAIL_MT_UNLOCK(mail_msg_lock);
}


/* waits for a message to be finished processing (freed)
   the messageid is from struct _mail_msg->seq */
void mail_msg_wait(unsigned int msgid)
{
    struct _mail_msg *m;
    int ismain = pthread_equal(pthread_self(), mail_gui_thread);

    if (ismain) {
        MAIL_MT_LOCK(mail_msg_lock);
        m = g_hash_table_lookup(mail_msg_active_table, GINT_TO_POINTER(msgid));
        while (m) {
            MAIL_MT_UNLOCK(mail_msg_lock);
            gtk_main_iteration();
            MAIL_MT_LOCK(mail_msg_lock);
            m = g_hash_table_lookup(mail_msg_active_table, GINT_TO_POINTER(msgid));
        }
        MAIL_MT_UNLOCK(mail_msg_lock);
    } else {
        MAIL_MT_LOCK(mail_msg_lock);
        m = g_hash_table_lookup(mail_msg_active_table, GINT_TO_POINTER(msgid));
        while (m) {
            pthread_cond_wait(&mail_msg_cond, &mail_msg_lock);
            m = g_hash_table_lookup(mail_msg_active_table, GINT_TO_POINTER(msgid));
        }
        MAIL_MT_UNLOCK(mail_msg_lock);
    }
}

int mail_msg_active(unsigned int msgid)
{
    int active;

    MAIL_MT_LOCK(mail_msg_lock);
    if (msgid == (unsigned int)-1)
        active = g_hash_table_size(mail_msg_active_table) > 0;
    else
        active = g_hash_table_lookup(mail_msg_active_table, GINT_TO_POINTER(msgid)) != NULL;
    MAIL_MT_UNLOCK(mail_msg_lock);

    return active;
}

void mail_msg_wait_all(void)
{
    int ismain = pthread_equal(pthread_self(), mail_gui_thread);

    if (ismain) {
        MAIL_MT_LOCK(mail_msg_lock);
        while (g_hash_table_size(mail_msg_active_table) > 0) {
            MAIL_MT_UNLOCK(mail_msg_lock);
            gtk_main_iteration();
            MAIL_MT_LOCK(mail_msg_lock);
        }
        MAIL_MT_UNLOCK(mail_msg_lock);
    } else {
        MAIL_MT_LOCK(mail_msg_lock);
        while (g_hash_table_size(mail_msg_active_table) > 0) {
            pthread_cond_wait(&mail_msg_cond, &mail_msg_lock);
        }
        MAIL_MT_UNLOCK(mail_msg_lock);
    }
}

/* **************************************** */
struct _cancel_hook_data {
    struct _cancel_hook_data *next;
    struct _cancel_hook_data *prev;

    GDestroyNotify func;
    void *data;
};

static EDList cancel_hook_list = E_DLIST_INITIALISER(cancel_hook_list);

void *mail_cancel_hook_add(GDestroyNotify func, void *data)
{
    struct _cancel_hook_data *d;

    d = g_malloc0(sizeof(*d));
    d->func = func;
    d->data = data;

    MAIL_MT_LOCK(mail_msg_lock);
    e_dlist_addtail(&cancel_hook_list, (EDListNode *)d);
    MAIL_MT_UNLOCK(mail_msg_lock);

    return (void *)d;
}

void mail_cancel_hook_remove(void *handle)
{
    struct _cancel_hook_data *d = handle;

    MAIL_MT_LOCK(mail_msg_lock);
    e_dlist_remove((EDListNode *)d);
    MAIL_MT_UNLOCK(mail_msg_lock);
    g_free(d);
}

void mail_cancel_all(void)
{
    struct _cancel_hook_data *d, *n;

    camel_operation_cancel(NULL);

    /* I can ssee a deadlock coming on ... */
    MAIL_MT_LOCK(mail_msg_lock);
    d = (struct _cancel_hook_data *)cancel_hook_list.head;
    n = d->next;
    while (n) {
        d->func(d->data);
        d = n;
        n = n->next;
    }
    MAIL_MT_UNLOCK(mail_msg_lock);
}

EMsgPort        *mail_gui_port;
static GIOChannel   *mail_gui_channel;
static guint         mail_gui_watch;

/* TODO: Merge these, gui_port2 doesn't do any mail_msg processing on the request (replies, forwards, frees) */
EMsgPort        *mail_gui_port2;
static GIOChannel   *mail_gui_channel2;
static guint         mail_gui_watch2;

EMsgPort        *mail_gui_reply_port;
static GIOChannel   *mail_gui_reply_channel;

/* a couple of global threads available */
#ifdef G_OS_WIN32
#undef mail_thread_queued
static
#endif
EThread *mail_thread_queued;    /* for operations that can (or should) be queued */
EThread *mail_thread_queued_slow;   /* for operations that can (or should) be queued, but take a long time */
EThread *mail_thread_new;   /* for operations that should run in a new thread each time */

#ifdef G_OS_WIN32
EThread *
mail_win32_get_mail_thread_queued (void)
{
    return mail_thread_queued;
}
#endif

static gboolean
mail_msgport_replied(GIOChannel *source, GIOCondition cond, void *d)
{
    EMsgPort *port = (EMsgPort *)d;
    mail_msg_t *m;

    while (( m = (mail_msg_t *)e_msgport_get(port))) {

#ifdef MALLOC_CHECK
        checkmem(m);
        checkmem(m->cancel);
        checkmem(m->priv);
#endif

#ifdef LOG_OPS
        if (log_ops)
            fprintf(log, "%p: Replied to GUI thread (exception `%s'\n", m,
                camel_exception_get_description(&m->ex)?camel_exception_get_description(&m->ex):"None");
#endif

        if (m->ops->reply_msg)
            m->ops->reply_msg(m);
        mail_msg_check_error(m);
        mail_msg_free(m);
    }

    return TRUE;
}

static gboolean
mail_msgport_received(GIOChannel *source, GIOCondition cond, void *d)
{
    EMsgPort *port = (EMsgPort *)d;
    mail_msg_t *m;

    while (( m = (mail_msg_t *)e_msgport_get(port))) {
#ifdef MALLOC_CHECK
        checkmem(m);
        checkmem(m->cancel);
        checkmem(m->priv);
#endif

#ifdef LOG_OPS
        if (log_ops)
            fprintf(log, "%p: Received at GUI thread\n", m);
#endif

        if (m->ops->receive_msg)
            m->ops->receive_msg(m);
        if (m->msg.reply_port)
            e_msgport_reply((EMsg *)m);
        else {
            if (m->ops->reply_msg)
                m->ops->reply_msg(m);
            mail_msg_free(m);
        }
    }

    return TRUE;
}

/* Test code, lighterwight, more configurable calls */
static gboolean
mail_msgport_received2(GIOChannel *source, GIOCondition cond, void *d)
{
    EMsgPort *port = (EMsgPort *)d;
    mail_msg_t *m;

    while (( m = (mail_msg_t *)e_msgport_get(port))) {
#ifdef LOG_OPS
        if (log_ops)
            fprintf(log, "%p: Received at GUI2 thread\n", m);
#endif

        if (m->ops->receive_msg)
            m->ops->receive_msg(m);
        else
            mail_msg_free(m);
    }

    return TRUE;
}


static void
mail_msg_destroy(EThread *e, EMsg *msg, void *data)
{
    mail_msg_t *m = (mail_msg_t *)msg;

#ifdef MALLOC_CHECK
    checkmem(m);
    checkmem(m->cancel);
    checkmem(m->priv);
#endif

    mail_msg_free(m);
}

static void
mail_msg_received(EThread *e, EMsg *msg, void *data)
{
    mail_msg_t *m = (mail_msg_t *)msg;

#ifdef MALLOC_CHECK
    checkmem(m);
    checkmem(m->cancel);
    checkmem(m->priv);
#endif

    if (m->ops->describe_msg) {
        char *text = m->ops->describe_msg(m, FALSE);

#ifdef LOG_OPS
        if (log_ops)
            fprintf(log, "%p: Received at thread %" G_GINT64_MODIFIER "x: '%s'\n", m, e_util_pthread_id(pthread_self()), text);
#endif

        d(printf("message received at thread\n"));
        camel_operation_register(m->cancel);
        camel_operation_start(m->cancel, "%s", text);
        g_free(text);
    }
#ifdef LOG_OPS
    else
        if (log_ops)
            fprintf(log, "%p: Received at thread %" G_GINT64_MODIFIER "x\n", m, e_util_pthread_id(pthread_self()));
#endif

    if (m->ops->receive_msg) {
        mail_enable_stop();
        m->ops->receive_msg(m);
        mail_disable_stop();
    }

    if (m->ops->describe_msg) {
        camel_operation_end(m->cancel);
        camel_operation_unregister(m->cancel);
        MAIL_MT_LOCK(mail_msg_lock);
        camel_operation_unref(m->cancel);
        m->cancel = NULL;
        MAIL_MT_UNLOCK(mail_msg_lock);
    }
}

void mail_msg_cleanup(void)
{
    mail_msg_wait_all();

    e_thread_destroy(mail_thread_queued_slow);
    e_thread_destroy(mail_thread_queued);
    e_thread_destroy(mail_thread_new);

    g_io_channel_unref(mail_gui_channel);
    g_io_channel_unref(mail_gui_reply_channel);

    e_msgport_destroy(mail_gui_port);
    e_msgport_destroy(mail_gui_reply_port);
}

static guint
em_channel_setup(EMsgPort **port, GIOChannel **channel, GIOFunc func)
{
    GSource *source;
    guint id;

    *port = e_msgport_new();
#ifndef G_OS_WIN32
    *channel = g_io_channel_unix_new(e_msgport_fd(*port));
#else
    *channel = g_io_channel_win32_new_socket(e_msgport_fd(*port));
#endif
    source = g_io_create_watch(*channel, G_IO_IN);
    g_source_set_callback(source, (GSourceFunc)func, *port, NULL);
    g_source_set_can_recurse(source, FALSE);
    id = g_source_attach(source, NULL);
    g_source_unref(source);

    return id;
}

void mail_msg_init(void)
{
    em_channel_setup(&mail_gui_reply_port, &mail_gui_reply_channel, mail_msgport_replied);
    mail_gui_watch = em_channel_setup(&mail_gui_port, &mail_gui_channel, mail_msgport_received);
    mail_gui_watch2 = em_channel_setup(&mail_gui_port2, &mail_gui_channel2, mail_msgport_received2);

    mail_thread_queued = e_thread_new(E_THREAD_QUEUE);
    e_thread_set_msg_destroy(mail_thread_queued, mail_msg_destroy, NULL);
    e_thread_set_msg_received(mail_thread_queued, mail_msg_received, NULL);
    e_thread_set_reply_port(mail_thread_queued, mail_gui_reply_port);

    mail_thread_queued_slow = e_thread_new(E_THREAD_QUEUE);
    e_thread_set_msg_destroy(mail_thread_queued_slow, mail_msg_destroy, NULL);
    e_thread_set_msg_received(mail_thread_queued_slow, mail_msg_received, NULL);
    e_thread_set_reply_port(mail_thread_queued_slow, mail_gui_reply_port);

    mail_thread_new = e_thread_new(E_THREAD_NEW);
    e_thread_set_msg_destroy(mail_thread_new, mail_msg_destroy, NULL);
    e_thread_set_msg_received(mail_thread_new, mail_msg_received, NULL);
    e_thread_set_reply_port(mail_thread_new, mail_gui_reply_port);
    e_thread_set_queue_limit(mail_thread_new, 10);

    mail_msg_active_table = g_hash_table_new(NULL, NULL);
    mail_gui_thread = pthread_self();

    mail_async_event = mail_async_event_new();
}

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

/* locks */
static pthread_mutex_t status_lock = PTHREAD_MUTEX_INITIALIZER;

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

struct _proxy_msg {
    struct _mail_msg msg;
    MailAsyncEvent *ea;
    mail_async_event_t type;

    pthread_t thread;
    int have_thread;

    MailAsyncFunc func;
    void *o;
    void *event_data;
    void *data;
};

static void
do_async_event(struct _mail_msg *mm)
{
    struct _proxy_msg *m = (struct _proxy_msg *)mm;

    m->thread = pthread_self();
    m->have_thread = TRUE;
    m->func(m->o, m->event_data, m->data);
    m->have_thread = FALSE;

    g_mutex_lock(m->ea->lock);
    m->ea->tasks = g_slist_remove(m->ea->tasks, m);
    g_mutex_unlock(m->ea->lock);
}

static int
idle_async_event(void *mm)
{
    do_async_event(mm);
    mail_msg_free(mm);

    return FALSE;
}

static struct _mail_msg_op async_event_op = {
    NULL,
    do_async_event,
    NULL,
    NULL,
};

MailAsyncEvent *mail_async_event_new(void)
{
    MailAsyncEvent *ea;

    ea = g_malloc0(sizeof(*ea));
    ea->lock = g_mutex_new();

    return ea;
}

int mail_async_event_emit(MailAsyncEvent *ea, mail_async_event_t type, MailAsyncFunc func, void *o, void *event_data, void *data)
{
    struct _proxy_msg *m;
    int id;
    int ismain = pthread_equal(pthread_self(), mail_gui_thread);

    /* we dont have a reply port for this, we dont care when/if it gets executed, just queue it */
    m = mail_msg_new(&async_event_op, NULL, sizeof(*m));
    m->func = func;
    m->o = o;
    m->event_data = event_data;
    m->data = data;
    m->ea = ea;
    m->type = type;
    m->have_thread = FALSE;

    id = m->msg.seq;
    g_mutex_lock(ea->lock);
    ea->tasks = g_slist_prepend(ea->tasks, m);
    g_mutex_unlock(ea->lock);

    /* We use an idle function instead of our own message port only because the
       gui message ports's notification buffer might overflow and deadlock us */
    if (type == MAIL_ASYNC_GUI) {
        if (ismain)
            g_idle_add(idle_async_event, m);
        else
            e_msgport_put(mail_gui_port, (EMsg *)m);
    } else
        e_thread_put(mail_thread_queued, (EMsg *)m);

    return id;
}

int mail_async_event_destroy(MailAsyncEvent *ea)
{
    int id;
    pthread_t thread = pthread_self();
    struct _proxy_msg *m;

    g_mutex_lock(ea->lock);
    while (ea->tasks) {
        m = ea->tasks->data;
        id = m->msg.seq;
        if (m->have_thread && pthread_equal(m->thread, thread)) {
            g_warning("Destroying async event from inside an event, returning EDEADLK");
            g_mutex_unlock(ea->lock);
            errno = EDEADLK;
            return -1;
        }
        g_mutex_unlock(ea->lock);
        mail_msg_wait(id);
        g_mutex_lock(ea->lock);
    }
    g_mutex_unlock(ea->lock);

    g_mutex_free(ea->lock);
    g_free(ea);

    return 0;
}

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

struct _call_msg {
    struct _mail_msg msg;
    mail_call_t type;
    MailMainFunc func;
    void *ret;
    va_list ap;
};

static void
do_call(struct _mail_msg *mm)
{
    struct _call_msg *m = (struct _call_msg *)mm;
    void *p1, *p2, *p3, *p4, *p5;
    int i1;
    va_list ap;

    G_VA_COPY(ap, m->ap);

    switch(m->type) {
    case MAIL_CALL_p_p:
        p1 = va_arg(ap, void *);
        m->ret = m->func(p1);
        break;
    case MAIL_CALL_p_pp:
        p1 = va_arg(ap, void *);
        p2 = va_arg(ap, void *);
        m->ret = m->func(p1, p2);
        break;
    case MAIL_CALL_p_ppp:
        p1 = va_arg(ap, void *);
        p2 = va_arg(ap, void *);
        p3 = va_arg(ap, void *);
        m->ret = m->func(p1, p2, p3);
        break;
    case MAIL_CALL_p_pppp:
        p1 = va_arg(ap, void *);
        p2 = va_arg(ap, void *);
        p3 = va_arg(ap, void *);
        p4 = va_arg(ap, void *);
        m->ret = m->func(p1, p2, p3, p4);
        break;
    case MAIL_CALL_p_ppppp:
        p1 = va_arg(ap, void *);
        p2 = va_arg(ap, void *);
        p3 = va_arg(ap, void *);
        p4 = va_arg(ap, void *);
        p5 = va_arg(ap, void *);
        m->ret = m->func(p1, p2, p3, p4, p5);
        break;
    case MAIL_CALL_p_ppippp:
        p1 = va_arg(ap, void *);
        p2 = va_arg(ap, void *);
        i1 = va_arg(ap, int);
        p3 = va_arg(ap, void *);
        p4 = va_arg(ap, void *);
        p5 = va_arg(ap, void *);
        m->ret = m->func(p1, p2, i1, p3, p4, p5);
        break;
    }
}

static struct _mail_msg_op mail_call_op = {
    NULL,
    do_call,
    NULL,
    NULL,
};

void *mail_call_main(mail_call_t type, MailMainFunc func, ...)
{
    struct _call_msg *m;
    void *ret;
    va_list ap;
    EMsgPort *reply = NULL;
    int ismain = pthread_equal(pthread_self(), mail_gui_thread);

    va_start(ap, func);

    if (!ismain)
        reply = e_msgport_new();

    m = mail_msg_new(&mail_call_op, reply, sizeof(*m));
    m->type = type;
    m->func = func;
    G_VA_COPY(m->ap, ap);

    if (!ismain) {
        e_msgport_put(mail_gui_port, (EMsg *)m);
        e_msgport_wait(reply);
        e_msgport_destroy(reply);
    } else {
        do_call(&m->msg);
    }

    va_end(ap);

    ret = m->ret;
    mail_msg_free(m);

    return ret;
}

/* ********************************************************************** */
/* locked via status_lock */
static int busy_state;

static void do_set_busy(struct _mail_msg *mm)
{
    set_stop(busy_state > 0);
}

static struct _mail_msg_op set_busy_op = {
    NULL,
    do_set_busy,
    NULL,
    NULL,
};

void mail_enable_stop(void)
{
    struct _mail_msg *m;

    MAIL_MT_LOCK(status_lock);
    busy_state++;
    if (busy_state == 1) {
        m = mail_msg_new(&set_busy_op, NULL, sizeof(*m));
        e_msgport_put(mail_gui_port, (EMsg *)m);
    }
    MAIL_MT_UNLOCK(status_lock);
}

void mail_disable_stop(void)
{
    struct _mail_msg *m;

    MAIL_MT_LOCK(status_lock);
    busy_state--;
    if (busy_state == 0) {
        m = mail_msg_new(&set_busy_op, NULL, sizeof(*m));
        e_msgport_put(mail_gui_port, (EMsg *)m);
    }
    MAIL_MT_UNLOCK(status_lock);
}

static void
operation_cancel (CamelOperation *p)
{
    camel_operation_cancel (p);
}

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

struct _op_status_msg {
    struct _mail_msg msg;

    struct _CamelOperation *op;
    char *what;
    int pc;
    void *data;
};

static void do_op_status(struct _mail_msg *mm)
{
    EActivityHandler *activity_handler = mail_component_peek_activity_handler (mail_component_peek ());
    struct _op_status_msg *m = (struct _op_status_msg *)mm;
    struct _mail_msg *msg;
    struct _mail_msg_priv *data;
    char *out, *p, *o, c;
    int pc;

    g_return_if_fail (pthread_equal(mail_gui_thread, pthread_self ()));

    MAIL_MT_LOCK (mail_msg_lock);

    msg = g_hash_table_lookup (mail_msg_active_table, m->data);

    if (msg == NULL) {
        MAIL_MT_UNLOCK (mail_msg_lock);
        return;
    }

    data = msg->priv;

    out = alloca (strlen (m->what) * 2 + 1);
    o = out;
    p = m->what;
    while ((c = *p++)) {
        if (c == '%')
            *o++ = '%';
        *o++ = c;
    }
    *o = 0;

    pc = m->pc;

    if (data->activity_id == 0) {
        char *what;

        /* its being created/removed?  well leave it be */
        if (data->activity_state == 1 || data->activity_state == 3) {
            MAIL_MT_UNLOCK (mail_msg_lock);
            return;
        } else {
            data->activity_state = 1;

            if (progress_icon == NULL)
                progress_icon = e_icon_factory_get_icon ("mail-unread", E_ICON_SIZE_MENU);

            MAIL_MT_UNLOCK (mail_msg_lock);
            if (msg->ops->describe_msg)
                what = msg->ops->describe_msg (msg, FALSE);
            else if (m->what)
                what = g_strdup (m->what);
            /* uncommenting because message is not very useful for a user, see bug 271734*/
            else {
                what = g_strdup("");
            }

            data->activity_id = e_activity_handler_cancelable_operation_started (activity_handler, "evolution-mail", progress_icon, what, TRUE, operation_cancel, msg->cancel);

            g_free (what);
            MAIL_MT_LOCK (mail_msg_lock);
            if (data->activity_state == 3) {
                int activity_id = data->activity_id;

                MAIL_MT_UNLOCK (mail_msg_lock);
                if (msg->cancel) {
                    camel_operation_mute (msg->cancel);
                    camel_operation_unref (msg->cancel);
                }
                camel_exception_clear (&msg->ex);
                g_free (msg->priv);
                g_free (msg);

                if (activity_id != 0)
                    mail_async_event_emit (mail_async_event, MAIL_ASYNC_GUI, (MailAsyncFunc) end_event_callback,
                                NULL, GINT_TO_POINTER (activity_id), NULL);
            } else {
                data->activity_state = 2;
                MAIL_MT_UNLOCK (mail_msg_lock);
            }
            return;
        }
    } else if (data->activity_id != 0) {
        MAIL_MT_UNLOCK (mail_msg_lock);
        e_activity_handler_operation_progressing (activity_handler, data->activity_id, out, (double)(pc/100.0));
    } else {
        MAIL_MT_UNLOCK (mail_msg_lock);
    }
}

static void
do_op_status_free (struct _mail_msg *mm)
{
    struct _op_status_msg *m = (struct _op_status_msg *)mm;

    g_free (m->what);
}

static struct _mail_msg_op op_status_op = {
    NULL,
    do_op_status,
    NULL,
    do_op_status_free,
};

static void
mail_operation_status (struct _CamelOperation *op, const char *what, int pc, void *data)
{
    struct _op_status_msg *m;

    d(printf("got operation statys: %s %d%%\n", what, pc));

    m = mail_msg_new(&op_status_op, NULL, sizeof(*m));
    m->op = op;
    m->what = g_strdup(what);
    switch (pc) {
    case CAMEL_OPERATION_START:
        pc = 0;
        break;
    case CAMEL_OPERATION_END:
        pc = 100;
        break;
    }
    m->pc = pc;
    m->data = data;
    e_msgport_put(mail_gui_port, (EMsg *)m);
}

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

static void
set_stop (int sensitive)
{
    static int last = FALSE;

    if (last == sensitive)
        return;

    /*bonobo_ui_component_set_prop (uic, "/commands/MailStop", "sensitive", sensitive ? "1" : "0", NULL);*/
    last = sensitive;
}