aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/backend/ebook/e-destination.c
blob: 25e1caa6ec9a68ed0124bca87d3623cebd6dcaed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * e-destination.c
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Developed by Jon Trowbridge <trow@ximian.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 "e-destination.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <gtk/gtkobject.h>
#include <gtk/gtkmain.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include "e-book.h"
#include "e-book-util.h"
#include <gal/widgets/e-unicode.h>
#include <gnome-xml/parser.h>
#include <gnome-xml/xmlmemory.h>
#include <camel/camel-internet-address.h>

enum {
    CHANGED,
    CARDIFIED,
    LAST_SIGNAL
};

guint e_destination_signals[LAST_SIGNAL] = { 0 };

struct _EDestinationPrivate {

    gchar *raw;

    gchar *book_uri;
    gchar *card_uid;
    ECard *card;
    gint card_email_num;

    gchar *name;
    gchar *email;
    gchar *addr;

    gboolean html_mail_override;
    gboolean wants_html_mail;

    GList *list_dests;

    gboolean has_been_cardified;
    gboolean allow_cardify;
    gboolean cannot_cardify;
    guint pending_cardification;
    EBook *cardify_book;

    gint freeze_count;
    gboolean pending_change;
};

static void e_destination_clear_card    (EDestination *);
static void e_destination_clear_strings (EDestination *);

static GtkObjectClass *parent_class;

static void
e_destination_destroy (GtkObject *obj)
{
    EDestination *dest = E_DESTINATION (obj);

    e_destination_clear (dest);
    
    if (dest->priv->cardify_book)
        gtk_object_unref (GTK_OBJECT (dest->priv->cardify_book));

    g_free (dest->priv);

    if (parent_class->destroy)
        parent_class->destroy (obj);
}

static void
e_destination_class_init (EDestinationClass *klass)
{
    GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);

    parent_class = GTK_OBJECT_CLASS (gtk_type_class (GTK_TYPE_OBJECT));

    object_class->destroy = e_destination_destroy;

    e_destination_signals[CHANGED] =
        gtk_signal_new ("changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EDestinationClass, changed),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    e_destination_signals[CARDIFIED] =
        gtk_signal_new ("cardified",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EDestinationClass, cardified),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, e_destination_signals, LAST_SIGNAL);
}

static void
e_destination_init (EDestination *dest)
{
    dest->priv = g_new0 (struct _EDestinationPrivate, 1);

    dest->priv->allow_cardify = TRUE;
    dest->priv->cannot_cardify = FALSE;
    dest->priv->pending_cardification = 0;
}

GtkType
e_destination_get_type (void)
{
    static GtkType dest_type = 0;

    if (!dest_type) {
        GtkTypeInfo dest_info = {
            "EDestination",
            sizeof (EDestination),
            sizeof (EDestinationClass),
            (GtkClassInitFunc) e_destination_class_init,
            (GtkObjectInitFunc) e_destination_init,
            NULL, NULL, /* reserved */
            (GtkClassInitFunc) NULL
        };

        dest_type = gtk_type_unique (gtk_object_get_type (), &dest_info);
    }

    return dest_type;
}

EDestination *
e_destination_new (void)
{
    return E_DESTINATION (gtk_type_new (E_TYPE_DESTINATION));
}

static void
e_destination_freeze (EDestination *dest)
{
    g_return_if_fail (E_IS_DESTINATION (dest));
    g_return_if_fail (dest->priv->freeze_count >= 0);
    ++dest->priv->freeze_count;
}

static void
e_destination_thaw (EDestination *dest)
{
    g_return_if_fail (E_IS_DESTINATION (dest));
    g_return_if_fail (dest->priv->freeze_count > 0);
    --dest->priv->freeze_count;
    if (dest->priv->freeze_count == 0 && dest->priv->pending_change)
        e_destination_changed (dest);
}

void
e_destination_changed (EDestination *dest)
{
    if (dest->priv->freeze_count == 0) {
        gtk_signal_emit (GTK_OBJECT (dest), e_destination_signals[CHANGED]);
        dest->priv->pending_change = FALSE;
        
        dest->priv->cannot_cardify = FALSE;
    
    } else {
        dest->priv->pending_change = TRUE;
    }
}

EDestination *
e_destination_copy (const EDestination *dest)
{
    EDestination *new_dest;
    GList *iter;

    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);

    new_dest = e_destination_new ();

    new_dest->priv->book_uri       = g_strdup (dest->priv->book_uri);
    new_dest->priv->card_uid       = g_strdup (dest->priv->card_uid);
    new_dest->priv->name           = g_strdup (dest->priv->name);
    new_dest->priv->email          = g_strdup (dest->priv->email);
    new_dest->priv->addr           = g_strdup (dest->priv->addr);
    new_dest->priv->card_email_num = dest->priv->card_email_num;

    new_dest->priv->card     = dest->priv->card;
    if (new_dest->priv->card)
        gtk_object_ref (GTK_OBJECT (new_dest->priv->card));

    new_dest->priv->html_mail_override = dest->priv->html_mail_override;
    new_dest->priv->wants_html_mail    = dest->priv->wants_html_mail;

    for (iter = dest->priv->list_dests; iter != NULL; iter = g_list_next (iter)) {
        new_dest->priv->list_dests = g_list_append (new_dest->priv->list_dests,
                                e_destination_copy (E_DESTINATION (iter->data)));
    }

    return new_dest;
}

static void
e_destination_clear_card (EDestination *dest)
{
    g_free (dest->priv->book_uri);
    dest->priv->book_uri = NULL;
    g_free (dest->priv->card_uid);
    dest->priv->card_uid = NULL;

    if (dest->priv->card)
        gtk_object_unref (GTK_OBJECT (dest->priv->card));
    dest->priv->card = NULL;

    dest->priv->card_email_num = -1;

    g_list_foreach (dest->priv->list_dests, (GFunc) gtk_object_unref, NULL);
    g_list_free (dest->priv->list_dests);
    dest->priv->list_dests = NULL;

    dest->priv->allow_cardify = TRUE;
    dest->priv->cannot_cardify = FALSE;

    e_destination_cancel_cardify (dest);

    e_destination_changed (dest);
}

static void
e_destination_clear_strings (EDestination *dest)
{
    g_free (dest->priv->raw);
    dest->priv->raw = NULL;
        
    g_free (dest->priv->name);
    dest->priv->name = NULL;

    g_free (dest->priv->email);
    dest->priv->email = NULL;

    g_free (dest->priv->addr);
    dest->priv->addr = NULL;

    e_destination_changed (dest);
}

void
e_destination_clear (EDestination *dest)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));

    e_destination_freeze (dest);

    e_destination_clear_card (dest);
    e_destination_clear_strings (dest);

    e_destination_thaw (dest);
}

gboolean
e_destination_is_empty (const EDestination *dest)
{
    struct _EDestinationPrivate *p;
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), TRUE);
    p = dest->priv;

    return !(p->card != NULL
         || (p->book_uri && *p->book_uri)
         || (p->card_uid && *p->card_uid)
         || (p->raw && *p->raw)
         || (p->name && *p->name)
         || (p->email && *p->email)
         || (p->addr && *p->addr)
         || (p->list_dests != NULL));
}

void
e_destination_set_card (EDestination *dest, ECard *card, gint email_num)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));
    g_return_if_fail (card && E_IS_CARD (card));

    if (dest->priv->card != card || dest->priv->card_email_num != email_num) {

        e_destination_clear (dest);

        dest->priv->card = card;
        gtk_object_ref (GTK_OBJECT (dest->priv->card));

        dest->priv->card_email_num = email_num;

        e_destination_changed (dest);
    }
}

void
e_destination_set_book_uri (EDestination *dest, const gchar *uri)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));
    g_return_if_fail (uri != NULL);

    if (dest->priv->book_uri == NULL
        || strcmp (dest->priv->book_uri, uri)) {
    
        g_free (dest->priv->book_uri);
        dest->priv->book_uri = g_strdup (uri);

        /* If we already have a card, remove it unless it's uri matches the one
           we just set. */
        if (dest->priv->card) {
            EBook *book = e_card_get_book (dest->priv->card);
            if ((!book) || strcmp (uri, e_book_get_uri (book))) {
                gtk_object_unref (GTK_OBJECT (dest->priv->card));
                dest->priv->card = NULL;
            }
        }

        e_destination_changed (dest);
    }
}

void
e_destination_set_card_uid (EDestination *dest, const gchar *uid, gint email_num)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));
    g_return_if_fail (uid != NULL);

    if (dest->priv->card_uid == NULL
        || strcmp (dest->priv->card_uid, uid)
        || dest->priv->card_email_num != email_num) {

        g_free (dest->priv->card_uid);
        dest->priv->card_uid = g_strdup (uid);
        dest->priv->card_email_num = email_num;

        /* If we already have a card, remove it unless it's uri matches the one
           we just set. */
        if (dest->priv->card && strcmp (uid, e_card_get_id (dest->priv->card))) {
            gtk_object_unref (GTK_OBJECT (dest->priv->card));
            dest->priv->card = NULL;
        }

        e_destination_changed (dest);
    }
}

void
e_destination_set_name (EDestination *dest, const gchar *name)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));
    g_return_if_fail (name != NULL);

    if (dest->priv->name == NULL || strcmp (dest->priv->name, name)) {

        g_free (dest->priv->name);
        dest->priv->name = g_strdup (name);

        if (dest->priv->addr != NULL) {
            g_free (dest->priv->addr);
            dest->priv->addr = NULL;
        }

        e_destination_changed (dest);
    }
}

void
e_destination_set_email (EDestination *dest, const gchar *email)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));
    g_return_if_fail (email != NULL);

    if (dest->priv->email == NULL || strcmp (dest->priv->email, email)) {

        g_free (dest->priv->email);
        dest->priv->email = g_strdup (email);

        if (dest->priv->addr != NULL) {
            g_free (dest->priv->addr);
            dest->priv->addr = NULL;
        }

        e_destination_changed (dest);
    }
}

void
e_destination_set_html_mail_pref (EDestination *dest, gboolean x)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));
    
    dest->priv->html_mail_override = TRUE;
    if (dest->priv->wants_html_mail != x) {
        dest->priv->wants_html_mail = x;
        e_destination_changed (dest);
    }
}

gboolean
e_destination_contains_card (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
    return dest->priv->card != NULL;
}

gboolean
e_destination_from_card (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
    return dest->priv->card != NULL || dest->priv->book_uri != NULL || dest->priv->card_uid != NULL;
}


typedef struct _UseCard UseCard;
struct _UseCard {
    EDestination *dest;
    EDestinationCardCallback cb;
    gpointer closure;
};

static void
use_card_cb (ECard *card, gpointer closure)
{
    UseCard *uc = (UseCard *) closure;

    if (card != NULL && uc->dest->priv->card == NULL) {

        uc->dest->priv->card = card;
        gtk_object_ref (GTK_OBJECT (uc->dest->priv->card));
        e_destination_changed (uc->dest);

    }

    if (uc->cb) {
        uc->cb (uc->dest, uc->dest->priv->card, uc->closure);
    }

    /* We held a copy of the destination during the callback. */
    gtk_object_unref (GTK_OBJECT (uc->dest));
    g_free (uc);
}

void
e_destination_use_card (EDestination *dest, EDestinationCardCallback cb, gpointer closure)
{
    g_return_if_fail (dest && E_IS_DESTINATION (dest));

    if (dest->priv->card != NULL) {

        if (cb) {
            cb (dest, dest->priv->card, closure);
        }

    } else if (dest->priv->book_uri != NULL && dest->priv->card_uid != NULL) {

        UseCard *uc = g_new (UseCard, 1);
        uc->dest = dest;
        /* Hold a reference to the destination during the callback. */
        gtk_object_ref (GTK_OBJECT (uc->dest));
        uc->cb = cb;
        uc->closure = closure;
        e_card_load_uri (dest->priv->book_uri, dest->priv->card_uid, use_card_cb, uc);
    }
}

ECard *
e_destination_get_card (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);

    return dest->priv->card;
}

const gchar *
e_destination_get_card_uid (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
    
    if (dest->priv->card_uid)
        return dest->priv->card_uid;
    
    if (dest->priv->card)
        return e_card_get_id (dest->priv->card);

    return NULL;
}

const gchar *
e_destination_get_book_uri (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
    
    if (dest->priv->book_uri)
        return dest->priv->book_uri;
    
    if (dest->priv->card) {
        EBook *book = e_card_get_book (dest->priv->card);
        if (book) {
            return e_book_get_uri (book);
        }
    }

    return NULL;
}

gint
e_destination_get_email_num (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), -1);

    if (dest->priv->card == NULL && (dest->priv->book_uri == NULL || dest->priv->card_uid == NULL))
        return -1;

    return dest->priv->card_email_num;
}

const gchar *
e_destination_get_name (const EDestination *dest)
{
    struct _EDestinationPrivate *priv;
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);

    priv = (struct _EDestinationPrivate *)dest->priv; /* cast out const */
    
    if (priv->name == NULL) {

        if (priv->card != NULL) {
        
            priv->name = e_card_name_to_string (priv->card->name);
        
            if (priv->name == NULL || *priv->name == '\0') {
                g_free (priv->name);
                priv->name = g_strdup (priv->card->file_as);
            }

            if (priv->name == NULL || *priv->name == '\0') {
                g_free (priv->name);
                priv->name = g_strdup (e_destination_get_email (dest));
            }

        } else if (priv->raw != NULL) {

            CamelInternetAddress *addr = camel_internet_address_new ();

            if (camel_address_unformat (CAMEL_ADDRESS (addr), priv->raw)) {
                const gchar *camel_name = NULL;
                camel_internet_address_get (addr, 0, &camel_name, NULL);
                priv->name = g_strdup (camel_name);
            }

            camel_object_unref (CAMEL_OBJECT (addr));
        }
    }
    
    return priv->name;
    
}

const gchar *
e_destination_get_email (const EDestination *dest)
{
    struct _EDestinationPrivate *priv;

    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);

    priv = (struct _EDestinationPrivate *)dest->priv; /* cast out const */

    if (priv->email == NULL) {

        if (priv->card != NULL) { /* Pull the address out of the card. */

            if (priv->card->email) {
                EIterator *iter = e_list_get_iterator (priv->card->email);
                gint n = priv->card_email_num;

                if (n >= 0) {
                    while (n > 0) {
                        e_iterator_next (iter);
                        --n;
                    }

                    if (e_iterator_is_valid (iter)) {
                        gconstpointer ptr = e_iterator_get (iter);
                        priv->email = g_strdup ((gchar *) ptr);
                    }
                }
            }
            else {
                priv->email = g_strdup ("");
            }

        } else if (priv->raw != NULL) {

            CamelInternetAddress *addr = camel_internet_address_new ();

            if (camel_address_unformat (CAMEL_ADDRESS (addr), priv->raw)) {
                const gchar *camel_email = NULL;
                camel_internet_address_get (addr, 0, NULL, &camel_email);
                priv->email = g_strdup (camel_email);
            }
            
            camel_object_unref (CAMEL_OBJECT (addr));
        }
        else {
            priv->email = g_strdup ("");
        }
    }

    return priv->email;
}

const gchar *
e_destination_get_address (const EDestination *dest)
{
    struct _EDestinationPrivate *priv;
    
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
    
    priv = (struct _EDestinationPrivate *)dest->priv; /* cast out const */

    if (priv->addr == NULL) {
        CamelInternetAddress *addr = camel_internet_address_new ();

        if (e_destination_is_evolution_list (dest)) {
            GList *iter = dest->priv->list_dests;
            
            while (iter) {
                EDestination *list_dest = E_DESTINATION (iter->data);
                if (!e_destination_is_empty (list_dest)) {
                    camel_internet_address_add (addr, 
                                    e_destination_get_name (list_dest),
                                    e_destination_get_email (list_dest));
                }
                iter = g_list_next (iter);
            }
            
            priv->addr = camel_address_encode (CAMEL_ADDRESS (addr));

        } else if (priv->raw) {

            if (camel_address_unformat (CAMEL_ADDRESS (addr), priv->raw)) {
                priv->addr = camel_address_encode (CAMEL_ADDRESS (addr));
            }

        } else {
            
            camel_internet_address_add (addr,
                            e_destination_get_name (dest),
                            e_destination_get_email (dest));

            priv->addr = camel_address_encode (CAMEL_ADDRESS (addr));
        }

        camel_object_unref (CAMEL_OBJECT (addr));
    }

    return priv->addr;
}

void
e_destination_set_raw (EDestination *dest, const gchar *raw)
{
    g_return_if_fail (E_IS_DESTINATION (dest));
    g_return_if_fail (raw != NULL);

    if (dest->priv->raw == NULL || strcmp (dest->priv->raw, raw)) {

        e_destination_freeze (dest);

        e_destination_clear (dest);
        dest->priv->raw = g_strdup (raw);
        e_destination_changed (dest);

        e_destination_thaw (dest);
    }
}

const gchar *
e_destination_get_textrep (const EDestination *dest)
{
    const gchar *txt;

    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);

    if (dest->priv->raw)
        return dest->priv->raw;

    txt = e_destination_get_name (dest);
    if (txt != NULL)
        return txt;

    txt = e_destination_get_email (dest);
    if (txt != NULL)
        return txt;

    return "";
}

gboolean
e_destination_is_evolution_list (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);

    if (dest->priv->list_dests == NULL
        && dest->priv->card != NULL
        && dest->priv->card->email != NULL
        && e_card_evolution_list (dest->priv->card)) {

        EIterator *iter = e_list_get_iterator (dest->priv->card->email);
        e_iterator_reset (iter);
        while (e_iterator_is_valid (iter)) {
            const gchar *dest_xml = (const gchar *) e_iterator_get (iter);
            EDestination *list_dest = e_destination_import (dest_xml);
            if (list_dest)
                dest->priv->list_dests = g_list_append (dest->priv->list_dests, list_dest);
            e_iterator_next (iter);
        }
    }

    return dest->priv->list_dests != NULL;
}

gboolean
e_destination_get_html_mail_pref (const EDestination *dest)
{
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);

    if (dest->priv->html_mail_override || dest->priv->card == NULL)
        return dest->priv->wants_html_mail;

    return dest->priv->card->wants_html;
}

gboolean
e_destination_allow_cardification (const EDestination *dest)
{
    g_return_val_if_fail (E_IS_DESTINATION (dest), FALSE);

    return dest->priv->allow_cardify;
}

void
e_destination_set_allow_cardification (EDestination *dest, gboolean x)
{
    g_return_if_fail (E_IS_DESTINATION (dest));
    
    dest->priv->allow_cardify = x;
}

static void
set_cardify_book (EDestination *dest, EBook *book)
{
    if (dest->priv->cardify_book && dest->priv->cardify_book != book) {
        gtk_object_unref (GTK_OBJECT (dest->priv->cardify_book));
    }
        
    dest->priv->cardify_book = book;

    if (book)
        gtk_object_ref (GTK_OBJECT (book));
}

static void
name_and_email_simple_query_cb (EBook *book, EBookSimpleQueryStatus status, const GList *cards, gpointer closure)
{
    EDestination *dest = E_DESTINATION (closure);

    if (status == E_BOOK_SIMPLE_QUERY_STATUS_SUCCESS && g_list_length ((GList *) cards) == 1) {
        ECard *card = E_CARD (cards->data);
        gint email_num = e_card_email_find_number (card, e_destination_get_email (dest));

        if (email_num >= 0) {
            dest->priv->has_been_cardified = TRUE;      
            e_destination_set_card (dest, E_CARD (cards->data), email_num);
            gtk_signal_emit (GTK_OBJECT (dest), e_destination_signals[CARDIFIED]);
        }
    }

    if (!dest->priv->has_been_cardified) {
        dest->priv->cannot_cardify = TRUE;
    }

    gtk_object_unref (GTK_OBJECT (dest));
}


static void
nickname_simple_query_cb (EBook *book, EBookSimpleQueryStatus status, const GList *cards, gpointer closure)
{
    EDestination *dest = E_DESTINATION (closure);

    if (status == E_BOOK_SIMPLE_QUERY_STATUS_SUCCESS && g_list_length ((GList *) cards) == 1) {

        dest->priv->has_been_cardified = TRUE;      
        e_destination_set_card (dest, E_CARD (cards->data), 0); /* Uses primary e-mail by default. */
        gtk_signal_emit (GTK_OBJECT (dest), e_destination_signals[CARDIFIED]);
        gtk_object_unref (GTK_OBJECT (dest));

    } else {

        e_book_name_and_email_query (book,
                         e_destination_get_name (dest),
                         e_destination_get_email (dest),
                         name_and_email_simple_query_cb,
                         dest);
    }
}

static void
launch_cardify_query (EDestination *dest)
{
    if (strchr (e_destination_get_textrep (dest), '@') == NULL) {
        
        /* If it doesn't look like an e-mail address, see if it is a nickname. */
        e_book_nickname_query (dest->priv->cardify_book,
                       e_destination_get_textrep (dest),
                       nickname_simple_query_cb,
                       dest);

    } else {

        e_book_name_and_email_query (dest->priv->cardify_book,
                         e_destination_get_name (dest),
                         e_destination_get_email (dest),
                         name_and_email_simple_query_cb,
                         dest);
    }
}

static void
use_local_book_cb (EBook *book, gpointer closure)
{
    EDestination *dest = E_DESTINATION (closure);
    if (dest->priv->cardify_book == NULL) {
        dest->priv->cardify_book = book;
        gtk_object_ref (GTK_OBJECT (book));
    }

    launch_cardify_query (dest);
}


void
e_destination_cardify (EDestination *dest, EBook *book)
{
    g_return_if_fail (E_IS_DESTINATION (dest));
    g_return_if_fail (book == NULL || E_IS_BOOK (book));

    if (e_destination_is_evolution_list (dest))
        return;

    if (e_destination_contains_card (dest))
        return;

    if (!dest->priv->allow_cardify)
        return;

    if (dest->priv->cannot_cardify)
        return;

    e_destination_cancel_cardify (dest);

    set_cardify_book (dest, book);

    /* Handle the case of an EDestination containing a card URL */
    if (e_destination_contains_card (dest)) {
        e_destination_use_card (dest, NULL, NULL);
        return;
    }
    
    /* If we have a book ready, proceed.  We hold a reference to ourselves
       until our query is complete. */
    gtk_object_ref (GTK_OBJECT (dest));
    if (dest->priv->cardify_book != NULL) {
        launch_cardify_query (dest);
    } else {
        e_book_use_local_address_book (use_local_book_cb, dest);
    }
}

static gint
do_cardify_delayed (gpointer ptr)
{
    EDestination *dest = E_DESTINATION (ptr);
    e_destination_cardify (dest, dest->priv->cardify_book);
    return FALSE;
}

void
e_destination_cardify_delayed (EDestination *dest, EBook *book, gint delay)
{
    g_return_if_fail (E_IS_DESTINATION (dest));
    g_return_if_fail (book == NULL || E_IS_BOOK (book));

    if (delay < 0)
        delay = 500;

    e_destination_cancel_cardify (dest);

    set_cardify_book (dest, book);

    dest->priv->pending_cardification = gtk_timeout_add (delay, do_cardify_delayed, dest);
}

void
e_destination_cancel_cardify (EDestination *dest)
{
    g_return_if_fail (E_IS_DESTINATION (dest));

    if (dest->priv->pending_cardification) {
        gtk_timeout_remove (dest->priv->pending_cardification);
        dest->priv->pending_cardification = 0;
    }
}

#if 0
void
e_destination_uncardify (EDestination *dest)
{
    g_return_if_fail (E_IS_DESTINATION (dest));

    g_assert_not_reached ();
}
#endif

/*
 * Destination import/export
 */

gchar *
e_destination_get_address_textv (EDestination **destv)
{
    gint i, j, len = 0;
    gchar **strv;
    gchar *str;
    
    g_return_val_if_fail (destv, NULL);

    /* Q: Please tell me this is only for assertion
           reasons. If this is considered to be ok behavior then you
           shouldn't use g_return's. Just a reminder ;-) 

       A: Yes, this is just an assertion.  (Though it does find the
       length of the vector in the process...)
    */
    while (destv[len]) {
        g_return_val_if_fail (E_IS_DESTINATION (destv[len]), NULL);
        ++len;
    }
    
    strv = g_new0 (gchar *, len+1);
    for (i = 0, j = 0; destv[i]; i++) {
        if (!e_destination_is_empty (destv[i])) {
            const gchar *addr = e_destination_get_address (destv[i]);
            strv[j++] = addr ? (gchar *) addr : "";
        }
    }
    
    str = g_strjoinv (", ", strv);
    
    g_free (strv);
    
    return str;
}

xmlNodePtr
e_destination_xml_encode (const EDestination *dest)
{
    xmlNodePtr dest_node;
    const gchar *str;

    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);

    dest_node = xmlNewNode (NULL, "destination");

    str = e_destination_get_name (dest);
    if (str)
        xmlNewTextChild (dest_node, NULL, "name", str);

    if (! e_destination_is_evolution_list (dest)) {
        str = e_destination_get_email (dest);
        if (str)
            xmlNewTextChild (dest_node, NULL, "email", str);
    } else {
        GList *iter = dest->priv->list_dests;
        
        while (iter) {
            EDestination *list_dest = E_DESTINATION (iter->data);
            xmlNodePtr list_node = xmlNewNode (NULL, "list_entry");
            
            str = e_destination_get_name (list_dest);
            if (str)
                xmlNewTextChild (list_node, NULL, "name", str);
            
            str = e_destination_get_email (list_dest);
            if (str)
                xmlNewTextChild (list_node, NULL, "email", str);

            xmlAddChild (dest_node, list_node);
            
            iter = g_list_next (iter);
        }

        xmlNewProp (dest_node, "is_list", "yes");
    }

    str = e_destination_get_book_uri (dest);
    if (str) {
        xmlNewTextChild (dest_node, NULL, "book_uri", str);
    }

    str = e_destination_get_card_uid (dest);
    if (str) {
        gchar buf[16];
        xmlNodePtr uri_node = xmlNewTextChild (dest_node, NULL, "card_uid", str);
        g_snprintf (buf, 16, "%d", e_destination_get_email_num (dest));
        xmlNewProp (uri_node, "email_num", buf);
    }

    xmlNewProp (dest_node, "html_mail", e_destination_get_html_mail_pref (dest) ? "yes" : "no");

    return dest_node;
}

gboolean
e_destination_xml_decode (EDestination *dest, xmlNodePtr node)
{
    gchar *name = NULL, *email = NULL, *book_uri = NULL, *card_uid = NULL;
    gint email_num = -1;
    gboolean html_mail = FALSE;
    gboolean is_list = FALSE;
    gchar *tmp;
    GList *list_dests = NULL;
    
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
    g_return_val_if_fail (node != NULL, FALSE);
    
    if (strcmp (node->name, "destination"))
        return FALSE;
    
    tmp = xmlGetProp (node, "html_mail");
    if (tmp) {
        html_mail = !strcmp (tmp, "yes");
        xmlFree (tmp);
    }
    
    tmp = xmlGetProp (node, "is_list");
    if (tmp) {
        is_list = !strcmp (tmp, "yes");
        xmlFree (tmp);
    }
    
    node = node->xmlChildrenNode;
    while (node) {
        if (!strcmp (node->name, "name")) {
            tmp = xmlNodeGetContent (node);
            g_free (name);
            name = g_strdup (tmp);
            xmlFree (tmp);
        } else if (!is_list && !strcmp (node->name, "email")) {
            tmp = xmlNodeGetContent (node);
            g_free (email);
            email = g_strdup (tmp);
            xmlFree (tmp);
        } else if (is_list && !strcmp (node->name, "list_entry")) {
            xmlNodePtr subnode = node->xmlChildrenNode;
            gchar *list_name = NULL, *list_email = NULL;
            
            while (subnode) {
                if (!strcmp (subnode->name, "name")) {
                    tmp = xmlNodeGetContent (subnode);
                    g_free (list_name);
                    list_name = g_strdup (tmp);
                    xmlFree (tmp);
                } else if (!strcmp (subnode->name, "email")) {
                    tmp = xmlNodeGetContent (subnode);
                    g_free (list_email);
                    list_email = g_strdup (tmp);
                    xmlFree (tmp);
                }
                
                subnode = subnode->next;
            }
            
            if (list_name || list_email) {
                EDestination *list_dest = e_destination_new ();
                if (list_name)
                    e_destination_set_name (list_dest, list_name);
                if (list_email)
                    e_destination_set_email (list_dest, list_email);
                
                list_dests = g_list_append (list_dests, list_dest);
            }
        } else if (!strcmp (node->name, "book_uri")) {
            tmp = xmlNodeGetContent (node);
            g_free (book_uri);
            book_uri = g_strdup (tmp);
            xmlFree (tmp);
        } else if (!strcmp (node->name, "card_uid")) {
            tmp = xmlNodeGetContent (node);
            g_free (card_uid);
            card_uid = g_strdup (tmp);
            xmlFree (tmp);
            
            tmp = xmlGetProp (node, "email_num");
            email_num = atoi (tmp);
            xmlFree (tmp);
        }
        
        node = node->next;
    }

    e_destination_freeze (dest);
    
    e_destination_clear (dest);
    
    if (name)
        e_destination_set_name (dest, name);
    if (email)
        e_destination_set_email (dest, email);
    if (book_uri)
        e_destination_set_book_uri (dest, book_uri);
    if (card_uid)
        e_destination_set_card_uid (dest, card_uid, email_num);
    if (list_dests)
        dest->priv->list_dests = list_dests;

    e_destination_thaw (dest);
    
    return TRUE;
}

/* FIXME: Make utf-8 safe */
static gchar *
null_terminate_and_remove_extra_whitespace (xmlChar *xml_in, gint size)
{
    gchar *xml;
    gchar *r, *w;
    gboolean skip_white = FALSE;

    if (xml_in == NULL || size <= 0) 
        return NULL;

    xml = g_strndup (xml_in, size);
    r = w = xml;

    while (*r) {
        if (*r == '\n' || *r == '\r') {
            skip_white = TRUE;
        } else {
            gboolean is_space = isspace (*r);

            *w = *r;

            if (! (skip_white && is_space))
                ++w;
            if (! is_space)
                skip_white = FALSE;
        }
        ++r;
    }

    *w = '\0';

    return xml;
}

gchar *
e_destination_export (const EDestination *dest)
{
    xmlNodePtr dest_node;
    xmlDocPtr  dest_doc;
    xmlChar   *buffer = NULL;
    gint       size = -1;
    gchar     *str;
    
    g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);

    dest_node = e_destination_xml_encode (dest);
    if (dest_node == NULL)
        return NULL;

    dest_doc = xmlNewDoc (XML_DEFAULT_VERSION);
    xmlDocSetRootElement (dest_doc, dest_node);

    xmlDocDumpMemory (dest_doc, &buffer, &size);
    xmlFreeDoc (dest_doc);

    str = null_terminate_and_remove_extra_whitespace (buffer, size);
    xmlFree (buffer);

    return str;
}

EDestination *
e_destination_import (const gchar *str)
{
    EDestination *dest = NULL;
    xmlDocPtr dest_doc;

    if (! (str && *str))
        return NULL;

    dest_doc = xmlParseMemory ((gchar *) str, strlen (str));
    if (dest_doc && dest_doc->xmlRootNode) {
        dest = e_destination_new ();
        if (! e_destination_xml_decode (dest, dest_doc->xmlRootNode)) {
            gtk_object_unref (GTK_OBJECT (dest));
            dest = NULL;
        }
    }
    xmlFreeDoc (dest_doc);

    return dest;
}

gchar *
e_destination_exportv (EDestination **destv)
{
    xmlDocPtr   destv_doc;
    xmlNodePtr  destv_node;
    xmlChar    *buffer = NULL;
    gint        size = -1;
    gchar      *str;
    gint        i;

    if (destv == NULL || *destv == NULL)
        return NULL;

    destv_doc  = xmlNewDoc (XML_DEFAULT_VERSION);
    destv_node = xmlNewNode (NULL, "destinations");
    xmlDocSetRootElement (destv_doc, destv_node);

    for (i=0; destv[i]; ++i) {
        if (! e_destination_is_empty (destv[i])) {
            xmlNodePtr dest_node = e_destination_xml_encode (destv[i]);
            if (dest_node)
                xmlAddChild (destv_node, dest_node);
        }
    }

    xmlDocDumpMemory (destv_doc, &buffer, &size);
    xmlFreeDoc (destv_doc);

    str = null_terminate_and_remove_extra_whitespace (buffer, size);
    xmlFree (buffer);

    return str;
}

EDestination **
e_destination_importv (const gchar *str)
{
    GPtrArray *dest_array = NULL;
    xmlDocPtr destv_doc;
    xmlNodePtr node;
    EDestination **destv = NULL;
    
    if (!(str && *str))
        return NULL;
    
    destv_doc = xmlParseMemory ((gchar *)str, strlen (str));
    if (destv_doc == NULL)
        return NULL;

    node = destv_doc->xmlRootNode;
    
    if (strcmp (node->name, "destinations"))
        goto finished;
    
    node = node->xmlChildrenNode;
    
    dest_array = g_ptr_array_new ();
    
    while (node) {
        EDestination *dest;
        
        dest = e_destination_new ();
        if (e_destination_xml_decode (dest, node)) {
            g_ptr_array_add (dest_array, dest);
        } else {
            gtk_object_unref (GTK_OBJECT (dest));
        }
        
        node = node->next;
    }
    
    /* we need destv to be NULL terminated */
    g_ptr_array_add (dest_array, NULL);
    
    destv = (EDestination **) dest_array->pdata;
    g_ptr_array_free (dest_array, FALSE);
    
 finished:
    xmlFreeDoc (destv_doc);
    
    return destv;
}

static void
touch_cb (EBook *book, const gchar *addr, ECard *card, gpointer closure)
{
    if (book != NULL && card != NULL) {
        e_card_touch (card);
        g_message ("Use score for \"%s\" is now %f", addr, e_card_get_use_score (card));
        e_book_commit_card (book, card, NULL, NULL);
    }
}

void
e_destination_touch (EDestination *dest)
{
    const gchar *email;

    g_return_if_fail (dest && E_IS_DESTINATION (dest));

    email = e_destination_get_email (dest);

    if (email) {
        e_book_query_address_locally (email, touch_cb, NULL);
    }
}