change gtk_scrolled_window_set_policy from
[claws.git] / src / browseldap.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003 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  * Browse LDAP entry.
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 <pthread.h>
43 #include "intl.h"
44 #include "gtkutils.h"
45 #include "stock_pixmap.h"
46 #include "prefs_common.h"
47 #include "browseldap.h"
48 #include "addritem.h"
49 #include "addrindex.h"
50 #include "manage_window.h"
51
52 #include "ldapquery.h"
53 #include "ldapserver.h"
54 #include "ldaplocate.h"
55
56 typedef enum {
57         COL_NAME  = 0,
58         COL_VALUE = 1
59 } LDAPEntryColumnPos;
60
61 #define BROWSELDAP_WIDTH    450
62 #define BROWSELDAP_HEIGHT   420
63
64 #define N_COLS              2
65 #define COL_WIDTH_NAME      140
66 #define COL_WIDTH_VALUE     140
67
68 static struct _LDAPEntry_dlg {
69         GtkWidget *window;
70         GtkWidget *label_server;
71         GtkWidget *label_address;
72         GtkWidget *list_entry;
73         GtkWidget *close_btn;
74         GtkWidget *statusbar;
75         gint status_cid;
76 } browseldap_dlg;
77
78 /**
79  * Message queue.
80  */
81 static GList *_displayQueue_ = NULL;
82
83 /**
84  * Mutex to protect callback from multiple threads.
85  */
86 static pthread_mutex_t _browseMutex_ = PTHREAD_MUTEX_INITIALIZER;
87
88 /**
89  * Current query ID.
90  */
91 static gint _queryID_ = 0;
92
93 /**
94  * Completion idle ID.
95  */
96 static guint _browseIdleID_ = 0;
97
98 /**
99  * Search complete indicator.
100  */
101 static gboolean _searchComplete_ = FALSE;
102
103 /**
104  * Callback entry point for each LDAP entry processed. The background thread
105  * (if any) appends the address list to the display queue.
106  * 
107  * \param qry        LDAP query object.
108  * \param queryID    Query ID of search request.
109  * \param listEMail  List of zero of more email objects that met search
110  *                   criteria.
111  * \param data       User data.
112  */
113 static gint browse_callback_entry(
114                 LdapQuery *qry, gint queryID, GList *listValues, gpointer data )
115 {
116         GList *node;
117         NameValuePair *nvp;
118
119         /* printf( "browse_callback_entry...\n" ); */
120         pthread_mutex_lock( & _browseMutex_ );
121         /* Append contents to end of display queue */
122         node = listValues;
123         while( node ) {
124                 nvp = ( NameValuePair * ) node->data;
125                 /* ldapqry_print_name_value( nvp, stdout ); */
126                 _displayQueue_ = g_list_append( _displayQueue_, nvp );
127                 node->data = NULL;
128                 node = g_list_next( node );
129         }
130         pthread_mutex_unlock( & _browseMutex_ );
131         /* printf( "browse_callback_entry...done\n" ); */
132
133         return 0;
134 }
135
136 /**
137  * Callback entry point for end of LDAP locate search.
138  * 
139  * \param qry     LDAP query object.
140  * \param queryID Query ID of search request.
141  * \param status  Status/error code.
142  * \param data    User data.
143  */
144 static gint browse_callback_end(
145                 LdapQuery *qry, gint queryID, gint status, gpointer data )
146 {
147         _searchComplete_ = TRUE;
148 }
149
150 /**
151  * Clear the display queue.
152  */
153 static void browse_clear_queue( void ) {
154         /* Clear out display queue */
155         pthread_mutex_lock( & _browseMutex_ );
156
157         ldapqry_free_list_name_value( _displayQueue_ );
158         g_list_free( _displayQueue_ );
159         _displayQueue_ = NULL;
160
161         pthread_mutex_unlock( & _browseMutex_ );
162 }
163
164 /**
165  * Clear message in status bar.
166  * \param msg Message.
167  */
168 static void browse_status_show( gchar *msg ) {
169         if( browseldap_dlg.statusbar != NULL ) {
170                 gtk_statusbar_pop( GTK_STATUSBAR(browseldap_dlg.statusbar),
171                         browseldap_dlg.status_cid );
172                 if( msg ) {
173                         gtk_statusbar_push(
174                                 GTK_STATUSBAR(browseldap_dlg.statusbar),
175                                 browseldap_dlg.status_cid, msg );
176                 }
177         }
178 }
179
180 /**
181  * Close window callback.
182  * \param widget    Widget.
183  * \param event     Event.
184  * \param cancelled Cancelled flag.
185  */
186 static gint browse_delete_event(
187                 GtkWidget *widget, GdkEventAny *event, gboolean *cancelled )
188 {
189         gtk_main_quit();
190         return TRUE;
191 }
192
193 /**
194  * Respond to key press in window.
195  * \param widget    Widget.
196  * \param event     Event.
197  * \param cancelled Cancelled flag.
198  */
199 static void browse_key_pressed(
200                 GtkWidget *widget, GdkEventKey *event, gboolean *cancelled )
201 {
202         if (event && event->keyval == GDK_Escape) {
203                 gtk_main_quit();
204         }
205 }
206
207 /**
208  * Callback to close window.
209  * \param widget    Widget.
210  * \param cancelled Cancelled flag.
211  */
212 static void browse_close( GtkWidget *widget, gboolean *cancelled ) {
213         gtk_main_quit();
214 }
215
216 /**
217  * Create the window to display data.
218  */
219 static void browse_create( void ) {
220         GtkWidget *window;
221         GtkWidget *vbox;
222         GtkWidget *table;
223         GtkWidget *label;
224         GtkWidget *label_server;
225         GtkWidget *label_addr;
226         GtkWidget *list_entry;
227         GtkWidget *vlbox;
228         GtkWidget *tree_win;
229         GtkWidget *hbbox;
230         GtkWidget *close_btn;
231         GtkWidget *hsbox;
232         GtkWidget *statusbar;
233         gint top;
234
235         window = gtk_dialog_new();
236         gtk_widget_set_usize( window, BROWSELDAP_WIDTH, BROWSELDAP_HEIGHT );
237         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
238         gtk_window_set_title( GTK_WINDOW(window), _("Browse Directory Entry") );
239         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
240         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
241         g_signal_connect(G_OBJECT(window), "delete_event",
242                          G_CALLBACK(browse_delete_event), NULL);
243         g_signal_connect(G_OBJECT(window), "key_press_event",
244                          G_CALLBACK(browse_key_pressed), NULL);
245
246         vbox = gtk_vbox_new(FALSE, 8);
247         gtk_container_add(GTK_CONTAINER(window), vbox);
248         gtk_container_set_border_width( GTK_CONTAINER(vbox), 8 );
249
250         table = gtk_table_new(2, 2, FALSE);
251         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
252         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
253         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
254         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
255
256         /* First row */
257         top = 0;
258         label = gtk_label_new(_("Server Name :"));
259         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
260         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
261
262         label_server = gtk_label_new("");
263         gtk_table_attach(GTK_TABLE(table), label_server, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
264         gtk_misc_set_alignment(GTK_MISC(label_server), 0, 0.5);
265
266         /* Second row */
267         top++;
268         label = gtk_label_new(_("Distinguished Name (dn) :"));
269         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
270         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
271
272         label_addr = gtk_label_new("");
273         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
274         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
275
276         /* Address book/folder tree */
277         vlbox = gtk_vbox_new(FALSE, 8);
278         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
279         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
280
281         tree_win = gtk_scrolled_window_new( NULL, NULL );
282         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
283                                         GTK_POLICY_AUTOMATIC,
284                                         GTK_POLICY_AUTOMATIC );
285         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
286
287         list_entry = gtk_clist_new( N_COLS );
288         gtk_container_add( GTK_CONTAINER(tree_win), list_entry );
289         gtk_clist_column_titles_show( GTK_CLIST(list_entry) );
290         gtk_clist_set_column_title(
291                 GTK_CLIST(list_entry), COL_NAME, _( "LDAP Name" ) );
292         gtk_clist_set_column_title(
293                 GTK_CLIST(list_entry), COL_VALUE, _( "Attribute Value" ) );
294         gtk_clist_set_selection_mode(
295                 GTK_CLIST(list_entry), GTK_SELECTION_BROWSE );
296         gtk_clist_set_column_width( GTK_CLIST(list_entry),
297                 COL_NAME, COL_WIDTH_NAME );
298         gtk_clist_set_auto_sort( GTK_CLIST(list_entry), TRUE );
299
300         /* Status line */
301         hsbox = gtk_hbox_new(FALSE, 0);
302         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
303         statusbar = gtk_statusbar_new();
304         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
305
306         /* Button panel */
307         gtkut_button_set_create(&hbbox, &close_btn, _( "Close" ),
308                                 NULL, NULL, NULL, NULL);
309         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
310         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
311
312         g_signal_connect(G_OBJECT(close_btn), "clicked",
313                          G_CALLBACK(browse_close), NULL);
314         gtk_widget_grab_default(close_btn);
315
316         gtk_widget_show_all(vbox);
317
318         browseldap_dlg.window        = window;
319         browseldap_dlg.label_server  = label_server;
320         browseldap_dlg.label_address = label_addr;
321         browseldap_dlg.list_entry    = list_entry;
322         browseldap_dlg.close_btn     = close_btn;
323         browseldap_dlg.statusbar     = statusbar;
324         browseldap_dlg.status_cid    =
325                 gtk_statusbar_get_context_id(
326                         GTK_STATUSBAR(statusbar), "Browse LDAP" );
327
328         gtk_widget_show_all( window );
329
330 }
331
332 /**
333  * Idler function. This function is called by the main (UI) thread during UI
334  * idle time while an address search is in progress. Items from the display
335  * queue are processed and appended to the address list.
336  *
337  * \param data Target data object.
338  * \return <i>TRUE</i> to ensure that idle event do not get ignored.
339  */
340 static gboolean browse_idle( gpointer data ) {
341         GList *node;
342         NameValuePair *nvp;
343         gchar *text[N_COLS];
344
345         /* Process all entries in display queue */
346         pthread_mutex_lock( & _browseMutex_ );
347         if( _displayQueue_ ) {
348                 node = _displayQueue_;
349                 while( node ) {
350                         /* Add entry into list */
351                         nvp = ( NameValuePair * ) node->data;
352                         text[COL_NAME]  = nvp->name;
353                         text[COL_VALUE] = nvp->value;
354                         gtk_clist_append(
355                                 GTK_CLIST(browseldap_dlg.list_entry), text );
356
357                         /* Free up entry */
358                         ldapqry_free_name_value( nvp );
359                         node->data = NULL;
360                         node = g_list_next( node );
361                 }
362                 g_list_free( _displayQueue_ );
363                 _displayQueue_ = NULL;
364         }
365         pthread_mutex_unlock( & _browseMutex_ );
366
367         if( _searchComplete_ ) {
368                 /* Remove idler */
369                 if( _browseIdleID_ != 0 ) {
370                         gtk_idle_remove( _browseIdleID_ );
371                         _browseIdleID_ = 0;
372                         gtk_clist_select_row(
373                                 GTK_CLIST( browseldap_dlg.list_entry ), 0, 0 );
374                 }
375         }
376
377         return TRUE;
378 }
379
380 /**
381  * Main entry point to browse LDAP entries.
382  * \param  ds Data source to process.
383  * \param  dn Distinguished name to retrieve.
384  * \return <code>TRUE</code>
385  */
386 gboolean browseldap_entry( AddressDataSource *ds, const gchar *dn ) {
387         LdapServer *server;
388
389         _queryID_ = 0;
390         _browseIdleID_ = 0;
391
392         server = ds->rawDataSource;
393
394         if( ! browseldap_dlg.window ) browse_create();
395         gtk_widget_grab_focus(browseldap_dlg.close_btn);
396         gtk_widget_show(browseldap_dlg.window);
397         manage_window_set_transient(GTK_WINDOW(browseldap_dlg.window));
398
399         browse_status_show( "" );
400         gtk_clist_select_row( GTK_CLIST( browseldap_dlg.list_entry ), 0, 0 );
401         gtk_widget_show(browseldap_dlg.window);
402
403         gtk_label_set_text( GTK_LABEL(browseldap_dlg.label_address ), "" );
404         if( dn ) {
405                 gtk_label_set_text(
406                         GTK_LABEL(browseldap_dlg.label_address ), dn );
407         }
408         gtk_label_set_text(
409                 GTK_LABEL(browseldap_dlg.label_server ),
410                 ldapsvr_get_name( server ) );
411
412         /* Setup search */
413         _searchComplete_ = FALSE;
414         _queryID_ = ldaplocate_search_setup(
415                         server, dn, browse_callback_entry, browse_callback_end );
416         _browseIdleID_ = gtk_idle_add( ( GtkFunction ) browse_idle, NULL );
417
418         /* Start search */
419         ldaplocate_search_start( _queryID_ );
420
421         /* Display dialog */
422         gtk_main();
423         gtk_widget_hide( browseldap_dlg.window );
424
425         /* Stop query */
426         ldaplocate_search_stop( _queryID_ );
427
428         if( _browseIdleID_ != 0 ) {
429                 gtk_idle_remove( _browseIdleID_ );
430                 _browseIdleID_ = 0;
431         }
432         browse_clear_queue();
433         gtk_clist_clear( GTK_CLIST( browseldap_dlg.list_entry ) );
434
435         return TRUE;
436 }
437
438 #endif /* USE_LDAP */
439
440 /*
441 * End of Source.
442 */
443