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