295b1729832d96ede766f96d20620f0766e64540
[claws.git] / src / editldap_basedn.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 /*
20  * LDAP Base DN selection dialog.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #include "claws-features.h"
26 #endif
27
28 #ifdef USE_LDAP
29
30 #include "defs.h"
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtk.h>
36
37 #include "prefs_common.h"
38 #include "ldaputil.h"
39 #include "mgutils.h"
40 #include "gtkutils.h"
41 #include "manage_window.h"
42
43 static struct _LDAPEdit_basedn {
44         GtkWidget *window;
45         GtkWidget *host_label;
46         GtkWidget *port_label;
47         GtkWidget *basedn_entry;
48         GtkWidget *basedn_list;
49         GtkWidget *ok_btn;
50         GtkWidget *cancel_btn;
51         GtkWidget *statusbar;
52         gint status_cid;
53 } ldapedit_basedn;
54
55 static gboolean ldapedit_basedn_cancelled;
56 static gboolean ldapedit_basedn_bad_server;
57
58 /*
59 * Edit functions.
60 */
61 static void edit_ldap_bdn_status_show( gchar *msg ) {
62         if( ldapedit_basedn.statusbar != NULL ) {
63                 gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid );
64                 if( msg ) {
65                         gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid, msg );
66                 }
67         }
68 }
69
70 static gint edit_ldap_bdn_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
71         ldapedit_basedn_cancelled = TRUE;
72         gtk_main_quit();
73         return TRUE;
74 }
75
76 static gboolean edit_ldap_bdn_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
77         if (event && event->keyval == GDK_KEY_Escape) {
78                 ldapedit_basedn_cancelled = TRUE;
79                 gtk_main_quit();
80         }
81         return FALSE;
82 }
83
84 static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) {
85         ldapedit_basedn_cancelled = FALSE;
86         gtk_main_quit();
87 }
88
89 static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) {
90         ldapedit_basedn_cancelled = TRUE;
91         gtk_main_quit();
92 }
93
94 static void set_selected()
95 {
96         GtkWidget *entry = ldapedit_basedn.basedn_entry;
97         GtkWidget *view = ldapedit_basedn.basedn_list;
98         gchar *text;
99
100         if (entry == NULL || view == NULL)
101                 return;
102
103         text = gtkut_tree_view_get_selected_pointer(
104                         GTK_TREE_VIEW(view), 0, NULL, NULL, NULL);
105
106         if (text == NULL)
107                 return;
108
109         gtk_entry_set_text(GTK_ENTRY(entry), text);
110         g_free(text);
111 }
112
113 static void basedn_list_cursor_changed(GtkTreeView *view,
114                 gpointer user_data)
115 {
116         set_selected();
117 }
118
119 static void basedn_list_row_activated(GtkTreeView *view,
120                 GtkTreePath *path,
121                 GtkTreeViewColumn *column,
122                 gpointer user_data)
123 {
124         set_selected();
125         edit_ldap_bdn_ok(NULL, NULL);
126 }
127
128 static void edit_ldap_bdn_create(void) {
129         GtkWidget *window;
130         GtkWidget *vbox;
131         GtkWidget *table;
132         GtkWidget *label;
133         GtkWidget *host_label;
134         GtkWidget *port_label;
135         GtkWidget *basedn_list;
136         GtkWidget *vlbox;
137         GtkWidget *lwindow;
138         GtkWidget *basedn_entry;
139         GtkWidget *hbbox;
140         GtkWidget *hsep;
141         GtkWidget *ok_btn;
142         GtkWidget *cancel_btn;
143         GtkWidget *hsbox;
144         GtkWidget *statusbar;
145         gint top;
146         GtkListStore *store;
147         GtkTreeSelection *sel;
148         GtkTreeViewColumn *col;
149         GtkCellRenderer *rdr;
150
151         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editldap_basedn");
152         gtk_container_set_border_width(GTK_CONTAINER(window), 0);
153         gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP - Select Search Base"));
154         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
155         g_signal_connect(G_OBJECT(window), "delete_event",
156                          G_CALLBACK(edit_ldap_bdn_delete_event), NULL );
157         g_signal_connect(G_OBJECT(window), "key_press_event",
158                          G_CALLBACK(edit_ldap_bdn_key_pressed), NULL );
159
160         vbox = gtk_vbox_new(FALSE, 8);
161         gtk_container_add(GTK_CONTAINER(window), vbox);
162         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
163
164         table = gtk_table_new(3, 2, FALSE);
165         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
166         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
167         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
168         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
169
170         /* First row */
171         top = 0;
172         label = gtk_label_new(_("Hostname"));
173         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
174         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
175
176         host_label = gtk_label_new("");
177         gtk_table_attach(GTK_TABLE(table), host_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
178         gtk_misc_set_alignment(GTK_MISC(host_label), 0, 0.5);
179
180         /* Second row */
181         top = 1;
182         label = gtk_label_new(_("Port"));
183         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
184         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
185
186         port_label = gtk_label_new("");
187         gtk_table_attach(GTK_TABLE(table), port_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
188         gtk_misc_set_alignment(GTK_MISC(port_label), 0, 0.5);
189
190         /* Third row */
191         top = 2;
192         label = gtk_label_new(_("Search Base"));
193         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
194         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
195
196         basedn_entry = gtk_entry_new();
197         gtk_table_attach(GTK_TABLE(table), basedn_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
198
199         /* Basedn list */
200         vlbox = gtk_vbox_new(FALSE, 8);
201         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
202         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
203
204         lwindow = gtk_scrolled_window_new(NULL, NULL);
205         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow),
206                                        GTK_POLICY_AUTOMATIC,
207                                        GTK_POLICY_AUTOMATIC);
208         gtk_box_pack_start(GTK_BOX(vlbox), lwindow, TRUE, TRUE, 0);
209
210         store = gtk_list_store_new(1, G_TYPE_STRING, -1);
211
212         basedn_list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
213         g_object_unref(store);
214         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(basedn_list), TRUE);
215         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(basedn_list));
216         gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
217
218         rdr = gtk_cell_renderer_text_new();
219         col = gtk_tree_view_column_new_with_attributes(
220                         _("Available Search Base(s)"), rdr,
221                         "markup", 0, NULL);
222         gtk_tree_view_append_column(GTK_TREE_VIEW(basedn_list), col);
223
224         gtk_container_add(GTK_CONTAINER(lwindow), basedn_list);
225
226         g_signal_connect(G_OBJECT(basedn_list), "cursor-changed",
227                         G_CALLBACK(basedn_list_cursor_changed), NULL);
228         g_signal_connect(G_OBJECT(basedn_list), "row-activated",
229                         G_CALLBACK(basedn_list_row_activated), NULL);
230
231
232         /* Status line */
233         hsbox = gtk_hbox_new(FALSE, 0);
234         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
235         statusbar = gtk_statusbar_new();
236         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
237
238         /* Button panel */
239         gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
240                                       &cancel_btn, GTK_STOCK_CANCEL,
241                                       NULL, NULL);
242         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
243         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
244         gtk_widget_grab_default(ok_btn);
245
246         hsep = gtk_hseparator_new();
247         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
248
249         g_signal_connect(G_OBJECT(ok_btn), "clicked",
250                          G_CALLBACK(edit_ldap_bdn_ok), NULL);
251         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
252                          G_CALLBACK(edit_ldap_bdn_cancel), NULL);
253
254         gtk_widget_show_all(vbox);
255
256         ldapedit_basedn.window     = window;
257         ldapedit_basedn.host_label = host_label;
258         ldapedit_basedn.port_label = port_label;
259         ldapedit_basedn.basedn_entry = basedn_entry;
260         ldapedit_basedn.basedn_list  = basedn_list;
261         ldapedit_basedn.ok_btn     = ok_btn;
262         ldapedit_basedn.cancel_btn = cancel_btn;
263         ldapedit_basedn.statusbar  = statusbar;
264         ldapedit_basedn.status_cid =
265                 gtk_statusbar_get_context_id(
266                         GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" );
267 }
268
269 static void edit_ldap_bdn_load_data(
270         const gchar *hostName, const gint iPort, const gint tov,
271         const gchar* bindDN, const gchar *bindPW, int ssl, int tls )
272 {
273         gchar *sHost;
274         gchar *sMsg = NULL;
275         gchar sPort[20];
276         gboolean flgConn;
277         gboolean flgDN;
278         GList *baseDN = NULL;
279         GtkWidget *view = ldapedit_basedn.basedn_list;
280         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
281         GtkTreeIter iter;
282         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
283
284         edit_ldap_bdn_status_show( "" );
285
286         gtk_list_store_clear(GTK_LIST_STORE(model));
287
288         ldapedit_basedn_bad_server = TRUE;
289         flgConn = flgDN = FALSE;
290         sHost = g_strdup( hostName );
291         sprintf( sPort, "%d", iPort );
292         gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
293         gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
294         if( *sHost != '\0' ) {
295                 /* Test connection to server */
296                 if( ldaputil_test_connect( sHost, iPort, ssl, tls, tov ) ) {
297                         /* Attempt to read base DN */
298                         baseDN = ldaputil_read_basedn( sHost, iPort, bindDN, bindPW, tov, ssl, tls );
299                         if( baseDN ) {
300                                 GList *node = baseDN;
301
302                                 while( node ) {
303                                         gtk_list_store_append(GTK_LIST_STORE(model), &iter);
304                                         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
305                                                         0, (gchar *)node->data, -1);
306                                         node = g_list_next( node );
307                                         flgDN = TRUE;
308                                 }
309
310                                 /* Select the first line */
311                                 if (gtk_tree_model_get_iter_first(model, &iter))
312                                         gtk_tree_selection_select_iter(sel, &iter);
313
314                                 g_list_free_full( baseDN, g_free );
315                                 baseDN = node = NULL;
316                         }
317                         ldapedit_basedn_bad_server = FALSE;
318                         flgConn = TRUE;
319                 }
320         }
321         g_free( sHost );
322
323         /* Display appropriate message */
324         if( flgConn ) {
325                 if( ! flgDN ) {
326                         sMsg = _( "Could not read Search Base(s) from server - please set manually" );
327                 }
328         }
329         else {
330                 sMsg = _( "Could not connect to server" );
331         }
332         edit_ldap_bdn_status_show( sMsg );
333 }
334
335 gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov,
336                const gchar* bindDN, const gchar *bindPW, int ssl, int tls ) {
337         gchar *retVal = NULL;
338
339         ldapedit_basedn_cancelled = FALSE;
340         if( ! ldapedit_basedn.window ) edit_ldap_bdn_create();
341         gtk_widget_grab_focus(ldapedit_basedn.ok_btn);
342         gtk_widget_show(ldapedit_basedn.window);
343         manage_window_set_transient(GTK_WINDOW(ldapedit_basedn.window));
344         gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn.window), TRUE);
345         edit_ldap_bdn_status_show( "" );
346         edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW, ssl, tls );
347         gtk_widget_show(ldapedit_basedn.window);
348
349         gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN);
350
351         gtk_main();
352         gtk_widget_hide(ldapedit_basedn.window);
353         gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn.window), FALSE);
354         if( ldapedit_basedn_cancelled ) return NULL;
355         if( ldapedit_basedn_bad_server ) return NULL;
356
357         retVal = gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn.basedn_entry), 0, -1 );
358         g_strstrip( retVal );
359         if( *retVal == '\0' ) {
360                 g_free( retVal );
361                 retVal = NULL;
362         }
363         return retVal;
364 }
365
366 #endif /* USE_LDAP */
367
368 /*
369 * End of Source.
370 */
371