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