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