aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/addressbook-config.c
blob: d23fb1ced2abb439f244b948a83a261f80776c7d (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#include <config.h>

#include <glib.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-propertybox.h>
#include <libgnomeui/gnome-stock.h>
#include <glade/glade.h>
#include <gal/widgets/e-unicode.h>
#include <gtkhtml/gtkhtml.h>
#include <e-util/e-html-utils.h>
#include "addressbook-config.h"
#include "addressbook-storage.h"

/* #define INCLUDE_FILE_SOURCE */

typedef struct _AddressbookSourceDialog AddressbookSourceDialog;
typedef struct _AddressbookSourcePageItem  AddressbookSourcePageItem;
typedef struct _LDAPAuthPageItem LDAPAuthPageItem;

struct _AddressbookSourceDialog {
    GtkWidget *html;
    GtkWidget *dialog;
    GtkWidget *vbox;
    GtkWidget *name;
    GtkWidget *description;
    GtkWidget *source_option;
    GtkWidget *notebook;

    GList *source_pages;
    AddressbookSourcePageItem *source_page;

    gint id; /* button we closed the dialog with */

    AddressbookSource *source; /* our result if the Ok button was clicked */
};

struct _AddressbookSourcePageItem {
    gint pnum;

    AddressbookSourceType source_type;

    AddressbookSourceDialog *dialog;

    GtkWidget *item;
    GtkWidget *vbox;

    /* file: addressbook's */
    GtkWidget *path;
    GtkWidget *creat;

    /* ldap: addressbook's */
    GtkWidget *host;
    GtkWidget *port;
    GtkWidget *rootdn;
    GtkWidget *scope_optionmenu;
    GtkWidget *auth_optionmenu;
    GtkWidget *auth_notebook;

    GList *auths;
    LDAPAuthPageItem *auth;
};

struct _LDAPAuthPageItem {
    gint pnum;

    AddressbookLDAPAuthType auth_type;

    AddressbookSourceDialog *dialog;
    AddressbookSourcePageItem *page;

    GtkWidget *item;
    GtkWidget *vbox;

    /* simple (password) auth */
    GtkWidget *binddn;
    GtkWidget *remember_passwd;
};

static void
html_size_req (GtkWidget *widget, GtkRequisition *requisition)
{
     requisition->height = GTK_LAYOUT (widget)->height;
}

/* Returns a GtkHTML which is already inside a GtkScrolledWindow. If
 * @white is TRUE, the GtkScrolledWindow will be inside a GtkFrame.
 */
static GtkWidget *
html_new (gboolean white)
{
    GtkWidget *html, *scrolled, *frame;
    GtkStyle *style;
    
    html = gtk_html_new ();
    GTK_LAYOUT (html)->height = 0;
    gtk_signal_connect (GTK_OBJECT (html), "size_request",
                GTK_SIGNAL_FUNC (html_size_req), NULL);
    gtk_html_set_editable (GTK_HTML (html), FALSE);
    style = gtk_rc_get_style (html);
    if (style) {
        gtk_html_set_default_background_color (GTK_HTML (html),
                               white ? &style->white :
                               &style->bg[0]);
    }
    gtk_widget_set_sensitive (html, FALSE);
    scrolled = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
                    GTK_POLICY_NEVER,
                    GTK_POLICY_NEVER);
    gtk_container_add (GTK_CONTAINER (scrolled), html);
    
    if (white) {
        frame = gtk_frame_new (NULL);
        gtk_frame_set_shadow_type (GTK_FRAME (frame),
                       GTK_SHADOW_ETCHED_IN);
        gtk_container_add (GTK_CONTAINER (frame), scrolled);
        gtk_widget_show_all (frame);
    } else
        gtk_widget_show_all (scrolled);
    
    return html;
}

static void
put_html (GtkHTML *html, char *text)
{
    GtkHTMLStream *handle;
    
    text = e_text_to_html (text, E_TEXT_TO_HTML_CONVERT_NL);
    handle = gtk_html_begin (html);
    gtk_html_write (html, handle, "<HTML><BODY>", 12);
    gtk_html_write (html, handle, text, strlen (text));
    gtk_html_write (html, handle, "</BODY></HTML>", 14);
    g_free (text);
    gtk_html_end (html, handle, GTK_HTML_STREAM_OK);
}

#ifdef INCLUDE_FILE_SOURCE
static const char *
addressbook_config_source_label (AddressbookSourceType type)
{
    switch (type) {
    case ADDRESSBOOK_SOURCE_LDAP:
        return _("LDAP Server");
    case ADDRESSBOOK_SOURCE_FILE:
        return _("File");
    default:
        g_assert(0);
        return _("Unknown addressbook type");
    }
}
#endif

static const char *
addressbook_config_auth_label (AddressbookLDAPAuthType type)
{
    switch (type) {
    case ADDRESSBOOK_LDAP_AUTH_NONE:
        return _("None (anonymous mode)");
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE:
        return _("Password");
    default:
        g_assert(0);
        return _("Unknown auth type");
    }
}

static const char *
addressbook_config_scope_label (AddressbookLDAPScopeType scope)
{
    switch (scope) {
    case ADDRESSBOOK_LDAP_SCOPE_BASE:
        return _("Base");
    case ADDRESSBOOK_LDAP_SCOPE_ONELEVEL:
        return _("One");
    case ADDRESSBOOK_LDAP_SCOPE_SUBTREE:
        return _("Subtree");
    default:
        g_assert (0);
        return _("Unknown scope type");
    }
}

static void
addressbook_source_edit_changed (GtkWidget *item, AddressbookSourceDialog *dialog)
{
    char *data;
    gboolean complete = TRUE;
    AddressbookSourcePageItem *source_page = dialog->source_page;

    if (source_page == NULL)
        complete = FALSE;

    if (complete) {
        data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (dialog->name), 0, -1);
        if (!data || !*data)
            complete = FALSE;
        g_free (data);
    }

    if (complete) {
        if (source_page->source_type == ADDRESSBOOK_SOURCE_FILE) {
            if (complete) {
                data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (source_page->path), 0, -1);
                if (!data || !*data)
                    complete = FALSE;
                g_free (data);
            }
        }
        else {
            if (complete) {
                data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (source_page->host), 0, -1);
                if (!data || !*data)
                    complete = FALSE;
                g_free (data);
            }

            if (complete) {
                data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (source_page->port), 0, -1);
                if (!data || !*data)
                    complete = FALSE;
                /* XXX more validation on port here */
                g_free (data);
            }

            if (complete) {
                LDAPAuthPageItem *auth_page = source_page->auth;

                if (auth_page->auth_type == ADDRESSBOOK_LDAP_AUTH_SIMPLE) {
                    data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (auth_page->binddn), 0, -1);
                    if (!data || !*data)
                        complete = FALSE;
                    g_free (data);
                }
                data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (source_page->port), 0, -1);
                if (!data || !*data)
                    complete = FALSE;
                /* XXX more validation on port here */
                g_free (data);
            }
        }
    }

    gnome_dialog_set_sensitive (GNOME_DIALOG (dialog->dialog), 0, complete);
}

static void
source_type_menuitem_activate (GtkWidget *item, gpointer data)
{
    AddressbookSourcePageItem *sitem = data;

    gtk_notebook_set_page (GTK_NOTEBOOK(sitem->dialog->notebook), sitem->pnum);
    sitem->dialog->source_page = sitem;

    addressbook_source_edit_changed (item, sitem->dialog);  
}

typedef struct {
    AddressbookSourceDialog *dialog;
    char *help_text;
} FocusHelpClosure;

static void
free_focus_help_closure (gpointer data)
{
    FocusHelpClosure *closure = data;
    g_free(closure->help_text);
    g_free(closure);
}

static gint
focus_help (GtkWidget *widget, GdkEventFocus *event, FocusHelpClosure *closure)
{
    put_html (GTK_HTML(closure->dialog->html), closure->help_text);
    return FALSE;
}

static GtkWidget *
table_add_elem (AddressbookSourceDialog *dialog, GtkWidget *table, 
        int row,
        const char *label_text,
        char *help_text)
{
    GtkWidget *label, *entry;
    FocusHelpClosure *focus_closure;

    label = gtk_label_new (label_text);
    gtk_table_attach (GTK_TABLE (table), label, 0, 1, 
              row, row + 1, GTK_FILL, 0, 0, 0);
    gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);

    entry = gtk_entry_new ();
    gtk_table_attach (GTK_TABLE (table), entry, 1, 3, row, row + 1,
              GTK_EXPAND | GTK_FILL, 0, 0, 0);

    gtk_signal_connect (GTK_OBJECT (entry), "changed",
                GTK_SIGNAL_FUNC (addressbook_source_edit_changed), dialog);

    focus_closure = g_new0 (FocusHelpClosure, 1);
    focus_closure->dialog = dialog;
    focus_closure->help_text = g_strdup(help_text);

    gtk_signal_connect_full (GTK_OBJECT (entry),
                 "focus_in_event" /* XXX */,
                 (GtkSignalFunc) focus_help, NULL,
                 focus_closure,
                 (GtkDestroyNotify) free_focus_help_closure,
                 FALSE, FALSE);
    return entry;
}

static void
ldap_auth_type_menuitem_activate (GtkWidget *item, gpointer data)
{
    LDAPAuthPageItem *auth_item = data;

    gtk_notebook_set_page (GTK_NOTEBOOK(auth_item->page->auth_notebook), auth_item->pnum);

    auth_item->page->auth = auth_item;

    addressbook_source_edit_changed (item, auth_item->dialog);  
}

static LDAPAuthPageItem *
addressbook_ldap_auth_item_new (AddressbookSourceDialog *dialog,
                AddressbookSourcePageItem *page,
                AddressbookLDAPAuthType type)
{
    LDAPAuthPageItem *item = g_new0 (LDAPAuthPageItem, 1);
    GtkWidget *table = NULL;
    int row = 0;

    item->pnum = type;
    item->auth_type = type;
    item->dialog = dialog;
    item->page = page;
    item->vbox = gtk_vbox_new (FALSE, 0);

    switch (type) {
    case ADDRESSBOOK_LDAP_AUTH_NONE:
        break;
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE:
        table = gtk_table_new (2, 2, FALSE);
        item->binddn = table_add_elem (dialog, table, row++,
                           _("Bind DN:"),
                           _("FIXME Bind DN Help text here"));

        item->remember_passwd = gtk_check_button_new_with_label (_("Remember this password"));

        gtk_table_attach (GTK_TABLE (table), item->remember_passwd, 1, 2,
                  row, row + 1, GTK_FILL, 0, 0, 0);

        gtk_box_pack_start (GTK_BOX (item->vbox), table,
                    TRUE, TRUE, 0);
        break;
    default:
        g_assert (0);
        return item;
    }

    if (table) {
        gtk_table_set_row_spacings (GTK_TABLE (table), 2);
        gtk_table_set_col_spacings (GTK_TABLE (table), 10);
        gtk_container_set_border_width (GTK_CONTAINER (table), 8);
    }

    gtk_widget_show_all (item->vbox);

    return item;
}

static AddressbookSourcePageItem *
addressbook_source_item_new (AddressbookSourceDialog *dialog, AddressbookSourceType type)
{
    AddressbookSourcePageItem *item = g_new0 (AddressbookSourcePageItem, 1);
    GtkWidget *table = NULL;
#if 0
    GtkWidget *advanced_button;
#endif
    int row = 0;

    item->pnum = type;
    item->source_type = type;
    item->dialog = dialog;

    item->vbox = gtk_vbox_new (FALSE, 0);

    switch (type) {
    case ADDRESSBOOK_SOURCE_LDAP: {
        GtkWidget *label;
        GtkWidget *menu;
        int i;
        LDAPAuthPageItem *first_item = NULL;
        int position;

        table = gtk_table_new (6, 2, FALSE);

        item->host = table_add_elem (dialog, table, row++,
                         _("Host:"),
                         _("FIXME Host help text here."));
        item->rootdn = table_add_elem (dialog, table, row++,
                           _("Root DN:"),
                           _("FIXME Root DN help text here."));

        /* XXX BEGIN PUT IN ADVANCED DIALOG */
        item->port = table_add_elem (dialog, table, row++,
                         _("Port:"),
                         _("FIXME Port help text here."));
        gtk_editable_insert_text (GTK_EDITABLE (item->port), "389", 3, &position);

        item->scope_optionmenu = gtk_option_menu_new ();
        menu = gtk_menu_new ();

        for (i = 0; i < ADDRESSBOOK_LDAP_SCOPE_LAST; i ++) {
            GtkWidget *scope_item = gtk_menu_item_new_with_label (addressbook_config_scope_label (i));

            gtk_signal_connect (GTK_OBJECT (scope_item), "activate",
                        GTK_SIGNAL_FUNC (addressbook_source_edit_changed),
                        dialog);

            gtk_menu_append (GTK_MENU (menu), scope_item);
            gtk_widget_show (scope_item);
        }

        gtk_option_menu_set_menu (GTK_OPTION_MENU (item->scope_optionmenu), menu);
        //      ldap_auth_type_menuitem_activate (first_item->item, first_item);
        gtk_option_menu_set_history (GTK_OPTION_MENU(item->scope_optionmenu), 0);
        label = gtk_label_new (_("Search Scope:"));
        gtk_table_attach (GTK_TABLE (table), label, 0, 1,
                  row, row + 1, GTK_FILL, 0, 0, 0);
        gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);

        gtk_table_attach (GTK_TABLE (table), 
                  item->scope_optionmenu, 
                  1, 2, row, row + 1, 
                  GTK_EXPAND | GTK_FILL, 0,
                  0, 0);

        row++;
        /* XXX END PUT IN ADVANCED DIALOG */

        item->auth_optionmenu = gtk_option_menu_new ();
        menu = gtk_menu_new ();

        item->auth_notebook = gtk_notebook_new();
        gtk_notebook_set_show_tabs (GTK_NOTEBOOK (item->auth_notebook), FALSE);

        for (i = 0; i < ADDRESSBOOK_LDAP_AUTH_LAST; i++) {
            LDAPAuthPageItem *auth_item;

            auth_item = addressbook_ldap_auth_item_new (dialog, item, i);

            item->auths = g_list_append (item->auths, auth_item);

            if (!first_item)
                first_item = auth_item;

            auth_item->item = gtk_menu_item_new_with_label (addressbook_config_auth_label (i));

            gtk_notebook_append_page (GTK_NOTEBOOK (item->auth_notebook),
                          auth_item->vbox, NULL);

            gtk_signal_connect (GTK_OBJECT (auth_item->item), "activate",
                        GTK_SIGNAL_FUNC (ldap_auth_type_menuitem_activate),
                        auth_item);

            gtk_menu_append (GTK_MENU (menu), auth_item->item);
            gtk_widget_show (auth_item->item);
        }

        gtk_option_menu_set_menu (GTK_OPTION_MENU (item->auth_optionmenu), menu);
        ldap_auth_type_menuitem_activate (first_item->item, first_item);
        gtk_option_menu_set_history (GTK_OPTION_MENU(item->auth_optionmenu), 0);

        label = gtk_label_new (_("Authentication:"));
        gtk_table_attach (GTK_TABLE (table), label, 0, 1,
                  row, row + 1, GTK_FILL, 0, 0, 0);
        gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);

        gtk_table_attach (GTK_TABLE (table), 
                  item->auth_optionmenu, 
                  1, 2, row, row + 1, 
                  GTK_EXPAND | GTK_FILL, 0,
                  0, 0);

        row++;

        gtk_box_pack_start (GTK_BOX (item->vbox), table,
                    TRUE, FALSE, 0);

        gtk_box_pack_start (GTK_BOX (item->vbox), item->auth_notebook,
                    TRUE, TRUE, 0);

#if 0
        table = gtk_table_new (1, 4, FALSE);

        advanced_button = gtk_button_new_with_label (_("Advanced LDAP Options"));

        gtk_table_attach (GTK_TABLE (table), 
                  advanced_button,
                  3, 4, 0, 1,
                  0, 0,
                  0, 0);


        gtk_box_pack_start (GTK_BOX (item->vbox), table,
                    FALSE, FALSE, 0);

        gtk_signal_connect (GTK_OBJECT (advanced_button), "clicked",
                    GTK_SIGNAL_FUNC (), dialog);
#endif

        break;
    }
    case ADDRESSBOOK_SOURCE_FILE: {
        table = gtk_table_new (2, 2, FALSE);
        item->path = table_add_elem (dialog, table, row++,
                         _("Path:"),
                         _("FIXME Path Help text here"));

        gtk_box_pack_start (GTK_BOX (item->vbox), table,
                    TRUE, TRUE, 0);

        item->creat = gtk_check_button_new_with_label (_("Create path if it doesn't exist."));
        gtk_table_attach (GTK_TABLE (table), item->creat, 1, 2,
                  row, row + 1, GTK_FILL, 0, 0, 0);
        break;
    }
    default:
        g_assert(0);
        return item;
    }

    gtk_table_set_row_spacings (GTK_TABLE (table), 2);
    gtk_table_set_col_spacings (GTK_TABLE (table), 10);
    gtk_container_set_border_width (GTK_CONTAINER (table), 8);

    gtk_widget_show_all (item->vbox);

    return item;
}

static void
addressbook_source_dialog_set_source (AddressbookSourceDialog *dialog, AddressbookSource *source)
{
    AddressbookSourcePageItem *source_page;

    e_utf8_gtk_entry_set_text (GTK_ENTRY (dialog->name), source->name);
    e_utf8_gtk_entry_set_text (GTK_ENTRY (dialog->description), source->description);

    /* choose the correct server page */
    source_page = g_list_nth_data (dialog->source_pages, source->type);
    source_type_menuitem_activate (source_page->item, source_page);
    gtk_option_menu_set_history (GTK_OPTION_MENU(dialog->source_option), source->type);

    if (source->type == ADDRESSBOOK_SOURCE_LDAP) {
        LDAPAuthPageItem *auth_page;

        e_utf8_gtk_entry_set_text (GTK_ENTRY (source_page->host), source->ldap.host);
        e_utf8_gtk_entry_set_text (GTK_ENTRY (source_page->port), source->ldap.port);
        e_utf8_gtk_entry_set_text (GTK_ENTRY (source_page->rootdn), source->ldap.rootdn);

        gtk_option_menu_set_history (GTK_OPTION_MENU(source_page->scope_optionmenu), source->ldap.scope);

        auth_page = g_list_nth_data (source_page->auths, source->ldap.auth);
        ldap_auth_type_menuitem_activate (auth_page->item, auth_page);
        gtk_option_menu_set_history (GTK_OPTION_MENU(source_page->auth_optionmenu), auth_page->auth_type);

        if (auth_page->auth_type == ADDRESSBOOK_LDAP_AUTH_SIMPLE) {
            e_utf8_gtk_entry_set_text (GTK_ENTRY (auth_page->binddn), source->ldap.binddn);
        }
    }
    else {
        e_utf8_gtk_entry_set_text (GTK_ENTRY (source_page->path), source->file.path);
    }
}

static AddressbookSource *
addressbook_source_dialog_get_source (AddressbookSourceDialog *dialog)
{
    AddressbookSource *source = g_new0 (AddressbookSource, 1);
    AddressbookSourcePageItem *source_page;

    source_page = dialog->source_page;

    source->name = e_utf8_gtk_entry_get_text (GTK_ENTRY (dialog->name));
    source->description = e_utf8_gtk_entry_get_text (GTK_ENTRY (dialog->description));
    source->type = source_page->source_type;

    if (source->type == ADDRESSBOOK_SOURCE_FILE) {
        source->file.path = e_utf8_gtk_entry_get_text (GTK_ENTRY (source_page->path));
    }
    else {
        LDAPAuthPageItem *auth_page;

        source->ldap.host   = e_utf8_gtk_entry_get_text (GTK_ENTRY (source_page->host));
        source->ldap.port   = e_utf8_gtk_entry_get_text (GTK_ENTRY (source_page->port));
        source->ldap.rootdn = e_utf8_gtk_entry_get_text (GTK_ENTRY (source_page->rootdn));

        auth_page = source_page->auth;

        source->ldap.auth = auth_page->auth_type;
        if (source->ldap.auth == ADDRESSBOOK_LDAP_AUTH_SIMPLE) {
            source->ldap.binddn = e_utf8_gtk_entry_get_text (GTK_ENTRY (auth_page->binddn));
            source->ldap.remember_passwd = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (auth_page->remember_passwd));
        }

        ldap_auth_type_menuitem_activate (auth_page->item, auth_page);
        gtk_option_menu_set_history (GTK_OPTION_MENU(source_page->auth_optionmenu), auth_page->auth_type);
    }

    addressbook_storage_init_source_uri (source);

    return source;
}

static void
addressbook_source_dialog_ok_clicked (GtkWidget *widget, AddressbookSourceDialog *dialog)
{
    dialog->source = addressbook_source_dialog_get_source (dialog);
}

static void
addressbook_source_dialog_destroy (AddressbookSourceDialog *dialog)
{
    GList *s;

    for (s = dialog->source_pages; s; s = s->next) {
        AddressbookSourcePageItem *source_item = s->data;

        g_list_foreach (source_item->auths, (GFunc)g_free, NULL);
        g_list_free (source_item->auths);
        g_free (source_item);
    }

    g_list_free (dialog->source_pages);

    if (dialog->source)
        addressbook_source_free (dialog->source);
    g_free (dialog);
}

static AddressbookSourceDialog*
addressbook_source_dialog (AddressbookSource *source, GtkWidget *parent)
{
    GtkWidget *table;
    AddressbookSourceDialog *dialog = g_new0 (AddressbookSourceDialog, 1);
    GtkWidget *vbox, *dialog_vbox;
#ifdef INCLUDE_FILE_SOURCE
    GtkWidget *menu;
#endif
    AddressbookSourcePageItem *first_item = NULL;
    GtkWidget *area;
    int i;
    int row = 0;

    if (source)
        dialog->dialog = gnome_dialog_new (_("Edit Addressbook"), NULL);
    else
        dialog->dialog = gnome_dialog_new (_("Add Addressbook"), NULL);

    gtk_window_set_modal (GTK_WINDOW (dialog->dialog), TRUE);
    gtk_window_set_policy (GTK_WINDOW (dialog->dialog), 
                   FALSE, TRUE, FALSE);
#if 0
    gtk_window_set_default_size (GTK_WINDOW (dialog->dialog), 300, 300);
#endif
    gnome_dialog_set_parent (GNOME_DIALOG (dialog->dialog),
                 GTK_WINDOW (parent));

    dialog->vbox = gtk_vbox_new (FALSE, 5);
    dialog_vbox = GNOME_DIALOG (dialog->dialog)->vbox;

    vbox = gtk_vbox_new (FALSE, 0);

    dialog->html = html_new (FALSE);
    put_html (GTK_HTML (dialog->html),
          _("Select the kind of addressbook you have, and enter "
            "the relevant information about it."));

    table = gtk_table_new (2, 2, FALSE);

    dialog->name = table_add_elem (dialog, table, row++,
                       _("Name:"),
                       _("FIXME Name help text here"));
    dialog->description = table_add_elem (dialog, table, row++,
                          _("Description:"),
                          _("FIXME Description help text here"));

    gtk_table_set_row_spacings (GTK_TABLE (table), 2);
    gtk_table_set_col_spacings (GTK_TABLE (table), 10);
    gtk_container_set_border_width (GTK_CONTAINER (table), 8);

    dialog->notebook = gtk_notebook_new();
    gtk_notebook_set_show_tabs (GTK_NOTEBOOK (dialog->notebook), FALSE);

#ifdef INCLUDE_FILE_SOURCE
    dialog->source_option = gtk_option_menu_new ();
    menu = gtk_menu_new ();
#endif

    for (i = ADDRESSBOOK_SOURCE_LDAP;
         i < ADDRESSBOOK_SOURCE_LAST;
         i ++) {
        AddressbookSourcePageItem *item;

        item = addressbook_source_item_new (dialog, i);

        dialog->source_pages = g_list_append (dialog->source_pages, item);

#ifdef INCLUDE_FILE_SOURCE
        item->item = gtk_menu_item_new_with_label (addressbook_config_source_label (i));
#endif
        
        if (!first_item)
            first_item = item;

        gtk_notebook_append_page (GTK_NOTEBOOK (dialog->notebook),
                      item->vbox, NULL);

        gtk_signal_connect (GTK_OBJECT (item->item), "activate",
                    GTK_SIGNAL_FUNC (source_type_menuitem_activate),
                    item);

#ifdef INCLUDE_FILE_SOURCE
        gtk_menu_append (GTK_MENU (menu), item->item);
#endif
        gtk_widget_show (item->item);
    }

#ifdef INCLUDE_FILE_SOURCE
    gtk_option_menu_set_menu (GTK_OPTION_MENU (dialog->source_option), menu);
#endif
    source_type_menuitem_activate (first_item->item, first_item);
#ifdef INCLUDE_FILE_SOURCE
    gtk_option_menu_set_history (GTK_OPTION_MENU(dialog->source_option), 0);
#endif

    gtk_box_pack_start (GTK_BOX (vbox), dialog->html->parent, 
                FALSE, TRUE, 0);

    gtk_box_pack_start (GTK_BOX (vbox), table,
                FALSE, FALSE, 0);

#ifdef INCLUDE_FILE_SOURCE
    gtk_box_pack_start (GTK_BOX (vbox), dialog->source_option,
                FALSE, FALSE, 0);
#endif

    gtk_box_pack_start (GTK_BOX (dialog->vbox), vbox, FALSE, TRUE, 0);

    gtk_box_pack_start (GTK_BOX (dialog->vbox), dialog->notebook, 
                TRUE, TRUE, 0);

    /* hook our ui into the gnome-dialog */
    gtk_box_pack_start (GTK_BOX (dialog_vbox), dialog->vbox, TRUE, TRUE, 0);

    gtk_widget_show_all (dialog->vbox);

    /* Buttons */
    area = GNOME_DIALOG (dialog->dialog)->action_area;
    gtk_widget_show (area);
    gtk_button_box_set_layout (GTK_BUTTON_BOX (area), GTK_BUTTONBOX_END);
    gtk_button_box_set_spacing (GTK_BUTTON_BOX (area), 8);

    gnome_dialog_append_button (GNOME_DIALOG (dialog->dialog), 
                    GNOME_STOCK_BUTTON_OK);
    gnome_dialog_append_button (GNOME_DIALOG (dialog->dialog), 
                    GNOME_STOCK_BUTTON_CANCEL); 

    gnome_dialog_set_default (GNOME_DIALOG (dialog->dialog), 0);

    /* fill in source info if there is some */
    if (source)
        addressbook_source_dialog_set_source (dialog, source);

    gnome_dialog_set_sensitive (GNOME_DIALOG (dialog->dialog), 0, FALSE);
    
    gnome_dialog_button_connect( GNOME_DIALOG (dialog->dialog), 0,
                     GTK_SIGNAL_FUNC (addressbook_source_dialog_ok_clicked),
                     dialog);

    return dialog;
}

static AddressbookSourceDialog *
addressbook_config_source (AddressbookSource *source, GtkWidget *parent)
{
    AddressbookSourceDialog* dialog = addressbook_source_dialog (source, parent);

    dialog->id = gnome_dialog_run_and_close (GNOME_DIALOG (dialog->dialog));

    return dialog;
}



typedef struct {
    GladeXML *gui;
    GNOME_Evolution_Shell shell;
    GtkWidget *dialog;
    GtkWidget *clistSources;
    GtkWidget *addSource;
    GtkWidget *editSource;
    GtkWidget *deleteSource;
    gint      source_row;
} AddressbookDialog;

static void
update_sensitivity (AddressbookDialog *dialog)
{
    gboolean sensitive = dialog->source_row != -1;

    gtk_widget_set_sensitive (dialog->editSource, sensitive);
    gtk_widget_set_sensitive (dialog->deleteSource, sensitive);
}

static void
add_source_clicked (GtkWidget *widget, AddressbookDialog *dialog)
{
    AddressbookSourceDialog *sdialog;

    sdialog = addressbook_config_source (NULL, dialog->dialog);
    if (sdialog->id == 0) {
        /* Ok was clicked */
        AddressbookSource *source = addressbook_source_copy(sdialog->source);
        gint row;
        gchar *text[2];

        text[0] = source->name;
        text[1] = source->uri;

        row = e_utf8_gtk_clist_append (GTK_CLIST(dialog->clistSources), text);
        gtk_clist_set_row_data_full (GTK_CLIST(dialog->clistSources), row, source, (GtkDestroyNotify) addressbook_source_free);
        gnome_property_box_changed (GNOME_PROPERTY_BOX (dialog->dialog));
        update_sensitivity (dialog);
    }

    addressbook_source_dialog_destroy (sdialog);
}

static void
edit_source_clicked (GtkWidget *widget, AddressbookDialog *dialog)
{
    AddressbookSource *source;
    AddressbookSourceDialog *sdialog;

    source = gtk_clist_get_row_data (GTK_CLIST (dialog->clistSources), dialog->source_row);

    sdialog = addressbook_config_source (source, dialog->dialog);
    if (sdialog->id == 0) {
        /* Ok was clicked */
        source = addressbook_source_copy(sdialog->source);

        e_utf8_gtk_clist_set_text (GTK_CLIST (dialog->clistSources), dialog->source_row, 0, source->name);
        e_utf8_gtk_clist_set_text (GTK_CLIST (dialog->clistSources), dialog->source_row, 1, source->uri);
        gtk_clist_set_row_data (GTK_CLIST (dialog->clistSources), dialog->source_row, source);
        gnome_property_box_changed (GNOME_PROPERTY_BOX (dialog->dialog));
        update_sensitivity (dialog);
    }

    addressbook_source_dialog_destroy (sdialog);
}

static void
delete_source_clicked (GtkWidget *widget, AddressbookDialog *dialog)
{
    gtk_clist_remove (GTK_CLIST (dialog->clistSources), dialog->source_row);
    dialog->source_row = -1;
    gnome_property_box_changed (GNOME_PROPERTY_BOX(dialog->dialog));
    update_sensitivity (dialog);
}

static void
sources_select_row (GtkWidget *widget, gint row, gint column,
            GdkEventButton *event, AddressbookDialog *dialog)
{
    dialog->source_row = row;

    update_sensitivity (dialog);
}

static void
addressbook_dialog_apply (GnomePropertyBox *property_box, gint page_num, AddressbookDialog *dialog)
{
    int i;

    if (page_num != -1)
        return;

    addressbook_storage_clear_sources();

    for (i = 0; i < GTK_CLIST(dialog->clistSources)->rows; i ++) {
        AddressbookSource *source = (AddressbookSource*)gtk_clist_get_row_data (GTK_CLIST (dialog->clistSources), i);
        addressbook_storage_add_source (addressbook_source_copy (source));
    }
}

static void
addressbook_dialog_close (GnomePropertyBox *property_box, AddressbookDialog *dialog)
{
    gtk_object_unref (GTK_OBJECT (dialog->gui));
    g_free (dialog);
}

void
addressbook_config (GNOME_Evolution_Shell shell)
{
    AddressbookDialog *dialog;
    GladeXML *gui;
    GtkWidget *clist;
    GList *l;

    dialog = g_new0 (AddressbookDialog, 1);

    dialog->source_row = -1;

    gui = glade_xml_new (EVOLUTION_GLADEDIR "/addressbook-config.glade", NULL);
    dialog->gui = gui;
    dialog->shell = shell;

    dialog->dialog = glade_xml_get_widget (gui, "dialog");

    clist = glade_xml_get_widget (gui, "clistSources");
    dialog->clistSources = clist;
    
    gtk_clist_column_titles_passive (GTK_CLIST (clist));
    gtk_clist_set_column_width (GTK_CLIST (clist), 0, 80);

    dialog->addSource = glade_xml_get_widget (gui, "addSource");
    gtk_signal_connect (GTK_OBJECT(dialog->addSource), "clicked",
                GTK_SIGNAL_FUNC (add_source_clicked),
                dialog);

    dialog->editSource = glade_xml_get_widget (gui, "editSource");
    gtk_signal_connect (GTK_OBJECT(dialog->editSource), "clicked",
                GTK_SIGNAL_FUNC (edit_source_clicked),
                dialog);

    dialog->deleteSource = glade_xml_get_widget (gui, "deleteSource");
    gtk_signal_connect (GTK_OBJECT(dialog->deleteSource), "clicked",
                GTK_SIGNAL_FUNC (delete_source_clicked),
                dialog);

    update_sensitivity (dialog);

    l = addressbook_storage_get_sources ();
    for (; l != NULL; l = l->next) {
        AddressbookSource *source;
        gint row;
        gchar *text[2];

        source = addressbook_source_copy ((AddressbookSource*)l->data);

        text[0] = source->name;
        text[1] = source->uri;

        row = e_utf8_gtk_clist_append (GTK_CLIST(clist), text);
        gtk_clist_set_row_data_full (GTK_CLIST(clist), row, source, (GtkDestroyNotify) addressbook_source_free);
    }

    gtk_signal_connect (GTK_OBJECT (clist), "select_row",
                GTK_SIGNAL_FUNC (sources_select_row),
                dialog);

    gtk_signal_connect (GTK_OBJECT (dialog->dialog), "apply",
                addressbook_dialog_apply, dialog);

    gtk_signal_connect (GTK_OBJECT (dialog->dialog), "destroy",
                addressbook_dialog_close, dialog);

    gtk_window_set_default_size (GTK_WINDOW (dialog->dialog), 300, 350);

    gtk_widget_show (dialog->dialog);
}