Statistics
| Revision:

root / src / editldap_basedn.c @ 1617

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