fix bug 2989, 'Segfault at startup because of corrupted folderlist.xml'
[claws.git] / src / addrduplicates.c
blob2812a977da5eb3dea9496bff4f12433a4a85f5ce
1 /* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
2 * Copyright (C) 2007-2012 Holger Berndt <hb@claws-mail.org>
3 * and the Claws Mail team
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include <gdk/gdk.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <glib/gi18n.h>
27 #include <string.h>
29 #include "defs.h"
31 #ifdef USE_LDAP
32 #include "ldapserver.h"
33 #include "ldapupdate.h"
34 #endif
35 #include "addrduplicates.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"
45 typedef struct
47 ItemPerson *person;
48 AddressDataSource *ds;
49 gchar *book_path;
51 AddrDupListEntry;
53 enum {
54 COL_BOOKPATH = 0,
55 COL_NAME,
56 COL_ITEM,
57 COL_DS,
58 NUM_COLS
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);
85 static void cb_del_btn_clicked(GtkButton *, gpointer);
86 static void cb_edit_btn_clicked(GtkButton *, gpointer);
87 static gchar* get_bookpath(ItemPerson*,AddressDataSource*);
88 static gboolean is_editing_entry_only_selection(void);
89 static void edit_post_update_cb(ItemPerson*);
91 static GHashTable *addr_hash;
92 static gboolean include_same_book = TRUE;
93 static gboolean include_other_books = TRUE;
95 static GtkListStore *email_store;
96 static GtkListStore *detail_store;
97 static GtkWidget *email_view;
98 static GtkWidget *detail_view;
99 static GtkWidget *inline_edit_vbox;
101 static GtkWidget *del_btn;
102 static GtkWidget *edit_btn;
104 static GtkWidget *dialog;
105 static gchar *editing_uid;
106 static gboolean detail_view_has_focus;
108 void addrduplicates_find(GtkWindow *parent)
110 if(create_dialog()) {
111 refresh_addr_hash();
112 present_finder_results(parent);
116 static gboolean create_dialog()
118 gboolean want_search;
119 GtkWidget *vbox;
120 GtkWidget *check_same_book;
121 GtkWidget *check_other_book;
122 AlertValue val;
124 want_search = FALSE;
126 vbox = gtk_vbox_new(FALSE, 0);
127 check_same_book = gtk_check_button_new_with_label(_("Show duplicates in "
128 "the same book"));
129 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_same_book),
130 include_same_book);
131 gtk_box_pack_start(GTK_BOX(vbox), check_same_book, FALSE, FALSE, 0);
132 gtk_widget_show(check_same_book);
133 check_other_book = gtk_check_button_new_with_label(_("Show duplicates in "
134 "different books"));
135 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_other_book),
136 include_other_books);
137 gtk_box_pack_start(GTK_BOX(vbox), check_other_book, FALSE, FALSE, 0);
138 gtk_widget_show(check_other_book);
140 /* prevent checkboxes from being destroyed on dialog close */
141 g_object_ref(check_same_book);
142 g_object_ref(check_other_book);
144 val = alertpanel_full(_("Find address book email duplicates"),
145 _("Claws Mail will now search for duplicate email "
146 "addresses in the address book."),
147 GTK_STOCK_CANCEL,GTK_STOCK_FIND,NULL, FALSE, vbox, ALERT_NOTICE,
148 G_ALERTALTERNATE);
149 if(val == G_ALERTALTERNATE) {
150 want_search = TRUE;
152 /* save options */
153 include_same_book =
154 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_same_book));
155 include_other_books =
156 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_other_book));
160 g_object_unref(check_same_book);
161 g_object_unref(check_other_book);
162 return want_search;
165 static void refresh_addr_hash(void)
167 if(addr_hash)
168 g_hash_table_destroy(addr_hash);
169 addr_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
170 g_free, destroy_addr_hash_val);
171 fill_hash_table();
174 static void destroy_addr_hash_val(gpointer value)
176 GSList *list = (GSList*) value;
177 GSList *walk;
179 for(walk = list; walk; walk = walk->next) {
180 AddrDupListEntry *entry = (AddrDupListEntry*) walk->data;
181 if(entry && entry->book_path)
182 g_free(entry->book_path);
183 if(entry)
184 g_free(entry);
186 if(list)
187 g_slist_free(list);
190 static GSList* deep_copy_hash_val(GSList *in)
192 GSList *walk;
193 GSList *out = NULL;
195 out = g_slist_copy(in);
196 for(walk = out; walk; walk = walk->next) {
197 AddrDupListEntry *out_entry;
198 AddrDupListEntry *in_entry = walk->data;
200 out_entry = g_new0(AddrDupListEntry,1);
201 out_entry->person = in_entry->person;
202 out_entry->ds = in_entry->ds;
203 out_entry->book_path = g_strdup(in_entry->book_path);
204 walk->data = out_entry;
207 return out;
210 static void fill_hash_table()
212 addrindex_load_person_ds(collect_emails);
213 g_hash_table_foreach_remove(addr_hash,is_not_duplicate, NULL);
216 static gboolean is_not_duplicate(gpointer key, gpointer value,
217 gpointer user_data)
219 gboolean is_in_same_book;
220 gboolean is_in_other_books;
221 GSList *books;
222 GSList *walk;
223 gboolean retval;
224 GSList *list = value;
226 /* remove everything that is just in one book */
227 if(g_slist_length(list) <= 1)
228 return TRUE;
230 /* work on a shallow copy */
231 books = g_slist_copy(list);
233 /* sorting the list makes it easier to check for books */
234 books = g_slist_sort(books, books_compare);
236 /* check if a book appears twice */
237 is_in_same_book = FALSE;
238 for(walk = books; walk && walk->next; walk = walk->next) {
239 if(books_compare(walk->data, walk->next->data) == 0) {
240 is_in_same_book = TRUE;
241 break;
245 /* check is at least two different books appear in the list */
246 is_in_other_books = FALSE;
247 if(books && books->next) {
248 for(walk = books->next; walk; walk = walk->next) {
249 if(books_compare(walk->data, books->data) != 0) {
250 is_in_other_books = TRUE;
251 break;
256 /* delete the shallow copy */
257 g_slist_free(books);
259 retval = FALSE;
260 if(is_in_same_book && include_same_book)
261 retval = TRUE;
262 if(is_in_other_books && include_other_books)
263 retval = TRUE;
264 retval = !retval;
266 return retval;
269 static gint collect_emails(ItemPerson *itemperson, AddressDataSource *ds)
271 gchar *addr;
272 GList *nodeM;
273 GSList *old_val;
274 GSList *new_val;
275 AddrDupListEntry *entry;
277 /* Process each E-Mail address */
278 nodeM = itemperson->listEMail;
279 while(nodeM) {
280 ItemEMail *email = nodeM->data;
282 addr = g_utf8_strdown(email->address, -1);
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;
289 entry = g_new0(AddrDupListEntry,1);
290 entry->person = itemperson;
291 entry->ds = ds;
292 entry->book_path = get_bookpath(itemperson, ds);
294 new_val = g_slist_prepend(new_val, entry);
295 g_hash_table_insert(addr_hash, addr, new_val);
297 nodeM = g_list_next(nodeM);
299 return 0;
302 static gint books_compare(gconstpointer a, gconstpointer b)
304 const AddrDupListEntry *entry1;
305 const AddrDupListEntry *entry2;
306 entry1 = a;
307 entry2 = b;
308 return strcmp(entry1->book_path, entry2->book_path);
311 static void present_finder_results(GtkWindow *parent)
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;
324 if(g_hash_table_size(addr_hash) == 0) {
325 alertpanel_notice(_("No duplicate email addresses found in the address book"));
326 return;
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);
335 g_signal_connect(email_select, "changed",
336 (GCallback)email_selection_changed, NULL);
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);
344 g_signal_connect(detail_select, "changed",
345 (GCallback)detail_selection_changed, NULL);
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;
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"));
358 vbox = gtk_vbox_new(FALSE, 0);
359 gtk_container_add(GTK_CONTAINER(dialog), vbox);
361 hpaned = gtk_hpaned_new();
362 gtk_box_pack_start(GTK_BOX(vbox), hpaned, TRUE, TRUE, 0);
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);
370 gtk_paned_add1(GTK_PANED(hpaned), scrolled_win);
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);
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);
389 g_object_get(G_OBJECT(hpaned),
390 "position", &pos, NULL);
391 if(pos < 200)
392 gtk_paned_set_position(GTK_PANED(hpaned), 200);
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);
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);
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);
408 close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
409 gtk_box_pack_start(GTK_BOX(hbox), close, TRUE, TRUE, 0);
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);
422 inc_lock();
423 gtk_widget_show_all(dialog);
426 static void cb_finder_results_dialog_destroy(GtkWindow *win, gpointer data)
428 email_store = NULL;
429 detail_store = NULL;
430 email_view = NULL;
431 inline_edit_vbox = NULL;
433 if(addr_hash) {
434 g_hash_table_destroy(addr_hash);
435 addr_hash = NULL;
437 dialog = NULL;
438 addressbook_refresh();
439 inc_unlock();
442 static GtkWidget* create_email_view(GtkListStore *store)
444 GtkWidget *view;
445 GtkCellRenderer *renderer;
447 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
448 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), prefs_common.use_stripes_everywhere);
449 renderer = gtk_cell_renderer_text_new();
450 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
452 _("Address"),
453 renderer,
454 "text", 0,
455 NULL);
456 g_object_unref(store);
457 return view;
460 static GtkWidget* create_detail_view(GtkListStore *store)
462 GtkWidget *view;
463 GtkCellRenderer *renderer;
464 GList *cols;
465 GList *walk;
467 view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
468 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), prefs_common.use_stripes_everywhere);
469 renderer = gtk_cell_renderer_text_new();
471 /* col 1 */
472 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
474 _("Address book path"),
475 renderer,
476 "text", COL_BOOKPATH,
477 NULL);
478 /* col 2 */
479 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
481 _("Name"),
482 renderer,
483 "text", COL_NAME,
484 NULL);
486 cols = gtk_tree_view_get_columns(GTK_TREE_VIEW(view));
487 for(walk = cols; walk; walk = walk->next)
488 gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(walk->data),
489 TRUE);
490 g_list_free(cols);
492 g_signal_connect(view, "row-activated",
493 G_CALLBACK(detail_row_activated), NULL);
495 g_signal_connect(view, "focus-in-event",
496 G_CALLBACK(detail_focus_in), NULL);
497 g_signal_connect(view, "focus-out-event",
498 G_CALLBACK(detail_focus_out), NULL);
501 return view;
504 static void append_to_email_store(gpointer key,gpointer value,gpointer data)
506 GtkTreeIter iter;
507 GtkListStore *store = (GtkListStore*) data;
509 gtk_list_store_append(store, &iter);
510 gtk_list_store_set(store, &iter, 0, (gchar*) key, -1);
513 static gboolean is_editing_entry_only_selection(void)
515 GtkTreeSelection *sel_detail;
516 GtkTreeIter iter;
517 GList *selected;
518 GtkTreeModel *model;
519 ItemPerson *item;
521 sel_detail = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
523 if(gtk_tree_selection_count_selected_rows(sel_detail) > 1)
524 return FALSE;
526 selected = gtk_tree_selection_get_selected_rows(sel_detail,&model);
527 if(!selected)
528 return FALSE;
530 gtk_tree_model_get_iter(model, &iter, (GtkTreePath*)selected->data);
531 g_list_foreach(selected, (GFunc)gtk_tree_path_free, NULL);
532 g_list_free(selected);
534 gtk_tree_model_get(model, &iter, COL_ITEM, &item,-1);
535 if(ADDRITEM_ID(item) && editing_uid &&
536 strcmp(ADDRITEM_ID(item),editing_uid) == 0)
537 return TRUE;
538 else
539 return FALSE;
542 static void detail_selection_changed(GtkTreeSelection *selection, gpointer data)
544 gint num_selected;
545 num_selected = gtk_tree_selection_count_selected_rows(selection);
547 if(num_selected > 0)
548 gtk_widget_set_sensitive(del_btn,TRUE);
549 else
550 gtk_widget_set_sensitive(del_btn,FALSE);
552 if(num_selected == 1)
553 gtk_widget_set_sensitive(edit_btn,TRUE);
554 else
555 gtk_widget_set_sensitive(edit_btn,FALSE);
557 if(!is_editing_entry_only_selection())
558 addressbook_edit_person_widgetset_hide();
561 static void email_selection_changed(GtkTreeSelection *selection, gpointer data)
563 GtkTreeIter iter;
564 GtkTreeModel *model;
565 gchar *email;
567 if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
568 GSList *hashval;
569 GSList *walk;
571 gtk_tree_model_get(model, &iter, 0, &email, -1);
573 hashval = g_hash_table_lookup(addr_hash, email);
574 gtk_list_store_clear(detail_store);
575 for(walk = hashval; walk; walk = walk->next) {
576 AddrDupListEntry *entry = walk->data;
577 if(!entry)
578 continue;
579 gtk_list_store_append(detail_store, &iter);
580 gtk_list_store_set(detail_store, &iter,
581 COL_BOOKPATH, entry->book_path,
582 COL_NAME, addressbook_set_col_name_guard(ADDRITEM_NAME(entry->person)),
583 COL_ITEM, entry->person,
584 COL_DS, entry->ds,
585 -1);
587 g_free(email);
591 static gchar* get_bookpath(ItemPerson *itemPerson, AddressDataSource *ds)
593 gchar *path;
594 gchar *tmp;
595 AddrItemObject *item;
597 item = (AddrItemObject*)itemPerson;
598 path = g_strdup("");
599 while((item = ADDRITEM_PARENT(item)) != NULL) {
601 if(ADDRITEM_TYPE(item) == ITEMTYPE_FOLDER) {
602 ItemFolder *folder = (ItemFolder*) item;
603 tmp = path;
604 path = g_strdup_printf("%s%s%s",
605 folder->isRoot ? addrindex_ds_get_name(ds) :
606 ADDRITEM_NAME(folder),
607 (*tmp == '\0') ? "" : "/", tmp);
608 g_free(tmp);
613 /* prepend bookpath */
614 if(ds && ds->interface && ds->interface->name) {
615 tmp = path;
616 path = g_strdup_printf("%s%s%s", ds->interface->name,
617 (*tmp == '\0') ? "" : "/", tmp);
618 g_free(tmp);
621 return path;
624 static void refresh_stores(gchar *email_to_select, GSList *detail_to_select)
626 refresh_addr_hash();
627 if(email_store)
628 gtk_list_store_clear(email_store);
629 if(detail_store)
630 gtk_list_store_clear(detail_store);
631 g_hash_table_foreach(addr_hash,append_to_email_store,email_store);
633 /* sort the email store */
634 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(email_store),
635 0, GTK_SORT_ASCENDING);
637 /* try to select email address */
638 if(email_to_select) {
639 /* Search email in email store */
640 GtkTreeIter iter;
641 GtkTreeSelection *selection;
643 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(email_store), &iter))
644 return;
645 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(email_view));
647 do {
648 gint retVal;
649 gchar *email;
651 gtk_tree_model_get(GTK_TREE_MODEL(email_store), &iter, 0, &email, -1);
652 retVal = g_ascii_strncasecmp(email,email_to_select,strlen(email));
653 g_free(email);
654 if(retVal == 0) {
655 gtk_tree_selection_select_iter(selection,&iter);
656 break;
658 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(email_store), &iter));
662 /* try to select detail rows */
663 if(detail_to_select) {
664 GtkTreeIter iter;
665 GtkTreeSelection *sel;
666 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(detail_store), &iter))
667 return;
668 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
670 do {
671 GSList *walk;
672 ItemPerson *person;
673 gtk_tree_model_get(GTK_TREE_MODEL(detail_store), &iter,
674 COL_ITEM, &person, -1);
675 for(walk = detail_to_select; walk; walk = walk->next) {
676 gchar *uid = walk->data;
677 if(uid && ADDRITEM_ID(person) &&
678 (strcmp(uid,ADDRITEM_ID(person)) == 0))
679 gtk_tree_selection_select_iter(sel,&iter);
681 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(detail_store), &iter));
685 static void detail_row_activated(GtkTreeView *tree_view,
686 GtkTreePath *path,
687 GtkTreeViewColumn *column,
688 gpointer user_data)
690 GtkTreeIter iter;
691 ItemPerson *person;
692 AddressDataSource *ds;
693 GtkTreeModel *model;
694 AddressBookFile *abf;
696 model = gtk_tree_view_get_model(tree_view);
698 if(!gtk_tree_model_get_iter(model,&iter,path))
699 return;
701 gtk_tree_model_get(model, &iter, COL_ITEM, &person, COL_DS, &ds, -1);
704 if(!((ds->type == ADDR_IF_BOOK) || ds->type == ADDR_IF_LDAP)) {
705 debug_print("Unsupported address datasource type for editing\n");
706 return;
709 abf = ds->rawDataSource;
710 if(inline_edit_vbox)
711 gtk_widget_show_all(inline_edit_vbox);
712 if(editing_uid)
713 g_free(editing_uid);
714 editing_uid = g_strdup(ADDRITEM_ID(person));
715 addressbook_edit_person(abf,NULL,person,FALSE,inline_edit_vbox,
716 edit_post_update_cb,FALSE);
719 static void edit_post_update_cb(ItemPerson *item)
721 GtkTreeSelection *sel;
722 gchar *email;
723 GList *detail_sel;
724 GList *walk;
725 GSList *detail;
726 GtkTreeIter iter;
727 GtkTreeModel *model;
728 ItemPerson *person;
730 /* save selection for after the update */
732 /* email -> string of email address */
733 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(email_view));
734 if(gtk_tree_selection_get_selected(sel,NULL,&iter))
735 gtk_tree_model_get(GTK_TREE_MODEL(email_store), &iter, 0, &email, -1);
736 else
737 email = NULL;
739 /* detail -> GSList of ItemPerson UIDs */
740 detail = NULL;
741 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
742 detail_sel = gtk_tree_selection_get_selected_rows(sel, &model);
743 for(walk = detail_sel; walk; walk = walk->next) {
744 GtkTreePath *path = walk->data;
745 if(!gtk_tree_model_get_iter(model,&iter,path))
746 continue;
747 gtk_tree_model_get(model, &iter, COL_ITEM, &person,-1);
748 detail = g_slist_prepend(detail, g_strdup(ADDRITEM_ID(person)));
750 g_list_foreach(detail_sel, (GFunc)gtk_tree_path_free, NULL);
751 g_list_free(detail_sel);
753 /* now refresh the stores, trying to keep the selections active */
754 refresh_stores(email,detail);
756 /* cleanup */
757 if(email)
758 g_free(email);
759 g_slist_foreach(detail, (GFunc)g_free, NULL);
760 g_slist_free(detail);
763 static void cb_edit_btn_clicked(GtkButton *button, gpointer data)
765 GtkTreeSelection *selection;
766 GList *selected;
767 GtkTreeModel *model;
769 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
770 selected = gtk_tree_selection_get_selected_rows(selection,&model);
771 cm_return_if_fail(selected);
773 /* we are guaranteed to have exactly one row selected */
774 gtk_tree_view_row_activated(GTK_TREE_VIEW(detail_view),(GtkTreePath*)selected->data,
775 gtk_tree_view_get_column(GTK_TREE_VIEW(detail_view),0));
777 g_list_foreach(selected, (GFunc)gtk_tree_path_free, NULL);
778 g_list_free(selected);
781 static void cb_del_btn_clicked(GtkButton *button, gpointer data)
783 GtkTreeIter iter;
784 GtkTreeModel *model;
785 GtkTreeSelection *selection;
786 ItemPerson *item;
787 AddressDataSource *ds;
788 GList *list;
789 GList *ref_list;
790 GList *walk;
791 GtkTreeRowReference *ref;
792 AlertValue aval;
793 GtkTreeSelection *sel;
794 gchar *email;
796 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(detail_view));
798 list = gtk_tree_selection_get_selected_rows(selection, &model);
800 if(!list)
801 return;
803 aval = alertpanel(_("Delete address(es)"),
804 _("Really delete the address(es)?"),
805 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL);
806 if(aval != G_ALERTALTERNATE)
807 return;
809 ref_list = NULL;
810 for(walk = list; walk; walk = walk->next) {
811 ref = gtk_tree_row_reference_new(model,(GtkTreePath*)(walk->data));
812 ref_list = g_list_prepend(ref_list, ref);
814 g_list_foreach(list, (GFunc)gtk_tree_path_free, NULL);
815 g_list_free(list);
817 for(walk = ref_list; walk; walk = walk->next) {
818 GtkTreePath *path;
819 ref = walk->data;
820 if(!gtk_tree_row_reference_valid(ref))
821 continue;
822 path = gtk_tree_row_reference_get_path(ref);
823 if(gtk_tree_model_get_iter(model, &iter, path)) {
824 gtk_tree_model_get(model, &iter, COL_ITEM, &item, COL_DS, &ds, -1);
825 addrduplicates_delete_item_person(item,ds);
827 gtk_tree_path_free(path);
830 g_list_foreach(ref_list, (GFunc)gtk_tree_row_reference_free, NULL);
831 g_list_free(ref_list);
833 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(email_view));
834 if(gtk_tree_selection_get_selected(sel,NULL,&iter))
835 gtk_tree_model_get(GTK_TREE_MODEL(email_store), &iter, 0, &email, -1);
836 else
837 email = NULL;
838 refresh_stores(email,NULL);
839 if(email)
840 g_free(email);
843 gboolean addrduplicates_delete_item_person(ItemPerson *item, AddressDataSource *ds)
845 AddressBookFile *abf;
846 AddressInterface *iface;
847 if (!ds)
848 return FALSE;
849 /* Test for read only */
850 iface = ds->interface;
851 if( iface && iface->readOnly ) {
852 alertpanel( _("Delete address"),
853 _("This address data is readonly and cannot be deleted."),
854 GTK_STOCK_CLOSE, NULL, NULL );
855 return FALSE;
858 if(!(abf = ds->rawDataSource))
859 return FALSE;
861 item->status = DELETE_ENTRY;
862 item = addrbook_remove_person(abf, item);
864 #ifdef USE_LDAP
866 if (ds && ds->type == ADDR_IF_LDAP) {
867 LdapServer *server = ds->rawDataSource;
868 ldapsvr_set_modified(server, TRUE);
869 ldapsvr_update_book(server, item);
872 #endif
874 if(item) {
875 gchar *filename = addritem_person_get_picture(item);
876 if (filename && is_file_exist(filename))
877 claws_unlink(filename);
878 g_free(filename);
879 addritem_free_item_person(item);
881 return TRUE;
884 static gboolean cb_finder_results_dialog_key_pressed(GtkWidget *widget,
885 GdkEventKey *event,
886 gpointer data)
888 if(event) {
889 if(event->keyval == GDK_KEY_Delete && detail_view_has_focus)
890 cb_del_btn_clicked(NULL,NULL);
891 else if(event->keyval == GDK_KEY_Escape)
892 gtk_widget_destroy(dialog);
895 return FALSE;
898 static gboolean detail_focus_in(GtkWidget *widget,
899 GdkEventFocus *event,gpointer data)
901 detail_view_has_focus = TRUE;
902 return FALSE;
905 static gboolean detail_focus_out(GtkWidget *widget,
906 GdkEventFocus *event,gpointer data)
908 detail_view_has_focus = FALSE;
909 return FALSE;