0.9.6claws85
[claws.git] / src / editldap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2003 Match Grun
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Edit LDAP address book data.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #ifdef USE_LDAP
29
30 #include "defs.h"
31
32 #include <glib.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtkwindow.h>
35 #include <gtk/gtksignal.h>
36 #include <gtk/gtklabel.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtktable.h>
39 #include <gtk/gtkbutton.h>
40
41 #include "intl.h"
42 #include "addressbook.h"
43 #include "prefs_common.h"
44 #include "addressitem.h"
45 #include "mgutils.h"
46 #include "ldapserver.h"
47 #include "ldapctrl.h"
48 #include "ldaputil.h"
49 #include "editldap_basedn.h"
50 #include "manage_window.h"
51 #include "gtkutils.h"
52
53 #define PAGE_BASIC      0
54 #define PAGE_SEARCH     1
55 #define PAGE_EXTENDED   2
56
57 #define ADDRESSBOOK_GUESS_LDAP_NAME     "MyServer"
58 #define ADDRESSBOOK_GUESS_LDAP_SERVER   "localhost"
59
60 #define LDAPEDIT_TABLE_ROWS     6
61 #define LDAPEDIT_TABLE_COLS     3
62
63 static struct _LDAPEdit {
64         GtkWidget *window;
65         GtkWidget *notebook;
66         GtkWidget *ok_btn;
67         GtkWidget *cancel_btn;
68         GtkWidget *statusbar;
69         gint status_cid;
70         GtkWidget *entry_name;
71         GtkWidget *entry_server;
72         GtkWidget *spinbtn_port;
73         GtkWidget *entry_baseDN;
74         GtkWidget *spinbtn_timeout;
75         GtkWidget *entry_bindDN;
76         GtkWidget *entry_bindPW;
77         GtkWidget *spinbtn_maxentry;
78         GtkWidget *entry_criteria;
79         GtkWidget *spinbtn_queryage;
80         GtkWidget *check_dynsearch;
81         GtkWidget *check_matchoption;
82 } ldapedit;
83
84 /**
85  * Parse out individual attribute names from criteria string.
86  * \param criteria Criteria string.
87  * \ctl   Control object.
88  */
89 static gboolean editldap_validate_criteria( gchar *criteria ) {
90         gchar *ptr;
91         gchar **splitStr;
92         gint i;
93         gboolean errorFlag;
94
95         errorFlag = TRUE;
96
97         /* Replace delimiters with spaces */
98         ptr = criteria;
99         while( *ptr ) {
100                 if( *ptr == ',' || *ptr == ';' || *ptr == '|' )
101                         *ptr = ' ';
102                 ptr++;
103         }
104
105         /* Parse string */
106         splitStr = g_strsplit( criteria, " ", 0 );
107         i = 0;
108         while( TRUE ) {
109                 if( splitStr[i] ) {
110                         if( *splitStr[i] ) {
111                                 errorFlag = FALSE;
112                                 break;
113                         }
114                 }
115                 else {
116                         break;
117                 }
118                 i++;
119         }
120         g_strfreev( splitStr );
121         return errorFlag;
122 }
123
124 /*
125 * Edit functions.
126 */
127 static void edit_ldap_status_show( gchar *msg ) {
128         if( ldapedit.statusbar != NULL ) {
129                 gtk_statusbar_pop( GTK_STATUSBAR(ldapedit.statusbar), ldapedit.status_cid );
130                 if( msg ) {
131                         gtk_statusbar_push( GTK_STATUSBAR(ldapedit.statusbar),
132                                 ldapedit.status_cid, msg );
133                 }
134         }
135 }
136
137 static gboolean edit_ldap_validate( void ) {
138         gchar *str;
139         gboolean errorFlag;
140         gint page;
141
142         errorFlag = FALSE;
143         str = gtk_editable_get_chars(
144                         GTK_EDITABLE(ldapedit.entry_name), 0, -1 );
145         if( *str == '\0' ) {
146                 page = PAGE_BASIC;
147                 gtk_widget_grab_focus( ldapedit.entry_name );
148                 edit_ldap_status_show( _( "A Name must be supplied." ) );
149                 errorFlag = TRUE;
150         }
151         g_free( str );
152
153         if( ! errorFlag ) {
154                 str = gtk_editable_get_chars(
155                                 GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
156                 if( *str == '\0' ) {
157                         page = PAGE_BASIC;
158                         gtk_widget_grab_focus( ldapedit.entry_server );
159                         edit_ldap_status_show(
160                                 _( "A Hostname must be supplied for the server." ) );
161                         errorFlag = TRUE;
162                 }
163                 g_free( str );
164         }
165
166         if( ! errorFlag ) {
167                 str = gtk_editable_get_chars(
168                                 GTK_EDITABLE(ldapedit.entry_criteria), 0, -1 );
169                 if( editldap_validate_criteria( str ) ) {
170                         page = PAGE_SEARCH;
171                         gtk_widget_grab_focus( ldapedit.entry_criteria );
172                         edit_ldap_status_show(
173                                 _( "At least one LDAP search attribute should be supplied." ) );
174                         errorFlag = TRUE;
175                 }
176                 g_free( str );
177         }
178
179         /* Switch to page with error */
180         if( errorFlag ) {
181                 gtk_notebook_set_page( GTK_NOTEBOOK(ldapedit.notebook), page );
182         }
183
184         return errorFlag;
185 }
186
187 static void edit_ldap_ok( GtkWidget *widget, gboolean *cancelled ) {
188         if( ! edit_ldap_validate() ) {
189                 *cancelled = FALSE;
190                 gtk_main_quit();
191         }
192 }
193
194 static void edit_ldap_cancel( GtkWidget *widget, gboolean *cancelled ) {
195         *cancelled = TRUE;
196         gtk_main_quit();
197 }
198
199 static gint edit_ldap_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
200         *cancelled = TRUE;
201         gtk_main_quit();
202         return TRUE;
203 }
204
205 static void edit_ldap_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
206         if (event && event->keyval == GDK_Escape) {
207                 *cancelled = TRUE;
208                 gtk_main_quit();
209         }
210 }
211
212 static void edit_ldap_server_check( void ) {
213         gchar *sHost, *sBind, *sPass;
214         gint iPort, iTime;
215         gchar *sMsg;
216         gchar *sBaseDN = NULL;
217         gint iBaseDN = 0;
218         gboolean flg;
219         GList *baseDN = NULL;
220
221         edit_ldap_status_show( "" );
222         flg = FALSE;
223         sHost = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
224         sBind = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
225         sPass = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
226         iPort = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
227         iTime = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
228         g_strchomp( sHost ); g_strchug( sHost );
229         g_strchomp( sBind ); g_strchug( sBind );
230         g_strchomp( sPass ); g_strchug( sPass );
231         if( *sHost != '\0' ) {
232                 /* Test connection to server */
233                 if( ldaputil_test_connect( sHost, iPort ) ) {
234                         /* Attempt to read base DN */
235                         baseDN = ldaputil_read_basedn( sHost, iPort, sBind, sPass, iTime );
236                         if( baseDN ) {
237                                 GList *node = baseDN;
238                                 while( node ) {
239                                         ++iBaseDN;
240                                         if( ! sBaseDN ) {
241                                                 sBaseDN = g_strdup( node->data );
242                                         }
243                                         node = g_list_next( node );
244                                 }
245                                 mgu_free_dlist( baseDN );
246                                 baseDN = node = NULL;
247                         }
248                         flg = TRUE;
249                 }
250         }
251         g_free( sHost );
252         g_free( sBind );
253         g_free( sPass );
254
255         if( sBaseDN ) {
256                 /* Load search DN */
257                 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), sBaseDN);
258                 g_free( sBaseDN );
259         }
260
261         /* Display appropriate message */
262         if( flg ) {
263                 sMsg = _( "Connected successfully to server" );
264         }
265         else {
266                 sMsg = _( "Could not connect to server" );
267         }
268         edit_ldap_status_show( sMsg );
269 }
270
271 static void edit_ldap_basedn_select( void ) {
272         gchar *sHost, *sBind, *sPass, *sBase;
273         gint iPort, iTime;
274         gchar *selectDN;
275
276         sHost = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
277         sBase = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_baseDN), 0, -1 );
278         sBind = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
279         sPass = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
280         iPort = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
281         iTime = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
282         g_strchomp( sHost ); g_strchug( sHost );
283         g_strchomp( sBind ); g_strchug( sBind );
284         g_strchomp( sPass ); g_strchug( sPass );
285         selectDN = edit_ldap_basedn_selection( sHost, iPort, sBase, iTime, sBind, sPass );
286         if( selectDN ) {
287                 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), selectDN);
288                 g_free( selectDN );
289                 selectDN = NULL;
290         }
291         g_free( sHost );
292         g_free( sBase );
293         g_free( sBind );
294         g_free( sPass );
295 }
296
297 static void edit_ldap_search_reset( void ) {
298         gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), LDAPCTL_DFL_ATTR_LIST );
299 }
300
301 static void addressbook_edit_ldap_dialog_create( gboolean *cancelled ) {
302         GtkWidget *window;
303         GtkWidget *vbox;
304         GtkWidget *notebook;
305         GtkWidget *hbbox;
306         GtkWidget *ok_btn;
307         GtkWidget *cancel_btn;
308         GtkWidget *hsbox;
309         GtkWidget *statusbar;
310
311         window = gtk_window_new(GTK_WINDOW_DIALOG);
312         gtk_widget_set_usize(window, 450, -1);
313         gtk_container_set_border_width(GTK_CONTAINER(window), 0);
314         gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP Server"));
315         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
316         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
317         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
318                            GTK_SIGNAL_FUNC(edit_ldap_delete_event),
319                            cancelled);
320         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
321                            GTK_SIGNAL_FUNC(edit_ldap_key_pressed),
322                            cancelled);
323
324         vbox = gtk_vbox_new( FALSE, 6 );
325         gtk_widget_show( vbox );
326         gtk_container_add( GTK_CONTAINER( window ), vbox );
327
328         /* Notebook */
329         notebook = gtk_notebook_new();
330         gtk_widget_show( notebook );
331         gtk_box_pack_start( GTK_BOX( vbox ), notebook, TRUE, TRUE, 0 );
332         gtk_container_set_border_width( GTK_CONTAINER( notebook ), 6 );
333
334         /* Status line */
335         hsbox = gtk_hbox_new(FALSE, 0);
336         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
337         statusbar = gtk_statusbar_new();
338         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
339
340         /* Button panel */
341         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
342                                 &cancel_btn, _("Cancel"), NULL, NULL);
343         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
344         gtk_widget_grab_default(ok_btn);
345
346         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
347                            GTK_SIGNAL_FUNC(edit_ldap_ok), cancelled);
348         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
349                            GTK_SIGNAL_FUNC(edit_ldap_cancel), cancelled);
350
351         gtk_widget_show_all(vbox);
352
353         ldapedit.window     = window;
354         ldapedit.notebook   = notebook;
355         ldapedit.ok_btn     = ok_btn;
356         ldapedit.cancel_btn = cancel_btn;
357         ldapedit.statusbar  = statusbar;
358         ldapedit.status_cid =
359                 gtk_statusbar_get_context_id(
360                         GTK_STATUSBAR(statusbar), "Edit LDAP Server Dialog" );
361 }
362
363 static void addressbook_edit_ldap_page_basic( gint pageNum, gchar *pageLbl ) {
364         GtkWidget *vbox;
365         GtkWidget *table;
366         GtkWidget *label;
367         GtkWidget *entry_name;
368         GtkWidget *entry_server;
369         GtkWidget *hbox_spin;
370         GtkObject *spinbtn_port_adj;
371         GtkWidget *spinbtn_port;
372         GtkWidget *entry_baseDN;
373         GtkWidget *check_btn;
374         GtkWidget *lookdn_btn;
375         GtkTooltips *toolTip;
376         gint top;
377
378         vbox = gtk_vbox_new( FALSE, 8 );
379         gtk_widget_show( vbox );
380         gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
381
382         label = gtk_label_new( pageLbl );
383         gtk_widget_show( label );
384         gtk_notebook_set_tab_label(
385                 GTK_NOTEBOOK( ldapedit.notebook ),
386                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
387
388         table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
389         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
390         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
391         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
392         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
393
394         /* First row */
395         top = 0;
396         label = gtk_label_new(_("Name"));
397         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
398         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
399
400         entry_name = gtk_entry_new();
401         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1),
402                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
403
404         toolTip = gtk_tooltips_new();
405         gtk_tooltips_set_tip( toolTip, entry_name, _( 
406                 "A name that you wish to call the server." ),
407                 NULL );
408
409         /* Next row */
410         ++top;
411         label = gtk_label_new(_("Hostname"));
412         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
413         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
414
415         entry_server = gtk_entry_new();
416         gtk_table_attach(GTK_TABLE(table), entry_server, 1, 2, top, (top + 1),
417                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
418
419         toolTip = gtk_tooltips_new();
420         gtk_tooltips_set_tip( toolTip, entry_server, _( 
421                 "This is the hostname of the server. For example, " \
422                 "\"ldap.mydomain.com\" may be appropriate for the " \
423                 "\"mydomain.com\" organization. An IP address may also be " \
424                 "used. You may specify \"localhost\" if running an LDAP " \
425                 "server on the same computer as Sylpheed." ),
426                 NULL );
427
428         /* Next row */
429         ++top;
430         label = gtk_label_new(_("Port"));
431         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
432         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
433
434         hbox_spin = gtk_hbox_new (FALSE, 8);
435         spinbtn_port_adj = gtk_adjustment_new (389, 1, 65535, 100, 1000, 1000);
436         spinbtn_port = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_port_adj), 1, 0);
437         gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_port, FALSE, FALSE, 0);
438         gtk_widget_set_usize (spinbtn_port, 64, -1);
439         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_port), TRUE);
440         gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
441                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
442
443         toolTip = gtk_tooltips_new();
444         gtk_tooltips_set_tip( toolTip, spinbtn_port, _( 
445                 "The port number that the server listens on. Port 389 is " \
446                 "the default." ),
447                 NULL );
448
449         check_btn = gtk_button_new_with_label( _(" Check Server "));
450         gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
451
452         toolTip = gtk_tooltips_new();
453         gtk_tooltips_set_tip( toolTip, check_btn, _( 
454                 "Press this button to test the connection to the server." ),
455                 NULL );
456
457         /* Next row */
458         ++top;
459         label = gtk_label_new(_("Search Base"));
460         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
461         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
462
463         entry_baseDN = gtk_entry_new();
464         gtk_table_attach(GTK_TABLE(table), entry_baseDN, 1, 2, top, (top + 1),
465                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
466
467         toolTip = gtk_tooltips_new();
468         gtk_tooltips_set_tip( toolTip, entry_baseDN, _( 
469                 "This specifies the name of the directory to be searched " \
470                 "on the server. Examples include:\n" \
471                 "  dc=sylpheed,dc=org\n" \
472                 "  ou=people,dc=domainname,dc=com\n" \
473                 "  o=Organization Name,c=Country\n"
474                 ),
475                 NULL );
476
477         lookdn_btn = gtk_button_new_with_label( _(" ... "));
478         gtk_table_attach(GTK_TABLE(table), lookdn_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
479
480         toolTip = gtk_tooltips_new();
481         gtk_tooltips_set_tip( toolTip, lookdn_btn, _( 
482                 "Press this button to lookup the name of available " \
483                 "directory names on the server." ),
484                 NULL );
485
486         /* Signal handlers */
487         gtk_signal_connect(GTK_OBJECT(check_btn), "clicked",
488                            GTK_SIGNAL_FUNC(edit_ldap_server_check), NULL);
489         gtk_signal_connect(GTK_OBJECT(lookdn_btn), "clicked",
490                            GTK_SIGNAL_FUNC(edit_ldap_basedn_select), NULL);
491
492         /* Done */
493         gtk_widget_show_all(vbox);
494
495         ldapedit.entry_name   = entry_name;
496         ldapedit.entry_server = entry_server;
497         ldapedit.spinbtn_port = spinbtn_port;
498         ldapedit.entry_baseDN = entry_baseDN;
499 }
500
501 static void addressbook_edit_ldap_page_search( gint pageNum, gchar *pageLbl ) {
502         GtkWidget *vbox;
503         GtkWidget *table;
504         GtkWidget *label;
505         GtkWidget *entry_criteria;
506         GtkWidget *hbox_spin;
507         GtkObject *spinbtn_queryage_adj;
508         GtkWidget *spinbtn_queryage;
509         GtkWidget *check_dynsearch;
510         GtkWidget *check_matchoption;
511         GtkWidget *reset_btn;
512         GtkTooltips *toolTip;
513         gint top;
514
515         vbox = gtk_vbox_new( FALSE, 8 );
516         gtk_widget_show( vbox );
517         gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
518
519         label = gtk_label_new( pageLbl );
520         gtk_widget_show( label );
521         gtk_notebook_set_tab_label(
522                 GTK_NOTEBOOK( ldapedit.notebook ),
523                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
524
525         table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
526         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
527         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
528         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
529         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
530
531         /* First row */
532         top = 0;
533         label = gtk_label_new(_("Search Attributes"));
534         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
535         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
536
537         entry_criteria = gtk_entry_new();
538         gtk_table_attach(GTK_TABLE(table), entry_criteria, 1, 2, top, (top + 1),
539                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
540
541         toolTip = gtk_tooltips_new();
542         gtk_tooltips_set_tip( toolTip, entry_criteria, _( 
543                 "A list of LDAP attribute names that should be searched " \
544                 "when attempting to find a name or address." ),
545                 NULL );
546
547         reset_btn = gtk_button_new_with_label( _(" Defaults "));
548         gtk_table_attach(GTK_TABLE(table), reset_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
549
550         toolTip = gtk_tooltips_new();
551         gtk_tooltips_set_tip( toolTip, reset_btn, _( 
552                 "This resets the attribute names to a default value " \
553                 "that should find most names and addresses during a " \
554                 "name or address search process." ),
555                 NULL );
556
557         /* Next row */
558         ++top;
559         label = gtk_label_new(_("Max Query Age (secs)"));
560         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
561         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
562
563         hbox_spin = gtk_hbox_new (FALSE, 8);
564         spinbtn_queryage_adj = gtk_adjustment_new(
565                 LDAPCTL_DFL_QUERY_AGE, 1, LDAPCTL_MAX_QUERY_AGE, 10, 1000, 1000 );
566         spinbtn_queryage = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_queryage_adj), 1, 0);
567         gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_queryage, FALSE, FALSE, 0);
568         gtk_widget_set_usize (spinbtn_queryage, 64, -1);
569         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_queryage), TRUE);
570         gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
571                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
572
573         toolTip = gtk_tooltips_new();
574         gtk_tooltips_set_tip( toolTip, spinbtn_queryage, _( 
575                 "This defines the maximum period of time (in seconds) that " \
576                 "an address search result is valid for address completion " \
577                 "purposes. Search results are stored in a cache until this " \
578                 "period of time has passed and then retired. This will " \
579                 "improve the response time when attempting to search for " \
580                 "the same name or address on subsequent address completion " \
581                 "requests. The cache will be searched in preference to " \
582                 "performing a new server search request. The default value " \
583                 "of 600 seconds (10 minutes), should be sufficient for most " \
584                 "servers. A larger value will reduce the search time for " \
585                 "subsequent searches. This is useful for servers that have " \
586                 "slow response times at the expense of more memory to cache " \
587                 "results." ),
588                 NULL );
589
590         /* Next row */
591         ++top;
592         check_dynsearch = gtk_check_button_new_with_label(
593                                 _("Include server in dynamic search") );
594         gtk_table_attach(GTK_TABLE(table), check_dynsearch, 1, 3, top, (top + 1),
595                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
596
597         toolTip = gtk_tooltips_new();
598         gtk_tooltips_set_tip( toolTip, check_dynsearch, _( 
599                 "Check this option to include this server for dynamic " \
600                 "searches when using address completion." ),
601                 NULL );
602
603         /* Next row */
604         ++top;
605         check_matchoption = gtk_check_button_new_with_label(
606                                 _("Match names 'containing' search term") );
607         gtk_table_attach(GTK_TABLE(table), check_matchoption, 1, 3, top, (top + 1),
608                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
609
610         toolTip = gtk_tooltips_new();
611         gtk_tooltips_set_tip( toolTip, check_matchoption, _( 
612                 "Searches for names and addresses can be performed either " \
613                 "using \"begins-with\" or \"contains\" search term. Check " \
614                 "this option to perform a contains search; this type of " \
615                 "search usually takes longer to complete. Note that for " \
616                 "performance reasons, address completion uses " \
617                 "\"begins-with\" for all searches against other address " \
618                 "interfaces." \
619                 ),
620                 NULL );
621
622         /* Signal handlers */
623         gtk_signal_connect(GTK_OBJECT(reset_btn), "clicked",
624                            GTK_SIGNAL_FUNC(edit_ldap_search_reset), NULL);
625
626         /* Done */
627         gtk_widget_show_all(vbox);
628
629         ldapedit.entry_criteria    = entry_criteria;
630         ldapedit.spinbtn_queryage  = spinbtn_queryage;
631         ldapedit.check_dynsearch   = check_dynsearch;
632         ldapedit.check_matchoption = check_matchoption;
633 }
634
635 static void addressbook_edit_ldap_page_extended( gint pageNum, gchar *pageLbl ) {
636         GtkWidget *vbox;
637         GtkWidget *table;
638         GtkWidget *label;
639         GtkWidget *entry_bindDN;
640         GtkWidget *entry_bindPW;
641         GtkWidget *hbox_spin;
642         GtkObject *spinbtn_timeout_adj;
643         GtkWidget *spinbtn_timeout;
644         GtkObject *spinbtn_maxentry_adj;
645         GtkWidget *spinbtn_maxentry;
646         GtkTooltips *toolTip;
647         gint top;
648
649         vbox = gtk_vbox_new( FALSE, 8 );
650         gtk_widget_show( vbox );
651         gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
652
653         label = gtk_label_new( pageLbl );
654         gtk_widget_show( label );
655         gtk_notebook_set_tab_label(
656                 GTK_NOTEBOOK( ldapedit.notebook ),
657                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
658
659         table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
660         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
661         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
662         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
663         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
664
665         /* Next row */
666         top = 0;
667         label = gtk_label_new(_("Bind DN"));
668         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
669         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
670
671         entry_bindDN = gtk_entry_new();
672         gtk_table_attach(GTK_TABLE(table), entry_bindDN, 1, 2, top, (top + 1),
673                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
674
675         toolTip = gtk_tooltips_new();
676         gtk_tooltips_set_tip( toolTip, entry_bindDN, _( 
677                 "The LDAP user account name to be used to connect to the " \
678                 "This is usually only used for protected servers. This name " \
679                 "is typically formatted as: \"cn=user,dc=sylpheed,dc=com\". " \
680                 "This is usually left empty when performing a search." ),
681                 NULL );
682
683         /* Next row */
684         ++top;
685         label = gtk_label_new(_("Bind Password"));
686         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
687         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
688
689         entry_bindPW = gtk_entry_new();
690         gtk_table_attach(GTK_TABLE(table), entry_bindPW, 1, 2, top, (top + 1),
691                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
692
693         toolTip = gtk_tooltips_new();
694         gtk_tooltips_set_tip( toolTip, entry_bindPW, _( 
695                 "The password to be used when connecting as the \"Bind DN\" " \
696                 "user." ),
697                 NULL );
698
699         /* Next row */
700         ++top;
701         label = gtk_label_new(_("Timeout (secs)"));
702         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
703         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
704
705         hbox_spin = gtk_hbox_new (FALSE, 8);
706         spinbtn_timeout_adj = gtk_adjustment_new (0, 0, 300, 1, 10, 10);
707         spinbtn_timeout = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_timeout_adj), 1, 0);
708         gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_timeout, FALSE, FALSE, 0);
709         gtk_widget_set_usize (spinbtn_timeout, 64, -1);
710         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_timeout), TRUE);
711         gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
712                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
713
714         toolTip = gtk_tooltips_new();
715         gtk_tooltips_set_tip( toolTip, spinbtn_timeout, _( 
716                 "The timeout period in seconds." ), NULL );
717
718         /* Next row */
719         ++top;
720         label = gtk_label_new(_("Maximum Entries"));
721         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
722         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
723
724         hbox_spin = gtk_hbox_new (FALSE, 8);
725         spinbtn_maxentry_adj = gtk_adjustment_new (0, 0, 500, 1, 10, 10);
726         spinbtn_maxentry = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_maxentry_adj), 1, 0);
727         gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_maxentry, FALSE, FALSE, 0);
728         gtk_widget_set_usize (spinbtn_maxentry, 64, -1);
729         gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_maxentry), TRUE);
730         gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
731                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
732
733         toolTip = gtk_tooltips_new();
734         gtk_tooltips_set_tip( toolTip, spinbtn_maxentry, _( 
735                 "The maximum number of entries that should be returned be " \
736                 "returned in the search results." ),
737                 NULL );
738
739         /* Done */
740         gtk_widget_show_all(vbox);
741
742         ldapedit.entry_bindDN     = entry_bindDN;
743         ldapedit.entry_bindPW     = entry_bindPW;
744         ldapedit.spinbtn_timeout  = spinbtn_timeout;
745         ldapedit.spinbtn_maxentry = spinbtn_maxentry;
746 }
747
748 static void addressbook_edit_ldap_create( gboolean *cancelled ) {
749         gint page = 0;
750         addressbook_edit_ldap_dialog_create( cancelled );
751         addressbook_edit_ldap_page_basic( page++, _( "Basic" ) );
752         addressbook_edit_ldap_page_search( page++, _( "Search" ) );
753         addressbook_edit_ldap_page_extended( page++, _( "Extended" ) );
754         gtk_widget_show_all( ldapedit.window );
755 }
756
757 void edit_ldap_set_optmenu( GtkOptionMenu *optmenu, const gint value ) {
758         GList *cur;
759         GtkWidget *menu;
760         GtkWidget *menuitem;
761         gint menuVal;
762         gint n = 0;
763
764         g_return_if_fail(optmenu != NULL);
765
766         menu = gtk_option_menu_get_menu(optmenu);
767         for( cur = GTK_MENU_SHELL(menu)->children; cur != NULL; cur = cur->next ) {
768                 menuitem = GTK_WIDGET(cur->data);
769                 menuVal = GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(menuitem)));
770                 if( menuVal == value ) {
771                         gtk_option_menu_set_history(optmenu, n);
772                         return;
773                 }
774                 n++;
775         }
776         gtk_option_menu_set_history(optmenu, 0);
777 }
778
779 gint edit_ldap_get_optmenu( GtkOptionMenu *optmenu ) {
780         GtkWidget *menu;
781         GtkWidget *menuitem;
782
783         g_return_val_if_fail(optmenu != NULL, -1);
784
785         menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
786         menuitem = gtk_menu_get_active(GTK_MENU(menu));
787         return GPOINTER_TO_INT(gtk_object_get_user_data(GTK_OBJECT(menuitem)));
788 }
789
790 /**
791  * Format criteria list for display.
792  * \param ctl Control object.
793  * \return Formatted string, or <i>NULL</i> if no attributes found.
794  */
795 static gchar *editldap_build_criteria_list( const LdapControl *ctl ) {
796         gchar *str = NULL;
797         gchar *tmp = NULL;
798         GList *node;
799
800         node = ldapctl_get_criteria_list( ctl );
801         while( node ) {
802                 gchar *attr = node->data;
803                 if( str ) {
804                         tmp = g_strdup_printf( "%s, %s", str, attr );
805                         g_free( str );
806                         str = tmp;
807                         tmp = NULL;
808                 }
809                 else {
810                         str = g_strdup( attr );
811                 }
812                 node = g_list_next( node );
813         }
814
815         return str;
816 }
817
818 /**
819  * Parse out individual attribute names from criteria string.
820  * \param criteria Criteria string.
821  * \ctl   Control object.
822  */
823 static void editldap_parse_criteria( gchar *criteria, LdapControl *ctl ) {
824         gchar *ptr;
825         gchar **splitStr;
826         gint i;
827
828         /* Replace delimiters with spaces */
829         ptr = criteria;
830         while( *ptr ) {
831                 if( *ptr == ',' || *ptr == ';' || *ptr == '|' )
832                         *ptr = ' ';
833                 ptr++;
834         }
835
836         /* Parse string */
837         ldapctl_criteria_list_clear( ctl );
838         splitStr = g_strsplit( criteria, " ", 0 );
839         i = 0;
840         while( TRUE ) {
841                 if( splitStr[i] ) {
842                         if( *splitStr[i] ) {
843                                 ldapctl_criteria_list_add( ctl, splitStr[i] );
844                         }
845                 }
846                 else {
847                         break;
848                 }
849                 i++;
850         }
851         g_strfreev( splitStr );
852 }
853
854 /**
855  * Clear entry fields to reasonable defaults (for a new server entry).
856  */
857 static void edit_ldap_clear_fields( void ) {
858         gtk_entry_set_text(
859                 GTK_ENTRY(ldapedit.entry_name), ADDRESSBOOK_GUESS_LDAP_NAME );
860         gtk_entry_set_text(
861                 GTK_ENTRY(ldapedit.entry_server), ADDRESSBOOK_GUESS_LDAP_SERVER );
862         gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), "");
863         gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindDN), "");
864         gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindPW), "");
865         gtk_spin_button_set_value(
866                 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ), LDAPCTL_DFL_PORT );
867         gtk_spin_button_set_value(
868                 GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ), LDAPCTL_DFL_TIMEOUT );
869         gtk_spin_button_set_value(
870                 GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ), LDAPCTL_DFL_TIMEOUT );
871         gtk_entry_set_text(
872                 GTK_ENTRY(ldapedit.entry_criteria), LDAPCTL_DFL_ATTR_LIST );
873         gtk_spin_button_set_value(
874                 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), LDAPCTL_DFL_QUERY_AGE );
875         gtk_toggle_button_set_active(
876                 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), TRUE );
877         gtk_toggle_button_set_active(
878                 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption), FALSE );
879 }
880
881 /**
882  * Load entry fields from server control data.
883  * \param server Server object.
884  */
885 static void edit_ldap_set_fields( LdapServer *server ) {
886         LdapControl *ctl;
887         gchar *crit;
888
889         if( ldapsvr_get_name( server ) )
890                 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_name),
891                 ldapsvr_get_name( server ) );
892
893         ctl = server->control;
894         if( ctl->hostName )
895                 gtk_entry_set_text(
896                         GTK_ENTRY(ldapedit.entry_server), ctl->hostName);
897         if( ctl->baseDN )
898                 gtk_entry_set_text(
899                         GTK_ENTRY(ldapedit.entry_baseDN), ctl->baseDN );
900         if( ctl->bindDN )
901                 gtk_entry_set_text(
902                         GTK_ENTRY(ldapedit.entry_bindDN), ctl->bindDN );
903         if( ctl->bindPass )
904                 gtk_entry_set_text(
905                         GTK_ENTRY(ldapedit.entry_bindPW), ctl->bindPass );
906         gtk_spin_button_set_value(
907                 GTK_SPIN_BUTTON(ldapedit.spinbtn_port), ctl->port );
908         gtk_spin_button_set_value(
909                 GTK_SPIN_BUTTON(ldapedit.spinbtn_timeout), ctl->timeOut );
910         gtk_spin_button_set_value(
911                 GTK_SPIN_BUTTON(ldapedit.spinbtn_maxentry), ctl->maxEntries );
912
913         /* Format criteria */
914         crit = editldap_build_criteria_list( ctl );
915         if( crit ) {
916                 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), crit );
917                 g_free( crit );
918         }
919         else {
920                 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), "" );
921         }
922         gtk_spin_button_set_value(
923                 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), ctl->maxQueryAge );
924         gtk_toggle_button_set_active(
925                 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), server->searchFlag );
926         gtk_toggle_button_set_active(
927                 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption),
928                 ( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) );
929 }
930
931 /**
932  * Edit LDAP server datasource that appears addressbook.
933  * \param addrIndex Address index object.
934  * \param ads       Data source adapter.
935  * \return Update data source adapter, or <code>NULL</code> if user cancelled
936  *         edit with dialog.
937  */
938 AdapterDSource *addressbook_edit_ldap(
939         AddressIndex *addrIndex, AdapterDSource *ads )
940 {
941         static gboolean cancelled;
942         gchar *sName, *sHost, *sBase, *sBind, *sPass, *sCrit;
943         gint iPort, iMaxE, iTime, iAge;
944         gboolean bSrch, bMatch;
945         AddressDataSource *ds = NULL;
946         LdapServer *server = NULL;
947         LdapControl *ctl = NULL;
948         gboolean fin;
949
950         if (!ldapedit.window)
951                 addressbook_edit_ldap_create(&cancelled);
952         gtk_notebook_set_page( GTK_NOTEBOOK(ldapedit.notebook), PAGE_BASIC );
953         gtk_widget_grab_focus(ldapedit.ok_btn);
954         gtk_widget_grab_focus(ldapedit.entry_name);
955         gtk_widget_show(ldapedit.window);
956         manage_window_set_transient(GTK_WINDOW(ldapedit.window));
957
958         edit_ldap_status_show( "" );
959         if( ads ) {
960                 ds = ads->dataSource;
961                 server = ds->rawDataSource;
962                 edit_ldap_set_fields( server );
963                 gtk_window_set_title(
964                         GTK_WINDOW(ldapedit.window), _("Edit LDAP Server"));
965         }
966         else {
967                 edit_ldap_clear_fields();
968                 gtk_window_set_title(
969                         GTK_WINDOW(ldapedit.window), _("Add New LDAP Server"));
970         }
971
972         gtk_main();
973         gtk_widget_hide(ldapedit.window);
974         if (cancelled == TRUE) return NULL;
975
976         sName = gtk_editable_get_chars(
977                         GTK_EDITABLE(ldapedit.entry_name), 0, -1 );
978         sHost = gtk_editable_get_chars(
979                         GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
980         sBase = gtk_editable_get_chars(
981                         GTK_EDITABLE(ldapedit.entry_baseDN), 0, -1 );
982         sCrit = gtk_editable_get_chars(
983                         GTK_EDITABLE(ldapedit.entry_criteria), 0, -1 );
984         sBind = gtk_editable_get_chars(
985                         GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
986         sPass = gtk_editable_get_chars(
987                         GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
988         iPort = gtk_spin_button_get_value_as_int(
989                         GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
990         iTime = gtk_spin_button_get_value_as_int(
991                         GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
992         iMaxE = gtk_spin_button_get_value_as_int(
993                         GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ) );
994         iAge  = gtk_spin_button_get_value_as_int(
995                         GTK_SPIN_BUTTON( ldapedit.spinbtn_queryage ) );
996         bSrch = gtk_toggle_button_get_active(
997                         GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch ) );
998         bMatch = gtk_toggle_button_get_active(
999                         GTK_TOGGLE_BUTTON( ldapedit.check_matchoption ) );
1000
1001         fin = FALSE;
1002         if( *sName == '\0' ) fin = TRUE;
1003         if( *sHost == '\0' ) fin = TRUE;
1004
1005         if( ! fin ) {
1006                 /* Save changes */
1007                 if( ! ads ) {
1008                         /* New server */
1009                         server = ldapsvr_create();
1010                         ds = addrindex_index_add_datasource(
1011                                 addrIndex, ADDR_IF_LDAP, server );
1012                         ads = addressbook_create_ds_adapter(
1013                                 ds, ADDR_LDAP, NULL );
1014                 }
1015                 ctl = server->control;
1016                 addressbook_ads_set_name( ads, sName );
1017                 ldapsvr_set_name( server, sName );
1018                 ldapsvr_set_search_flag( server, bSrch );
1019                 ldapctl_set_host( ctl, sHost );
1020                 ldapctl_set_base_dn( ctl, sBase );
1021                 ldapctl_set_bind_dn( ctl, sBind );
1022                 ldapctl_set_bind_password( ctl, sPass );
1023                 ldapctl_set_port( ctl, iPort );
1024                 ldapctl_set_max_entries( ctl, iMaxE );
1025                 ldapctl_set_timeout( ctl, iTime );
1026                 ldapctl_set_max_query_age( ctl, iAge );
1027                 ldapctl_set_matching_option(
1028                         ctl, bMatch ?
1029                         LDAPCTL_MATCH_CONTAINS : LDAPCTL_MATCH_BEGINWITH );
1030
1031                 /* Save attributes */
1032                 editldap_parse_criteria( sCrit, ctl );
1033
1034         }
1035         g_free( sName );
1036         g_free( sHost );
1037         g_free( sBase );
1038         g_free( sBind );
1039         g_free( sPass );
1040         g_free( sCrit );
1041
1042         return ads;
1043 }
1044
1045 #endif /* USE_LDAP */
1046
1047 /*
1048 * End of Source.
1049 */