2009-01-02 [colin] 3.7.0cvs8
[claws.git] / src / addressbook_foldersel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2007 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  * Add address to address book dialog.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtk.h>
34
35 #include "gtkutils.h"
36 #include "stock_pixmap.h"
37 #include "prefs_common.h"
38 #include "addressadd.h"
39 #include "addritem.h"
40 #include "addrbook.h"
41 #include "addrindex.h"
42 #include "manage_window.h"
43
44 typedef struct {
45         AddressBookFile *book;
46         ItemFolder      *folder;
47 } FolderInfo;
48
49 typedef struct {
50         gchar **folder_path;
51         gboolean matched;
52         gint index;
53         GtkCMCTreeNode *node;
54 } FolderPathMatch;
55
56 static struct _AddressBookFolderSel_dlg {
57         GtkWidget *window;
58         GtkWidget *tree_folder;
59         GtkWidget *ok_btn;
60         GtkWidget *cancel_btn;
61         gint status_cid;
62         FolderInfo *fiSelected;
63 } addressbook_foldersel_dlg;
64
65 static GdkPixbuf *folderXpm;
66 static GdkPixbuf *bookXpm;
67
68 static gboolean addressbook_foldersel_cancelled;
69
70 static FolderInfo *addressbook_foldersel_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
71 {
72         FolderInfo *fi = g_new0( FolderInfo, 1 );
73         fi->book   = abf;
74         fi->folder = folder;
75         return fi;
76 }
77
78 static void addressbook_foldersel_free_folderinfo( FolderInfo *fi ) {
79         fi->book   = NULL;
80         fi->folder = NULL;
81         g_free( fi );
82 }
83
84 static gint addressbook_foldersel_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled )
85 {
86         addressbook_foldersel_cancelled = TRUE;
87         gtk_main_quit();
88         return TRUE;
89 }
90
91 static gboolean addressbook_foldersel_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled )
92 {
93         if ( event && event->keyval == GDK_Escape ) {
94                 addressbook_foldersel_cancelled = TRUE;
95                 gtk_main_quit();
96         }
97         return FALSE;
98 }
99
100 static void addressbook_foldersel_ok( GtkWidget *widget, gboolean *cancelled )
101 {
102         addressbook_foldersel_cancelled = FALSE;
103         gtk_main_quit();
104 }
105
106 static void addressbook_foldersel_cancel( GtkWidget *widget, gboolean *cancelled )
107 {
108         addressbook_foldersel_cancelled = TRUE;
109         gtk_main_quit();
110 }
111
112 static void addressbook_foldersel_folder_select( GtkCMCTree *ctree, GtkCMCTreeNode *node,
113                                       gint column, gpointer data )
114 {
115         addressbook_foldersel_dlg.fiSelected = gtk_cmctree_node_get_row_data( ctree, node );
116 }
117
118 static gboolean addressbook_foldersel_tree_button( GtkCMCTree *ctree, GdkEventButton *event, gpointer data )
119 {
120         if ( ! event )
121                 return FALSE;
122         if ( event->button == 1 ) {
123                 /* Handle double click */
124                 if ( event->type == GDK_2BUTTON_PRESS ) {
125                         addressbook_foldersel_cancelled = FALSE;
126                         gtk_main_quit();
127                 }
128         }
129
130         return FALSE;
131 }
132
133 static void addressbook_foldersel_size_allocate_cb(GtkWidget *widget,
134                                          GtkAllocation *allocation)
135 {
136         g_return_if_fail(allocation != NULL);
137
138         prefs_common.addressbook_folderselwin_width = allocation->width;
139         prefs_common.addressbook_folderselwin_height = allocation->height;
140 }
141
142 static void addressbook_foldersel_create( void )
143 {
144         GtkWidget *window;
145         GtkWidget *vbox;
146         GtkWidget *tree_folder;
147         GtkWidget *vlbox;
148         GtkWidget *tree_win;
149         GtkWidget *hbbox;
150         GtkWidget *ok_btn;
151         GtkWidget *cancel_btn;
152         static GdkGeometry geometry;
153         gchar *titles[1];
154
155         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addressbook_foldersel" );
156         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
157         gtk_window_set_title( GTK_WINDOW(window), _("Select Address Book Folder") );
158         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
159         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
160         g_signal_connect( G_OBJECT(window), "delete_event",
161                           G_CALLBACK(addressbook_foldersel_delete_event), NULL );
162         g_signal_connect( G_OBJECT(window), "key_press_event",
163                           G_CALLBACK(addressbook_foldersel_key_pressed), NULL );
164         g_signal_connect(G_OBJECT(window), "size_allocate",
165                          G_CALLBACK(addressbook_foldersel_size_allocate_cb), NULL);
166
167         vbox = gtk_vbox_new(FALSE, 8);
168         gtk_container_add(GTK_CONTAINER(window), vbox);
169         gtk_container_set_border_width( GTK_CONTAINER(vbox), 8 );
170
171         /* Address book/folder tree */
172         vlbox = gtk_vbox_new(FALSE, 8);
173         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
174         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
175
176         tree_win = gtk_scrolled_window_new( NULL, NULL );
177         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
178                                         GTK_POLICY_AUTOMATIC,
179                                         GTK_POLICY_AUTOMATIC );
180         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
181
182         titles[0] = _( "Address Book") ;
183
184         tree_folder = gtk_sctree_new_with_titles( 1, 0, titles );
185         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
186         gtk_cmclist_column_titles_show( GTK_CMCLIST(tree_folder) );
187         if (prefs_common.enable_dotted_lines) {
188                 gtk_cmctree_set_line_style(GTK_CMCTREE(tree_folder), GTK_CMCTREE_LINES_DOTTED);
189                 gtk_cmctree_set_expander_style(GTK_CMCTREE(tree_folder),
190                                      GTK_CMCTREE_EXPANDER_SQUARE);
191         } else {
192                 gtk_cmctree_set_line_style(GTK_CMCTREE(tree_folder), GTK_CMCTREE_LINES_NONE);
193                 gtk_cmctree_set_expander_style(GTK_CMCTREE(tree_folder),
194                                      GTK_CMCTREE_EXPANDER_TRIANGLE);
195         }
196         gtk_sctree_set_stripes(GTK_SCTREE(tree_folder), prefs_common.use_stripes_everywhere);
197         gtk_cmclist_set_selection_mode( GTK_CMCLIST(tree_folder), GTK_SELECTION_BROWSE );
198         gtk_cmctree_set_indent( GTK_CMCTREE(tree_folder), CTREE_INDENT );
199         gtk_cmclist_set_auto_sort( GTK_CMCLIST(tree_folder), TRUE );
200
201         /* Button panel */
202         gtkut_stock_button_set_create( &hbbox, &cancel_btn, GTK_STOCK_CANCEL,
203                                       &ok_btn, GTK_STOCK_OK,
204                                       NULL, NULL );
205         gtk_box_pack_end( GTK_BOX(vbox), hbbox, FALSE, FALSE, 0 );
206         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
207         gtk_widget_grab_default( ok_btn );
208
209         g_signal_connect( G_OBJECT(ok_btn), "clicked",
210                          G_CALLBACK(addressbook_foldersel_ok), NULL );
211         g_signal_connect( G_OBJECT(cancel_btn), "clicked",
212                          G_CALLBACK(addressbook_foldersel_cancel), NULL );
213         g_signal_connect( G_OBJECT(tree_folder), "tree_select_row",
214                          G_CALLBACK(addressbook_foldersel_folder_select), NULL );
215         g_signal_connect( G_OBJECT(tree_folder), "button_press_event",
216                          G_CALLBACK(addressbook_foldersel_tree_button), NULL );
217
218         if ( !geometry.min_height ) {
219                 geometry.min_width = 300;
220                 geometry.min_height = 350;
221         }
222
223         gtk_window_set_geometry_hints( GTK_WINDOW(window), NULL, &geometry,
224                                       GDK_HINT_MIN_SIZE );
225         gtk_widget_set_size_request( window, prefs_common.addressbook_folderselwin_width,
226                                     prefs_common.addressbook_folderselwin_height );
227
228         gtk_widget_show_all( vbox );
229
230         addressbook_foldersel_dlg.window      = window;
231         addressbook_foldersel_dlg.tree_folder = tree_folder;
232         addressbook_foldersel_dlg.ok_btn      = ok_btn;
233         addressbook_foldersel_dlg.cancel_btn  = cancel_btn;
234
235         gtk_widget_show_all( window );
236
237         stock_pixbuf_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm);
238         stock_pixbuf_gdk( window, STOCK_PIXMAP_DIR_OPEN,
239                           &folderXpm);
240 }
241
242 static void addressbook_foldersel_load_folder( GtkCMCTreeNode *parentNode, ItemFolder *parentFolder,
243                                         FolderInfo *fiParent, FolderPathMatch *match )
244 {
245         GtkCMCTree *tree = GTK_CMCTREE( addressbook_foldersel_dlg.tree_folder );
246         GList *list;
247         ItemFolder *folder;
248         gchar *fName;
249         gchar **name;
250         GtkCMCTreeNode *node;
251         FolderInfo *fi;
252         FolderPathMatch *nextmatch = NULL;
253
254         list = parentFolder->listFolder;
255         while ( list ) {
256                 folder = list->data;
257                 fName = g_strdup( ADDRITEM_NAME(folder) );
258
259                 name = &fName;
260                 node = gtk_cmctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
261                                 folderXpm, folderXpm,
262                                 FALSE, TRUE );
263
264                 /* match folder name, match pointer will be set to NULL if next recursive call
265                    doesn't need to match subfolder name */
266                 if ( match != NULL &&
267                          match->matched == FALSE ) {
268                         if ( strcmp(match->folder_path[match->index], folder->obj.uid) == 0 ) {
269                                 /* folder name matches, prepare next subfolder match */
270
271                                 debug_print("matched folder name '%s'\n", fName);
272
273                                 match->index++;
274
275                                 if ( match->folder_path[match->index] == NULL ) {
276                                         /* we've matched all elements */
277                                         match->matched = TRUE;
278                                         match->node = node;
279                                         debug_print("book/folder path matched!\n");
280                                 } else {
281                                         /* keep on matching */
282                                         nextmatch = match;
283                                 }
284                         }
285                 }
286
287                 g_free( fName );
288
289                 fi = addressbook_foldersel_create_folderinfo( fiParent->book, folder );
290                 gtk_cmctree_node_set_row_data_full( tree, node, fi,
291                                 ( GDestroyNotify ) addressbook_foldersel_free_folderinfo );
292                 addressbook_foldersel_load_folder( node, folder, fi, nextmatch );
293                 list = g_list_next( list );
294         }
295 }
296
297 static void addressbook_foldersel_load_data( AddressIndex *addrIndex, 
298                                              FolderPathMatch* match )
299 {
300         AddressDataSource *ds;
301         GList *list, *nodeDS;
302         gchar **name;
303         gchar *dsName;
304         ItemFolder *rootFolder;
305         AddressBookFile *abf;
306         FolderInfo *fi;
307         GtkCMCTree *tree = GTK_CMCTREE( addressbook_foldersel_dlg.tree_folder );
308         GtkCMCTreeNode *node;
309         FolderPathMatch *nextmatch;
310
311         gtk_cmclist_clear( GTK_CMCLIST( tree ) );
312         list = addrindex_get_interface_list( addrIndex );
313         while ( list ) {
314                 AddressInterface *interface = list->data;
315                 if ( interface->type == ADDR_IF_BOOK ) {
316                         nodeDS = interface->listSource;
317                         while ( nodeDS ) {
318                                 ds = nodeDS->data;
319                                 dsName = g_strdup( addrindex_ds_get_name( ds ) );
320
321                                 /* Read address book */
322                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
323                                         addrindex_ds_read_data( ds );
324                                 }
325
326                                 /* Add node for address book */
327                                 abf = ds->rawDataSource;
328                                 name = &dsName;
329                                 node = gtk_cmctree_insert_node( tree, NULL, NULL,
330                                                 name, FOLDER_SPACING, bookXpm,
331                                                 bookXpm,
332                                                 FALSE, TRUE );
333                                 g_free( dsName );
334
335                                 /* try to match subfolders if this book is the right book
336                                         (and if there's smth to match, and not yet matched) */
337                                 nextmatch = NULL;
338                                 if ( match->folder_path != NULL &&
339                                          match->matched == FALSE &&
340                                          match->folder_path[0] != NULL &&
341                                          strcmp(match->folder_path[0], abf->fileName) == 0 ) {
342
343                                         debug_print("matched book name '%s'\n", abf->fileName);
344
345                                         match->index = 1;
346
347                                         if ( match->folder_path[match->index] == NULL ) {
348                                                 /* we've matched all elements */
349                                                 match->matched = TRUE;
350                                                 match->node = node;
351                                                 debug_print("book path matched!\n");
352                                         } else {
353                                                 /* keep on matching */
354                                                 nextmatch = match;
355                                         }
356                                 }
357
358                                 fi = addressbook_foldersel_create_folderinfo( abf, NULL );
359                                 gtk_cmctree_node_set_row_data_full( tree, node, fi,
360                                                 ( GDestroyNotify ) addressbook_foldersel_free_folderinfo );
361
362                                 rootFolder = addrindex_ds_get_root_folder( ds );
363                                 addressbook_foldersel_load_folder( node, rootFolder, fi, nextmatch );
364
365                                 nodeDS = g_list_next( nodeDS );
366                         }
367                 }
368                 list = g_list_next( list );
369         }
370 }
371
372 gboolean addressbook_foldersel_selection( AddressIndex *addrIndex,
373                                         AddressBookFile **book, ItemFolder **folder, 
374                                         const gchar* path)
375 {
376         FolderPathMatch folder_path_match = { NULL, FALSE, 0, NULL };
377         gboolean retVal = FALSE;
378         addressbook_foldersel_cancelled = FALSE;
379
380         if ( ! addressbook_foldersel_dlg.window )
381                 addressbook_foldersel_create();
382         gtk_widget_grab_focus(addressbook_foldersel_dlg.ok_btn);
383         gtk_widget_show(addressbook_foldersel_dlg.window);
384         manage_window_set_transient(GTK_WINDOW(addressbook_foldersel_dlg.window));
385
386         addressbook_foldersel_dlg.fiSelected = NULL;
387
388         /* split the folder path we've received, we'll try to match this path, subpath by
389            subpath against the book/folder structure in order to select the folder that
390        corresponds to what we received */
391
392         if ( path != NULL ) {
393                 if ( g_utf8_collate(path, _("Any")) == 0 || strcasecmp(path, "Any") ==0 || *path == '\0' )
394                         /* consider "Any" (both translated or untranslated forms) and ""
395                            as valid addressbook roots */
396                         folder_path_match.matched = TRUE;
397                 else
398                         folder_path_match.folder_path = g_strsplit( path, "/", 256 );
399         }
400
401         addressbook_foldersel_load_data( addrIndex, &folder_path_match );
402
403         if ( folder_path_match.folder_path != NULL && folder_path_match.matched == FALSE)
404                 g_warning("addressbook_foldersel_load_data: couldn't match book/folder path '%s'\n", path);
405
406         g_strfreev( folder_path_match.folder_path );
407
408         if ( folder_path_match.node != NULL)
409                 gtk_cmctree_select( GTK_CMCTREE( addressbook_foldersel_dlg.tree_folder ),
410                                                         GTK_CMCTREE_NODE( folder_path_match.node ) );
411         else
412                 gtk_cmclist_select_row( GTK_CMCLIST( addressbook_foldersel_dlg.tree_folder ), 0, 0 );
413         gtk_widget_show(addressbook_foldersel_dlg.window);
414
415         gtk_main();
416         gtk_widget_hide( addressbook_foldersel_dlg.window );
417
418         if ( ! addressbook_foldersel_cancelled ) {
419
420                 *book = NULL;
421                 *folder = NULL;
422
423                 if ( addressbook_foldersel_dlg.fiSelected ) {
424                         *book = addressbook_foldersel_dlg.fiSelected->book;
425                         *folder = addressbook_foldersel_dlg.fiSelected->folder;
426                         retVal = TRUE;
427                 }
428         }
429
430         gtk_cmclist_clear( GTK_CMCLIST( addressbook_foldersel_dlg.tree_folder ) );
431
432         return retVal;
433 }
434
435 /*
436 * End of Source.
437 */