Fix truncated input widgets in preferences window
[claws.git] / src / editldap_basedn.c
blobbcc5681e0644334f02be2ff16fbf8aeacb10ef76
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
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.
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, see <http://www.gnu.org/licenses/>.
20 * LDAP Base DN selection dialog.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #include "claws-features.h"
26 #endif
28 #ifdef USE_LDAP
30 #include "defs.h"
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtk.h>
37 #include "prefs_common.h"
38 #include "ldaputil.h"
39 #include "mgutils.h"
40 #include "gtkutils.h"
41 #include "manage_window.h"
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;
55 static gboolean ldapedit_basedn_cancelled;
56 static gboolean ldapedit_basedn_bad_server;
59 * Edit functions.
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 );
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;
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();
81 return FALSE;
84 static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) {
85 ldapedit_basedn_cancelled = FALSE;
86 gtk_main_quit();
89 static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) {
90 ldapedit_basedn_cancelled = TRUE;
91 gtk_main_quit();
94 static void edit_ldap_bdn_list_select( GtkCMCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
95 gchar *text = NULL;
97 if( gtk_cmclist_get_text( clist, row, 0, &text ) ) {
98 if( text ) {
99 gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), text );
104 static gboolean edit_ldap_bdn_list_button( GtkCMCList *clist, GdkEventButton *event, gpointer data ) {
105 if( ! event ) return FALSE;
106 if( event->button == 1 ) {
107 if( event->type == GDK_2BUTTON_PRESS ) {
108 ldapedit_basedn_cancelled = FALSE;
109 gtk_main_quit();
112 return FALSE;
115 static void edit_ldap_bdn_create(void) {
116 GtkWidget *window;
117 GtkWidget *vbox;
118 GtkWidget *table;
119 GtkWidget *label;
120 GtkWidget *host_label;
121 GtkWidget *port_label;
122 GtkWidget *basedn_list;
123 GtkWidget *vlbox;
124 GtkWidget *lwindow;
125 GtkWidget *basedn_entry;
126 GtkWidget *hbbox;
127 GtkWidget *hsep;
128 GtkWidget *ok_btn;
129 GtkWidget *cancel_btn;
130 GtkWidget *hsbox;
131 GtkWidget *statusbar;
132 gint top;
134 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editldap_basedn");
135 gtk_widget_set_size_request(window, 300, 270);
136 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
137 gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP - Select Search Base"));
138 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
139 g_signal_connect(G_OBJECT(window), "delete_event",
140 G_CALLBACK(edit_ldap_bdn_delete_event), NULL );
141 g_signal_connect(G_OBJECT(window), "key_press_event",
142 G_CALLBACK(edit_ldap_bdn_key_pressed), NULL );
144 vbox = gtk_vbox_new(FALSE, 8);
145 gtk_container_add(GTK_CONTAINER(window), vbox);
146 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
148 table = gtk_table_new(3, 2, FALSE);
149 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
150 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
151 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
152 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
154 /* First row */
155 top = 0;
156 label = gtk_label_new(_("Hostname"));
157 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
158 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
160 host_label = gtk_label_new("");
161 gtk_table_attach(GTK_TABLE(table), host_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
162 gtk_misc_set_alignment(GTK_MISC(host_label), 0, 0.5);
164 /* Second row */
165 top = 1;
166 label = gtk_label_new(_("Port"));
167 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
168 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
170 port_label = gtk_label_new("");
171 gtk_table_attach(GTK_TABLE(table), port_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
172 gtk_misc_set_alignment(GTK_MISC(port_label), 0, 0.5);
174 /* Third row */
175 top = 2;
176 label = gtk_label_new(_("Search Base"));
177 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
178 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
180 basedn_entry = gtk_entry_new();
181 gtk_table_attach(GTK_TABLE(table), basedn_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
183 /* Basedn list */
184 vlbox = gtk_vbox_new(FALSE, 8);
185 gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
186 gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
188 lwindow = gtk_scrolled_window_new(NULL, NULL);
189 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow),
190 GTK_POLICY_AUTOMATIC,
191 GTK_POLICY_AUTOMATIC);
192 gtk_box_pack_start(GTK_BOX(vlbox), lwindow, TRUE, TRUE, 0);
194 basedn_list = gtk_cmclist_new(1);
195 gtk_container_add(GTK_CONTAINER(lwindow), basedn_list);
196 gtk_cmclist_column_titles_show( GTK_CMCLIST(basedn_list) );
197 gtk_cmclist_set_column_title( GTK_CMCLIST(basedn_list), 0, _( "Available Search Base(s)" ) );
198 gtk_cmclist_set_selection_mode(GTK_CMCLIST(basedn_list), GTK_SELECTION_BROWSE);
200 /* Status line */
201 hsbox = gtk_hbox_new(FALSE, 0);
202 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
203 statusbar = gtk_statusbar_new();
204 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
206 /* Button panel */
207 gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
208 &cancel_btn, GTK_STOCK_CANCEL,
209 NULL, NULL);
210 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
211 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
212 gtk_widget_grab_default(ok_btn);
214 hsep = gtk_hseparator_new();
215 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
217 g_signal_connect(G_OBJECT(ok_btn), "clicked",
218 G_CALLBACK(edit_ldap_bdn_ok), NULL);
219 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
220 G_CALLBACK(edit_ldap_bdn_cancel), NULL);
221 g_signal_connect(G_OBJECT(basedn_list), "select_row",
222 G_CALLBACK(edit_ldap_bdn_list_select), NULL);
223 g_signal_connect(G_OBJECT(basedn_list), "button_press_event",
224 G_CALLBACK(edit_ldap_bdn_list_button), NULL);
226 gtk_widget_show_all(vbox);
228 ldapedit_basedn.window = window;
229 ldapedit_basedn.host_label = host_label;
230 ldapedit_basedn.port_label = port_label;
231 ldapedit_basedn.basedn_entry = basedn_entry;
232 ldapedit_basedn.basedn_list = basedn_list;
233 ldapedit_basedn.ok_btn = ok_btn;
234 ldapedit_basedn.cancel_btn = cancel_btn;
235 ldapedit_basedn.statusbar = statusbar;
236 ldapedit_basedn.status_cid =
237 gtk_statusbar_get_context_id(
238 GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" );
241 static void edit_ldap_bdn_load_data(
242 const gchar *hostName, const gint iPort, const gint tov,
243 const gchar* bindDN, const gchar *bindPW, int ssl, int tls )
245 gchar *sHost;
246 gchar *sMsg = NULL;
247 gchar sPort[20];
248 gboolean flgConn;
249 gboolean flgDN;
250 GList *baseDN = NULL;
252 edit_ldap_bdn_status_show( "" );
253 gtk_cmclist_clear(GTK_CMCLIST(ldapedit_basedn.basedn_list));
254 ldapedit_basedn_bad_server = TRUE;
255 flgConn = flgDN = FALSE;
256 sHost = g_strdup( hostName );
257 sprintf( sPort, "%d", iPort );
258 gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
259 gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
260 if( *sHost != '\0' ) {
261 /* Test connection to server */
262 if( ldaputil_test_connect( sHost, iPort, ssl, tls, tov ) ) {
263 /* Attempt to read base DN */
264 baseDN = ldaputil_read_basedn( sHost, iPort, bindDN, bindPW, tov, ssl, tls );
265 if( baseDN ) {
266 GList *node = baseDN;
267 gchar *text[2] = { NULL, NULL };
269 while( node ) {
270 text[0] = (gchar *)node->data;
271 gtk_cmclist_append(GTK_CMCLIST(ldapedit_basedn.basedn_list), text);
272 node = g_list_next( node );
273 flgDN = TRUE;
275 mgu_free_dlist( baseDN );
276 baseDN = node = NULL;
278 ldapedit_basedn_bad_server = FALSE;
279 flgConn = TRUE;
282 g_free( sHost );
284 /* Display appropriate message */
285 if( flgConn ) {
286 if( ! flgDN ) {
287 sMsg = _( "Could not read Search Base(s) from server - please set manually" );
290 else {
291 sMsg = _( "Could not connect to server" );
293 edit_ldap_bdn_status_show( sMsg );
296 gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov,
297 const gchar* bindDN, const gchar *bindPW, int ssl, int tls ) {
298 gchar *retVal = NULL;
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 gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn.window), TRUE);
306 edit_ldap_bdn_status_show( "" );
307 edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW, ssl, tls );
308 gtk_widget_show(ldapedit_basedn.window);
310 gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN);
312 gtk_main();
313 gtk_widget_hide(ldapedit_basedn.window);
314 gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn.window), FALSE);
315 if( ldapedit_basedn_cancelled ) return NULL;
316 if( ldapedit_basedn_bad_server ) return NULL;
318 retVal = gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn.basedn_entry), 0, -1 );
319 g_strstrip( retVal );
320 if( *retVal == '\0' ) {
321 g_free( retVal );
322 retVal = NULL;
324 return retVal;
327 #endif /* USE_LDAP */
330 * End of Source.