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