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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Edit LDAP address book data.
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtkwindow.h>
36 #include <gtk/gtksignal.h>
37 #include <gtk/gtklabel.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtktable.h>
40 #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_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
343 &cancel_btn, GTK_STOCK_CANCEL,
345 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
346 gtk_widget_grab_default(ok_btn);
348 g_signal_connect(G_OBJECT(ok_btn), "clicked",
349 G_CALLBACK(edit_ldap_ok), cancelled);
350 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
351 G_CALLBACK(edit_ldap_cancel), cancelled);
353 gtk_widget_show_all(vbox);
355 ldapedit.window = window;
356 ldapedit.notebook = notebook;
357 ldapedit.ok_btn = ok_btn;
358 ldapedit.cancel_btn = cancel_btn;
359 ldapedit.statusbar = statusbar;
360 ldapedit.status_cid =
361 gtk_statusbar_get_context_id(
362 GTK_STATUSBAR(statusbar), "Edit LDAP Server Dialog" );
365 static void addressbook_edit_ldap_page_basic( gint pageNum, gchar *pageLbl ) {
369 GtkWidget *entry_name;
370 GtkWidget *entry_server;
371 GtkWidget *hbox_spin;
372 GtkObject *spinbtn_port_adj;
373 GtkWidget *spinbtn_port;
374 GtkWidget *entry_baseDN;
375 GtkWidget *check_btn;
376 GtkWidget *lookdn_btn;
377 GtkTooltips *toolTip;
380 vbox = gtk_vbox_new( FALSE, 8 );
381 gtk_widget_show( vbox );
382 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
384 label = gtk_label_new( pageLbl );
385 gtk_widget_show( label );
386 gtk_notebook_set_tab_label(
387 GTK_NOTEBOOK( ldapedit.notebook ),
388 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
390 table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
391 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
392 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
393 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
394 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
398 label = gtk_label_new(_("Name"));
399 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
400 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
402 entry_name = gtk_entry_new();
403 gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1),
404 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
406 toolTip = gtk_tooltips_new();
407 gtk_tooltips_set_tip( toolTip, entry_name, _(
408 "A name that you wish to call the server." ),
413 label = gtk_label_new(_("Hostname"));
414 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
415 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
417 entry_server = gtk_entry_new();
418 gtk_table_attach(GTK_TABLE(table), entry_server, 1, 2, top, (top + 1),
419 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
421 toolTip = gtk_tooltips_new();
422 gtk_tooltips_set_tip( toolTip, entry_server, _(
423 "This is the hostname of the server. For example, " \
424 "\"ldap.mydomain.com\" may be appropriate for the " \
425 "\"mydomain.com\" organization. An IP address may also be " \
426 "used. You may specify \"localhost\" if running an LDAP " \
427 "server on the same computer as Sylpheed." ),
432 label = gtk_label_new(_("Port"));
433 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
434 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
436 hbox_spin = gtk_hbox_new (FALSE, 8);
437 spinbtn_port_adj = gtk_adjustment_new (389, 1, 65535, 100, 1000, 1000);
438 spinbtn_port = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_port_adj), 1, 0);
439 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_port, FALSE, FALSE, 0);
440 gtk_widget_set_size_request (spinbtn_port, 64, -1);
441 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_port), TRUE);
442 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
443 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
445 toolTip = gtk_tooltips_new();
446 gtk_tooltips_set_tip( toolTip, spinbtn_port, _(
447 "The port number that the server listens on. Port 389 is " \
451 check_btn = gtk_button_new_with_label( _(" Check Server "));
452 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
454 toolTip = gtk_tooltips_new();
455 gtk_tooltips_set_tip( toolTip, check_btn, _(
456 "Press this button to test the connection to the server." ),
461 label = gtk_label_new(_("Search Base"));
462 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
463 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
465 entry_baseDN = gtk_entry_new();
466 gtk_table_attach(GTK_TABLE(table), entry_baseDN, 1, 2, top, (top + 1),
467 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
469 toolTip = gtk_tooltips_new();
470 gtk_tooltips_set_tip( toolTip, entry_baseDN, _(
471 "This specifies the name of the directory to be searched " \
472 "on the server. Examples include:\n" \
473 " dc=sylpheed,dc=org\n" \
474 " ou=people,dc=domainname,dc=com\n" \
475 " o=Organization Name,c=Country\n"
479 lookdn_btn = gtkut_get_browse_file_btn(_("_Browse"));
480 gtk_table_attach(GTK_TABLE(table), lookdn_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
482 toolTip = gtk_tooltips_new();
483 gtk_tooltips_set_tip( toolTip, lookdn_btn, _(
484 "Press this button to lookup the name of available " \
485 "directory names on the server." ),
488 /* Signal handlers */
489 g_signal_connect(G_OBJECT(check_btn), "clicked",
490 G_CALLBACK(edit_ldap_server_check), NULL);
491 g_signal_connect(G_OBJECT(lookdn_btn), "clicked",
492 G_CALLBACK(edit_ldap_basedn_select), NULL);
495 gtk_widget_show_all(vbox);
497 ldapedit.entry_name = entry_name;
498 ldapedit.entry_server = entry_server;
499 ldapedit.spinbtn_port = spinbtn_port;
500 ldapedit.entry_baseDN = entry_baseDN;
503 static void addressbook_edit_ldap_page_search( gint pageNum, gchar *pageLbl ) {
507 GtkWidget *entry_criteria;
508 GtkWidget *hbox_spin;
509 GtkObject *spinbtn_queryage_adj;
510 GtkWidget *spinbtn_queryage;
511 GtkWidget *check_dynsearch;
512 GtkWidget *check_matchoption;
513 GtkWidget *reset_btn;
514 GtkTooltips *toolTip;
517 vbox = gtk_vbox_new( FALSE, 8 );
518 gtk_widget_show( vbox );
519 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
521 label = gtk_label_new( pageLbl );
522 gtk_widget_show( label );
523 gtk_notebook_set_tab_label(
524 GTK_NOTEBOOK( ldapedit.notebook ),
525 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
527 table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
528 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
529 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
530 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
531 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
535 label = gtk_label_new(_("Search Attributes"));
536 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
537 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
539 entry_criteria = gtk_entry_new();
540 gtk_table_attach(GTK_TABLE(table), entry_criteria, 1, 2, top, (top + 1),
541 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
543 toolTip = gtk_tooltips_new();
544 gtk_tooltips_set_tip( toolTip, entry_criteria, _(
545 "A list of LDAP attribute names that should be searched " \
546 "when attempting to find a name or address." ),
549 reset_btn = gtk_button_new_with_label( _(" Defaults "));
550 gtk_table_attach(GTK_TABLE(table), reset_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
552 toolTip = gtk_tooltips_new();
553 gtk_tooltips_set_tip( toolTip, reset_btn, _(
554 "This resets the attribute names to a default value " \
555 "that should find most names and addresses during a " \
556 "name or address search process." ),
561 label = gtk_label_new(_("Max Query Age (secs)"));
562 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
563 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
565 hbox_spin = gtk_hbox_new (FALSE, 8);
566 spinbtn_queryage_adj = gtk_adjustment_new(
567 LDAPCTL_DFL_QUERY_AGE, 1, LDAPCTL_MAX_QUERY_AGE, 10, 1000, 1000 );
568 spinbtn_queryage = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_queryage_adj), 1, 0);
569 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_queryage, FALSE, FALSE, 0);
570 gtk_widget_set_size_request (spinbtn_queryage, 64, -1);
571 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_queryage), TRUE);
572 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
573 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
575 toolTip = gtk_tooltips_new();
576 gtk_tooltips_set_tip( toolTip, spinbtn_queryage, _(
577 "This defines the maximum period of time (in seconds) that " \
578 "an address search result is valid for address completion " \
579 "purposes. Search results are stored in a cache until this " \
580 "period of time has passed and then retired. This will " \
581 "improve the response time when attempting to search for " \
582 "the same name or address on subsequent address completion " \
583 "requests. The cache will be searched in preference to " \
584 "performing a new server search request. The default value " \
585 "of 600 seconds (10 minutes), should be sufficient for most " \
586 "servers. A larger value will reduce the search time for " \
587 "subsequent searches. This is useful for servers that have " \
588 "slow response times at the expense of more memory to cache " \
594 check_dynsearch = gtk_check_button_new_with_label(
595 _("Include server in dynamic search") );
596 gtk_table_attach(GTK_TABLE(table), check_dynsearch, 1, 3, top, (top + 1),
597 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
599 toolTip = gtk_tooltips_new();
600 gtk_tooltips_set_tip( toolTip, check_dynsearch, _(
601 "Check this option to include this server for dynamic " \
602 "searches when using address completion." ),
607 check_matchoption = gtk_check_button_new_with_label(
608 _("Match names 'containing' search term") );
609 gtk_table_attach(GTK_TABLE(table), check_matchoption, 1, 3, top, (top + 1),
610 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
612 toolTip = gtk_tooltips_new();
613 gtk_tooltips_set_tip( toolTip, check_matchoption, _(
614 "Searches for names and addresses can be performed either " \
615 "using \"begins-with\" or \"contains\" search term. Check " \
616 "this option to perform a \"contains\" search; this type of " \
617 "search usually takes longer to complete. Note that for " \
618 "performance reasons, address completion uses " \
619 "\"begins-with\" for all searches against other address " \
624 /* Signal handlers */
625 g_signal_connect(G_OBJECT(reset_btn), "clicked",
626 G_CALLBACK(edit_ldap_search_reset), NULL);
629 gtk_widget_show_all(vbox);
631 ldapedit.entry_criteria = entry_criteria;
632 ldapedit.spinbtn_queryage = spinbtn_queryage;
633 ldapedit.check_dynsearch = check_dynsearch;
634 ldapedit.check_matchoption = check_matchoption;
637 static void addressbook_edit_ldap_page_extended( gint pageNum, gchar *pageLbl ) {
641 GtkWidget *entry_bindDN;
642 GtkWidget *entry_bindPW;
643 GtkWidget *hbox_spin;
644 GtkObject *spinbtn_timeout_adj;
645 GtkWidget *spinbtn_timeout;
646 GtkObject *spinbtn_maxentry_adj;
647 GtkWidget *spinbtn_maxentry;
648 GtkTooltips *toolTip;
651 vbox = gtk_vbox_new( FALSE, 8 );
652 gtk_widget_show( vbox );
653 gtk_container_add( GTK_CONTAINER( ldapedit.notebook ), vbox );
655 label = gtk_label_new( pageLbl );
656 gtk_widget_show( label );
657 gtk_notebook_set_tab_label(
658 GTK_NOTEBOOK( ldapedit.notebook ),
659 gtk_notebook_get_nth_page( GTK_NOTEBOOK( ldapedit.notebook ), pageNum ), label );
661 table = gtk_table_new( LDAPEDIT_TABLE_ROWS, LDAPEDIT_TABLE_COLS, FALSE);
662 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
663 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
664 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
665 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
669 label = gtk_label_new(_("Bind DN"));
670 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
671 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
673 entry_bindDN = gtk_entry_new();
674 gtk_table_attach(GTK_TABLE(table), entry_bindDN, 1, 2, top, (top + 1),
675 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
677 toolTip = gtk_tooltips_new();
678 gtk_tooltips_set_tip( toolTip, entry_bindDN, _(
679 "The LDAP user account name to be used to connect to the server. " \
680 "This is usually only used for protected servers. This name " \
681 "is typically formatted as: \"cn=user,dc=sylpheed,dc=com\". " \
682 "This is usually left empty when performing a search." ),
687 label = gtk_label_new(_("Bind Password"));
688 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
689 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
691 entry_bindPW = gtk_entry_new();
692 gtk_table_attach(GTK_TABLE(table), entry_bindPW, 1, 2, top, (top + 1),
693 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
694 gtk_entry_set_visibility(GTK_ENTRY(entry_bindPW), FALSE);
696 toolTip = gtk_tooltips_new();
697 gtk_tooltips_set_tip( toolTip, entry_bindPW, _(
698 "The password to be used when connecting as the \"Bind DN\" " \
704 label = gtk_label_new(_("Timeout (secs)"));
705 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
706 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
708 hbox_spin = gtk_hbox_new (FALSE, 8);
709 spinbtn_timeout_adj = gtk_adjustment_new (0, 0, 300, 1, 10, 10);
710 spinbtn_timeout = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_timeout_adj), 1, 0);
711 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_timeout, FALSE, FALSE, 0);
712 gtk_widget_set_size_request (spinbtn_timeout, 64, -1);
713 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_timeout), TRUE);
714 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
715 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
717 toolTip = gtk_tooltips_new();
718 gtk_tooltips_set_tip( toolTip, spinbtn_timeout, _(
719 "The timeout period in seconds." ), NULL );
723 label = gtk_label_new(_("Maximum Entries"));
724 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
725 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
727 hbox_spin = gtk_hbox_new (FALSE, 8);
728 spinbtn_maxentry_adj = gtk_adjustment_new (0, 0, 500, 1, 10, 10);
729 spinbtn_maxentry = gtk_spin_button_new(GTK_ADJUSTMENT (spinbtn_maxentry_adj), 1, 0);
730 gtk_box_pack_start (GTK_BOX (hbox_spin), spinbtn_maxentry, FALSE, FALSE, 0);
731 gtk_widget_set_size_request (spinbtn_maxentry, 64, -1);
732 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbtn_maxentry), TRUE);
733 gtk_table_attach(GTK_TABLE(table), hbox_spin, 1, 2, top, (top + 1),
734 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
736 toolTip = gtk_tooltips_new();
737 gtk_tooltips_set_tip( toolTip, spinbtn_maxentry, _(
738 "The maximum number of entries that should be returned " \
739 "in the search result." ),
743 gtk_widget_show_all(vbox);
745 ldapedit.entry_bindDN = entry_bindDN;
746 ldapedit.entry_bindPW = entry_bindPW;
747 ldapedit.spinbtn_timeout = spinbtn_timeout;
748 ldapedit.spinbtn_maxentry = spinbtn_maxentry;
751 static void addressbook_edit_ldap_create( gboolean *cancelled ) {
753 addressbook_edit_ldap_dialog_create( cancelled );
754 addressbook_edit_ldap_page_basic( page++, _( "Basic" ) );
755 addressbook_edit_ldap_page_search( page++, _( "Search" ) );
756 addressbook_edit_ldap_page_extended( page++, _( "Extended" ) );
757 gtk_widget_show_all( ldapedit.window );
760 void edit_ldap_set_optmenu( GtkOptionMenu *optmenu, const gint value ) {
767 g_return_if_fail(optmenu != NULL);
769 menu = gtk_option_menu_get_menu(optmenu);
770 for( cur = GTK_MENU_SHELL(menu)->children; cur != NULL; cur = cur->next ) {
771 menuitem = GTK_WIDGET(cur->data);
772 menuVal = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), "user_data"));
773 if( menuVal == value ) {
774 gtk_option_menu_set_history(optmenu, n);
779 gtk_option_menu_set_history(optmenu, 0);
782 gint edit_ldap_get_optmenu( GtkOptionMenu *optmenu ) {
786 g_return_val_if_fail(optmenu != NULL, -1);
788 menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(optmenu));
789 menuitem = gtk_menu_get_active(GTK_MENU(menu));
790 return GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), "user_data"));
794 * Format criteria list for display.
795 * \param ctl Control object.
796 * \return Formatted string, or <i>NULL</i> if no attributes found.
798 static gchar *editldap_build_criteria_list( const LdapControl *ctl ) {
803 node = ldapctl_get_criteria_list( ctl );
805 gchar *attr = node->data;
807 tmp = g_strdup_printf( "%s, %s", str, attr );
813 str = g_strdup( attr );
815 node = g_list_next( node );
822 * Parse out individual attribute names from criteria string.
823 * \param criteria Criteria string.
824 * \ctl Control object.
826 static void editldap_parse_criteria( gchar *criteria, LdapControl *ctl ) {
831 /* Replace delimiters with spaces */
834 if( *ptr == ',' || *ptr == ';' || *ptr == '|' )
840 ldapctl_criteria_list_clear( ctl );
841 splitStr = g_strsplit( criteria, " ", 0 );
846 ldapctl_criteria_list_add( ctl, splitStr[i] );
854 g_strfreev( splitStr );
858 * Clear entry fields to reasonable defaults (for a new server entry).
860 static void edit_ldap_clear_fields( void ) {
862 GTK_ENTRY(ldapedit.entry_name), ADDRESSBOOK_GUESS_LDAP_NAME );
864 GTK_ENTRY(ldapedit.entry_server), ADDRESSBOOK_GUESS_LDAP_SERVER );
865 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_baseDN), "");
866 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindDN), "");
867 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_bindPW), "");
868 gtk_spin_button_set_value(
869 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ), LDAPCTL_DFL_PORT );
870 gtk_spin_button_set_value(
871 GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ), LDAPCTL_DFL_TIMEOUT );
872 gtk_spin_button_set_value(
873 GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ), LDAPCTL_DFL_TIMEOUT );
875 GTK_ENTRY(ldapedit.entry_criteria), LDAPCTL_DFL_ATTR_LIST );
876 gtk_spin_button_set_value(
877 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), LDAPCTL_DFL_QUERY_AGE );
878 gtk_toggle_button_set_active(
879 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), TRUE );
880 gtk_toggle_button_set_active(
881 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption), FALSE );
885 * Load entry fields from server control data.
886 * \param server Server object.
888 static void edit_ldap_set_fields( LdapServer *server ) {
892 if( ldapsvr_get_name( server ) )
893 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_name),
894 ldapsvr_get_name( server ) );
896 ctl = server->control;
899 GTK_ENTRY(ldapedit.entry_server), ctl->hostName);
902 GTK_ENTRY(ldapedit.entry_baseDN), ctl->baseDN );
905 GTK_ENTRY(ldapedit.entry_bindDN), ctl->bindDN );
908 GTK_ENTRY(ldapedit.entry_bindPW), ctl->bindPass );
909 gtk_spin_button_set_value(
910 GTK_SPIN_BUTTON(ldapedit.spinbtn_port), ctl->port );
911 gtk_spin_button_set_value(
912 GTK_SPIN_BUTTON(ldapedit.spinbtn_timeout), ctl->timeOut );
913 gtk_spin_button_set_value(
914 GTK_SPIN_BUTTON(ldapedit.spinbtn_maxentry), ctl->maxEntries );
916 /* Format criteria */
917 crit = editldap_build_criteria_list( ctl );
919 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), crit );
923 gtk_entry_set_text(GTK_ENTRY(ldapedit.entry_criteria), "" );
925 gtk_spin_button_set_value(
926 GTK_SPIN_BUTTON(ldapedit.spinbtn_queryage), ctl->maxQueryAge );
927 gtk_toggle_button_set_active(
928 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch), server->searchFlag );
929 gtk_toggle_button_set_active(
930 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption),
931 ( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) );
935 * Edit LDAP server datasource that appears addressbook.
936 * \param addrIndex Address index object.
937 * \param ads Data source adapter.
938 * \return Update data source adapter, or <code>NULL</code> if user cancelled
941 AdapterDSource *addressbook_edit_ldap(
942 AddressIndex *addrIndex, AdapterDSource *ads )
944 static gboolean cancelled;
945 gchar *sName, *sHost, *sBase, *sBind, *sPass, *sCrit;
946 gint iPort, iMaxE, iTime, iAge;
947 gboolean bSrch, bMatch;
948 AddressDataSource *ds = NULL;
949 LdapServer *server = NULL;
950 LdapControl *ctl = NULL;
953 if (!ldapedit.window)
954 addressbook_edit_ldap_create(&cancelled);
955 gtk_notebook_set_current_page( GTK_NOTEBOOK(ldapedit.notebook), PAGE_BASIC );
956 gtk_widget_grab_focus(ldapedit.ok_btn);
957 gtk_widget_grab_focus(ldapedit.entry_name);
958 gtk_widget_show(ldapedit.window);
959 manage_window_set_transient(GTK_WINDOW(ldapedit.window));
961 edit_ldap_status_show( "" );
963 ds = ads->dataSource;
964 server = ds->rawDataSource;
965 edit_ldap_set_fields( server );
966 gtk_window_set_title(
967 GTK_WINDOW(ldapedit.window), _("Edit LDAP Server"));
970 edit_ldap_clear_fields();
971 gtk_window_set_title(
972 GTK_WINDOW(ldapedit.window), _("Add New LDAP Server"));
976 gtk_widget_hide(ldapedit.window);
977 if (cancelled == TRUE) return NULL;
979 sName = gtk_editable_get_chars(
980 GTK_EDITABLE(ldapedit.entry_name), 0, -1 );
981 sHost = gtk_editable_get_chars(
982 GTK_EDITABLE(ldapedit.entry_server), 0, -1 );
983 sBase = gtk_editable_get_chars(
984 GTK_EDITABLE(ldapedit.entry_baseDN), 0, -1 );
985 sCrit = gtk_editable_get_chars(
986 GTK_EDITABLE(ldapedit.entry_criteria), 0, -1 );
987 sBind = gtk_editable_get_chars(
988 GTK_EDITABLE(ldapedit.entry_bindDN), 0, -1 );
989 sPass = gtk_editable_get_chars(
990 GTK_EDITABLE(ldapedit.entry_bindPW), 0, -1 );
991 iPort = gtk_spin_button_get_value_as_int(
992 GTK_SPIN_BUTTON( ldapedit.spinbtn_port ) );
993 iTime = gtk_spin_button_get_value_as_int(
994 GTK_SPIN_BUTTON( ldapedit.spinbtn_timeout ) );
995 iMaxE = gtk_spin_button_get_value_as_int(
996 GTK_SPIN_BUTTON( ldapedit.spinbtn_maxentry ) );
997 iAge = gtk_spin_button_get_value_as_int(
998 GTK_SPIN_BUTTON( ldapedit.spinbtn_queryage ) );
999 bSrch = gtk_toggle_button_get_active(
1000 GTK_TOGGLE_BUTTON( ldapedit.check_dynsearch ) );
1001 bMatch = gtk_toggle_button_get_active(
1002 GTK_TOGGLE_BUTTON( ldapedit.check_matchoption ) );
1005 if( *sName == '\0' ) fin = TRUE;
1006 if( *sHost == '\0' ) fin = TRUE;
1012 server = ldapsvr_create();
1013 ds = addrindex_index_add_datasource(
1014 addrIndex, ADDR_IF_LDAP, server );
1015 ads = addressbook_create_ds_adapter(
1016 ds, ADDR_LDAP, NULL );
1018 ctl = server->control;
1019 addressbook_ads_set_name( ads, sName );
1020 ldapsvr_set_name( server, sName );
1021 ldapsvr_set_search_flag( server, bSrch );
1022 ldapctl_set_host( ctl, sHost );
1023 ldapctl_set_base_dn( ctl, sBase );
1024 ldapctl_set_bind_dn( ctl, sBind );
1025 ldapctl_set_bind_password( ctl, sPass );
1026 ldapctl_set_port( ctl, iPort );
1027 ldapctl_set_max_entries( ctl, iMaxE );
1028 ldapctl_set_timeout( ctl, iTime );
1029 ldapctl_set_max_query_age( ctl, iAge );
1030 ldapctl_set_matching_option(
1032 LDAPCTL_MATCH_CONTAINS : LDAPCTL_MATCH_BEGINWITH );
1034 /* Save attributes */
1035 editldap_parse_criteria( sCrit, ctl );
1048 #endif /* USE_LDAP */