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