675aa7ca0e07f442b577a2a3b58cf06b1880bd7f
[claws.git] / src / editaddress.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2016 Hiroyuki Yamamoto 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 #include "claws-features.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtk.h>
30
31 #include "alertpanel.h"
32 #include "stock_pixmap.h"
33 #include "mgutils.h"
34 #include "addressbook.h"
35 #include "addressitem.h"
36 #include "addritem.h"
37 #include "addrbook.h"
38 #include "manage_window.h"
39 #include "gtkutils.h"
40 #include "filesel.h"
41 #include "codeconv.h"
42 #include "editaddress.h"
43 #include "editaddress_other_attributes_ldap.h"
44 #include "prefs_common.h"
45 #include "menu.h"
46 #include "combobox.h"
47 #include "file-utils.h"
48
49 /* transient data */
50 static struct _PersonEdit_dlg personeditdlg;
51 static AddressBookFile *current_abf = NULL;
52 static ItemPerson *current_person = NULL;
53 static ItemFolder *current_parent_folder = NULL;
54 static EditAddressPostUpdateCallback edit_person_close_post_update_cb = NULL;
55
56 typedef enum {
57         EMAIL_COL_EMAIL,
58         EMAIL_COL_ALIAS,
59         EMAIL_COL_REMARKS,
60         EMAIL_COL_PTR
61 } PersonEditEMailColumnPos;
62
63 #define EDITPERSON_WIDTH      520
64 #define EDITPERSON_HEIGHT     320
65
66 #ifndef GENERIC_UMPC
67 # define EMAIL_N_COLS          4
68 # define EMAIL_COL_WIDTH_EMAIL 180
69 # define EMAIL_COL_WIDTH_ALIAS 80
70 #else
71 # define EMAIL_N_COLS          2
72 # define EMAIL_COL_WIDTH_EMAIL 130
73 # define EMAIL_COL_WIDTH_ALIAS 130
74 #endif
75
76 #ifndef GENERIC_UMPC
77 # define ATTRIB_COL_WIDTH_NAME  240
78 # define ATTRIB_COL_WIDTH_VALUE 0
79 #else
80 # define ATTRIB_COL_WIDTH_NAME  120
81 # define ATTRIB_COL_WIDTH_VALUE 120
82 #endif
83
84 #define PAGE_BASIC             0
85 #define PAGE_EMAIL             1
86 #define PAGE_ATTRIBUTES        2
87
88 static gboolean addressbook_edit_person_close( gboolean cancelled );
89 static GList *edit_person_build_email_list();
90 static GList *edit_person_build_attrib_list();
91
92 static gchar* edit_person_get_common_name_from_widgets(void)
93 {
94         gchar *cn = NULL; /* cn must be freed by caller */
95
96 #ifndef GENERIC_UMPC
97         {
98                 cn = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_name), 0, -1 );
99                 if ( cn == NULL || *cn == '\0' ) {
100                         gchar *first = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_first), 0, -1 );
101                         gchar *last = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_last), 0, -1 );
102                         cn = g_strdup_printf("%s%s%s", first?first:"", (first && last && *first && *last)?" ":"", last?last:"");
103                         g_free(first);
104                         g_free(last);
105                 }
106                 if ( cn == NULL || *cn == '\0' ) {
107                         g_free(cn);
108                         cn = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_nick), 0, -1 );;
109                 }
110         }
111 #else
112         {
113                 gchar *first = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_first), 0, -1 );
114                 gchar *last = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_last), 0, -1 );
115                 cn = g_strdup_printf("%s%s%s", first?first:"", (first && last && *first && *last)?" ":"", last?last:"");
116                 g_free(first);
117                 g_free(last);
118         }
119 #endif
120         if ( cn != NULL )
121                 g_strstrip(cn);
122         return cn;
123 }
124
125 static void edit_person_status_show( gchar *msg ) {
126         if( personeditdlg.statusbar != NULL ) {
127                 gtk_statusbar_pop( GTK_STATUSBAR(personeditdlg.statusbar), personeditdlg.status_cid );
128                 if( msg ) {
129                         gtk_statusbar_push( GTK_STATUSBAR(personeditdlg.statusbar), personeditdlg.status_cid, msg );
130                 }
131         }
132 }
133
134 static void edit_person_cancel(GtkWidget *widget, gboolean *cancelled) {
135         *cancelled = TRUE;
136         if (prefs_common.addressbook_use_editaddress_dialog)
137                 gtk_main_quit();
138         else
139                 addressbook_edit_person_close( *cancelled );
140 }
141
142 static void edit_person_ok(GtkWidget *widget, gboolean *cancelled) {
143         GList *listEMail = edit_person_build_email_list();
144         GList *listAttrib = edit_person_build_attrib_list();
145         gchar *cn = edit_person_get_common_name_from_widgets();
146
147         if( (cn == NULL || *cn == '\0') && listEMail == NULL && listAttrib == NULL ) {
148                 gint val;
149
150                 val = alertpanel( _("Add New Person"),
151 #ifndef GENERIC_UMPC
152                                 _("Adding a new person requires at least one of the\n"
153                                   "following values to be set:\n"
154                                   " - Display Name\n"
155                                   " - First Name\n"
156                                   " - Last Name\n"
157                                   " - Nickname\n"
158                                   " - any email address\n"
159                                   " - any additional attribute\n\n"
160                                   "Click OK to keep editing this contact.\n"
161                                   "Click Cancel to close without saving."),
162 #else
163                                 _("Adding a new person requires at least one of the\n"
164                                   "following values to be set:\n"
165                                   " - First Name\n"
166                                   " - Last Name\n"
167                                   " - any email address\n"
168                                   " - any additional attribute\n\n"
169                                   "Click OK to keep editing this contact.\n"
170                                   "Click Cancel to close without saving."),
171 #endif
172                                 GTK_STOCK_CANCEL, GTK_STOCK_OK, NULL, ALERTFOCUS_SECOND );
173                 if( val == G_ALERTDEFAULT ) {
174                         edit_person_cancel(widget, cancelled);
175                 }
176                 g_free( cn );
177                 return;
178         }
179         g_free( cn );
180
181         *cancelled = FALSE;
182         if (prefs_common.addressbook_use_editaddress_dialog)
183                 gtk_main_quit();
184         else
185                 addressbook_edit_person_close( *cancelled );
186 }
187
188 /* Updated up/down buttons sensitivity, depending on list
189  * cursor position */
190 static void edit_person_email_update_buttons()
191 {
192         GtkTreeModel *model;
193         GtkTreeIter iter, otheriter;
194         GtkTreePath *path;
195         gboolean has_prev;
196         ItemEMail *email;
197
198         email = gtkut_tree_view_get_selected_pointer(
199                         GTK_TREE_VIEW(personeditdlg.view_email), EMAIL_COL_PTR,
200                         &model, NULL, &iter);
201
202         if (email) {
203                 if (!personeditdlg.read_only) {
204                         otheriter = iter;
205                         path = gtk_tree_model_get_path(model, &otheriter);
206                         has_prev = gtk_tree_path_prev(path);
207                         if (has_prev) {
208                                 gtk_tree_model_get_iter(model, &otheriter, path);
209                         }
210                         gtk_widget_set_sensitive(personeditdlg.email_up, has_prev);
211
212                         otheriter = iter;
213                         gtk_widget_set_sensitive(personeditdlg.email_down,
214                                         gtk_tree_model_iter_next(model, &otheriter));
215                 }
216         } else {
217                 gtk_widget_set_sensitive(personeditdlg.email_del, FALSE);
218                 gtk_widget_set_sensitive(personeditdlg.email_up, FALSE);
219                 gtk_widget_set_sensitive(personeditdlg.email_down, FALSE);
220         }
221 }
222
223 static void edit_person_email_cursor_changed(GtkTreeView *view,
224                 gpointer user_data)
225 {
226         GtkTreeModel *model;
227         GtkTreeIter iter;
228         ItemEMail *email;
229
230         email = gtkut_tree_view_get_selected_pointer(
231                         GTK_TREE_VIEW(personeditdlg.view_email), EMAIL_COL_PTR,
232                         &model, NULL, &iter);
233
234         if (email != NULL) {
235                 if (email->address != NULL)
236                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_email),
237                                         email->address);
238                 if (ADDRITEM_NAME(email) != NULL)
239                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_alias),
240                                         ADDRITEM_NAME(email));
241                 if (email->remarks != NULL)
242                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_remarks),
243                                         email->remarks);
244         }
245
246         edit_person_email_update_buttons();
247 }
248
249
250 static gint edit_person_delete_event(GtkWidget *widget, GdkEventAny *event, gboolean *cancelled) {
251         *cancelled = TRUE;
252         if (prefs_common.addressbook_use_editaddress_dialog)
253                 gtk_main_quit();
254         else
255                 addressbook_edit_person_close( *cancelled );
256         return TRUE;
257 }
258
259 static gboolean edit_person_key_pressed(GtkWidget *widget, GdkEventKey *event, gboolean *cancelled) {
260         if (prefs_common.addressbook_use_editaddress_dialog) {
261         if (event && event->keyval == GDK_KEY_Escape) {
262                 *cancelled = TRUE;
263                 gtk_main_quit();
264         }
265         }
266         return FALSE;
267 }
268
269 static gchar *_title_new_ = NULL;
270 static gchar *_title_edit_ = NULL;
271
272 static void edit_person_set_widgets_title( gchar *text )
273 {
274         gchar *label = NULL;
275
276         cm_return_if_fail( text != NULL );
277
278         gtk_label_set_text(GTK_LABEL(personeditdlg.title), "");
279         label = g_markup_printf_escaped("<b>%s</b>", text);
280         gtk_label_set_markup(GTK_LABEL(personeditdlg.title), label);
281         g_free(label);
282 }
283
284 static void edit_person_set_window_title( gint pageNum ) {
285         gchar *sTitle;
286
287         if( _title_new_ == NULL ) {
288                 _title_new_ = g_strdup( _("Add New Person") );
289                 _title_edit_ = g_strdup( _("Edit Person Details") );
290         }
291
292         if( pageNum == PAGE_BASIC ) {
293                 if( personeditdlg.editNew ) {
294                         if (prefs_common.addressbook_use_editaddress_dialog)
295                                 gtk_window_set_title( GTK_WINDOW(personeditdlg.container), _title_new_ );
296                         else
297                                 edit_person_set_widgets_title( _title_new_ );
298                 }
299                 else {
300                         if (prefs_common.addressbook_use_editaddress_dialog)
301                                 gtk_window_set_title( GTK_WINDOW(personeditdlg.container), _title_edit_ );
302                         else
303                                 edit_person_set_widgets_title( _title_edit_ );
304                 }
305         }
306         else {
307                 if( personeditdlg.entry_name == NULL ) {
308                         sTitle = g_strdup( _title_edit_ );
309                 }
310                 else {
311                         gchar *name;
312                         name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_name), 0, -1 );
313                         g_strstrip(name);
314                         if ( *name != '\0' )
315                                 sTitle = g_strdup_printf( "%s - %s", _title_edit_, name );
316                         else
317                                 sTitle = g_strdup( _title_edit_ );
318                         g_free( name );
319                 }
320                 if (prefs_common.addressbook_use_editaddress_dialog)
321                         gtk_window_set_title( GTK_WINDOW(personeditdlg.container), sTitle );
322                 else
323                         edit_person_set_widgets_title( sTitle );
324                 g_free( sTitle );
325         }
326 }
327
328 static void edit_person_email_clear( gpointer data ) {
329         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_email), "" );
330         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_alias), "" );
331         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_remarks), "" );
332 }
333
334 static void edit_person_attrib_clear( gpointer data ) {
335         if (!personeditdlg.ldap) {
336                 gtk_entry_set_text( GTK_ENTRY(gtk_bin_get_child(GTK_BIN((personeditdlg.entry_atname)))), "" );
337                 gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_atvalue), "" );
338         }
339 }
340
341 static void edit_person_switch_page( GtkNotebook *notebook, gpointer page,
342                                         gint pageNum, gpointer user_data)
343 {
344         edit_person_set_window_title( pageNum );
345         edit_person_status_show( "" );
346 }
347
348 /*
349 * Load clist with a copy of person's email addresses.
350 */
351 static void edit_person_load_email( ItemPerson *person ) {
352         GList *node = person->listEMail;
353         GtkTreeModel *model = gtk_tree_view_get_model(
354                         GTK_TREE_VIEW(personeditdlg.view_email));
355         GtkTreeIter iter;
356
357         while( node ) {
358                 ItemEMail *emorig = ( ItemEMail * ) node->data;
359                 ItemEMail *email = addritem_copyfull_item_email( emorig );
360
361                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
362                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
363                                 EMAIL_COL_EMAIL, email->address,
364 #ifndef GENERIC_UMPC
365                                 EMAIL_COL_ALIAS, email->obj.name,
366                                 EMAIL_COL_REMARKS, email->remarks,
367 #endif
368                                 EMAIL_COL_PTR, email,
369                                 -1);
370
371                 node = g_list_next( node );
372         }
373 }
374
375 static void edit_person_email_move_up( gpointer data ) {
376         GtkTreeModel *model;
377         GtkTreeIter iter, otheriter;
378         GtkTreePath *path;
379         ItemEMail *email;
380         gboolean has_prev;
381
382         edit_person_email_clear( NULL );
383         edit_person_status_show( NULL );
384
385         email = gtkut_tree_view_get_selected_pointer(
386                         GTK_TREE_VIEW(personeditdlg.view_email), EMAIL_COL_PTR,
387                         &model, NULL, &iter);
388
389         if( email ) {
390                 otheriter = iter;
391                 /* GTK+2 does not have gtk_tree_model_iter_previous(), so
392                  * we have to go through GtkTreePath */
393                 path = gtk_tree_model_get_path(model, &otheriter);
394                 has_prev = gtk_tree_path_prev(path);
395                 if (has_prev) {
396                         gtk_tree_model_get_iter(model, &otheriter, path);
397                         gtk_list_store_move_before(GTK_LIST_STORE(model), &iter, &otheriter);
398                 }
399                 gtk_tree_path_free(path);
400         }
401
402         edit_person_email_update_buttons();
403 }
404
405 static void edit_person_email_move_down( gpointer data ) {
406         GtkTreeModel *model;
407         GtkTreeIter iter, otheriter;
408         ItemEMail *email;
409
410         edit_person_email_clear( NULL );
411         edit_person_status_show( NULL );
412
413         email = gtkut_tree_view_get_selected_pointer(
414                         GTK_TREE_VIEW(personeditdlg.view_email), EMAIL_COL_PTR,
415                         &model, NULL, &iter);
416
417         if( email ) {
418                 otheriter = iter;
419                 if (gtk_tree_model_iter_next(model, &otheriter)) {
420                         gtk_list_store_move_after(GTK_LIST_STORE(model), &iter, &otheriter);
421                 }
422         }
423
424         edit_person_email_update_buttons();
425 }
426
427 static void edit_person_attrib_cursor_changed(GtkTreeView *view,
428                 gpointer user_data)
429 {
430         UserAttribute *attrib = gtkut_tree_view_get_selected_pointer(
431                         view, ATTRIB_COL_PTR, NULL, NULL, NULL);
432
433         if( attrib && !personeditdlg.read_only && !personeditdlg.ldap ) {
434                 gtk_entry_set_text( GTK_ENTRY(gtk_bin_get_child(GTK_BIN((personeditdlg.entry_atname))) ), attrib->name );
435                 gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_atvalue), attrib->value );
436                 gtk_widget_set_sensitive(personeditdlg.attrib_del, TRUE);
437         } else {
438                 gtk_widget_set_sensitive(personeditdlg.attrib_del, FALSE);
439         }
440         edit_person_status_show( NULL );
441 }
442
443
444 static void edit_person_email_delete( gpointer data ) {
445         GtkTreeModel *model;
446         GtkTreeIter iter;
447         GtkTreeSelection *sel;
448         ItemEMail *email;
449         gboolean has_row = FALSE;
450         gint n;
451
452         edit_person_email_clear( NULL );
453         edit_person_status_show( NULL );
454
455         email = gtkut_tree_view_get_selected_pointer(
456                         GTK_TREE_VIEW(personeditdlg.view_email), EMAIL_COL_PTR,
457                         &model, &sel, &iter);
458
459         if( email ) {
460                 /* Remove list entry and set iter to next row, if any */
461                 has_row = gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
462                 addritem_free_item_email( email );
463                 email = NULL;
464         }
465
466         /* Position hilite bar */
467         if (!has_row) {
468                 /* The removed row was the last in the list, so iter is not
469                  * valid. Find out if there is at least one row remaining
470                  * in the list, and select the last one if so. */
471                 n = gtk_tree_model_iter_n_children(model, NULL);
472                 if (n > 0 && gtk_tree_model_iter_nth_child(model, &iter, NULL, n-1)) {
473                         /* It exists */
474                         has_row = TRUE;
475                 }
476         }
477
478         if (has_row)
479                 gtk_tree_selection_select_iter(sel, &iter);
480
481         edit_person_email_cursor_changed(
482                         GTK_TREE_VIEW(personeditdlg.view_email), NULL);
483 }
484
485 static ItemEMail *edit_person_email_edit( gboolean *error, ItemEMail *email ) {
486         ItemEMail *retVal = NULL;
487         gchar *sEmail, *sAlias, *sRemarks, *sEmail_;
488
489         *error = TRUE;
490         sEmail_ = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_email), 0, -1 );
491         sAlias = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_alias), 0, -1 );
492         sRemarks = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_remarks), 0, -1 );
493         sEmail = mgu_email_check_empty( sEmail_ );
494         g_free( sEmail_ );
495
496         if( sEmail ) {
497                 if( email == NULL ) {
498                         email = addritem_create_item_email();
499                 }
500                 addritem_email_set_address( email, sEmail );
501                 addritem_email_set_alias( email, sAlias );
502                 addritem_email_set_remarks( email, sRemarks );
503                 retVal = email;
504                 *error = FALSE;
505         }
506         else {
507                 edit_person_status_show( _( "An Email address must be supplied." ) );
508         }
509
510         g_free( sEmail );
511         g_free( sAlias );
512         g_free( sRemarks );
513
514         return retVal;
515 }
516
517 static void edit_person_email_modify( gpointer data ) {
518         GtkTreeModel *model;
519         GtkTreeIter iter;
520         gboolean errFlg = FALSE;
521         ItemEMail *email;
522
523         email = gtkut_tree_view_get_selected_pointer(
524                         GTK_TREE_VIEW(personeditdlg.view_email), EMAIL_COL_PTR,
525                         &model, NULL, &iter);
526
527         if( email ) {
528                 edit_person_email_edit( &errFlg, email );
529                 if( ! errFlg ) {
530                         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
531                                         EMAIL_COL_EMAIL, email->address,
532 #ifndef GENERIC_UMPC
533                                         EMAIL_COL_ALIAS, email->obj.name,
534                                         EMAIL_COL_REMARKS, email->remarks,
535 #endif
536                                         -1);
537                         edit_person_email_clear( NULL );
538                 }
539         }
540 }
541
542 static void edit_person_email_add( gpointer data ) {
543         GtkTreeModel *model;
544         GtkTreeIter iter, otheriter;
545         GtkTreeSelection *sel;
546         gboolean errFlg = FALSE, prev_exists = FALSE;
547         ItemEMail *email = NULL;
548
549         email = gtkut_tree_view_get_selected_pointer(
550                         GTK_TREE_VIEW(personeditdlg.view_email), EMAIL_COL_PTR,
551                         &model, &sel, &otheriter);
552         if (email != NULL)
553                 prev_exists = TRUE;
554
555         email = edit_person_email_edit( &errFlg, NULL );
556         if( ! errFlg ) {
557                 if (prev_exists)
558                         gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter, &otheriter);
559                 else /* list is empty */
560                         gtk_list_store_append(GTK_LIST_STORE(model), &iter);
561
562                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
563                                         EMAIL_COL_EMAIL, email->address,
564 #ifndef GENERIC_UMPC
565                                         EMAIL_COL_ALIAS, email->obj.name,
566                                         EMAIL_COL_REMARKS, email->remarks,
567 #endif
568                                         EMAIL_COL_PTR, email,
569                                         -1);
570                 gtk_tree_selection_select_iter(sel, &iter);
571                 edit_person_email_update_buttons();
572
573                 edit_person_email_clear( NULL );
574         }
575 }
576
577 static gboolean list_find_email(const gchar *addr)
578 {
579         GtkWidget *view = personeditdlg.view_email;
580         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
581         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
582         GtkTreeIter iter;
583         ItemEMail *email;
584
585         if (!gtk_tree_model_get_iter_first(model, &iter))
586                 return FALSE;
587
588         do {
589                 gtk_tree_model_get(model, &iter, EMAIL_COL_PTR, &email, -1);
590                 if (!g_ascii_strcasecmp(email->address, addr)) {
591                         gtk_tree_selection_select_iter(sel, &iter);
592                         return TRUE;
593                 }
594         } while (gtk_tree_model_iter_next(model, &iter));
595
596         return FALSE;
597 }
598
599 static gboolean list_find_attribute(const gchar *attr)
600 {
601         GtkWidget *view = personeditdlg.view_attrib;
602         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
603         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
604         GtkTreeIter iter;
605         UserAttribute *attrib;
606
607         if (!gtk_tree_model_get_iter_first(model, &iter))
608                 return FALSE;
609
610         do {
611                 gtk_tree_model_get(model, &iter, ATTRIB_COL_PTR, &attrib, -1);
612                 if (!g_ascii_strcasecmp(attrib->name, attr)) {
613                         gtk_tree_selection_select_iter(sel, &iter);
614                         return TRUE;
615                 }
616         } while (gtk_tree_model_iter_next(model, &iter));
617
618         return FALSE;
619 }
620
621 /*
622 * Load list with a copy of person's email addresses.
623 */
624 static void edit_person_load_attrib( ItemPerson *person ) {
625         GList *node = person->listAttrib;
626         GtkWidget *view = personeditdlg.view_attrib;
627         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
628         GtkTreeIter iter;
629
630         while( node ) {
631                 UserAttribute *atorig = ( UserAttribute * ) node->data;
632                 UserAttribute *attrib = addritem_copy_attribute( atorig );
633
634                 debug_print("name: %s value: %s\n", attrib->name, attrib->value);
635
636                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
637                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
638                                 ATTRIB_COL_NAME, attrib->name,
639                                 ATTRIB_COL_VALUE, attrib->value,
640                                 ATTRIB_COL_PTR, attrib,
641                                 -1);
642
643                 node = g_list_next( node );
644         }
645 }
646
647 static void edit_person_attrib_delete(gpointer data) {
648         UserAttribute *attrib;
649         GtkTreeModel *model;
650         GtkTreeSelection *sel;
651         GtkTreeIter iter;
652         gboolean has_row = FALSE;
653         gint n;
654
655         edit_person_attrib_clear(NULL);
656         edit_person_status_show(NULL);
657
658         attrib = gtkut_tree_view_get_selected_pointer(
659                         GTK_TREE_VIEW(personeditdlg.view_attrib), ATTRIB_COL_PTR,
660                         &model, &sel, &iter);
661
662         if (attrib) {
663                 /* Remove list entry, and set iter to next row, if any */
664                 has_row = gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
665                 addritem_free_attribute(attrib);
666                 attrib = NULL;
667         }
668
669         /* Position hilite bar */
670         if (!has_row) {
671                 /* The removed row was the last in the list, so iter is not
672                  * valid. Find out if there is at least one row remaining
673                  * in the list, and select the last one if so. */
674                 n = gtk_tree_model_iter_n_children(model, NULL);
675                 if (n > 0 && gtk_tree_model_iter_nth_child(model, &iter, NULL, n-1)) {
676                         /* It exists. */
677                         has_row = TRUE;
678                 }
679         }
680
681         if (has_row)
682                 gtk_tree_selection_select_iter(sel, &iter);
683
684         edit_person_attrib_cursor_changed(
685                         GTK_TREE_VIEW(personeditdlg.view_attrib), NULL);
686 }
687
688 static UserAttribute *edit_person_attrib_edit( gboolean *error, UserAttribute *attrib ) {
689         UserAttribute *retVal = NULL;
690         gchar *sName, *sValue, *sName_, *sValue_;
691
692         *error = TRUE;
693         sName_ = gtk_editable_get_chars( GTK_EDITABLE(gtk_bin_get_child(GTK_BIN((personeditdlg.entry_atname)))), 0, -1 );
694         sValue_ = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_atvalue), 0, -1 );
695         sName = mgu_email_check_empty( sName_ );
696         sValue = mgu_email_check_empty( sValue_ );
697         g_free( sName_ );
698         g_free( sValue_ );
699
700         if( sName && sValue ) {
701                 if( attrib == NULL ) {
702                         attrib = addritem_create_attribute();
703                 }
704                 addritem_attrib_set_name( attrib, sName );
705                 addritem_attrib_set_value( attrib, sValue );
706                 retVal = attrib;
707                 *error = FALSE;
708         }
709         else {
710                 edit_person_status_show( _( "A Name and Value must be supplied." ) );
711         }
712
713         g_free( sName );
714         g_free( sValue );
715
716         return retVal;
717 }
718
719 static void edit_person_attrib_modify(gpointer data) {
720         gboolean errFlg = FALSE;
721         GtkTreeModel *model;
722         GtkTreeIter iter;
723         UserAttribute *attrib;
724
725         attrib = gtkut_tree_view_get_selected_pointer(
726                         GTK_TREE_VIEW(personeditdlg.view_attrib), ATTRIB_COL_PTR,
727                         &model, NULL, &iter);
728         if (attrib) {
729                 edit_person_attrib_edit(&errFlg, attrib);
730                 if (!errFlg) {
731                         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
732                                         ATTRIB_COL_NAME, attrib->name,
733                                         ATTRIB_COL_VALUE, attrib->value,
734                                         -1);
735                         edit_person_attrib_clear(NULL);
736                 }
737         }
738 }
739
740 static void edit_person_attrib_add(gpointer data) {
741         gboolean errFlg = FALSE;
742         GtkTreeModel *model;
743         GtkTreeSelection *sel;
744         GtkTreeIter iter, iter2;
745         UserAttribute *prev_attrib, *attrib;
746
747         attrib = edit_person_attrib_edit(&errFlg, NULL);
748         if (attrib == NULL) /* input for new attribute is not valid */
749                 return;
750
751         prev_attrib = gtkut_tree_view_get_selected_pointer(
752                         GTK_TREE_VIEW(personeditdlg.view_attrib), ATTRIB_COL_PTR,
753                         &model, &sel, &iter);
754
755         if (prev_attrib == NULL) {
756                 /* No row selected, or list empty, add new attribute as first row. */
757                 gtk_list_store_insert(GTK_LIST_STORE(model), &iter, 0);
758         } else {
759                 /* Add it after the currently selected row. */
760                 gtk_list_store_insert_after(GTK_LIST_STORE(model), &iter2,
761                                 &iter);
762                 iter = iter2;
763         }
764
765         /* Fill out the new row. */
766         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
767                         ATTRIB_COL_NAME, attrib->name,
768                         ATTRIB_COL_VALUE, attrib->value,
769                         ATTRIB_COL_PTR, attrib,
770                         -1);
771         gtk_tree_selection_select_iter(sel, &iter);
772         edit_person_attrib_clear(NULL);
773 }
774
775 /*!
776  *\brief        Save Gtk object size to prefs dataset
777  */
778 static void edit_person_size_allocate_cb(GtkWidget *widget,
779                                          GtkAllocation *allocation)
780 {
781         cm_return_if_fail(allocation != NULL);
782
783         prefs_common.addressbookeditpersonwin_width = allocation->width;
784         prefs_common.addressbookeditpersonwin_height = allocation->height;
785 }
786
787 /* build edit person widgets, return a pointer to the main container of the widgetset (a vbox) */
788 static GtkWidget* addressbook_edit_person_widgets_create( GtkWidget* container, gboolean *cancelled )
789 {
790         GtkWidget *vbox;
791         GtkWidget *vnbox;
792         GtkWidget *notebook;
793         GtkWidget *hbbox;
794         GtkWidget *ok_btn;
795         GtkWidget *cancel_btn;
796
797         vbox = gtk_vbox_new(FALSE, 4);
798          gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH); 
799         gtk_widget_show(vbox);
800         gtk_container_add(GTK_CONTAINER(container), vbox);
801
802         vnbox = gtk_vbox_new(FALSE, 4);
803         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
804         gtk_widget_show(vnbox);
805         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
806
807         /* Notebook */
808         notebook = gtk_notebook_new();
809         gtk_widget_show(notebook);
810         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
811         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
812
813         /* Button panel */
814         if (prefs_common.addressbook_use_editaddress_dialog)
815         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
816                                       &ok_btn, GTK_STOCK_OK,
817                                       NULL, NULL);
818         else
819                 gtkut_stock_with_text_button_set_create(&hbbox,
820                                           &cancel_btn, GTK_STOCK_CANCEL, _("Discard"),
821                                       &ok_btn, GTK_STOCK_OK, _("Apply"),
822                                       NULL, NULL, NULL);
823         gtk_box_pack_end(GTK_BOX(vnbox), hbbox, FALSE, FALSE, 0);
824         gtk_widget_grab_default(ok_btn);
825
826         g_signal_connect(G_OBJECT(ok_btn), "clicked",
827                          G_CALLBACK(edit_person_ok), cancelled);
828         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
829                          G_CALLBACK(edit_person_cancel), cancelled);
830         g_signal_connect(G_OBJECT(notebook), "switch_page",
831                          G_CALLBACK(edit_person_switch_page), NULL );
832
833         gtk_widget_show_all(vbox);
834
835         personeditdlg.notebook   = notebook;
836         personeditdlg.ok_btn     = ok_btn;
837         personeditdlg.cancel_btn = cancel_btn;
838
839         return vbox;
840 }
841
842 static void addressbook_edit_person_dialog_create( gboolean *cancelled ) {
843         GtkWidget *window;
844         GtkWidget *hsbox;
845         GtkWidget *vbox;
846         GtkWidget *statusbar;
847         static GdkGeometry geometry;
848
849         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editaddress");
850         /* gtk_container_set_border_width(GTK_CONTAINER(window), 0); */
851         gtk_window_set_title(GTK_WINDOW(window), _("Edit Person Data"));
852         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
853         gtk_window_set_modal(GTK_WINDOW(window), FALSE);
854         g_signal_connect(G_OBJECT(window), "delete_event",
855                          G_CALLBACK(edit_person_delete_event),
856                          cancelled);
857         g_signal_connect(G_OBJECT(window), "size_allocate",
858                          G_CALLBACK(edit_person_size_allocate_cb),
859                         cancelled);
860         g_signal_connect(G_OBJECT(window), "key_press_event",
861                          G_CALLBACK(edit_person_key_pressed),
862                          cancelled);
863
864         vbox = addressbook_edit_person_widgets_create(window, cancelled);
865
866         /* Status line */
867         hsbox = gtk_hbox_new(FALSE, 0);
868         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
869         statusbar = gtk_statusbar_new();
870         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
871
872         if (!geometry.min_height) {
873                 geometry.min_width = EDITPERSON_WIDTH;
874                 geometry.min_height = EDITPERSON_HEIGHT;
875         }
876
877         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
878                                       GDK_HINT_MIN_SIZE);
879         gtk_widget_set_size_request(window, prefs_common.addressbookeditpersonwin_width,
880                                     prefs_common.addressbookeditpersonwin_height);
881
882         personeditdlg.container  = window;
883         personeditdlg.statusbar  = statusbar;
884         personeditdlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit Person Dialog" );
885
886 }
887
888 /* parent must be a box */
889 static void addressbook_edit_person_widgetset_create( GtkWidget *parent, gboolean *cancelled )
890 {
891         GtkWidget *vbox;
892         GtkWidget *label;
893
894         if ( parent == NULL )
895                 g_warning("addressbook_edit_person_widgetset_create: parent is NULL");
896
897         vbox = gtk_vbox_new(FALSE, 0);
898         gtk_box_pack_end(GTK_BOX(parent), vbox, TRUE, TRUE, 0);
899
900         label = gtk_label_new(_("Edit Person Data"));
901         gtk_label_set_justify( GTK_LABEL(label), GTK_JUSTIFY_CENTER);
902         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
903
904         addressbook_edit_person_widgets_create(vbox, cancelled);
905
906         gtk_widget_set_size_request(vbox, EDITPERSON_WIDTH, EDITPERSON_HEIGHT);
907
908         personeditdlg.container = vbox;
909         personeditdlg.title = label;
910         personeditdlg.statusbar  = NULL;
911         personeditdlg.status_cid = 0;
912 }
913
914 void addressbook_edit_person_widgetset_hide( void )
915 {
916         if ( personeditdlg.container )
917                 gtk_widget_hide( personeditdlg.container );
918 }
919
920 static void addressbook_edit_person_set_picture(void)
921 {
922         GError *error = NULL;
923         gchar *filename;
924         int width, height, scalewidth, scaleheight;
925
926         if (personeditdlg.ldap)
927                 return;
928
929         if ( (filename = filesel_select_file_open(_("Choose a picture"), NULL)) ) {
930                 GdkPixbuf *pixbuf = NULL;
931                 gdk_pixbuf_get_file_info(filename, &width, &height);
932
933                 if ( width > 128 || height > 128 ) {
934                         if (width > height) {
935                                 scaleheight = (height * 128) / width;
936                                 scalewidth = 128;
937                         }
938                         else {
939                                 scalewidth = (width * 128) / height;
940                                 scaleheight = 128;
941                         }
942                         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, 
943                                         scalewidth, scaleheight, TRUE, &error);
944                 } else {
945                         pixbuf = gdk_pixbuf_new_from_file(filename, &error);
946                 }
947                 if (error) {
948                         alertpanel_error(_("Failed to import image: \n%s"),
949                                         error->message);
950                         g_error_free(error);
951                         error = NULL;
952                         /* keep the previous picture if any */
953                         g_free(filename);
954                         if (pixbuf)
955                                 g_object_unref(pixbuf);
956                         return;
957                 }
958                 personeditdlg.picture_set = TRUE;
959                 cm_menu_set_sensitive("EditAddressPopup/UnsetPicture", personeditdlg.picture_set);
960                 g_free(filename);
961                 gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
962                 g_object_unref(pixbuf);
963         }
964 }       
965
966 static void addressbook_edit_person_clear_picture(void)
967 {
968         GdkPixbuf *pixbuf;
969
970         stock_pixbuf_gdk(STOCK_PIXMAP_ANONYMOUS, &pixbuf);
971         personeditdlg.picture_set = FALSE;
972         cm_menu_set_sensitive("EditAddressPopup/UnsetPicture", personeditdlg.picture_set);
973         gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
974 }
975
976 static void addressbook_edit_person_set_picture_menu_cb (GtkAction *action, gpointer data)
977 {
978         addressbook_edit_person_set_picture();
979 }
980
981 static void addressbook_edit_person_unset_picture_menu_cb (GtkAction *action, gpointer data)
982 {
983         addressbook_edit_person_clear_picture();
984 }
985
986 static GtkWidget *editaddr_popup_menu = NULL;
987 static GtkActionEntry editaddr_popup_entries[] =
988 {
989         {"EditAddressPopup",                    NULL, "EditAddressPopup", NULL, NULL, NULL },
990         {"EditAddressPopup/SetPicture",         NULL, N_("_Set picture"), NULL, NULL, G_CALLBACK(addressbook_edit_person_set_picture_menu_cb) },
991         {"EditAddressPopup/UnsetPicture",       NULL, N_("_Unset picture"), NULL, NULL, G_CALLBACK(addressbook_edit_person_unset_picture_menu_cb) },
992 };
993
994 static void addressbook_edit_person_set_picture_cb(GtkWidget *widget, 
995                 GdkEventButton *event, gpointer data)
996 {       
997         if (event->button == 1) {
998                 addressbook_edit_person_set_picture();
999         } else {
1000                 gtk_menu_popup(GTK_MENU(editaddr_popup_menu), 
1001                                NULL, NULL, NULL, NULL, 
1002                                event->button, event->time);
1003         }
1004 }
1005
1006 static gboolean addressbook_edit_person_picture_popup_menu(GtkWidget *widget, gpointer data)
1007 {
1008         GdkEventButton event;
1009         
1010         event.button = 3;
1011         event.time = gtk_get_current_event_time();
1012         
1013         addressbook_edit_person_set_picture_cb(NULL, &event, data);
1014
1015         return TRUE;
1016 }
1017
1018 static void addressbook_edit_person_page_basic( gint pageNum, gchar *pageLbl ) {
1019         GtkWidget *vbox;
1020         GtkWidget *hbox;
1021         GtkWidget *table;
1022         GtkWidget *label;
1023         GtkWidget *ebox_picture;
1024         GtkWidget *frame_picture;
1025         GtkWidget *entry_name;
1026         GtkWidget *entry_fn;
1027         GtkWidget *entry_ln;
1028         GtkWidget *entry_nn;
1029         const gchar *locale;
1030         gint top = 0;
1031
1032         vbox = gtk_vbox_new( FALSE, 20 );
1033         hbox = gtk_hbox_new( FALSE, 8 );
1034
1035         gtk_widget_show( vbox );        
1036
1037         if (!editaddr_popup_menu) {
1038                 cm_menu_create_action_group("EditAddressPopup", editaddr_popup_entries,
1039                         G_N_ELEMENTS(editaddr_popup_entries), (gpointer)NULL);
1040                 MENUITEM_ADDUI("/Menus", "EditAddressPopup", "EditAddressPopup", GTK_UI_MANAGER_MENU)
1041                 MENUITEM_ADDUI("/Menus/EditAddressPopup", "SetPicture", "EditAddressPopup/SetPicture", GTK_UI_MANAGER_MENUITEM)
1042                 MENUITEM_ADDUI("/Menus/EditAddressPopup", "UnsetPicture", "EditAddressPopup/UnsetPicture", GTK_UI_MANAGER_MENUITEM)
1043
1044                 editaddr_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1045                         gtk_ui_manager_get_widget(gtkut_ui_manager(), "/Menus/EditAddressPopup")) );
1046         }
1047         /* User's picture */
1048         ebox_picture = gtk_event_box_new();
1049         frame_picture = gtk_frame_new(_("Photo"));
1050         
1051         /* Room for a photo */
1052         personeditdlg.image = gtk_image_new();
1053         addressbook_edit_person_clear_picture();
1054
1055         gtk_container_add(GTK_CONTAINER(ebox_picture), personeditdlg.image);
1056         gtk_container_add(GTK_CONTAINER(frame_picture), ebox_picture);  
1057         gtk_container_add(GTK_CONTAINER( personeditdlg.notebook ), hbox );
1058         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
1059         gtk_container_set_border_width( GTK_CONTAINER (hbox), BORDER_WIDTH );
1060
1061         label = gtk_label_new_with_mnemonic( pageLbl );
1062         gtk_widget_show( label );
1063         
1064         gtk_box_pack_start(GTK_BOX(hbox), frame_picture, TRUE, TRUE, 0);
1065         
1066         gtk_notebook_set_tab_label(
1067                 GTK_NOTEBOOK( personeditdlg.notebook ),
1068                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
1069         
1070         g_signal_connect(G_OBJECT(ebox_picture), "popup-menu",
1071                          G_CALLBACK(addressbook_edit_person_picture_popup_menu), NULL);
1072         g_signal_connect(G_OBJECT(ebox_picture), "button_press_event", 
1073                         G_CALLBACK(addressbook_edit_person_set_picture_cb), NULL);
1074
1075         table = gtk_table_new( 3, 3, FALSE);
1076
1077 #define ATTACH_ROW(text, entry) \
1078 { \
1079         label = gtk_label_new(text); \
1080         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
1081                          GTK_FILL, 0, 0, 0); \
1082         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
1083  \
1084         entry = gtk_entry_new(); \
1085         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
1086                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
1087         top++; \
1088 }
1089
1090 #define ATTACH_HIDDEN_ROW(text, entry) \
1091 { \
1092         entry = gtk_entry_new(); \
1093 }
1094
1095 #ifndef GENERIC_UMPC
1096         ATTACH_ROW(_("Display Name"), entry_name);
1097 #else
1098         ATTACH_HIDDEN_ROW(_("Display Name"), entry_name);
1099 #endif
1100         locale = conv_get_current_locale();
1101         if (locale &&
1102             (!g_ascii_strncasecmp(locale, "hu", 2) ||
1103              !g_ascii_strncasecmp(locale, "ja", 2) ||
1104              !g_ascii_strncasecmp(locale, "ko", 2) ||
1105              !g_ascii_strncasecmp(locale, "vi", 2) ||
1106              !g_ascii_strncasecmp(locale, "zh", 2))) {
1107                 ATTACH_ROW(_("Last Name"), entry_ln);
1108                 ATTACH_ROW(_("First Name"), entry_fn);
1109         } else {
1110                 ATTACH_ROW(_("First Name"), entry_fn);
1111                 ATTACH_ROW(_("Last Name"), entry_ln);
1112         }
1113 #ifndef GENERIC_UMPC
1114         ATTACH_ROW(_("Nickname"), entry_nn);
1115 #else
1116         ATTACH_HIDDEN_ROW(_("Nickname"), entry_nn);
1117 #endif
1118
1119 #undef ATTACH_ROW
1120 #undef ATTACH_HIDDEN_ROW
1121         gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
1122         gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
1123         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
1124         gtk_table_set_row_spacings(GTK_TABLE(table), 15);
1125         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1126
1127         gtk_widget_show_all(vbox);
1128         personeditdlg.entry_name  = entry_name;
1129         personeditdlg.entry_first = entry_fn;
1130         personeditdlg.entry_last  = entry_ln;
1131         personeditdlg.entry_nick  = entry_nn;
1132 }
1133
1134 static gboolean email_adding = FALSE, email_saving = FALSE;
1135
1136 static void edit_person_entry_email_changed (GtkWidget *entry, gpointer data)
1137 {
1138         GtkTreeModel *model = gtk_tree_view_get_model(
1139                         GTK_TREE_VIEW(personeditdlg.view_email));
1140         gboolean non_empty = (gtk_tree_model_iter_n_children(model, NULL) > 0);
1141
1142         if (personeditdlg.read_only)
1143                 return;
1144
1145         if (gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email)) == NULL
1146         ||  strlen(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email))) == 0) {
1147                 gtk_widget_set_sensitive(personeditdlg.email_add,FALSE);
1148                 gtk_widget_set_sensitive(personeditdlg.email_mod,FALSE);
1149                 email_adding = FALSE;
1150                 email_saving = FALSE;
1151         } else if (list_find_email(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email)))) {
1152                 gtk_widget_set_sensitive(personeditdlg.email_add,FALSE);
1153                 gtk_widget_set_sensitive(personeditdlg.email_mod,non_empty);
1154                 email_adding = FALSE;
1155                 email_saving = non_empty;
1156         } else {
1157                 gtk_widget_set_sensitive(personeditdlg.email_add,TRUE);
1158                 gtk_widget_set_sensitive(personeditdlg.email_mod,non_empty);
1159                 email_adding = TRUE;
1160                 email_saving = non_empty;
1161         }
1162 }
1163
1164 static gboolean edit_person_entry_email_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
1165 {
1166         if (event && event->keyval == GDK_KEY_Return) {
1167                 if (email_saving)
1168                         edit_person_email_modify(NULL);         
1169                 else if (email_adding)
1170                         edit_person_email_add(NULL);
1171         }
1172         return FALSE;
1173 }
1174
1175
1176 static void addressbook_edit_person_page_email( gint pageNum, gchar *pageLbl ) {
1177         GtkWidget *vbox;
1178         GtkWidget *hbox;
1179         GtkWidget *vboxl;
1180         GtkWidget *vboxb;
1181         GtkWidget *vbuttonbox;
1182         GtkWidget *buttonUp;
1183         GtkWidget *buttonDown;
1184         GtkWidget *buttonDel;
1185         GtkWidget *buttonMod;
1186         GtkWidget *buttonAdd;
1187
1188         GtkWidget *table;
1189         GtkWidget *label;
1190         GtkWidget *scrollwin;
1191         GtkWidget *view;
1192         GtkWidget *entry_email;
1193         GtkWidget *entry_alias;
1194         GtkWidget *entry_remarks;
1195         gint top;
1196         GtkListStore *store;
1197         GtkTreeViewColumn *col;
1198         GtkCellRenderer *rdr;
1199         GtkTreeSelection *sel;
1200
1201         vbox = gtk_vbox_new( FALSE, 8 );
1202         gtk_widget_show( vbox );
1203         gtk_container_add( GTK_CONTAINER( personeditdlg.notebook ), vbox );
1204         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
1205
1206         label = gtk_label_new_with_mnemonic( pageLbl );
1207         gtk_widget_show( label );
1208         gtk_notebook_set_tab_label(
1209                 GTK_NOTEBOOK( personeditdlg.notebook ),
1210                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
1211
1212         /* Split into two areas */
1213         hbox = gtk_hbox_new( FALSE, 0 );
1214         gtk_container_add( GTK_CONTAINER( vbox ), hbox );
1215
1216         /* Address list */
1217         vboxl = gtk_vbox_new( FALSE, 4 );
1218         gtk_container_add( GTK_CONTAINER( hbox ), vboxl );
1219         gtk_container_set_border_width( GTK_CONTAINER(vboxl), 4 );
1220
1221         scrollwin = gtk_scrolled_window_new( NULL, NULL );
1222         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
1223                                        GTK_POLICY_AUTOMATIC,
1224                                        GTK_POLICY_AUTOMATIC);
1225
1226         store = gtk_list_store_new(EMAIL_N_COLS,
1227                         G_TYPE_STRING,
1228 #ifndef GENERIC_UMPC
1229                         G_TYPE_STRING,
1230                         G_TYPE_STRING,
1231 #endif
1232                         G_TYPE_POINTER,
1233                         -1);
1234
1235         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1236         g_object_unref(store);
1237 #ifndef GENERIC_UMPC
1238         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
1239 #else
1240         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
1241 #endif
1242         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
1243         gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
1244
1245         rdr = gtk_cell_renderer_text_new();
1246         col = gtk_tree_view_column_new_with_attributes(_("Email Address"), rdr,
1247                         "markup", EMAIL_COL_EMAIL, NULL);
1248         gtk_tree_view_column_set_min_width(col, EMAIL_COL_WIDTH_EMAIL);
1249         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
1250
1251 #ifndef GENERIC_UMPC
1252         col = gtk_tree_view_column_new_with_attributes(_("Alias"), rdr,
1253                         "markup", EMAIL_COL_ALIAS, NULL);
1254         gtk_tree_view_column_set_min_width(col, EMAIL_COL_WIDTH_ALIAS);
1255         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
1256
1257         col = gtk_tree_view_column_new_with_attributes(_("Remarks"), rdr,
1258                         "markup", EMAIL_COL_REMARKS, NULL);
1259         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
1260 #endif
1261
1262         gtk_container_add( GTK_CONTAINER(scrollwin), view );
1263
1264         /* Data entry area */
1265         table = gtk_table_new( 4, 2, FALSE);
1266
1267 #ifndef GENERIC_UMPC
1268         gtk_container_add( GTK_CONTAINER(vboxl), scrollwin );
1269         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1270 #else
1271         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1272         gtk_container_add( GTK_CONTAINER(vboxl), scrollwin );
1273 #endif
1274         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
1275         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1276         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
1277
1278         entry_email = gtk_entry_new();
1279         entry_alias = gtk_entry_new();
1280         entry_remarks = gtk_entry_new();
1281
1282         /* First row */
1283         top = 0;
1284         label = gtk_label_new(_("Email Address"));
1285         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1286         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1287
1288         gtk_table_attach(GTK_TABLE(table), entry_email, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1289
1290 #ifndef GENERIC_UMPC
1291         /* Next row */
1292         ++top;
1293         label = gtk_label_new(_("Alias"));
1294         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1295         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1296
1297         gtk_table_attach(GTK_TABLE(table), entry_alias, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1298
1299         /* Next row */
1300         ++top;
1301         label = gtk_label_new(_("Remarks"));
1302         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1303         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1304
1305         gtk_table_attach(GTK_TABLE(table), entry_remarks, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1306 #endif
1307
1308         /* Button box */
1309         vboxb = gtk_vbox_new( FALSE, 4 );
1310         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
1311
1312         vbuttonbox = gtk_vbutton_box_new();
1313         gtk_button_box_set_layout( GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START );
1314         gtk_box_set_spacing( GTK_BOX(vbuttonbox), 8 );
1315         gtk_container_set_border_width( GTK_CONTAINER(vbuttonbox), 4 );
1316         gtk_container_add( GTK_CONTAINER(vboxb), vbuttonbox );
1317
1318         /* Buttons */
1319         buttonUp = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
1320         buttonDown = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
1321         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1322         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
1323         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
1324         
1325
1326 #ifndef GENERIC_UMPC
1327         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonUp );
1328
1329         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDown );
1330 #endif
1331         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDel );
1332
1333         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonMod );
1334
1335         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonAdd );
1336
1337         gtk_widget_show_all(vbox);
1338
1339         /* Event handlers */
1340         g_signal_connect( G_OBJECT(view), "cursor-changed",
1341                           G_CALLBACK( edit_person_email_cursor_changed ), NULL );
1342         g_signal_connect( G_OBJECT(buttonUp), "clicked",
1343                           G_CALLBACK( edit_person_email_move_up ), NULL );
1344         g_signal_connect( G_OBJECT(buttonDown), "clicked",
1345                           G_CALLBACK( edit_person_email_move_down ), NULL );
1346         g_signal_connect( G_OBJECT(buttonDel), "clicked",
1347                           G_CALLBACK( edit_person_email_delete ), NULL );
1348         g_signal_connect( G_OBJECT(buttonMod), "clicked",
1349                           G_CALLBACK( edit_person_email_modify ), NULL );
1350         g_signal_connect( G_OBJECT(buttonAdd), "clicked",
1351                           G_CALLBACK( edit_person_email_add ), NULL );
1352         g_signal_connect(G_OBJECT(entry_email), "changed",
1353                          G_CALLBACK(edit_person_entry_email_changed), NULL);
1354         g_signal_connect(G_OBJECT(entry_email), "key_press_event",
1355                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1356         g_signal_connect(G_OBJECT(entry_alias), "key_press_event",
1357                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1358         g_signal_connect(G_OBJECT(entry_remarks), "key_press_event",
1359                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1360
1361         personeditdlg.view_email   = view;
1362         personeditdlg.entry_email   = entry_email;
1363         personeditdlg.entry_alias   = entry_alias;
1364         personeditdlg.entry_remarks = entry_remarks;
1365         personeditdlg.email_up = buttonUp;
1366         personeditdlg.email_down = buttonDown;
1367         personeditdlg.email_del = buttonDel;
1368         personeditdlg.email_mod = buttonMod;
1369         personeditdlg.email_add = buttonAdd;
1370 }
1371
1372 static gboolean attrib_adding = FALSE, attrib_saving = FALSE;
1373
1374 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
1375 {
1376         GtkTreeModel *model = gtk_tree_view_get_model(
1377                         GTK_TREE_VIEW(personeditdlg.view_attrib));
1378         gboolean non_empty = (gtk_tree_model_iter_n_children(model, NULL) > 0);
1379         const gchar *atname;
1380
1381         if (personeditdlg.read_only || personeditdlg.ldap)
1382                 return;
1383
1384         atname = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN((personeditdlg.entry_atname)))));
1385         if ( atname == NULL
1386         ||  strlen(atname) == 0) {
1387                 gtk_widget_set_sensitive(personeditdlg.attrib_add,FALSE);
1388                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,FALSE);
1389                 attrib_adding = FALSE;
1390                 attrib_saving = FALSE;
1391         } else if (list_find_attribute(atname)) {
1392                 gtk_widget_set_sensitive(personeditdlg.attrib_add,FALSE);
1393                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,non_empty);
1394                 attrib_adding = FALSE;
1395                 attrib_saving = non_empty;
1396         } else {
1397                 gtk_widget_set_sensitive(personeditdlg.attrib_add,TRUE);
1398                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,non_empty);
1399                 attrib_adding = TRUE;
1400                 attrib_saving = non_empty;
1401         }
1402 }
1403
1404 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
1405 {
1406         if (event && event->keyval == GDK_KEY_Return) {
1407                 if (attrib_saving)
1408                         edit_person_attrib_modify(NULL);
1409                 else if (attrib_adding)
1410                         edit_person_attrib_add(NULL);
1411         }
1412         return FALSE;
1413 }
1414
1415 static void addressbook_edit_person_page_attrib( gint pageNum, gchar *pageLbl ) {
1416         GtkWidget *vbox;
1417         GtkWidget *hbox;
1418         GtkWidget *vboxl;
1419         GtkWidget *vboxb;
1420         GtkWidget *vbuttonbox;
1421         GtkWidget *buttonDel;
1422         GtkWidget *buttonMod;
1423         GtkWidget *buttonAdd;
1424
1425         GtkWidget *table;
1426         GtkWidget *label;
1427         GtkWidget *scrollwin;
1428         GtkWidget *view;
1429         GtkWidget *entry_name;
1430         GtkWidget *entry_value;
1431         gint top;
1432         GtkListStore *store;
1433         GtkTreeViewColumn *col;
1434         GtkCellRenderer *rdr;
1435         GtkTreeSelection *sel;
1436
1437         vbox = gtk_vbox_new( FALSE, 8 );
1438         gtk_widget_show( vbox );
1439         gtk_container_add( GTK_CONTAINER( personeditdlg.notebook ), vbox );
1440         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
1441
1442         label = gtk_label_new_with_mnemonic( pageLbl );
1443         gtk_widget_show( label );
1444         gtk_notebook_set_tab_label(
1445                 GTK_NOTEBOOK( personeditdlg.notebook ),
1446                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
1447
1448         /* Split into two areas */
1449         hbox = gtk_hbox_new( FALSE, 0 );
1450         gtk_container_add( GTK_CONTAINER( vbox ), hbox );
1451
1452         /* Attribute list */
1453         vboxl = gtk_vbox_new( FALSE, 4 );
1454         gtk_container_add( GTK_CONTAINER( hbox ), vboxl );
1455         gtk_container_set_border_width( GTK_CONTAINER(vboxl), 4 );
1456
1457         scrollwin = gtk_scrolled_window_new( NULL, NULL );
1458         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
1459                                        GTK_POLICY_AUTOMATIC,
1460                                        GTK_POLICY_AUTOMATIC);
1461
1462         store = gtk_list_store_new(ATTRIB_N_COLS,
1463                         G_TYPE_STRING, G_TYPE_STRING,
1464                         G_TYPE_POINTER, -1);
1465
1466         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1467         g_object_unref(store);
1468         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
1469         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
1470         gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
1471
1472         rdr = gtk_cell_renderer_text_new();
1473         col = gtk_tree_view_column_new_with_attributes(_("Name"), rdr,
1474                         "markup", ATTRIB_COL_NAME, NULL);
1475         gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_NAME);
1476         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
1477
1478         col = gtk_tree_view_column_new_with_attributes(_("Value"), rdr,
1479                         "markup", ATTRIB_COL_VALUE, NULL);
1480         gtk_tree_view_column_set_min_width(col, ATTRIB_COL_WIDTH_VALUE);
1481         gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
1482
1483         gtk_container_add( GTK_CONTAINER(scrollwin), view );
1484
1485         /* Data entry area */
1486 #ifndef GENERIC_UMPC
1487         table = gtk_table_new( 4, 2, FALSE);
1488         gtk_container_add( GTK_CONTAINER(vboxl), scrollwin );
1489         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1490 #else
1491         table = gtk_table_new( 2, 4, FALSE);
1492         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1493         gtk_container_add( GTK_CONTAINER(vboxl), scrollwin );
1494 #endif
1495         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
1496         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1497         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
1498
1499         /* First row */
1500         top = 0;
1501 #ifndef GENERIC_UMPC
1502         label = gtk_label_new(_("Name"));
1503         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1504         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1505
1506         entry_name = gtk_combo_box_text_new_with_entry ();
1507         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1508
1509         /* Next row */
1510         ++top;
1511         label = gtk_label_new(_("Value"));
1512         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1513         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1514
1515         entry_value = gtk_entry_new();
1516         gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1517 #else
1518         label = gtk_label_new(_("Name"));
1519         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
1520         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1521
1522         entry_name = gtk_combo_box_text_new_with_entry ();
1523         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, 0, 1, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1524
1525         /* Next row */
1526         ++top;
1527         label = gtk_label_new(_("Value"));
1528         gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
1529         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1530
1531         entry_value = gtk_entry_new();
1532         gtk_table_attach(GTK_TABLE(table), entry_value, 3, 4, 0, 1, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1533 #endif
1534         gtk_combo_box_set_active(GTK_COMBO_BOX(entry_name), -1);
1535         if (prefs_common.addressbook_custom_attributes)
1536                 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(entry_name),
1537                                 prefs_common.addressbook_custom_attributes);            
1538         /* Button box */
1539         vboxb = gtk_vbox_new( FALSE, 4 );
1540         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
1541
1542         vbuttonbox = gtk_vbutton_box_new();
1543         gtk_button_box_set_layout( GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START );
1544         gtk_box_set_spacing( GTK_BOX(vbuttonbox), 8 );
1545         gtk_container_set_border_width( GTK_CONTAINER(vbuttonbox), 4 );
1546         gtk_container_add( GTK_CONTAINER(vboxb), vbuttonbox );
1547
1548         /* Buttons */
1549         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1550         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDel );
1551
1552         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
1553         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonMod );
1554
1555         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
1556         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonAdd );
1557         
1558         gtk_widget_set_sensitive(buttonDel,FALSE);
1559         gtk_widget_set_sensitive(buttonMod,FALSE);
1560         gtk_widget_set_sensitive(buttonAdd,FALSE);
1561
1562         gtk_widget_show_all(vbox);
1563
1564         /* Event handlers */
1565         g_signal_connect( G_OBJECT(view), "cursor-changed",
1566                           G_CALLBACK( edit_person_attrib_cursor_changed ), NULL );
1567         g_signal_connect( G_OBJECT(buttonDel), "clicked",
1568                           G_CALLBACK( edit_person_attrib_delete ), NULL );
1569         g_signal_connect( G_OBJECT(buttonMod), "clicked",
1570                           G_CALLBACK( edit_person_attrib_modify ), NULL );
1571         g_signal_connect( G_OBJECT(buttonAdd), "clicked",
1572                           G_CALLBACK( edit_person_attrib_add ), NULL );
1573         g_signal_connect(G_OBJECT(entry_name), "changed",
1574                          G_CALLBACK(edit_person_entry_att_changed), NULL);
1575         g_signal_connect(G_OBJECT(entry_name), "key_press_event",
1576                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
1577         g_signal_connect(G_OBJECT(entry_value), "key_press_event",
1578                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
1579
1580         personeditdlg.view_attrib  = view;
1581         personeditdlg.entry_atname  = entry_name;
1582         personeditdlg.entry_atvalue = entry_value;
1583         personeditdlg.attrib_add = buttonAdd;
1584         personeditdlg.attrib_del = buttonDel;
1585         personeditdlg.attrib_mod = buttonMod;
1586 }
1587
1588 static void addressbook_edit_person_create( GtkWidget *parent, gboolean *cancelled ) {
1589         if (prefs_common.addressbook_use_editaddress_dialog)
1590                 addressbook_edit_person_dialog_create( cancelled );
1591         else
1592                 addressbook_edit_person_widgetset_create( parent, cancelled );
1593         addressbook_edit_person_page_basic( PAGE_BASIC, _( "_User Data" ) );
1594         addressbook_edit_person_page_email( PAGE_EMAIL, _( "_Email Addresses" ) );
1595 #ifdef USE_LDAP
1596         if (personeditdlg.ldap)
1597                 addressbook_edit_person_page_attrib_ldap(&personeditdlg, PAGE_ATTRIBUTES, _("O_ther Attributes"));
1598         else
1599 #endif
1600                 addressbook_edit_person_page_attrib( PAGE_ATTRIBUTES, _( "O_ther Attributes" ) );
1601         gtk_widget_show_all( personeditdlg.container );
1602 }
1603
1604 /*
1605 * Return list of email items.
1606 */
1607 static GList *edit_person_build_email_list() {
1608         GtkWidget *view = personeditdlg.view_email;
1609         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
1610         GtkTreeIter iter;
1611         GList *listEMail = NULL;
1612         ItemEMail *email;
1613
1614         if (!gtk_tree_model_get_iter_first(model, &iter))
1615                 return NULL;
1616
1617         do {
1618                 gtk_tree_model_get(model, &iter, EMAIL_COL_PTR, &email, -1);
1619                 listEMail = g_list_append( listEMail, email );
1620         } while (gtk_tree_model_iter_next(model, &iter));
1621
1622         return listEMail;
1623 }
1624
1625 /*
1626 * Return list of attributes.
1627 */
1628 static GList *edit_person_build_attrib_list() {
1629         GtkTreeModel *model = gtk_tree_view_get_model(
1630                         GTK_TREE_VIEW(personeditdlg.view_attrib));
1631         GtkTreeIter iter;
1632         GList *listAttrib = NULL;
1633         UserAttribute *attrib;
1634
1635         /* Iterate through all the rows, selecting the one that
1636          * matches. */
1637         if (!gtk_tree_model_get_iter_first(model, &iter))
1638                 return FALSE;
1639
1640         do {
1641                 gtk_tree_model_get(model, &iter, ATTRIB_COL_PTR, &attrib, -1);
1642                 listAttrib = g_list_append( listAttrib, attrib );
1643         } while (gtk_tree_model_iter_next(model, &iter));
1644
1645         return listAttrib;
1646 }
1647
1648 static void update_sensitivity(void)
1649 {
1650         gtk_widget_set_sensitive(personeditdlg.entry_name,    !personeditdlg.read_only);
1651         gtk_widget_set_sensitive(personeditdlg.entry_first,   !personeditdlg.read_only);
1652         gtk_widget_set_sensitive(personeditdlg.entry_last,    !personeditdlg.read_only);
1653         gtk_widget_set_sensitive(personeditdlg.entry_nick,    !personeditdlg.read_only && !personeditdlg.ldap);
1654         gtk_widget_set_sensitive(personeditdlg.entry_email,   !personeditdlg.read_only);
1655         gtk_widget_set_sensitive(personeditdlg.entry_alias,   !personeditdlg.read_only && !personeditdlg.ldap);
1656         gtk_widget_set_sensitive(personeditdlg.entry_remarks, !personeditdlg.read_only && !personeditdlg.ldap);
1657         gtk_widget_set_sensitive(personeditdlg.email_up,      !personeditdlg.read_only);
1658         gtk_widget_set_sensitive(personeditdlg.email_down,    !personeditdlg.read_only);
1659         gtk_widget_set_sensitive(personeditdlg.email_del,     !personeditdlg.read_only);
1660         gtk_widget_set_sensitive(personeditdlg.email_mod,     !personeditdlg.read_only);
1661         gtk_widget_set_sensitive(personeditdlg.email_add,     !personeditdlg.read_only);
1662         gtk_widget_set_sensitive(personeditdlg.entry_atname,  !personeditdlg.read_only);
1663         gtk_widget_set_sensitive(personeditdlg.entry_atvalue, !personeditdlg.read_only);
1664         gtk_widget_set_sensitive(personeditdlg.attrib_add,    !personeditdlg.read_only);
1665         gtk_widget_set_sensitive(personeditdlg.attrib_del,    !personeditdlg.read_only);
1666         gtk_widget_set_sensitive(personeditdlg.attrib_mod,    !personeditdlg.read_only);
1667 }
1668
1669 static void addressbook_edit_person_flush_transient( void )
1670 {
1671         ItemPerson *person = current_person;
1672         EditAddressPostUpdateCallback callback = edit_person_close_post_update_cb;
1673
1674         /* reset transient data */
1675         current_abf = NULL;
1676         current_person = NULL;
1677         current_parent_folder = NULL;
1678         edit_person_close_post_update_cb = NULL;
1679
1680         /* post action to perform on addressbook side */
1681         if (callback)
1682                 callback( person );
1683 }
1684
1685 void addressbook_edit_person_invalidate( AddressBookFile *abf, ItemFolder *parent_folder,
1686                                                                                  ItemPerson *person )
1687 {
1688         if (current_abf == NULL &&
1689                 current_person == NULL &&
1690                 current_parent_folder == NULL)
1691                 /* edit address form is already hidden */
1692                 return;
1693
1694         /* unconditional invalidation or invalidating the currently edited item */
1695         if ( ( abf == NULL && person == NULL && parent_folder == NULL )
1696                 || (current_abf == abf ||
1697                         current_person == person ||
1698                         current_parent_folder == parent_folder))
1699                 addressbook_edit_person_close( TRUE );
1700 }
1701
1702 static gboolean addressbook_edit_person_close( gboolean cancelled )
1703 {
1704         GList *listEMail = NULL;
1705         GList *listAttrib = NULL;
1706         GError *error = NULL;
1707         GtkTreeModel *model;
1708
1709         listEMail = edit_person_build_email_list();
1710         listAttrib = edit_person_build_attrib_list();
1711         if( cancelled ) {
1712                 addritem_free_list_email( listEMail );
1713                 addritem_free_list_attribute( listAttrib );
1714                 model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_email));
1715                 gtk_list_store_clear(GTK_LIST_STORE(model));
1716                 model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_attrib));
1717                 gtk_list_store_clear(GTK_LIST_STORE(model));
1718
1719                 if (!prefs_common.addressbook_use_editaddress_dialog)
1720                         gtk_widget_hide( personeditdlg.container );
1721
1722                 /* no callback, as we're discarding the form */
1723                 edit_person_close_post_update_cb = NULL;
1724                 addressbook_edit_person_flush_transient();
1725                 current_person = NULL;
1726
1727                 /* set focus to the address list (this is done by the post_update_cb usually) */
1728                 addressbook_address_list_set_focus();
1729                 return FALSE;
1730         }
1731
1732         if( current_person && current_abf ) {
1733                 /* Update email/attribute list for existing current_person */
1734                 addrbook_update_address_list( current_abf, current_person, listEMail );
1735                 addrbook_update_attrib_list( current_abf, current_person, listAttrib );
1736         }
1737         else {
1738                 /* Create new current_person and email/attribute list */
1739                 if( ! cancelled && current_abf ) {
1740                         current_person = addrbook_add_address_list( current_abf, current_parent_folder, listEMail );
1741                         addrbook_add_attrib_list( current_abf, current_person, listAttrib );
1742                 }
1743         }
1744         listEMail = NULL;
1745         listAttrib = NULL;
1746
1747         if(!cancelled && current_person != NULL) {
1748                 /* Set current_person stuff */          
1749
1750                 gchar *name;
1751                 gchar *cn = edit_person_get_common_name_from_widgets();
1752
1753                 addritem_person_set_common_name( current_person, cn );
1754                 g_free( cn );
1755
1756                 if (personeditdlg.picture_set) { 
1757                         GdkPixbuf * pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(personeditdlg.image));
1758
1759                         if (!current_person->picture)
1760                                 name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1761                                                 ADDRITEM_ID(current_person), ".png", NULL );
1762                         else 
1763                                 name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1764                                                 current_person->picture, ".png", NULL );
1765
1766                         gdk_pixbuf_save(pixbuf, name, "png", &error, NULL);
1767                         if (error) {
1768                                 alertpanel_error(_("Failed to save image: \n%s"),
1769                                                 error->message);
1770                                 g_error_free(error);
1771                         } else {
1772                                 debug_print("saved picture to %s\n", name);
1773                         }
1774                         if (!current_person->picture)
1775                                 addritem_person_set_picture( current_person, ADDRITEM_ID(current_person) ) ;
1776                         g_free( name );
1777                 } else {
1778                         if (!current_person->picture)
1779                                 name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1780                                                 ADDRITEM_ID(current_person), ".png", NULL );
1781                         else 
1782                                 name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1783                                                 current_person->picture, ".png", NULL );
1784                         claws_unlink(name);
1785                         g_free(name);
1786                 }
1787                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_first), 0, -1 );
1788                 addritem_person_set_first_name( current_person, name );
1789                 g_free( name );
1790                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_last), 0, -1 );
1791                 addritem_person_set_last_name( current_person, name );
1792                 g_free( name );
1793                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_nick), 0, -1 );
1794                 addritem_person_set_nick_name( current_person, name );
1795                 g_free( name );
1796         }
1797
1798         model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_email));
1799         gtk_list_store_clear(GTK_LIST_STORE(model));
1800         model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_attrib));
1801         gtk_list_store_clear(GTK_LIST_STORE(model));
1802
1803         if (!prefs_common.addressbook_use_editaddress_dialog)
1804                 gtk_widget_hide( personeditdlg.container );
1805
1806         addressbook_edit_person_flush_transient();
1807
1808         return TRUE;
1809 }
1810  
1811 /*
1812 * Edit person.
1813 * Enter: abf    Address book.
1814 *        parent Parent folder for person (or NULL if adding to root folder). Argument is
1815 *               only required for new objects).
1816 *        person Person to edit, or NULL for a new person object.
1817 *        pgMail If TRUE, E-Mail page will be activated.
1818 * Return: Edited object, or NULL if cancelled.*/
1819 ItemPerson *addressbook_edit_person( AddressBookFile *abf, ItemFolder *parent_folder, ItemPerson *person,
1820                                                                          gboolean pgMail, GtkWidget *parent_container,
1821                                                                          void (*post_update_cb) (ItemPerson *person),
1822                                                                          gboolean get_focus) {
1823         static gboolean cancelled;
1824         GError *error = NULL;
1825         GdkPixbuf *pixbuf = NULL;
1826         GtkTreeModel *model;
1827         GtkTreeSelection *sel;
1828         GtkTreeIter iter;
1829
1830         /* set transient data */
1831         current_abf = abf;
1832         current_person = person;
1833         current_parent_folder = parent_folder;
1834         edit_person_close_post_update_cb = post_update_cb;
1835         personeditdlg.ldap = (abf && abf->type == ADBOOKTYPE_LDAP)? TRUE : FALSE;
1836
1837         if( personeditdlg.container ) {
1838                 gtk_widget_destroy(personeditdlg.container);
1839                 personeditdlg.container = NULL;
1840         }
1841         addressbook_edit_person_create(parent_container, &cancelled);
1842
1843         /* typically, get focus when dialog mode is enabled, or when editing a new address */
1844         if( get_focus ) {
1845                 gtk_widget_grab_focus(personeditdlg.ok_btn);
1846                 gtk_widget_grab_focus(personeditdlg.entry_name);
1847         }
1848         
1849         personeditdlg.read_only = (current_abf == NULL);
1850         update_sensitivity();
1851
1852         gtk_widget_show(personeditdlg.container);
1853         if (prefs_common.addressbook_use_editaddress_dialog)
1854                 manage_window_set_transient(GTK_WINDOW(personeditdlg.container));
1855         else
1856                 if (get_focus)
1857                         addressbook_address_list_disable_some_actions();
1858
1859         /* Clear all fields */
1860         edit_person_status_show( "" );
1861
1862         model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_email));
1863         gtk_list_store_clear(GTK_LIST_STORE(model));
1864         model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_attrib));
1865         gtk_list_store_clear(GTK_LIST_STORE(model));
1866
1867         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_name), "" );
1868         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_first), "" );
1869         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_last), "" );
1870         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_nick), "" );
1871
1872         personeditdlg.editNew = FALSE;
1873         if( current_person ) {
1874                 gchar *filename = NULL;
1875
1876                 if( ADDRITEM_NAME(current_person) )
1877                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_name), ADDRITEM_NAME(person) );
1878
1879                 cm_menu_set_sensitive("EditAddressPopup/SetPicture", !personeditdlg.ldap);
1880                 cm_menu_set_sensitive("EditAddressPopup/UnsetPicture", !personeditdlg.ldap);
1881                 if( current_person->picture ) { 
1882                         filename = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1883                                                         current_person->picture, ".png", NULL );
1884                         if (is_file_exist(filename)) {
1885                                 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1886                                 if (error) {
1887                                         debug_print("Failed to import image: %s\n",
1888                                                         error->message);
1889                                         g_error_free(error);
1890                                         goto no_img;
1891                                 }
1892                                 personeditdlg.picture_set = TRUE;
1893                                 cm_menu_set_sensitive("EditAddressPopup/UnsetPicture", !personeditdlg.ldap && personeditdlg.picture_set);
1894                         } else {
1895                                 goto no_img;
1896                         }
1897                         gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
1898                 } else {
1899 no_img:
1900                         addressbook_edit_person_clear_picture();
1901                 }
1902                 
1903                 g_free(filename);
1904                 if (pixbuf) {
1905                         g_object_unref(pixbuf);
1906                         pixbuf = NULL;
1907                 }
1908
1909                 if( current_person->firstName )
1910                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_first), current_person->firstName );
1911                 if( current_person->lastName )
1912                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_last), current_person->lastName );
1913                 if( current_person->nickName )
1914                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_nick), current_person->nickName );
1915                 edit_person_load_email( current_person );
1916                 edit_person_load_attrib( current_person );
1917                 gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_atvalue), "");
1918         }
1919         else {
1920                 personeditdlg.editNew = TRUE;
1921         }
1922
1923         /* Select appropriate start page */
1924         if( pgMail ) {
1925                 gtk_notebook_set_current_page( GTK_NOTEBOOK(personeditdlg.notebook), PAGE_EMAIL );
1926         }
1927         else {
1928                 gtk_notebook_set_current_page( GTK_NOTEBOOK(personeditdlg.notebook), PAGE_BASIC );
1929         }
1930
1931         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(personeditdlg.view_email));
1932         model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_email));
1933         if (gtk_tree_model_get_iter_first(model, &iter)) {
1934                 gtk_tree_selection_select_iter(sel, &iter);
1935                 edit_person_email_cursor_changed(
1936                                 GTK_TREE_VIEW(personeditdlg.view_email), NULL);
1937         }
1938
1939         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(personeditdlg.view_attrib));
1940         model = gtk_tree_view_get_model(GTK_TREE_VIEW(personeditdlg.view_attrib));
1941         if (gtk_tree_model_get_iter_first(model, &iter))
1942                 gtk_tree_selection_select_iter(sel, &iter);
1943         edit_person_attrib_cursor_changed(
1944                         GTK_TREE_VIEW(personeditdlg.view_attrib), NULL);
1945
1946         edit_person_email_clear( NULL );
1947         edit_person_attrib_clear( NULL );
1948
1949         if (prefs_common.addressbook_use_editaddress_dialog) {
1950                 gtk_main();
1951                 gtk_widget_hide( personeditdlg.container );
1952
1953                 if (!addressbook_edit_person_close( cancelled )) {
1954                         return NULL;
1955                 }
1956         }
1957         
1958         return person;
1959 }
1960
1961 void addressbook_edit_reload_attr_list( void )
1962 {
1963         if (personeditdlg.entry_atname) {
1964                 combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(personeditdlg.entry_atname));
1965                 if (prefs_common.addressbook_custom_attributes)
1966                         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(personeditdlg.entry_atname),
1967                                         prefs_common.addressbook_custom_attributes);
1968         }
1969 }
1970
1971 /*
1972 * End of Source.
1973 */
1974