aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/contact-list-editor/e-contact-list-editor.c
blob: fd48e53af7adbf0a46eac82a42674e46b64c1363 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* e-contact-list-editor.c
 * Copyright (C) 2001  Ximian, Inc.
 * Author: Chris Toshok <toshok@ximian.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include "e-contact-list-editor.h"
#include <e-util/e-icon-factory.h>

#include <string.h>

#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-window-icon.h>
#include <gtk/gtkentry.h>
#include <gtk/gtktogglebutton.h>
#include <gtk/gtkdialog.h>

#include <libedataserverui/e-source-option-menu.h>

#include <table/e-table-scrolled.h>
#include <libgnomevfs/gnome-vfs-ops.h>

#include "shell/evolution-shell-component-utils.h"

#include "misc/e-image-chooser.h"

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

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

static void e_contact_list_editor_init      (EContactListEditor      *editor);
static void e_contact_list_editor_class_init    (EContactListEditorClass     *klass);
static void e_contact_list_editor_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void e_contact_list_editor_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
static void e_contact_list_editor_dispose (GObject *object);

static void     e_contact_list_editor_show       (EABEditor *editor);
static void     e_contact_list_editor_raise      (EABEditor *editor);
static void     e_contact_list_editor_close      (EABEditor *editor);
static void     e_contact_list_editor_save_contact (EABEditor *editor, gboolean should_close);
static gboolean e_contact_list_editor_is_valid   (EABEditor *editor);
static gboolean e_contact_list_editor_is_changed (EABEditor *editor);
static GtkWindow* e_contact_list_editor_get_window (EABEditor *editor);

static void set_editable (EContactListEditor *editor);
static void command_state_changed (EContactListEditor *editor);
static void extract_info(EContactListEditor *editor);
static void fill_in_info(EContactListEditor *editor);

static void add_email_cb (GtkWidget *w, EContactListEditor *editor);
static void remove_entry_cb (GtkWidget *w, EContactListEditor *editor);
static void select_cb (GtkWidget *w, EContactListEditor *editor);
static void list_name_changed_cb (GtkWidget *w, EContactListEditor *editor);
static void list_image_changed_cb (GtkWidget *w, EContactListEditor *editor);
static void visible_addrs_toggled_cb (GtkWidget *w, EContactListEditor *editor);
static void source_selected (GtkWidget *source_option_menu, ESource *source, EContactListEditor *editor);

static void close_cb (GtkWidget *widget, EContactListEditor *editor);
static void save_and_close_cb (GtkWidget *widget, EContactListEditor *editor);

static gint app_delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data);
static gboolean table_drag_drop_cb (ETable *table, int row, int col, GdkDragContext *context,
                    gint x, gint y, guint time, EContactListEditor *editor);
static gboolean table_drag_motion_cb (ETable *table, int row, int col, GdkDragContext *context,
                      gint x, gint y, guint time, EContactListEditor *editor);
static void table_drag_data_received_cb (ETable *table, int row, int col,
                     GdkDragContext *context,
                     gint x, gint y,
                     GtkSelectionData *selection_data, guint info, guint time,
                     EContactListEditor *editor);

static EABEditorClass *parent_class = NULL;

enum DndTargetType {
    DND_TARGET_TYPE_VCARD,
};
#define VCARD_TYPE "text/x-vcard"

static GtkTargetEntry list_drag_types[] = {
    { VCARD_TYPE, 0, DND_TARGET_TYPE_VCARD },
};
static const int num_list_drag_types = sizeof (list_drag_types) / sizeof (list_drag_types[0]);

/* The arguments we take */
enum {
    PROP_0,
    PROP_BOOK,
    PROP_CONTACT,
    PROP_IS_NEW_LIST,
    PROP_EDITABLE
};

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

    if (!cle_type) {
        static const GTypeInfo cle_info =  {
            sizeof (EContactListEditorClass),
            NULL,           /* base_init */
            NULL,           /* base_finalize */
            (GClassInitFunc) e_contact_list_editor_class_init,
            NULL,           /* class_finalize */
            NULL,           /* class_data */
            sizeof (EContactListEditor),
            0,             /* n_preallocs */
            (GInstanceInitFunc) e_contact_list_editor_init,
        };

        cle_type = g_type_register_static (EAB_TYPE_EDITOR, "EContactListEditor", &cle_info, 0);
    }

    return cle_type;
}


static void
e_contact_list_editor_class_init (EContactListEditorClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    EABEditorClass *editor_class = EAB_EDITOR_CLASS (klass);

    parent_class = g_type_class_ref (EAB_TYPE_EDITOR);

    editor_class->show = e_contact_list_editor_show;
    editor_class->raise = e_contact_list_editor_raise;
    editor_class->close = e_contact_list_editor_close;
    editor_class->save_contact = e_contact_list_editor_save_contact;
    editor_class->is_valid = e_contact_list_editor_is_valid;
    editor_class->is_changed = e_contact_list_editor_is_changed;
    editor_class->get_window = e_contact_list_editor_get_window;

    object_class->set_property = e_contact_list_editor_set_property;
    object_class->get_property = e_contact_list_editor_get_property;
    object_class->dispose = e_contact_list_editor_dispose;

    g_object_class_install_property (object_class, PROP_BOOK, 
                     g_param_spec_object ("book",
                                  _("Book"),
                                  /*_( */"XXX blurb" /*)*/,
                                  E_TYPE_BOOK,
                                  G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_CONTACT, 
                     g_param_spec_object ("contact",
                                  _("Contact"),
                                  /*_( */"XXX blurb" /*)*/,
                                  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"),
                                   /*_( */"XXX blurb" /*)*/,
                                   FALSE,
                                   G_PARAM_READWRITE));

    g_object_class_install_property (object_class, PROP_EDITABLE, 
                     g_param_spec_boolean ("editable",
                                   _("Editable"),
                                   /*_( */"XXX blurb" /*)*/,
                                   FALSE,
                                   G_PARAM_READWRITE));
}

static void
e_contact_list_editor_init (EContactListEditor *editor)
{
    GladeXML *gui;
    GList *icon_list;

    editor->contact = NULL;
    editor->changed = FALSE;
    editor->image_set = FALSE;
    editor->editable = TRUE;
    editor->allows_contact_lists = TRUE;
    editor->in_async_call = FALSE;
    editor->is_new_list = FALSE;

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

    gui = glade_xml_new (EVOLUTION_GLADEDIR "/contact-list-editor.glade", NULL, NULL);
    editor->gui = gui;

    editor->app = glade_xml_get_widget (gui, "contact list editor");

    editor->table = glade_xml_get_widget (gui, "contact-list-table");
    editor->model = g_object_get_data (G_OBJECT(editor->table), "model");

    /* XXX need this for libglade-2 it seems */
    gtk_widget_show (editor->table);

    editor->add_button = glade_xml_get_widget (editor->gui, "add-email-button");
    editor->remove_button = glade_xml_get_widget (editor->gui, "remove-button");
    editor->select_button = glade_xml_get_widget (editor->gui, "select-button");

    editor->email_entry = glade_xml_get_widget (gui, "email-entry");
    editor->list_name_entry = glade_xml_get_widget (gui, "list-name-entry");
    editor->list_image = glade_xml_get_widget (gui, "list-image");
    editor->visible_addrs_checkbutton = glade_xml_get_widget (gui, "visible-addrs-checkbutton");
    editor->source_menu = glade_xml_get_widget (gui, "source-option-menu-source");

    editor->ok_button = glade_xml_get_widget (gui, "ok-button");
    editor->cancel_button = glade_xml_get_widget (gui, "cancel-button");

    /* connect signals */
    g_signal_connect (editor->add_button,
              "clicked", G_CALLBACK(add_email_cb), editor);
    g_signal_connect (editor->email_entry,
              "activate", G_CALLBACK(add_email_cb), editor);
    g_signal_connect (editor->remove_button,
              "clicked", G_CALLBACK(remove_entry_cb), editor);
    g_signal_connect (editor->select_button,
              "clicked", G_CALLBACK(select_cb), editor);
    g_signal_connect (editor->list_name_entry,
              "changed", G_CALLBACK(list_name_changed_cb), editor);
    g_signal_connect (editor->visible_addrs_checkbutton,
              "toggled", G_CALLBACK(visible_addrs_toggled_cb), editor);

    e_table_drag_dest_set (e_table_scrolled_get_table (E_TABLE_SCROLLED (editor->table)),
                   0, list_drag_types, num_list_drag_types, GDK_ACTION_LINK);

    g_signal_connect (e_table_scrolled_get_table (E_TABLE_SCROLLED (editor->table)),
              "table_drag_motion", G_CALLBACK(table_drag_motion_cb), editor);
    g_signal_connect (e_table_scrolled_get_table (E_TABLE_SCROLLED (editor->table)),
              "table_drag_drop", G_CALLBACK (table_drag_drop_cb), editor);
    g_signal_connect (e_table_scrolled_get_table (E_TABLE_SCROLLED (editor->table)),
              "table_drag_data_received", G_CALLBACK(table_drag_data_received_cb), editor);

    g_signal_connect (editor->ok_button,
              "clicked", G_CALLBACK(save_and_close_cb), editor);
    g_signal_connect (editor->cancel_button,
              "clicked", G_CALLBACK(close_cb), editor);
    

    g_signal_connect (editor->list_image,
              "changed", G_CALLBACK(list_image_changed_cb), editor);

    g_signal_connect (editor->source_menu,
              "source_selected", G_CALLBACK (source_selected), editor);

    command_state_changed (editor);

    /* Connect to the deletion of the dialog */
    g_signal_connect (editor->app, "delete_event",
              G_CALLBACK (app_delete_event_cb), editor);

    gtk_dialog_set_has_separator (GTK_DIALOG (editor->app), FALSE);

    /* set the icon */
    icon_list = e_icon_factory_get_icon_list ("stock_contact-list");
    if (icon_list) {
        gtk_window_set_icon_list (GTK_WINDOW (editor->app), icon_list);
        g_list_foreach (icon_list, (GFunc) g_object_unref, NULL);
        g_list_free (icon_list);
    }

    gtk_widget_show_all (editor->app);
}

static void
new_target_cb (EBook *new_book, EBookStatus status, EContactListEditor *editor)
{
    editor->load_source_id = 0;
    editor->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_option_menu_select (E_SOURCE_OPTION_MENU (editor->source_menu),
                         e_book_get_source (editor->book));

        if (new_book)
            g_object_unref (new_book);
        return;
    }

    g_object_set (editor, "book", new_book, NULL);
    g_object_unref (new_book);
}

static void
cancel_load (EContactListEditor *editor)
{
    if (editor->load_source_id) {
        addressbook_load_cancel (editor->load_source_id);
        editor->load_source_id = 0;

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

static void
source_selected (GtkWidget *source_option_menu, ESource *source, EContactListEditor *editor)
{
    cancel_load (editor);

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

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

static void
e_contact_list_editor_dispose (GObject *object)
{
    EContactListEditor *editor = E_CONTACT_LIST_EDITOR (object);

    cancel_load (editor);

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

    if (G_OBJECT_CLASS (parent_class)->dispose)
        (* G_OBJECT_CLASS (parent_class)->dispose) (object);
}

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

static void
list_added_cb (EBook *book, EBookStatus status, const char *id, EditorCloseStruct *ecs)
{
    EContactListEditor *cle = ecs->cle;
    gboolean should_close = ecs->should_close;

    if (cle->app)
        gtk_widget_set_sensitive (cle->app, TRUE);
    cle->in_async_call = FALSE;

    e_contact_set (cle->contact, E_CONTACT_UID, (char*)id);

    eab_editor_contact_added (EAB_EDITOR (cle), status, cle->contact);

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

        if (should_close)
            eab_editor_close (EAB_EDITOR (cle));
        else
            command_state_changed (cle);
    }

    g_object_unref (cle);
    g_free (ecs);
}

static void
list_modified_cb (EBook *book, EBookStatus status, EditorCloseStruct *ecs)
{
    EContactListEditor *cle = ecs->cle;
    gboolean should_close = ecs->should_close;

    if (cle->app)
        gtk_widget_set_sensitive (cle->app, TRUE);
    cle->in_async_call = FALSE;

    eab_editor_contact_modified (EAB_EDITOR (cle), status, cle->contact);

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

    g_object_unref (cle); /* release ref held for ebook callback */
    g_free (ecs);
}

static void
save_contact (EContactListEditor *cle, gboolean should_close)
{
    extract_info (cle);

    if (cle->book) {
        EditorCloseStruct *ecs = g_new(EditorCloseStruct, 1);
        
        ecs->cle = cle;
        g_object_ref (cle);
        ecs->should_close = should_close;

        if (cle->app)
            gtk_widget_set_sensitive (cle->app, FALSE);
        cle->in_async_call = TRUE;

        if (cle->is_new_list)
            eab_merging_book_add_contact (cle->book, cle->contact, (EBookIdCallback)list_added_cb, ecs);
        else
            eab_merging_book_commit_contact (cle->book, cle->contact, (EBookCallback)list_modified_cb, ecs);

        cle->changed = FALSE;
    }
}

static gboolean
is_named (EContactListEditor *editor)
{
    char *string = gtk_editable_get_chars(GTK_EDITABLE (editor->list_name_entry), 0, -1);
    gboolean named = FALSE;

    if (string && *string) {
        named = TRUE;
    }

    g_free (string);

    return named;
}

static void
e_contact_list_editor_save_contact (EABEditor *editor, gboolean should_close)
{
    EContactListEditor *cle = E_CONTACT_LIST_EDITOR (editor);

    save_contact (cle, should_close);
}

static gboolean
e_contact_list_editor_is_valid (EABEditor *editor)
{
    EContactListEditor *cle = E_CONTACT_LIST_EDITOR (editor);

    return is_named (cle);
}

static gboolean
e_contact_list_editor_is_changed (EABEditor *editor)
{
    EContactListEditor *cle = E_CONTACT_LIST_EDITOR (editor);

    return cle->changed;
}

static GtkWindow*
e_contact_list_editor_get_window (EABEditor *editor)
{
    return GTK_WINDOW (E_CONTACT_LIST_EDITOR (editor)->app);
}

static void
close_cb (GtkWidget *widget, EContactListEditor *cle)
{
    eab_editor_prompt_to_save_changes (EAB_EDITOR (cle), GTK_WINDOW (cle->app));
}

static void
save_and_close_cb (GtkWidget *widget, EContactListEditor *cle)
{
    save_contact (cle, TRUE);
}

static void
contact_list_editor_destroy_notify (gpointer data,
                    GObject *where_the_object_was)
{
    eab_editor_remove (EAB_EDITOR (data));
}

EContactListEditor *
e_contact_list_editor_new (EBook *book,
               EContact *list_contact,
               gboolean is_new_list,
               gboolean editable)
{
    EContactListEditor *ce = g_object_new (E_TYPE_CONTACT_LIST_EDITOR, NULL);

    eab_editor_add (EAB_EDITOR (ce));
    g_object_weak_ref (G_OBJECT (ce), contact_list_editor_destroy_notify, ce);

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

    return ce;
}

static void
e_contact_list_editor_set_property (GObject *object, guint prop_id,
                    const GValue *value, GParamSpec *pspec)
{
    EContactListEditor *editor;

    editor = E_CONTACT_LIST_EDITOR (object);
    
    switch (prop_id){
    case PROP_BOOK: {
        gboolean changed;

        if (editor->book)
            g_object_unref (editor->book);
        editor->book = E_BOOK(g_value_get_object (value));
        g_object_ref (editor->book);
        /* XXX more here about editable/etc. */

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

        if (changed) {
            set_editable (editor);
            command_state_changed (editor);
        }
        break;
    }
    case PROP_CONTACT:
        if (editor->contact)
            g_object_unref (editor->contact);
        editor->contact = e_contact_duplicate(E_CONTACT(g_value_get_object (value)));
        fill_in_info(editor);
        editor->changed = FALSE;
        command_state_changed (editor);
        break;
    case PROP_IS_NEW_LIST: {
        gboolean new_value = g_value_get_boolean (value);
        gboolean changed = (editor->is_new_list != new_value);

        editor->is_new_list = new_value;
        
        if (changed)
            command_state_changed (editor);
        break;
    }
    case PROP_EDITABLE: {
        gboolean new_value = g_value_get_boolean (value);
        gboolean changed = (editor->editable != new_value);

        editor->editable = new_value;

        if (changed) {
            set_editable (editor);
            command_state_changed (editor);
        }
        break;
    }
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
e_contact_list_editor_get_property (GObject *object, guint prop_id,
                    GValue *value, GParamSpec *pspec)
{
    EContactListEditor *editor;

    editor = E_CONTACT_LIST_EDITOR (object);

    switch (prop_id) {
    case PROP_BOOK:
        g_value_set_object (value, editor->book);
        break;

    case PROP_CONTACT:
        extract_info(editor);
        g_value_set_object (value, editor->contact);
        break;

    case PROP_IS_NEW_LIST:
        g_value_set_boolean (value, editor->is_new_list);
        break;

    case PROP_EDITABLE:
        g_value_set_boolean (value, editor->editable);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
e_contact_list_editor_show (EABEditor *editor)
{
    EContactListEditor *cle = E_CONTACT_LIST_EDITOR (editor);
    gtk_widget_show (cle->app);
}

static void
e_contact_list_editor_raise (EABEditor *editor)
{
    EContactListEditor *cle = E_CONTACT_LIST_EDITOR (editor);
    gdk_window_raise (GTK_WIDGET (cle->app)->window);
}

static void
e_contact_list_editor_close (EABEditor *editor)
{
    EContactListEditor *cle = E_CONTACT_LIST_EDITOR (editor);
    g_assert (cle->app != NULL);

    gtk_widget_destroy (cle->app);
    cle->app = NULL;

    eab_editor_closed (EAB_EDITOR (cle));
}

GtkWidget *
e_contact_list_editor_create_table(gchar *name,
                   gchar *string1, gchar *string2,
                   gint int1, gint int2);

GtkWidget *
e_contact_list_editor_create_table(gchar *name,
                   gchar *string1, gchar *string2,
                   gint int1, gint int2)
{
    
    ETableModel *model;
    GtkWidget *table;

    model = e_contact_list_model_new ();

    table = e_table_scrolled_new_from_spec_file (model,
                      NULL,
                      EVOLUTION_ETSPECDIR "/e-contact-list-editor.etspec",
                      NULL);

    g_object_set_data(G_OBJECT(table), "model", model);

    return table;
}

static void
add_to_model (EContactListEditor *editor, GList *destinations)
{
    GList *l;

    for (l = destinations; l; l = g_list_next (l)) {
        EDestination *destination = l->data;
        e_contact_list_model_add_destination (E_CONTACT_LIST_MODEL(editor->model), destination);
    }
}

static void
select_names_ok_cb (GtkWidget *widget, gint response, gpointer data)
{
    EContactListEditor  *ce;
    ENameSelectorDialog *name_selector_dialog;
    ENameSelectorModel  *name_selector_model;
    EDestinationStore   *destination_store;
    GList               *destinations;

    ce = E_CONTACT_LIST_EDITOR (data);

    name_selector_dialog = e_name_selector_peek_dialog (ce->name_selector);
    gtk_widget_hide (GTK_WIDGET (name_selector_dialog));

    name_selector_model = e_name_selector_peek_model (ce->name_selector);
    e_name_selector_model_peek_section (name_selector_model, "Members", NULL, &destination_store);
    destinations = e_destination_store_list_destinations (destination_store);

    add_to_model (ce, destinations);

    g_list_free (destinations);

    ce->changed = TRUE;
    command_state_changed (ce);
}

static void
setup_name_selector (EContactListEditor *editor)
{
    ENameSelectorModel  *name_selector_model;
    ENameSelectorDialog *name_selector_dialog;

    if (editor->name_selector)
        return;

    editor->name_selector = e_name_selector_new ();

    name_selector_model = e_name_selector_peek_model (editor->name_selector);
    e_name_selector_model_add_section (name_selector_model, "Members", gettext ("_Members"), NULL);

    name_selector_dialog = e_name_selector_peek_dialog (editor->name_selector);
    gtk_window_set_title (GTK_WINDOW (name_selector_dialog), _("Contact List Members"));
    g_signal_connect (name_selector_dialog, "response",
              G_CALLBACK (select_names_ok_cb), editor);
}

static void
select_cb (GtkWidget *w, EContactListEditor *editor)
{
    ENameSelectorModel  *name_selector_model;
    ENameSelectorDialog *name_selector_dialog;
    EDestinationStore   *destination_store;
    GList               *destinations;
    GList               *l;

    setup_name_selector (editor);

    /* 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. */

    name_selector_model = e_name_selector_peek_model (editor->name_selector);
    e_name_selector_model_peek_section (name_selector_model, "Members", NULL, &destination_store);
    destinations = e_destination_store_list_destinations (destination_store);

    for (l = destinations; l; l = g_list_next (l)) {
        EDestination *destination = l->data;
        e_destination_store_remove_destination (destination_store, destination);
    }

    g_list_free (destinations);

    name_selector_dialog = e_name_selector_peek_dialog (editor->name_selector);
    gtk_widget_show (GTK_WIDGET (name_selector_dialog));
}

GtkWidget *
e_contact_list_editor_create_source_option_menu (gchar *name,
                         gchar *string1, gchar *string2,
                         gint int1, gint int2);

GtkWidget *
e_contact_list_editor_create_source_option_menu (gchar *name,
                         gchar *string1, gchar *string2,
                         gint int1, gint int2)
{

    GtkWidget   *menu;
    GConfClient *gconf_client;
    ESourceList *source_list;

    gconf_client = gconf_client_get_default ();
    source_list = e_source_list_new_for_gconf (gconf_client, "/apps/evolution/addressbook/sources");

    menu = e_source_option_menu_new (source_list);
    g_object_unref (source_list);

    gtk_widget_show (menu);
    return menu;
}

static void
add_email_cb (GtkWidget *w, EContactListEditor *editor)
{
    GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (editor->table));
    const char *text = gtk_entry_get_text (GTK_ENTRY(editor->email_entry));

    if (text && *text) {
        e_contact_list_model_add_email (E_CONTACT_LIST_MODEL(editor->model), text);

        /* Skip to the end of the list */
        if (adj->upper - adj->lower > adj->page_size)
            gtk_adjustment_set_value (adj, adj->upper);
        
        editor->changed = TRUE;

    }

    gtk_entry_set_text (GTK_ENTRY(editor->email_entry), "");
    command_state_changed (editor);
}

static void
remove_row (int model_row, EContactListEditor *editor)
{
    e_contact_list_model_remove_row (E_CONTACT_LIST_MODEL (editor->model), model_row);
}

static void
remove_entry_cb (GtkWidget *w, EContactListEditor *editor)
{
    e_table_selected_row_foreach (e_table_scrolled_get_table(E_TABLE_SCROLLED(editor->table)),
                      (EForeachFunc)remove_row, editor);
    editor->changed = TRUE;
    command_state_changed (editor);
}

static void
list_name_changed_cb (GtkWidget *w, EContactListEditor *editor)
{
    char *string = gtk_editable_get_chars(GTK_EDITABLE (w), 0, -1);
    char *title;

    editor->changed = TRUE;

    if (string && *string)
        title = string;
    else
        title = _("Contact List Editor");

    gtk_window_set_title (GTK_WINDOW (editor->app), title);

    g_free (string);

    command_state_changed (editor);
}

static void
list_image_changed_cb (GtkWidget *w, EContactListEditor *editor)
{
    editor->image_set = TRUE;
    editor->changed = TRUE;
    command_state_changed (editor);
}

static void
visible_addrs_toggled_cb (GtkWidget *w, EContactListEditor *editor)
{
    editor->changed = TRUE;
    command_state_changed (editor);
}

static void
set_editable (EContactListEditor *editor)
{
    gtk_widget_set_sensitive (editor->email_entry, editor->editable && editor->allows_contact_lists);
    gtk_widget_set_sensitive (editor->list_name_entry, editor->editable && editor->allows_contact_lists);
    gtk_widget_set_sensitive (editor->add_button, editor->editable && editor->allows_contact_lists);
    gtk_widget_set_sensitive (editor->remove_button, editor->editable && editor->allows_contact_lists);
    gtk_widget_set_sensitive (editor->select_button, editor->editable && editor->allows_contact_lists);
    gtk_widget_set_sensitive (editor->table, editor->editable && editor->allows_contact_lists);
}

/* Callback used when the editor is destroyed */
static gint
app_delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
{
    EContactListEditor *ce;

    ce = E_CONTACT_LIST_EDITOR (data);

    /* if we're in an async call, don't allow the dialog to close */
    if (ce->in_async_call)
        return TRUE;

    eab_editor_prompt_to_save_changes (EAB_EDITOR (ce), GTK_WINDOW (ce->app));
    return TRUE;
}

static gboolean
table_drag_motion_cb (ETable *table, int row, int col,
              GdkDragContext *context,
              gint x, gint y, guint time, EContactListEditor *editor)
{
    GList *p;

    for (p = context->targets; p != NULL; p = p->next) {
        char *possible_type;

        possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
        if (!strcmp (possible_type, VCARD_TYPE)) {
            g_free (possible_type);
            gdk_drag_status (context, GDK_ACTION_LINK, time);
            return TRUE;
        }

        g_free (possible_type);
    }

    return FALSE;
}

static gboolean
table_drag_drop_cb (ETable *table, int row, int col,
            GdkDragContext *context,
            gint x, gint y, guint time, EContactListEditor *editor)
{
    GList *p;

    for (p = context->targets; p != NULL; p = p->next) {
        char *possible_type;

        possible_type = gdk_atom_name (GDK_POINTER_TO_ATOM (p->data));
        if (!strcmp (possible_type, VCARD_TYPE)) {
            g_free (possible_type);
            break;
        }

        g_free (possible_type);
    }


    if (p) {
        gtk_drag_get_data (GTK_WIDGET (table), context,
                   GDK_POINTER_TO_ATOM (p->data),
                   time);
        return TRUE;
    }

    return FALSE;
}

static void
table_drag_data_received_cb (ETable *table, int row, int col,
                 GdkDragContext *context,
                 gint x, gint y,
                 GtkSelectionData *selection_data,
                 guint info, guint time, EContactListEditor *editor)
{
    GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (editor->table));
    char *target_type;
    gboolean changed = FALSE;
    gboolean handled = FALSE;

    target_type = gdk_atom_name (selection_data->target);

    if (!strcmp (target_type, VCARD_TYPE)) {

        GList *contact_list = eab_contact_list_from_string (selection_data->data);
        GList *c;

        if (contact_list)
            handled = TRUE;

        for (c = contact_list; c; c = c->next) {
            EContact *contact = c->data;

            if (!e_contact_get (contact, E_CONTACT_IS_LIST)) {
                e_contact_list_model_add_contact (E_CONTACT_LIST_MODEL (editor->model),
                                  contact,
                                  0  /* Hard-wired for default e-mail */);

                changed = TRUE;
            }
        }
        g_list_foreach (contact_list, (GFunc)g_object_unref, NULL);
        g_list_free (contact_list);

        /* Skip to the end of the list */
        if (adj->upper - adj->lower > adj->page_size)
            gtk_adjustment_set_value (adj, adj->upper);

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

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

static void
command_state_changed (EContactListEditor *editor)
{
    gboolean valid = eab_editor_is_valid (EAB_EDITOR (editor));

    /* FIXME set the ok button to ok */
    gtk_widget_set_sensitive (editor->ok_button, valid && editor->allows_contact_lists);
    gtk_widget_set_sensitive (editor->source_menu, editor->is_new_list);
    gtk_widget_set_sensitive (glade_xml_get_widget (editor->gui, "source-label"), editor->is_new_list);
}

static void
extract_info(EContactListEditor *editor)
{
    EContact *contact = editor->contact;
    if (contact) {
        int i;
        char *image_data;
        gsize image_data_len;
        char *string = gtk_editable_get_chars(GTK_EDITABLE (editor->list_name_entry), 0, -1);

        if (string && *string) {
            e_contact_set (contact, E_CONTACT_FILE_AS, string);
            e_contact_set (contact, E_CONTACT_FULL_NAME, string);
        }

        g_free (string);

        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(editor->visible_addrs_checkbutton))));

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

        /* then refill it from the contact list model */
        for (i = 0; i < e_table_model_row_count (editor->model); i ++) {
            const EDestination *dest = e_contact_list_model_get_destination (E_CONTACT_LIST_MODEL (editor->model), i);
            EVCardAttribute *attr;

            attr = e_vcard_attribute_new (NULL, EVC_EMAIL);

            e_vcard_add_attribute (E_VCARD (contact), attr);

            e_destination_export_to_vcard_attribute ((EDestination*)dest, attr);
        }

        if (editor->image_set
            && e_image_chooser_get_image_data (E_IMAGE_CHOOSER (editor->list_image),
                               &image_data,
                               &image_data_len)) {
            EContactPhoto photo;

            photo.data = image_data;
            photo.length = image_data_len;

            e_contact_set (contact, E_CONTACT_LOGO, &photo);
            g_free (image_data);
        }
        else {
            e_contact_set (contact, E_CONTACT_LOGO, NULL);
        }
    }
}

static void
fill_in_info(EContactListEditor *editor)
{
    if (editor->contact) {
        EContactPhoto *photo;
        char *file_as;
        gboolean show_addresses = FALSE;
        gboolean is_evolution_list = FALSE;
        GList *email_list;
        GList *iter;

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

        gtk_editable_delete_text (GTK_EDITABLE (editor->list_name_entry), 0, -1);
        if (file_as) {
            int position = 0;
            gtk_editable_insert_text (GTK_EDITABLE (editor->list_name_entry), file_as, strlen (file_as), &position);
            g_free (file_as);
        }

        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(editor->visible_addrs_checkbutton), !show_addresses);

        e_contact_list_model_remove_all (E_CONTACT_LIST_MODEL (editor->model));

        for (iter = email_list; iter; iter = iter->next) {
            EVCardAttribute *attr = iter->data;
            GList *p;
            EDestination *list_dest = e_destination_new ();
            char *contact_uid = NULL;
            char *email = NULL;
            char *name = NULL;
            int email_num = -1;
            gboolean html_pref = FALSE;

            for (p = e_vcard_attribute_get_params (attr); p; p = p->next) {
                EVCardAttributeParam *param = p->data;
                const char *param_name = e_vcard_attribute_param_get_name (param);
                if (!g_ascii_strcasecmp (param_name,
                             EVC_X_DEST_CONTACT_UID)) {
                    GList *v = e_vcard_attribute_param_get_values (param);
                    contact_uid = v ? v->data : NULL;
                }
                else if (!g_ascii_strcasecmp (param_name,
                                  EVC_X_DEST_EMAIL_NUM)) {
                    GList *v = e_vcard_attribute_param_get_values (param);
                    email_num = v ? atoi (v->data) : -1;
                }
                else if (!g_ascii_strcasecmp (param_name,
                                  EVC_X_DEST_NAME)) {
                    GList *v = e_vcard_attribute_param_get_values (param);
                    name = v ? v->data : NULL;
                }
                else if (!g_ascii_strcasecmp (param_name,
                                  EVC_X_DEST_EMAIL)) {
                    GList *v = e_vcard_attribute_param_get_values (param);
                    email = v ? v->data : NULL;
                }
                else if (!g_ascii_strcasecmp (param_name,
                                  EVC_X_DEST_HTML_MAIL)) {
                    GList *v = e_vcard_attribute_param_get_values (param);
                    html_pref = v ? !g_ascii_strcasecmp (v->data, "true") : FALSE;
                }
            }

            if (contact_uid) e_destination_set_contact_uid (list_dest, contact_uid, email_num);
            if (name) e_destination_set_name (list_dest, name);
            if (email) e_destination_set_email (list_dest, email);
            e_destination_set_html_mail_pref (list_dest, html_pref);
            
            e_contact_list_model_add_destination (E_CONTACT_LIST_MODEL (editor->model), list_dest);
        }

        g_list_foreach (email_list, (GFunc) e_vcard_attribute_free, NULL);
        g_list_free (email_list);

        photo = e_contact_get (editor->contact, E_CONTACT_LOGO);
        if (photo) {
            e_image_chooser_set_image_data (E_IMAGE_CHOOSER (editor->list_image), photo->data, photo->length);
            e_contact_photo_free (photo);
        }
    }
    
    if (editor->book) {
        ESource   *source;

        source = e_book_get_source (editor->book);      
        e_source_option_menu_select (E_SOURCE_OPTION_MENU (editor->source_menu), source);
        gtk_widget_set_sensitive (editor->source_menu, editor->is_new_list);
        gtk_widget_set_sensitive (glade_xml_get_widget (editor->gui, "source-label"), editor->is_new_list);
    }
}