14ca696b14dddfa4ded50f6be6d8378e9117495e
[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 enum {
46         COL_ICON,
47         COL_NAME,
48         COL_PTR,
49         N_COLS
50 };
51
52 typedef struct {
53         AddressBookFile *book;
54         ItemFolder      *folder;
55 } FolderInfo;
56
57 static struct _AddressBookFolderSel_dlg {
58         GtkWidget *window;
59         GtkWidget *view_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 set_selected_ptr()
102 {
103         GtkWidget *view = addressbook_foldersel_dlg.view_folder;
104
105         addressbook_foldersel_dlg.fiSelected =
106                 gtkut_tree_view_get_selected_pointer(GTK_TREE_VIEW(view), COL_PTR,
107                                 NULL, NULL, NULL);
108 }
109
110 static void addressbook_foldersel_ok( GtkWidget *widget, gboolean *cancelled )
111 {
112         set_selected_ptr();
113         addressbook_foldersel_cancelled = FALSE;
114         gtk_main_quit();
115 }
116
117 static void addressbook_foldersel_cancel( GtkWidget *widget, gboolean *cancelled )
118 {
119         set_selected_ptr();
120         addressbook_foldersel_cancelled = TRUE;
121         gtk_main_quit();
122 }
123
124 static void addressbook_foldersel_row_activated(GtkTreeView *view,
125                 GtkTreePath *path, GtkTreeViewColumn *col,
126                 gpointer user_data)
127 {
128         addressbook_foldersel_ok(NULL, NULL);
129 }
130
131 static void addressbook_foldersel_size_allocate_cb(GtkWidget *widget,
132                                          GtkAllocation *allocation)
133 {
134         cm_return_if_fail(allocation != NULL);
135
136         prefs_common.addressbook_folderselwin_width = allocation->width;
137         prefs_common.addressbook_folderselwin_height = allocation->height;
138 }
139
140 static void addressbook_foldersel_create( void )
141 {
142         GtkWidget *window;
143         GtkWidget *vbox;
144         GtkWidget *view;
145         GtkWidget *vlbox;
146         GtkWidget *tree_win;
147         GtkWidget *hbbox;
148         GtkWidget *ok_btn;
149         GtkWidget *cancel_btn;
150         static GdkGeometry geometry;
151         GtkTreeStore *store;
152         GtkTreeSelection *sel;
153         GtkCellRenderer *rdr;
154         GtkTreeViewColumn *col;
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         store = gtk_tree_store_new(N_COLS,
183                         GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_POINTER);
184
185         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
186         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
187         gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(view), FALSE);
188         gtk_tree_view_set_search_column(GTK_TREE_VIEW(view), COL_NAME);
189
190         col = gtk_tree_view_column_new();
191         rdr = gtk_cell_renderer_pixbuf_new();
192         gtk_cell_renderer_set_padding(rdr, 0, 0);
193         gtk_tree_view_column_pack_start(col, rdr, FALSE);
194         gtk_tree_view_column_set_attributes(col, rdr,
195                         "pixbuf", COL_ICON, NULL);
196         rdr = gtk_cell_renderer_text_new();
197         gtk_tree_view_column_pack_start(col, rdr, TRUE);
198         gtk_tree_view_column_set_attributes(col, rdr,
199                         "markup", COL_NAME, NULL);
200         gtk_tree_view_column_set_title(col, _("Address Book"));
201         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
202
203         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
204         gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
205
206         gtk_container_add( GTK_CONTAINER(tree_win), view );
207
208         /* Button panel */
209         gtkut_stock_button_set_create( &hbbox, &cancel_btn, GTK_STOCK_CANCEL,
210                                       &ok_btn, GTK_STOCK_OK,
211                                       NULL, NULL );
212         gtk_box_pack_end( GTK_BOX(vbox), hbbox, FALSE, FALSE, 0 );
213         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
214         gtk_widget_grab_default( ok_btn );
215
216         g_signal_connect( G_OBJECT(view), "row-activated",
217                         G_CALLBACK(addressbook_foldersel_row_activated), NULL);
218         g_signal_connect( G_OBJECT(ok_btn), "clicked",
219                          G_CALLBACK(addressbook_foldersel_ok), NULL );
220         g_signal_connect( G_OBJECT(cancel_btn), "clicked",
221                          G_CALLBACK(addressbook_foldersel_cancel), NULL );
222
223         if ( !geometry.min_height ) {
224                 geometry.min_width = 300;
225                 geometry.min_height = 350;
226         }
227
228         gtk_window_set_geometry_hints( GTK_WINDOW(window), NULL, &geometry,
229                                       GDK_HINT_MIN_SIZE );
230         gtk_widget_set_size_request( window, prefs_common.addressbook_folderselwin_width,
231                                     prefs_common.addressbook_folderselwin_height );
232
233         gtk_widget_show_all( vbox );
234
235         addressbook_foldersel_dlg.window      = window;
236         addressbook_foldersel_dlg.view_folder = view;
237         addressbook_foldersel_dlg.ok_btn      = ok_btn;
238         addressbook_foldersel_dlg.cancel_btn  = cancel_btn;
239
240         gtk_widget_show_all( window );
241
242         stock_pixbuf_gdk(STOCK_PIXMAP_BOOK, &bookXpm);
243         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN, &folderXpm);
244 }
245
246 static gboolean tree_clear_foreach_func(GtkTreeModel *model,
247                 GtkTreePath *path, GtkTreeIter *iter,
248                 gpointer user_data)
249 {
250         FolderInfo *fi;
251
252         gtk_tree_model_get(model, iter, COL_PTR, &fi, -1);
253         if (fi != NULL) {
254                 addressbook_foldersel_free_folderinfo(fi);
255         }
256         gtk_tree_store_set(GTK_TREE_STORE(model), iter, COL_PTR, NULL, -1);
257         return FALSE;
258 }
259
260 static void addressbook_foldersel_tree_clear()
261 {
262         GtkWidget *view = addressbook_foldersel_dlg.view_folder;
263         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
264
265         gtk_tree_model_foreach(model, tree_clear_foreach_func, NULL);
266         gtk_tree_store_clear(GTK_TREE_STORE(model));
267 }
268
269 static void addressbook_foldersel_load_folder( GtkTreeIter *parent_iter,
270                 ItemFolder *parentFolder, FolderInfo *fiParent)
271 {
272         GtkWidget *view = addressbook_foldersel_dlg.view_folder;
273         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
274         GtkTreeIter iter;
275         GList *list;
276         ItemFolder *folder;
277         gchar *name;
278         FolderInfo *fi;
279
280         list = parentFolder->listFolder;
281         while ( list ) {
282                 folder = list->data;
283                 name = g_strdup( ADDRITEM_NAME(folder) );
284
285                 fi = addressbook_foldersel_create_folderinfo( fiParent->book, folder );
286
287                 debug_print("adding folder '%s'\n", name);
288                 gtk_tree_store_append(GTK_TREE_STORE(model), &iter, parent_iter);
289                 gtk_tree_store_set(GTK_TREE_STORE(model), &iter,
290                                 COL_ICON, folderXpm,
291                                 COL_NAME, name,
292                                 COL_PTR, fi,
293                                 -1);
294                 g_free(name);
295
296                 addressbook_foldersel_load_folder( parent_iter, folder, fi );
297                 list = g_list_next( list );
298         }
299 }
300
301 static void addressbook_foldersel_load_data( AddressIndex *addrIndex )
302 {
303         AddressDataSource *ds;
304         GList *list, *nodeDS;
305         gchar *name;
306         ItemFolder *rootFolder;
307         AddressBookFile *abf;
308         FolderInfo *fi;
309         GtkWidget *view = addressbook_foldersel_dlg.view_folder;
310         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
311         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
312         GtkTreeIter iter;
313
314         addressbook_foldersel_tree_clear();
315
316         list = addrindex_get_interface_list( addrIndex );
317         while ( list ) {
318                 AddressInterface *interface = list->data;
319                 if ( interface->type == ADDR_IF_BOOK ) {
320                         nodeDS = interface->listSource;
321                         while ( nodeDS ) {
322                                 ds = nodeDS->data;
323                                 name = g_strdup( addrindex_ds_get_name( ds ) );
324
325                                 /* Read address book */
326                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
327                                         addrindex_ds_read_data( ds );
328                                 }
329
330                                 /* Add node for address book */
331                                 abf = ds->rawDataSource;
332                                 fi = addressbook_foldersel_create_folderinfo( abf, NULL );
333
334                                 debug_print("adding AB '%s'\n", name);
335                                 gtk_tree_store_append(GTK_TREE_STORE(model), &iter, NULL);
336                                 gtk_tree_store_set(GTK_TREE_STORE(model), &iter,
337                                                 COL_ICON, bookXpm,
338                                                 COL_NAME, name,
339                                                 COL_PTR, fi,
340                                                 -1);
341                                 g_free(name);
342
343                                 rootFolder = addrindex_ds_get_root_folder( ds );
344                                 addressbook_foldersel_load_folder( &iter, rootFolder, fi );
345
346                                 nodeDS = g_list_next( nodeDS );
347                         }
348                 }
349                 list = g_list_next( list );
350         }
351
352         if (gtk_tree_model_get_iter_first(model, &iter))
353                 gtk_tree_selection_select_iter(sel, &iter);
354 }
355
356 gboolean addressbook_foldersel_selection( AddressIndex *addrIndex,
357                                         AddressBookFile **book, ItemFolder **folder, 
358                                         const gchar* path)
359 {
360         gboolean retVal = FALSE;
361         addressbook_foldersel_cancelled = FALSE;
362
363         if ( ! addressbook_foldersel_dlg.window )
364                 addressbook_foldersel_create();
365         gtk_widget_grab_focus(addressbook_foldersel_dlg.ok_btn);
366         gtk_widget_show(addressbook_foldersel_dlg.window);
367         manage_window_set_transient(GTK_WINDOW(addressbook_foldersel_dlg.window));
368         gtk_window_set_modal(GTK_WINDOW(addressbook_foldersel_dlg.window), TRUE);
369         
370         addressbook_foldersel_dlg.fiSelected = NULL;
371
372         addressbook_foldersel_load_data( addrIndex );
373
374         gtk_widget_show(addressbook_foldersel_dlg.window);
375
376         gtk_main();
377         gtk_widget_hide( addressbook_foldersel_dlg.window );
378         gtk_window_set_modal(GTK_WINDOW(addressbook_foldersel_dlg.window), FALSE);
379         if ( ! addressbook_foldersel_cancelled ) {
380
381                 *book = NULL;
382                 *folder = NULL;
383
384                 if ( addressbook_foldersel_dlg.fiSelected ) {
385                         *book = addressbook_foldersel_dlg.fiSelected->book;
386                         *folder = addressbook_foldersel_dlg.fiSelected->folder;
387                         retVal = TRUE;
388                 }
389         }
390
391         addressbook_foldersel_tree_clear();
392
393         return retVal;
394 }
395
396 /*
397 * End of Source.
398 */