Statistics
| Revision:

root / src / editldap_basedn.c @ 1

History | View | Annotate | Download (10.2 KB)

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
#include "mgutils.h"
46
#include "gtkutils.h"
47
#include "manage_window.h"
48

    
49
static struct _LDAPEdit_basedn {
50
        GtkWidget *window;
51
        GtkWidget *host_label;
52
        GtkWidget *port_label;
53
        GtkWidget *basedn_entry;
54
        GtkWidget *basedn_list;
55
        GtkWidget *ok_btn;
56
        GtkWidget *cancel_btn;
57
        GtkWidget *statusbar;
58
        gint status_cid;
59
} ldapedit_basedn;
60

    
61
static gboolean ldapedit_basedn_cancelled;
62
static gboolean ldapedit_basedn_bad_server;
63

    
64
/*
65
* Edit functions.
66
*/
67
static void edit_ldap_bdn_status_show( gchar *msg ) {
68
        if( ldapedit_basedn.statusbar != NULL ) {
69
                gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid );
70
                if( msg ) {
71
                        gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid, msg );
72
                }
73
        }
74
}
75

    
76
static gint edit_ldap_bdn_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
77
        ldapedit_basedn_cancelled = TRUE;
78
        gtk_main_quit();
79
        return TRUE;
80
}
81

    
82
static gboolean edit_ldap_bdn_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
83
        if (event && event->keyval == GDK_Escape) {
84
                ldapedit_basedn_cancelled = TRUE;
85
                gtk_main_quit();
86
        }
87
        return FALSE;
88
}
89

    
90
static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) {
91
        ldapedit_basedn_cancelled = FALSE;
92
        gtk_main_quit();
93
}
94

    
95
static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) {
96
        ldapedit_basedn_cancelled = TRUE;
97
        gtk_main_quit();
98
}
99

    
100
static void edit_ldap_bdn_list_select( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
101
        gchar *text = NULL;
102

    
103
        if( gtk_clist_get_text( clist, row, 0, &text ) ) {
104
                if( text ) {
105
                        gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), text );
106
                }
107
        }
108
}
109

    
110
static gboolean edit_ldap_bdn_list_button( GtkCList *clist, GdkEventButton *event, gpointer data ) {
111
        if( ! event ) return FALSE;
112
        if( event->button == 1 ) {
113
                if( event->type == GDK_2BUTTON_PRESS ) {
114
                        ldapedit_basedn_cancelled = FALSE;
115
                        gtk_main_quit();
116
                }
117
        }
118
        return FALSE;
119
}
120

    
121
static void edit_ldap_bdn_create(void) {
122
        GtkWidget *window;
123
        GtkWidget *vbox;
124
        GtkWidget *table;
125
        GtkWidget *label;
126
        GtkWidget *host_label;
127
        GtkWidget *port_label;
128
        GtkWidget *basedn_list;
129
        GtkWidget *vlbox;
130
        GtkWidget *lwindow;
131
        GtkWidget *basedn_entry;
132
        GtkWidget *hbbox;
133
        GtkWidget *hsep;
134
        GtkWidget *ok_btn;
135
        GtkWidget *cancel_btn;
136
        GtkWidget *hsbox;
137
        GtkWidget *statusbar;
138
        gint top;
139

    
140
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
141
        gtk_widget_set_size_request(window, 300, 270);
142
        gtk_container_set_border_width(GTK_CONTAINER(window), 0);
143
        gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP - Select Search Base"));
144
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
145
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);        
146
        g_signal_connect(G_OBJECT(window), "delete_event",
147
                         G_CALLBACK(edit_ldap_bdn_delete_event), NULL);
148
        g_signal_connect(G_OBJECT(window), "key_press_event",
149
                         G_CALLBACK(edit_ldap_bdn_key_pressed), NULL);
150

    
151
        vbox = gtk_vbox_new(FALSE, 8);
152
        gtk_container_add(GTK_CONTAINER(window), vbox);
153
        gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
154

    
155
        table = gtk_table_new(3, 2, FALSE);
156
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
157
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
158
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
159
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
160

    
161
        /* First row */
162
        top = 0;
163
        label = gtk_label_new(_("Hostname"));
164
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
165
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
166

    
167
        host_label = gtk_label_new("");
168
        gtk_table_attach(GTK_TABLE(table), host_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
169
        gtk_misc_set_alignment(GTK_MISC(host_label), 0, 0.5);
170

    
171
        /* Second row */
172
        top = 1;
173
        label = gtk_label_new(_("Port"));
174
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
175
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
176

    
177
        port_label = gtk_label_new("");
178
        gtk_table_attach(GTK_TABLE(table), port_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
179
        gtk_misc_set_alignment(GTK_MISC(port_label), 0, 0.5);
180

    
181
        /* Third row */
182
        top = 2;
183
        label = gtk_label_new(_("Search Base"));
184
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
185
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
186

    
187
        basedn_entry = gtk_entry_new();
188
        gtk_table_attach(GTK_TABLE(table), basedn_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
189

    
190
        /* Basedn list */
191
        vlbox = gtk_vbox_new(FALSE, 8);
192
        gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
193
        gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
194

    
195
        lwindow = gtk_scrolled_window_new(NULL, NULL);
196
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow),
197
                                       GTK_POLICY_AUTOMATIC,
198
                                       GTK_POLICY_ALWAYS);
199
        gtk_box_pack_start(GTK_BOX(vlbox), lwindow, TRUE, TRUE, 0);
200

    
201
        basedn_list = gtk_clist_new(1);
202
        gtk_container_add(GTK_CONTAINER(lwindow), basedn_list);
203
        gtk_clist_column_titles_show( GTK_CLIST(basedn_list) );
204
        gtk_clist_set_column_title( GTK_CLIST(basedn_list), 0, _( "Available Search Base(s)" ) );
205
        gtk_clist_set_selection_mode(GTK_CLIST(basedn_list), GTK_SELECTION_BROWSE);
206

    
207
        /* Status line */
208
        hsbox = gtk_hbox_new(FALSE, 0);
209
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
210
        statusbar = gtk_statusbar_new();
211
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
212

    
213
        /* Button panel */
214
        gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
215
                                &cancel_btn, _("Cancel"), NULL, NULL);
216
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
217
        gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
218
        gtk_widget_grab_default(ok_btn);
219

    
220
        hsep = gtk_hseparator_new();
221
        gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
222

    
223
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
224
                         G_CALLBACK(edit_ldap_bdn_ok), NULL);
225
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
226
                         G_CALLBACK(edit_ldap_bdn_cancel), NULL);
227
        g_signal_connect(G_OBJECT(basedn_list), "select_row",
228
                         G_CALLBACK(edit_ldap_bdn_list_select), NULL);
229
        g_signal_connect(G_OBJECT(basedn_list), "button_press_event",
230
                         G_CALLBACK(edit_ldap_bdn_list_button), NULL);
231

    
232
        gtk_widget_show_all(vbox);
233

    
234
        ldapedit_basedn.window     = window;
235
        ldapedit_basedn.host_label = host_label;
236
        ldapedit_basedn.port_label = port_label;
237
        ldapedit_basedn.basedn_entry = basedn_entry;
238
        ldapedit_basedn.basedn_list  = basedn_list;
239
        ldapedit_basedn.ok_btn     = ok_btn;
240
        ldapedit_basedn.cancel_btn = cancel_btn;
241
        ldapedit_basedn.statusbar  = statusbar;
242
        ldapedit_basedn.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" );
243
}
244

    
245
void edit_ldap_bdn_load_data( const gchar *hostName, const gint iPort, const gint tov, const gchar* bindDN,
246
               const gchar *bindPW ) {
247
        gchar *sHost;
248
        gchar *sMsg = NULL;
249
        gchar sPort[20];
250
        gboolean flgConn;
251
        gboolean flgDN;
252

    
253
        edit_ldap_bdn_status_show( "" );
254
        gtk_clist_clear(GTK_CLIST(ldapedit_basedn.basedn_list));
255
        ldapedit_basedn_bad_server = TRUE;
256
        flgConn = flgDN = FALSE;
257
        sHost = g_strdup( hostName );
258
        sprintf( sPort, "%d", iPort );
259
        gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName);
260
        gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort);
261
        if( *sHost != '\0' ) {
262
                /* Test connection to server */
263
                if( syldap_test_connect_s( sHost, iPort ) ) {
264
                        /* Attempt to read base DN */
265
                        GList *baseDN = syldap_read_basedn_s( sHost, iPort, bindDN, bindPW, tov );
266
                        if( baseDN ) {
267
                                GList *node = baseDN;
268
                                gchar *text[2] = { NULL, NULL };
269

    
270
                                while( node ) {
271
                                        text[0] = (gchar *)node->data;
272
                                        gtk_clist_append(GTK_CLIST(ldapedit_basedn.basedn_list), text);
273
                                        node = g_list_next( node );
274
                                        flgDN = TRUE;
275
                                }
276
                                mgu_free_dlist( baseDN );
277
                                baseDN = node = NULL;
278
                        }
279
                        ldapedit_basedn_bad_server = FALSE;
280
                        flgConn = TRUE;
281
                }
282
        }
283
        g_free( sHost );
284

    
285
        /* Display appropriate message */
286
        if( flgConn ) {
287
                if( ! flgDN ) {
288
                        sMsg = _( "Could not read Search Base(s) from server - please set manually" );
289
                }
290
        }
291
        else {
292
                sMsg = _( "Could not connect to server" );
293
        }
294
        edit_ldap_bdn_status_show( sMsg );
295
}
296

    
297
gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov,
298
               const gchar* bindDN, const gchar *bindPW ) {
299
        gchar *retVal = NULL;
300

    
301
        ldapedit_basedn_cancelled = FALSE;
302
        if( ! ldapedit_basedn.window ) edit_ldap_bdn_create();
303
        gtk_widget_grab_focus(ldapedit_basedn.ok_btn);
304
        gtk_widget_show(ldapedit_basedn.window);
305
        manage_window_set_transient(GTK_WINDOW(ldapedit_basedn.window));
306

    
307
        edit_ldap_bdn_status_show( "" );
308
        edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW );
309
        gtk_widget_show(ldapedit_basedn.window);
310

    
311
        gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN);
312

    
313
        gtk_main();
314
        gtk_widget_hide(ldapedit_basedn.window);
315
        if( ldapedit_basedn_cancelled ) return NULL;
316
        if( ldapedit_basedn_bad_server ) return NULL;
317

    
318
        retVal = gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn.basedn_entry), 0, -1 );
319
        g_strchomp( retVal ); g_strchug( retVal );
320
        if( *retVal == '\0' ) {
321
                g_free( retVal );
322
                retVal = NULL;
323
        }
324
        return retVal;
325
}
326

    
327
#endif /* USE_LDAP */
328

    
329
/*
330
* End of Source.
331
*/
332