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