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