2009-01-02 [colin] 3.7.0cvs8
[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 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 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtk.h>
34
35 #include "gtkutils.h"
36 #include "stock_pixmap.h"
37 #include "prefs_common.h"
38 #include "prefs_gtk.h"
39 #include "addressadd.h"
40 #include "addritem.h"
41 #include "addrbook.h"
42 #include "addrindex.h"
43 #include "manage_window.h"
44 #include "ldapserver.h"
45 #include "ldapupdate.h"
46 #include "alertpanel.h"
47
48 typedef struct {
49         AddressBookFile *book;
50         ItemFolder      *folder;
51 } FolderInfo;
52
53 static struct _AddressAdd_dlg {
54         GtkWidget *window;
55         GtkWidget *picture;
56         GtkWidget *entry_name;
57         GtkWidget *label_address;
58         GtkWidget *entry_remarks;
59         GtkWidget *tree_folder;
60         GtkWidget *ok_btn;
61         GtkWidget *cancel_btn;
62         FolderInfo *fiSelected;
63 } addressadd_dlg;
64
65 static GdkPixbuf *folderXpm;
66 static GdkPixbuf *bookXpm;
67
68 static gboolean addressadd_cancelled;
69
70 static FolderInfo *addressadd_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
71 {
72         FolderInfo *fi = g_new0( FolderInfo, 1 );
73         fi->book   = abf;
74         fi->folder = folder;
75         return fi;
76 }
77
78 static void addressadd_free_folderinfo( FolderInfo *fi ) {
79         fi->book   = NULL;
80         fi->folder = NULL;
81         g_free( fi );
82 }
83
84 static gint addressadd_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
85         addressadd_cancelled = TRUE;
86         gtk_main_quit();
87         return TRUE;
88 }
89
90 static gboolean addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
91         if (event && event->keyval == GDK_Escape) {
92                 addressadd_cancelled = TRUE;
93                 gtk_main_quit();
94         }
95         return FALSE;
96 }
97
98 static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
99         addressadd_cancelled = FALSE;
100         gtk_main_quit();
101 }
102
103 static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
104         addressadd_cancelled = TRUE;
105         gtk_main_quit();
106 }
107
108 static void addressadd_folder_select( GtkCMCTree *ctree, GtkCMCTreeNode *node,
109                                       gint column, gpointer data )
110 {
111         addressadd_dlg.fiSelected = gtk_cmctree_node_get_row_data( ctree, node );
112 }
113
114 static gboolean addressadd_tree_button( GtkCMCTree *ctree, GdkEventButton *event, gpointer data ) {
115         if( ! event )
116                 return FALSE;
117         if( event->button == 1 ) {
118                 /* Handle double click */
119                 if( event->type == GDK_2BUTTON_PRESS ) {
120                         addressadd_cancelled = FALSE;
121                         gtk_main_quit();
122                 }
123         }
124
125         return FALSE;
126 }
127
128 static void addressadd_size_allocate_cb(GtkWidget *widget,
129                                          GtkAllocation *allocation)
130 {
131         g_return_if_fail(allocation != NULL);
132
133         prefs_common.addressaddwin_width = allocation->width;
134         prefs_common.addressaddwin_height = allocation->height;
135 }
136
137 static void addressadd_create( void ) {
138         GtkWidget *window;
139         GtkWidget *vbox, *hbox;
140         GtkWidget *frame;
141         GtkWidget *table;
142         GtkWidget *label;
143         GtkWidget *picture;
144         GtkWidget *entry_name;
145         GtkWidget *label_addr;
146         GtkWidget *entry_rems;
147         GtkWidget *tree_folder;
148         GtkWidget *vlbox;
149         GtkWidget *tree_win;
150         GtkWidget *hbbox;
151         GtkWidget *ok_btn;
152         GtkWidget *cancel_btn;
153         gint top;
154         static GdkGeometry geometry;
155         gchar *titles[1];
156
157         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addressadd");
158         gtk_container_set_border_width( GTK_CONTAINER(window), VBOX_BORDER );
159         gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
160         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
161         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
162         g_signal_connect( G_OBJECT(window), "delete_event",
163                           G_CALLBACK(addressadd_delete_event), NULL );
164         g_signal_connect( G_OBJECT(window), "key_press_event",
165                           G_CALLBACK(addressadd_key_pressed), NULL );
166         g_signal_connect(G_OBJECT(window), "size_allocate",
167                          G_CALLBACK(addressadd_size_allocate_cb), NULL);
168         
169         hbox = gtk_hbox_new(FALSE, 10);
170         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
171         vbox = gtk_vbox_new(FALSE, 8);
172         gtk_container_add(GTK_CONTAINER(window), vbox);
173
174         picture = gtk_image_new();
175         gtk_box_pack_start(GTK_BOX(hbox), picture, FALSE, FALSE, 0);
176
177         table = gtk_table_new(3, 2, FALSE);
178         gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
179         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
180         gtk_table_set_col_spacings(GTK_TABLE(table), HSPACING_NARROW);
181
182         frame = gtk_frame_new(_("Contact"));
183         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
184         gtk_container_add(GTK_CONTAINER(frame), hbox);
185         gtk_container_set_border_width( GTK_CONTAINER(frame), 4);
186         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
187
188         /* First row */
189         top = 0;
190         label = gtk_label_new(_("Name"));
191         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
192         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
193
194         entry_name = gtk_entry_new();
195         gtk_widget_set_size_request(entry_name, 150, -1);
196         gtk_entry_set_text (GTK_ENTRY(entry_name),"");
197         
198         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
199
200         /* Second row */
201         top = 1;
202         label = gtk_label_new(_("Address"));
203         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
204         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
205
206         label_addr = gtk_label_new("");
207         gtk_widget_set_size_request(label_addr, 150, -1);
208         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
209         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
210
211         /* Third row */
212         top = 2;
213         label = gtk_label_new(_("Remarks"));
214         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
215         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
216
217         entry_rems = gtk_entry_new();
218         gtk_widget_set_size_request(entry_rems, 150, -1);
219         gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
220         
221         /* Address book/folder tree */
222         vlbox = gtk_vbox_new(FALSE, VBOX_BORDER);
223         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
224         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 4);
225
226         tree_win = gtk_scrolled_window_new( NULL, NULL );
227         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
228                                         GTK_POLICY_AUTOMATIC,
229                                         GTK_POLICY_AUTOMATIC );
230         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
231
232         titles[0] = _( "Select Address Book Folder") ;
233
234         tree_folder = gtk_sctree_new_with_titles( 1, 0, titles );
235         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
236         gtk_cmclist_column_titles_show( GTK_CMCLIST(tree_folder) );
237         if (prefs_common.enable_dotted_lines) {
238                 gtk_cmctree_set_line_style(GTK_CMCTREE(tree_folder), GTK_CMCTREE_LINES_DOTTED);
239                 gtk_cmctree_set_expander_style(GTK_CMCTREE(tree_folder),
240                                      GTK_CMCTREE_EXPANDER_SQUARE);
241         } else {
242                 gtk_cmctree_set_line_style(GTK_CMCTREE(tree_folder), GTK_CMCTREE_LINES_NONE);
243                 gtk_cmctree_set_expander_style(GTK_CMCTREE(tree_folder),
244                                      GTK_CMCTREE_EXPANDER_TRIANGLE);
245         }
246         gtk_sctree_set_stripes(GTK_SCTREE(tree_folder), prefs_common.use_stripes_everywhere);
247         gtk_cmclist_set_selection_mode( GTK_CMCLIST(tree_folder), GTK_SELECTION_BROWSE );
248         gtk_cmctree_set_indent( GTK_CMCTREE(tree_folder), CTREE_INDENT );
249         gtk_cmclist_set_auto_sort( GTK_CMCLIST(tree_folder), TRUE );
250
251         /* Button panel */
252         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
253                                       &ok_btn, GTK_STOCK_OK,
254                                       NULL, NULL);
255         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
256         gtk_container_set_border_width( GTK_CONTAINER(hbbox), HSPACING_NARROW );
257         gtk_widget_grab_default(ok_btn);
258
259         g_signal_connect(G_OBJECT(ok_btn), "clicked",
260                          G_CALLBACK(addressadd_ok), NULL);
261         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
262                          G_CALLBACK(addressadd_cancel), NULL);
263         g_signal_connect(G_OBJECT(tree_folder), "tree_select_row",
264                          G_CALLBACK(addressadd_folder_select), NULL);
265         g_signal_connect(G_OBJECT(tree_folder), "button_press_event",
266                          G_CALLBACK(addressadd_tree_button), NULL);
267
268         if (!geometry.min_height) {
269                 geometry.min_width = 300;
270                 geometry.min_height = 350;
271         }
272
273         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
274                                       GDK_HINT_MIN_SIZE);
275         gtk_widget_set_size_request(window, prefs_common.addressaddwin_width,
276                                     prefs_common.addressaddwin_height);
277
278         addressadd_dlg.window        = window;
279         addressadd_dlg.picture       = picture;
280         addressadd_dlg.entry_name    = entry_name;
281         addressadd_dlg.label_address = label_addr;
282         addressadd_dlg.entry_remarks = entry_rems;
283         addressadd_dlg.tree_folder   = tree_folder;
284         addressadd_dlg.ok_btn        = ok_btn;
285         addressadd_dlg.cancel_btn    = cancel_btn;
286
287         gtk_widget_show_all( window );
288
289         stock_pixbuf_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm );
290         stock_pixbuf_gdk( window, STOCK_PIXMAP_DIR_OPEN,
291                           &folderXpm );
292 }
293
294 static void addressadd_load_folder( GtkCMCTreeNode *parentNode, ItemFolder *parentFolder,
295                                         FolderInfo *fiParent )
296 {
297         GtkCMCTree *tree = GTK_CMCTREE( addressadd_dlg.tree_folder );
298         GList *list;
299         ItemFolder *folder;
300         gchar *fName;
301         gchar **name;
302         GtkCMCTreeNode *node;
303         FolderInfo *fi;
304
305         list = parentFolder->listFolder;
306         while( list ) {
307                 folder = list->data;
308                 fName = g_strdup( ADDRITEM_NAME(folder) );
309                 name = &fName;
310                 node = gtk_sctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
311                                 folderXpm, folderXpm,
312                                 FALSE, TRUE );
313                 g_free( fName );
314                 fi = addressadd_create_folderinfo( fiParent->book, folder );
315                 gtk_cmctree_node_set_row_data_full( tree, node, fi,
316                                 ( GDestroyNotify ) addressadd_free_folderinfo );
317                 addressadd_load_folder( node, folder, fi );
318                 list = g_list_next( list );
319         }
320 }
321
322 static void addressadd_load_data( AddressIndex *addrIndex ) {
323         AddressDataSource *ds;
324         GList *list, *nodeDS;
325         gchar **name;
326         gchar *dsName;
327         ItemFolder *rootFolder;
328         AddressBookFile *abf;
329         FolderInfo *fi;
330         GtkCMCTree *tree = GTK_CMCTREE( addressadd_dlg.tree_folder );
331         GtkCMCTreeNode *node;
332
333         gtk_cmclist_clear( GTK_CMCLIST( tree ) );
334         list = addrindex_get_interface_list( addrIndex );
335         while( list ) {
336                 AddressInterface *interface = list->data;
337                 if( interface->type == ADDR_IF_BOOK || 
338                                 interface->type == ADDR_IF_LDAP ) {
339                         nodeDS = interface->listSource;
340                         while( nodeDS ) {
341                                 ds = nodeDS->data;
342                                 dsName = g_strdup( addrindex_ds_get_name( ds ) );
343
344                                 /* Read address book */
345                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
346                                         addrindex_ds_read_data( ds );
347                                 }
348
349                                 /* Add node for address book */
350                                 abf = ds->rawDataSource;
351                                 name = &dsName;
352                                 node = gtk_cmctree_insert_node( tree, NULL, NULL,
353                                                 name, FOLDER_SPACING, bookXpm,
354                                                 bookXpm,
355                                                 FALSE, TRUE );
356                                 g_free( dsName );
357
358                                 fi = addressadd_create_folderinfo( abf, NULL );
359                                 gtk_cmctree_node_set_row_data_full( tree, node, fi,
360                                                 ( GDestroyNotify ) addressadd_free_folderinfo );
361
362                                 rootFolder = addrindex_ds_get_root_folder( ds );
363                                 addressadd_load_folder( node, rootFolder, fi );
364
365                                 nodeDS = g_list_next( nodeDS );
366                         }
367                 }
368                 list = g_list_next( list );
369         }
370 }
371
372 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, 
373                 const gchar *address, const gchar *remarks, GdkPixbuf *picture ) {
374         gboolean retVal = FALSE;
375         ItemPerson *person = NULL;
376         FolderInfo *fi = NULL;
377         addressadd_cancelled = FALSE;
378
379         if( ! addressadd_dlg.window ) addressadd_create();
380         gtk_widget_grab_focus(addressadd_dlg.ok_btn);
381         gtk_widget_show(addressadd_dlg.window);
382         manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
383
384         addressadd_dlg.fiSelected = NULL;
385         addressadd_load_data( addrIndex );
386         gtk_cmclist_select_row( GTK_CMCLIST( addressadd_dlg.tree_folder ), 0, 0 );
387         gtk_widget_show(addressadd_dlg.window);
388
389         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
390         gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
391         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
392         if( name )
393                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
394         if( address )
395                 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
396         if( remarks )
397                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
398         if( picture ) {
399                 gtk_image_set_from_pixbuf(GTK_IMAGE(addressadd_dlg.picture), picture);
400                 gtk_widget_show(GTK_WIDGET(addressadd_dlg.picture));
401         } else {
402                 gtk_widget_hide(GTK_WIDGET(addressadd_dlg.picture));
403         }
404         gtk_main();
405         gtk_widget_hide( addressadd_dlg.window );
406
407         if( ! addressadd_cancelled ) {
408                 if( addressadd_dlg.fiSelected ) {
409                         gchar *returned_name;
410                         gchar *returned_remarks;
411                         returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
412                         returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
413
414                         fi = addressadd_dlg.fiSelected;
415                         
416                         person = addrbook_add_contact( fi->book, fi->folder, 
417                                                         returned_name, 
418                                                         address, 
419                                                         returned_remarks);
420                         person->status = ADD_ENTRY;
421
422                         if (picture) {
423                                 GError *error = NULL;
424                                 gchar *name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
425                                                         ADDRITEM_ID(person), ".png", NULL );
426                                 gdk_pixbuf_save(picture, name, "png", &error, NULL);
427                                 if (error) {
428                                         g_warning(_("Failed to save image: \n%s"),
429                                                         error->message);
430                                         g_error_free(error);
431                                 }
432                                 addritem_person_set_picture( person, ADDRITEM_ID(person) ) ;
433                                 g_free( name );
434                         }
435 #ifdef USE_LDAP
436                         if (fi->book->type == ADBOOKTYPE_LDAP) {
437                                 LdapServer *server = (LdapServer *) fi->book;
438                                 ldapsvr_set_modified(server, TRUE);
439                                 ldapsvr_update_book(server, person);
440                                 if (server->retVal != LDAPRC_SUCCESS) {
441                                         alertpanel( _("Add address(es)"),
442                                                 _("Can't add the specified address"),
443                                                 GTK_STOCK_CLOSE, NULL, NULL );
444                                         return server->retVal;
445                                 }
446                         }
447 #endif
448                         g_free(returned_name);
449                         g_free(returned_remarks);
450                         if( person ) retVal = TRUE;
451                 }
452         }
453
454         gtk_cmclist_clear( GTK_CMCLIST( addressadd_dlg.tree_folder ) );
455
456         return retVal;
457 }
458
459 /*
460 * End of Source.
461 */
462