Fix memory leaks from results of gtk_tree_model_get().
[claws.git] / src / addrcustomattr.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2007-2013 The Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "menu.h"
35 #include "addrcustomattr.h"
36 #include "manage_window.h"
37 #include "prefs_common.h"
38 #include "alertpanel.h"
39 #include "addrbook.h"
40 #include "editaddress.h"
41
42 static GtkActionGroup *custom_attr_popup_action = NULL;
43 static GtkWidget *custom_attr_popup_menu = NULL;
44
45 static struct CustomAttrWindow
46 {
47         GtkWidget *window;
48         GtkWidget *attr_list;
49         GtkWidget *hbox1;
50         GtkWidget *hbox2;
51         GtkWidget *vbox1;
52         GtkWidget *label;
53         GtkWidget *cancel_btn;
54         GtkWidget *ok_btn;
55         GtkWidget *add_entry;
56         GtkWidget *add_btn;
57 } custom_attr_window;
58
59 enum {
60         CUSTOM_ATTR_NAME,
61         N_CUSTOM_ATTR
62 };
63
64 static gchar *default_addressbook_attributes_table[] = {
65         N_("date of birth"),
66         N_("address"),
67         N_("phone"),
68         N_("mobile phone"),
69         N_("organization"),
70         N_("office address"),
71         N_("office phone"),
72         N_("fax"),
73         N_("website"),
74         NULL
75 };
76
77 static gboolean dirty = FALSE;
78
79 static void custom_attr_window_create(void);
80 static void custom_attr_selected_attr_edited(GtkCellRendererText *widget,
81                 gchar *arg1, gchar *arg2,
82                 GtkWidget *list_view);
83 static void custom_attr_window_load_list(GList *list);
84 static void custom_attr_window_save_list (void);
85 static GList *custom_attr_default_list(void);
86
87 void addressbook_custom_attr_edit()
88 {
89         if (!custom_attr_window.window)
90                 custom_attr_window_create();
91
92         manage_window_set_transient(GTK_WINDOW(custom_attr_window.window));
93         gtk_widget_grab_focus(custom_attr_window.ok_btn);
94         
95         custom_attr_window_load_list(prefs_common.addressbook_custom_attributes);
96
97         gtk_widget_show(custom_attr_window.window);
98         gtk_widget_grab_focus(custom_attr_window.attr_list);
99         gtk_window_set_modal(GTK_WINDOW(custom_attr_window.window), TRUE);
100 }
101
102 static gint custom_attr_cmp_func (GtkTreeModel *model, GtkTreeIter *a,
103                                                                   GtkTreeIter *b, gpointer userdata)
104  {
105         gchar *name1, *name2;
106         gint res;
107
108         gtk_tree_model_get(model, a, CUSTOM_ATTR_NAME, &name1, -1);
109         gtk_tree_model_get(model, b, CUSTOM_ATTR_NAME, &name2, -1);
110         
111         if (name1 == NULL)
112                 return name2 == NULL ? 0:1;
113         
114         if (name2 == NULL)
115                 return 1;
116         
117         res = g_utf8_collate(name1, name2);
118         g_free(name1);
119         g_free(name2);
120
121         return res;
122 }
123
124 static GtkListStore* custom_attr_window_create_data_store(void)
125 {
126         GtkListStore *store = gtk_list_store_new(N_CUSTOM_ATTR,
127                                   G_TYPE_STRING,
128                                   -1);
129         GtkTreeSortable *sortable = GTK_TREE_SORTABLE(store);
130
131         gtk_tree_sortable_set_sort_func(sortable, 0, custom_attr_cmp_func,
132                                     NULL, NULL);
133
134         return store;
135 }
136
137 static void custom_attr_window_create_list_view_columns(GtkWidget *list_view)
138 {
139         GtkTreeViewColumn *column;
140         GtkCellRenderer *renderer;
141
142         renderer = gtk_cell_renderer_text_new();
143         g_object_set(G_OBJECT(renderer), "editable", TRUE, NULL);
144
145         column = gtk_tree_view_column_new_with_attributes
146                 (_("Attribute name"),
147                  renderer,
148                  "text", CUSTOM_ATTR_NAME,
149                  NULL);
150         gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
151         gtk_tree_view_column_set_resizable(column, TRUE);
152         gtk_tree_view_set_search_column(GTK_TREE_VIEW(list_view),
153                                         CUSTOM_ATTR_NAME);
154         g_signal_connect(G_OBJECT(renderer), "edited",
155                          G_CALLBACK(custom_attr_selected_attr_edited),
156                          list_view);
157 }
158
159 static void custom_attr_window_list_view_clear_list(GtkWidget *list_view, gboolean warn)
160 {
161         if (!warn || alertpanel(_("Delete all attribute names"),
162                        _("Do you really want to delete all attribute names?"),
163                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_FIRST) == G_ALERTALTERNATE) {
164                 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
165                                                 (GTK_TREE_VIEW(list_view)));
166                 gtk_list_store_clear(list_store);
167                 dirty = TRUE;
168         }
169 }
170
171 static void custom_attr_popup_clear_list (void *obj, void *data)
172 {
173         custom_attr_window_list_view_clear_list(custom_attr_window.attr_list, TRUE);
174 }
175
176 static void custom_attr_popup_delete (void *obj, void *data)
177 {
178         GtkTreeIter sel;
179         GtkTreeModel *model;
180         
181         if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection
182                                 (GTK_TREE_VIEW(custom_attr_window.attr_list)),
183                                 &model, &sel))
184                 return;                         
185
186         if (alertpanel(_("Delete attribute name"),
187                        _("Do you really want to delete this attribute name?"),
188                        GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_FIRST) == G_ALERTALTERNATE) {
189                 gtk_list_store_remove(GTK_LIST_STORE(model), &sel);
190                 dirty = TRUE;
191         }
192 }
193
194 static void custom_attr_popup_factory_defaults (void *obj, void *data)
195 {
196         if (alertpanel(_("Reset to default"),
197                        _("Do you really want to replace all attribute names\nwith the default set?"),
198                        GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_FIRST) == G_ALERTALTERNATE) {
199                 GList *tmp = custom_attr_default_list();
200                 custom_attr_window_load_list(tmp);
201                 if (tmp) {
202                         GList *cur;
203                         cur = tmp;
204                         while (cur) {
205                                 g_free(cur->data);
206                                 cur = cur->next;
207                         }
208                         g_list_free(tmp);
209                 }
210                 dirty = TRUE;
211         }
212 }
213
214 static GtkActionEntry custom_attr_popup_entries[] =
215 {
216         {"CustomAttrPopup",                     NULL, "CustomAttrPopup" },
217         {"CustomAttrPopup/Delete",              NULL, N_("_Delete"), NULL, NULL, G_CALLBACK(custom_attr_popup_delete) },
218         {"CustomAttrPopup/DeleteAll",           NULL, N_("Delete _all"), NULL, NULL, G_CALLBACK(custom_attr_popup_clear_list) },
219         {"CustomAttrPopup/Reset",               NULL, N_("_Reset to default"), NULL, NULL, G_CALLBACK(custom_attr_popup_factory_defaults) },
220 };
221
222 static gint custom_attr_list_btn_pressed(GtkWidget *widget, GdkEventButton *event,
223                                     GtkTreeView *list_view)
224 {
225         if (event && event->button == 3) {
226                 GtkTreeModel *model = gtk_tree_view_get_model(list_view);
227                 GtkTreeIter iter;
228                 gboolean non_empty;
229
230                 if (!custom_attr_popup_menu) {
231                                 custom_attr_popup_action = cm_menu_create_action_group("CustomAttrPopup", custom_attr_popup_entries,
232                                         G_N_ELEMENTS(custom_attr_popup_entries), (gpointer)list_view);
233                                 MENUITEM_ADDUI("/Menus", "CustomAttrPopup", "CustomAttrPopup", GTK_UI_MANAGER_MENU)
234                                 MENUITEM_ADDUI("/Menus/CustomAttrPopup", "Delete", "CustomAttrPopup/Delete", GTK_UI_MANAGER_MENUITEM)
235                                 MENUITEM_ADDUI("/Menus/CustomAttrPopup", "DeleteAll", "CustomAttrPopup/DeleteAll", GTK_UI_MANAGER_MENUITEM)
236                                 MENUITEM_ADDUI("/Menus/CustomAttrPopup", "Reset", "CustomAttrPopup/Reset", GTK_UI_MANAGER_MENUITEM)
237                                 custom_attr_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
238                                         gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/CustomAttrPopup")) );
239                 }
240
241                 /* grey out popup menu items if list is empty */
242                 non_empty = gtk_tree_model_get_iter_first(model, &iter);
243                 cm_menu_set_sensitive("CustomAttrPopup/Delete", non_empty);
244                 cm_menu_set_sensitive("CustomAttrPopup/DeleteAll", non_empty);
245
246                 gtk_menu_popup(GTK_MENU(custom_attr_popup_menu), 
247                                NULL, NULL, NULL, NULL, 
248                                event->button, event->time);
249
250                 return FALSE;
251         }
252         return FALSE;
253 }
254
255 static gboolean custom_attr_list_popup_menu(GtkWidget *widget, gpointer data)
256 {
257         GtkTreeView *list_view = (GtkTreeView *)data;
258         GdkEventButton event;
259         
260         event.button = 3;
261         event.time = gtk_get_current_event_time();
262         
263         custom_attr_list_btn_pressed(NULL, &event, list_view);
264
265         return TRUE;
266 }
267
268 static GtkWidget *custom_attr_window_list_view_create   (void)
269 {
270         GtkTreeView *list_view;
271         GtkTreeSelection *selector;
272         GtkTreeModel *model;
273
274         model = GTK_TREE_MODEL(custom_attr_window_create_data_store());
275         list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
276         g_object_unref(model);  
277         gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
278                         CUSTOM_ATTR_NAME, GTK_SORT_ASCENDING);
279
280         gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
281         
282         selector = gtk_tree_view_get_selection(list_view);
283         gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
284
285         /* create the columns */
286         custom_attr_window_create_list_view_columns(GTK_WIDGET(list_view));
287
288         g_signal_connect(G_OBJECT(list_view), "popup-menu",
289                          G_CALLBACK(custom_attr_list_popup_menu), list_view);
290         g_signal_connect(G_OBJECT(list_view), "button-press-event",
291                         G_CALLBACK(custom_attr_list_btn_pressed), list_view);
292         return GTK_WIDGET(list_view);
293 }
294
295 static void custom_attr_window_close(void) 
296 {
297         if (dirty)
298                 custom_attr_window_save_list();
299         custom_attr_window_list_view_clear_list(custom_attr_window.attr_list, FALSE);
300         gtk_widget_hide(custom_attr_window.window);
301         gtk_window_set_modal(GTK_WINDOW(custom_attr_window.window), FALSE);
302         if (dirty && !prefs_common.addressbook_use_editaddress_dialog)
303                 addressbook_edit_reload_attr_list();
304 }
305
306 static void custom_attr_window_cancel_cb(GtkWidget *widget,
307                                  gpointer data) 
308 {
309         dirty = FALSE;
310         custom_attr_window_close();
311 }
312
313 static void custom_attr_window_ok_cb(GtkWidget *widget,
314                                  gpointer data) 
315 {
316         custom_attr_window_close();
317 }
318
319 static void custom_attr_selected_attr_edited(GtkCellRendererText *widget,
320                 gchar *path, gchar *new_text,
321                 GtkWidget *list_view)
322 {
323         GtkTreeIter iter;
324         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list_view));
325
326         if (!gtk_tree_model_get_iter_from_string(model, &iter, path))
327                 return;
328
329         if (!new_text || !*new_text)
330                 return;
331
332         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
333                            CUSTOM_ATTR_NAME, new_text,
334                            -1);
335         dirty = TRUE;
336 }
337
338 typedef struct FindAttrInStore {
339         gchar           *attr;
340         GtkTreePath     *path;
341         GtkTreeIter      iter;
342 } FindAttrInStore;
343
344 static gboolean find_attr_in_store(GtkTreeModel *model,
345                                       GtkTreePath  *path,
346                                       GtkTreeIter  *iter,
347                                       FindAttrInStore *data)
348 {
349         gchar *attr;
350         gtk_tree_model_get(model, iter, CUSTOM_ATTR_NAME, &attr, -1);
351
352         if (g_utf8_collate(data->attr, attr)==0) {
353                 data->path = path; /* signal we found it */
354                 data->iter = *iter;
355                 g_free(attr);
356                 return TRUE;
357         }
358         g_free(attr);
359         return FALSE; 
360 }
361
362 static void custom_attr_window_add_attr(void)
363 {
364         gchar *new_attr = gtk_editable_get_chars(GTK_EDITABLE(custom_attr_window.add_entry),
365                                                                 0, -1);
366         g_strstrip(new_attr);
367         if (new_attr && *new_attr) {
368                 GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
369                                                 (GTK_TREE_VIEW(custom_attr_window.attr_list)));
370                 FindAttrInStore fis;
371
372                 fis.attr = new_attr;
373                 fis.path = NULL;
374                 gtk_tree_model_foreach(gtk_tree_view_get_model
375                                 (GTK_TREE_VIEW(custom_attr_window.attr_list)), 
376                                 (GtkTreeModelForeachFunc) find_attr_in_store,
377                                 &fis);
378
379                 if (fis.path) {
380                         /* activate existing one */
381                         GtkTreeSelection *selection;
382                         GtkTreePath* path;
383                         GtkTreeModel *model = gtk_tree_view_get_model(
384                                 GTK_TREE_VIEW(custom_attr_window.attr_list));
385
386                         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(custom_attr_window.attr_list));
387                         gtk_tree_selection_select_iter(selection, &fis.iter);
388
389                         path = gtk_tree_model_get_path(model, &fis.iter);
390                         /* XXX returned path may not be valid??? create new one to be sure */ 
391                         gtk_tree_view_set_cursor(GTK_TREE_VIEW(custom_attr_window.attr_list),
392                                                 path, NULL, FALSE);
393
394                         gtk_list_store_set(list_store, &fis.iter,
395                                                 CUSTOM_ATTR_NAME, new_attr,
396                                                 -1);
397
398                         gtk_tree_path_free(path);
399                 } else {
400                         /* append new */
401                         GtkTreeIter iter;
402
403                         gtk_list_store_append(list_store, &iter);
404                         gtk_list_store_set(list_store, &iter,
405                                            CUSTOM_ATTR_NAME, new_attr,
406                                            -1);
407                 }
408                 dirty = TRUE;
409         } else {
410                 alertpanel_error(_("Attribute name is not set."));
411         }
412         g_free(new_attr);
413 }
414
415 static void custom_attr_window_add_attr_cb(GtkWidget *widget,
416                                  gpointer data) 
417 {
418         custom_attr_window_add_attr();
419         gtk_entry_set_text(GTK_ENTRY(custom_attr_window.add_entry), "");
420         gtk_widget_grab_focus(custom_attr_window.attr_list);
421 }
422
423 static void custom_attr_window_del_attr_cb(GtkWidget *widget,
424                                  gpointer data) 
425 {
426         custom_attr_popup_delete(NULL, NULL);
427         gtk_widget_grab_focus(custom_attr_window.attr_list);
428 }
429
430 static gboolean custom_attr_window_key_pressed(GtkWidget *widget,
431                                                                                            GdkEventKey *event, gpointer data)
432 {
433         if (event && event->keyval == GDK_KEY_Escape)
434                 custom_attr_window_close();
435         else if (event && event->keyval == GDK_KEY_Delete)
436                 custom_attr_popup_delete(NULL, NULL);
437         return FALSE;
438 }
439
440 static gboolean custom_attr_window_add_key_pressed(GtkWidget *widget,
441                                                                                                    GdkEventKey *event, gpointer data)
442 {
443         if (event && (event->keyval == GDK_KEY_KP_Enter || event->keyval == GDK_KEY_Return)) {
444                 custom_attr_window_add_attr();
445                 gtk_entry_set_text(GTK_ENTRY(custom_attr_window.add_entry), "");
446                 gtk_widget_grab_focus(custom_attr_window.attr_list);
447         }
448         return FALSE;
449 }
450
451 static void custom_attr_window_create(void) 
452 {
453         GtkWidget *window;
454         GtkWidget *hbox1;
455         GtkWidget *hbox2;
456         GtkWidget *vbox1;
457         GtkWidget *label;
458         GtkWidget *attr_list;
459         GtkWidget *cancel_btn;
460         GtkWidget *ok_btn;
461         GtkWidget *scrolledwin;
462         GtkWidget *new_attr_label;
463         GtkWidget *new_attr_entry;
464         GtkWidget *add_btn;
465         GtkWidget *del_btn;
466
467         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "custom_attr_edit_window");
468         gtk_window_set_title (GTK_WINDOW(window),
469                               C_("Dialog title", "Edit attribute names"));
470
471         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
472         gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
473         gtk_window_set_resizable(GTK_WINDOW (window), TRUE);
474         g_signal_connect(G_OBJECT(window), "delete_event",
475                          G_CALLBACK(custom_attr_window_cancel_cb), NULL);
476         g_signal_connect(G_OBJECT(window), "key_press_event",
477                          G_CALLBACK(custom_attr_window_key_pressed), NULL);
478         MANAGE_WINDOW_SIGNALS_CONNECT (window);
479
480         vbox1 = gtk_vbox_new(FALSE, 6);
481         hbox1 = gtk_hbox_new(FALSE, 6);
482         
483         new_attr_label = gtk_label_new(_("New attribute name:"));
484         gtk_misc_set_alignment(GTK_MISC(new_attr_label), 0, 0.5);
485         gtk_box_pack_start(GTK_BOX(hbox1), new_attr_label, FALSE, FALSE, 0);
486         
487         new_attr_entry = gtk_entry_new();
488         gtk_box_pack_start(GTK_BOX(hbox1), new_attr_entry, FALSE, FALSE, 0);
489         g_signal_connect(G_OBJECT(new_attr_entry), "key_press_event",
490                          G_CALLBACK(custom_attr_window_add_key_pressed), NULL);
491         
492         add_btn = gtk_button_new_from_stock(GTK_STOCK_ADD);
493         gtk_box_pack_start(GTK_BOX(hbox1), add_btn, FALSE, FALSE, 0);
494         
495         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
496         gtk_box_pack_start(GTK_BOX(hbox1), del_btn, FALSE, FALSE, 0);
497         
498         gtkut_stock_button_set_create(&hbox2, &cancel_btn, GTK_STOCK_CANCEL,
499                                       &ok_btn, GTK_STOCK_OK,
500                                       NULL, NULL);
501
502         gtk_widget_show(new_attr_label);
503         gtk_widget_show(new_attr_entry);
504         gtk_widget_show(add_btn);
505         gtk_widget_show(del_btn);
506         gtk_widget_show(cancel_btn);
507         gtk_widget_show(ok_btn);
508
509         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
510                          G_CALLBACK(custom_attr_window_cancel_cb), NULL);
511         g_signal_connect(G_OBJECT(ok_btn), "clicked",
512                          G_CALLBACK(custom_attr_window_ok_cb), NULL);
513         g_signal_connect(G_OBJECT(add_btn), "clicked",
514                          G_CALLBACK(custom_attr_window_add_attr_cb), NULL);
515         g_signal_connect(G_OBJECT(del_btn), "clicked",
516                          G_CALLBACK(custom_attr_window_del_attr_cb), NULL);
517
518         attr_list = custom_attr_window_list_view_create();
519         
520         label = gtk_label_new(_("Adding or removing attribute names won't "
521                                 "affect attributes already set for contacts."));
522         gtk_widget_set_size_request(GTK_WIDGET(label), 380, -1);
523         gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
524         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
525         gtk_box_pack_start(GTK_BOX(vbox1), label, FALSE, TRUE, 0);
526         
527         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
528         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
529                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
530                                        
531         gtk_widget_set_size_request(scrolledwin, 400, 250);
532
533         gtk_container_add(GTK_CONTAINER(scrolledwin), attr_list);
534         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwin, TRUE, TRUE, 0);
535         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 0);
536         gtk_box_pack_start(GTK_BOX(vbox1), hbox2, FALSE, FALSE, 0);
537         
538         gtk_widget_show(label);
539         gtk_widget_show(scrolledwin);
540         gtk_widget_show(attr_list);
541         gtk_widget_show(hbox2);
542         gtk_widget_show(hbox1);
543         gtk_widget_show(vbox1);
544         gtk_container_add(GTK_CONTAINER (window), vbox1);
545
546         custom_attr_window.window = window;
547         custom_attr_window.hbox1 = hbox1;
548         custom_attr_window.hbox2 = hbox2;
549         custom_attr_window.vbox1 = vbox1;
550         custom_attr_window.label = label;
551         custom_attr_window.attr_list = attr_list;
552         custom_attr_window.cancel_btn = cancel_btn;
553         custom_attr_window.ok_btn = ok_btn;
554         custom_attr_window.add_btn = add_btn;
555         custom_attr_window.add_entry = new_attr_entry;
556 }
557
558 static void custom_attr_window_load_list (GList *list) 
559 {
560         /* copy attribute names list from prefs to store */
561         GList *cur;
562         GtkTreeIter iter;
563         GtkListStore *list_store = GTK_LIST_STORE(gtk_tree_view_get_model
564                                         (GTK_TREE_VIEW(custom_attr_window.attr_list)));
565
566         custom_attr_window_list_view_clear_list(custom_attr_window.attr_list, FALSE);
567         
568         cur = list;
569         while (cur) {
570                 gtk_list_store_append(list_store, &iter);
571                 gtk_list_store_set(list_store, &iter,
572                                    CUSTOM_ATTR_NAME, cur->data,
573                                    -1);
574                 cur = cur->next;
575         }
576 }
577
578 static GList *store_to_glist = NULL;
579
580 static gboolean custom_attr_store_to_glist (GtkTreeModel *model,
581                                       GtkTreePath  *path,
582                                       GtkTreeIter  *iter,
583                                       gpointer     *data)
584 {
585         gchar *attr;
586
587         gtk_tree_model_get(model, iter, CUSTOM_ATTR_NAME, &attr, -1);
588         if (attr) {
589                 store_to_glist = g_list_prepend(store_to_glist, g_strdup(attr));
590                 g_free(attr);
591         }
592         return FALSE;
593 }
594
595 static void custom_attr_window_save_list (void) 
596 {
597         GList *cur;
598
599         /* clear existing attribute names list in prefs */
600         cur = prefs_common.addressbook_custom_attributes;
601         while (cur) {
602                 g_free(cur->data);
603                 cur = cur->next;
604         }
605         g_list_free(prefs_common.addressbook_custom_attributes);
606
607         /* copy attribute names list from store to prefs */
608         gtk_tree_model_foreach(gtk_tree_view_get_model
609                         (GTK_TREE_VIEW(custom_attr_window.attr_list)), 
610                         (GtkTreeModelForeachFunc) custom_attr_store_to_glist,
611                     NULL);
612         prefs_common.addressbook_custom_attributes = g_list_reverse(store_to_glist);
613         store_to_glist = NULL;
614 }
615
616 static GList *custom_attr_default_list(void)
617 {
618         /* returned GList must be deallocated by caller */
619         GList *list;
620         gint i;
621         i = 0;
622
623         list = NULL;
624         while (default_addressbook_attributes_table[i]) {
625                 list = g_list_prepend(
626                         list, g_strdup(gettext(default_addressbook_attributes_table[i])));
627                 i++;
628         }
629         list = g_list_reverse(list);
630         return list;
631 }
632
633 GList *addressbook_update_custom_attr_from_prefs(void)
634 {
635         /* load addressbook custom attribute names list from file */
636         /* use a list of default attribute names if storage file doesn't exist */
637         GList *list;
638         GList *default_attr_list;
639         GList *cur;
640
641         /* load table into glist */
642         default_attr_list = custom_attr_default_list();
643
644         list =  prefs_common_read_history_from_dir_with_defaults(ADDRBOOK_DIR,
645                                 ADDRESSBOOK_CUSTOM_ATTRIBUTES,
646                                 default_attr_list);
647
648         /* free glist if it's the one we return (the default one) */
649         if (list != default_attr_list) {
650                 cur = default_attr_list;
651                 while (cur) {
652                         g_free(cur->data);
653                         cur = cur->next;
654                 }
655                 g_list_free(default_attr_list);
656         }
657         return list;
658 }