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