aboutsummaryrefslogtreecommitdiffstats
path: root/libemail-engine/e-mail-utils.c
blob: 20b96f7d2679421b8bd6ed467429cada5346f867 (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
/*
 * 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:
 *      Srinivasa Ragavan <sragavan@gnome.org>
 *
 *
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>

#include <glib/gstdio.h>

#ifdef G_OS_WIN32
/* Work around namespace clobbage in <windows.h> */
#define DATADIR windows_DATADIR
#include <windows.h>
#undef DATADIR
#undef interface
#endif

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

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

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

#define d(x)

/**
 * em_utils_folder_is_drafts:
 * @registry: an #ESourceRegistry
 * @folder: a #CamelFolder
 *
 * Decides if @folder is a Drafts folder.
 *
 * Returns %TRUE if this is a Drafts folder or %FALSE otherwise.
 **/
gboolean
em_utils_folder_is_drafts (ESourceRegistry *registry,
                           CamelFolder *folder)
{
    CamelFolder *local_drafts_folder;
    CamelSession *session;
    CamelStore *store;
    GList *list, *iter;
    gchar *folder_uri;
    gboolean is_drafts = FALSE;
    const gchar *extension_name;

    g_return_val_if_fail (CAMEL_IS_FOLDER (folder), FALSE);

    store = camel_folder_get_parent_store (folder);
    session = camel_service_get_session (CAMEL_SERVICE (store));

    local_drafts_folder =
        e_mail_session_get_local_folder (
        E_MAIL_SESSION (session), E_MAIL_LOCAL_FOLDER_DRAFTS);

    if (folder == local_drafts_folder)
        return TRUE;

    folder_uri = e_mail_folder_uri_from_folder (folder);

    store = camel_folder_get_parent_store (folder);
    session = camel_service_get_session (CAMEL_SERVICE (store));

    extension_name = E_SOURCE_EXTENSION_MAIL_COMPOSITION;
    list = e_source_registry_list_sources (registry, extension_name);

    for (iter = list; iter != NULL; iter = g_list_next (iter)) {
        ESource *source = E_SOURCE (iter->data);
        ESourceExtension *extension;
        const gchar *drafts_folder_uri;

        extension = e_source_get_extension (source, extension_name);

        drafts_folder_uri =
            e_source_mail_composition_get_drafts_folder (
            E_SOURCE_MAIL_COMPOSITION (extension));

        if (drafts_folder_uri != NULL)
            is_drafts = e_mail_folder_uri_equal (
                session, folder_uri, drafts_folder_uri);

        if (is_drafts)
            break;
    }

    g_list_free_full (list, (GDestroyNotify) g_object_unref);
    g_free (folder_uri);

    return is_drafts;
}

/**
 * em_utils_folder_is_templates:
 * @registry: an #ESourceRegistry
 * @folder: a #CamelFolder
 *
 * Decides if @folder is a Templates folder.
 *
 * Returns %TRUE if this is a Templates folder or %FALSE otherwise.
 **/

gboolean
em_utils_folder_is_templates (ESourceRegistry *registry,
                              CamelFolder *folder)
{
    CamelFolder *local_templates_folder;
    CamelSession *session;
    CamelStore *store;
    GList *list, *iter;
    gchar *folder_uri;
    gboolean is_templates = FALSE;
    const gchar *extension_name;

    g_return_val_if_fail (CAMEL_IS_FOLDER (folder), FALSE);

    store = camel_folder_get_parent_store (folder);
    session = camel_service_get_session (CAMEL_SERVICE (store));

    local_templates_folder =
        e_mail_session_get_local_folder (
        E_MAIL_SESSION (session), E_MAIL_LOCAL_FOLDER_TEMPLATES);

    if (folder == local_templates_folder)
        return TRUE;

    folder_uri = e_mail_folder_uri_from_folder (folder);

    store = camel_folder_get_parent_store (folder);
    session = camel_service_get_session (CAMEL_SERVICE (store));

    extension_name = E_SOURCE_EXTENSION_MAIL_COMPOSITION;
    list = e_source_registry_list_sources (registry, extension_name);

    for (iter = list; iter != NULL; iter = g_list_next (iter)) {
        ESource *source = E_SOURCE (iter->data);
        ESourceExtension *extension;
        const gchar *templates_folder_uri;

        extension = e_source_get_extension (source, extension_name);

        templates_folder_uri =
            e_source_mail_composition_get_templates_folder (
            E_SOURCE_MAIL_COMPOSITION (extension));

        if (templates_folder_uri != NULL)
            is_templates = e_mail_folder_uri_equal (
                session, folder_uri, templates_folder_uri);

        if (is_templates)
            break;
    }

    g_list_free_full (list, (GDestroyNotify) g_object_unref);
    g_free (folder_uri);

    return is_templates;
}

/**
 * em_utils_folder_is_sent:
 * @registry: an #ESourceRegistry
 * @folder: a #CamelFolder
 *
 * Decides if @folder is a Sent folder.
 *
 * Returns %TRUE if this is a Sent folder or %FALSE otherwise.
 **/
gboolean
em_utils_folder_is_sent (ESourceRegistry *registry,
                         CamelFolder *folder)
{
    CamelFolder *local_sent_folder;
    CamelSession *session;
    CamelStore *store;
    GList *list, *iter;
    gchar *folder_uri;
    gboolean is_sent = FALSE;
    const gchar *extension_name;

    g_return_val_if_fail (CAMEL_IS_FOLDER (folder), FALSE);

    store = camel_folder_get_parent_store (folder);
    session = camel_service_get_session (CAMEL_SERVICE (store));

    local_sent_folder =
        e_mail_session_get_local_folder (
        E_MAIL_SESSION (session), E_MAIL_LOCAL_FOLDER_SENT);

    if (folder == local_sent_folder)
        return TRUE;

    folder_uri = e_mail_folder_uri_from_folder (folder);

    store = camel_folder_get_parent_store (folder);
    session = camel_service_get_session (CAMEL_SERVICE (store));

    extension_name = E_SOURCE_EXTENSION_MAIL_SUBMISSION;
    list = e_source_registry_list_sources (registry, extension_name);

    for (iter = list; iter != NULL; iter = g_list_next (iter)) {
        ESource *source = E_SOURCE (iter->data);
        ESourceExtension *extension;
        const gchar *sent_folder_uri;

        extension = e_source_get_extension (source, extension_name);

        sent_folder_uri =
            e_source_mail_submission_get_sent_folder (
            E_SOURCE_MAIL_SUBMISSION (extension));

        if (sent_folder_uri != NULL)
            is_sent = e_mail_folder_uri_equal (
                session, folder_uri, sent_folder_uri);

        if (is_sent)
            break;
    }

    g_list_free_full (list, (GDestroyNotify) g_object_unref);
    g_free (folder_uri);

    return is_sent;
}

/**
 * em_utils_folder_is_outbox:
 * @registry: an #ESourceRegistry
 * @folder: a #CamelFolder
 *
 * Decides if @folder is an Outbox folder.
 *
 * Returns %TRUE if this is an Outbox folder or %FALSE otherwise.
 **/
gboolean
em_utils_folder_is_outbox (ESourceRegistry *registry,
                           CamelFolder *folder)
{
    CamelStore *store;
    CamelSession *session;
    CamelFolder *local_outbox_folder;

    g_return_val_if_fail (CAMEL_IS_FOLDER (folder), FALSE);

    store = camel_folder_get_parent_store (folder);
    session = camel_service_get_session (CAMEL_SERVICE (store));

    local_outbox_folder =
        e_mail_session_get_local_folder (
        E_MAIL_SESSION (session), E_MAIL_LOCAL_FOLDER_OUTBOX);

    return (folder == local_outbox_folder);
}

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

struct TryOpenEBookStruct {
    GError **error;
    EFlag *flag;
    gboolean result;
};

static void
try_open_book_client_cb (GObject *source_object,
                         GAsyncResult *result,
                         gpointer closure)
{
    EBookClient *book_client = E_BOOK_CLIENT (source_object);
    struct TryOpenEBookStruct *data = (struct TryOpenEBookStruct *) closure;
    GError *error = NULL;

    if (!data)
        return;

    e_client_open_finish (E_CLIENT (book_client), result, &error);

    data->result = error == NULL;

    if (!data->result) {
        g_clear_error (data->error);
        g_propagate_error (data->error, error);
    }

    e_flag_set (data->flag);
}

/*
 * try_open_book_client:
 * Tries to open address book asynchronously, but acts as synchronous.
 * The advantage is it checks periodically whether the camel_operation
 * has been canceled or not, and if so, then stops immediately, with
 * result FALSE. Otherwise returns same as e_client_open()
 */
static gboolean
try_open_book_client (EBookClient *book_client,
                      gboolean only_if_exists,
                      GCancellable *cancellable,
                      GError **error)
{
    struct TryOpenEBookStruct data;
    gboolean canceled = FALSE;
    EFlag *flag = e_flag_new ();

    data.error = error;
    data.flag = flag;
    data.result = FALSE;

    e_client_open (
        E_CLIENT (book_client), only_if_exists,
        cancellable, try_open_book_client_cb, &data);

    while (canceled = g_cancellable_is_cancelled (cancellable),
            !canceled && !e_flag_is_set (flag)) {
        GTimeVal wait;

        g_get_current_time (&wait);
        g_time_val_add (&wait, 250000); /* waits 250ms */

        e_flag_timed_wait (flag, &wait);
    }

    if (canceled) {
        g_cancellable_cancel (cancellable);

        g_clear_error (error);
        g_propagate_error (
            error, e_client_error_create (
            E_CLIENT_ERROR_CANCELLED, NULL));
    }

    e_flag_wait (flag);
    e_flag_free (flag);

    return data.result && (!error || !*error);
}

#define NOT_FOUND_BOOK (GINT_TO_POINTER (1))

G_LOCK_DEFINE_STATIC (contact_cache);

/* key is lowercased contact email; value is EBook pointer
 * (just for comparison) where it comes from */
static GHashTable *contact_cache = NULL;

/* key is source ID; value is pointer to EBook */
static GHashTable *emu_books_hash = NULL;

/* key is source ID; value is same pointer as key; this is hash of
 * broken books, which failed to open for some reason */
static GHashTable *emu_broken_books_hash = NULL;

static gboolean
search_address_in_addressbooks (ESourceRegistry *registry,
                                const gchar *address,
                                gboolean local_only,
                                gboolean (*check_contact) (EContact *contact,
                                                           gpointer user_data),
                                gpointer user_data,
                                GCancellable *cancellable)
{
    GList *list, *link;
    GList *addr_sources = NULL;
    gboolean found = FALSE, stop = FALSE, found_any = FALSE;
    gchar *lowercase_addr;
    gpointer ptr;
    EBookQuery *book_query;
    gchar *query;
    const gchar *extension_name;

    if (!address || !*address)
        return FALSE;

    G_LOCK (contact_cache);

    if (emu_books_hash == NULL) {
        emu_books_hash = g_hash_table_new_full (
            g_str_hash, g_str_equal, g_free, g_object_unref);
        emu_broken_books_hash = g_hash_table_new_full (
            g_str_hash, g_str_equal, g_free, NULL);
        contact_cache = g_hash_table_new_full (
            g_str_hash, g_str_equal, g_free, NULL);
    }

    lowercase_addr = g_utf8_strdown (address, -1);
    ptr = g_hash_table_lookup (contact_cache, lowercase_addr);
    if (ptr != NULL && (check_contact == NULL || ptr == NOT_FOUND_BOOK)) {
        g_free (lowercase_addr);
        G_UNLOCK (contact_cache);
        return ptr != NOT_FOUND_BOOK;
    }

    G_UNLOCK (contact_cache);

    book_query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_IS, address);
    query = e_book_query_to_string (book_query);
    e_book_query_unref (book_query);

    extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
    list = e_source_registry_list_sources (registry, extension_name);

    for (link = list; link != NULL; link = g_list_next (link)) {
        ESource *source = E_SOURCE (link->data);
        ESourceExtension *extension;
        const gchar *backend_name;
        gboolean source_is_local;
        gboolean autocomplete;

        extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;
        extension = e_source_get_extension (source, extension_name);

        backend_name = e_source_backend_get_backend_name (
            E_SOURCE_BACKEND (extension));

        source_is_local = (g_strcmp0 (backend_name, "local") == 0);

        if (local_only && !source_is_local)
            continue;

        extension_name = E_SOURCE_EXTENSION_AUTOCOMPLETE;
        extension = e_source_get_extension (source, extension_name);

        autocomplete = e_source_autocomplete_get_include_me (
            E_SOURCE_AUTOCOMPLETE (extension));

        if (!autocomplete)
            continue;

        addr_sources = g_list_prepend (
            addr_sources, g_object_ref (source));
    }

    g_list_free_full (list, (GDestroyNotify) g_object_unref);

    for (link = addr_sources; !stop && !found && link != NULL; link = g_list_next (link)) {
        ESource *source = E_SOURCE (link->data);
        GSList *contacts;
        EBookClient *book_client = NULL;
        gboolean cached_book = FALSE;
        const gchar *display_name;
        const gchar *uid;
        GError *err = NULL;

        uid = e_source_get_uid (source);
        display_name = e_source_get_display_name (source);

        G_LOCK (contact_cache);

        /* can be NULL on quit */
        if (!emu_books_hash || !emu_broken_books_hash) {
            G_UNLOCK (contact_cache);
            break;
        }

        /* failed to load this book last time, skip it now */
        if (g_hash_table_lookup (emu_broken_books_hash, uid) != NULL) {
            d(printf ("%s: skipping broken book '%s'\n",
                G_STRFUNC, display_name));
            G_UNLOCK (contact_cache);
            continue;
        }

        d(printf(" checking '%s'\n", e_source_get_uri(source)));

        book_client = g_hash_table_lookup (emu_books_hash, uid);
        if (!book_client) {
            G_UNLOCK (contact_cache);

            book_client = e_book_client_new (source, &err);

            if (book_client == NULL) {
                if (err && (g_error_matches (err, E_CLIENT_ERROR, E_CLIENT_ERROR_CANCELLED) ||
                    g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))) {
                    stop = TRUE;
                } else if (err) {
                    gchar *source_uid;

                    source_uid = g_strdup (uid);

                    G_LOCK (contact_cache);

                    /* can be NULL on quit */
                    if (emu_broken_books_hash) {
                        g_hash_table_insert (
                            emu_broken_books_hash,
                            source_uid, source_uid);
                    } else {
                        G_UNLOCK (contact_cache);
                        g_clear_error (&err);
                        break;
                    }

                    G_UNLOCK (contact_cache);

                    g_warning (
                        "%s: Unable to create addressbook '%s': %s",
                        G_STRFUNC,
                        display_name,
                        err->message);
                }
                g_clear_error (&err);
            } else if (!stop && !try_open_book_client (book_client, TRUE, cancellable, &err)) {
                g_object_unref (book_client);
                book_client = NULL;

                if (err && (g_error_matches (err, E_CLIENT_ERROR, E_CLIENT_ERROR_CANCELLED) ||
                    g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))) {
                    stop = TRUE;
                } else if (err) {
                    gchar *source_uid;

                    source_uid = g_strdup (uid);

                    G_LOCK (contact_cache);

                    /* can be NULL on quit */
                    if (emu_broken_books_hash) {
                        g_hash_table_insert (
                            emu_broken_books_hash,
                            source_uid, source_uid);
                    } else {
                        G_UNLOCK (contact_cache);
                        g_clear_error (&err);
                        break;
                    }

                    G_UNLOCK (contact_cache);

                    g_warning (
                        "%s: Unable to open addressbook '%s': %s",
                        G_STRFUNC,
                        display_name,
                        err->message);
                }
                g_clear_error (&err);
            }
        } else {
            G_UNLOCK (contact_cache);

            cached_book = TRUE;
        }

        if (book_client && !stop &&
            e_book_client_get_contacts_sync (
            book_client, query, &contacts, cancellable, &err)) {
            if (contacts != NULL) {
                if (!found_any) {
                    G_LOCK (contact_cache);

                    /* can be NULL on quit */
                    if (contact_cache) {
                        g_hash_table_insert (
                            contact_cache,
                            g_strdup (lowercase_addr),
                            book_client);
                    } else {
                        G_UNLOCK (contact_cache);
                        break;
                    }

                    G_UNLOCK (contact_cache);
                }
                found_any = TRUE;

                if (check_contact) {
                    GSList *l;

                    for (l = contacts; l && !found; l = l->next) {
                        EContact *contact = l->data;

                        found = check_contact (contact, user_data);
                    }
                } else {
                    found = TRUE;
                }

                g_slist_foreach (contacts, (GFunc) g_object_unref, NULL);
                g_slist_free (contacts);
            }
        } else if (book_client) {
            stop = stop || (err &&
                (g_error_matches (err, E_CLIENT_ERROR, E_CLIENT_ERROR_CANCELLED) ||
                 g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)));
            if (err && !stop) {
                gchar *source_uid = g_strdup (uid);

                G_LOCK (contact_cache);

                /* can be NULL on quit */
                if (emu_broken_books_hash) {
                    g_hash_table_insert (
                        emu_broken_books_hash,
                        source_uid, source_uid);
                } else {
                    G_UNLOCK (contact_cache);
                    break;
                }

                G_UNLOCK (contact_cache);

                g_warning (
                    "%s: Can't get contacts from '%s': %s",
                    G_STRFUNC,
                    display_name,
                    err->message);
            }
            g_clear_error (&err);
        }

        if (stop && !cached_book && book_client) {
            g_object_unref (book_client);
        } else if (!stop && book_client && !cached_book) {
            G_LOCK (contact_cache);

            /* can be NULL on quit */
            if (emu_books_hash) {
                g_hash_table_insert (
                    emu_books_hash, g_strdup (uid), book_client);
            } else {
                G_UNLOCK (contact_cache);
                break;
            }

            G_UNLOCK (contact_cache);
        }
    }

    g_list_free_full (addr_sources, (GDestroyNotify) g_object_unref);

    g_free (query);

    G_LOCK (contact_cache);

    if (!found_any && contact_cache) {
        g_hash_table_insert (contact_cache, lowercase_addr, NOT_FOUND_BOOK);
        lowercase_addr = NULL;
    }

    G_UNLOCK (contact_cache);

    g_free (lowercase_addr);

    return found_any;
}

gboolean
em_utils_in_addressbook (ESourceRegistry *registry,
                         CamelInternetAddress *iaddr,
                         gboolean local_only,
                         GCancellable *cancellable)
{
    const gchar *addr;

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);

    /* TODO: check all addresses? */
    if (iaddr == NULL || !camel_internet_address_get (iaddr, 0, NULL, &addr))
        return FALSE;

    return search_address_in_addressbooks (
        registry, addr, local_only, NULL, NULL, cancellable);
}

static gboolean
extract_photo_data (EContact *contact,
                    gpointer user_data)
{
    EContactPhoto **photo = user_data;

    g_return_val_if_fail (contact != NULL, FALSE);
    g_return_val_if_fail (user_data != NULL, FALSE);

    *photo = e_contact_get (contact, E_CONTACT_PHOTO);
    if (!*photo)
        *photo = e_contact_get (contact, E_CONTACT_LOGO);

    return *photo != NULL;
}

typedef struct _PhotoInfo {
    gchar *address;
    EContactPhoto *photo;
} PhotoInfo;

static void
emu_free_photo_info (PhotoInfo *pi)
{
    if (!pi)
        return;

    if (pi->address)
        g_free (pi->address);
    if (pi->photo)
        e_contact_photo_free (pi->photo);
    g_free (pi);
}

G_LOCK_DEFINE_STATIC (photos_cache);
static GSList *photos_cache = NULL; /* list of PhotoInfo-s */

CamelMimePart *
em_utils_contact_photo (ESourceRegistry *registry,
                        CamelInternetAddress *cia,
                        gboolean local_only,
                        GCancellable *cancellable)
{
    const gchar *addr = NULL;
    CamelMimePart *part = NULL;
    EContactPhoto *photo = NULL;
    GSList *p, *last = NULL;
    gint cache_len;

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);

    if (cia == NULL || !camel_internet_address_get (cia, 0, NULL, &addr) || !addr) {
        return NULL;
    }

    G_LOCK (photos_cache);

    /* search a cache first */
    cache_len = 0;
    last = NULL;
    for (p = photos_cache; p; p = p->next) {
        PhotoInfo *pi = p->data;

        if (!pi)
            continue;

        if (g_ascii_strcasecmp (addr, pi->address) == 0) {
            photo = pi->photo;
            break;
        }

        cache_len++;
        last = p;
    }

    G_UNLOCK (photos_cache);

    /* !p means the address had not been found in the cache */
    if (!p && search_address_in_addressbooks (
        registry, addr, local_only, extract_photo_data, &photo, cancellable)) {

        PhotoInfo *pi;

        G_LOCK (photos_cache);

        /* keep only up to 10 photos in memory */
        if (last && (cache_len >= 10)) {
            pi = last->data;
            photos_cache = g_slist_remove (photos_cache, pi);

            if (pi)
                emu_free_photo_info (pi);
        }

        pi = g_new0 (PhotoInfo, 1);
        pi->address = g_strdup (addr);
        pi->photo = photo;

        photos_cache = g_slist_prepend (photos_cache, pi);

        G_UNLOCK (photos_cache);
    }

    /* some photo found, use it */
    if (photo) {
        /* Form a mime part out of the photo */
        part = camel_mime_part_new ();

        if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
            camel_mime_part_set_content (part,
                (const gchar *) photo->data.inlined.data,
                photo->data.inlined.length, "image/jpeg");
        } else {
            gchar *s = g_filename_from_uri (photo->data.uri, NULL, NULL);
            camel_mime_part_set_filename (part, s);
            g_free (s);
        }
    }

    return part;
}

/* list of email addresses (strings) to remove from local cache of photos and
 * contacts, but only if the photo doesn't exist or is an not-found contact */
void
emu_remove_from_mail_cache (const GSList *addresses)
{
    const GSList *a;
    GSList *p;
    CamelInternetAddress *cia;

    cia = camel_internet_address_new ();

    for (a = addresses; a; a = a->next) {
        const gchar *addr = NULL;

        if (!a->data)
            continue;

        if (camel_address_decode ((CamelAddress *) cia, a->data) != -1 &&
            camel_internet_address_get (cia, 0, NULL, &addr) && addr) {
            gchar *lowercase_addr = g_utf8_strdown (addr, -1);

            G_LOCK (contact_cache);
            if (g_hash_table_lookup (contact_cache, lowercase_addr) == NOT_FOUND_BOOK)
                g_hash_table_remove (contact_cache, lowercase_addr);
            G_UNLOCK (contact_cache);

            g_free (lowercase_addr);

            G_LOCK (photos_cache);
            for (p = photos_cache; p; p = p->next) {
                PhotoInfo *pi = p->data;

                if (pi && !pi->photo && g_ascii_strcasecmp (pi->address, addr) == 0) {
                    photos_cache = g_slist_remove (photos_cache, pi);
                    emu_free_photo_info (pi);
                    break;
                }
            }
            G_UNLOCK (photos_cache);
        }
    }

    g_object_unref (cia);
}

void
emu_remove_from_mail_cache_1 (const gchar *address)
{
    GSList *l;

    g_return_if_fail (address != NULL);

    l = g_slist_append (NULL, (gpointer) address);

    emu_remove_from_mail_cache (l);

    g_slist_free (l);
}

/* frees all data created by call of em_utils_in_addressbook() or
 * em_utils_contact_photo() */
void
emu_free_mail_cache (void)
{
    G_LOCK (contact_cache);

    if (emu_books_hash) {
        g_hash_table_destroy (emu_books_hash);
        emu_books_hash = NULL;
    }

    if (emu_broken_books_hash) {
        g_hash_table_destroy (emu_broken_books_hash);
        emu_broken_books_hash = NULL;
    }

    if (contact_cache) {
        g_hash_table_destroy (contact_cache);
        contact_cache = NULL;
    }

    G_UNLOCK (contact_cache);

    G_LOCK (photos_cache);

    g_slist_foreach (photos_cache, (GFunc) emu_free_photo_info, NULL);
    g_slist_free (photos_cache);
    photos_cache = NULL;

    G_UNLOCK (photos_cache);
}

static ESource *
guess_mail_account_from_folder (ESourceRegistry *registry,
                                CamelFolder *folder)
{
    ESource *source;
    CamelStore *store;
    const gchar *uid;

    /* Lookup an ESource by CamelStore UID. */
    store = camel_folder_get_parent_store (folder);
    uid = camel_service_get_uid (CAMEL_SERVICE (store));
    source = e_source_registry_ref_source (registry, uid);

    /* If we found an ESource, make sure it's a mail account. */
    if (source != NULL) {
        const gchar *extension_name;

        extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
        if (!e_source_has_extension (source, extension_name)) {
            g_object_unref (source);
            source = NULL;
        }
    }

    return source;
}

static ESource *
guess_mail_account_from_message (ESourceRegistry *registry,
                                 CamelMimeMessage *message)
{
    ESource *source = NULL;
    const gchar *uid;

    /* Lookup an ESource by 'X-Evolution-Source' header. */
    uid = camel_mime_message_get_source (message);
    if (uid != NULL)
        source = e_source_registry_ref_source (registry, uid);

    /* If we found an ESource, make sure it's a mail account. */
    if (source != NULL) {
        const gchar *extension_name;

        extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
        if (!e_source_has_extension (source, extension_name)) {
            g_object_unref (source);
            source = NULL;
        }
    }

    return source;
}

ESource *
em_utils_guess_mail_account (ESourceRegistry *registry,
                             CamelMimeMessage *message,
                             CamelFolder *folder)
{
    ESource *source = NULL;
    const gchar *newsgroups;

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
    g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), NULL);

    if (folder != NULL)
        g_return_val_if_fail (CAMEL_IS_FOLDER (folder), NULL);

    /* check for newsgroup header */
    newsgroups = camel_medium_get_header (
        CAMEL_MEDIUM (message), "Newsgroups");
    if (folder != NULL && newsgroups != NULL)
        source = guess_mail_account_from_folder (registry, folder);

    /* check for source folder */
    if (source == NULL && folder != NULL)
        source = guess_mail_account_from_folder (registry, folder);

    /* then message source */
    if (source == NULL)
        source = guess_mail_account_from_message (registry, message);

    return source;
}

ESource *
em_utils_guess_mail_identity (ESourceRegistry *registry,
                              CamelMimeMessage *message,
                              CamelFolder *folder)
{
    ESource *source;
    ESourceExtension *extension;
    const gchar *extension_name;
    const gchar *uid;

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
    g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), NULL);

    if (folder != NULL)
        g_return_val_if_fail (CAMEL_IS_FOLDER (folder), NULL);

    source = em_utils_guess_mail_account (registry, message, folder);

    if (source == NULL)
        return NULL;

    extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
    extension = e_source_get_extension (source, extension_name);

    uid = e_source_mail_account_get_identity_uid (
        E_SOURCE_MAIL_ACCOUNT (extension));
    if (uid == NULL)
        return NULL;

    source = e_source_registry_ref_source (registry, uid);
    if (source == NULL)
        return NULL;

    extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
    if (!e_source_has_extension (source, extension_name)) {
        g_object_unref (source);
        return NULL;
    }

    return source;
}

static gboolean
mail_account_in_recipients (ESourceRegistry *registry,
                            ESource *source,
                            GHashTable *recipients)
{
    ESourceExtension *extension;
    const gchar *extension_name;
    const gchar *uid;
    gboolean match = FALSE;
    gchar *address;

    /* Disregard disabled mail accounts. */
    if (!e_source_get_enabled (source))
        return FALSE;

    extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
    extension = e_source_get_extension (source, extension_name);

    uid = e_source_mail_account_get_identity_uid (
        E_SOURCE_MAIL_ACCOUNT (extension));
    if (uid == NULL)
        return FALSE;

    source = e_source_registry_ref_source (registry, uid);
    if (source == NULL)
        return FALSE;

    extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
    if (!e_source_has_extension (source, extension_name)) {
        g_object_unref (source);
        return FALSE;
    }

    extension = e_source_get_extension (source, extension_name);

    address = e_source_mail_identity_dup_address (
        E_SOURCE_MAIL_IDENTITY (extension));

    g_object_unref (source);

    if (address != NULL) {
        match = (g_hash_table_lookup (recipients, address) != NULL);
        g_free (address);
    }

    return match;
}

ESource *
em_utils_guess_mail_account_with_recipients (ESourceRegistry *registry,
                                             CamelMimeMessage *message,
                                             CamelFolder *folder)
{
    ESource *source = NULL;
    GHashTable *recipients;
    CamelInternetAddress *addr;
    GList *list, *iter;
    const gchar *extension_name;
    const gchar *type;
    const gchar *key;

    /* This policy is subject to debate and tweaking,
     * but please also document the rational here. */

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
    g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), NULL);

    /* Build a set of email addresses in which to test for membership.
     * Only the keys matter here; the values just need to be non-NULL. */
    recipients = g_hash_table_new (g_str_hash, g_str_equal);

    type = CAMEL_RECIPIENT_TYPE_TO;
    addr = camel_mime_message_get_recipients (message, type);
    if (addr != NULL) {
        gint index = 0;

        while (camel_internet_address_get (addr, index++, NULL, &key))
            g_hash_table_insert (
                recipients, (gpointer) key,
                GINT_TO_POINTER (1));
    }

    type = CAMEL_RECIPIENT_TYPE_CC;
    addr = camel_mime_message_get_recipients (message, type);
    if (addr != NULL) {
        gint index = 0;

        while (camel_internet_address_get (addr, index++, NULL, &key))
            g_hash_table_insert (
                recipients, (gpointer) key,
                GINT_TO_POINTER (1));
    }

    /* First Preference: We were given a folder that maps to an
     * enabled mail account, and that account's address appears
     * in the list of To: or Cc: recipients. */

    if (folder != NULL)
        source = guess_mail_account_from_folder (registry, folder);

    if (source == NULL)
        goto second_preference;

    if (mail_account_in_recipients (registry, source, recipients))
        goto exit;

second_preference:

    /* Second Preference: Choose any enabled mail account whose
     * address appears in the list to To: or Cc: recipients. */

    if (source != NULL) {
        g_object_unref (source);
        source = NULL;
    }

    extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
    list = e_source_registry_list_sources (registry, extension_name);

    for (iter = list; iter != NULL; iter = g_list_next (iter)) {
        ESource *temp = E_SOURCE (iter->data);

        if (mail_account_in_recipients (registry, temp, recipients)) {
            source = g_object_ref (temp);
            break;
        }
    }

    g_list_free_full (list, (GDestroyNotify) g_object_unref);

    if (source != NULL)
        goto exit;

    /* Last Preference: Defer to em_utils_guess_mail_account(). */
    source = em_utils_guess_mail_account (registry, message, folder);

exit:
    g_hash_table_destroy (recipients);

    return source;
}

ESource *
em_utils_guess_mail_identity_with_recipients (ESourceRegistry *registry,
                                              CamelMimeMessage *message,
                                              CamelFolder *folder)
{
    ESource *source;
    ESourceExtension *extension;
    const gchar *extension_name;
    const gchar *uid;

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
    g_return_val_if_fail (CAMEL_IS_MIME_MESSAGE (message), NULL);

    source = em_utils_guess_mail_account_with_recipients (
        registry, message, folder);

    if (source == NULL)
        return NULL;

    extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
    extension = e_source_get_extension (source, extension_name);

    uid = e_source_mail_account_get_identity_uid (
        E_SOURCE_MAIL_ACCOUNT (extension));
    if (uid == NULL)
        return NULL;

    source = e_source_registry_ref_source (registry, uid);
    if (source == NULL)
        return NULL;

    extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
    if (!e_source_has_extension (source, extension_name)) {
        g_object_unref (source);
        return NULL;
    }

    return source;
}

ESource *
em_utils_ref_mail_identity_for_store (ESourceRegistry *registry,
                                      CamelStore *store)
{
    ESourceMailAccount *extension;
    ESource *source;
    const gchar *extension_name;
    const gchar *store_uid;
    gchar *identity_uid;

    g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
    g_return_val_if_fail (CAMEL_IS_STORE (store), NULL);

    store_uid = camel_service_get_uid (CAMEL_SERVICE (store));
    g_return_val_if_fail (store_uid != NULL, NULL);

    source = e_source_registry_ref_source (registry, store_uid);
    g_return_val_if_fail (source != NULL, NULL);

    extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
    extension = e_source_get_extension (source, extension_name);
    identity_uid = e_source_mail_account_dup_identity_uid (extension);

    g_object_unref (source);
    source = NULL;

    if (identity_uid != NULL) {
        source = e_source_registry_ref_source (registry, identity_uid);
        g_free (identity_uid);
    }

    return source;
}

/**
 * em_utils_uids_free:
 * @uids: array of uids
 *
 * Frees the array of uids pointed to by @uids back to the system.
 **/
void
em_utils_uids_free (GPtrArray *uids)
{
    gint i;

    for (i = 0; i < uids->len; i++)
        g_free (uids->pdata[i]);

    g_ptr_array_free (uids, TRUE);
}

/* Returns TRUE if CamelURL points to a local mbox file. */
gboolean
em_utils_is_local_delivery_mbox_file (CamelURL *url)
{
    g_return_val_if_fail (url != NULL, FALSE);

    return g_str_equal (url->protocol, "mbox") &&
        (url->path != NULL) &&
        g_file_test (url->path, G_FILE_TEST_EXISTS) &&
        !g_file_test (url->path, G_FILE_TEST_IS_DIR);
}