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