aboutsummaryrefslogtreecommitdiffstats
path: root/modules/addressbook/addressbook-config.c
blob: 901ee4353b88671f05349f3579188774818a4770 (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
/*
 * 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>
 *      Chris Lahey <clahey@ximian.com>
 *      Michael Zucchi <notzed@ximian.com>
 *      And no doubt others ...
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

/*#define STANDALONE*/

#include <config.h>

#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

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

#ifdef G_OS_WIN32
/* Include <windows.h> early and work around DATADIR lossage */
#define DATADIR crap_DATADIR
#include <windows.h>
#undef DATADIR
#endif

#include <glade/glade.h>

#include "addressbook.h"
#include "addressbook-config.h"

#include "e-util/e-error.h"
#include "e-util/e-util-private.h"

#include "addressbook/gui/widgets/eab-config.h"

#define d(x)

#ifdef HAVE_LDAP
#ifndef G_OS_WIN32
#include <ldap.h>
#ifndef SUNLDAP
#include <ldap_schema.h>
#endif
#else
#include <winldap.h>
#include "openldap-extract.h"
#endif
#endif

#define LDAP_PORT_STRING "389"
#define LDAPS_PORT_STRING "636"

#define GLADE_FILE_NAME "ldap-config.glade"

GtkWidget* supported_bases_create_table (gchar *name, gchar *string1, gchar *string2,
                     gint num1, gint num2);

/* default objectclasses */
#define TOP                  "top"
#define PERSON               "person"
#define ORGANIZATIONALPERSON "organizationalPerson"
#define INETORGPERSON        "inetOrgPerson"
#define EVOLUTIONPERSON      "evolutionPerson"
#define CALENTRY             "calEntry"


typedef struct _AddressbookSourceDialog AddressbookSourceDialog;

struct _AddressbookSourceDialog {
    GladeXML  *gui;

    EABConfig *config;  /* the config manager */

    GtkWidget *window;

    /* Source selection (assistant only) */
    ESourceList *source_list;
    GSList *menu_source_groups;

    /* ESource we're currently editing */
    ESource *source;
    /* The original source in edit mode.  Also used to flag when we are in edit mode. */
    ESource *original_source;

    /* Source group we're creating/editing a source in */
    ESourceGroup *source_group;

    /* info page fields */
    GtkWidget *host;
    GtkWidget *auth_combobox;
    AddressbookLDAPAuthType auth;
    GtkWidget *auth_principal;

    /* connecting page fields */
    GtkWidget *port_comboentry;
    GtkWidget *ssl_combobox;
    AddressbookLDAPSSLType ssl;

    /* searching page fields */
    GtkWidget *rootdn;
    AddressbookLDAPScopeType scope;
    GtkWidget *scope_combobox;
    GtkWidget *search_filter;
    GtkWidget *timeout_scale;
    GtkWidget *limit_spinbutton;
    GtkWidget *canbrowsecheck;

    /* display name page fields */
    GtkWidget *display_name;
};



#ifdef HAVE_LDAP

static const gchar *
ldap_unparse_auth (AddressbookLDAPAuthType auth_type)
{
    switch (auth_type) {
    case ADDRESSBOOK_LDAP_AUTH_NONE:
        return "none";
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE_EMAIL:
        return "ldap/simple-email";
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE_BINDDN:
        return "ldap/simple-binddn";
    default:
        g_return_val_if_reached ("none");
    }
}

static AddressbookLDAPAuthType
ldap_parse_auth (const gchar *auth)
{
    if (!auth)
        return ADDRESSBOOK_LDAP_AUTH_NONE;

    if (!strcmp (auth, "ldap/simple-email") || !strcmp (auth, "simple"))
        return ADDRESSBOOK_LDAP_AUTH_SIMPLE_EMAIL;
    else if (!strcmp (auth, "ldap/simple-binddn"))
        return ADDRESSBOOK_LDAP_AUTH_SIMPLE_BINDDN;
    else
        return ADDRESSBOOK_LDAP_AUTH_NONE;
}

static const gchar *
ldap_unparse_scope (AddressbookLDAPScopeType scope_type)
{
    switch (scope_type) {
    case ADDRESSBOOK_LDAP_SCOPE_BASE:
        return "base";
    case ADDRESSBOOK_LDAP_SCOPE_ONELEVEL:
        return "one";
    case ADDRESSBOOK_LDAP_SCOPE_SUBTREE:
        return "sub";
    default:
        g_return_val_if_reached ("");
    }
}

static const gchar *
ldap_unparse_ssl (AddressbookLDAPSSLType ssl_type)
{
    switch (ssl_type) {
    case ADDRESSBOOK_LDAP_SSL_NEVER:
        return "never";
    case ADDRESSBOOK_LDAP_SSL_WHENEVER_POSSIBLE:
        return "whenever_possible";
    case ADDRESSBOOK_LDAP_SSL_ALWAYS:
        return "always";
    default:
        g_return_val_if_reached ("");
    }
}

static AddressbookLDAPSSLType
ldap_parse_ssl (const gchar *ssl)
{
    if (!ssl)
        return ADDRESSBOOK_LDAP_SSL_WHENEVER_POSSIBLE; /* XXX good default? */

    if (!strcmp (ssl, "always"))
        return ADDRESSBOOK_LDAP_SSL_ALWAYS;
    else if (!strcmp (ssl, "never"))
        return ADDRESSBOOK_LDAP_SSL_NEVER;
    else
        return ADDRESSBOOK_LDAP_SSL_WHENEVER_POSSIBLE;
}

static const gchar *
ldap_get_ssl_tooltip (AddressbookLDAPSSLType ssl_type)
{
    switch (ssl_type) {
    case ADDRESSBOOK_LDAP_SSL_ALWAYS:
        return _("Selecting this option means that Evolution will only connect to your LDAP server if your LDAP server supports SSL.");
    case ADDRESSBOOK_LDAP_SSL_WHENEVER_POSSIBLE:
        return _("Selecting this option means that Evolution will only connect to your LDAP server if your LDAP server supports TLS.");
    case ADDRESSBOOK_LDAP_SSL_NEVER:
        return _("Selecting this option means that your server does not support either SSL or TLS. This means that your connection will be insecure, and that you will be vulnerable to security exploits.");
    }

    return NULL;
}

static gboolean
source_to_uri_parts (ESource *source, gchar **host, gchar **rootdn, AddressbookLDAPScopeType *scope, gchar **search_filter, gint *port)
{
    gchar       *uri;
    LDAPURLDesc *lud;
    gint         ldap_error;

    g_return_val_if_fail (source, FALSE);

    uri = e_source_get_uri (source);
    ldap_error = ldap_url_parse ((gchar *) uri, &lud);
    g_free (uri);

    if (ldap_error != LDAP_SUCCESS)
        return FALSE;

    if (host)
        *host = g_strdup (lud->lud_host ? lud->lud_host : "");
    if (rootdn)
        *rootdn = g_strdup (lud->lud_dn ? lud->lud_dn : "");
    if (port)
        *port = lud->lud_port ? lud->lud_port : LDAP_PORT;
    if (scope)
        *scope = lud->lud_scope == LDAP_SCOPE_BASE     ? ADDRESSBOOK_LDAP_SCOPE_BASE :
             lud->lud_scope == LDAP_SCOPE_ONELEVEL ? ADDRESSBOOK_LDAP_SCOPE_ONELEVEL :
             lud->lud_scope == LDAP_SCOPE_SUBTREE  ? ADDRESSBOOK_LDAP_SCOPE_SUBTREE :
             ADDRESSBOOK_LDAP_SCOPE_ONELEVEL;
    if (search_filter && lud->lud_filter)
        *search_filter = g_strdup (lud->lud_filter);

    ldap_free_urldesc (lud);
    return TRUE;
}

static gboolean
source_group_is_remote (ESourceGroup *group)
{
    return strncmp ("ldap:", e_source_group_peek_base_uri (group), 5) == 0;
}

/* ldap api foo */
static LDAP *
addressbook_ldap_init (GtkWidget *window, ESource *source)
{
    LDAP  *ldap;
    gchar *host;
    gint   port;
    gint ldap_error;
    gint protocol_version = LDAP_VERSION3;

    if (!source_to_uri_parts (source, &host, NULL, NULL, NULL, &port))
        return NULL;

    if (!(ldap = ldap_init (host, port))) {
        e_error_run ((GtkWindow *) window, "addressbook:ldap-init", NULL);
        goto done;
    }

    ldap_error = ldap_set_option (ldap, LDAP_OPT_PROTOCOL_VERSION, &protocol_version);
    if (LDAP_SUCCESS != ldap_error)
        g_warning ("failed to set protocol version to LDAPv3");

    /* XXX do TLS if it's configured in */

 done:
    g_free (host);
    return ldap;
}

static gint
addressbook_ldap_auth (GtkWidget *window, LDAP *ldap)
{
    gint ldap_error;

    /* XXX use auth info from source */
    ldap_error = ldap_simple_bind_s (ldap, NULL, NULL);
    if (LDAP_SUCCESS != ldap_error)
        e_error_run ((GtkWindow *) window, "addressbook:ldap-auth", NULL);

    return ldap_error;
}

static gint
addressbook_root_dse_query (AddressbookSourceDialog *dialog, LDAP *ldap,
                const gchar **attrs, LDAPMessage **resp)
{
    gint ldap_error;
    struct timeval timeout;

    timeout.tv_sec = (gint) gtk_adjustment_get_value (GTK_RANGE(dialog->timeout_scale)->adjustment);
    timeout.tv_usec = 0;

    ldap_error = ldap_search_ext_s (ldap,
                    LDAP_ROOT_DSE, LDAP_SCOPE_BASE,
                    "(objectclass=*)",
                    (gchar **) attrs, 0, NULL, NULL, &timeout, LDAP_NO_LIMIT, resp);
    if (LDAP_SUCCESS != ldap_error)
        e_error_run (GTK_WINDOW (dialog->window), "addressbook:ldap-search-base", NULL);

    return ldap_error;
}

/* searching page */
GtkWidget*
supported_bases_create_table (gchar *name, gchar *string1, gchar *string2, gint num1, gint num2)
{
    GtkWidget *table, *scrolled;
    GtkTreeSelection *selection;
    GtkCellRenderer *renderer;
    GtkListStore *model;

    scrolled = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled), GTK_SHADOW_IN);

    model = gtk_list_store_new (1, G_TYPE_STRING);
    table = gtk_tree_view_new_with_model ((GtkTreeModel *) model);
    g_object_unref (model);
    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_insert_column_with_attributes ((GtkTreeView *) table, -1, _("Base"), renderer, "text", 0, NULL);
    gtk_tree_view_set_headers_visible ((GtkTreeView *) table, FALSE);
    selection = gtk_tree_view_get_selection ((GtkTreeView *) table);
    gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);

    gtk_container_add (GTK_CONTAINER (scrolled), table);
    g_object_set_data((GObject *)scrolled, "table", table);

    return scrolled;
}

static gboolean
do_ldap_root_dse_query (AddressbookSourceDialog *sdialog, GtkListStore *model, ESource *source)
{
    LDAP *ldap;
    const gchar *attrs[2];
    gint ldap_error;
    gchar **values;
    LDAPMessage *resp;
    gint i;

    ldap = addressbook_ldap_init (sdialog->window, source);
    if (!ldap)
        return FALSE;

    if (LDAP_SUCCESS != addressbook_ldap_auth (sdialog->window, ldap))
        goto fail;

    attrs[0] = "namingContexts";
    attrs[1] = NULL;

    ldap_error = addressbook_root_dse_query (sdialog, ldap, attrs, &resp);

    if (ldap_error != LDAP_SUCCESS)
        goto fail;

    values = ldap_get_values (ldap, resp, "namingContexts");
    if (!values || values[0] == NULL || strlen (values[0]) == 0) {
        e_error_run (GTK_WINDOW (sdialog->window), "addressbook:ldap-search-base", NULL);
        goto fail;
    }

    for (i = 0; values[i]; i++) {
        GtkTreeIter iter;

        gtk_list_store_append (model, &iter);
        gtk_list_store_set (model, &iter, 0, values[i], -1);
    }

    ldap_value_free (values);
    ldap_unbind_s (ldap);
    return TRUE;

 fail:
    ldap_unbind_s (ldap);
    return FALSE;
}

static void
search_base_selection_model_changed (GtkTreeSelection *selection, GtkWidget *dialog)
{
    gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
                       GTK_RESPONSE_OK,
                       gtk_tree_selection_get_selected(selection, NULL, NULL));
}

static void
query_for_supported_bases (GtkWidget *button, AddressbookSourceDialog *sdialog)
{
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeView *table;
    GtkWidget *dialog;
    GtkWidget *supported_bases_table;
    GladeXML *gui;
    GtkTreeIter iter;
    gchar *gladefile;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR,
                      GLADE_FILE_NAME,
                      NULL);
    gui = glade_xml_new (gladefile, "supported-bases-dialog", NULL);
    g_free (gladefile);

    dialog = glade_xml_get_widget (gui, "supported-bases-dialog");

    gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (sdialog->window));
    gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);

    gtk_widget_ensure_style (dialog);
    gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), 0);
    gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), 12);

    supported_bases_table = glade_xml_get_widget (gui, "supported-bases-table");
    gtk_widget_show_all (supported_bases_table);

    table = g_object_get_data (G_OBJECT (supported_bases_table), "table");
    model = gtk_tree_view_get_model (table);
    selection = gtk_tree_view_get_selection (table);
    g_signal_connect (selection, "changed", G_CALLBACK (search_base_selection_model_changed), dialog);
    search_base_selection_model_changed (selection, dialog);

    if (do_ldap_root_dse_query (sdialog, GTK_LIST_STORE (model), sdialog->source)) {
        gtk_widget_show (dialog);

        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK
            && gtk_tree_selection_get_selected (selection, &model, &iter)) {
            gchar *dn;

            gtk_tree_model_get (model, &iter, 0, &dn, -1);
            gtk_entry_set_text((GtkEntry *)sdialog->rootdn, dn);
            g_free(dn);
        }
    }

    gtk_widget_destroy (dialog);
}

#endif /* HAVE_LDAP */

GtkWidget*
addressbook_config_create_new_source (GtkWidget *parent)
{
    return addressbook_config_edit_source(parent, NULL);
}

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

static void
eabc_type_changed(GtkComboBox *dropdown, AddressbookSourceDialog *sdialog)
{
    gint id = gtk_combo_box_get_active(dropdown);
    GtkTreeModel *model;
    GtkTreeIter iter;

    model = gtk_combo_box_get_model(dropdown);
    if (id == -1 || !gtk_tree_model_iter_nth_child(model, &iter, NULL, id))
        return;

    /* TODO: when we change the group type, we lose all of the pre-filled dialog info */

    gtk_tree_model_get(model, &iter, 1, &sdialog->source_group, -1);
    /* HACK: doesn't work if you don't do this */
    e_source_set_absolute_uri(sdialog->source, NULL);
    e_source_set_group(sdialog->source, sdialog->source_group);

    /* BIG HACK: We load the defaults for each type here.
       I guess plugins will have to use the do it in their factory callbacks */
    if (!strncmp(e_source_group_peek_base_uri(sdialog->source_group), "groupwise:", 10)) {
        GSList *l;
        ESource *source;
        gchar *tmp;

        l = e_source_group_peek_sources(sdialog->source_group);
        if (l && l->data ) {
            source = l->data;
            e_source_set_property(sdialog->source, "auth", e_source_get_property(source, "auth"));
            e_source_set_property(sdialog->source, "user", e_source_get_property(source, "user"));
            e_source_set_property(sdialog->source, "user_ssl", e_source_get_property(source, "use_ssl"));
        }

        e_source_set_property(sdialog->source, "auth-domain", "Groupwise");
        tmp = g_strconcat (";", e_source_peek_name(sdialog->source), NULL);
        e_source_set_relative_uri (sdialog->source, tmp);
        g_free (tmp);
#ifdef HAVE_LDAP
    } else if (!strncmp(e_source_group_peek_base_uri(sdialog->source_group), "ldap:", 5)) {
        gchar *tmp;

        tmp = g_strdup_printf ("%s:%s/%s?" /* trigraph prevention */ "?%s",
                       "", LDAP_PORT_STRING,
                       "",
                       "one");
        e_source_set_relative_uri (sdialog->source, tmp);
        g_free (tmp);
        e_source_set_property(sdialog->source, "timeout", "3");
        e_source_set_property(sdialog->source, "limit", "100");
#endif
    } else {
        e_source_set_relative_uri (sdialog->source, e_source_peek_uid (sdialog->source));
    }

    e_config_target_changed((EConfig *)sdialog->config, E_CONFIG_TARGET_CHANGED_REBUILD);
}

static GtkWidget *
eabc_general_type(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    GtkComboBox *dropdown;
    GtkCellRenderer *cell;
    GtkListStore *store;
    GtkTreeIter iter;
    GSList *l;
    GtkWidget *w, *label;
    gint i, row = 0;

    if (old)
        return old;

    w = gtk_hbox_new(FALSE, 6);
    label = gtk_label_new_with_mnemonic(_("_Type:"));
    gtk_box_pack_start((GtkBox *)w, label, FALSE, FALSE, 0);

    dropdown = (GtkComboBox *)gtk_combo_box_new();
    cell = gtk_cell_renderer_text_new();
    store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
    i = 0;
    for (l=sdialog->menu_source_groups;l;l=g_slist_next(l)) {
        ESourceGroup *group = l->data;

        gtk_list_store_append(store, &iter);
        gtk_list_store_set(store, &iter, 0, e_source_group_peek_name(group), 1, group, -1);
        if (e_source_peek_group(sdialog->source) == group)
            row = i;
        i++;
    }

    gtk_cell_layout_pack_start((GtkCellLayout *)dropdown, cell, TRUE);
    gtk_cell_layout_set_attributes((GtkCellLayout *)dropdown, cell, "text", 0, NULL);
    gtk_combo_box_set_model(dropdown, (GtkTreeModel *)store);
    gtk_combo_box_set_active(dropdown, -1);
    gtk_combo_box_set_active(dropdown, row);
    g_signal_connect(dropdown, "changed", G_CALLBACK(eabc_type_changed), sdialog);
    gtk_widget_show((GtkWidget *)dropdown);
    gtk_box_pack_start((GtkBox *)w, (GtkWidget *)dropdown, TRUE, TRUE, 0);
    gtk_label_set_mnemonic_widget((GtkLabel *)label, (GtkWidget *)dropdown);

    gtk_box_pack_start((GtkBox *)parent, (GtkWidget *)w, FALSE, FALSE, 0);

    gtk_widget_show_all(w);

    return (GtkWidget *)w;
}

static void
name_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    e_source_set_name (sdialog->source, gtk_entry_get_text (GTK_ENTRY (sdialog->display_name)));
}

static void
offline_status_changed_cb (GtkWidget *widget, AddressbookSourceDialog *sdialog)
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
        e_source_set_property (sdialog->source, "offline_sync", "1");
    else
        e_source_set_property (sdialog->source, "offline_sync", "0");

}

static GtkWidget *
eabc_general_name(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    const gchar *uri;
    GtkWidget *w;
    GladeXML *gui;
    gchar *gladefile;

    if (old)
        return old;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR,
                      GLADE_FILE_NAME,
                      NULL);
    gui = glade_xml_new (gladefile, item->label, NULL);
    g_free (gladefile);

    w = glade_xml_get_widget(gui, item->label);
    gtk_box_pack_start((GtkBox *)parent, w, FALSE, FALSE, 0);

    sdialog->display_name = glade_xml_get_widget (gui, "account-editor-display-name-entry");
    g_signal_connect(sdialog->display_name, "changed", G_CALLBACK(name_changed_cb), sdialog);
    gtk_entry_set_text((GtkEntry *)sdialog->display_name, e_source_peek_name(sdialog->source));

    /* Hardcoded: groupwise can't edit the name (or anything else) */
    if (sdialog->original_source) {
        uri = e_source_group_peek_base_uri (sdialog->source_group);
        if (uri && strncmp(uri, "groupwise:", 10) == 0) {
            gtk_widget_set_sensitive (GTK_WIDGET(sdialog->display_name), FALSE);
        }
    }

    g_object_unref(gui);

    return w;
}

static GtkWidget *
eabc_general_offline(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    GtkWidget *offline_setting;
    const gchar *offline_sync;
    gboolean is_local_book;

    is_local_book = g_str_has_prefix (e_source_group_peek_base_uri (sdialog->source_group), "file:");
    offline_sync =  e_source_get_property (sdialog->source, "offline_sync");
    if (old)
        return old;
    else {
        offline_setting = gtk_check_button_new_with_mnemonic (_("Copy _book content locally for offline operation"));
        gtk_widget_show (offline_setting);
        gtk_container_add (GTK_CONTAINER (parent), offline_setting);
        g_signal_connect (offline_setting, "toggled", G_CALLBACK (offline_status_changed_cb), sdialog);

    }
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (offline_setting), (offline_sync && g_str_equal (offline_sync, "1"))  ? TRUE : FALSE);
    if (is_local_book)
        gtk_widget_hide (offline_setting);
    return offline_setting;

}

#ifdef HAVE_LDAP
static gchar *
form_ldap_search_filter (GtkWidget *w)
{
    gchar *filter;
    const gchar *search_filter = gtk_entry_get_text ((GtkEntry *) w);

    /* this function can be used to format the search filter entered */
    if ((strlen (search_filter) !=0) && *search_filter != '(' && *(search_filter + (strlen (search_filter-1))) != ')')
        filter = g_strdup_printf ("(%s)", search_filter);
    else
        filter = g_strdup_printf ("%s", search_filter);

    return filter;
}

static void
url_changed(AddressbookSourceDialog *sdialog)
{
    gchar *str, *search_filter;

    search_filter = form_ldap_search_filter (sdialog->search_filter);
    str = g_strdup_printf ("%s:%s/%s?" /* trigraph prevention */ "?%s?%s",
                   gtk_entry_get_text (GTK_ENTRY (sdialog->host)),
                   gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (sdialog->port_comboentry)))),
                   gtk_entry_get_text (GTK_ENTRY (sdialog->rootdn)),
                   ldap_unparse_scope (sdialog->scope),
                   search_filter);
    e_source_set_relative_uri (sdialog->source, str);
    g_free (search_filter);
    g_free (str);
}

static void
host_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    url_changed(sdialog);
}

static void
port_entry_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    const gchar *port = gtk_entry_get_text((GtkEntry *)w);

    if (!strcmp (port, LDAPS_PORT_STRING)) {
        sdialog->ssl = ADDRESSBOOK_LDAP_SSL_ALWAYS;
        gtk_combo_box_set_active (GTK_COMBO_BOX (sdialog->ssl_combobox), sdialog->ssl);
        gtk_widget_set_sensitive (sdialog->ssl_combobox, FALSE);
    } else {
        gtk_widget_set_sensitive (sdialog->ssl_combobox, TRUE);
    }

    url_changed(sdialog);
}

static void
ssl_combobox_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    sdialog->ssl = gtk_combo_box_get_active (GTK_COMBO_BOX (w));
    e_source_set_property (sdialog->source, "ssl", ldap_unparse_ssl (sdialog->ssl));

    gtk_widget_set_tooltip_text (sdialog->ssl_combobox, ldap_get_ssl_tooltip (sdialog->ssl));
}

static GtkWidget *
eabc_general_host(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    const gchar *tmp;
    GtkWidget *w;
    gchar *uri, port[16];
    LDAPURLDesc *lud;
    GladeXML *gui;
    gchar *gladefile;

    if (!source_group_is_remote(sdialog->source_group))
        return NULL;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR,
                      GLADE_FILE_NAME,
                      NULL);
    gui = glade_xml_new (gladefile, item->label, NULL);
    g_free (gladefile);

    w = glade_xml_get_widget(gui, item->label);
    gtk_box_pack_start((GtkBox *)parent, w, FALSE, FALSE, 0);

    uri = e_source_get_uri(sdialog->source);
    if (ldap_url_parse(uri, &lud) != LDAP_SUCCESS)
        lud = NULL;
    g_free(uri);

    sdialog->host = glade_xml_get_widget (gui, "server-name-entry");
    gtk_entry_set_text((GtkEntry *)sdialog->host, lud && lud->lud_host ? lud->lud_host : "");
    g_signal_connect (sdialog->host, "changed", G_CALLBACK (host_changed_cb), sdialog);

    sdialog->port_comboentry = glade_xml_get_widget (gui, "port-comboentry");
    gtk_widget_set_has_tooltip (sdialog->port_comboentry, TRUE);
    gtk_widget_set_tooltip_text (sdialog->port_comboentry, _("This is the port on the LDAP server that Evolution will try to connect to. A list of standard ports has been provided. Ask your system administrator what port you should specify."));
    sprintf(port, "%u", lud && lud->lud_port? lud->lud_port : LDAP_PORT);
    gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (sdialog->port_comboentry))), port);
    g_signal_connect (gtk_bin_get_child (GTK_BIN (sdialog->port_comboentry)), "changed", G_CALLBACK (port_entry_changed_cb), sdialog);

    if (lud)
        ldap_free_urldesc (lud);

    sdialog->ssl_combobox = glade_xml_get_widget (gui, "ssl-combobox");
    gtk_widget_set_has_tooltip (sdialog->ssl_combobox, TRUE);
    tmp = e_source_get_property (sdialog->source, "ssl");
    sdialog->ssl = tmp ? ldap_parse_ssl (tmp) : ADDRESSBOOK_LDAP_SSL_WHENEVER_POSSIBLE;
    gtk_combo_box_set_active (GTK_COMBO_BOX (sdialog->ssl_combobox), sdialog->ssl);
    gtk_widget_set_tooltip_text (sdialog->ssl_combobox, ldap_get_ssl_tooltip (sdialog->ssl));
    gtk_widget_set_sensitive (sdialog->ssl_combobox, strcmp (port, LDAPS_PORT_STRING) != 0);
    g_signal_connect (sdialog->ssl_combobox, "changed", G_CALLBACK (ssl_combobox_changed_cb), sdialog);

    g_object_unref(gui);

    return w;
}

static void
auth_entry_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    const gchar *principal = gtk_entry_get_text((GtkEntry *)w);

    /* seems messy ... but the api is */
    switch (sdialog->auth) {
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE_BINDDN:
        e_source_set_property(sdialog->source, "email_addr", NULL);
        e_source_set_property(sdialog->source, "binddn", principal);
        break;
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE_EMAIL:
        e_source_set_property(sdialog->source, "binddn", NULL);
        e_source_set_property(sdialog->source, "email_addr", principal);
        break;
    case ADDRESSBOOK_LDAP_AUTH_NONE:
    default:
        e_source_set_property(sdialog->source, "email_addr", NULL);
        e_source_set_property(sdialog->source, "binddn", NULL);
        break;
    }
}

static void
auth_combobox_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    sdialog->auth = gtk_combo_box_get_active (GTK_COMBO_BOX (w));
    e_source_set_property (sdialog->source, "auth", ldap_unparse_auth (sdialog->auth));

    /* make sure the right property is set for the auth - ugh, funny api */
    auth_entry_changed_cb(sdialog->auth_principal, sdialog);
}

static GtkWidget *
eabc_general_auth(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    GtkWidget *w;
    const gchar *tmp;
    GladeXML *gui;
    gchar *gladefile;

    if (!source_group_is_remote(sdialog->source_group))
        return NULL;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR,
                      GLADE_FILE_NAME,
                      NULL);
    gui = glade_xml_new (gladefile, item->label, NULL);
    g_free (gladefile);

    w = glade_xml_get_widget(gui, item->label);
    gtk_box_pack_start((GtkBox *)parent, w, FALSE, FALSE, 0);

    sdialog->auth_combobox = glade_xml_get_widget (gui, "auth-combobox");
    gtk_widget_set_has_tooltip (sdialog->auth_combobox, TRUE);
    gtk_widget_set_tooltip_text (sdialog->auth_combobox, _("This is the method Evolution will use to authenticate you.  Note that setting this to \"Email Address\" requires anonymous access to your LDAP server."));
    tmp = e_source_get_property(sdialog->source, "auth");
    sdialog->auth = tmp ? ldap_parse_auth(tmp) : ADDRESSBOOK_LDAP_AUTH_NONE;
    gtk_combo_box_set_active (GTK_COMBO_BOX (sdialog->auth_combobox), sdialog->auth);
    g_signal_connect (sdialog->auth_combobox, "changed", G_CALLBACK(auth_combobox_changed_cb), sdialog);

    sdialog->auth_principal = glade_xml_get_widget (gui, "auth-entry");
    switch (sdialog->auth) {
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE_EMAIL:
        tmp = e_source_get_property(sdialog->source, "email_addr");
        break;
    case ADDRESSBOOK_LDAP_AUTH_SIMPLE_BINDDN:
        tmp = e_source_get_property(sdialog->source, "binddn");
        break;
    case ADDRESSBOOK_LDAP_AUTH_NONE:
    default:
        tmp = "";
        break;
    }
    gtk_entry_set_text((GtkEntry *)sdialog->auth_principal, tmp?tmp:"");
    g_signal_connect (sdialog->auth_principal, "changed", G_CALLBACK (auth_entry_changed_cb), sdialog);

    g_object_unref(gui);

    return w;
}

static void
rootdn_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    url_changed(sdialog);
}

static void
search_filter_changed_cb (GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    url_changed (sdialog);
}

static void
scope_combobox_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    sdialog->scope = gtk_combo_box_get_active (GTK_COMBO_BOX (w));
    url_changed(sdialog);
}

static GtkWidget *
eabc_details_search(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    GtkWidget *w;
    LDAPURLDesc *lud;
    gchar *uri;
    GladeXML *gui;
    gchar *gladefile;

    if (!source_group_is_remote(sdialog->source_group))
        return NULL;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR,
                      GLADE_FILE_NAME,
                      NULL);
    gui = glade_xml_new (gladefile, item->label, NULL);
    g_free (gladefile);

    w = glade_xml_get_widget(gui, item->label);
    gtk_box_pack_start((GtkBox *)parent, w, FALSE, FALSE, 0);

    uri = e_source_get_uri(sdialog->source);
    if (ldap_url_parse(uri, &lud) != LDAP_SUCCESS)
        lud = NULL;
    g_free(uri);

    sdialog->rootdn = glade_xml_get_widget (gui, "rootdn-entry");
    gtk_entry_set_text((GtkEntry *)sdialog->rootdn, lud && lud->lud_dn ? lud->lud_dn : "");
    g_signal_connect (sdialog->rootdn, "changed", G_CALLBACK (rootdn_changed_cb), sdialog);

    sdialog->scope_combobox = glade_xml_get_widget (gui, "scope-combobox");
    gtk_widget_set_has_tooltip (sdialog->scope_combobox, TRUE);
    gtk_widget_set_tooltip_text (sdialog->scope_combobox, _("The search scope defines how deep you would like the search to extend down the directory tree. A search scope of \"sub\" will include all entries below your search base. A search scope of \"one\" will only include the entries one level beneath your base."));
    if (lud) {
        switch (lud->lud_scope) {
        case LDAP_SCOPE_BASE:
            sdialog->scope = ADDRESSBOOK_LDAP_SCOPE_BASE;
            break;
        default:
        case LDAP_SCOPE_ONELEVEL:
            sdialog->scope = ADDRESSBOOK_LDAP_SCOPE_ONELEVEL;
            break;
        case LDAP_SCOPE_SUBTREE:
            sdialog->scope = ADDRESSBOOK_LDAP_SCOPE_SUBTREE;
            break;
        }
    }
    gtk_combo_box_set_active (GTK_COMBO_BOX (sdialog->scope_combobox), sdialog->scope);
    g_signal_connect (sdialog->scope_combobox, "changed", G_CALLBACK(scope_combobox_changed_cb), sdialog);

    sdialog->search_filter =  glade_xml_get_widget (gui, "search-filter-entry");
    gtk_entry_set_text((GtkEntry *)sdialog->search_filter, lud && lud->lud_filter ? lud->lud_filter : "");
    g_signal_connect (sdialog->search_filter, "changed",  G_CALLBACK (search_filter_changed_cb), sdialog);

    g_signal_connect (glade_xml_get_widget(gui, "rootdn-button"), "clicked",
              G_CALLBACK(query_for_supported_bases), sdialog);

    if (lud)
        ldap_free_urldesc (lud);

    g_object_unref(gui);

    return w;
}

static void
timeout_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    gchar *timeout;

    timeout = g_strdup_printf("%f", gtk_adjustment_get_value(((GtkRange *)sdialog->timeout_scale)->adjustment));
    e_source_set_property(sdialog->source, "timeout", timeout);
    g_free(timeout);
}

static void
limit_changed_cb(GtkWidget *w, AddressbookSourceDialog *sdialog)
{
    gchar limit[16];

    sprintf(limit, "%d", gtk_spin_button_get_value_as_int((GtkSpinButton *)sdialog->limit_spinbutton));
    e_source_set_property(sdialog->source, "limit", limit);
}

static void
canbrowse_toggled_cb (GtkWidget *toggle_button, ESource *source)
{
    if (!source || !toggle_button)
        return;

    e_source_set_property (source, "can-browse", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (toggle_button)) ? "1" : NULL);
}

static GtkWidget *
eabc_details_limit(EConfig *ec, EConfigItem *item, GtkWidget *parent, GtkWidget *old, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    GtkWidget *w;
    const gchar *tmp;
    GladeXML *gui;
    gchar *gladefile;

    if (!source_group_is_remote(sdialog->source_group))
        return NULL;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR,
                      GLADE_FILE_NAME,
                      NULL);
    gui = glade_xml_new (gladefile, item->label, NULL);
    g_free (gladefile);

    w = glade_xml_get_widget(gui, item->label);
    gtk_box_pack_start((GtkBox *)parent, w, FALSE, FALSE, 0);

    sdialog->timeout_scale = glade_xml_get_widget (gui, "timeout-scale");
    tmp = e_source_get_property(sdialog->source, "timeout");
    gtk_adjustment_set_value(((GtkRange *)sdialog->timeout_scale)->adjustment, tmp?g_strtod(tmp, NULL):3.0);
    g_signal_connect (GTK_RANGE(sdialog->timeout_scale)->adjustment, "value_changed", G_CALLBACK (timeout_changed_cb), sdialog);

    sdialog->limit_spinbutton = glade_xml_get_widget (gui, "download-limit-spinbutton");
    tmp = e_source_get_property(sdialog->source, "limit");
    gtk_spin_button_set_value((GtkSpinButton *)sdialog->limit_spinbutton, tmp?g_strtod(tmp, NULL):100.0);
    g_signal_connect (sdialog->limit_spinbutton, "value_changed", G_CALLBACK (limit_changed_cb), sdialog);

    sdialog->canbrowsecheck = glade_xml_get_widget (gui, "canbrowsecheck");
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (sdialog->canbrowsecheck), e_source_get_property (sdialog->source, "can-browse") && strcmp (e_source_get_property (sdialog->source, "can-browse"), "1") == 0);
    g_signal_connect (sdialog->canbrowsecheck, "toggled", G_CALLBACK (canbrowse_toggled_cb), sdialog->source);

    g_object_unref(gui);

    return w;
}
#endif

static EConfigItem eabc_items[] = {
    { E_CONFIG_BOOK, (gchar *) (gchar *) "", },
    { E_CONFIG_PAGE, (gchar *) "00.general", (gchar *) N_("General") },
    { E_CONFIG_SECTION, (gchar *) "00.general/10.display", (gchar *) N_("Address Book") },
    { E_CONFIG_ITEM, (gchar *) "00.general/10.display/10.name", (gchar *) "hbox122", eabc_general_name },
    { E_CONFIG_ITEM, (gchar *) "00.general/10.display/20.offline", NULL, eabc_general_offline },
#ifdef HAVE_LDAP
    { E_CONFIG_SECTION, (gchar *) "00.general/20.server", (gchar *) N_("Server Information") },
    { E_CONFIG_ITEM, (gchar *) "00.general/20.server/00.host", (gchar *) "table31", eabc_general_host },
    { E_CONFIG_SECTION, (gchar *) "00.general/30.auth", (gchar *) N_("Authentication") },
    { E_CONFIG_ITEM, (gchar *) "00.general/30.auth/00.auth", (gchar *) "table32", eabc_general_auth },

    { E_CONFIG_PAGE, (gchar *) "10.details", (gchar *) N_("Details") },
    { E_CONFIG_SECTION, (gchar *) "10.details/00.search", (gchar *) N_("Searching") },
    { E_CONFIG_ITEM, (gchar *) "10.details/00.search/00.search", (gchar *) "table33", eabc_details_search },
    { E_CONFIG_SECTION, (gchar *) "10.details/10.limit", (gchar *) N_("Downloading") },
    { E_CONFIG_ITEM, (gchar *) "10.details/10.limit/00.limit", (gchar *) "table34", eabc_details_limit },
#endif
    { 0 },
};

/* items needed for the 'new addressbook' window */
static EConfigItem eabc_new_items[] = {
    { E_CONFIG_ITEM, (gchar *) "00.general/10.display/00.type", NULL, eabc_general_type },
    { 0 },
};

static void
eabc_commit(EConfig *ec, GSList *items, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    xmlNodePtr xml;
#if d(!)0
    gchar *txt;
#endif
    if (sdialog->original_source) {
        d(printf("committing addressbook changes\n"));

        /* these api's kinda suck */
        xml = xmlNewNode(NULL, (const guchar *)"dummy");
        e_source_dump_to_xml_node(sdialog->source, xml);
        e_source_update_from_xml_node(sdialog->original_source, xml->children, NULL);
        xmlFreeNode(xml);
#if d(!)0
        txt = e_source_to_standalone_xml(sdialog->original_source);
        printf("source is now:\n%s\n", txt);
        g_free(txt);
#endif
    } else {
        d(printf("committing new source\n"));
        e_source_group_add_source(sdialog->source_group, sdialog->source, -1);
        e_source_list_sync(sdialog->source_list, NULL);
    }

#if d(!)0
    txt = e_source_to_standalone_xml(sdialog->source);
    printf("running source is now:\n%s\n", txt);
    g_free(txt);
#endif
}

static void
eabc_free(EConfig *ec, GSList *items, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;

    g_slist_free(items);

    g_object_unref(sdialog->source);
    if (sdialog->original_source)
        g_object_unref(sdialog->original_source);
    if (sdialog->source_list)
        g_object_unref(sdialog->source_list);
    g_slist_free(sdialog->menu_source_groups);

    g_object_unref(sdialog->gui);

    g_free(sdialog);
}

static gboolean
eabc_check_complete(EConfig *ec, const gchar *pageid, gpointer data)
{
    AddressbookSourceDialog *sdialog = data;
    gint valid = TRUE;
    const gchar *tmp;
    ESource *source;

    d(printf("check complete, pageid = '%s'\n", pageid?pageid:"<all>"));
    /* have name, and unique */
    tmp = e_source_peek_name(sdialog->source);
    valid = tmp && tmp[0] != 0
        && ((source = e_source_group_peek_source_by_name(sdialog->source_group, tmp)) == NULL
            || source == sdialog->original_source);

#ifdef HAVE_LDAP
    if (valid && source_group_is_remote(sdialog->source_group)) {
        gchar *uri = e_source_get_uri(sdialog->source);
        LDAPURLDesc *lud;

        /* check host and port set */
        if (ldap_url_parse(uri, &lud) == LDAP_SUCCESS) {
            valid = lud->lud_host != NULL
                && lud->lud_host[0] != 0
                && lud->lud_port != 0;
            ldap_free_urldesc (lud);
        } else
            valid = FALSE;
        g_free(uri);

        /* check auth name provided if auth set */
        if (valid && (tmp = e_source_get_property(sdialog->source, "auth"))) {
            switch (ldap_parse_auth(tmp)) {
            case ADDRESSBOOK_LDAP_AUTH_SIMPLE_EMAIL:
                tmp = e_source_get_property(sdialog->source, "email_addr");
                break;
            case ADDRESSBOOK_LDAP_AUTH_SIMPLE_BINDDN:
                tmp = e_source_get_property(sdialog->source, "binddn");
                break;
            default:
                tmp = "dummy";
                break;
            }
            valid = tmp && tmp[0];
        }

        /* check timeout isn't too short (why don't we just force it?) */
        if (valid) {
            tmp = e_source_get_property(sdialog->source, "timeout");
            valid = tmp && g_strtod(tmp, NULL) > 0.0;
        }
    }
#endif
    return valid;
}

/* debug only: */
#if d(!)0
static void
source_changed(ESource *source, AddressbookSourceDialog *sdialog)
{
    gchar *xml;

    xml = e_source_to_standalone_xml(source);
    printf("source changed:\n%s\n", xml);
    g_free(xml);
}
#endif

GtkWidget*
addressbook_config_edit_source (GtkWidget *parent, ESource *source)
{
    AddressbookSourceDialog *sdialog = g_new0 (AddressbookSourceDialog, 1);
    EABConfig *ec;
    gint i;
    GSList *items = NULL;
    EABConfigTargetSource *target;
    gchar *xml;
    gchar *gladefile;

    gladefile = g_build_filename (EVOLUTION_GLADEDIR,
                      GLADE_FILE_NAME,
                      NULL);
    sdialog->gui = glade_xml_new (gladefile, "account-editor-notebook", NULL);
    g_free (gladefile);

    if (source) {
        sdialog->original_source = source;
        g_object_ref(source);
        sdialog->source_group = e_source_peek_group (source);
        xml = e_source_to_standalone_xml(source);
        sdialog->source = e_source_new_from_standalone_xml(xml);
        g_free(xml);
    } else {
        GConfClient *gconf;
        GSList *l;

        sdialog->source = e_source_new("", "");
        gconf = gconf_client_get_default();
        sdialog->source_list = e_source_list_new_for_gconf(gconf, "/apps/evolution/addressbook/sources");
        l = e_source_list_peek_groups(sdialog->source_list);
        if (!l) {
            g_warning ("Address Book source groups are missing! Check your GConf setup.");
            g_object_unref (gconf);
            g_free (sdialog);
            return NULL;
        }

        sdialog->menu_source_groups = g_slist_copy(l);
#ifndef HAVE_LDAP
        for (;l;l = g_slist_next(l))
            if (!strncmp("ldap:", e_source_group_peek_base_uri(l->data), 5))
                sdialog->menu_source_groups = g_slist_remove (sdialog->menu_source_groups, l->data);
#endif
        sdialog->source_group = (ESourceGroup *)sdialog->menu_source_groups->data;
        for (i=0;eabc_new_items[i].path;i++)
            items = g_slist_prepend(items, &eabc_new_items[i]);
        g_object_unref(gconf);
    }

    /* HACK: doesn't work if you don't do this */
    e_source_set_group(sdialog->source, sdialog->source_group);

#if d(!)0
    xml = e_source_to_standalone_xml(sdialog->source);
    printf("but working standalone xml: %s\n", xml);
    g_free(xml);
    g_signal_connect(sdialog->source, "changed", source_changed, sdialog);
#endif

    sdialog->config = ec = eab_config_new(E_CONFIG_BOOK, "com.novell.evolution.addressbook.config.accountEditor");

    for (i=0;eabc_items[i].path;i++) {
        if (eabc_items[i].label)
            eabc_items[i].label = gettext(eabc_items[i].label);
        items = g_slist_prepend(items, &eabc_items[i]);
    }

    e_config_add_items((EConfig *)ec, items, eabc_commit, NULL, eabc_free, sdialog);
    e_config_add_page_check((EConfig *)ec, NULL, eabc_check_complete, sdialog);

    target = eab_config_target_new_source(ec, sdialog->source);
    e_config_set_target((EConfig *)ec, (EConfigTarget *)target);

    if (source)
        sdialog->window = e_config_create_window((EConfig *)ec, NULL, _("Address Book Properties"));
    else
        sdialog->window = e_config_create_window((EConfig *)ec, NULL, _("New Address Book"));

    /* forces initial validation */
    if (!sdialog->original_source)
        e_config_target_changed((EConfig *)ec, E_CONFIG_TARGET_CHANGED_STATE);

    return sdialog->window;
}