add credit for translations
[claws.git] / src / editldap_basedn.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  * LDAP Base DN selection dialog.
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/gtkhbox.h>
37 #include <gtk/gtklabel.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkhbbox.h>
40 #include <gtk/gtkbutton.h>
41
42 #include "intl.h"
43 #include "prefs_common.h"
44 #include "ldaputil.h"
45 #include "mgutils.h"
46 #include "gtkutils.h"
47 #include "manage_window.h"
48
49 static struct _LDAPEdit_basedn {
50         GtkWidget *window;
51         GtkWidget *host_label;
52         GtkWidget *port_label;
53         GtkWidget *basedn_entry;
54         GtkWidget *basedn_list;
55         GtkWidget *ok_btn;
56         GtkWidget *cancel_btn;
57         GtkWidget *statusbar;
58         gint status_cid;
59 } ldapedit_basedn;
60
61 static gboolean ldapedit_basedn_cancelled;
62 static gboolean ldapedit_basedn_bad_server;
63
64 /*
65 * Edit functions.
66 */
67 static void edit_ldap_bdn_status_show( gchar *msg ) {
68         if( ldapedit_basedn.statusbar != NULL ) {
69                 gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid );
70                 if( msg ) {
71                         gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid, msg );
72                 }
73         }
74 }
75
76 static gint edit_ldap_bdn_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
77         ldapedit_basedn_cancelled = TRUE;
78         gtk_main_quit();
79         return TRUE;
80 }
81
82 static void edit_ldap_bdn_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
83         if (event && event->keyval == GDK_Escape) {
84                 ldapedit_basedn_cancelled = TRUE;
85                 gtk_main_quit();
86         }
87 }
88
89 static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) {
90         ldapedit_basedn_cancelled = FALSE;
91         gtk_main_quit();
92 }
93
94 static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) {
95         ldapedit_basedn_cancelled = TRUE;
96         gtk_main_quit();
97 }
98
99 static void edit_ldap_bdn_list_select( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
100         gchar *text = NULL;
101
102         if( gtk_clist_get_text( clist, row, 0, &text ) ) {
103                 if( text ) {
104                         gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), text );
105                 }
106         }
107 }
108
109 static void edit_ldap_bdn_list_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
110         if( ! event ) return;
111         if( event->button == 1 ) {
112                 if( event->type == GDK_2BUTTON_PRESS ) {
113                         ldapedit_basedn_cancelled = FALSE;
114                         gtk_main_quit();
115                 }
116         }
117 }
118
119 static void edit_ldap_bdn_create(void) {
120         GtkWidget *window;
121         GtkWidget *vbox;
122         GtkWidget *table;
123         GtkWidget *label;
124         GtkWidget *host_label;
125         GtkWidget *port_label;
126         GtkWidget *basedn_list;
127         GtkWidget *vlbox;
128         GtkWidget *lwindow;
129         GtkWidget *basedn_entry;
130         GtkWidget *hbbox;
131         GtkWidget *hsep;
132         GtkWidget *ok_btn;
133         GtkWidget *cancel_btn;
134         GtkWidget *hsbox;
135         GtkWidget *statusbar;
136         gint top;
137
138         window = gtk_window_new(GTK_WINDOW_DIALOG);
139         gtk_widget_set_usize(window, 300, 270);
140         gtk_container_set_border_width(GTK_CONTAINER(window), 0);
141         gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP - Select Search Base"));
142         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
143         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
144         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
145                            GTK_SIGNAL_FUNC(edit_ldap_bdn_delete_event), NULL );
146         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
147                            GTK_SIGNAL_FUNC(edit_ldap_bdn_key_pressed), NULL );
148
149         vbox = gtk_vbox_new(FALSE, 8);
150         gtk_container_add(GTK_CONTAINER(window), vbox);
151         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
152
153         table = gtk_table_new(3, 2, FALSE);
154         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
155         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
156         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
157         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
158
159         /* First row */
160         top = 0;
161         label = gtk_label_new(_("Hostname"));
162         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
163         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
164
165         host_label = gtk_label_new("");
166         gtk_table_attach(GTK_TABLE(table), host_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
167         gtk_misc_set_alignment(GTK_MISC(host_label), 0, 0.5);
168
169         /* Second row */
170         top = 1;
171         label = gtk_label_new(_("Port"));
172         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
173         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
174
175         port_label = gtk_label_new("");
176         gtk_table_attach(GTK_TABLE(table), port_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
177         gtk_misc_set_alignment(GTK_MISC(port_label), 0, 0.5);
178
179         /* Third row */
180         top = 2;
181         label = gtk_label_new(_("Search Base"));
182         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
183         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
184
185         basedn_entry = gtk_entry_new();
186         gtk_table_attach(GTK_TABLE(table), basedn_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
187
188         /* Basedn list */
189         vlbox = gtk_vbox_new(FALSE, 8);
190         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
191         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
192
193         lwindow = gtk_scrolled_window_new(NULL, NULL);
194         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow),
195                                        GTK_POLICY_AUTOMATIC,
196                                        GTK_POLICY_ALWAYS);
197         gtk_box_pack_start(GTK_BOX(vlbox), lwindow, TRUE, TRUE, 0);
198
199         basedn_list = gtk_clist_new(1);
200         gtk_container_add(GTK_CONTAINER(lwindow), basedn_list);
201         gtk_clist_column_titles_show( GTK_CLIST(basedn_list) );
202         gtk_clist_set_column_title( GTK_CLIST(basedn_list), 0, _( "Available Search Base(s)" ) );
203         gtk_clist_set_selection_mode(GTK_CLIST(basedn_list), GTK_SELECTION_BROWSE);
204
205         /* Status line */
206         hsbox = gtk_hbox_new(FALSE, 0);
207         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
208         statusbar = gtk_statusbar_new();
209         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
210
211         /* Button panel */
212         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
213                                 &cancel_btn, _("Cancel"), NULL, NULL);
214         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
215         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
216         gtk_widget_grab_default(ok_btn);
217
218         hsep = gtk_hseparator_new();
219         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
220
221         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
222                            GTK_SIGNAL_FUNC(edit_ldap_bdn_ok), NULL);
223         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
224                            GTK_SIGNAL_FUNC(edit_ldap_bdn_cancel), NULL);
225         gtk_signal_connect(GTK_OBJECT(basedn_list), "select_row",
226                            GTK_SIGNAL_FUNC(edit_ldap_bdn_list_select), NULL);
227         gtk_signal_connect(GTK_OBJECT(basedn_list), "button_press_event",
228                            GTK_SIGNAL_FUNC(edit_ldap_bdn_list_button), NULL);
229
230         gtk_widget_show_all(vbox);
231
232         ldapedit_basedn.window     = window;
233         ldapedit_basedn.host_label = host_label;
234         ldapedit_basedn.port_label = port_label;
235         ldapedit_basedn.basedn_entry = basedn_entry;
236         ldapedit_basedn.basedn_list  = basedn_list;
237         ldapedit_basedn.ok_btn     = ok_btn;
238         ldapedit_basedn.cancel_btn = cancel_btn;
239         ldapedit_basedn.statusbar  = statusbar;
240         ldapedit_basedn.status_cid =
241                 gtk_statusbar_get_context_id(
242                         GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" );
243 }
244
245 void edit_ldap_bdn_load_data(
246         const gchar *hostName, const gint iPort, const gint tov,
247         const gchar* bindDN, const gchar *bindPW )
248 {
249         gchar *sHost;
250         gchar *sMsg = NULL;
251         gchar sPort[20];
252         gboolean flgConn;
253         gboolean flgDN;
254         GList *baseDN = NULL;
255
256         edit_ldap_bdn_status_show( "" );
257         gtk_clist_clear(GTK_CLIST(ldapedit_basedn.basedn_list));
258         ldapedit_basedn_bad_server = TRUE;
259         flgConn = flgDN = FALSE;
260         sHost = g_strdup( hostName );
261         sprintf( sPort, "%d", iPort );
262         gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
263         gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
264         if( *sHost != '\0' ) {
265                 /* Test connection to server */
266                 if( ldaputil_test_connect( sHost, iPort ) ) {
267                         /* Attempt to read base DN */
268                         baseDN = ldaputil_read_basedn( sHost, iPort, bindDN, bindPW, tov );
269                         if( baseDN ) {
270                                 GList *node = baseDN;
271                                 gchar *text[2] = { NULL, NULL };
272
273                                 while( node ) {
274                                         text[0] = (gchar *)node->data;
275                                         gtk_clist_append(GTK_CLIST(ldapedit_basedn.basedn_list), text);
276                                         node = g_list_next( node );
277                                         flgDN = TRUE;
278                                 }
279                                 mgu_free_dlist( baseDN );
280                                 baseDN = node = NULL;
281                         }
282                         ldapedit_basedn_bad_server = FALSE;
283                         flgConn = TRUE;
284                 }
285         }
286         g_free( sHost );
287
288         /* Display appropriate message */
289         if( flgConn ) {
290                 if( ! flgDN ) {
291                         sMsg = _( "Could not read Search Base(s) from server - please set manually" );
292                 }
293         }
294         else {
295                 sMsg = _( "Could not connect to server" );
296         }
297         edit_ldap_bdn_status_show( sMsg );
298 }
299
300 gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov,
301                const gchar* bindDN, const gchar *bindPW ) {
302         gchar *retVal = NULL;
303
304         ldapedit_basedn_cancelled = FALSE;
305         if( ! ldapedit_basedn.window ) edit_ldap_bdn_create();
306         gtk_widget_grab_focus(ldapedit_basedn.ok_btn);
307         gtk_widget_show(ldapedit_basedn.window);
308         manage_window_set_transient(GTK_WINDOW(ldapedit_basedn.window));
309
310         edit_ldap_bdn_status_show( "" );
311         edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW );
312         gtk_widget_show(ldapedit_basedn.window);
313
314         gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN);
315
316         gtk_main();
317         gtk_widget_hide(ldapedit_basedn.window);
318         if( ldapedit_basedn_cancelled ) return NULL;
319         if( ldapedit_basedn_bad_server ) return NULL;
320
321         retVal = gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn.basedn_entry), 0, -1 );
322         g_strchomp( retVal ); g_strchug( retVal );
323         if( *retVal == '\0' ) {
324                 g_free( retVal );
325                 retVal = NULL;
326         }
327         return retVal;
328 }
329
330 #endif /* USE_LDAP */
331
332 /*
333 * End of Source.
334 */
335