sync with sylpheed 0.5.3 release
[claws.git] / src / editldap_basedn.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 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 "syldap.h"
45
46 static struct _LDAPEdit_basedn {
47         GtkWidget *window;
48         GtkWidget *host_label;
49         GtkWidget *port_label;
50         GtkWidget *basedn_entry;
51         GtkWidget *basedn_list;
52         GtkWidget *ok_btn;
53         GtkWidget *cancel_btn;
54         GtkWidget *statusbar;
55         gint status_cid;
56 } ldapedit_basedn;
57
58 static gboolean ldapedit_basedn_cancelled;
59 static gboolean ldapedit_basedn_bad_server;
60
61 /*
62 * Edit functions.
63 */
64 void edit_ldap_bdn_status_show( gchar *msg ) {
65         if( ldapedit_basedn.statusbar != NULL ) {
66                 gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid );
67                 if( msg ) {
68                         gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid, msg );
69                 }
70         }
71 }
72
73 static gint edit_ldap_bdn_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
74         ldapedit_basedn_cancelled = TRUE;
75         gtk_main_quit();
76         return TRUE;
77 }
78
79 static void edit_ldap_bdn_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
80         if (event && event->keyval == GDK_Escape) {
81                 ldapedit_basedn_cancelled = TRUE;
82                 gtk_main_quit();
83         }
84 }
85
86 static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) {
87         ldapedit_basedn_cancelled = FALSE;
88         gtk_main_quit();
89 }
90
91 static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) {
92         ldapedit_basedn_cancelled = TRUE;
93         gtk_main_quit();
94 }
95
96 static void edit_ldap_bdn_list_select( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
97         gchar **text;
98         if( gtk_clist_get_text( clist, row, 0, text ) ) {
99                 if( *text ) {
100                         gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), *text );
101                 }
102         }
103 }
104
105 static void edit_ldap_bdn_list_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
106         if( ! event ) return;
107         if( event->button == 1 ) {
108                 if( event->type == GDK_2BUTTON_PRESS ) {
109                         ldapedit_basedn_cancelled = FALSE;
110                         gtk_main_quit();
111                 }
112         }
113 }
114
115 static void edit_ldap_bdn_create() {
116         GtkWidget *window;
117         GtkWidget *vbox;
118         GtkWidget *table;
119         GtkWidget *label;
120         GtkWidget *host_label;
121         GtkWidget *port_label;
122         GtkWidget *basedn_label;
123         GtkWidget *basedn_list;
124         GtkWidget *vlbox;
125         GtkWidget *lwindow;
126         GtkWidget *basedn_entry;
127         GtkWidget *hlbox;
128         GtkWidget *hbbox;
129         GtkWidget *hsep;
130         GtkWidget *ok_btn;
131         GtkWidget *cancel_btn;
132         GtkWidget *check_btn;
133         GtkWidget *file_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 = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" );
241 }
242
243 void edit_ldap_bdn_load_data( const gchar *hostName, const gint iPort, const gint tov, const gchar* bindDN,
244                const gchar *bindPW ) {
245         gchar *sHost;
246         gchar *sMsg = NULL;
247         gchar sPort[20];
248         gboolean flgConn;
249         gboolean flgDN;
250
251         edit_ldap_status_show( "" );
252         gtk_clist_clear(GTK_CLIST(ldapedit_basedn.basedn_list));
253         ldapedit_basedn_bad_server = TRUE;
254         flgConn = flgDN = FALSE;
255         sHost = g_strdup( hostName );
256         sprintf( sPort, "%d", iPort );
257         gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
258         gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
259         if( *sHost != '\0' ) {
260                 // Test connection to server
261                 if( syldap_test_connect_s( sHost, iPort ) ) {
262                         // Attempt to read base DN
263                         GList *baseDN = syldap_read_basedn_s( sHost, iPort, bindDN, bindPW, tov );
264                         if( baseDN ) {
265                                 GList *node = baseDN;
266                                 gchar *sBase[2];
267                                 sBase[1] = NULL;
268                                 while( node ) {
269                                         sBase[0] = g_strdup( node->data );
270                                         gtk_clist_append(GTK_CLIST(ldapedit_basedn.basedn_list), sBase);
271                                         node = g_list_next( node );
272                                         flgDN = TRUE;
273                                 }
274                                 mgu_free_dlist( baseDN );
275                                 baseDN = node = NULL;
276                                 sBase[0] = NULL;
277                         }
278                         ldapedit_basedn_bad_server = FALSE;
279                         flgConn = TRUE;
280                 }
281         }
282         g_free( sHost );
283
284         // Display appropriate message
285         if( flgConn ) {
286                 if( ! flgDN ) {
287                         sMsg = _( "Could not read Search Base(s) from server - please set manually" );
288                 }
289         }
290         else {
291                 sMsg = _( "Could not connect to server" );
292         }
293         edit_ldap_bdn_status_show( sMsg );
294 }
295
296 gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov,
297                const gchar* bindDN, const gchar *bindPW ) {
298         gchar *retVal = NULL;
299
300         ldapedit_basedn_cancelled = FALSE;
301         if( ! ldapedit_basedn.window ) edit_ldap_bdn_create();
302         gtk_widget_grab_focus(ldapedit_basedn.ok_btn);
303         gtk_widget_show(ldapedit_basedn.window);
304         manage_window_set_transient(GTK_WINDOW(ldapedit_basedn.window));
305
306         edit_ldap_bdn_status_show( "" );
307         edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW );
308         gtk_widget_show(ldapedit_basedn.window);
309
310 //      sprintf( sPort, "%d", port );
311 //      gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
312 //      gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
313         gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN);
314
315         gtk_main();
316         gtk_widget_hide(ldapedit_basedn.window);
317         if( ldapedit_basedn_cancelled ) return NULL;
318 //      if( cancelled == TRUE ) return NULL;
319         if( ldapedit_basedn_bad_server ) return NULL;
320
321         retVal = g_strdup( 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