2007-02-01 [colin] 2.7.2cvs10
[claws.git] / src / addressadd.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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 static struct _AddressAdd_dlg {
56         GtkWidget *window;
57         GtkWidget *entry_name;
58         GtkWidget *label_address;
59         GtkWidget *entry_remarks;
60         GtkWidget *tree_folder;
61         GtkWidget *ok_btn;
62         GtkWidget *cancel_btn;
63         GtkWidget *statusbar;
64         gint status_cid;
65         FolderInfo *fiSelected;
66 } addressadd_dlg;
67
68 static GdkPixmap *folderXpm;
69 static GdkBitmap *folderXpmMask;
70 static GdkPixmap *bookXpm;
71 static GdkBitmap *bookXpmMask;
72
73 static gboolean addressadd_cancelled;
74
75 static FolderInfo *addressadd_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
76 {
77         FolderInfo *fi = g_new0( FolderInfo, 1 );
78         fi->book   = abf;
79         fi->folder = folder;
80         return fi;
81 }
82
83 static void addressadd_free_folderinfo( FolderInfo *fi ) {
84         fi->book   = NULL;
85         fi->folder = NULL;
86         g_free( fi );
87 }
88
89 /*
90 * Edit functions.
91 */
92 static void addressadd_status_show( gchar *msg ) {
93         if( addressadd_dlg.statusbar != NULL ) {
94                 gtk_statusbar_pop( GTK_STATUSBAR(addressadd_dlg.statusbar), addressadd_dlg.status_cid );
95                 if( msg ) {
96                         gtk_statusbar_push( GTK_STATUSBAR(addressadd_dlg.statusbar), addressadd_dlg.status_cid, msg );
97                 }
98         }
99 }
100
101 static gint addressadd_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
102         addressadd_cancelled = TRUE;
103         gtk_main_quit();
104         return TRUE;
105 }
106
107 static gboolean addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
108         if (event && event->keyval == GDK_Escape) {
109                 addressadd_cancelled = TRUE;
110                 gtk_main_quit();
111         }
112         return FALSE;
113 }
114
115 static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
116         addressadd_cancelled = FALSE;
117         gtk_main_quit();
118 }
119
120 static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
121         addressadd_cancelled = TRUE;
122         gtk_main_quit();
123 }
124
125 static void addressadd_folder_select( GtkCTree *ctree, gint row, gint column,
126                                         GdkEvent *event, gpointer data )
127 {
128         addressadd_dlg.fiSelected = gtk_clist_get_row_data( GTK_CLIST(ctree), row );
129 }
130
131 static gboolean addressadd_tree_button( GtkCTree *ctree, GdkEventButton *event, gpointer data ) {
132         if( ! event ) return FALSE;
133         if( event->button == 1 ) {
134                 /* Handle double click */
135                 if( event->type == GDK_2BUTTON_PRESS ) {
136                         addressadd_cancelled = FALSE;
137                         gtk_main_quit();
138                 }
139         }
140
141         return FALSE;
142 }
143
144 static void addressadd_size_allocate_cb(GtkWidget *widget,
145                                          GtkAllocation *allocation)
146 {
147         g_return_if_fail(allocation != NULL);
148
149         prefs_common.addressaddwin_width = allocation->width;
150         prefs_common.addressaddwin_height = allocation->height;
151 }
152
153 static void addressadd_create( void ) {
154         GtkWidget *window;
155         GtkWidget *vbox;
156         GtkWidget *table;
157         GtkWidget *label;
158         GtkWidget *entry_name;
159         GtkWidget *label_addr;
160         GtkWidget *entry_rems;
161         GtkWidget *tree_folder;
162         GtkWidget *vlbox;
163         GtkWidget *tree_win;
164         GtkWidget *hbbox;
165         GtkWidget *ok_btn;
166         GtkWidget *cancel_btn;
167         GtkWidget *hsbox;
168         GtkWidget *statusbar;
169         gint top;
170         static GdkGeometry geometry;
171
172         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
173         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
174         gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
175         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
176         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
177         g_signal_connect( G_OBJECT(window), "delete_event",
178                           G_CALLBACK(addressadd_delete_event), NULL );
179         g_signal_connect( G_OBJECT(window), "key_press_event",
180                           G_CALLBACK(addressadd_key_pressed), NULL );
181         g_signal_connect(G_OBJECT(window), "size_allocate",
182                          G_CALLBACK(addressadd_size_allocate_cb), NULL);
183
184         vbox = gtk_vbox_new(FALSE, 8);
185         gtk_container_add(GTK_CONTAINER(window), vbox);
186         gtk_container_set_border_width( GTK_CONTAINER(vbox), 8 );
187
188         table = gtk_table_new(3, 2, FALSE);
189         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
190         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
191         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
192         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
193
194         /* First row */
195         top = 0;
196         label = gtk_label_new(_("Name"));
197         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
198         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
199
200         entry_name = gtk_entry_new();
201         /* gtk_entry_set_max_length(); */
202         gtk_entry_set_text (GTK_ENTRY(entry_name),"");
203         
204         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
205
206         /* Second row */
207         top = 1;
208         label = gtk_label_new(_("Address"));
209         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
210         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
211
212         label_addr = gtk_label_new("");
213         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
214         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
215
216         /* Third row */
217         top = 2;
218         label = gtk_label_new(_("Remarks"));
219         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
220         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
221
222         entry_rems = gtk_entry_new();
223         /* gtk_entry_set_max_length(); */
224         gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
225
226         /* Address book/folder tree */
227         vlbox = gtk_vbox_new(FALSE, 8);
228         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
229         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
230
231         tree_win = gtk_scrolled_window_new( NULL, NULL );
232         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
233                                         GTK_POLICY_AUTOMATIC,
234                                         GTK_POLICY_AUTOMATIC );
235         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
236
237         tree_folder = gtk_ctree_new( 1, 0 );
238         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
239         gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
240         gtk_clist_set_column_title( GTK_CLIST(tree_folder), 0, _( "Select Address Book Folder" ) );
241         gtk_ctree_set_line_style( GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED );
242         gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
243         gtk_ctree_set_expander_style( GTK_CTREE(tree_folder), GTK_CTREE_EXPANDER_SQUARE );
244         gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
245         gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
246
247         /* Status line */
248         hsbox = gtk_hbox_new(FALSE, 0);
249         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
250         statusbar = gtk_statusbar_new();
251         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
252
253         /* Button panel */
254         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
255                                       &ok_btn, GTK_STOCK_OK,
256                                       NULL, NULL);
257         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
258         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
259         gtk_widget_grab_default(ok_btn);
260
261         g_signal_connect(G_OBJECT(ok_btn), "clicked",
262                          G_CALLBACK(addressadd_ok), NULL);
263         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
264                          G_CALLBACK(addressadd_cancel), NULL);
265         g_signal_connect(G_OBJECT(tree_folder), "select_row",
266                          G_CALLBACK(addressadd_folder_select), NULL);
267         g_signal_connect(G_OBJECT(tree_folder), "button_press_event",
268                          G_CALLBACK(addressadd_tree_button), NULL);
269
270         if (!geometry.min_height) {
271                 geometry.min_width = 300;
272                 geometry.min_height = 400;
273         }
274
275         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
276                                       GDK_HINT_MIN_SIZE);
277         gtk_widget_set_size_request(window, prefs_common.addressaddwin_width,
278                                     prefs_common.addressaddwin_height);
279
280         gtk_widget_show_all(vbox);
281
282         addressadd_dlg.window        = window;
283         addressadd_dlg.entry_name    = entry_name;
284         addressadd_dlg.label_address = label_addr;
285         addressadd_dlg.entry_remarks = entry_rems;
286         addressadd_dlg.tree_folder   = tree_folder;
287         addressadd_dlg.ok_btn        = ok_btn;
288         addressadd_dlg.cancel_btn    = cancel_btn;
289         addressadd_dlg.statusbar     = statusbar;
290         addressadd_dlg.status_cid    = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Address Add" );
291
292         gtk_widget_show_all( window );
293
294         stock_pixmap_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm, &bookXpmMask );
295         stock_pixmap_gdk( window, STOCK_PIXMAP_DIR_OPEN,
296                           &folderXpm, &folderXpmMask );
297 }
298
299 static void addressadd_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
300                                         FolderInfo *fiParent )
301 {
302         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
303         GList *list;
304         ItemFolder *folder;
305         gchar *fName;
306         gchar **name;
307         GtkCTreeNode *node;
308         FolderInfo *fi;
309
310         list = parentFolder->listFolder;
311         while( list ) {
312                 folder = list->data;
313                 fName = g_strdup( ADDRITEM_NAME(folder) );
314                 name = &fName;
315                 node = gtk_sctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
316                                 folderXpm, folderXpmMask, folderXpm, folderXpmMask,
317                                 FALSE, TRUE );
318                 g_free( fName );
319                 fi = addressadd_create_folderinfo( fiParent->book, folder );
320                 gtk_ctree_node_set_row_data_full( tree, node, fi,
321                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
322                 addressadd_load_folder( node, folder, fi );
323                 list = g_list_next( list );
324         }
325 }
326
327 static void addressadd_load_data( AddressIndex *addrIndex ) {
328         AddressDataSource *ds;
329         GList *list, *nodeDS;
330         gchar **name;
331         gchar *dsName;
332         ItemFolder *rootFolder;
333         AddressBookFile *abf;
334         FolderInfo *fi;
335         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
336         GtkCTreeNode *node;
337
338         gtk_clist_clear( GTK_CLIST( tree ) );
339         list = addrindex_get_interface_list( addrIndex );
340         while( list ) {
341                 AddressInterface *interface = list->data;
342                 if( interface->type == ADDR_IF_BOOK ) {
343                         nodeDS = interface->listSource;
344                         while( nodeDS ) {
345                                 ds = nodeDS->data;
346                                 dsName = g_strdup( addrindex_ds_get_name( ds ) );
347
348                                 /* Read address book */
349                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
350                                         addrindex_ds_read_data( ds );
351                                 }
352
353                                 /* Add node for address book */
354                                 abf = ds->rawDataSource;
355                                 name = &dsName;
356                                 node = gtk_ctree_insert_node( tree, NULL, NULL,
357                                                 name, FOLDER_SPACING, bookXpm,
358                                                 bookXpmMask, bookXpm, bookXpmMask,
359                                                 FALSE, TRUE );
360                                 g_free( dsName );
361
362                                 fi = addressadd_create_folderinfo( abf, NULL );
363                                 gtk_ctree_node_set_row_data_full( tree, node, fi,
364                                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
365
366                                 rootFolder = addrindex_ds_get_root_folder( ds );
367                                 addressadd_load_folder( node, rootFolder, fi );
368
369                                 nodeDS = g_list_next( nodeDS );
370                         }
371                 }
372                 list = g_list_next( list );
373         }
374 }
375
376 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, const gchar *address, const gchar *remarks ) {
377         gboolean retVal = FALSE;
378         ItemPerson *person = NULL;
379         FolderInfo *fi = NULL;
380         addressadd_cancelled = FALSE;
381         if( ! addressadd_dlg.window ) addressadd_create();
382         gtk_widget_grab_focus(addressadd_dlg.ok_btn);
383         gtk_widget_show(addressadd_dlg.window);
384         manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
385
386         addressadd_dlg.fiSelected = NULL;
387         addressadd_status_show( "" );
388         addressadd_load_data( addrIndex );
389         gtk_clist_select_row( GTK_CLIST( addressadd_dlg.tree_folder ), 0, 0 );
390         gtk_widget_show(addressadd_dlg.window);
391
392         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
393         gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
394         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
395         if( name )
396                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
397         if( address )
398                 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
399         if( remarks )
400                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
401
402         gtk_main();
403         gtk_widget_hide( addressadd_dlg.window );
404
405         if( ! addressadd_cancelled ) {
406                 if( addressadd_dlg.fiSelected ) {
407                         gchar *returned_name;
408                         gchar *returned_remarks;
409                         returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
410                         returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
411
412                         fi = addressadd_dlg.fiSelected;
413                         
414                         person = addrbook_add_contact( fi->book, fi->folder, 
415                                                         returned_name, 
416                                                         address, 
417                                                         returned_remarks);
418                         g_free(returned_name);
419                         g_free(returned_remarks);
420                         if( person ) retVal = TRUE;
421                 }
422         }
423
424         gtk_clist_clear( GTK_CLIST( addressadd_dlg.tree_folder ) );
425
426         return retVal;
427 }
428
429 /*
430 * End of Source.
431 */
432