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