2007-10-10 [paul] 3.0.2cvs51
[claws.git] / src / addrduplicates.c
1 /* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
2  * Copyright (C) 2007 Holger Berndt <hb@claws-mail.org> 
3  * 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 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #endif
22
23 #include <gdk/gdk.h>
24 #include <gdk/gdkkeysyms.h>
25 #include <glib/gi18n.h>
26 #include <string.h>
27
28 #include "defs.h"
29
30 #ifdef USE_LDAP
31 #include "ldapserver.h"
32 #include "ldapupdate.h"
33 #endif
34 #include "addrduplicates.h"
35 #include "addrindex.h"
36 #include "addrbook.h"
37 #include "addressbook.h"
38 #include "editaddress.h"
39 #include "alertpanel.h"
40 #include "gtkutils.h"
41 #include "inc.h"
42 #include "utils.h"
43 #include "prefs_common.h"
44
45 typedef struct
46 {
47         ItemPerson        *person;
48         AddressDataSource *ds;
49         gchar             *book_path;
50 }
51 AddrDupListEntry;
52
53 enum {
54     COL_BOOKPATH = 0,
55     COL_NAME,
56     COL_ITEM,
57     COL_DS,
58     NUM_COLS
59 };
60
61 static gboolean create_dialog();
62 static void refresh_addr_hash(void);
63 static void refresh_stores(gchar*,GSList*);
64 static void present_finder_results(GtkWindow*);
65 static void cb_finder_results_dialog_destroy(GtkWindow*, gpointer);
66 static gboolean cb_finder_results_dialog_key_pressed(GtkWidget*, GdkEventKey*,
67         gpointer);
68 static void destroy_addr_hash_val(gpointer);
69 static GSList* deep_copy_hash_val(GSList*);
70 static void fill_hash_table();
71 static gint collect_emails(ItemPerson*, AddressDataSource*);
72 static gboolean is_not_duplicate(gpointer, gpointer, gpointer);
73 static gint books_compare(gconstpointer, gconstpointer);
74 static GtkWidget* create_email_view(GtkListStore*);
75 static GtkWidget* create_detail_view(GtkListStore*);
76 static void append_to_email_store(gpointer,gpointer,gpointer);
77 static void email_selection_changed(GtkTreeSelection*,gpointer);
78 static void detail_selection_changed(GtkTreeSelection*,gpointer);
79 static void detail_row_activated(GtkTreeView*,GtkTreePath*,
80                                  GtkTreeViewColumn*,
81                                  gpointer);
82 static gboolean detail_focus_in(GtkWidget*,GdkEventFocus*,gpointer);
83 static gboolean detail_focus_out(GtkWidget*,GdkEventFocus*,gpointer);
84
85 static void cb_del_btn_clicked(GtkButton *, gpointer);
86 static void cb_edit_btn_clicked(GtkButton *, gpointer);
87 static gboolean delete_item(ItemPerson*, AddressDataSource*);
88 static gchar* get_bookpath(ItemPerson*,AddressDataSource*);
89 static gboolean is_editing_entry_only_selection(void);
90 static void edit_post_update_cb(ItemPerson*);
91
92 static GHashTable *addr_hash;
93 static gboolean include_same_book = TRUE;
94 static gboolean include_other_books = TRUE;
95
96 static GtkListStore *email_store;
97 static GtkListStore *detail_store;
98 static GtkWidget    *email_view;
99 static GtkWidget    *detail_view;
100 static GtkWidget    *inline_edit_vbox;
101
102 static GtkWidget *del_btn;
103 static GtkWidget *edit_btn;
104
105 static GtkWidget *dialog;
106 static gchar *editing_uid;
107 static gboolean detail_view_has_focus;
108
109 void addrduplicates_find(GtkWindow *parent)
110 {
111         if(create_dialog()) {
112                 refresh_addr_hash();
113                 present_finder_results(parent);
114         }
115 }
116
117 static gboolean create_dialog()
118 {
119         gboolean want_search;
120         GtkWidget *vbox;
121         GtkWidget *check_same_book;
122         GtkWidget *check_other_book;
123         AlertValue val;
124
125         want_search = FALSE;
126
127         vbox = gtk_vbox_new(FALSE, 0);
128         check_same_book = gtk_check_button_new_with_label(_("Show duplicates in "
129                           "the same book"));
130         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_same_book),
131                                      include_same_book);
132         gtk_box_pack_start(GTK_BOX(vbox), check_same_book, FALSE, FALSE, 0);
133         gtk_widget_show(check_same_book);
134         check_other_book = gtk_check_button_new_with_label(_("Show duplicates in "
135                            "different books"));
136         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_other_book),
137                                      include_other_books);
138         gtk_box_pack_start(GTK_BOX(vbox), check_other_book, FALSE, FALSE, 0);
139         gtk_widget_show(check_other_book);
140
141         /* prevent checkboxes from being destroyed on dialog close */
142         g_object_ref(check_same_book);
143         g_object_ref(check_other_book);
144
145         val = alertpanel_full(_("Find address book email duplicates"),
146                               _("Claws-Mail will now search for duplicate email "
147                                 "addresses in the address book."),
148                               GTK_STOCK_CANCEL,GTK_STOCK_FIND,NULL, FALSE, vbox, ALERT_NOTICE,
149                               G_ALERTALTERNATE);
150         if(val == G_ALERTALTERNATE) {
151                 want_search = TRUE;
152
153                 /* save options */
154                 include_same_book =
155                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_same_book));
156                 include_other_books =
157                     gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_other_book));
158
159         }
160
161         g_object_unref(check_same_book);
162         g_object_unref(check_other_book);
163         return want_search;
164 }
165
166 static void refresh_addr_hash(void)
167 {
168         if(addr_hash)
169                 g_hash_table_destroy(addr_hash);
170         addr_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
171                                           g_free, destroy_addr_hash_val);
172         fill_hash_table();
173 }
174
175 static void destroy_addr_hash_val(gpointer value)
176 {
177         GSList *list = (GSList*) value;
178         GSList *walk;
179
180         for(walk = list; walk; walk = walk->next) {
181                 AddrDupListEntry *entry = (AddrDupListEntry*) walk->data;
182                 if(entry->book_path)
183                         g_free(entry->book_path);
184                 if(entry)
185                         g_free(entry);
186         }
187         if(list)
188                 g_slist_free(list);
189 }
190
191 static GSList* deep_copy_hash_val(GSList *in)
192 {
193         GSList *walk;
194         GSList *out = NULL;
195
196         out = g_slist_copy(in);
197         for(walk = out; walk; walk = walk->next) {
198                 AddrDupListEntry *out_entry;
199                 AddrDupListEntry *in_entry = walk->data;
200
201                 out_entry = g_new0(AddrDupListEntry,1);
202                 out_entry->person = in_entry->person;
203                 out_entry->ds     = in_entry->ds;
204                 out_entry->book_path = g_strdup(in_entry->book_path);
205                 walk->data = out_entry;
206         }
207
208         return out;
209 }
210
211 static void fill_hash_table()
212 {
213         addrindex_load_person_ds(collect_emails);
214         g_hash_table_foreach_remove(addr_hash,is_not_duplicate, NULL);
215 }
216
217 static gboolean is_not_duplicate(gpointer key, gpointer value,
218                                  gpointer user_data)
219 {
220         gboolean is_in_same_book;
221         gboolean is_in_other_books;
222         GSList *books;
223         GSList *walk;
224         gboolean retval;
225         GSList *list = value;
226
227         /* remove everything that is just in one book */
228         if(g_slist_length(list) <= 1)
229                 return TRUE;
230
231         /* work on a shallow copy */
232         books = g_slist_copy(list);
233
234         /* sorting the list makes it easier to check for books */
235         books = g_slist_sort(books, books_compare);
236
237         /* check if a book appears twice */
238         is_in_same_book = FALSE;
239         for(walk = books; walk && walk->next; walk = walk->next) {
240                 if(books_compare(walk->data, walk->next->data) == 0) {
241                         is_in_same_book = TRUE;
242                         break;
243                 }
244         }
245
246         /* check is at least two different books appear in the list */
247         is_in_other_books = FALSE;
248         if(books && books->next) {
249                 for(walk = books->next; walk; walk = walk->next) {
250                         if(books_compare(walk->data, books->data) != 0) {
251                                 is_in_other_books = TRUE;
252                                 break;
253                         }
254                 }
255         }
256
257         /* delete the shallow copy */
258         g_slist_free(books);
259
260         retval = FALSE;
261         if(is_in_same_book && include_same_book)
262                 retval = TRUE;
263         if(is_in_other_books && include_other_books)
264                 retval = TRUE;
265         retval = !retval;
266
267         return retval;
268 }
269
270 static gint collect_emails(ItemPerson *itemperson, AddressDataSource *ds)
271 {
272         gchar *addr;
273         GList *nodeM;
274         GSList *old_val;
275         GSList *new_val;
276         AddrDupListEntry *entry;
277
278         /* Process each E-Mail address */
279         nodeM = itemperson->listEMail;
280         while(nodeM) {
281                 ItemEMail *email = nodeM->data;
282
283                 addr = g_strdup(email->address);
284                 g_strdown(addr);
285                 old_val = g_hash_table_lookup(addr_hash, addr);
286                 if(old_val)
287                         new_val = deep_copy_hash_val(old_val);
288                 else
289                         new_val = NULL;
290
291                 entry = g_new0(AddrDupListEntry,1);
292                 entry->person = itemperson;
293                 entry->ds     = ds;
294                 entry->book_path = get_bookpath(itemperson, ds);
295
296                 new_val = g_slist_prepend(new_val, entry);
297                 g_hash_table_insert(addr_hash, addr, new_val);
298
299                 nodeM = g_list_next(nodeM);
300         }
301         return 0;
302 }
303
304 static gint books_compare(gconstpointer a, gconstpointer b)
305 {
306         const AddrDupListEntry *entry1;
307         const AddrDupListEntry *entry2;
308         entry1 = a;
309         entry2 = b;
310         return strcmp(entry1->book_path, entry2->book_path);
311 }
312
313 static void present_finder_results(GtkWindow *parent)
314 {
315         GtkWidget *scrolled_win;
316         GtkWidget *vbox;
317         GtkWidget *hbox;
318         GtkWidget *hpaned;
319         GtkWidget *vpaned;
320         GtkWidget *close;
321         gint pos;
322         GtkTreeSelection *email_select;
323         GtkTreeSelection *detail_select;
324         static GdkGeometry geometry;
325
326         if(g_hash_table_size(addr_hash) == 0) {
327                 alertpanel_notice(_("No duplicate email addresses found in the address book"));
328                 return;
329         }
330
331         email_store = gtk_list_store_new(1, G_TYPE_STRING);
332         refresh_stores(NULL,NULL);
333         email_view = create_email_view(email_store);
334         email_select = gtk_tree_view_get_selection(GTK_TREE_VIEW(email_view));
335         gtk_tree_selection_set_mode(email_select,GTK_SELECTION_SINGLE);
336
337         g_signal_connect(email_select, "changed",
338                          (GCallback)email_selection_changed, NULL);
339
340         detail_store = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_STRING,
341                                           G_TYPE_POINTER, G_TYPE_POINTER);
342         detail_view = create_detail_view(detail_store);
343         detail_select = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
344         gtk_tree_selection_set_mode(detail_select,GTK_SELECTION_MULTIPLE);
345
346         g_signal_connect(detail_select, "changed",
347                          (GCallback)detail_selection_changed, NULL);
348
349         dialog = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "address_dupes_finder");
350         gtk_window_set_transient_for(GTK_WINDOW(dialog),parent);
351         gtk_window_set_modal(GTK_WINDOW(dialog),TRUE);
352         if(!geometry.min_height) {
353                 geometry.min_width = 600;
354                 geometry.min_height = 400;
355         }
356         gtk_window_set_geometry_hints(GTK_WINDOW(dialog), NULL, &geometry,
357                                       GDK_HINT_MIN_SIZE);
358         gtk_window_set_title(GTK_WINDOW(dialog), _("Duplicate email addresses"));
359
360         vbox = gtk_vbox_new(FALSE, 0);
361         gtk_container_add(GTK_CONTAINER(dialog), vbox);
362
363         hpaned = gtk_hpaned_new();
364         gtk_box_pack_start(GTK_BOX(vbox), hpaned, TRUE, TRUE, 0);
365
366         scrolled_win = gtk_scrolled_window_new(NULL,NULL);
367         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_win),
368                                        GTK_POLICY_AUTOMATIC,
369                                        GTK_POLICY_AUTOMATIC);
370         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_win),
371                                               email_view);
372
373         gtk_paned_add1(GTK_PANED(hpaned), scrolled_win);
374
375         scrolled_win = gtk_scrolled_window_new(NULL,NULL);
376         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_win),
377                                        GTK_POLICY_AUTOMATIC,
378                                        GTK_POLICY_AUTOMATIC);
379         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled_win),
380                                               detail_view);
381
382         if (prefs_common.addressbook_use_editaddress_dialog) {
383                 gtk_paned_add2(GTK_PANED(hpaned), scrolled_win);
384                 inline_edit_vbox = NULL;
385         } else {
386                 inline_edit_vbox = gtk_vbox_new(FALSE, 4);
387                 vpaned = gtk_vpaned_new();
388                 gtk_paned_pack1(GTK_PANED(vpaned), scrolled_win, FALSE, FALSE);
389                 gtk_paned_pack2(GTK_PANED(vpaned), inline_edit_vbox, TRUE, FALSE);
390                 gtk_paned_pack2(GTK_PANED(hpaned), vpaned, TRUE, FALSE);
391         }
392
393         g_object_get(G_OBJECT(hpaned),
394                      "position", &pos, NULL);
395         if(pos < 200)
396                 gtk_paned_set_position(GTK_PANED(hpaned), 200);
397
398         hbox = gtk_hbutton_box_new();
399         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
400         gtk_box_set_spacing(GTK_BOX(hbox), 2);
401         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
402         gtk_box_pack_end(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
403
404         edit_btn = gtk_button_new_from_stock(GTK_STOCK_EDIT);
405         gtk_box_pack_start(GTK_BOX(hbox), edit_btn, TRUE, TRUE, 0);
406         gtk_widget_set_sensitive(edit_btn, FALSE);
407
408         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
409         gtk_box_pack_start(GTK_BOX(hbox), del_btn, TRUE, TRUE, 0);
410         gtk_widget_set_sensitive(del_btn, FALSE);
411
412         close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
413         gtk_box_pack_start(GTK_BOX(hbox), close, TRUE, TRUE, 0);
414
415         g_signal_connect(dialog, "destroy",
416                          G_CALLBACK(cb_finder_results_dialog_destroy), NULL);
417         g_signal_connect(G_OBJECT(dialog), "key-press-event",
418                          G_CALLBACK(cb_finder_results_dialog_key_pressed), NULL);
419         g_signal_connect_swapped(close, "clicked",
420                                  G_CALLBACK(gtk_widget_destroy), dialog);
421         g_signal_connect(del_btn, "clicked",
422                          G_CALLBACK(cb_del_btn_clicked), detail_view);
423         g_signal_connect(edit_btn, "clicked",
424                          G_CALLBACK(cb_edit_btn_clicked), detail_view);
425
426         inc_lock();
427         gtk_widget_show_all(dialog);
428 }
429
430 static void cb_finder_results_dialog_destroy(GtkWindow *win, gpointer data)
431 {
432         email_store = NULL;
433         detail_store = NULL;
434         email_view = NULL;
435         inline_edit_vbox = NULL;
436
437         if(addr_hash) {
438                 g_hash_table_destroy(addr_hash);
439                 addr_hash = NULL;
440         }
441         dialog = NULL;
442         addressbook_refresh();
443         inc_unlock();
444 }
445
446 static GtkWidget* create_email_view(GtkListStore *store)
447 {
448         GtkWidget *view;
449         GtkCellRenderer *renderer;
450
451         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
452         renderer = gtk_cell_renderer_text_new();
453         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
454                 -1,
455                 _("Address"),
456                 renderer,
457                 "text", 0,
458                 NULL);
459         g_object_unref(store);
460         return view;
461 }
462
463 static GtkWidget* create_detail_view(GtkListStore *store)
464 {
465         GtkWidget *view;
466         GtkCellRenderer *renderer;
467         GList *cols;
468         GList *walk;
469
470         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
471         renderer = gtk_cell_renderer_text_new();
472
473         /* col 1 */
474         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
475                 -1,
476                 _("Address book path"),
477                 renderer,
478                 "text", COL_BOOKPATH,
479                 NULL);
480         /* col 2 */
481         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
482                 -1,
483                 _("Name"),
484                 renderer,
485                 "text", COL_NAME,
486                 NULL);
487
488         cols = gtk_tree_view_get_columns(GTK_TREE_VIEW(view));
489         for(walk = cols; walk; walk = walk->next)
490                 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(walk->data),
491                                                    TRUE);
492         g_list_free(cols);
493
494         g_signal_connect(view, "row-activated",
495                          G_CALLBACK(detail_row_activated), NULL);
496
497         g_signal_connect(view, "focus-in-event",
498                          G_CALLBACK(detail_focus_in), NULL);
499         g_signal_connect(view, "focus-out-event",
500                          G_CALLBACK(detail_focus_out), NULL);
501
502
503         return view;
504 }
505
506 static void append_to_email_store(gpointer key,gpointer value,gpointer data)
507 {
508         GtkTreeIter iter;
509         GtkListStore *store = (GtkListStore*) data;
510
511         gtk_list_store_append(store, &iter);
512         gtk_list_store_set(store, &iter, 0, (gchar*) key, -1);
513 }
514
515 static gboolean is_editing_entry_only_selection(void)
516 {
517         GtkTreeSelection *sel_detail;
518         GtkTreeIter iter;
519         GList *selected;
520         GtkTreeModel *model;
521         ItemPerson *item;
522
523         sel_detail = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
524
525         if(gtk_tree_selection_count_selected_rows(sel_detail) > 1)
526                 return FALSE;
527
528         selected = gtk_tree_selection_get_selected_rows(sel_detail,&model);
529         if(!selected)
530                 return FALSE;
531
532         gtk_tree_model_get_iter(model, &iter, (GtkTreePath*)selected->data);
533         g_list_foreach(selected, (GFunc)gtk_tree_path_free, NULL);
534         g_list_free(selected);
535
536         gtk_tree_model_get(model, &iter, COL_ITEM, &item,-1);
537         if(ADDRITEM_ID(item) && editing_uid &&
538                 strcmp(ADDRITEM_ID(item),editing_uid) == 0)
539                 return TRUE;
540         else
541                 return FALSE;
542 }
543
544 static void detail_selection_changed(GtkTreeSelection *selection, gpointer data)
545 {
546         gint num_selected;
547         num_selected = gtk_tree_selection_count_selected_rows(selection);
548
549         if(num_selected > 0)
550                 gtk_widget_set_sensitive(del_btn,TRUE);
551         else
552                 gtk_widget_set_sensitive(del_btn,FALSE);
553
554         if(num_selected == 1)
555                 gtk_widget_set_sensitive(edit_btn,TRUE);
556         else
557                 gtk_widget_set_sensitive(edit_btn,FALSE);
558
559         if(!is_editing_entry_only_selection())
560                 addressbook_edit_person_widgetset_hide();
561 }
562
563 static void email_selection_changed(GtkTreeSelection *selection, gpointer data)
564 {
565         GtkTreeIter iter;
566         GtkTreeModel *model;
567         gchar *email;
568
569         if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
570                 GSList *hashval;
571                 GSList *walk;
572
573                 gtk_tree_model_get(model, &iter, 0, &email, -1);
574
575                 hashval = g_hash_table_lookup(addr_hash, email);
576                 gtk_list_store_clear(detail_store);
577                 for(walk = hashval; walk; walk = walk->next) {
578                         AddrDupListEntry *entry = walk->data;
579                         if(!entry)
580                                 continue;
581                         gtk_list_store_append(detail_store, &iter);
582                         gtk_list_store_set(detail_store, &iter,
583                                            COL_BOOKPATH, entry->book_path,
584                                            COL_NAME, ADDRITEM_NAME(entry->person),
585                                            COL_ITEM, entry->person,
586                                            COL_DS, entry->ds,
587                                            -1);
588                 }
589                 g_free(email);
590         }
591 }
592
593 static gchar* get_bookpath(ItemPerson *itemPerson, AddressDataSource *ds)
594 {
595         gchar *path;
596         gchar *tmp;
597         AddrItemObject *item;
598
599         item = (AddrItemObject*)itemPerson;
600         path = g_strdup("");
601         while((item = ADDRITEM_PARENT(item)) != NULL) {
602
603                 if(ADDRITEM_TYPE(item) == ITEMTYPE_FOLDER) {
604                         ItemFolder *folder = (ItemFolder*) item;
605                         tmp = path;
606                         path = g_strdup_printf("%s%s%s",
607                                                folder->isRoot ? addrindex_ds_get_name(ds) :
608                                                ADDRITEM_NAME(folder),
609                                                (*tmp == '\0') ? "" : "/", tmp);
610                         g_free(tmp);
611                 }
612
613         }
614
615         /* prepend bookpath */
616         if(ds && ds->interface && ds->interface->name) {
617                 tmp = path;
618                 path = g_strdup_printf("%s%s%s", ds->interface->name,
619                                        (*tmp == '\0') ? "" : "/", tmp);
620                 g_free(tmp);
621         }
622
623         return path;
624 }
625
626 static void refresh_stores(gchar *email_to_select, GSList *detail_to_select)
627 {
628         refresh_addr_hash();
629         if(email_store)
630                 gtk_list_store_clear(email_store);
631         if(detail_store)
632                 gtk_list_store_clear(detail_store);
633         g_hash_table_foreach(addr_hash,append_to_email_store,email_store);
634
635         /* sort the email store */
636         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(email_store),
637                                              0, GTK_SORT_ASCENDING);
638
639         /* try to select email address */
640         if(email_to_select) {
641                 /* Search email in email store */
642                 GtkTreeIter iter;
643                 GtkTreeSelection *selection;
644
645                 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(email_store), &iter))
646                         return;
647                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(email_view));
648
649                 do {
650                         gint retVal;
651                         gchar *email;
652
653                         gtk_tree_model_get(GTK_TREE_MODEL(email_store), &iter, 0, &email, -1);
654                         retVal = g_ascii_strncasecmp(email,email_to_select,strlen(email));
655                         g_free(email);
656                         if(retVal == 0) {
657                                 gtk_tree_selection_select_iter(selection,&iter);
658                                 break;
659                         }
660                 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(email_store), &iter));
661
662         }
663
664         /* try to select detail rows */
665         if(detail_to_select) {
666                 GtkTreeIter iter;
667                 GtkTreeSelection *sel;
668                 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(detail_store), &iter))
669                         return;
670                 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
671
672                 do {
673                         GSList *walk;
674                         ItemPerson *person;
675                         gtk_tree_model_get(GTK_TREE_MODEL(detail_store), &iter,
676                                            COL_ITEM, &person, -1);
677                         for(walk = detail_to_select; walk; walk = walk->next) {
678                                 gchar *uid = walk->data;
679                                 if(uid && ADDRITEM_ID(person) &&
680                                         (strcmp(uid,ADDRITEM_ID(person)) == 0))
681                                         gtk_tree_selection_select_iter(sel,&iter);
682                         }
683                 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(detail_store), &iter));
684         }
685 }
686
687 static void detail_row_activated(GtkTreeView       *tree_view,
688                                  GtkTreePath       *path,
689                                  GtkTreeViewColumn *column,
690                                  gpointer           user_data)
691 {
692         GtkTreeIter iter;
693         ItemPerson *person;
694         AddressDataSource *ds;
695         GtkTreeModel *model;
696         AddressBookFile *abf;
697
698         model = gtk_tree_view_get_model(tree_view);
699
700         if(!gtk_tree_model_get_iter(model,&iter,path))
701                 return;
702
703         gtk_tree_model_get(model, &iter, COL_ITEM, &person, COL_DS, &ds, -1);
704
705
706         if(!((ds->type == ADDR_IF_BOOK) || ds->type == ADDR_IF_LDAP)) {
707                 debug_print("Unsupported address datasource type for editing\n");
708                 return;
709         }
710
711         abf = ds->rawDataSource;
712         if(inline_edit_vbox)
713                 gtk_widget_show_all(inline_edit_vbox);
714         if(editing_uid)
715                 g_free(editing_uid);
716         editing_uid = g_strdup(ADDRITEM_ID(person));
717         addressbook_edit_person(abf,NULL,person,FALSE,inline_edit_vbox,
718                                 edit_post_update_cb,FALSE);
719 }
720
721 static void edit_post_update_cb(ItemPerson *item)
722 {
723         GtkTreeSelection *sel;
724         gchar *email;
725         GList *detail_sel;
726         GList *walk;
727         GSList *detail;
728         GtkTreeIter iter;
729         GtkTreeModel *model;
730         ItemPerson *person;
731
732         /* save selection for after the update */
733
734         /* email -> string of email address */
735         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(email_view));
736         if(gtk_tree_selection_get_selected(sel,NULL,&iter))
737                 gtk_tree_model_get(GTK_TREE_MODEL(email_store), &iter, 0, &email, -1);
738         else
739                 email = NULL;
740
741         /* detail -> GSList of ItemPerson UIDs */
742         detail = NULL;
743         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
744         detail_sel = gtk_tree_selection_get_selected_rows(sel, &model);
745         for(walk = detail_sel; walk; walk = walk->next) {
746                 GtkTreePath *path = walk->data;
747                 if(!gtk_tree_model_get_iter(model,&iter,path))
748                         continue;
749                 gtk_tree_model_get(model, &iter, COL_ITEM, &person,-1);
750                 detail = g_slist_prepend(detail, g_strdup(ADDRITEM_ID(person)));
751         }
752         g_list_foreach(detail_sel, (GFunc)gtk_tree_path_free, NULL);
753         g_list_free(detail_sel);
754
755         /* now refresh the stores, trying to keep the selections active */
756         refresh_stores(email,detail);
757
758         /* cleanup */
759         if(email)
760                 g_free(email);
761         g_slist_foreach(detail, (GFunc)g_free, NULL);
762         g_slist_free(detail);
763 }
764
765 static void cb_edit_btn_clicked(GtkButton *button, gpointer data)
766 {
767         GtkTreeSelection *selection;
768         GList *selected;
769         GtkTreeModel *model;
770
771         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
772         selected = gtk_tree_selection_get_selected_rows(selection,&model);
773         g_return_if_fail(selected);
774
775         /* we are guaranteed to have exactly one row selected */
776         gtk_tree_view_row_activated(GTK_TREE_VIEW(detail_view),(GtkTreePath*)selected->data,
777                                     gtk_tree_view_get_column(GTK_TREE_VIEW(detail_view),0));
778
779         g_list_foreach(selected, (GFunc)gtk_tree_path_free, NULL);
780         g_list_free(selected);
781 }
782
783 static void cb_del_btn_clicked(GtkButton *button, gpointer data)
784 {
785         GtkTreeIter iter;
786         GtkTreeModel *model;
787         GtkTreeSelection *selection;
788         ItemPerson *item;
789         AddressDataSource *ds;
790         GList *list;
791         GList *ref_list;
792         GList *walk;
793         GtkTreeRowReference *ref;
794         AlertValue aval;
795         GtkTreeSelection *sel;
796         gchar *email;
797
798         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
799
800         list = gtk_tree_selection_get_selected_rows(selection, &model);
801
802         if(!list)
803                 return;
804
805         aval = alertpanel(_("Delete address(es)"),
806                           _("Really delete the address(es)?"),
807                           GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL);
808         if(aval != G_ALERTALTERNATE)
809                 return;
810
811         ref_list = NULL;
812         for(walk = list; walk; walk = walk->next) {
813                 ref = gtk_tree_row_reference_new(model,(GtkTreePath*)(walk->data));
814                 ref_list = g_list_prepend(ref_list, ref);
815         }
816         g_list_foreach(list, (GFunc)gtk_tree_path_free, NULL);
817         g_list_free(list);
818
819         for(walk = ref_list; walk; walk = walk->next) {
820                 GtkTreePath *path;
821                 ref = walk->data;
822                 if(!gtk_tree_row_reference_valid(ref))
823                         continue;
824                 path = gtk_tree_row_reference_get_path(ref);
825                 if(gtk_tree_model_get_iter(model, &iter, path)) {
826                         gtk_tree_model_get(model, &iter, COL_ITEM, &item, COL_DS, &ds, -1);
827                         delete_item(item,ds);
828                 }
829                 gtk_tree_path_free(path);
830         }
831
832         g_list_foreach(ref_list, (GFunc)gtk_tree_row_reference_free, NULL);
833         g_list_free(ref_list);
834
835         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(email_view));
836         if(gtk_tree_selection_get_selected(sel,NULL,&iter))
837                 gtk_tree_model_get(GTK_TREE_MODEL(email_store), &iter, 0, &email, -1);
838         else
839                 email = NULL;
840         refresh_stores(email,NULL);
841         if(email)
842                 g_free(email);
843 }
844
845 static gboolean delete_item(ItemPerson *item, AddressDataSource *ds)
846 {
847         AddressBookFile *abf;
848         AddressInterface *iface;
849
850         /* Test for read only */
851         iface = ds->interface;
852         if( iface->readOnly ) {
853                 alertpanel( _("Delete address"),
854                             _("This address data is readonly and cannot be deleted."),
855                             GTK_STOCK_CLOSE, NULL, NULL );
856                 return FALSE;
857         }
858
859         if(!(abf = ds->rawDataSource))
860                 return FALSE;
861
862         item->status = DELETE_ENTRY;
863         item = addrbook_remove_person(abf, item);
864
865 #ifdef USE_LDAP
866
867         if (ds && ds->type == ADDR_IF_LDAP) {
868                 LdapServer *server = ds->rawDataSource;
869                 ldapsvr_set_modified(server, TRUE);
870                 ldapsvr_update_book(server, item);
871         }
872
873 #endif
874
875         if(item) {
876                 gchar *filename = addritem_person_get_picture(item);
877                 if (filename && is_file_exist(filename))
878                         g_unlink(filename);
879                 g_free(filename);
880                 addritem_free_item_person(item);
881         }
882         return TRUE;
883 }
884
885 static gboolean cb_finder_results_dialog_key_pressed(GtkWidget *widget,
886         GdkEventKey *event,
887         gpointer data)
888 {
889         if(event) {
890                 if(event->keyval == GDK_Delete && detail_view_has_focus)
891                         cb_del_btn_clicked(NULL,NULL);
892                 else if(event->keyval == GDK_Escape)
893                         gtk_widget_destroy(dialog);
894         }
895
896         return FALSE;
897 }
898
899 static gboolean detail_focus_in(GtkWidget *widget,
900                                 GdkEventFocus *event,gpointer data)
901 {
902         detail_view_has_focus = TRUE;
903         return FALSE;
904 }
905
906 static gboolean detail_focus_out(GtkWidget *widget,
907                                  GdkEventFocus *event,gpointer data)
908 {
909         detail_view_has_focus = FALSE;
910         return FALSE;
911 }