aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/addressbook-config.c
blob: d7f8d43f1b5a24d680ace4f6ca9763c6887e8bf6 (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
/* -*- 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-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"

typedef struct _AddressbookSourceDialog AddressbookSourceDialog;
typedef struct _AddressbookSourcePageItem  AddressbookSourcePageItem;

struct _AddressbookSourceDialog {
    GladeXML *gui;

    GtkWidget *dialog;

    GtkWidget *basic_notebook;
    GtkWidget *advanced_notebook;

    GtkWidget *name;
    GtkWidget *host;

    GtkWidget *port;
    GtkWidget *rootdn;
    GtkWidget *scope_optionmenu;
    GtkWidget *auth_checkbutton;

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

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

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

    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 (complete) {
            data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (dialog->host), 0, -1);
            if (!data || !*data)
                complete = FALSE;
            g_free (data);
        }

        if (complete) {
            data = e_utf8_gtk_editable_get_chars (GTK_EDITABLE (dialog->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);
}

typedef struct {
    GtkWidget *notebook;
    int page_num;
} FocusHelpClosure;

static void
focus_help (GtkWidget *w, GdkEventFocus *event, FocusHelpClosure *closure)
{
    gtk_notebook_set_page (GTK_NOTEBOOK(closure->notebook), closure->page_num);
}

static void
add_focus_handler (GtkWidget *widget, GtkWidget *notebook, int page_num)
{
    FocusHelpClosure *focus_closure = g_new0 (FocusHelpClosure, 1);
    focus_closure->notebook = notebook;
    focus_closure->page_num = page_num;

    gtk_signal_connect_full (GTK_OBJECT (widget),
                 "focus_in_event" /* XXX */,
                 (GtkSignalFunc) focus_help, NULL,
                 focus_closure,
                 (GtkDestroyNotify) g_free,
                 FALSE, FALSE);
}

static void
addressbook_source_dialog_set_source (AddressbookSourceDialog *dialog, AddressbookSource *source)
{
    e_utf8_gtk_entry_set_text (GTK_ENTRY (dialog->name), source ? source->name : "");
    e_utf8_gtk_entry_set_text (GTK_ENTRY (dialog->host), source ? source->host : "");
    e_utf8_gtk_entry_set_text (GTK_ENTRY (dialog->port), source ? source->port : "389");
    e_utf8_gtk_entry_set_text (GTK_ENTRY (dialog->rootdn), source ? source->rootdn : "");

    gtk_option_menu_set_history (GTK_OPTION_MENU(dialog->scope_optionmenu), source ? source->scope : 0);
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(dialog->auth_checkbutton), source && source->auth == ADDRESSBOOK_LDAP_AUTH_SIMPLE);
}

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

    source->name   = e_utf8_gtk_entry_get_text (GTK_ENTRY (dialog->name));
    source->host   = e_utf8_gtk_entry_get_text (GTK_ENTRY (dialog->host));
    source->port   = e_utf8_gtk_entry_get_text (GTK_ENTRY (dialog->port));
    source->rootdn = e_utf8_gtk_entry_get_text (GTK_ENTRY (dialog->rootdn));
    source->auth   = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->auth_checkbutton)) ? ADDRESSBOOK_LDAP_AUTH_SIMPLE : ADDRESSBOOK_LDAP_AUTH_NONE;

    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 AddressbookSourceDialog*
addressbook_source_dialog (GladeXML *gui, AddressbookSource *source, GtkWidget *parent)
{
    AddressbookSourceDialog *dialog = g_new0 (AddressbookSourceDialog, 1);

    dialog->gui = gui;

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

    if (source)
        gtk_window_set_title (GTK_WINDOW (dialog->dialog), _("Edit Addressbook"));

    gtk_window_set_modal (GTK_WINDOW (dialog->dialog), TRUE);
    gtk_window_set_policy (GTK_WINDOW (dialog->dialog), 
                   FALSE, TRUE, FALSE);

    gnome_dialog_set_parent (GNOME_DIALOG (dialog->dialog),
                 GTK_WINDOW (parent));

    dialog->basic_notebook = glade_xml_get_widget (gui, "basic-notebook");
    dialog->advanced_notebook = glade_xml_get_widget (gui, "advanced-notebook");

    /* BASIC STUFF */
    dialog->name = glade_xml_get_widget (gui, "account-name-entry");
    gtk_signal_connect (GTK_OBJECT (dialog->name), "changed",
                GTK_SIGNAL_FUNC (addressbook_source_edit_changed), dialog);
    add_focus_handler (dialog->name, dialog->basic_notebook, 0);

    dialog->host = glade_xml_get_widget (gui, "server-name-entry");
    gtk_signal_connect (GTK_OBJECT (dialog->host), "changed",
                GTK_SIGNAL_FUNC (addressbook_source_edit_changed), dialog);
    add_focus_handler (dialog->host, dialog->basic_notebook, 1);

    dialog->auth_checkbutton = glade_xml_get_widget (gui, "auth-checkbutton");
    add_focus_handler (dialog->auth_checkbutton, dialog->basic_notebook, 2);
    gtk_signal_connect (GTK_OBJECT (dialog->auth_checkbutton), "toggled",
                GTK_SIGNAL_FUNC (addressbook_source_edit_changed), dialog);

    /* ADVANCED STUFF */
    dialog->port = glade_xml_get_widget (gui, "port-entry");
    gtk_signal_connect (GTK_OBJECT (dialog->port), "changed",
                GTK_SIGNAL_FUNC (addressbook_source_edit_changed), dialog);
    add_focus_handler (dialog->port, dialog->advanced_notebook, 0);

    dialog->rootdn = glade_xml_get_widget (gui, "rootdn-entry");
    gtk_signal_connect (GTK_OBJECT (dialog->rootdn), "changed",
                GTK_SIGNAL_FUNC (addressbook_source_edit_changed), dialog);
    add_focus_handler (dialog->rootdn, dialog->advanced_notebook, 1);

    dialog->scope_optionmenu = glade_xml_get_widget (gui, "scope-optionmenu");
    add_focus_handler (dialog->scope_optionmenu, dialog->advanced_notebook, 2);
    /* XXX changed */

    /* fill in source info if there is some */
    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_with_gui (GladeXML *gui, AddressbookSource *source, GtkWidget *parent)
{
    AddressbookSourceDialog* dialog;

    dialog = addressbook_source_dialog (gui, source, parent);

    gnome_dialog_close_hides (GNOME_DIALOG(dialog->dialog), TRUE);

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

    return dialog;
}

void
addressbook_create_new_source (char *new_source, GtkWidget *parent)
{
    AddressbookSourceDialog *dialog;
    GladeXML *gui;

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

    dialog = addressbook_source_dialog (gui, NULL, parent);

    e_utf8_gtk_entry_set_text (GTK_ENTRY (dialog->name), new_source);

    gnome_dialog_close_hides (GNOME_DIALOG(dialog->dialog), TRUE);

    dialog->id = gnome_dialog_run_and_close (GNOME_DIALOG (dialog->dialog));
    
    gtk_object_unref (GTK_OBJECT (dialog->gui));

    if (dialog->id == 0) {
        /* Ok was clicked */
        addressbook_storage_add_source (addressbook_source_copy(dialog->source));
        addressbook_storage_write_sources();
    }
}



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_with_gui (dialog->gui, 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->host;

        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_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 0, TRUE);
        gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 1, TRUE);
        update_sensitivity (dialog);
    }
}

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_with_gui (dialog->gui, 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->host);
        gtk_clist_set_row_data (GTK_CLIST (dialog->clistSources), dialog->source_row, source);
        gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 0, TRUE);
        gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 1, TRUE);
        update_sensitivity (dialog);
    }
}

static void
delete_source_clicked (GtkWidget *widget, AddressbookDialog *dialog)
{
    gtk_clist_remove (GTK_CLIST (dialog->clistSources), dialog->source_row);
    dialog->source_row = -1;
    gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 0, TRUE);
    gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 1, TRUE);
    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_close (GtkWidget *w, AddressbookDialog *dialog)
{
    gtk_widget_destroy (dialog->dialog);
    gtk_object_unref (GTK_OBJECT (dialog->gui));
    g_free (dialog);
}

static void
addressbook_dialog_apply (GtkWidget *w, AddressbookDialog *dialog)
{
    int i;

    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));
    }

    addressbook_storage_write_sources();

    gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 0, FALSE);
    gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 1, FALSE);
}

static void
addressbook_dialog_ok (GtkWidget *w, AddressbookDialog *dialog)
{
    addressbook_dialog_apply(w, dialog);
    addressbook_dialog_close(w, 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, "addressbook_sources");

    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);

    gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 0, FALSE);
    gnome_dialog_set_sensitive (GNOME_DIALOG(dialog->dialog), 1, FALSE);
    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->host;

        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);

    gnome_dialog_button_connect (GNOME_DIALOG(dialog->dialog),
                     0 /* OK */, addressbook_dialog_ok, dialog);

    gnome_dialog_button_connect (GNOME_DIALOG(dialog->dialog),
                     1 /* APPLY */, addressbook_dialog_apply, dialog);

    gnome_dialog_button_connect (GNOME_DIALOG(dialog->dialog),
                     2 /* CLOSE */, addressbook_dialog_close, dialog);

    gnome_dialog_run (GNOME_DIALOG(dialog->dialog));
}