inital gtk2 patch
[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 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_create( void ) {
145         GtkWidget *window;
146         GtkWidget *vbox;
147         GtkWidget *table;
148         GtkWidget *label;
149         GtkWidget *entry_name;
150         GtkWidget *label_addr;
151         GtkWidget *entry_rems;
152         GtkWidget *tree_folder;
153         GtkWidget *vlbox;
154         GtkWidget *tree_win;
155         GtkWidget *hbbox;
156         GtkWidget *ok_btn;
157         GtkWidget *cancel_btn;
158         GtkWidget *hsbox;
159         GtkWidget *statusbar;
160         gint top;
161
162         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
163         gtk_widget_set_size_request( window, 300, 400 );
164         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
165         gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
166         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
167         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
168         g_signal_connect( G_OBJECT(window), "delete_event",
169                           G_CALLBACK(addressadd_delete_event), NULL );
170         g_signal_connect( G_OBJECT(window), "key_press_event",
171                           G_CALLBACK(addressadd_key_pressed), NULL );
172
173         vbox = gtk_vbox_new(FALSE, 8);
174         gtk_container_add(GTK_CONTAINER(window), vbox);
175         gtk_container_set_border_width( GTK_CONTAINER(vbox), 8 );
176
177         table = gtk_table_new(3, 2, FALSE);
178         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
179         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
180         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
181         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
182
183         /* First row */
184         top = 0;
185         label = gtk_label_new(_("Name"));
186         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
187         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
188
189         entry_name = gtk_entry_new();
190         /* gtk_entry_set_max_length(); */
191         gtk_entry_set_text (GTK_ENTRY(entry_name),"");
192         
193         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
194
195         /* Second row */
196         top = 1;
197         label = gtk_label_new(_("Address"));
198         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
199         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
200
201         label_addr = gtk_label_new("");
202         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
203         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
204
205         /* Third row */
206         top = 2;
207         label = gtk_label_new(_("Remarks"));
208         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
209         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
210
211         entry_rems = gtk_entry_new();
212         /* gtk_entry_set_max_length(); */
213         gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
214
215         /* Address book/folder tree */
216         vlbox = gtk_vbox_new(FALSE, 8);
217         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
218         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
219
220         tree_win = gtk_scrolled_window_new( NULL, NULL );
221         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
222                                         GTK_POLICY_AUTOMATIC,
223                                         GTK_POLICY_ALWAYS );
224         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
225
226         tree_folder = gtk_ctree_new( 1, 0 );
227         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
228         gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
229         gtk_clist_set_column_title( GTK_CLIST(tree_folder), 0, _( "Select Address Book Folder" ) );
230         gtk_ctree_set_line_style( GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED );
231         gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
232         gtk_ctree_set_expander_style( GTK_CTREE(tree_folder), GTK_CTREE_EXPANDER_SQUARE );
233         gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
234         gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
235
236         /* Status line */
237         hsbox = gtk_hbox_new(FALSE, 0);
238         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
239         statusbar = gtk_statusbar_new();
240         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
241
242         /* Button panel */
243         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
244                                 &cancel_btn, _("Cancel"), NULL, NULL);
245         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
246         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
247         gtk_widget_grab_default(ok_btn);
248
249         g_signal_connect(G_OBJECT(ok_btn), "clicked",
250                          G_CALLBACK(addressadd_ok), NULL);
251         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
252                          G_CALLBACK(addressadd_cancel), NULL);
253         g_signal_connect(G_OBJECT(tree_folder), "select_row",
254                          G_CALLBACK(addressadd_folder_select), NULL);
255         g_signal_connect(G_OBJECT(tree_folder), "button_press_event",
256                          G_CALLBACK(addressadd_tree_button), NULL);
257
258         gtk_widget_show_all(vbox);
259
260         addressadd_dlg.window        = window;
261         addressadd_dlg.entry_name    = entry_name;
262         addressadd_dlg.label_address = label_addr;
263         addressadd_dlg.entry_remarks = entry_rems;
264         addressadd_dlg.tree_folder   = tree_folder;
265         addressadd_dlg.ok_btn        = ok_btn;
266         addressadd_dlg.cancel_btn    = cancel_btn;
267         addressadd_dlg.statusbar     = statusbar;
268         addressadd_dlg.status_cid    = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Address Add" );
269
270         gtk_widget_show_all( window );
271
272         stock_pixmap_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm, &bookXpmMask );
273         stock_pixmap_gdk( window, STOCK_PIXMAP_DIR_OPEN,
274                           &folderXpm, &folderXpmMask );
275 }
276
277 static void addressadd_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
278                                         FolderInfo *fiParent )
279 {
280         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
281         GList *list;
282         ItemFolder *folder;
283         gchar *fName;
284         gchar **name;
285         GtkCTreeNode *node;
286         FolderInfo *fi;
287
288         list = parentFolder->listFolder;
289         while( list ) {
290                 folder = list->data;
291                 fName = g_strdup( ADDRITEM_NAME(folder) );
292                 name = &fName;
293                 node = gtk_ctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
294                                 folderXpm, folderXpmMask, folderXpm, folderXpmMask,
295                                 FALSE, TRUE );
296                 g_free( fName );
297                 fi = addressadd_create_folderinfo( fiParent->book, folder );
298                 gtk_ctree_node_set_row_data_full( tree, node, fi,
299                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
300                 addressadd_load_folder( node, folder, fi );
301                 list = g_list_next( list );
302         }
303 }
304
305 static void addressadd_load_data( AddressIndex *addrIndex ) {
306         AddressDataSource *ds;
307         GList *list, *nodeDS;
308         gchar **name;
309         gchar *dsName;
310         ItemFolder *rootFolder;
311         AddressBookFile *abf;
312         FolderInfo *fi;
313         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
314         GtkCTreeNode *node;
315
316         gtk_clist_clear( GTK_CLIST( tree ) );
317         list = addrindex_get_interface_list( addrIndex );
318         while( list ) {
319                 AddressInterface *interface = list->data;
320                 if( interface->type == ADDR_IF_BOOK ) {
321                         nodeDS = interface->listSource;
322                         while( nodeDS ) {
323                                 ds = nodeDS->data;
324                                 dsName = g_strdup( addrindex_ds_get_name( ds ) );
325
326                                 /* Read address book */
327                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
328                                         addrindex_ds_read_data( ds );
329                                 }
330
331                                 /* Add node for address book */
332                                 abf = ds->rawDataSource;
333                                 name = &dsName;
334                                 node = gtk_ctree_insert_node( tree, NULL, NULL,
335                                                 name, FOLDER_SPACING, bookXpm,
336                                                 bookXpmMask, bookXpm, bookXpmMask,
337                                                 FALSE, TRUE );
338                                 g_free( dsName );
339
340                                 fi = addressadd_create_folderinfo( abf, NULL );
341                                 gtk_ctree_node_set_row_data_full( tree, node, fi,
342                                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
343
344                                 rootFolder = addrindex_ds_get_root_folder( ds );
345                                 addressadd_load_folder( node, rootFolder, fi );
346
347                                 nodeDS = g_list_next( nodeDS );
348                         }
349                 }
350                 list = g_list_next( list );
351         }
352 }
353
354 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, const gchar *address, const gchar *remarks ) {
355         gboolean retVal = FALSE;
356         ItemPerson *person = NULL;
357         FolderInfo *fi = NULL;
358         addressadd_cancelled = FALSE;
359         if( ! addressadd_dlg.window ) addressadd_create();
360         gtk_widget_grab_focus(addressadd_dlg.ok_btn);
361         gtk_widget_show(addressadd_dlg.window);
362         manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
363
364         addressadd_dlg.fiSelected = NULL;
365         addressadd_status_show( "" );
366         addressadd_load_data( addrIndex );
367         gtk_clist_select_row( GTK_CLIST( addressadd_dlg.tree_folder ), 0, 0 );
368         gtk_widget_show(addressadd_dlg.window);
369
370         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
371         gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
372         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
373         if( name )
374                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
375         if( address )
376                 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
377         if( remarks )
378                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
379
380         gtk_main();
381         gtk_widget_hide( addressadd_dlg.window );
382
383         if( ! addressadd_cancelled ) {
384                 if( addressadd_dlg.fiSelected ) {
385                         gchar *returned_name;
386                         gchar *returned_remarks;
387                         returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
388                         returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
389
390                         fi = addressadd_dlg.fiSelected;
391                         
392                         person = addrbook_add_contact( fi->book, fi->folder, 
393                                                         returned_name, 
394                                                         address, 
395                                                         returned_remarks);
396                         g_free(returned_name);
397                         g_free(returned_remarks);
398                         if( person ) retVal = TRUE;
399                 }
400         }
401
402         gtk_clist_clear( GTK_CLIST( addressadd_dlg.tree_folder ) );
403
404         return retVal;
405 }
406
407 /*
408 * End of Source.
409 */
410