* src/addressadd.c
[claws.git] / src / addressadd.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 Match Grun
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <gdk/gdkkeysyms.h>
32 #include <gtk/gtkwindow.h>
33 #include <gtk/gtksignal.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtkhbbox.h>
38 #include <gtk/gtkbutton.h>
39
40 #include "intl.h"
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 void 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 }
113
114 static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
115         addressadd_cancelled = FALSE;
116         gtk_main_quit();
117 }
118
119 static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
120         addressadd_cancelled = TRUE;
121         gtk_main_quit();
122 }
123
124 static void addressadd_folder_select( GtkCTree *ctree, gint row, gint column,
125                                         GdkEvent *event, gpointer data )
126 {
127         addressadd_dlg.fiSelected = gtk_clist_get_row_data( GTK_CLIST(ctree), row );
128 }
129
130 static void addressadd_tree_button( GtkCTree *ctree, GdkEventButton *event, gpointer data ) {
131         if( ! event ) return;
132         if( event->button == 1 ) {
133                 /* Handle double click */
134                 if( event->type == GDK_2BUTTON_PRESS ) {
135                         addressadd_cancelled = FALSE;
136                         gtk_main_quit();
137                 }
138         }
139 }
140
141 static void addressadd_create( void ) {
142         GtkWidget *window;
143         GtkWidget *vbox;
144         GtkWidget *table;
145         GtkWidget *label;
146         GtkWidget *entry_name;
147         GtkWidget *label_addr;
148         GtkWidget *entry_rems;
149         GtkWidget *tree_folder;
150         GtkWidget *vlbox;
151         GtkWidget *tree_win;
152         GtkWidget *hbbox;
153         GtkWidget *hsep;
154         GtkWidget *ok_btn;
155         GtkWidget *cancel_btn;
156         GtkWidget *hsbox;
157         GtkWidget *statusbar;
158         gint top;
159
160         window = gtk_window_new(GTK_WINDOW_DIALOG);
161         gtk_widget_set_usize( window, 300, 400 );
162         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
163         gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
164         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
165         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
166         gtk_signal_connect( GTK_OBJECT(window), "delete_event",
167                             GTK_SIGNAL_FUNC(addressadd_delete_event), NULL );
168         gtk_signal_connect( GTK_OBJECT(window), "key_press_event",
169                             GTK_SIGNAL_FUNC(addressadd_key_pressed), NULL );
170
171         vbox = gtk_vbox_new(FALSE, 8);
172         gtk_container_add(GTK_CONTAINER(window), vbox);
173         gtk_container_set_border_width( GTK_CONTAINER(vbox), 8 );
174
175         table = gtk_table_new(3, 2, FALSE);
176         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
177         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
178         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
179         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
180
181         /* First row */
182         top = 0;
183         label = gtk_label_new(_("Name"));
184         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
185         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
186
187         entry_name = gtk_entry_new();
188         /* gtk_entry_set_max_length(); */
189         gtk_entry_set_text (GTK_ENTRY(entry_name),"");
190         
191         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
192
193         /* Second row */
194         top = 1;
195         label = gtk_label_new(_("Address"));
196         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
197         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
198
199         label_addr = gtk_label_new("");
200         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
201         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
202
203         /* Third row */
204         top = 2;
205         label = gtk_label_new(_("Remarks"));
206         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
207         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
208
209         entry_rems = gtk_entry_new();
210         /* gtk_entry_set_max_length(); */
211         gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
212
213         /* Address book/folder tree */
214         vlbox = gtk_vbox_new(FALSE, 8);
215         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
216         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
217
218         tree_win = gtk_scrolled_window_new( NULL, NULL );
219         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
220                                         GTK_POLICY_AUTOMATIC,
221                                         GTK_POLICY_ALWAYS );
222         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
223
224         tree_folder = gtk_ctree_new( 1, 0 );
225         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
226         gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
227         gtk_clist_set_column_title( GTK_CLIST(tree_folder), 0, _( "Select Address Book Folder" ) );
228         gtk_ctree_set_line_style( GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED );
229         gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
230         gtk_ctree_set_expander_style( GTK_CTREE(tree_folder), GTK_CTREE_EXPANDER_SQUARE );
231         gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
232         gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
233
234         /* Button panel */
235         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
236                                 &cancel_btn, _("Cancel"), NULL, NULL);
237         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
238         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
239         gtk_widget_grab_default(ok_btn);
240
241         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
242                            GTK_SIGNAL_FUNC(addressadd_ok), NULL);
243         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
244                            GTK_SIGNAL_FUNC(addressadd_cancel), NULL);
245         gtk_signal_connect(GTK_OBJECT(tree_folder), "select_row",
246                            GTK_SIGNAL_FUNC(addressadd_folder_select), NULL);
247         gtk_signal_connect(GTK_OBJECT(tree_folder), "button_press_event",
248                            GTK_SIGNAL_FUNC(addressadd_tree_button), NULL);
249
250         gtk_widget_show_all(vbox);
251
252         addressadd_dlg.window        = window;
253         addressadd_dlg.entry_name    = entry_name;
254         addressadd_dlg.label_address = label_addr;
255         addressadd_dlg.entry_remarks = entry_rems;
256         addressadd_dlg.tree_folder   = tree_folder;
257         addressadd_dlg.ok_btn        = ok_btn;
258         addressadd_dlg.cancel_btn    = cancel_btn;
259         addressadd_dlg.statusbar     = statusbar;
260         addressadd_dlg.status_cid    = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Address Add" );
261
262         gtk_widget_show_all( window );
263
264         stock_pixmap_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm, &bookXpmMask );
265         stock_pixmap_gdk( window, STOCK_PIXMAP_DIR_OPEN,
266                           &folderXpm, &folderXpmMask );
267 }
268
269 static void addressadd_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
270                                         FolderInfo *fiParent )
271 {
272         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
273         GList *list;
274         ItemFolder *folder;
275         gchar *fName;
276         gchar **name;
277         GtkCTreeNode *node;
278         FolderInfo *fi;
279
280         list = parentFolder->listFolder;
281         while( list ) {
282                 folder = list->data;
283                 fName = g_strdup( ADDRITEM_NAME(folder) );
284                 name = &fName;
285                 node = gtk_ctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
286                                 folderXpm, folderXpmMask, folderXpm, folderXpmMask,
287                                 FALSE, TRUE );
288                 g_free( fName );
289                 fi = addressadd_create_folderinfo( fiParent->book, folder );
290                 gtk_ctree_node_set_row_data_full( tree, node, fi,
291                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
292                 addressadd_load_folder( node, folder, fi );
293                 list = g_list_next( list );
294         }
295 }
296
297 static void addressadd_load_data( AddressIndex *addrIndex ) {
298         AddressDataSource *ds;
299         GList *list, *nodeDS;
300         gchar **name;
301         gchar *dsName;
302         ItemFolder *rootFolder;
303         AddressBookFile *abf;
304         FolderInfo *fi;
305         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
306         GtkCTreeNode *node;
307
308         gtk_clist_clear( GTK_CLIST( tree ) );
309         list = addrindex_get_interface_list( addrIndex );
310         while( list ) {
311                 AddressInterface *interface = list->data;
312                 if( interface->type == ADDR_IF_BOOK ) {
313                         nodeDS = interface->listSource;
314                         while( nodeDS ) {
315                                 ds = nodeDS->data;
316                                 dsName = g_strdup( addrindex_ds_get_name( ds ) );
317
318                                 /* Read address book */
319                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
320                                         addrindex_ds_read_data( ds );
321                                 }
322
323                                 /* Add node for address book */
324                                 abf = ds->rawDataSource;
325                                 name = &dsName;
326                                 node = gtk_ctree_insert_node( tree, NULL, NULL,
327                                                 name, FOLDER_SPACING, bookXpm,
328                                                 bookXpmMask, bookXpm, bookXpmMask,
329                                                 FALSE, TRUE );
330                                 g_free( dsName );
331
332                                 fi = addressadd_create_folderinfo( abf, NULL );
333                                 gtk_ctree_node_set_row_data_full( tree, node, fi,
334                                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
335
336                                 rootFolder = addrindex_ds_get_root_folder( ds );
337                                 addressadd_load_folder( node, rootFolder, fi );
338
339                                 nodeDS = g_list_next( nodeDS );
340                         }
341                 }
342                 list = g_list_next( list );
343         }
344 }
345
346 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, const gchar *address, const gchar *remarks ) {
347         gboolean retVal = FALSE;
348         ItemPerson *person = NULL;
349         FolderInfo *fi = NULL;
350         addressadd_cancelled = FALSE;
351         if( ! addressadd_dlg.window ) addressadd_create();
352         gtk_widget_grab_focus(addressadd_dlg.ok_btn);
353         gtk_widget_show(addressadd_dlg.window);
354         manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
355
356         addressadd_dlg.fiSelected = NULL;
357         addressadd_status_show( "" );
358         addressadd_load_data( addrIndex );
359         gtk_clist_select_row( GTK_CLIST( addressadd_dlg.tree_folder ), 0, 0 );
360         gtk_widget_show(addressadd_dlg.window);
361
362         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
363         gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
364         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
365         if( name )
366                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
367         if( address )
368                 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
369         if( remarks )
370                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
371
372         gtk_main();
373         gtk_widget_hide( addressadd_dlg.window );
374
375         if( ! addressadd_cancelled ) {
376                 if( addressadd_dlg.fiSelected ) {
377                         gchar *returned_name;
378                         gchar *returned_remarks;
379                         returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
380                         returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
381
382                         fi = addressadd_dlg.fiSelected;
383                         
384                         person = addrbook_add_contact( fi->book, fi->folder, 
385                                                         returned_name, 
386                                                         address, 
387                                                         returned_remarks);
388                         g_free(returned_name);
389                         g_free(returned_remarks);
390                         if( person ) retVal = TRUE;
391                 }
392         }
393
394         gtk_clist_clear( GTK_CLIST( addressadd_dlg.tree_folder ) );
395
396         return retVal;
397 }
398
399 /*
400 * End of Source.
401 */
402