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

#include <config.h>

#include "e-contact-list-editor.h"
#include <e-util/e-util-private.h>
#include <e-util/e-alert-dialog.h>
#include <e-util/e-selection.h>
#include "shell/e-shell.h"

#include <string.h>

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

#include <libedataserverui/e-source-combo-box.h>

#include "e-util/e-util.h"
#include "addressbook/gui/widgets/eab-gui-util.h"
#include "addressbook/util/addressbook.h"
#include "addressbook/util/eab-book-util.h"

#include "eab-editor.h"
#include "e-contact-editor.h"
#include "e-contact-list-model.h"
#include "eab-contact-merging.h"

#define E_CONTACT_LIST_EDITOR_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_CONTACT_LIST_EDITOR, EContactListEditorPrivate))

#define CONTACT_LIST_EDITOR_WIDGET(editor, name) \
    (e_builder_get_widget \
    (E_CONTACT_LIST_EDITOR_GET_PRIVATE (editor)->builder, name))

/* More macros, less typos. */
#define CONTACT_LIST_EDITOR_WIDGET_ADD_BUTTON(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "add-button")
#define CONTACT_LIST_EDITOR_WIDGET_CHECK_BUTTON(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "check-button")
#define CONTACT_LIST_EDITOR_WIDGET_DIALOG(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "dialog")
#define CONTACT_LIST_EDITOR_WIDGET_EMAIL_ENTRY(editor) \
    E_CONTACT_LIST_EDITOR_GET_PRIVATE (editor)->email_entry
#define CONTACT_LIST_EDITOR_WIDGET_LIST_NAME_ENTRY(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "list-name-entry")
#define CONTACT_LIST_EDITOR_WIDGET_MEMBERS_VBOX(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "members-vbox")
#define CONTACT_LIST_EDITOR_WIDGET_OK_BUTTON(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "ok-button")
#define CONTACT_LIST_EDITOR_WIDGET_REMOVE_BUTTON(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "remove-button")
#define CONTACT_LIST_EDITOR_WIDGET_SOURCE_MENU(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "source-combo-box")
#define CONTACT_LIST_EDITOR_WIDGET_TREE_VIEW(editor) \
    CONTACT_LIST_EDITOR_WIDGET ((editor), "tree-view")

/* Shorthand, requires a variable named "editor". */
#define WIDGET(name)    (CONTACT_LIST_EDITOR_WIDGET_##name (editor))

#define TOPLEVEL_KEY    (g_type_name (E_TYPE_CONTACT_LIST_EDITOR))

enum {
    PROP_0,
    PROP_BOOK,
    PROP_CONTACT,
    PROP_IS_NEW_LIST,
    PROP_EDITABLE
};

typedef struct {
    EContactListEditor *editor;
    gboolean should_close;
} EditorCloseStruct;

struct _EContactListEditorPrivate {

    EBook *book;
    EContact *contact;

    GtkBuilder *builder;
    GtkTreeModel *model;
    ENameSelector *name_selector;

    /* This is kept here because the builder has an old widget
     * which was changed with this one. */
    ENameSelectorEntry *email_entry;

    /* Whether we are editing a new contact or an existing one. */
    guint is_new_list : 1;

    /* Whether the contact has been changed since bringing up the
     * contact editor. */
    guint changed : 1;

    /* Whether the contact editor will accept modifications. */
    guint editable : 1;

    /* Whether the target book accepts storing of contact lists. */
    guint allows_contact_lists : 1;

    /* Whether an async wombat call is in progress. */
    guint in_async_call : 1;

    /* ID for async load_source call */
    guint load_source_id;
    EBook *load_book;
};

static gpointer parent_class;

static EContactListEditor *
contact_list_editor_extract (GtkWidget *widget)
{
    GtkWidget *toplevel;

    toplevel = gtk_widget_get_toplevel (widget);
    return g_object_get_data (G_OBJECT (toplevel), TOPLEVEL_KEY);
}

static void
contact_list_editor_scroll_to_end (EContactListEditor *editor)
{
    GtkTreeView *view;
    GtkTreePath *path;
    gint n_rows;

    view = GTK_TREE_VIEW (WIDGET (TREE_VIEW));
    n_rows = gtk_tree_model_iter_n_children (editor->priv->model, NULL);

    path = gtk_tree_path_new_from_indices (n_rows - 1, -1);
    gtk_tree_view_scroll_to_cell (view, path, NULL, FALSE, 0., 0.);
    gtk_tree_view_set_cursor (view, path, NULL, FALSE);
    gtk_tree_path_free (path);
}

static void
contact_list_editor_update (EContactListEditor *editor)
{
    EContactListEditorPrivate *priv = editor->priv;

    gtk_widget_set_sensitive (
        WIDGET (OK_BUTTON),
        eab_editor_is_valid (EAB_EDITOR (editor)) &&
        priv->allows_contact_lists);

    gtk_widget_set_sensitive (
        WIDGET (SOURCE_MENU), priv->is_new_list);
}

static void
contact_list_editor_notify_cb (EContactListEditor *editor,
                               GParamSpec *pspec)
{
    EContactListEditorPrivate *priv = editor->priv;
    gboolean sensitive;

    sensitive = priv->editable && priv->allows_contact_lists;

    gtk_widget_set_sensitive (WIDGET (LIST_NAME_ENTRY), sensitive);
    gtk_widget_set_sensitive (WIDGET (MEMBERS_VBOX), sensitive);
}

static void
contact_list_editor_add_email (EContactListEditor *editor)
{
    EContactListEditorPrivate *priv = editor->priv;
    EContactListModel *model;
    GtkEntry *entry;
    const gchar *text;

    entry = GTK_ENTRY (WIDGET (EMAIL_ENTRY));
    model = E_CONTACT_LIST_MODEL (priv->model);

    text = gtk_entry_get_text (entry);
    if (text != NULL && *text != '\0') {
        e_contact_list_model_add_email (model, text);
        contact_list_editor_scroll_to_end (editor);
        priv->changed = TRUE;
    }

    gtk_entry_set_text (entry, "");
    contact_list_editor_update (editor);
}

static void
contact_list_editor_book_loaded (EBook *new_book,
                                 EBookStatus status,
                                 EContactListEditor *editor)
{
    EContactListEditorPrivate *priv = editor->priv;
    EContactStore *contact_store;
    ENameSelectorEntry *entry;

    priv->load_source_id = 0;
    priv->load_book = NULL;

    if (status != E_BOOK_ERROR_OK || new_book == NULL) {
        eab_load_error_dialog (
            NULL, e_book_get_source (new_book), status);
        e_source_combo_box_set_active (
            E_SOURCE_COMBO_BOX (WIDGET (SOURCE_MENU)),
            e_book_get_source (priv->book));
        if (new_book)
            g_object_unref (new_book);
        return;
    }

    entry = E_NAME_SELECTOR_ENTRY (WIDGET (EMAIL_ENTRY));
    contact_store = e_name_selector_entry_peek_contact_store (entry);
    e_contact_store_add_book (contact_store, new_book);
    e_contact_list_editor_set_book (editor, new_book);
    g_object_unref (new_book);
}

static void
contact_list_editor_cancel_load (EContactListEditor *editor)
{
    EContactListEditorPrivate *priv = editor->priv;

    if (priv->load_source_id == 0)
        return;

    addressbook_load_cancel (priv->load_source_id);
    priv->load_source_id = 0;

    g_object_unref (priv->load_book);
    priv->load_book = NULL;
}

static gboolean
contact_list_editor_contact_exists (EContactListModel *model,
                                    const gchar *email)
{
    const gchar *tag = "addressbook:ask-list-add-exists";

    if (!e_contact_list_model_has_email (model, email))
        return FALSE;

    return (e_alert_run_dialog_for_args (e_shell_get_active_window (NULL),
                         tag, email, NULL) != GTK_RESPONSE_YES);
}

static void
contact_list_editor_list_added_cb (EBook *book,
                                   EBookStatus status,
                                   const gchar *id,
                                   EditorCloseStruct *ecs)
{
    EContactListEditor *editor = ecs->editor;
    EContactListEditorPrivate *priv = editor->priv;
    gboolean should_close = ecs->should_close;

    gtk_widget_set_sensitive (WIDGET (DIALOG), TRUE);
    priv->in_async_call = FALSE;

    e_contact_set (priv->contact, E_CONTACT_UID, (gchar *) id);

    eab_editor_contact_added (
        EAB_EDITOR (editor), status, priv->contact);

    if (status == E_BOOK_ERROR_OK) {
        priv->is_new_list = FALSE;

        if (should_close)
            eab_editor_close (EAB_EDITOR (editor));
        else
            contact_list_editor_update (editor);
    }

    g_object_unref (editor);
    g_free (ecs);
}

static void
contact_list_editor_list_modified_cb (EBook *book,
                                      EBookStatus status,
                                      EditorCloseStruct *ecs)
{
    EContactListEditor *editor = ecs->editor;
    EContactListEditorPrivate *priv = editor->priv;
    gboolean should_close = ecs->should_close;

    gtk_widget_set_sensitive (WIDGET (DIALOG), TRUE);
    priv->in_async_call = FALSE;

    eab_editor_contact_modified (
        EAB_EDITOR (editor), status, priv->contact);

    if (status == E_BOOK_ERROR_OK) {
        if (should_close)
            eab_editor_close (EAB_EDITOR (editor));
    }

    g_object_unref (editor);
    g_free (ecs);
}

static void
contact_list_editor_render_destination (GtkTreeViewColumn *column,
                                        GtkCellRenderer *renderer,
                                        GtkTreeModel *model,
                                        GtkTreeIter *iter)
{
    /* XXX Would be nice if EDestination had a text property
     *     that we could just bind the GtkCellRenderer to. */

    EDestination *destination;
    const gchar *textrep;

    gtk_tree_model_get (model, iter, 0, &destination, -1);
    textrep = e_destination_get_textrep (destination, TRUE);
    g_object_set (renderer, "text", textrep, NULL);
    g_object_unref (destination);
}

static void
contact_list_editor_row_deleted_cb (GtkTreeModel *model,
                                    GtkTreePath *path,
                                    EContactListEditor *editor)
{
    gint n_children;

    n_children = gtk_tree_model_iter_n_children (model, NULL);
    gtk_widget_set_sensitive (WIDGET (REMOVE_BUTTON), n_children > 0);
}

static void
contact_list_editor_row_inserted_cb (GtkTreeModel *model,
                                     GtkTreePath *path,
                                     GtkTreeIter *iter,
                                     EContactListEditor *editor)
{
    gtk_widget_set_sensitive (WIDGET (REMOVE_BUTTON), TRUE);
}

/*********************** Autoconnected Signal Handlers ***********************/

void
contact_list_editor_add_button_clicked_cb (GtkWidget *widget);

void
contact_list_editor_add_button_clicked_cb (GtkWidget *widget)
{
    EContactListEditor *editor;

    editor = contact_list_editor_extract (widget);

    contact_list_editor_add_email (editor);
}

void
contact_list_editor_cancel_button_clicked_cb (GtkWidget *widget);

void
contact_list_editor_cancel_button_clicked_cb (GtkWidget *widget)
{
    EContactListEditor *editor;
    GtkWindow *window;

    editor = contact_list_editor_extract (widget);
    window = GTK_WINDOW (WIDGET (DIALOG));

    eab_editor_prompt_to_save_changes (EAB_EDITOR (editor), window);
}

void
contact_list_editor_check_button_toggled_cb (GtkWidget *widget);

void
contact_list_editor_check_button_toggled_cb (GtkWidget *widget)
{
    EContactListEditor *editor;

    editor = contact_list_editor_extract (widget);

    editor->priv->changed = TRUE;
    contact_list_editor_update (editor);
}

gboolean
contact_list_editor_delete_event_cb (GtkWidget *widget,
                                     GdkEvent *event);

gboolean
contact_list_editor_delete_event_cb (GtkWidget *widget,
                                     GdkEvent *event)
{
    EContactListEditor *editor;
    GtkWindow *window;

    editor = contact_list_editor_extract (widget);
    window = GTK_WINDOW (WIDGET (DIALOG));

    /* If we're in an async call, don't allow the dialog to close. */
    if (!editor->priv->in_async_call)
        eab_editor_prompt_to_save_changes (
            EAB_EDITOR (editor), window);

    return TRUE;
}

void
contact_list_editor_drag_data_received_cb (GtkWidget *widget,
                                           GdkDragContext *context,
                                             gint x, gint y,
                                             GtkSelectionData *selection_data,
                                             guint info,
                                             guint time);

void
contact_list_editor_drag_data_received_cb (GtkWidget *widget,
                                           GdkDragContext *context,
                                           gint x, gint y,
                                           GtkSelectionData *selection_data,
                                           guint info,
                                           guint time)
{
    EContactListEditor *editor;
    EContactListModel *model;
    gboolean changed = FALSE;
    gboolean handled = FALSE;
    const guchar *data;
    GList *list, *iter;
    GdkAtom target;

    editor = contact_list_editor_extract (widget);

    model = E_CONTACT_LIST_MODEL (editor->priv->model);

    /* Sanity check the selection target. */
    target = gtk_selection_data_get_target (selection_data);
    if (!e_targets_include_directory (&target, 1))
        goto exit;

    data = gtk_selection_data_get_data (selection_data);
    list = eab_contact_list_from_string ((gchar *) data);

    if (list != NULL)
        handled = TRUE;

    for (iter = list; iter != NULL; iter = iter->next) {
        EContact *contact = iter->data;
        const gchar *email;

        if (e_contact_get (contact, E_CONTACT_IS_LIST))
            continue;

        email = e_contact_get (contact, E_CONTACT_EMAIL_1);
        if (email == NULL) {
            g_warning (
                "Contact with no email-ids listed "
                "can't be added to a Contact-List");
            continue;
        }

        if (!contact_list_editor_contact_exists (model, email)) {
            /* Hard-wired for default e-mail */
            e_contact_list_model_add_contact (model, contact, 0);
            changed = TRUE;
        }
    }

    g_list_foreach (list, (GFunc) g_object_unref, NULL);
    g_list_free (list);

    contact_list_editor_scroll_to_end (editor);

    if (changed) {
        editor->priv->changed = TRUE;
        contact_list_editor_update (editor);
    }

exit:
    gtk_drag_finish (context, handled, FALSE, time);
}

gboolean
contact_list_editor_drag_drop_cb (GtkWidget *widget,
                                  GdkDragContext *context,
                                  gint x, gint y,
                                  guint time);

gboolean
contact_list_editor_drag_drop_cb (GtkWidget *widget,
                                  GdkDragContext *context,
                                  gint x, gint y,
                                  guint time)
{
    GList *iter;

    for (iter = context->targets; iter != NULL; iter = iter->next) {
        GdkAtom target = GDK_POINTER_TO_ATOM (iter->data);

        if (e_targets_include_directory (&target, 1)) {
            gtk_drag_get_data (widget, context, target, time);
            return TRUE;
        }
    }

    return FALSE;
}

gboolean
contact_list_editor_drag_motion_cb (GtkWidget *widget,
                                    GdkDragContext *context,
                                    gint x, gint y,
                                    guint time);

gboolean
contact_list_editor_drag_motion_cb (GtkWidget *widget,
                                    GdkDragContext *context,
                                    gint x, gint y,
                                    guint time)
{
    GList *iter;

    for (iter = context->targets; iter != NULL; iter = iter->next) {
        GdkAtom target = GDK_POINTER_TO_ATOM (iter->data);

        if (e_targets_include_directory (&target, 1)) {
            gdk_drag_status (context, GDK_ACTION_LINK, time);
            return TRUE;
        }
    }

    return FALSE;
}

void
contact_list_editor_email_entry_activate_cb (GtkWidget *widget);

void
contact_list_editor_email_entry_activate_cb (GtkWidget *widget)
{
    EContactListEditor *editor;

    editor = contact_list_editor_extract (widget);

    contact_list_editor_add_email (editor);
}

void
contact_list_editor_email_entry_changed_cb (GtkWidget *widget);

void
contact_list_editor_email_entry_changed_cb (GtkWidget *widget)
{
    EContactListEditor *editor;
    const gchar *text;
    gboolean sensitive;

    editor = contact_list_editor_extract (widget);
    text = gtk_entry_get_text (GTK_ENTRY (widget));

    sensitive = (text != NULL && *text != '\0');
    gtk_widget_set_sensitive (WIDGET (ADD_BUTTON), sensitive);
}

gboolean
contact_list_editor_email_entry_key_press_event_cb (GtkWidget *widget,
                                                    GdkEventKey *event);

gboolean
contact_list_editor_email_entry_key_press_event_cb (GtkWidget *widget,
                                                    GdkEventKey *event)
{
    EContactListEditor *editor;
    gboolean can_comma = FALSE;

    editor = contact_list_editor_extract (widget);

    if (event->keyval == GDK_comma) {
        GtkEntry *entry;
        gint cpos = -1;

        entry = GTK_ENTRY (WIDGET (EMAIL_ENTRY));
        g_object_get (G_OBJECT (entry), "cursor-position", &cpos, NULL);

        /* not the first letter */
        if (cpos > 0) {
            const gchar *text;
            gint quotes = 0, i;

            text = gtk_entry_get_text (entry);

            for (i = 0; text && text [i] && i < cpos; i++) {
                if (text [i] == '\"')
                    quotes++;
            }

            /* even count of quotes */
            can_comma = (quotes & 1) == 0;
        }
    }

    if (can_comma || event->keyval == GDK_Return) {
        g_signal_emit_by_name (widget, "activate", 0);
        contact_list_editor_add_email (editor);

        return TRUE;
    }

    return FALSE;
}

void
contact_list_editor_email_entry_updated_cb (GtkWidget *widget,
                                            EDestination *destination);

void
contact_list_editor_email_entry_updated_cb (GtkWidget *widget,
                                            EDestination *destination)
{
    EContactListEditor *editor;
    ENameSelectorEntry *entry;
    EContactListModel *model;
    EDestinationStore *store;
    gchar *email;

    editor = contact_list_editor_extract (widget);

    entry = E_NAME_SELECTOR_ENTRY (widget);
    model = E_CONTACT_LIST_MODEL (editor->priv->model);

    email = g_strdup (e_destination_get_address (destination));
    store = e_name_selector_entry_peek_destination_store (entry);
    e_destination_store_remove_destination (store, destination);
    gtk_entry_set_text (GTK_ENTRY (WIDGET (EMAIL_ENTRY)), "");

    if (email && *email) {
        e_contact_list_model_add_email (model, email);
        contact_list_editor_scroll_to_end (editor);
        editor->priv->changed = TRUE;
    }

    g_free (email);
    contact_list_editor_update (editor);
}

void
contact_list_editor_list_name_entry_changed_cb (GtkWidget *widget);

void
contact_list_editor_list_name_entry_changed_cb (GtkWidget *widget)
{
    EContactListEditor *editor;
    const gchar *title;

    editor = contact_list_editor_extract (widget);

    title = gtk_entry_get_text (GTK_ENTRY (widget));

    if (title == NULL || *title == '\0')
        title = _("Contact List Editor");

    gtk_window_set_title (GTK_WINDOW (WIDGET (DIALOG)), title);

    editor->priv->changed = TRUE;
    contact_list_editor_update (editor);
}

void
contact_list_editor_ok_button_clicked_cb (GtkWidget *widget);

void
contact_list_editor_ok_button_clicked_cb (GtkWidget *widget)
{
    EContactListEditor *editor;
    gboolean save_contact;

    editor = contact_list_editor_extract (widget);

    save_contact =
        editor->priv->editable &&
        editor->priv->allows_contact_lists;

    if (save_contact)
        eab_editor_save_contact (EAB_EDITOR (editor), TRUE);
    else
        eab_editor_close (EAB_EDITOR (editor));
}

void
contact_list_editor_remove_button_clicked_cb (GtkWidget *widget);

void
contact_list_editor_remove_button_clicked_cb (GtkWidget *widget)
{
    EContactListEditor *editor;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeView *view;
    GList *list, *iter;

    editor = contact_list_editor_extract (widget);

    view = GTK_TREE_VIEW (WIDGET (TREE_VIEW));
    selection = gtk_tree_view_get_selection (view);
    list = gtk_tree_selection_get_selected_rows (selection, &model);

    /* Convert the GtkTreePaths to GtkTreeRowReferences. */
    for (iter = list; iter != NULL; iter = iter->next) {
        GtkTreePath *path = iter->data;

        iter->data = gtk_tree_row_reference_new (model, path);
        gtk_tree_path_free (path);
    }

    /* Delete each row in the list. */
    for (iter = list; iter != NULL; iter = iter->next) {
        GtkTreeRowReference *reference = iter->data;
        GtkTreePath *path;
        GtkTreeIter iter;
        gboolean valid;

        path = gtk_tree_row_reference_get_path (reference);
        valid = gtk_tree_model_get_iter (model, &iter, path);
        gtk_tree_path_free (path);
        g_assert (valid);

        gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
        gtk_tree_row_reference_free (reference);
    }

    g_list_free (list);

    editor->priv->changed = TRUE;
    contact_list_editor_update (editor);
}

void
contact_list_editor_select_button_clicked_cb (GtkWidget *widget);

void
contact_list_editor_select_button_clicked_cb (GtkWidget *widget)
{
    EContactListEditor *editor;
    EContactListModel *model;
    ENameSelectorDialog *dialog;
    EDestinationStore *store;
    GList *list, *iter;

    editor = contact_list_editor_extract (widget);

    model = E_CONTACT_LIST_MODEL (editor->priv->model);
    dialog = e_name_selector_peek_dialog (editor->priv->name_selector);
    gtk_window_set_title (GTK_WINDOW (dialog), _("Contact List Members"));

    /* We need to empty out the destination store, since we copy its
     * contents every time.  This sucks, we should really be wired
     * directly to the EDestinationStore that the name selector uses
     * in true MVC fashion. */

    e_name_selector_model_peek_section (
        e_name_selector_peek_model (editor->priv->name_selector),
        "Members", NULL, &store);

    list = e_destination_store_list_destinations (store);

    for (iter = list; iter != NULL; iter = iter->next)
        e_destination_store_remove_destination (store, iter->data);

    g_list_free (list);

    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_hide (GTK_WIDGET (dialog));

    list = e_destination_store_list_destinations (store);

    for (iter = list; iter != NULL; iter = iter->next) {
        EDestination *destination = iter->data;
        const gchar *email = e_destination_get_email (destination);

        if (email == NULL)
            continue;

        if (!contact_list_editor_contact_exists (model, email))
            e_contact_list_model_add_destination (
                model, destination);
    }

    g_list_free (list);

    editor->priv->changed = TRUE;
    contact_list_editor_update (editor);
}

void
contact_list_editor_source_menu_changed_cb (GtkWidget *widget);

void
contact_list_editor_source_menu_changed_cb (GtkWidget *widget)
{
    EContactListEditor *editor;
    ESource *source;

    editor = contact_list_editor_extract (widget);
    source = e_source_combo_box_get_active (E_SOURCE_COMBO_BOX (widget));

    if (e_source_equal (e_book_get_source (editor->priv->book), source))
        return;

    editor->priv->load_book = e_book_new (source, NULL);
    editor->priv->load_source_id = addressbook_load (
        editor->priv->load_book, (EBookCallback)
        contact_list_editor_book_loaded, editor);
}

gboolean
contact_list_editor_tree_view_key_press_event_cb (GtkWidget *widget,
                                                  GdkEventKey *event);
gboolean
contact_list_editor_tree_view_key_press_event_cb (GtkWidget *widget,
                                                  GdkEventKey *event)
{
    EContactListEditor *editor;

    editor = contact_list_editor_extract (widget);

    if (event->keyval == GDK_Delete) {
        g_signal_emit_by_name (WIDGET (REMOVE_BUTTON), "clicked");
        return TRUE;
    }

    return FALSE;
}

/******************** GtkBuilder Custom Widgets Functions ********************/

static gpointer
contact_editor_fudge_new (EBook *book,
                          EContact *contact,
                          gboolean is_new,
                          gboolean editable)
{
    EShell *shell = e_shell_get_default ();

    /* XXX Putting this function signature in libedataserverui
     *     was a terrible idea.  Now we're stuck with it. */

    return e_contact_editor_new (
        shell, book, contact, is_new, editable);
}

static gpointer
contact_list_editor_fudge_new (EBook *book,
                               EContact *contact,
                               gboolean is_new,
                               gboolean editable)
{
    EShell *shell = e_shell_get_default ();

    /* XXX Putting this function signature in libedataserverui
     *     was a terrible idea.  Now we're stuck with it. */

    return e_contact_list_editor_new (
        shell, book, contact, is_new, editable);
}

static void
setup_custom_widgets (EContactListEditor *editor)
{
    const gchar *key = "/apps/evolution/addressbook/sources";
    GtkWidget *combo_box;
    GConfClient *client;
    ESourceList *source_list;
    ENameSelectorEntry *name_selector_entry;
    ENameSelector *name_selector;
    GtkWidget *old, *parent;
    EContactListEditorPrivate *priv;
    guint ba = 0, la = 0, ra = 0, ta = 0, xo = 0, xp = 0, yo = 0, yp = 0;

    g_return_if_fail (editor != NULL);

    priv = E_CONTACT_LIST_EDITOR_GET_PRIVATE (editor);

    combo_box = WIDGET (SOURCE_MENU);
    client = gconf_client_get_default ();
    source_list = e_source_list_new_for_gconf (client, key);
    g_object_set (G_OBJECT (combo_box), "source-list", source_list, NULL);
    g_object_unref (source_list);
    g_object_unref (client);

    g_signal_connect (
        combo_box, "changed", G_CALLBACK (
        contact_list_editor_source_menu_changed_cb), NULL);

    old = CONTACT_LIST_EDITOR_WIDGET (editor, "email-entry");
    g_return_if_fail (old != NULL);

    name_selector = e_name_selector_new ();

    e_name_selector_model_add_section (
        e_name_selector_peek_model (name_selector),
        "Members", _("_Members"), NULL);

    name_selector_entry = e_name_selector_peek_section_entry (
        name_selector, "Members");

    gtk_widget_set_name (
        GTK_WIDGET (name_selector_entry),
        gtk_widget_get_name (old));
    parent = gtk_widget_get_parent (old);

    gtk_container_child_get (GTK_CONTAINER (parent), old,
        "bottom-attach", &ba,
        "left-attach", &la,
        "right-attach", &ra,
        "top-attach", &ta,
        "x-options", &xo,
        "x-padding", &xp,
        "y-options", &yo,
        "y-padding", &yp,
        NULL);

    /* only hide it... */
    gtk_widget_hide (old);

    /* ... and place the new name selector to the
     * exact place as is the old one in UI file */
    gtk_widget_show (GTK_WIDGET (name_selector_entry));
    gtk_table_attach (
        GTK_TABLE (parent), GTK_WIDGET (name_selector_entry),
        la, ra, ta, ba, xo, yo, xp, yp);
    priv->email_entry = name_selector_entry;

    e_name_selector_entry_set_contact_editor_func (
        name_selector_entry, contact_editor_fudge_new);
    e_name_selector_entry_set_contact_list_editor_func (
        name_selector_entry, contact_list_editor_fudge_new);

    g_signal_connect (
        name_selector_entry, "activate", G_CALLBACK (
        contact_list_editor_email_entry_activate_cb), NULL);
    g_signal_connect (
        name_selector_entry, "changed", G_CALLBACK (
        contact_list_editor_email_entry_changed_cb), NULL);
    g_signal_connect (
        name_selector_entry, "key-press-event", G_CALLBACK (
        contact_list_editor_email_entry_key_press_event_cb), NULL);
    g_signal_connect (
        name_selector_entry, "updated", G_CALLBACK (
        contact_list_editor_email_entry_updated_cb), NULL);
}

/***************************** GObject Callbacks *****************************/

static GObject *
contact_list_editor_constructor (GType type,
                                 guint n_construct_properties,
                                 GObjectConstructParam *construct_properties)
{
    GObject *object;

    /* Chain up to parent's constructor() method. */
    object = G_OBJECT_CLASS (parent_class)->constructor (
        type, n_construct_properties, construct_properties);

    contact_list_editor_update (E_CONTACT_LIST_EDITOR (object));

    return object;
}

static void
contact_list_editor_set_property (GObject *object,
                                  guint property_id,
                  const GValue *value,
                                  GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_BOOK:
            e_contact_list_editor_set_book (
                E_CONTACT_LIST_EDITOR (object),
                g_value_get_object (value));
            return;

        case PROP_CONTACT:
            e_contact_list_editor_set_contact (
                E_CONTACT_LIST_EDITOR (object),
                g_value_get_object (value));
            return;

        case PROP_IS_NEW_LIST:
            e_contact_list_editor_set_is_new_list (
                E_CONTACT_LIST_EDITOR (object),
                g_value_get_boolean (value));
            return;

        case PROP_EDITABLE:
            e_contact_list_editor_set_editable (
                E_CONTACT_LIST_EDITOR (object),
                g_value_get_boolean (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
contact_list_editor_get_property (GObject *object,
                                  guint property_id,
                                  GValue *value,
                                  GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_BOOK:
            g_value_set_object (
                value,
                e_contact_list_editor_get_book (
                E_CONTACT_LIST_EDITOR (object)));
            return;

        case PROP_CONTACT:
            g_value_set_object (
                value,
                e_contact_list_editor_get_contact (
                E_CONTACT_LIST_EDITOR (object)));
            return;

        case PROP_IS_NEW_LIST:
            g_value_set_boolean (
                value,
                e_contact_list_editor_get_is_new_list (
                E_CONTACT_LIST_EDITOR (object)));
            return;

        case PROP_EDITABLE:
            g_value_set_boolean (
                value,
                e_contact_list_editor_get_editable (
                E_CONTACT_LIST_EDITOR (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
contact_list_editor_dispose (GObject *object)
{
    EContactListEditor *editor = E_CONTACT_LIST_EDITOR (object);
    EContactListEditorPrivate *priv = editor->priv;

    contact_list_editor_cancel_load (editor);

    if (priv->name_selector) {
        g_object_unref (priv->name_selector);
        priv->name_selector = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (parent_class)->dispose (object);
}

/**************************** EABEditor Callbacks ****************************/

static void
contact_list_editor_show (EABEditor *editor)
{
    gtk_widget_show (WIDGET (DIALOG));
}

static void
contact_list_editor_close (EABEditor *editor)
{
    gtk_widget_destroy (WIDGET (DIALOG));
    eab_editor_closed (editor);
}

static void
contact_list_editor_raise (EABEditor *editor)
{
    GdkWindow *window;

    window = gtk_widget_get_window (WIDGET (DIALOG));
    gdk_window_raise (window);
}

static void
contact_list_editor_save_contact (EABEditor *eab_editor,
                                  gboolean should_close)
{
    EContactListEditor *editor = E_CONTACT_LIST_EDITOR (eab_editor);
    EContactListEditorPrivate *priv = editor->priv;
    EditorCloseStruct *ecs;
    EContact *contact;

    contact = e_contact_list_editor_get_contact (editor);

    if (priv->book == NULL)
        return;

    ecs = g_new (EditorCloseStruct, 1);
    ecs->editor = g_object_ref (editor);
    ecs->should_close = should_close;

    gtk_widget_set_sensitive (WIDGET (DIALOG), FALSE);
    priv->in_async_call = TRUE;

    if (priv->is_new_list)
        eab_merging_book_add_contact (
            priv->book, contact, (EBookIdCallback)
            contact_list_editor_list_added_cb, ecs);
    else
        eab_merging_book_commit_contact (
            priv->book, contact, (EBookCallback)
            contact_list_editor_list_modified_cb, ecs);

    priv->changed = FALSE;
}

static gboolean
contact_list_editor_is_valid (EABEditor *editor)
{
    GtkEditable *editable;
    gboolean valid;
    gchar *chars;

    editable = GTK_EDITABLE (WIDGET (LIST_NAME_ENTRY));
    chars = gtk_editable_get_chars (editable, 0, -1);
    valid = (chars != NULL && *chars != '\0');
    g_free (chars);

    return valid;
}

static gboolean
contact_list_editor_is_changed (EABEditor *editor)
{
    return E_CONTACT_LIST_EDITOR_GET_PRIVATE (editor)->changed;
}

static GtkWindow *
contact_list_editor_get_window (EABEditor *editor)
{
    return GTK_WINDOW (WIDGET (DIALOG));
}

static void
contact_list_editor_contact_added (EABEditor *editor,
                                   EBookStatus status,
                                   EContact *contact)
{
    if (status == E_BOOK_ERROR_OK)
        return;

    if (status == E_BOOK_ERROR_CANCELLED)
        return;

    eab_error_dialog (_("Error adding list"), status);
}

static void
contact_list_editor_contact_modified (EABEditor *editor,
                                      EBookStatus status,
                                      EContact *contact)
{
    if (status == E_BOOK_ERROR_OK)
        return;

    if (status == E_BOOK_ERROR_CANCELLED)
        return;

    eab_error_dialog (_("Error modifying list"), status);
}

static void
contact_list_editor_contact_deleted (EABEditor *editor,
                                     EBookStatus status,
                                     EContact *contact)
{
    if (status == E_BOOK_ERROR_OK)
        return;

    if (status == E_BOOK_ERROR_CANCELLED)
        return;

    eab_error_dialog (_("Error removing list"), status);
}

static void
contact_list_editor_closed (EABEditor *editor)
{
    g_object_unref (editor);
}

/****************************** GType Callbacks ******************************/

static void
contact_list_editor_class_init (EContactListEditorClass *class)
{
    GObjectClass *object_class;
    EABEditorClass *editor_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EContactListEditorPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->constructor = contact_list_editor_constructor;
    object_class->set_property = contact_list_editor_set_property;
    object_class->get_property = contact_list_editor_get_property;
    object_class->dispose = contact_list_editor_dispose;

    editor_class = EAB_EDITOR_CLASS (class);
    editor_class->show = contact_list_editor_show;
    editor_class->close = contact_list_editor_close;
    editor_class->raise = contact_list_editor_raise;
    editor_class->save_contact = contact_list_editor_save_contact;
    editor_class->is_valid = contact_list_editor_is_valid;
    editor_class->is_changed = contact_list_editor_is_changed;
    editor_class->get_window = contact_list_editor_get_window;
    editor_class->contact_added = contact_list_editor_contact_added;
    editor_class->contact_modified = contact_list_editor_contact_modified;
    editor_class->contact_deleted = contact_list_editor_contact_deleted;
    editor_class->editor_closed = contact_list_editor_closed;

    g_object_class_install_property (
        object_class,
        PROP_BOOK,
        g_param_spec_object (
            "book",
            "Book",
            NULL,
            E_TYPE_BOOK,
            G_PARAM_READWRITE));

    g_object_class_install_property (
        object_class,
        PROP_CONTACT,
        g_param_spec_object (
            "contact",
            "Contact",
            NULL,
            E_TYPE_CONTACT,
            G_PARAM_READWRITE));

    g_object_class_install_property (
        object_class,
        PROP_IS_NEW_LIST,
        g_param_spec_boolean (
            "is_new_list",
            "Is New List",
            NULL,
            FALSE,
            G_PARAM_READWRITE));

    g_object_class_install_property (
        object_class,
        PROP_EDITABLE,
        g_param_spec_boolean (
            "editable",
            "Editable",
            NULL,
            FALSE,
            G_PARAM_READWRITE));
}

static void
contact_list_editor_init (EContactListEditor *editor)
{
    EContactListEditorPrivate *priv;
    GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;
    GtkTreeView *view;

    priv = E_CONTACT_LIST_EDITOR_GET_PRIVATE (editor);

    priv->editable = TRUE;
    priv->allows_contact_lists = TRUE;

    priv->builder = gtk_builder_new ();
    e_load_ui_builder_definition (priv->builder, "contact-list-editor.ui");
    gtk_builder_connect_signals (priv->builder, NULL);

    /* Embed a pointer to the EContactListEditor in the top-level
     * widget.  Signal handlers can then access the pointer from any
     * child widget by calling contact_list_editor_extract(widget). */
    g_object_set_data (G_OBJECT (WIDGET (DIALOG)), TOPLEVEL_KEY, editor);

    view = GTK_TREE_VIEW (WIDGET (TREE_VIEW));
    priv->model = e_contact_list_model_new ();
    gtk_tree_view_set_model (view, priv->model);

    gtk_tree_selection_set_mode (
        gtk_tree_view_get_selection (view), GTK_SELECTION_MULTIPLE);

    gtk_tree_view_enable_model_drag_dest (view, NULL, 0, GDK_ACTION_LINK);
    e_drag_dest_add_directory_targets (WIDGET (TREE_VIEW));

    g_signal_connect (
        priv->model, "row-deleted",
        G_CALLBACK (contact_list_editor_row_deleted_cb), editor);
    g_signal_connect (
        priv->model, "row-inserted",
        G_CALLBACK (contact_list_editor_row_inserted_cb), editor);

    column = gtk_tree_view_column_new ();
    renderer = gtk_cell_renderer_text_new ();
    g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
    gtk_tree_view_column_pack_start (column, renderer, TRUE);
    gtk_tree_view_append_column (view, column);

    gtk_tree_view_column_set_cell_data_func (
        column, renderer, (GtkTreeCellDataFunc)
        contact_list_editor_render_destination, NULL, NULL);

    priv->name_selector = e_name_selector_new ();

    e_name_selector_model_add_section (
        e_name_selector_peek_model (priv->name_selector),
        "Members", _("_Members"), NULL);

    g_signal_connect (
        editor, "notify::book",
        G_CALLBACK (contact_list_editor_notify_cb), NULL);
    g_signal_connect (
        editor, "notify::editable",
        G_CALLBACK (contact_list_editor_notify_cb), NULL);

    gtk_widget_show_all (WIDGET (DIALOG));

    setup_custom_widgets (editor);

    editor->priv = priv;
}

/***************************** Public Interface ******************************/

GType
e_contact_list_editor_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
        type = g_type_register_static_simple (
            EAB_TYPE_EDITOR,
            "EContactListEditor",
            sizeof (EContactListEditorClass),
            (GClassInitFunc) contact_list_editor_class_init,
            sizeof (EContactListEditor),
            (GInstanceInitFunc) contact_list_editor_init, 0);

    return type;
}

EABEditor *
e_contact_list_editor_new (EShell *shell,
                           EBook *book,
                           EContact *list_contact,
                           gboolean is_new_list,
                           gboolean editable)
{
    EABEditor *editor;

    g_return_val_if_fail (E_IS_SHELL (shell), NULL);

    editor = g_object_new (
        E_TYPE_CONTACT_LIST_EDITOR,
        "shell", shell, NULL);

    g_object_set (editor,
              "book", book,
              "contact", list_contact,
              "is_new_list", is_new_list,
              "editable", editable,
              NULL);

    return editor;
}

EBook *
e_contact_list_editor_get_book (EContactListEditor *editor)
{
    g_return_val_if_fail (E_IS_CONTACT_LIST_EDITOR (editor), NULL);

    return editor->priv->book;
}

void
e_contact_list_editor_set_book (EContactListEditor *editor,
                                EBook *book)
{
    g_return_if_fail (E_IS_CONTACT_LIST_EDITOR (editor));
    g_return_if_fail (E_IS_BOOK (book));

    if (editor->priv->book != NULL)
        g_object_unref (editor->priv->book);
    editor->priv->book = g_object_ref (book);

    editor->priv->allows_contact_lists =
        e_book_check_static_capability (
        editor->priv->book, "contact-lists");

    contact_list_editor_update (editor);

    g_object_notify (G_OBJECT (editor), "book");
}

EContact *
e_contact_list_editor_get_contact (EContactListEditor *editor)
{
    GtkTreeModel *model;
    EContact *contact;
    GtkTreeIter iter;
    gboolean iter_valid;
    const gchar *text;
    GSList *attrs = NULL, *a;

    g_return_val_if_fail (E_IS_CONTACT_LIST_EDITOR (editor), NULL);

    model = editor->priv->model;
    contact = editor->priv->contact;

    if (contact == NULL)
        return NULL;

    text = gtk_entry_get_text (GTK_ENTRY (WIDGET (LIST_NAME_ENTRY)));
    if (text != NULL && *text != '\0') {
        e_contact_set (contact, E_CONTACT_FILE_AS, (gpointer) text);
        e_contact_set (contact, E_CONTACT_FULL_NAME, (gpointer) text);
    }

    e_contact_set (contact, E_CONTACT_LOGO, NULL);
    e_contact_set (contact, E_CONTACT_IS_LIST, GINT_TO_POINTER (TRUE));

    e_contact_set (
        contact, E_CONTACT_LIST_SHOW_ADDRESSES,
        GINT_TO_POINTER (!gtk_toggle_button_get_active (
        GTK_TOGGLE_BUTTON (WIDGET (CHECK_BUTTON)))));

    e_vcard_remove_attributes (E_VCARD (contact), "", EVC_EMAIL);

    iter_valid = gtk_tree_model_get_iter_first (model, &iter);

    while (iter_valid) {
        EDestination *dest;
        EVCardAttribute *attr;

        gtk_tree_model_get (model, &iter, 0, &dest, -1);
        attr = e_vcard_attribute_new (NULL, EVC_EMAIL);
        attrs = g_slist_prepend (attrs, attr);
        e_destination_export_to_vcard_attribute (dest, attr);
        g_object_unref (dest);

        iter_valid = gtk_tree_model_iter_next (model, &iter);
    }

    /* Put it in reverse order because e_vcard_add_attribute also uses prepend,
       but we want to keep order of mails there. Hopefully noone will change
       the behaviour of the e_vcard_add_attribute. */
    for (a = attrs; a; a = a->next) {
        e_vcard_add_attribute (E_VCARD (contact), a->data);
    }

    return contact;
}

/* Helper for e_contact_list_editor_set_contact() */
static void
contact_list_editor_add_destination (EVCardAttribute *attr,
                                     EContactListEditor *editor)
{
    EDestination *destination;
    gchar *contact_uid = NULL;
    gint email_num = -1;
    GList *list, *iter;
    GList *values;
    gchar *value;

    destination = e_destination_new ();

    list = e_vcard_attribute_get_params (attr);
    for (iter = list; iter; iter = iter->next) {
        EVCardAttributeParam *param = iter->data;
        const gchar *param_name;
        gpointer param_data;

        values = e_vcard_attribute_param_get_values (param);
        if (values == NULL)
            continue;

        param_name = e_vcard_attribute_param_get_name (param);
        param_data = values->data;

        if (!g_ascii_strcasecmp (param_name, EVC_X_DEST_CONTACT_UID))
            contact_uid = param_data;
        else if (!g_ascii_strcasecmp (param_name, EVC_X_DEST_EMAIL_NUM))
            email_num = atoi (param_data);
        else if (!g_ascii_strcasecmp (param_name, EVC_X_DEST_HTML_MAIL))
            e_destination_set_html_mail_pref (
                destination,
                !g_ascii_strcasecmp (param_data, "true"));
    }

    value = e_vcard_attribute_get_value (attr);
    if (value)
        e_destination_set_raw (destination, value);
    g_free (value);

    if (contact_uid != NULL)
        e_destination_set_contact_uid (
            destination, contact_uid, email_num);

    e_contact_list_model_add_destination (
        E_CONTACT_LIST_MODEL (editor->priv->model), destination);

    e_vcard_attribute_free (attr);
}

void
e_contact_list_editor_set_contact (EContactListEditor *editor,
                                   EContact *contact)
{
    EContactListEditorPrivate *priv;

    g_return_if_fail (E_IS_CONTACT_LIST_EDITOR (editor));
    g_return_if_fail (E_IS_CONTACT (contact));

    priv = editor->priv;

    if (priv->contact != NULL)
        g_object_unref (priv->contact);
    priv->contact = e_contact_duplicate (contact);

    if (priv->contact != NULL) {
        const gchar *file_as;
        gboolean show_addresses;
        GList *email_list;

        file_as = e_contact_get_const (
            priv->contact, E_CONTACT_FILE_AS);
        email_list = e_contact_get_attributes (
            priv->contact, E_CONTACT_EMAIL);
        show_addresses = GPOINTER_TO_INT (e_contact_get (
            priv->contact, E_CONTACT_LIST_SHOW_ADDRESSES));

        if (file_as == NULL)
            file_as = "";

        gtk_entry_set_text (
            GTK_ENTRY (WIDGET (LIST_NAME_ENTRY)), file_as);

        gtk_toggle_button_set_active (
            GTK_TOGGLE_BUTTON (WIDGET (CHECK_BUTTON)),
            !show_addresses);

        e_contact_list_model_remove_all (
            E_CONTACT_LIST_MODEL (priv->model));

        g_list_foreach (
            email_list, (GFunc)
            contact_list_editor_add_destination, editor);
        g_list_free (email_list);
    }

    if (priv->book != NULL) {
        e_source_combo_box_set_active (
            E_SOURCE_COMBO_BOX (WIDGET (SOURCE_MENU)),
            e_book_get_source (priv->book));
        gtk_widget_set_sensitive (
            WIDGET (SOURCE_MENU), priv->is_new_list);
    }

    priv->changed = FALSE;
    contact_list_editor_update (editor);

    g_object_notify (G_OBJECT (editor), "contact");
}

gboolean
e_contact_list_editor_get_is_new_list (EContactListEditor *editor)
{
    g_return_val_if_fail (E_IS_CONTACT_LIST_EDITOR (editor), FALSE);

    return editor->priv->is_new_list;
}

void
e_contact_list_editor_set_is_new_list (EContactListEditor *editor,
                                       gboolean is_new_list)
{

    g_return_if_fail (E_IS_CONTACT_LIST_EDITOR (editor));

    editor->priv->is_new_list = is_new_list;
    contact_list_editor_update (editor);

    g_object_notify (G_OBJECT (editor), "is_new_list");
}

gboolean
e_contact_list_editor_get_editable (EContactListEditor *editor)
{
    g_return_val_if_fail (E_IS_CONTACT_LIST_EDITOR (editor), FALSE);

    return editor->priv->editable;
}

void
e_contact_list_editor_set_editable (EContactListEditor *editor,
                                    gboolean editable)
{
    g_return_if_fail (E_IS_CONTACT_LIST_EDITOR (editor));

    editor->priv->editable = editable;
    contact_list_editor_update (editor);

    g_object_notify (G_OBJECT (editor), "editable");
}