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