2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2009 Match Grun and the Claws Mail team
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.
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.
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/>.
21 * Add address to address book dialog.
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
36 #include "stock_pixmap.h"
37 #include "prefs_common.h"
38 #include "addressadd.h"
41 #include "addrindex.h"
42 #include "manage_window.h"
45 AddressBookFile *book;
56 static struct _AddressBookFolderSel_dlg {
58 GtkWidget *tree_folder;
60 GtkWidget *cancel_btn;
62 FolderInfo *fiSelected;
63 } addressbook_foldersel_dlg;
65 static GdkPixbuf *folderXpm;
66 static GdkPixbuf *bookXpm;
68 static gboolean addressbook_foldersel_cancelled;
70 static FolderInfo *addressbook_foldersel_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
72 FolderInfo *fi = g_new0( FolderInfo, 1 );
78 static void addressbook_foldersel_free_folderinfo( FolderInfo *fi ) {
84 static gint addressbook_foldersel_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled )
86 addressbook_foldersel_cancelled = TRUE;
91 static gboolean addressbook_foldersel_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled )
93 if ( event && event->keyval == GDK_Escape ) {
94 addressbook_foldersel_cancelled = TRUE;
100 static void addressbook_foldersel_ok( GtkWidget *widget, gboolean *cancelled )
102 addressbook_foldersel_cancelled = FALSE;
106 static void addressbook_foldersel_cancel( GtkWidget *widget, gboolean *cancelled )
108 addressbook_foldersel_cancelled = TRUE;
112 static void addressbook_foldersel_folder_select( GtkCMCTree *ctree, GtkCMCTreeNode *node,
113 gint column, gpointer data )
115 addressbook_foldersel_dlg.fiSelected = gtk_cmctree_node_get_row_data( ctree, node );
118 static gboolean addressbook_foldersel_tree_button( GtkCMCTree *ctree, GdkEventButton *event, gpointer data )
122 if ( event->button == 1 ) {
123 /* Handle double click */
124 if ( event->type == GDK_2BUTTON_PRESS ) {
125 addressbook_foldersel_cancelled = FALSE;
133 static void addressbook_foldersel_size_allocate_cb(GtkWidget *widget,
134 GtkAllocation *allocation)
136 cm_return_if_fail(allocation != NULL);
138 prefs_common.addressbook_folderselwin_width = allocation->width;
139 prefs_common.addressbook_folderselwin_height = allocation->height;
142 static void addressbook_foldersel_create( void )
146 GtkWidget *tree_folder;
151 GtkWidget *cancel_btn;
152 static GdkGeometry geometry;
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 g_signal_connect( G_OBJECT(window), "delete_event",
160 G_CALLBACK(addressbook_foldersel_delete_event), NULL );
161 g_signal_connect( G_OBJECT(window), "key_press_event",
162 G_CALLBACK(addressbook_foldersel_key_pressed), NULL );
163 g_signal_connect(G_OBJECT(window), "size_allocate",
164 G_CALLBACK(addressbook_foldersel_size_allocate_cb), NULL);
166 vbox = gtk_vbox_new(FALSE, 8);
167 gtk_container_add(GTK_CONTAINER(window), vbox);
168 gtk_container_set_border_width( GTK_CONTAINER(vbox), 8 );
170 /* Address book/folder tree */
171 vlbox = gtk_vbox_new(FALSE, 8);
172 gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
173 gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
175 tree_win = gtk_scrolled_window_new( NULL, NULL );
176 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
177 GTK_POLICY_AUTOMATIC,
178 GTK_POLICY_AUTOMATIC );
179 gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
181 titles[0] = _( "Address Book") ;
183 tree_folder = gtk_sctree_new_with_titles( 1, 0, titles );
184 gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
185 gtk_cmclist_column_titles_show( GTK_CMCLIST(tree_folder) );
186 if (prefs_common.enable_dotted_lines) {
187 gtk_cmctree_set_line_style(GTK_CMCTREE(tree_folder), GTK_CMCTREE_LINES_DOTTED);
188 gtk_cmctree_set_expander_style(GTK_CMCTREE(tree_folder),
189 GTK_CMCTREE_EXPANDER_SQUARE);
191 gtk_cmctree_set_line_style(GTK_CMCTREE(tree_folder), GTK_CMCTREE_LINES_NONE);
192 gtk_cmctree_set_expander_style(GTK_CMCTREE(tree_folder),
193 GTK_CMCTREE_EXPANDER_TRIANGLE);
195 gtk_sctree_set_stripes(GTK_SCTREE(tree_folder), prefs_common.use_stripes_everywhere);
196 gtk_cmclist_set_selection_mode( GTK_CMCLIST(tree_folder), GTK_SELECTION_BROWSE );
197 gtk_cmctree_set_indent( GTK_CMCTREE(tree_folder), CTREE_INDENT );
198 gtk_cmclist_set_auto_sort( GTK_CMCLIST(tree_folder), TRUE );
201 gtkut_stock_button_set_create( &hbbox, &cancel_btn, GTK_STOCK_CANCEL,
202 &ok_btn, GTK_STOCK_OK,
204 gtk_box_pack_end( GTK_BOX(vbox), hbbox, FALSE, FALSE, 0 );
205 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
206 gtk_widget_grab_default( ok_btn );
208 g_signal_connect( G_OBJECT(ok_btn), "clicked",
209 G_CALLBACK(addressbook_foldersel_ok), NULL );
210 g_signal_connect( G_OBJECT(cancel_btn), "clicked",
211 G_CALLBACK(addressbook_foldersel_cancel), NULL );
212 g_signal_connect( G_OBJECT(tree_folder), "tree_select_row",
213 G_CALLBACK(addressbook_foldersel_folder_select), NULL );
214 g_signal_connect( G_OBJECT(tree_folder), "button_press_event",
215 G_CALLBACK(addressbook_foldersel_tree_button), NULL );
217 if ( !geometry.min_height ) {
218 geometry.min_width = 300;
219 geometry.min_height = 350;
222 gtk_window_set_geometry_hints( GTK_WINDOW(window), NULL, &geometry,
224 gtk_widget_set_size_request( window, prefs_common.addressbook_folderselwin_width,
225 prefs_common.addressbook_folderselwin_height );
227 gtk_widget_show_all( vbox );
229 addressbook_foldersel_dlg.window = window;
230 addressbook_foldersel_dlg.tree_folder = tree_folder;
231 addressbook_foldersel_dlg.ok_btn = ok_btn;
232 addressbook_foldersel_dlg.cancel_btn = cancel_btn;
234 gtk_widget_show_all( window );
236 stock_pixbuf_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm);
237 stock_pixbuf_gdk( window, STOCK_PIXMAP_DIR_OPEN,
241 static void addressbook_foldersel_load_folder( GtkCMCTreeNode *parentNode, ItemFolder *parentFolder,
242 FolderInfo *fiParent, FolderPathMatch *match )
244 GtkCMCTree *tree = GTK_CMCTREE( addressbook_foldersel_dlg.tree_folder );
249 GtkCMCTreeNode *node;
251 FolderPathMatch *nextmatch = NULL;
253 list = parentFolder->listFolder;
256 fName = g_strdup( ADDRITEM_NAME(folder) );
259 node = gtk_cmctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
260 folderXpm, folderXpm,
263 /* match folder name, match pointer will be set to NULL if next recursive call
264 doesn't need to match subfolder name */
265 if ( match != NULL &&
266 match->matched == FALSE ) {
267 if ( strcmp(match->folder_path[match->index], folder->obj.uid) == 0 ) {
268 /* folder name matches, prepare next subfolder match */
270 debug_print("matched folder name '%s'\n", fName);
274 if ( match->folder_path[match->index] == NULL ) {
275 /* we've matched all elements */
276 match->matched = TRUE;
278 debug_print("book/folder path matched!\n");
280 /* keep on matching */
288 fi = addressbook_foldersel_create_folderinfo( fiParent->book, folder );
289 gtk_cmctree_node_set_row_data_full( tree, node, fi,
290 ( GDestroyNotify ) addressbook_foldersel_free_folderinfo );
291 addressbook_foldersel_load_folder( node, folder, fi, nextmatch );
292 list = g_list_next( list );
296 static void addressbook_foldersel_load_data( AddressIndex *addrIndex,
297 FolderPathMatch* match )
299 AddressDataSource *ds;
300 GList *list, *nodeDS;
303 ItemFolder *rootFolder;
304 AddressBookFile *abf;
306 GtkCMCTree *tree = GTK_CMCTREE( addressbook_foldersel_dlg.tree_folder );
307 GtkCMCTreeNode *node;
308 FolderPathMatch *nextmatch;
310 gtk_cmclist_clear( GTK_CMCLIST( tree ) );
311 list = addrindex_get_interface_list( addrIndex );
313 AddressInterface *interface = list->data;
314 if ( interface->type == ADDR_IF_BOOK ) {
315 nodeDS = interface->listSource;
318 dsName = g_strdup( addrindex_ds_get_name( ds ) );
320 /* Read address book */
321 if( ! addrindex_ds_get_read_flag( ds ) ) {
322 addrindex_ds_read_data( ds );
325 /* Add node for address book */
326 abf = ds->rawDataSource;
328 node = gtk_cmctree_insert_node( tree, NULL, NULL,
329 name, FOLDER_SPACING, bookXpm,
334 /* try to match subfolders if this book is the right book
335 (and if there's smth to match, and not yet matched) */
337 if ( match->folder_path != NULL &&
338 match->matched == FALSE &&
339 match->folder_path[0] != NULL &&
340 strcmp(match->folder_path[0], abf->fileName) == 0 ) {
342 debug_print("matched book name '%s'\n", abf->fileName);
346 if ( match->folder_path[match->index] == NULL ) {
347 /* we've matched all elements */
348 match->matched = TRUE;
350 debug_print("book path matched!\n");
352 /* keep on matching */
357 fi = addressbook_foldersel_create_folderinfo( abf, NULL );
358 gtk_cmctree_node_set_row_data_full( tree, node, fi,
359 ( GDestroyNotify ) addressbook_foldersel_free_folderinfo );
361 rootFolder = addrindex_ds_get_root_folder( ds );
362 addressbook_foldersel_load_folder( node, rootFolder, fi, nextmatch );
364 nodeDS = g_list_next( nodeDS );
367 list = g_list_next( list );
371 gboolean addressbook_foldersel_selection( AddressIndex *addrIndex,
372 AddressBookFile **book, ItemFolder **folder,
375 FolderPathMatch folder_path_match = { NULL, FALSE, 0, NULL };
376 gboolean retVal = FALSE;
377 addressbook_foldersel_cancelled = FALSE;
379 if ( ! addressbook_foldersel_dlg.window )
380 addressbook_foldersel_create();
381 gtk_widget_grab_focus(addressbook_foldersel_dlg.ok_btn);
382 gtk_widget_show(addressbook_foldersel_dlg.window);
383 manage_window_set_transient(GTK_WINDOW(addressbook_foldersel_dlg.window));
384 gtk_window_set_modal(GTK_WINDOW(addressbook_foldersel_dlg.window), TRUE);
386 addressbook_foldersel_dlg.fiSelected = NULL;
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 */
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;
398 folder_path_match.folder_path = g_strsplit( path, "/", 256 );
401 addressbook_foldersel_load_data( addrIndex, &folder_path_match );
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);
406 g_strfreev( folder_path_match.folder_path );
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 ) );
412 gtk_cmclist_select_row( GTK_CMCLIST( addressbook_foldersel_dlg.tree_folder ), 0, 0 );
413 gtk_widget_show(addressbook_foldersel_dlg.window);
416 gtk_widget_hide( addressbook_foldersel_dlg.window );
417 gtk_window_set_modal(GTK_WINDOW(addressbook_foldersel_dlg.window), FALSE);
418 if ( ! addressbook_foldersel_cancelled ) {
423 if ( addressbook_foldersel_dlg.fiSelected ) {
424 *book = addressbook_foldersel_dlg.fiSelected->book;
425 *folder = addressbook_foldersel_dlg.fiSelected->folder;
430 gtk_cmclist_clear( GTK_CMCLIST( addressbook_foldersel_dlg.tree_folder ) );