Statistics
| Revision:

root / src / editldap_basedn.c @ 238

History | View | Annotate | Download (10.3 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 1 hiro
 * Copyright (C) 2001 Match Grun
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
/*
21 1 hiro
 * LDAP Base DN selection dialog.
22 1 hiro
 */
23 1 hiro
24 1 hiro
#ifdef HAVE_CONFIG_H
25 1 hiro
#  include "config.h"
26 1 hiro
#endif
27 1 hiro
28 1 hiro
#ifdef USE_LDAP
29 1 hiro
30 1 hiro
#include "defs.h"
31 1 hiro
32 1 hiro
#include <glib.h>
33 92 hiro
#include <glib/gi18n.h>
34 1 hiro
#include <gdk/gdkkeysyms.h>
35 1 hiro
#include <gtk/gtkwindow.h>
36 1 hiro
#include <gtk/gtksignal.h>
37 1 hiro
#include <gtk/gtkhbox.h>
38 238 hiro
#include <gtk/gtkvbox.h>
39 238 hiro
#include <gtk/gtktable.h>
40 1 hiro
#include <gtk/gtklabel.h>
41 1 hiro
#include <gtk/gtkentry.h>
42 1 hiro
#include <gtk/gtkhbbox.h>
43 1 hiro
#include <gtk/gtkbutton.h>
44 238 hiro
#include <gtk/gtkstatusbar.h>
45 238 hiro
#include <gtk/gtkhseparator.h>
46 1 hiro
47 1 hiro
#include "prefs_common.h"
48 1 hiro
#include "syldap.h"
49 1 hiro
#include "mgutils.h"
50 1 hiro
#include "gtkutils.h"
51 1 hiro
#include "manage_window.h"
52 1 hiro
53 1 hiro
static struct _LDAPEdit_basedn {
54 1 hiro
        GtkWidget *window;
55 1 hiro
        GtkWidget *host_label;
56 1 hiro
        GtkWidget *port_label;
57 1 hiro
        GtkWidget *basedn_entry;
58 1 hiro
        GtkWidget *basedn_list;
59 1 hiro
        GtkWidget *ok_btn;
60 1 hiro
        GtkWidget *cancel_btn;
61 1 hiro
        GtkWidget *statusbar;
62 1 hiro
        gint status_cid;
63 1 hiro
} ldapedit_basedn;
64 1 hiro
65 1 hiro
static gboolean ldapedit_basedn_cancelled;
66 1 hiro
static gboolean ldapedit_basedn_bad_server;
67 1 hiro
68 1 hiro
/*
69 1 hiro
* Edit functions.
70 1 hiro
*/
71 1 hiro
static void edit_ldap_bdn_status_show( gchar *msg ) {
72 1 hiro
        if( ldapedit_basedn.statusbar != NULL ) {
73 1 hiro
                gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid );
74 1 hiro
                if( msg ) {
75 1 hiro
                        gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid, msg );
76 1 hiro
                }
77 1 hiro
        }
78 1 hiro
}
79 1 hiro
80 1 hiro
static gint edit_ldap_bdn_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
81 1 hiro
        ldapedit_basedn_cancelled = TRUE;
82 1 hiro
        gtk_main_quit();
83 1 hiro
        return TRUE;
84 1 hiro
}
85 1 hiro
86 1 hiro
static gboolean edit_ldap_bdn_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
87 1 hiro
        if (event && event->keyval == GDK_Escape) {
88 1 hiro
                ldapedit_basedn_cancelled = TRUE;
89 1 hiro
                gtk_main_quit();
90 1 hiro
        }
91 1 hiro
        return FALSE;
92 1 hiro
}
93 1 hiro
94 1 hiro
static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) {
95 1 hiro
        ldapedit_basedn_cancelled = FALSE;
96 1 hiro
        gtk_main_quit();
97 1 hiro
}
98 1 hiro
99 1 hiro
static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) {
100 1 hiro
        ldapedit_basedn_cancelled = TRUE;
101 1 hiro
        gtk_main_quit();
102 1 hiro
}
103 1 hiro
104 1 hiro
static void edit_ldap_bdn_list_select( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
105 1 hiro
        gchar *text = NULL;
106 1 hiro
107 1 hiro
        if( gtk_clist_get_text( clist, row, 0, &text ) ) {
108 1 hiro
                if( text ) {
109 1 hiro
                        gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), text );
110 1 hiro
                }
111 1 hiro
        }
112 1 hiro
}
113 1 hiro
114 1 hiro
static gboolean edit_ldap_bdn_list_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
115 1 hiro
        if( ! event ) return FALSE;
116 1 hiro
        if( event->button == 1 ) {
117 1 hiro
                if( event->type == GDK_2BUTTON_PRESS ) {
118 1 hiro
                        ldapedit_basedn_cancelled = FALSE;
119 1 hiro
                        gtk_main_quit();
120 1 hiro
                }
121 1 hiro
        }
122 1 hiro
        return FALSE;
123 1 hiro
}
124 1 hiro
125 1 hiro
static void edit_ldap_bdn_create(void) {
126 1 hiro
        GtkWidget *window;
127 1 hiro
        GtkWidget *vbox;
128 1 hiro
        GtkWidget *table;
129 1 hiro
        GtkWidget *label;
130 1 hiro
        GtkWidget *host_label;
131 1 hiro
        GtkWidget *port_label;
132 1 hiro
        GtkWidget *basedn_list;
133 1 hiro
        GtkWidget *vlbox;
134 1 hiro
        GtkWidget *lwindow;
135 1 hiro
        GtkWidget *basedn_entry;
136 1 hiro
        GtkWidget *hbbox;
137 1 hiro
        GtkWidget *hsep;
138 1 hiro
        GtkWidget *ok_btn;
139 1 hiro
        GtkWidget *cancel_btn;
140 1 hiro
        GtkWidget *hsbox;
141 1 hiro
        GtkWidget *statusbar;
142 1 hiro
        gint top;
143 1 hiro
144 1 hiro
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
145 1 hiro
        gtk_widget_set_size_request(window, 300, 270);
146 1 hiro
        gtk_container_set_border_width(GTK_CONTAINER(window), 0);
147 1 hiro
        gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP - Select Search Base"));
148 1 hiro
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
149 1 hiro
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
150 1 hiro
        g_signal_connect(G_OBJECT(window), "delete_event",
151 1 hiro
                         G_CALLBACK(edit_ldap_bdn_delete_event), NULL);
152 1 hiro
        g_signal_connect(G_OBJECT(window), "key_press_event",
153 1 hiro
                         G_CALLBACK(edit_ldap_bdn_key_pressed), NULL);
154 1 hiro
155 1 hiro
        vbox = gtk_vbox_new(FALSE, 8);
156 1 hiro
        gtk_container_add(GTK_CONTAINER(window), vbox);
157 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
158 1 hiro
159 1 hiro
        table = gtk_table_new(3, 2, FALSE);
160 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
161 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
162 1 hiro
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
163 1 hiro
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
164 1 hiro
165 1 hiro
        /* First row */
166 1 hiro
        top = 0;
167 1 hiro
        label = gtk_label_new(_("Hostname"));
168 1 hiro
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
169 1 hiro
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
170 1 hiro
171 1 hiro
        host_label = gtk_label_new("");
172 1 hiro
        gtk_table_attach(GTK_TABLE(table), host_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
173 1 hiro
        gtk_misc_set_alignment(GTK_MISC(host_label), 0, 0.5);
174 1 hiro
175 1 hiro
        /* Second row */
176 1 hiro
        top = 1;
177 1 hiro
        label = gtk_label_new(_("Port"));
178 1 hiro
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
179 1 hiro
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
180 1 hiro
181 1 hiro
        port_label = gtk_label_new("");
182 1 hiro
        gtk_table_attach(GTK_TABLE(table), port_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
183 1 hiro
        gtk_misc_set_alignment(GTK_MISC(port_label), 0, 0.5);
184 1 hiro
185 1 hiro
        /* Third row */
186 1 hiro
        top = 2;
187 1 hiro
        label = gtk_label_new(_("Search Base"));
188 1 hiro
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
189 1 hiro
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
190 1 hiro
191 1 hiro
        basedn_entry = gtk_entry_new();
192 1 hiro
        gtk_table_attach(GTK_TABLE(table), basedn_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
193 1 hiro
194 1 hiro
        /* Basedn list */
195 1 hiro
        vlbox = gtk_vbox_new(FALSE, 8);
196 1 hiro
        gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
197 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
198 1 hiro
199 1 hiro
        lwindow = gtk_scrolled_window_new(NULL, NULL);
200 1 hiro
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow),
201 1 hiro
                                       GTK_POLICY_AUTOMATIC,
202 1 hiro
                                       GTK_POLICY_ALWAYS);
203 1 hiro
        gtk_box_pack_start(GTK_BOX(vlbox), lwindow, TRUE, TRUE, 0);
204 1 hiro
205 1 hiro
        basedn_list = gtk_clist_new(1);
206 1 hiro
        gtk_container_add(GTK_CONTAINER(lwindow), basedn_list);
207 1 hiro
        gtk_clist_column_titles_show( GTK_CLIST(basedn_list) );
208 1 hiro
        gtk_clist_set_column_title( GTK_CLIST(basedn_list), 0, _( "Available Search Base(s)" ) );
209 1 hiro
        gtk_clist_set_selection_mode(GTK_CLIST(basedn_list), GTK_SELECTION_BROWSE);
210 1 hiro
211 1 hiro
        /* Status line */
212 1 hiro
        hsbox = gtk_hbox_new(FALSE, 0);
213 1 hiro
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
214 1 hiro
        statusbar = gtk_statusbar_new();
215 1 hiro
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
216 1 hiro
217 1 hiro
        /* Button panel */
218 31 hiro
        gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
219 31 hiro
                                      &cancel_btn, GTK_STOCK_CANCEL,
220 31 hiro
                                      NULL, NULL);
221 1 hiro
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
222 1 hiro
        gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
223 1 hiro
        gtk_widget_grab_default(ok_btn);
224 1 hiro
225 1 hiro
        hsep = gtk_hseparator_new();
226 1 hiro
        gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
227 1 hiro
228 1 hiro
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
229 1 hiro
                         G_CALLBACK(edit_ldap_bdn_ok), NULL);
230 1 hiro
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
231 1 hiro
                         G_CALLBACK(edit_ldap_bdn_cancel), NULL);
232 1 hiro
        g_signal_connect(G_OBJECT(basedn_list), "select_row",
233 1 hiro
                         G_CALLBACK(edit_ldap_bdn_list_select), NULL);
234 1 hiro
        g_signal_connect(G_OBJECT(basedn_list), "button_press_event",
235 1 hiro
                         G_CALLBACK(edit_ldap_bdn_list_button), NULL);
236 1 hiro
237 1 hiro
        gtk_widget_show_all(vbox);
238 1 hiro
239 1 hiro
        ldapedit_basedn.window     = window;
240 1 hiro
        ldapedit_basedn.host_label = host_label;
241 1 hiro
        ldapedit_basedn.port_label = port_label;
242 1 hiro
        ldapedit_basedn.basedn_entry = basedn_entry;
243 1 hiro
        ldapedit_basedn.basedn_list  = basedn_list;
244 1 hiro
        ldapedit_basedn.ok_btn     = ok_btn;
245 1 hiro
        ldapedit_basedn.cancel_btn = cancel_btn;
246 1 hiro
        ldapedit_basedn.statusbar  = statusbar;
247 1 hiro
        ldapedit_basedn.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" );
248 1 hiro
}
249 1 hiro
250 1 hiro
void edit_ldap_bdn_load_data( const gchar *hostName, const gint iPort, const gint tov, const gchar* bindDN,
251 1 hiro
               const gchar *bindPW ) {
252 1 hiro
        gchar *sHost;
253 1 hiro
        gchar *sMsg = NULL;
254 1 hiro
        gchar sPort[20];
255 1 hiro
        gboolean flgConn;
256 1 hiro
        gboolean flgDN;
257 1 hiro
258 1 hiro
        edit_ldap_bdn_status_show( "" );
259 1 hiro
        gtk_clist_clear(GTK_CLIST(ldapedit_basedn.basedn_list));
260 1 hiro
        ldapedit_basedn_bad_server = TRUE;
261 1 hiro
        flgConn = flgDN = FALSE;
262 1 hiro
        sHost = g_strdup( hostName );
263 1 hiro
        sprintf( sPort, "%d", iPort );
264 1 hiro
        gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
265 1 hiro
        gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
266 1 hiro
        if( *sHost != '\0' ) {
267 1 hiro
                /* Test connection to server */
268 1 hiro
                if( syldap_test_connect_s( sHost, iPort ) ) {
269 1 hiro
                        /* Attempt to read base DN */
270 1 hiro
                        GList *baseDN = syldap_read_basedn_s( sHost, iPort, bindDN, bindPW, tov );
271 1 hiro
                        if( baseDN ) {
272 1 hiro
                                GList *node = baseDN;
273 1 hiro
                                gchar *text[2] = { NULL, NULL };
274 1 hiro
275 1 hiro
                                while( node ) {
276 1 hiro
                                        text[0] = (gchar *)node->data;
277 1 hiro
                                        gtk_clist_append(GTK_CLIST(ldapedit_basedn.basedn_list), text);
278 1 hiro
                                        node = g_list_next( node );
279 1 hiro
                                        flgDN = TRUE;
280 1 hiro
                                }
281 1 hiro
                                mgu_free_dlist( baseDN );
282 1 hiro
                                baseDN = node = NULL;
283 1 hiro
                        }
284 1 hiro
                        ldapedit_basedn_bad_server = FALSE;
285 1 hiro
                        flgConn = TRUE;
286 1 hiro
                }
287 1 hiro
        }
288 1 hiro
        g_free( sHost );
289 1 hiro
290 1 hiro
        /* Display appropriate message */
291 1 hiro
        if( flgConn ) {
292 1 hiro
                if( ! flgDN ) {
293 1 hiro
                        sMsg = _( "Could not read Search Base(s) from server - please set manually" );
294 1 hiro
                }
295 1 hiro
        }
296 1 hiro
        else {
297 1 hiro
                sMsg = _( "Could not connect to server" );
298 1 hiro
        }
299 1 hiro
        edit_ldap_bdn_status_show( sMsg );
300 1 hiro
}
301 1 hiro
302 1 hiro
gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov,
303 1 hiro
               const gchar* bindDN, const gchar *bindPW ) {
304 1 hiro
        gchar *retVal = NULL;
305 1 hiro
306 1 hiro
        ldapedit_basedn_cancelled = FALSE;
307 1 hiro
        if( ! ldapedit_basedn.window ) edit_ldap_bdn_create();
308 1 hiro
        gtk_widget_grab_focus(ldapedit_basedn.ok_btn);
309 1 hiro
        gtk_widget_show(ldapedit_basedn.window);
310 1 hiro
        manage_window_set_transient(GTK_WINDOW(ldapedit_basedn.window));
311 1 hiro
312 1 hiro
        edit_ldap_bdn_status_show( "" );
313 1 hiro
        edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW );
314 1 hiro
        gtk_widget_show(ldapedit_basedn.window);
315 1 hiro
316 1 hiro
        gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN);
317 1 hiro
318 1 hiro
        gtk_main();
319 1 hiro
        gtk_widget_hide(ldapedit_basedn.window);
320 1 hiro
        if( ldapedit_basedn_cancelled ) return NULL;
321 1 hiro
        if( ldapedit_basedn_bad_server ) return NULL;
322 1 hiro
323 1 hiro
        retVal = gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn.basedn_entry), 0, -1 );
324 1 hiro
        g_strchomp( retVal ); g_strchug( retVal );
325 1 hiro
        if( *retVal == '\0' ) {
326 1 hiro
                g_free( retVal );
327 1 hiro
                retVal = NULL;
328 1 hiro
        }
329 1 hiro
        return retVal;
330 1 hiro
}
331 1 hiro
332 1 hiro
#endif /* USE_LDAP */
333 1 hiro
334 1 hiro
/*
335 1 hiro
* End of Source.
336 1 hiro
*/