2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2003 Match Grun
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.
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.
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.
21 * Edit LDAP address book data.
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>
42 #include "addressbook.h"
43 #include "prefs_common.h"
44 #include "addressitem.h"
46 #include "ldapserver.h"
49 #include "editldap_basedn.h"
50 #include "manage_window.h"
55 #define PAGE_EXTENDED 2
57 #define ADDRESSBOOK_GUESS_LDAP_NAME "MyServer"
58 #define ADDRESSBOOK_GUESS_LDAP_SERVER "localhost"
60 #define LDAPEDIT_TABLE_ROWS 6
61 #define LDAPEDIT_TABLE_COLS 3
63 static struct _LDAPEdit {
67 GtkWidget *cancel_btn;
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;
85 * Parse out individual attribute names from criteria string.
86 * \param criteria Criteria string.
87 * \ctl Control object.
89 static gboolean editldap_validate_criteria( gchar *criteria ) {
97 /* Replace delimiters with spaces */
100 if( *ptr == ',' || *ptr == ';' || *ptr == '|' )
106 splitStr = g_strsplit( criteria, " ", 0 );
120 g_strfreev( splitStr );
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 );
131 gtk_statusbar_push( GTK_STATUSBAR(ldapedit.statusbar),
132 ldapedit.status_cid, msg );
137 static gboolean edit_ldap_validate( void ) {
143 str = gtk_editable_get_chars(
144 GTK_EDITABLE(ldapedit.entry_name), 0, -1 );
147 gtk_widget_grab_focus( ldapedit.entry_name );
148 edit_ldap_status_show( _( "A Name must be supplied." ) );
154 str = gtk_editable_get_chars(
155 GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
158 gtk_widget_grab_focus( ldapedit.entry_server );
159 edit_ldap_status_show(
160 _( "A Hostname must be supplied for the server." ) );
167 str = gtk_editable_get_chars(
168 GTK_EDITABLE(ldapedit.entry_criteria), 0, -1 );
169 if( editldap_validate_criteria( str ) ) {
171 gtk_widget_grab_focus( ldapedit.entry_criteria );
172 edit_ldap_status_show(
173 _( "At least one LDAP search attribute should be supplied." ) );
179 /* Switch to page with error */
181 gtk_notebook_set_current_page( GTK_NOTEBOOK(ldapedit.notebook), page );
187 static void edit_ldap_ok( GtkWidget *widget, gboolean *cancelled ) {
188 if( ! edit_ldap_validate() ) {
194 static void edit_ldap_cancel( GtkWidget *widget, gboolean *cancelled ) {
199 static gint edit_ldap_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
205 static gboolean edit_ldap_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
206 if (event && event->keyval == GDK_Escape) {
213 static void edit_ldap_server_check( void ) {
214 gchar *sHost, *sBind, *sPass;
217 gchar *sBaseDN = NULL;
220 GList *baseDN = NULL;
222 edit_ldap_status_show( "" );
224 sHost = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
225 sBind = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
226 sPass = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
227 iPort = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
228 iTime = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
229 g_strchomp( sHost ); g_strchug( sHost );
230 g_strchomp( sBind ); g_strchug( sBind );
231 g_strchomp( sPass ); g_strchug( sPass );
232 if( *sHost != '\0' ) {
233 /* Test connection to server */
234 if( ldaputil_test_connect( sHost, iPort ) ) {
235 /* Attempt to read base DN */
236 baseDN = ldaputil_read_basedn( sHost, iPort, sBind, sPass, iTime );
238 GList *node = baseDN;
242 sBaseDN = g_strdup( node->data );
244 node = g_list_next( node );
246 mgu_free_dlist( baseDN );
247 baseDN = node = NULL;
258 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), sBaseDN);
262 /* Display appropriate message */
264 sMsg = _( "Connected successfully to server" );
267 sMsg = _( "Could not connect to server" );
269 edit_ldap_status_show( sMsg );
272 static void edit_ldap_basedn_select( void ) {
273 gchar *sHost, *sBind, *sPass, *sBase;
277 sHost = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
278 sBase = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_baseDN), 0, -1 );
279 sBind = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
280 sPass = gtk_editable_get_chars( GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
281 iPort = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
282 iTime = gtk_spin_button_get_value_as_int( GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
283 g_strchomp( sHost ); g_strchug( sHost );
284 g_strchomp( sBind ); g_strchug( sBind );
285 g_strchomp( sPass ); g_strchug( sPass );
286 selectDN = edit_ldap_basedn_selection( sHost, iPort, sBase, iTime, sBind, sPass );
288 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), selectDN);
298 static void edit_ldap_search_reset( void ) {
299 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), LDAPCTL_DFL_ATTR_LIST );
302 static void addressbook_edit_ldap_dialog_create( gboolean *cancelled ) {
308 GtkWidget *cancel_btn;
310 GtkWidget *statusbar;
312 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
313 gtk_widget_set_size_request(window, 450, -1);
314 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
315 gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP Server"));
316 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
317 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
318 g_signal_connect(G_OBJECT(window), "delete_event",
319 G_CALLBACK(edit_ldap_delete_event),
321 g_signal_connect(G_OBJECT(window), "key_press_event",
322 G_CALLBACK(edit_ldap_key_pressed),
325 vbox = gtk_vbox_new( FALSE, 6 );
326 gtk_widget_show( vbox );
327 gtk_container_add( GTK_CONTAINER( window ), vbox );
330 notebook = gtk_notebook_new();
331 gtk_widget_show( notebook );
332 gtk_box_pack_start( GTK_BOX( vbox ), notebook, TRUE, TRUE, 0 );
333 gtk_container_set_border_width( GTK_CONTAINER( notebook ), 6 );
336 hsbox = gtk_hbox_new(FALSE, 0);
337 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
338 statusbar = gtk_statusbar_new();
339 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
342 gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
343 &cancel_btn, _("Cancel"), NULL, NULL);
344 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
345 gtk_widget_grab_default(ok_btn);
347 g_signal_connect(G_OBJECT(ok_btn), "clicked",
348 G_CALLBACK(edit_ldap_ok), cancelled);
349 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
350 G_CALLBACK(edit_ldap_cancel), cancelled);
352 gtk_widget_show_all(vbox);
354 ldapedit.window = window;
355 ldapedit.notebook = notebook;
356 ldapedit.ok_btn = ok_btn;
357 ldapedit.cancel_btn = cancel_btn;
358 ldapedit.statusbar = statusbar;
359 ldapedit.status_cid =
360 gtk_statusbar_get_context_id(
361 GTK_STATUSBAR(statusbar), "Edit LDAP Server Dialog" );
364 static void addressbook_edit_ldap_page_basic( gint pageNum, gchar *pageLbl ) {
368 GtkWidget *entry_name;
369 GtkWidget *entry_server;
370 GtkWidget *hbox_spin;
371 GtkObject *spinbtn_port_adj;
372 GtkWidget *spinbtn_port;
373 GtkWidget *entry_baseDN;
374 GtkWidget *check_btn;
375 GtkWidget *lookdn_btn;
376 GtkTooltips *toolTip;
379 vbox = gtk_vbox_new( FALSE, 8 );
380 gtk_widget_show( vbox );
381 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
383 label = gtk_label_new( pageLbl );
384 gtk_widget_show( label );
385 gtk_notebook_set_tab_label(
386 GTK_NOTEBOOK( ldapedit.notebook ),
387 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
389 table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
390 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
391 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
392 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
393 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
397 label = gtk_label_new(_("Name"));
398 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
399 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
401 entry_name = gtk_entry_new();
402 gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1),
403 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
405 toolTip = gtk_tooltips_new();
406 gtk_tooltips_set_tip( toolTip, entry_name, _(
407 "A name that you wish to call the server." ),
412 label = gtk_label_new(_("Hostname"));
413 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
414 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
416 entry_server = gtk_entry_new();
417 gtk_table_attach(GTK_TABLE(table), entry_server, 1, 2, top, (top + 1),
418 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
420 toolTip = gtk_tooltips_new();
421 gtk_tooltips_set_tip( toolTip, entry_server, _(
422 "This is the hostname of the server. For example, " \
423 "\"ldap.mydomain.com\" may be appropriate for the " \
424 "\"mydomain.com\" organization. An IP address may also be " \
425 "used. You may specify \"localhost\" if running an LDAP " \
426 "server on the same computer as Sylpheed." ),
431 label = gtk_label_new(_("Port"));
432 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
433 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
435 hbox_spin = gtk_hbox_new (FALSE, 8);
436 spinbtn_port_adj = gtk_adjustment_new (389, 1, 65535, 100, 1000, 1000);
437 spinbtn_port = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_port_adj), 1, 0);
438 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_port, FALSE, FALSE, 0);
439 gtk_widget_set_size_request (spinbtn_port, 64, -1);
440 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_port), TRUE);
441 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
442 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
444 toolTip = gtk_tooltips_new();
445 gtk_tooltips_set_tip( toolTip, spinbtn_port, _(
446 "The port number that the server listens on. Port 389 is " \
450 check_btn = gtk_button_new_with_label( _(" Check Server "));
451 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
453 toolTip = gtk_tooltips_new();
454 gtk_tooltips_set_tip( toolTip, check_btn, _(
455 "Press this button to test the connection to the server." ),
460 label = gtk_label_new(_("Search Base"));
461 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
462 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
464 entry_baseDN = gtk_entry_new();
465 gtk_table_attach(GTK_TABLE(table), entry_baseDN, 1, 2, top, (top + 1),
466 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
468 toolTip = gtk_tooltips_new();
469 gtk_tooltips_set_tip( toolTip, entry_baseDN, _(
470 "This specifies the name of the directory to be searched " \
471 "on the server. Examples include:\n" \
472 " dc=sylpheed,dc=org\n" \
473 " ou=people,dc=domainname,dc=com\n" \
474 " o=Organization Name,c=Country\n"
478 lookdn_btn = gtk_button_new_with_label( _(" ... "));
479 gtk_table_attach(GTK_TABLE(table), lookdn_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
481 toolTip = gtk_tooltips_new();
482 gtk_tooltips_set_tip( toolTip, lookdn_btn, _(
483 "Press this button to lookup the name of available " \
484 "directory names on the server." ),
487 /* Signal handlers */
488 g_signal_connect(G_OBJECT(check_btn), "clicked",
489 G_CALLBACK(edit_ldap_server_check), NULL);
490 g_signal_connect(G_OBJECT(lookdn_btn), "clicked",
491 G_CALLBACK(edit_ldap_basedn_select), NULL);
494 gtk_widget_show_all(vbox);
496 ldapedit.entry_name = entry_name;
497 ldapedit.entry_server = entry_server;
498 ldapedit.spinbtn_port = spinbtn_port;
499 ldapedit.entry_baseDN = entry_baseDN;
502 static void addressbook_edit_ldap_page_search( gint pageNum, gchar *pageLbl ) {
506 GtkWidget *entry_criteria;
507 GtkWidget *hbox_spin;
508 GtkObject *spinbtn_queryage_adj;
509 GtkWidget *spinbtn_queryage;
510 GtkWidget *check_dynsearch;
511 GtkWidget *check_matchoption;
512 GtkWidget *reset_btn;
513 GtkTooltips *toolTip;
516 vbox = gtk_vbox_new( FALSE, 8 );
517 gtk_widget_show( vbox );
518 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
520 label = gtk_label_new( pageLbl );
521 gtk_widget_show( label );
522 gtk_notebook_set_tab_label(
523 GTK_NOTEBOOK( ldapedit.notebook ),
524 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
526 table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
527 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
528 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
529 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
530 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
534 label = gtk_label_new(_("Search Attributes"));
535 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
536 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
538 entry_criteria = gtk_entry_new();
539 gtk_table_attach(GTK_TABLE(table), entry_criteria, 1, 2, top, (top + 1),
540 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
542 toolTip = gtk_tooltips_new();
543 gtk_tooltips_set_tip( toolTip, entry_criteria, _(
544 "A list of LDAP attribute names that should be searched " \
545 "when attempting to find a name or address." ),
548 reset_btn = gtk_button_new_with_label( _(" Defaults "));
549 gtk_table_attach(GTK_TABLE(table), reset_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
551 toolTip = gtk_tooltips_new();
552 gtk_tooltips_set_tip( toolTip, reset_btn, _(
553 "This resets the attribute names to a default value " \
554 "that should find most names and addresses during a " \
555 "name or address search process." ),
560 label = gtk_label_new(_("Max Query Age (secs)"));
561 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
562 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
564 hbox_spin = gtk_hbox_new (FALSE, 8);
565 spinbtn_queryage_adj = gtk_adjustment_new(
566 LDAPCTL_DFL_QUERY_AGE, 1, LDAPCTL_MAX_QUERY_AGE, 10, 1000, 1000 );
567 spinbtn_queryage = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_queryage_adj), 1, 0);
568 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_queryage, FALSE, FALSE, 0);
569 gtk_widget_set_size_request (spinbtn_queryage, 64, -1);
570 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_queryage), TRUE);
571 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
572 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
574 toolTip = gtk_tooltips_new();
575 gtk_tooltips_set_tip( toolTip, spinbtn_queryage, _(
576 "This defines the maximum period of time (in seconds) that " \
577 "an address search result is valid for address completion " \
578 "purposes. Search results are stored in a cache until this " \
579 "period of time has passed and then retired. This will " \
580 "improve the response time when attempting to search for " \
581 "the same name or address on subsequent address completion " \
582 "requests. The cache will be searched in preference to " \
583 "performing a new server search request. The default value " \
584 "of 600 seconds (10 minutes), should be sufficient for most " \
585 "servers. A larger value will reduce the search time for " \
586 "subsequent searches. This is useful for servers that have " \
587 "slow response times at the expense of more memory to cache " \
593 check_dynsearch = gtk_check_button_new_with_label(
594 _("Include server in dynamic search") );
595 gtk_table_attach(GTK_TABLE(table), check_dynsearch, 1, 3, top, (top + 1),
596 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
598 toolTip = gtk_tooltips_new();
599 gtk_tooltips_set_tip( toolTip, check_dynsearch, _(
600 "Check this option to include this server for dynamic " \
601 "searches when using address completion." ),
606 check_matchoption = gtk_check_button_new_with_label(
607 _("Match names 'containing' search term") );
608 gtk_table_attach(GTK_TABLE(table), check_matchoption, 1, 3, top, (top + 1),
609 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
611 toolTip = gtk_tooltips_new();
612 gtk_tooltips_set_tip( toolTip, check_matchoption, _(
613 "Searches for names and addresses can be performed either " \
614 "using \"begins-with\" or \"contains\" search term. Check " \
615 "this option to perform a \"contains\" search; this type of " \
616 "search usually takes longer to complete. Note that for " \
617 "performance reasons, address completion uses " \
618 "\"begins-with\" for all searches against other address " \
623 /* Signal handlers */
624 g_signal_connect(G_OBJECT(reset_btn), "clicked",
625 G_CALLBACK(edit_ldap_search_reset), NULL);
628 gtk_widget_show_all(vbox);
630 ldapedit.entry_criteria = entry_criteria;
631 ldapedit.spinbtn_queryage = spinbtn_queryage;
632 ldapedit.check_dynsearch = check_dynsearch;
633 ldapedit.check_matchoption = check_matchoption;
636 static void addressbook_edit_ldap_page_extended( gint pageNum, gchar *pageLbl ) {
640 GtkWidget *entry_bindDN;
641 GtkWidget *entry_bindPW;
642 GtkWidget *hbox_spin;
643 GtkObject *spinbtn_timeout_adj;
644 GtkWidget *spinbtn_timeout;
645 GtkObject *spinbtn_maxentry_adj;
646 GtkWidget *spinbtn_maxentry;
647 GtkTooltips *toolTip;
650 vbox = gtk_vbox_new( FALSE, 8 );
651 gtk_widget_show( vbox );
652 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
654 label = gtk_label_new( pageLbl );
655 gtk_widget_show( label );
656 gtk_notebook_set_tab_label(
657 GTK_NOTEBOOK( ldapedit.notebook ),
658 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
660 table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
661 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
662 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
663 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
664 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
668 label = gtk_label_new(_("Bind DN"));
669 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
670 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
672 entry_bindDN = gtk_entry_new();
673 gtk_table_attach(GTK_TABLE(table), entry_bindDN, 1, 2, top, (top + 1),
674 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
676 toolTip = gtk_tooltips_new();
677 gtk_tooltips_set_tip( toolTip, entry_bindDN, _(
678 "The LDAP user account name to be used to connect to the server. " \
679 "This is usually only used for protected servers. This name " \
680 "is typically formatted as: \"cn=user,dc=sylpheed,dc=com\". " \
681 "This is usually left empty when performing a search." ),
686 label = gtk_label_new(_("Bind Password"));
687 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
688 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
690 entry_bindPW = gtk_entry_new();
691 gtk_table_attach(GTK_TABLE(table), entry_bindPW, 1, 2, top, (top + 1),
692 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
694 toolTip = gtk_tooltips_new();
695 gtk_tooltips_set_tip( toolTip, entry_bindPW, _(
696 "The password to be used when connecting as the \"Bind DN\" " \
702 label = gtk_label_new(_("Timeout (secs)"));
703 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
704 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
706 hbox_spin = gtk_hbox_new (FALSE, 8);
707 spinbtn_timeout_adj = gtk_adjustment_new (0, 0, 300, 1, 10, 10);
708 spinbtn_timeout = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_timeout_adj), 1, 0);
709 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_timeout, FALSE, FALSE, 0);
710 gtk_widget_set_size_request (spinbtn_timeout, 64, -1);
711 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_timeout), TRUE);
712 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
713 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
715 toolTip = gtk_tooltips_new();
716 gtk_tooltips_set_tip( toolTip, spinbtn_timeout, _(
717 "The timeout period in seconds." ), NULL );
721 label = gtk_label_new(_("Maximum Entries"));
722 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
723 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
725 hbox_spin = gtk_hbox_new (FALSE, 8);
726 spinbtn_maxentry_adj = gtk_adjustment_new (0, 0, 500, 1, 10, 10);
727 spinbtn_maxentry = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_maxentry_adj), 1, 0);
728 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_maxentry, FALSE, FALSE, 0);
729 gtk_widget_set_size_request (spinbtn_maxentry, 64, -1);
730 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_maxentry), TRUE);
731 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
732 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
734 toolTip = gtk_tooltips_new();
735 gtk_tooltips_set_tip( toolTip, spinbtn_maxentry, _(
736 "The maximum number of entries that should be returned " \
737 "in the search result." ),
741 gtk_widget_show_all(vbox);
743 ldapedit.entry_bindDN = entry_bindDN;
744 ldapedit.entry_bindPW = entry_bindPW;
745 ldapedit.spinbtn_timeout = spinbtn_timeout;
746 ldapedit.spinbtn_maxentry = spinbtn_maxentry;
749 static void addressbook_edit_ldap_create( gboolean *cancelled ) {
751 addressbook_edit_ldap_dialog_create( cancelled );
752 addressbook_edit_ldap_page_basic( page++, _( "Basic" ) );
753 addressbook_edit_ldap_page_search( page++, _( "Search" ) );
754 addressbook_edit_ldap_page_extended( page++, _( "Extended" ) );
755 gtk_widget_show_all( ldapedit.window );
758 void edit_ldap_set_optmenu( GtkOptionMenu *optmenu, const gint value ) {
765 g_return_if_fail(optmenu != NULL);
767 menu = gtk_option_menu_get_menu(optmenu);
768 for( cur = GTK_MENU_SHELL(menu)->children; cur != NULL; cur = cur->next ) {
769 menuitem = GTK_WIDGET(cur->data);
770 menuVal = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), "user_data"));
771 if( menuVal == value ) {
772 gtk_option_menu_set_history(optmenu, n);
777 gtk_option_menu_set_history(optmenu, 0);
780 gint edit_ldap_get_optmenu( GtkOptionMenu *optmenu ) {
784 g_return_val_if_fail(optmenu != NULL, -1);
786 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
787 menuitem = gtk_menu_get_active(GTK_MENU(menu));
788 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), "user_data"));
792 * Format criteria list for display.
793 * \param ctl Control object.
794 * \return Formatted string, or <i>NULL</i> if no attributes found.
796 static gchar *editldap_build_criteria_list( const LdapControl *ctl ) {
801 node = ldapctl_get_criteria_list( ctl );
803 gchar *attr = node->data;
805 tmp = g_strdup_printf( "%s, %s", str, attr );
811 str = g_strdup( attr );
813 node = g_list_next( node );
820 * Parse out individual attribute names from criteria string.
821 * \param criteria Criteria string.
822 * \ctl Control object.
824 static void editldap_parse_criteria( gchar *criteria, LdapControl *ctl ) {
829 /* Replace delimiters with spaces */
832 if( *ptr == ',' || *ptr == ';' || *ptr == '|' )
838 ldapctl_criteria_list_clear( ctl );
839 splitStr = g_strsplit( criteria, " ", 0 );
844 ldapctl_criteria_list_add( ctl, splitStr[i] );
852 g_strfreev( splitStr );
856 * Clear entry fields to reasonable defaults (for a new server entry).
858 static void edit_ldap_clear_fields( void ) {
860 GTK_ENTRY(ldapedit.entry_name), ADDRESSBOOK_GUESS_LDAP_NAME );
862 GTK_ENTRY(ldapedit.entry_server), ADDRESSBOOK_GUESS_LDAP_SERVER );
863 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), "");
864 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindDN), "");
865 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindPW), "");
866 gtk_spin_button_set_value(
867 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ), LDAPCTL_DFL_PORT );
868 gtk_spin_button_set_value(
869 GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ), LDAPCTL_DFL_TIMEOUT );
870 gtk_spin_button_set_value(
871 GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ), LDAPCTL_DFL_TIMEOUT );
873 GTK_ENTRY(ldapedit.entry_criteria), LDAPCTL_DFL_ATTR_LIST );
874 gtk_spin_button_set_value(
875 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), LDAPCTL_DFL_QUERY_AGE );
876 gtk_toggle_button_set_active(
877 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), TRUE );
878 gtk_toggle_button_set_active(
879 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption), FALSE );
883 * Load entry fields from server control data.
884 * \param server Server object.
886 static void edit_ldap_set_fields( LdapServer *server ) {
890 if( ldapsvr_get_name( server ) )
891 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_name),
892 ldapsvr_get_name( server ) );
894 ctl = server->control;
897 GTK_ENTRY(ldapedit.entry_server), ctl->hostName);
900 GTK_ENTRY(ldapedit.entry_baseDN), ctl->baseDN );
903 GTK_ENTRY(ldapedit.entry_bindDN), ctl->bindDN );
906 GTK_ENTRY(ldapedit.entry_bindPW), ctl->bindPass );
907 gtk_spin_button_set_value(
908 GTK_SPIN_BUTTON(ldapedit.spinbtn_port), ctl->port );
909 gtk_spin_button_set_value(
910 GTK_SPIN_BUTTON(ldapedit.spinbtn_timeout), ctl->timeOut );
911 gtk_spin_button_set_value(
912 GTK_SPIN_BUTTON(ldapedit.spinbtn_maxentry), ctl->maxEntries );
914 /* Format criteria */
915 crit = editldap_build_criteria_list( ctl );
917 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), crit );
921 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), "" );
923 gtk_spin_button_set_value(
924 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), ctl->maxQueryAge );
925 gtk_toggle_button_set_active(
926 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), server->searchFlag );
927 gtk_toggle_button_set_active(
928 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption),
929 ( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) );
933 * Edit LDAP server datasource that appears addressbook.
934 * \param addrIndex Address index object.
935 * \param ads Data source adapter.
936 * \return Update data source adapter, or <code>NULL</code> if user cancelled
939 AdapterDSource *addressbook_edit_ldap(
940 AddressIndex *addrIndex, AdapterDSource *ads )
942 static gboolean cancelled;
943 gchar *sName, *sHost, *sBase, *sBind, *sPass, *sCrit;
944 gint iPort, iMaxE, iTime, iAge;
945 gboolean bSrch, bMatch;
946 AddressDataSource *ds = NULL;
947 LdapServer *server = NULL;
948 LdapControl *ctl = NULL;
951 if (!ldapedit.window)
952 addressbook_edit_ldap_create(&cancelled);
953 gtk_notebook_set_current_page( GTK_NOTEBOOK(ldapedit.notebook), PAGE_BASIC );
954 gtk_widget_grab_focus(ldapedit.ok_btn);
955 gtk_widget_grab_focus(ldapedit.entry_name);
956 gtk_widget_show(ldapedit.window);
957 manage_window_set_transient(GTK_WINDOW(ldapedit.window));
959 edit_ldap_status_show( "" );
961 ds = ads->dataSource;
962 server = ds->rawDataSource;
963 edit_ldap_set_fields( server );
964 gtk_window_set_title(
965 GTK_WINDOW(ldapedit.window), _("Edit LDAP Server"));
968 edit_ldap_clear_fields();
969 gtk_window_set_title(
970 GTK_WINDOW(ldapedit.window), _("Add New LDAP Server"));
974 gtk_widget_hide(ldapedit.window);
975 if (cancelled == TRUE) return NULL;
977 sName = gtk_editable_get_chars(
978 GTK_EDITABLE(ldapedit.entry_name), 0, -1 );
979 sHost = gtk_editable_get_chars(
980 GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
981 sBase = gtk_editable_get_chars(
982 GTK_EDITABLE(ldapedit.entry_baseDN), 0, -1 );
983 sCrit = gtk_editable_get_chars(
984 GTK_EDITABLE(ldapedit.entry_criteria), 0, -1 );
985 sBind = gtk_editable_get_chars(
986 GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
987 sPass = gtk_editable_get_chars(
988 GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
989 iPort = gtk_spin_button_get_value_as_int(
990 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
991 iTime = gtk_spin_button_get_value_as_int(
992 GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
993 iMaxE = gtk_spin_button_get_value_as_int(
994 GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ) );
995 iAge = gtk_spin_button_get_value_as_int(
996 GTK_SPIN_BUTTON( ldapedit.spinbtn_queryage ) );
997 bSrch = gtk_toggle_button_get_active(
998 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch ) );
999 bMatch = gtk_toggle_button_get_active(
1000 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption ) );
1003 if( *sName == '\0' ) fin = TRUE;
1004 if( *sHost == '\0' ) fin = TRUE;
1010 server = ldapsvr_create();
1011 ds = addrindex_index_add_datasource(
1012 addrIndex, ADDR_IF_LDAP, server );
1013 ads = addressbook_create_ds_adapter(
1014 ds, ADDR_LDAP, NULL );
1016 ctl = server->control;
1017 addressbook_ads_set_name( ads, sName );
1018 ldapsvr_set_name( server, sName );
1019 ldapsvr_set_search_flag( server, bSrch );
1020 ldapctl_set_host( ctl, sHost );
1021 ldapctl_set_base_dn( ctl, sBase );
1022 ldapctl_set_bind_dn( ctl, sBind );
1023 ldapctl_set_bind_password( ctl, sPass );
1024 ldapctl_set_port( ctl, iPort );
1025 ldapctl_set_max_entries( ctl, iMaxE );
1026 ldapctl_set_timeout( ctl, iTime );
1027 ldapctl_set_max_query_age( ctl, iAge );
1028 ldapctl_set_matching_option(
1030 LDAPCTL_MATCH_CONTAINS : LDAPCTL_MATCH_BEGINWITH );
1032 /* Save attributes */
1033 editldap_parse_criteria( sCrit, ctl );
1046 #endif /* USE_LDAP */