Add gtkut_tree_view_get_selected_pointer() helper function.
[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_ALT_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_ALT_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 enum {
82         ADDRADD_COL_ICON,
83         ADDRADD_COL_NAME,
84         ADDRADD_COL_PTR,
85         N_ADDRADD_COLS
86 };
87
88 #ifndef USE_ALT_ADDRBOOK
89 static FolderInfo *addressadd_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
90 {
91         FolderInfo *fi = g_new0( FolderInfo, 1 );
92         fi->book   = abf;
93         fi->folder = folder;
94         return fi;
95 }
96 #else
97 static FolderInfo *addressadd_create_folderinfo(gchar* book) {
98         FolderInfo *fi = g_new0( FolderInfo, 1 );
99         fi->book = book;
100         return fi;
101 }
102 #endif
103
104 #ifndef USE_ALT_ADDRBOOK
105 static void addressadd_free_folderinfo( FolderInfo *fi ) {
106         fi->book   = NULL;
107         fi->folder = NULL;
108         g_free( fi );
109 }
110 #else
111 static void addressadd_free_folderinfo( FolderInfo *fi ) {
112         fi->book   = NULL;
113         g_free( fi );
114 }
115 #endif
116
117 static gint addressadd_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
118         addressadd_cancelled = TRUE;
119         gtk_main_quit();
120         return TRUE;
121 }
122
123 static gboolean addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
124         if (event && event->keyval == GDK_KEY_Escape) {
125                 addressadd_cancelled = TRUE;
126                 gtk_main_quit();
127         }
128         return FALSE;
129 }
130
131 /* Points addressadd_dlg.fiSelected to the selected item */
132 static void set_selected_ptr()
133 {
134         addressadd_dlg.fiSelected = gtkut_tree_view_get_selected_pointer(
135                         GTK_TREE_VIEW(addressadd_dlg.tree_folder), ADDRADD_COL_PTR);
136 }
137
138 static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
139         set_selected_ptr();
140         addressadd_cancelled = FALSE;
141         gtk_main_quit();
142 }
143
144 static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
145         set_selected_ptr();
146         addressadd_cancelled = TRUE;
147         gtk_main_quit();
148 }
149
150 static void addressadd_tree_row_activated(GtkTreeView *view,
151                 GtkTreePath *path, GtkTreeViewColumn *col,
152                 gpointer user_data)
153 {
154         addressadd_ok(NULL, NULL);
155 }
156
157 static void addressadd_size_allocate_cb(GtkWidget *widget,
158                                          GtkAllocation *allocation)
159 {
160         cm_return_if_fail(allocation != NULL);
161
162         prefs_common.addressaddwin_width = allocation->width;
163         prefs_common.addressaddwin_height = allocation->height;
164 }
165
166 static void addressadd_create( void ) {
167         GtkWidget *window;
168         GtkWidget *vbox, *hbox;
169         GtkWidget *frame;
170         GtkWidget *table;
171         GtkWidget *label;
172         GtkWidget *picture;
173         GtkWidget *entry_name;
174         GtkWidget *label_addr;
175         GtkWidget *entry_rems;
176         GtkWidget *tree_folder;
177         GtkWidget *vlbox;
178         GtkWidget *tree_win;
179         GtkWidget *hbbox;
180         GtkWidget *ok_btn;
181         GtkWidget *cancel_btn;
182         gint top;
183         static GdkGeometry geometry;
184         GtkTreeStore *store;
185         GtkTreeViewColumn *col;
186         GtkCellRenderer *rdr;
187         GtkTreeSelection *sel;
188
189         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addressadd");
190         gtk_container_set_border_width( GTK_CONTAINER(window), VBOX_BORDER );
191         gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
192         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
193         g_signal_connect( G_OBJECT(window), "delete_event",
194                           G_CALLBACK(addressadd_delete_event), NULL );
195         g_signal_connect( G_OBJECT(window), "key_press_event",
196                           G_CALLBACK(addressadd_key_pressed), NULL );
197         g_signal_connect(G_OBJECT(window), "size_allocate",
198                          G_CALLBACK(addressadd_size_allocate_cb), NULL);
199         
200         hbox = gtk_hbox_new(FALSE, 10);
201         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
202         vbox = gtk_vbox_new(FALSE, 8);
203         gtk_container_add(GTK_CONTAINER(window), vbox);
204
205         picture = gtk_image_new();
206         gtk_box_pack_start(GTK_BOX(hbox), picture, FALSE, FALSE, 0);
207
208         table = gtk_table_new(3, 2, FALSE);
209         gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
210         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
211         gtk_table_set_col_spacings(GTK_TABLE(table), HSPACING_NARROW);
212
213         frame = gtk_frame_new(_("Contact"));
214         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
215         gtk_container_add(GTK_CONTAINER(frame), hbox);
216         gtk_container_set_border_width( GTK_CONTAINER(frame), 4);
217         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
218
219         /* First row */
220         top = 0;
221         label = gtk_label_new(_("Name"));
222         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
223         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
224
225         entry_name = gtk_entry_new();
226         gtk_widget_set_size_request(entry_name, 150, -1);
227         gtk_entry_set_text (GTK_ENTRY(entry_name),"");
228         
229         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
230
231         /* Second row */
232         top = 1;
233         label = gtk_label_new(_("Address"));
234         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
235         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
236
237         label_addr = gtk_label_new("");
238         gtk_widget_set_size_request(label_addr, 150, -1);
239         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
240         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
241
242         /* Third row */
243         top = 2;
244         label = gtk_label_new(_("Remarks"));
245         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
246         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
247
248         entry_rems = gtk_entry_new();
249         gtk_widget_set_size_request(entry_rems, 150, -1);
250         gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
251         
252         /* Address book/folder tree */
253         vlbox = gtk_vbox_new(FALSE, VBOX_BORDER);
254         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
255         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 4);
256
257         tree_win = gtk_scrolled_window_new( NULL, NULL );
258         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
259                                         GTK_POLICY_AUTOMATIC,
260                                         GTK_POLICY_AUTOMATIC );
261         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
262
263         store = gtk_tree_store_new(N_ADDRADD_COLS,
264                         GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_POINTER);
265
266         tree_folder = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
267         g_object_unref(store);
268         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_folder), TRUE);
269         gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(tree_folder), FALSE);
270         gtk_tree_view_set_search_column(GTK_TREE_VIEW(tree_folder),
271                         ADDRADD_COL_NAME);
272
273         /* Column with addressbook/folder icon and name. It has two
274          * renderers, so we create it a bit differently. */
275         col = gtk_tree_view_column_new();
276         rdr = gtk_cell_renderer_pixbuf_new();
277         gtk_cell_renderer_set_padding(rdr, 0, 0);
278         gtk_tree_view_column_pack_start(col, rdr, FALSE);
279         gtk_tree_view_column_set_attributes(col, rdr,
280                         "pixbuf", ADDRADD_COL_ICON, NULL);
281         rdr = gtk_cell_renderer_text_new();
282         gtk_tree_view_column_pack_start(col, rdr, TRUE);
283         gtk_tree_view_column_set_attributes(col, rdr,
284                         "markup", ADDRADD_COL_NAME, NULL);
285         gtk_tree_view_column_set_title(col, _("Select Address Book Folder"));
286         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_folder), col);
287
288         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_folder));
289         gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
290
291         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
292
293         g_signal_connect(G_OBJECT(tree_folder), "row-activated",
294                         G_CALLBACK(addressadd_tree_row_activated), NULL);
295
296         /* Button panel */
297         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
298                                       &ok_btn, GTK_STOCK_OK,
299                                       NULL, NULL);
300         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
301         gtk_container_set_border_width( GTK_CONTAINER(hbbox), HSPACING_NARROW );
302         gtk_widget_grab_default(ok_btn);
303
304         g_signal_connect(G_OBJECT(ok_btn), "clicked",
305                          G_CALLBACK(addressadd_ok), NULL);
306         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
307                          G_CALLBACK(addressadd_cancel), NULL);
308
309         if (!geometry.min_height) {
310                 geometry.min_width = 300;
311                 geometry.min_height = 350;
312         }
313
314         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
315                                       GDK_HINT_MIN_SIZE);
316         gtk_widget_set_size_request(window, prefs_common.addressaddwin_width,
317                                     prefs_common.addressaddwin_height);
318
319         addressadd_dlg.window        = window;
320         addressadd_dlg.picture       = picture;
321         addressadd_dlg.entry_name    = entry_name;
322         addressadd_dlg.label_address = label_addr;
323         addressadd_dlg.entry_remarks = entry_rems;
324         addressadd_dlg.tree_folder   = tree_folder;
325         addressadd_dlg.ok_btn        = ok_btn;
326         addressadd_dlg.cancel_btn    = cancel_btn;
327
328         gtk_widget_show_all( window );
329
330         stock_pixbuf_gdk(STOCK_PIXMAP_BOOK, &bookXpm );
331         stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN, &folderXpm );
332 }
333
334 /* Helper function used by addressadd_tree_clear(). */
335 static gboolean close_dialog_foreach_func(GtkTreeModel *model,
336                 GtkTreePath *path,
337                 GtkTreeIter *iter,
338                 gpointer user_data)
339 {
340         FolderInfo *fi;
341
342         gtk_tree_model_get(model, iter, ADDRADD_COL_PTR, &fi, -1);
343         if (fi != NULL) {
344                 addressadd_free_folderinfo(fi);
345         }
346         gtk_tree_store_set(GTK_TREE_STORE(model), iter, ADDRADD_COL_PTR, NULL, -1);
347         return FALSE;
348 }
349
350 /* Function to remove all rows from the tree view, and free the
351  * FolderInfo pointers in them in pointer column. */
352 static void addressadd_tree_clear()
353 {
354         GtkWidget *view = addressadd_dlg.tree_folder;
355         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
356
357         gtk_tree_model_foreach(model, close_dialog_foreach_func, NULL);
358         gtk_tree_store_clear(GTK_TREE_STORE(model));
359 }
360
361 #ifndef USE_ALT_ADDRBOOK
362 static void addressadd_load_folder( GtkTreeIter *parent_iter,
363                 ItemFolder *parentFolder, FolderInfo *fiParent )
364 {
365         GtkWidget *view = addressadd_dlg.tree_folder;
366         GtkTreeIter iter;
367         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
368         GList *list;
369         ItemFolder *folder;
370         gchar *name;
371         FolderInfo *fi;
372
373         list = parentFolder->listFolder;
374         while( list ) {
375                 folder = list->data;
376                 name = g_strdup( ADDRITEM_NAME(folder) );
377                 fi = addressadd_create_folderinfo( fiParent->book, folder );
378
379                 gtk_tree_store_append(GTK_TREE_STORE(model), &iter, parent_iter);
380                 gtk_tree_store_set(GTK_TREE_STORE(model), &iter,
381                                 ADDRADD_COL_ICON, folderXpm,
382                                 ADDRADD_COL_NAME, name,
383                                 ADDRADD_COL_PTR, fi,
384                                 -1);
385                 g_free( name );
386
387                 addressadd_load_folder( parent_iter, folder, fi );
388
389                 list = g_list_next( list );
390         }
391 }
392
393 static void addressadd_load_data( AddressIndex *addrIndex ) {
394         AddressDataSource *ds;
395         GList *list, *nodeDS;
396         gchar *name;
397         ItemFolder *rootFolder;
398         AddressBookFile *abf;
399         FolderInfo *fi;
400         GtkWidget *view = addressadd_dlg.tree_folder;
401         GtkTreeIter iter;
402         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
403         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
404
405         addressadd_tree_clear();
406
407         list = addrindex_get_interface_list( addrIndex );
408         while( list ) {
409                 AddressInterface *ainterface = list->data;
410                 if( ainterface->type == ADDR_IF_BOOK || 
411                                 ainterface->type == ADDR_IF_LDAP ) {
412                         nodeDS = ainterface->listSource;
413                         while( nodeDS ) {
414                                 ds = nodeDS->data;
415                                 name = g_strdup( addrindex_ds_get_name( ds ) );
416
417                                 /* Read address book */
418                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
419                                         addrindex_ds_read_data( ds );
420                                 }
421
422                                 /* Add node for address book */
423                                 abf = ds->rawDataSource;
424                                 fi = addressadd_create_folderinfo( abf, NULL );
425
426                                 gtk_tree_store_append(GTK_TREE_STORE(model), &iter, NULL);
427                                 gtk_tree_store_set(GTK_TREE_STORE(model), &iter,
428                                                 ADDRADD_COL_ICON, bookXpm,
429                                                 ADDRADD_COL_NAME, name,
430                                                 ADDRADD_COL_PTR, fi,
431                                                 -1);
432                                 g_free( name );
433
434                                 rootFolder = addrindex_ds_get_root_folder( ds );
435                                 addressadd_load_folder( &iter, rootFolder, fi );
436
437                                 nodeDS = g_list_next( nodeDS );
438                         }
439                 }
440                 list = g_list_next( list );
441         }
442
443         if (gtk_tree_model_get_iter_first(model, &iter))
444                 gtk_tree_selection_select_iter(sel, &iter);
445 }
446 #else
447 static void addressadd_load_data() {
448         GSList *list;
449         FolderInfo *fi = NULL;
450         GError* error = NULL;
451         GtkWidget *view = addressadd_dlg.tree_folder;
452         GtkTreeIter iter;
453         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
454         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
455
456         addressadd_tree_clear();
457
458         list = addressbook_dbus_get_books(&error);
459         for (; list; list = g_slist_next(list)) {
460                 fi = addressadd_create_folderinfo(name);
461                 gtk_tree_store_append(GTK_TREE_STORE(model), &iter, NULL);
462                 gtk_tree_store_set(GTK_TREE_STORE(model), &iter,
463                                 ADDRADD_COL_ICON, bookXpm,ADDRADD_COL_NAME, (gchar *)list->data,
464                                 ADDRADD_COL_PTR, fi,
465                                 -1);
466         }
467 }
468 #endif
469
470 #ifndef USE_ALT_ADDRBOOK 
471 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, 
472                 const gchar *address, const gchar *remarks, GdkPixbuf *picture ) {
473 #else
474 gboolean addressadd_selection(const gchar *name, const gchar *address,
475                                                           const gchar *remarks, GdkPixbuf *picture ) {
476 #endif
477         gboolean retVal = FALSE;
478 #ifndef USE_ALT_ADDRBOOK 
479         ItemPerson *person = NULL;
480 #endif
481         FolderInfo *fi = NULL;
482         addressadd_cancelled = FALSE;
483
484         if( ! addressadd_dlg.window ) addressadd_create();
485
486         addressadd_dlg.fiSelected = NULL;
487 #ifndef USE_ALT_ADDRBOOK
488         addressadd_load_data( addrIndex );
489 #else
490         addressadd_load_data();
491 #endif
492         gtk_widget_show(addressadd_dlg.window);
493         gtk_window_set_modal(GTK_WINDOW(addressadd_dlg.window), TRUE);
494         gtk_widget_grab_focus(addressadd_dlg.ok_btn);
495
496         manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
497
498         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
499         gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
500         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
501         if( name )
502                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
503         if( address )
504                 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
505         if( remarks )
506                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
507         if( picture ) {
508                 gtk_image_set_from_pixbuf(GTK_IMAGE(addressadd_dlg.picture), picture);
509                 gtk_widget_show(GTK_WIDGET(addressadd_dlg.picture));
510         } else {
511                 gtk_widget_hide(GTK_WIDGET(addressadd_dlg.picture));
512         }
513         gtk_main();
514         gtk_widget_hide( addressadd_dlg.window );
515         gtk_window_set_modal(GTK_WINDOW(addressadd_dlg.window), FALSE);
516         if( ! addressadd_cancelled ) {
517                 if( addressadd_dlg.fiSelected ) {
518                         gchar *returned_name;
519                         gchar *returned_remarks;
520                         returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
521                         returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
522
523                         fi = addressadd_dlg.fiSelected;
524                         
525 #ifndef USE_ALT_ADDRBOOK
526                         person = addrbook_add_contact( fi->book, fi->folder, 
527                                                         returned_name, 
528                                                         address, 
529                                                         returned_remarks);
530                         
531                         if (person != NULL) {
532                                 person->status = ADD_ENTRY;
533
534                                 if (picture) {
535                                         GError *error = NULL;
536                                         gchar *name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
537                                                                 ADDRITEM_ID(person), ".png", NULL );
538                                         gdk_pixbuf_save(picture, name, "png", &error, NULL);
539                                         if (error) {
540                                                 g_warning("Failed to save image: %s",
541                                                                 error->message);
542                                                 g_error_free(error);
543                                         }
544                                         addritem_person_set_picture( person, ADDRITEM_ID(person) ) ;
545                                         g_free( name );
546                                 }
547                         }
548 #else
549                         ContactData* contact = g_new0(ContactData, 1);
550                         GError* error = NULL;
551                         
552                         if (returned_name)
553                                 contact->cn = g_strdup(returned_name);
554                         else
555                                 contact->cn = g_strdup(address);
556                         
557                         contact->name = g_strdup(returned_name);
558                         contact->email = g_strdup(address);
559                         contact->remarks = g_strdup(returned_remarks);
560                         contact->book = g_strdup(fi->book);
561                         contact->picture = picture;
562                         
563             if (addressbook_dbus_add_contact(contact, &error) == 0) {
564                                 debug_print("Added to addressbook:\n%s\n%s\n%s\n%s\n",
565                                                          returned_name, address, returned_remarks, fi->book);
566                                 retVal = TRUE;
567                         }
568                         else {
569                                 retVal = FALSE;
570                                 if (error) {
571                                         GtkWidget* dialog = gtk_message_dialog_new (
572                                                                 GTK_WINDOW(addressadd_dlg.window),
573                                 GTK_DIALOG_DESTROY_WITH_PARENT,
574                                 GTK_MESSAGE_ERROR,
575                                 GTK_BUTTONS_CLOSE,
576                                 "%s", error->message);
577                                         gtk_dialog_run (GTK_DIALOG (dialog));
578                                         gtk_widget_destroy (dialog);
579                                         g_error_free(error);
580                                 }
581                         }
582                         contact_data_free(&contact);
583 #endif
584 #ifdef USE_LDAP
585                         if (person != NULL && fi->book->type == ADBOOKTYPE_LDAP) {
586                                 LdapServer *server = (LdapServer *) fi->book;
587                                 ldapsvr_set_modified(server, TRUE);
588                                 ldapsvr_update_book(server, person);
589                                 if (server->retVal != LDAPRC_SUCCESS) {
590                                         alertpanel( _("Add address(es)"),
591                                                 _("Can't add the specified address"),
592                                                 GTK_STOCK_CLOSE, NULL, NULL, ALERTFOCUS_FIRST );
593                                         return server->retVal;
594                                 }
595                         }
596 #endif
597                         g_free(returned_name);
598                         g_free(returned_remarks);
599 #ifndef USE_ALT_ADDRBOOK
600                         if( person ) retVal = TRUE;
601 #endif
602                 }
603         }
604
605         addressadd_tree_clear();
606
607         return retVal;
608 }
609
610 /*
611 * End of Source.
612 */
613