05c0777ec4f08a94592626b76e9d10afc3a67550
[claws.git] / src / editaddress.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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
20 #ifdef HAVE_CONFIG_H
21 #  include "config.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/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtklabel.h>
32 #include <gtk/gtkvbox.h>
33 #include <gtk/gtkentry.h>
34 #include <gtk/gtktable.h>
35
36 #include "alertpanel.h"
37 #include "mgutils.h"
38 #include "addressbook.h"
39 #include "addressitem.h"
40 #include "addritem.h"
41 #include "addrbook.h"
42 #include "manage_window.h"
43 #include "gtkutils.h"
44 #include "filesel.h"
45 #include "codeconv.h"
46 #include "editaddress.h"
47 #include "editaddress_other_attributes_ldap.h"
48 #include "prefs_common.h"
49 #include "menu.h"
50
51 /* transient data */
52 static struct _PersonEdit_dlg personeditdlg;
53 static AddressBookFile *current_abf = NULL;
54 static ItemPerson *current_person = NULL;
55 static ItemFolder *current_parent_folder = NULL;
56 static EditAddressPostUpdateCallback edit_person_close_post_update_cb = NULL;
57
58 typedef enum {
59         EMAIL_COL_EMAIL   = 0,
60         EMAIL_COL_ALIAS   = 1,
61         EMAIL_COL_REMARKS = 2
62 } PersonEditEMailColumnPos;
63
64 typedef enum {
65         ATTRIB_COL_NAME    = 0,
66         ATTRIB_COL_VALUE   = 1
67 } PersonEditAttribColumnPos;
68
69 #define EDITPERSON_WIDTH      520
70 #define EDITPERSON_HEIGHT     320
71
72 #ifndef MAEMO
73 # define EMAIL_N_COLS          3
74 # define EMAIL_COL_WIDTH_EMAIL 180
75 # define EMAIL_COL_WIDTH_ALIAS 80
76 #else
77 # define EMAIL_N_COLS          1
78 # define EMAIL_COL_WIDTH_EMAIL 130
79 # define EMAIL_COL_WIDTH_ALIAS 130
80 #endif
81
82 #ifndef MAEMO
83 # define ATTRIB_N_COLS          2
84 # define ATTRIB_COL_WIDTH_NAME  240
85 # define ATTRIB_COL_WIDTH_VALUE 0
86 #else
87 # define ATTRIB_N_COLS          2
88 # define ATTRIB_COL_WIDTH_NAME  120
89 # define ATTRIB_COL_WIDTH_VALUE 120
90 #endif
91
92 #define PAGE_BASIC             0
93 #define PAGE_EMAIL             1
94 #define PAGE_ATTRIBUTES        2
95
96 static gboolean addressbook_edit_person_close( gboolean cancelled );
97
98 static void edit_person_status_show( gchar *msg ) {
99         if( personeditdlg.statusbar != NULL ) {
100                 gtk_statusbar_pop( GTK_STATUSBAR(personeditdlg.statusbar), personeditdlg.status_cid );
101                 if( msg ) {
102                         gtk_statusbar_push( GTK_STATUSBAR(personeditdlg.statusbar), personeditdlg.status_cid, msg );
103                 }
104         }
105 }
106
107 static void edit_person_ok(GtkWidget *widget, gboolean *cancelled) {
108         *cancelled = FALSE;
109         if (prefs_common.addressbook_use_editaddress_dialog)
110                 gtk_main_quit();
111         else
112                 addressbook_edit_person_close( *cancelled );
113 }
114
115 static void edit_person_cancel(GtkWidget *widget, gboolean *cancelled) {
116         *cancelled = TRUE;
117         if (prefs_common.addressbook_use_editaddress_dialog)
118                 gtk_main_quit();
119         else
120                 addressbook_edit_person_close( *cancelled );
121 }
122
123 static gint edit_person_delete_event(GtkWidget *widget, GdkEventAny *event, gboolean *cancelled) {
124         *cancelled = TRUE;
125         if (prefs_common.addressbook_use_editaddress_dialog)
126                 gtk_main_quit();
127         else
128                 addressbook_edit_person_close( *cancelled );
129         return TRUE;
130 }
131
132 static gboolean edit_person_key_pressed(GtkWidget *widget, GdkEventKey *event, gboolean *cancelled) {
133         if (prefs_common.addressbook_use_editaddress_dialog) {
134         if (event && event->keyval == GDK_Escape) {
135                 *cancelled = TRUE;
136                 gtk_main_quit();
137         }
138         }
139         return FALSE;
140 }
141
142 static gchar *_title_new_ = NULL;
143 static gchar *_title_edit_ = NULL;
144
145 static void edit_person_set_widgets_title( gchar *text )
146 {
147         gchar *label = NULL;
148
149         g_return_if_fail( text != NULL );
150
151         gtk_label_set_text(GTK_LABEL(personeditdlg.title), "");
152         label = g_markup_printf_escaped("<b>%s</b>", text);
153         gtk_label_set_markup(GTK_LABEL(personeditdlg.title), label);
154         g_free(label);
155 }
156
157 static void edit_person_set_window_title( gint pageNum ) {
158         gchar *sTitle;
159
160         if( _title_new_ == NULL ) {
161                 _title_new_ = g_strdup( _("Add New Person") );
162                 _title_edit_ = g_strdup( _("Edit Person Details") );
163         }
164
165         if( pageNum == PAGE_BASIC ) {
166                 if( personeditdlg.editNew ) {
167                         if (prefs_common.addressbook_use_editaddress_dialog)
168                                 gtk_window_set_title( GTK_WINDOW(personeditdlg.container), _title_new_ );
169                         else
170                                 edit_person_set_widgets_title( _title_new_ );
171                 }
172                 else {
173                         if (prefs_common.addressbook_use_editaddress_dialog)
174                                 gtk_window_set_title( GTK_WINDOW(personeditdlg.container), _title_edit_ );
175                         else
176                                 edit_person_set_widgets_title( _title_edit_ );
177                 }
178         }
179         else {
180                 if( personeditdlg.entry_name == NULL ) {
181                         sTitle = g_strdup( _title_edit_ );
182                 }
183                 else {
184                         gchar *name;
185                         name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_name), 0, -1 );
186                         sTitle = g_strdup_printf( "%s - %s", _title_edit_, name );
187                         g_free( name );
188                 }
189                 if (prefs_common.addressbook_use_editaddress_dialog)
190                         gtk_window_set_title( GTK_WINDOW(personeditdlg.container), sTitle );
191                 else
192                         edit_person_set_widgets_title( sTitle );
193                 g_free( sTitle );
194         }
195 }
196
197 static void edit_person_email_clear( gpointer data ) {
198         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_email), "" );
199         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_alias), "" );
200         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_remarks), "" );
201 }
202
203 static void edit_person_attrib_clear( gpointer data ) {
204         if (!personeditdlg.ldap) {
205                 gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_atname), "" );
206                 gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_atvalue), "" );
207         }
208 }
209
210 static void edit_person_switch_page( GtkNotebook *notebook, GtkNotebookPage *page,
211                                         gint pageNum, gpointer user_data)
212 {
213         edit_person_set_window_title( pageNum );
214         edit_person_status_show( "" );
215 }
216
217 /*
218 * Load clist with a copy of person's email addresses.
219 */
220 static void edit_person_load_email( ItemPerson *person ) {
221         GList *node = person->listEMail;
222         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
223         gchar *text[ EMAIL_N_COLS ];
224         while( node ) {
225                 ItemEMail *emorig = ( ItemEMail * ) node->data;
226                 ItemEMail *email = addritem_copyfull_item_email( emorig );
227                 gint row;
228                 text[ EMAIL_COL_EMAIL   ] = email->address;
229 #ifndef MAEMO
230                 text[ EMAIL_COL_ALIAS   ] = email->obj.name;
231                 text[ EMAIL_COL_REMARKS ] = email->remarks;
232 #endif
233                 row = gtk_clist_append( clist, text );
234                 gtk_clist_set_row_data( clist, row, email );
235                 node = g_list_next( node );
236         }
237 }
238
239 static void edit_person_email_list_selected( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
240         ItemEMail *email = gtk_clist_get_row_data( clist, row );
241         if( email ) {
242                 if( email->address )
243                         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_email), email->address );
244                 if( ADDRITEM_NAME(email) )
245                         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_alias), ADDRITEM_NAME(email) );
246                 if( email->remarks )
247                         gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_remarks), email->remarks );
248                 if (!personeditdlg.read_only) {
249                         gtk_widget_set_sensitive(personeditdlg.email_del, TRUE);
250                         gtk_widget_set_sensitive(personeditdlg.email_up, row > 0);
251                         gtk_widget_set_sensitive(personeditdlg.email_down, gtk_clist_get_row_data(clist, row + 1) != NULL);
252                 }
253         } else {
254                 gtk_widget_set_sensitive(personeditdlg.email_del, FALSE);
255                 gtk_widget_set_sensitive(personeditdlg.email_up, FALSE);
256                 gtk_widget_set_sensitive(personeditdlg.email_down, FALSE);
257         }
258         personeditdlg.rowIndEMail = row;
259         edit_person_status_show( NULL );
260 }
261
262 static void edit_person_email_move( gint dir ) {
263         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
264         gint row = personeditdlg.rowIndEMail + dir;
265         ItemEMail *email = gtk_clist_get_row_data( clist, row );
266         if( email ) {
267                 gtk_clist_row_move( clist, personeditdlg.rowIndEMail, row );
268                 personeditdlg.rowIndEMail = row;
269                 if (!personeditdlg.read_only) {
270                         gtk_widget_set_sensitive(personeditdlg.email_up, row > 0);
271                         gtk_widget_set_sensitive(personeditdlg.email_down, gtk_clist_get_row_data(clist, row + 1) != NULL);
272                 }
273         } else {
274                 gtk_widget_set_sensitive(personeditdlg.email_up, FALSE);
275                 gtk_widget_set_sensitive(personeditdlg.email_down, FALSE);
276         }
277         edit_person_email_clear( NULL );
278         edit_person_status_show( NULL );
279 }
280
281 static void edit_person_email_move_up( gpointer data ) {
282         edit_person_email_move( -1 );
283 }
284
285 static void edit_person_email_move_down( gpointer data ) {
286         edit_person_email_move( +1 );
287 }
288
289 static void edit_person_email_delete( gpointer data ) {
290         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
291         gint row = personeditdlg.rowIndEMail;
292         ItemEMail *email = gtk_clist_get_row_data( clist, row );
293         edit_person_email_clear( NULL );
294         if( email ) {
295                 /* Remove list entry */
296                 gtk_clist_remove( clist, row );
297                 addritem_free_item_email( email );
298                 email = NULL;
299         }
300
301         /* Position hilite bar */
302         email = gtk_clist_get_row_data( clist, row );
303         if( ! email ) {
304                 personeditdlg.rowIndEMail = -1 + row;
305         }
306         if (!personeditdlg.read_only) {
307                 gtk_widget_set_sensitive(personeditdlg.email_del, gtk_clist_get_row_data(clist, 0) != NULL);
308                 gtk_widget_set_sensitive(personeditdlg.email_up, gtk_clist_get_row_data(clist, personeditdlg.rowIndEMail + 1) != NULL);
309                 gtk_widget_set_sensitive(personeditdlg.email_down, gtk_clist_get_row_data(clist, personeditdlg.rowIndEMail - 1) != NULL);
310         }
311         edit_person_status_show( NULL );
312 }
313
314 static ItemEMail *edit_person_email_edit( gboolean *error, ItemEMail *email ) {
315         ItemEMail *retVal = NULL;
316         gchar *sEmail, *sAlias, *sRemarks, *sEmail_;
317
318         *error = TRUE;
319         sEmail_ = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_email), 0, -1 );
320         sAlias = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_alias), 0, -1 );
321         sRemarks = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_remarks), 0, -1 );
322         sEmail = mgu_email_check_empty( sEmail_ );
323         g_free( sEmail_ );
324
325         if( sEmail ) {
326                 if( email == NULL ) {
327                         email = addritem_create_item_email();
328                 }
329                 addritem_email_set_address( email, sEmail );
330                 addritem_email_set_alias( email, sAlias );
331                 addritem_email_set_remarks( email, sRemarks );
332                 retVal = email;
333                 *error = FALSE;
334         }
335         else {
336                 edit_person_status_show( _( "An Email address must be supplied." ) );
337         }
338
339         g_free( sEmail );
340         g_free( sAlias );
341         g_free( sRemarks );
342
343         return retVal;
344 }
345
346 static void edit_person_email_modify( gpointer data ) {
347         gboolean errFlg = FALSE;
348         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
349         gint row = personeditdlg.rowIndEMail;
350         ItemEMail *email = gtk_clist_get_row_data( clist, row );
351         if( email ) {
352                 edit_person_email_edit( &errFlg, email );
353                 if( ! errFlg ) {
354                         gtk_clist_set_text( clist, row, EMAIL_COL_EMAIL, email->address );
355                         gtk_clist_set_text( clist, row, EMAIL_COL_ALIAS, email->obj.name );
356                         gtk_clist_set_text( clist, row, EMAIL_COL_REMARKS, email->remarks );
357                         edit_person_email_clear( NULL );
358                 }
359         }
360 }
361
362 static void edit_person_email_add( gpointer data ) {
363         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
364         gboolean errFlg = FALSE;
365         ItemEMail *email = NULL;
366         gint row = personeditdlg.rowIndEMail;
367         if( gtk_clist_get_row_data( clist, row ) == NULL ) row = 0;
368
369         email = edit_person_email_edit( &errFlg, NULL );
370         if( ! errFlg ) {
371                 gchar *text[ EMAIL_N_COLS ];
372                 text[ EMAIL_COL_EMAIL   ] = email->address;
373 #ifndef MAEMO
374                 text[ EMAIL_COL_ALIAS   ] = email->obj.name;
375                 text[ EMAIL_COL_REMARKS ] = email->remarks;
376 #endif
377                 row = gtk_clist_insert( clist, 1 + row, text );
378                 gtk_clist_set_row_data( clist, row, email );
379                 gtk_clist_select_row( clist, row, 0 );
380                 edit_person_email_clear( NULL );
381         }
382 }
383
384 /*
385 * Comparison using cell contents (text in first column). Used for sort
386 * address index widget.
387 */
388 static gint edit_person_attrib_compare_func(
389         GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 )
390 {
391         GtkCell *cell1 = ((GtkCListRow *)ptr1)->cell;
392         GtkCell *cell2 = ((GtkCListRow *)ptr2)->cell;
393         gchar *name1 = NULL, *name2 = NULL;
394
395         if( cell1 ) name1 = cell1->u.text;
396         if( cell2 ) name2 = cell2->u.text;
397         if( ! name1 ) return ( name2 != NULL );
398         if( ! name2 ) return -1;
399         return g_utf8_collate( name1, name2 );
400 }
401
402 static gboolean list_find_attribute(const gchar *attr)
403 {
404         GtkCList *clist = GTK_CLIST(personeditdlg.clist_attrib);
405         UserAttribute *attrib;
406         gint row = 0;
407         while( (attrib = gtk_clist_get_row_data( clist, row )) ) {
408                 if (!g_ascii_strcasecmp(attrib->name, attr)) {
409                         gtk_clist_select_row(clist, row, 0);
410                         return TRUE;
411                 }
412                 row++;
413         }
414         return FALSE;
415 }
416
417 static gboolean list_find_email(const gchar *addr)
418 {
419         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
420         ItemEMail *email;
421         gint row = 0;
422         while( (email = gtk_clist_get_row_data( clist, row )) ) {
423                 if (!g_ascii_strcasecmp(email->address, addr)) {
424                         gtk_clist_select_row(clist, row, 0);
425                         return TRUE;
426                 }
427                 row++;
428         }
429         return FALSE;
430 }
431
432 /*
433 * Load clist with a copy of person's email addresses.
434 */
435 static void edit_person_load_attrib( ItemPerson *person ) {
436         GList *node = person->listAttrib;
437         GtkCList *clist = GTK_CLIST(personeditdlg.clist_attrib);
438         gchar *text[ ATTRIB_N_COLS ];
439         while( node ) {
440                 UserAttribute *atorig = ( UserAttribute * ) node->data;
441                 UserAttribute *attrib = addritem_copy_attribute( atorig );
442                 gint row;
443                 debug_print("name: %s value: %s\n", attrib->name, attrib->value);
444                 text[ ATTRIB_COL_NAME  ] = attrib->name;
445                 text[ ATTRIB_COL_VALUE ] = attrib->value;
446
447                 row = gtk_clist_append( clist, text );
448                 gtk_clist_set_row_data( clist, row, attrib );
449                 node = g_list_next( node );
450         }
451 }
452
453 static void edit_person_attrib_list_selected( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
454         UserAttribute *attrib = gtk_clist_get_row_data( clist, row );
455         if( attrib && !personeditdlg.read_only && !personeditdlg.ldap ) {
456                 gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_atname), attrib->name );
457                 gtk_entry_set_text( GTK_ENTRY(personeditdlg.entry_atvalue), attrib->value );
458                 gtk_widget_set_sensitive(personeditdlg.attrib_del, TRUE);
459         } else {
460                 gtk_widget_set_sensitive(personeditdlg.attrib_del, FALSE);
461         }
462         personeditdlg.rowIndAttrib = row;
463         edit_person_status_show( NULL );
464 }
465
466 static void edit_person_attrib_delete( gpointer data ) {
467         GtkCList *clist = GTK_CLIST(personeditdlg.clist_attrib);
468         gint row = personeditdlg.rowIndAttrib;
469         UserAttribute *attrib = gtk_clist_get_row_data( clist, row );
470         edit_person_attrib_clear( NULL );
471         if( attrib ) {
472                 /* Remove list entry */
473                 gtk_clist_remove( clist, row );
474                 addritem_free_attribute( attrib );
475                 attrib = NULL;
476         }
477
478         /* Position hilite bar */
479         attrib = gtk_clist_get_row_data( clist, row );
480         if( ! attrib ) {
481                 personeditdlg.rowIndAttrib = -1 + row;
482         } 
483         
484         if (!personeditdlg.read_only && !personeditdlg.ldap)
485                 gtk_widget_set_sensitive(personeditdlg.attrib_del, gtk_clist_get_row_data(clist, 0) != NULL);
486         
487         edit_person_status_show( NULL );
488 }
489
490 static UserAttribute *edit_person_attrib_edit( gboolean *error, UserAttribute *attrib ) {
491         UserAttribute *retVal = NULL;
492         gchar *sName, *sValue, *sName_, *sValue_;
493
494         *error = TRUE;
495         sName_ = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_atname), 0, -1 );
496         sValue_ = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_atvalue), 0, -1 );
497         sName = mgu_email_check_empty( sName_ );
498         sValue = mgu_email_check_empty( sValue_ );
499         g_free( sName_ );
500         g_free( sValue_ );
501
502         if( sName && sValue ) {
503                 if( attrib == NULL ) {
504                         attrib = addritem_create_attribute();
505                 }
506                 addritem_attrib_set_name( attrib, sName );
507                 addritem_attrib_set_value( attrib, sValue );
508                 retVal = attrib;
509                 *error = FALSE;
510         }
511         else {
512                 edit_person_status_show( _( "A Name and Value must be supplied." ) );
513         }
514
515         g_free( sName );
516         g_free( sValue );
517
518         return retVal;
519 }
520
521 static void edit_person_attrib_modify( gpointer data ) {
522         gboolean errFlg = FALSE;
523         GtkCList *clist = GTK_CLIST(personeditdlg.clist_attrib);
524         gint row = personeditdlg.rowIndAttrib;
525         UserAttribute *attrib = gtk_clist_get_row_data( clist, row );
526         if( attrib ) {
527                 edit_person_attrib_edit( &errFlg, attrib );
528                 if( ! errFlg ) {
529                         gtk_clist_set_text( clist, row, ATTRIB_COL_NAME, attrib->name );
530                         gtk_clist_set_text( clist, row, ATTRIB_COL_VALUE, attrib->value );
531                         edit_person_attrib_clear( NULL );
532                 }
533         }
534 }
535
536 static void edit_person_attrib_add( gpointer data ) {
537         GtkCList *clist = GTK_CLIST(personeditdlg.clist_attrib);
538         gboolean errFlg = FALSE;
539         UserAttribute *attrib = NULL;
540         gint row = personeditdlg.rowIndAttrib;
541         if( gtk_clist_get_row_data( clist, row ) == NULL ) row = 0;
542
543         attrib = edit_person_attrib_edit( &errFlg, NULL );
544         if( ! errFlg ) {
545                 gchar *text[ ATTRIB_N_COLS ];
546                 text[ ATTRIB_COL_NAME  ] = attrib->name;
547                 text[ ATTRIB_COL_VALUE ] = attrib->value;
548
549                 row = gtk_clist_insert( clist, 1 + row, text );
550                 gtk_clist_set_row_data( clist, row, attrib );
551                 gtk_clist_select_row( clist, row, 0 );
552                 edit_person_attrib_clear( NULL );
553         }
554 }
555
556 /*!
557  *\brief        Save Gtk object size to prefs dataset
558  */
559 static void edit_person_size_allocate_cb(GtkWidget *widget,
560                                          GtkAllocation *allocation)
561 {
562         g_return_if_fail(allocation != NULL);
563
564         prefs_common.addressbookeditpersonwin_width = allocation->width;
565         prefs_common.addressbookeditpersonwin_height = allocation->height;
566 }
567
568 /* build edit person widgets, return a pointer to the main container of the widgetset (a vbox) */
569 static GtkWidget* addressbook_edit_person_widgets_create( GtkWidget* container, gboolean *cancelled )
570 {
571         GtkWidget *vbox;
572         GtkWidget *vnbox;
573         GtkWidget *notebook;
574         GtkWidget *hbbox;
575         GtkWidget *ok_btn;
576         GtkWidget *cancel_btn;
577
578         vbox = gtk_vbox_new(FALSE, 4);
579          gtk_container_set_border_width(GTK_CONTAINER(vbox), BORDER_WIDTH); 
580         gtk_widget_show(vbox);
581         gtk_container_add(GTK_CONTAINER(container), vbox);
582
583         vnbox = gtk_vbox_new(FALSE, 4);
584         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
585         gtk_widget_show(vnbox);
586         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
587
588         /* Notebook */
589         notebook = gtk_notebook_new();
590         gtk_widget_show(notebook);
591         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
592         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
593
594         /* Button panel */
595         if (prefs_common.addressbook_use_editaddress_dialog)
596         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
597                                       &ok_btn, GTK_STOCK_OK,
598                                       NULL, NULL);
599         else
600                 gtkut_stock_with_text_button_set_create(&hbbox,
601                                           &cancel_btn, GTK_STOCK_CANCEL, _("Discard"),
602                                       &ok_btn, GTK_STOCK_OK, _("Apply"),
603                                       NULL, NULL, NULL);
604         gtk_box_pack_end(GTK_BOX(vnbox), hbbox, FALSE, FALSE, 0);
605         gtk_widget_grab_default(ok_btn);
606
607         g_signal_connect(G_OBJECT(ok_btn), "clicked",
608                          G_CALLBACK(edit_person_ok), cancelled);
609         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
610                          G_CALLBACK(edit_person_cancel), cancelled);
611         g_signal_connect(G_OBJECT(notebook), "switch_page",
612                          G_CALLBACK(edit_person_switch_page), NULL );
613
614         gtk_widget_show_all(vbox);
615
616         personeditdlg.notebook   = notebook;
617         personeditdlg.ok_btn     = ok_btn;
618         personeditdlg.cancel_btn = cancel_btn;
619
620         return vbox;
621 }
622
623 static void addressbook_edit_person_dialog_create( gboolean *cancelled ) {
624         GtkWidget *window;
625         GtkWidget *hsbox;
626         GtkWidget *vbox;
627         GtkWidget *statusbar;
628         static GdkGeometry geometry;
629
630         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editaddress");
631         /* gtk_container_set_border_width(GTK_CONTAINER(window), 0); */
632         gtk_window_set_title(GTK_WINDOW(window), _("Edit Person Data"));
633         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
634         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
635         g_signal_connect(G_OBJECT(window), "delete_event",
636                          G_CALLBACK(edit_person_delete_event),
637                          cancelled);
638         g_signal_connect(G_OBJECT(window), "size_allocate",
639                          G_CALLBACK(edit_person_size_allocate_cb),
640                         cancelled);
641         g_signal_connect(G_OBJECT(window), "key_press_event",
642                          G_CALLBACK(edit_person_key_pressed),
643                          cancelled);
644
645         vbox = addressbook_edit_person_widgets_create(window, cancelled);
646
647         /* Status line */
648         hsbox = gtk_hbox_new(FALSE, 0);
649         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
650         statusbar = gtk_statusbar_new();
651         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
652
653         if (!geometry.min_height) {
654                 geometry.min_width = EDITPERSON_WIDTH;
655                 geometry.min_height = EDITPERSON_HEIGHT;
656         }
657
658         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
659                                       GDK_HINT_MIN_SIZE);
660         gtk_widget_set_size_request(window, prefs_common.addressbookeditpersonwin_width,
661                                     prefs_common.addressbookeditpersonwin_height);
662
663         personeditdlg.container  = window;
664         personeditdlg.statusbar  = statusbar;
665         personeditdlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit Person Dialog" );
666
667 }
668
669 /* parent must be a box */
670 static void addressbook_edit_person_widgetset_create( GtkWidget *parent, gboolean *cancelled )
671 {
672         GtkWidget *vbox;
673         GtkWidget *label;
674
675         if ( parent == NULL )
676                 g_warning("addressbook_edit_person_widgetset_create: parent is NULL");
677
678         vbox = gtk_vbox_new(FALSE, 0);
679         gtk_box_pack_end(GTK_BOX(parent), vbox, TRUE, TRUE, 0);
680
681         label = gtk_label_new(_("Edit Person Data"));
682         gtk_label_set_justify( GTK_LABEL(label), GTK_JUSTIFY_CENTER);
683         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
684
685         addressbook_edit_person_widgets_create(vbox, cancelled);
686
687         gtk_widget_set_size_request(vbox, EDITPERSON_WIDTH, EDITPERSON_HEIGHT);
688
689         personeditdlg.container = vbox;
690         personeditdlg.title = label;
691         personeditdlg.statusbar  = NULL;
692         personeditdlg.status_cid = 0;
693 }
694
695 void addressbook_edit_person_widgetset_hide( void )
696 {
697         if ( personeditdlg.container )
698                 gtk_widget_hide( personeditdlg.container );
699 }
700
701 void addressbook_edit_person_set_picture(void)
702 {
703         GError *error = NULL;
704         gchar *filename;
705         int width, height, scalewidth, scaleheight;
706
707         if ( (filename = filesel_select_file_open(_("Choose a picture"), NULL)) ) {
708                 GdkPixbuf *pixbuf = NULL;
709                 gdk_pixbuf_get_file_info(filename, &width, &height);
710
711                 if ( width > 128 || height > 128 ) {
712                         if (width > height) {
713                                 scaleheight = (height * 128) / width;
714                                 scalewidth = 128;
715                         }
716                         else {
717                                 scalewidth = (width * 128) / height;
718                                 scaleheight = 128;
719                         }
720                         pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, 
721                                         scalewidth, scaleheight, TRUE, &error);
722                 } else {
723                         pixbuf = gdk_pixbuf_new_from_file(filename, &error);
724                 }
725                 if (error) {
726                         alertpanel_error(_("Failed to import image: \n%s"),
727                                         error->message);
728                         g_error_free(error);
729                         error = NULL;
730                         /* keep the previous picture if any */
731                         g_free(filename);
732                         if (pixbuf)
733                                 g_object_unref(pixbuf);
734                         return;
735                 }
736                 personeditdlg.picture_set = TRUE;
737                 g_free(filename);
738                 gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
739                 g_object_unref(pixbuf);
740         }
741 }       
742
743 static void addressbook_edit_person_clear_picture(void)
744 {
745         GdkPixbuf *pixbuf;
746
747         stock_pixbuf_gdk(NULL, STOCK_PIXMAP_ANONYMOUS, &pixbuf);
748         personeditdlg.picture_set = FALSE;
749         gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
750         g_object_unref(pixbuf);
751 }
752
753 static void addressbook_edit_person_set_picture_menu_cb (void *obj, guint action, void *data)
754 {
755         addressbook_edit_person_set_picture();
756 }
757
758 static void addressbook_edit_person_unset_picture_menu_cb (void *obj, guint action, void *data)
759 {
760         addressbook_edit_person_clear_picture();
761 }
762
763 static GtkItemFactoryEntry editaddr_popup_entries[] =
764 {
765         {N_("/_Set picture"),           NULL, addressbook_edit_person_set_picture_menu_cb, 0, NULL},
766         {N_("/_Unset picture"),         NULL, addressbook_edit_person_unset_picture_menu_cb, 0, NULL},
767 };
768
769 static void addressbook_edit_person_set_picture_cb(GtkWidget *widget, 
770                 GdkEventButton *event, gpointer data)
771 {       
772         if (event->button == 1) {
773                 addressbook_edit_person_set_picture();
774         } else {
775                 gtk_menu_popup(GTK_MENU(personeditdlg.editaddr_popupmenu), 
776                                NULL, NULL, NULL, NULL, 
777                                event->button, event->time);
778         }
779 }
780
781 static void addressbook_edit_person_page_basic( gint pageNum, gchar *pageLbl ) {
782         GtkWidget *vbox;
783         GtkWidget *hbox;
784         GtkWidget *table;
785         GtkWidget *label;
786         GtkWidget *ebox_picture;
787         GtkWidget *frame_picture;
788         GtkWidget *entry_name;
789         GtkWidget *entry_fn;
790         GtkWidget *entry_ln;
791         GtkWidget *entry_nn;
792         const gchar *locale;
793         gint top = 0;
794         gint n_entries;
795         vbox = gtk_vbox_new( FALSE, 20 );
796         hbox = gtk_hbox_new( FALSE, 8 );
797
798         gtk_widget_show( vbox );        
799         
800         /* User's picture */
801         ebox_picture = gtk_event_box_new();
802         frame_picture = gtk_frame_new(_("Photo"));
803         
804         /* Room for a photo */
805         personeditdlg.image = gtk_image_new();
806         addressbook_edit_person_clear_picture();
807
808         gtk_container_add(GTK_CONTAINER(ebox_picture), personeditdlg.image);
809         gtk_container_add(GTK_CONTAINER(frame_picture), ebox_picture);  
810         gtk_container_add(GTK_CONTAINER( personeditdlg.notebook ), hbox );
811         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
812         gtk_container_set_border_width( GTK_CONTAINER (hbox), BORDER_WIDTH );
813
814         label = gtk_label_new_with_mnemonic( pageLbl );
815         gtk_widget_show( label );
816         
817         gtk_box_pack_start(GTK_BOX(hbox), frame_picture, TRUE, TRUE, 0);
818         
819         gtk_notebook_set_tab_label(
820                 GTK_NOTEBOOK( personeditdlg.notebook ),
821                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
822         
823         g_signal_connect(G_OBJECT(ebox_picture), "button_press_event", 
824                         G_CALLBACK(addressbook_edit_person_set_picture_cb), NULL);
825
826         n_entries = sizeof(editaddr_popup_entries) /
827                 sizeof(editaddr_popup_entries[0]);
828         personeditdlg.editaddr_popupmenu = menu_create_items(editaddr_popup_entries, n_entries,
829                                       "<EditAddrPopupMenu>", &personeditdlg.editaddr_popupfactory,
830                                       NULL);
831
832         table = gtk_table_new( 3, 3, FALSE);
833
834 #define ATTACH_ROW(text, entry) \
835 { \
836         label = gtk_label_new(text); \
837         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
838                          GTK_FILL, 0, 0, 0); \
839         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
840  \
841         entry = gtk_entry_new(); \
842         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
843                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
844         top++; \
845 }
846
847 #define ATTACH_HIDDEN_ROW(text, entry) \
848 { \
849         entry = gtk_entry_new(); \
850 }
851
852 #ifndef MAEMO
853         ATTACH_ROW(_("Display Name"), entry_name);
854 #else
855         ATTACH_HIDDEN_ROW(_("Display Name"), entry_name);
856 #endif
857         locale = conv_get_current_locale();
858         if (locale &&
859             (!g_ascii_strncasecmp(locale, "ja", 2) ||
860              !g_ascii_strncasecmp(locale, "ko", 2) ||
861              !g_ascii_strncasecmp(locale, "zh", 2))) {
862                 ATTACH_ROW(_("Last Name"), entry_ln);
863                 ATTACH_ROW(_("First Name"), entry_fn);
864         } else {
865                 ATTACH_ROW(_("First Name"), entry_fn);
866                 ATTACH_ROW(_("Last Name"), entry_ln);
867         }
868 #ifndef MAEMO
869         ATTACH_ROW(_("Nickname"), entry_nn);
870 #else
871         ATTACH_HIDDEN_ROW(_("Nickname"), entry_nn);
872 #endif
873
874 #undef ATTACH_ROW
875 #undef ATTACH_HIDDEN_ROW
876         gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
877         gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
878         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
879         gtk_table_set_row_spacings(GTK_TABLE(table), 15);
880         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
881
882         gtk_widget_show_all(vbox);
883         personeditdlg.entry_name  = entry_name;
884         personeditdlg.entry_first = entry_fn;
885         personeditdlg.entry_last  = entry_ln;
886         personeditdlg.entry_nick  = entry_nn;
887 }
888
889 static gboolean email_adding = FALSE, email_saving = FALSE;
890
891 static void edit_person_entry_email_changed (GtkWidget *entry, gpointer data)
892 {
893         gboolean non_empty = gtk_clist_get_row_data(GTK_CLIST(personeditdlg.clist_email), 0) != NULL;
894
895         if (personeditdlg.read_only)
896                 return;
897
898         if (gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email)) == NULL
899         ||  strlen(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email))) == 0) {
900                 gtk_widget_set_sensitive(personeditdlg.email_add,FALSE);
901                 gtk_widget_set_sensitive(personeditdlg.email_mod,FALSE);
902                 email_adding = FALSE;
903                 email_saving = FALSE;
904         } else if (list_find_email(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email)))) {
905                 gtk_widget_set_sensitive(personeditdlg.email_add,FALSE);
906                 gtk_widget_set_sensitive(personeditdlg.email_mod,non_empty);
907                 email_adding = FALSE;
908                 email_saving = non_empty;
909         } else {
910                 gtk_widget_set_sensitive(personeditdlg.email_add,TRUE);
911                 gtk_widget_set_sensitive(personeditdlg.email_mod,non_empty);
912                 email_adding = TRUE;
913                 email_saving = non_empty;
914         }
915 }
916
917 static gboolean edit_person_entry_email_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
918 {
919         if (event && event->keyval == GDK_Return) {
920                 if (email_saving)
921                         edit_person_email_modify(NULL);         
922                 else if (email_adding)
923                         edit_person_email_add(NULL);
924         }
925         return FALSE;
926 }
927
928
929 static void addressbook_edit_person_page_email( gint pageNum, gchar *pageLbl ) {
930         GtkWidget *vbox;
931         GtkWidget *hbox;
932         GtkWidget *vboxl;
933         GtkWidget *vboxb;
934         GtkWidget *vbuttonbox;
935         GtkWidget *buttonUp;
936         GtkWidget *buttonDown;
937         GtkWidget *buttonDel;
938         GtkWidget *buttonMod;
939         GtkWidget *buttonAdd;
940
941         GtkWidget *table;
942         GtkWidget *label;
943         GtkWidget *clist_swin;
944         GtkWidget *clist;
945         GtkWidget *entry_email;
946         GtkWidget *entry_alias;
947         GtkWidget *entry_remarks;
948         gint top;
949
950         gchar *titles[ EMAIL_N_COLS ];
951         gint i;
952
953         titles[ EMAIL_COL_EMAIL   ] = _("Email Address");
954 #ifndef MAEMO
955         titles[ EMAIL_COL_ALIAS   ] = _("Alias");
956         titles[ EMAIL_COL_REMARKS ] = _("Remarks");
957 #endif
958         vbox = gtk_vbox_new( FALSE, 8 );
959         gtk_widget_show( vbox );
960         gtk_container_add( GTK_CONTAINER( personeditdlg.notebook ), vbox );
961         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
962
963         label = gtk_label_new_with_mnemonic( pageLbl );
964         gtk_widget_show( label );
965         gtk_notebook_set_tab_label(
966                 GTK_NOTEBOOK( personeditdlg.notebook ),
967                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
968
969         /* Split into two areas */
970         hbox = gtk_hbox_new( FALSE, 0 );
971         gtk_container_add( GTK_CONTAINER( vbox ), hbox );
972
973         /* Address list */
974         vboxl = gtk_vbox_new( FALSE, 4 );
975         gtk_container_add( GTK_CONTAINER( hbox ), vboxl );
976         gtk_container_set_border_width( GTK_CONTAINER(vboxl), 4 );
977
978         clist_swin = gtk_scrolled_window_new( NULL, NULL );
979         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
980                                        GTK_POLICY_AUTOMATIC,
981                                        GTK_POLICY_AUTOMATIC);
982
983         clist = gtk_clist_new_with_titles( EMAIL_N_COLS, titles );
984
985         gtk_container_add( GTK_CONTAINER(clist_swin), clist );
986         gtk_clist_set_selection_mode( GTK_CLIST(clist), GTK_SELECTION_BROWSE );
987         gtk_clist_set_column_width( GTK_CLIST(clist), EMAIL_COL_EMAIL, EMAIL_COL_WIDTH_EMAIL );
988         gtk_clist_set_column_width( GTK_CLIST(clist), EMAIL_COL_ALIAS, EMAIL_COL_WIDTH_ALIAS );
989
990         for( i = 0; i < EMAIL_N_COLS; i++ )
991                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button, GTK_CAN_FOCUS);
992
993         /* Data entry area */
994         table = gtk_table_new( 4, 2, FALSE);
995
996 #ifndef MAEMO
997         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
998         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
999 #else
1000         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1001         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
1002         gtk_clist_column_titles_hide(GTK_CLIST(clist));
1003 #endif
1004         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
1005         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1006         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
1007
1008         entry_email = gtk_entry_new();
1009         entry_alias = gtk_entry_new();
1010         entry_remarks = gtk_entry_new();
1011
1012         /* First row */
1013         top = 0;
1014         label = gtk_label_new(_("Email Address"));
1015         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1016         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1017
1018         gtk_table_attach(GTK_TABLE(table), entry_email, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1019
1020 #ifndef MAEMO
1021         /* Next row */
1022         ++top;
1023         label = gtk_label_new(_("Alias"));
1024         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1025         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1026
1027         gtk_table_attach(GTK_TABLE(table), entry_alias, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1028
1029         /* Next row */
1030         ++top;
1031         label = gtk_label_new(_("Remarks"));
1032         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1033         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1034
1035         gtk_table_attach(GTK_TABLE(table), entry_remarks, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1036 #endif
1037
1038         /* Button box */
1039         vboxb = gtk_vbox_new( FALSE, 4 );
1040         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
1041
1042         vbuttonbox = gtk_vbutton_box_new();
1043         gtk_button_box_set_layout( GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START );
1044         gtk_box_set_spacing( GTK_BOX(vbuttonbox), 8 );
1045         gtk_container_set_border_width( GTK_CONTAINER(vbuttonbox), 4 );
1046         gtk_container_add( GTK_CONTAINER(vboxb), vbuttonbox );
1047
1048         /* Buttons */
1049         buttonUp = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
1050         buttonDown = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
1051         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1052         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
1053         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
1054         
1055
1056 #ifndef MAEMO
1057         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonUp );
1058
1059         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDown );
1060 #endif
1061         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDel );
1062
1063         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonMod );
1064
1065         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonAdd );
1066
1067         gtk_widget_show_all(vbox);
1068
1069         /* Event handlers */
1070         g_signal_connect( G_OBJECT(clist), "select_row",
1071                           G_CALLBACK( edit_person_email_list_selected), NULL );
1072         g_signal_connect( G_OBJECT(buttonUp), "clicked",
1073                           G_CALLBACK( edit_person_email_move_up ), NULL );
1074         g_signal_connect( G_OBJECT(buttonDown), "clicked",
1075                           G_CALLBACK( edit_person_email_move_down ), NULL );
1076         g_signal_connect( G_OBJECT(buttonDel), "clicked",
1077                           G_CALLBACK( edit_person_email_delete ), NULL );
1078         g_signal_connect( G_OBJECT(buttonMod), "clicked",
1079                           G_CALLBACK( edit_person_email_modify ), NULL );
1080         g_signal_connect( G_OBJECT(buttonAdd), "clicked",
1081                           G_CALLBACK( edit_person_email_add ), NULL );
1082         g_signal_connect(G_OBJECT(entry_email), "changed",
1083                          G_CALLBACK(edit_person_entry_email_changed), NULL);
1084         g_signal_connect(G_OBJECT(entry_email), "key_press_event",
1085                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1086         g_signal_connect(G_OBJECT(entry_alias), "key_press_event",
1087                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1088         g_signal_connect(G_OBJECT(entry_remarks), "key_press_event",
1089                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1090
1091         personeditdlg.clist_email   = clist;
1092         personeditdlg.entry_email   = entry_email;
1093         personeditdlg.entry_alias   = entry_alias;
1094         personeditdlg.entry_remarks = entry_remarks;
1095         personeditdlg.email_up = buttonUp;
1096         personeditdlg.email_down = buttonDown;
1097         personeditdlg.email_del = buttonDel;
1098         personeditdlg.email_mod = buttonMod;
1099         personeditdlg.email_add = buttonAdd;
1100 }
1101
1102 static gboolean attrib_adding = FALSE, attrib_saving = FALSE;
1103
1104 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
1105 {
1106         gboolean non_empty = gtk_clist_get_row_data(GTK_CLIST(personeditdlg.clist_attrib), 0) != NULL;
1107
1108         if (personeditdlg.read_only || personeditdlg.ldap)
1109                 return;
1110
1111         if (gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_atname)) == NULL
1112         ||  strlen(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_atname))) == 0) {
1113                 gtk_widget_set_sensitive(personeditdlg.attrib_add,FALSE);
1114                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,FALSE);
1115                 attrib_adding = FALSE;
1116                 attrib_saving = FALSE;
1117         } else if (list_find_attribute(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_atname)))) {
1118                 gtk_widget_set_sensitive(personeditdlg.attrib_add,FALSE);
1119                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,non_empty);
1120                 attrib_adding = FALSE;
1121                 attrib_saving = non_empty;
1122         } else {
1123                 gtk_widget_set_sensitive(personeditdlg.attrib_add,TRUE);
1124                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,non_empty);
1125                 attrib_adding = TRUE;
1126                 attrib_saving = non_empty;
1127         }
1128 }
1129
1130 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
1131 {
1132         if (event && event->keyval == GDK_Return) {
1133                 if (attrib_saving)
1134                         edit_person_attrib_modify(NULL);
1135                 else if (attrib_adding)
1136                         edit_person_attrib_add(NULL);
1137         }
1138         return FALSE;
1139 }
1140
1141 static void addressbook_edit_person_page_attrib( gint pageNum, gchar *pageLbl ) {
1142         GtkWidget *vbox;
1143         GtkWidget *hbox;
1144         GtkWidget *vboxl;
1145         GtkWidget *vboxb;
1146         GtkWidget *vbuttonbox;
1147         GtkWidget *buttonDel;
1148         GtkWidget *buttonMod;
1149         GtkWidget *buttonAdd;
1150
1151         GtkWidget *table;
1152         GtkWidget *label;
1153         GtkWidget *clist_swin;
1154         GtkWidget *clist;
1155         GtkWidget *entry_name;
1156         GtkWidget *entry_value;
1157         gint top;
1158
1159         gchar *titles[ ATTRIB_N_COLS ];
1160         gint i;
1161
1162         titles[ ATTRIB_COL_NAME  ] = _("Name");
1163         titles[ ATTRIB_COL_VALUE ] = _("Value");
1164
1165         vbox = gtk_vbox_new( FALSE, 8 );
1166         gtk_widget_show( vbox );
1167         gtk_container_add( GTK_CONTAINER( personeditdlg.notebook ), vbox );
1168         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
1169
1170         label = gtk_label_new_with_mnemonic( pageLbl );
1171         gtk_widget_show( label );
1172         gtk_notebook_set_tab_label(
1173                 GTK_NOTEBOOK( personeditdlg.notebook ),
1174                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
1175
1176         /* Split into two areas */
1177         hbox = gtk_hbox_new( FALSE, 0 );
1178         gtk_container_add( GTK_CONTAINER( vbox ), hbox );
1179
1180         /* Attribute list */
1181         vboxl = gtk_vbox_new( FALSE, 4 );
1182         gtk_container_add( GTK_CONTAINER( hbox ), vboxl );
1183         gtk_container_set_border_width( GTK_CONTAINER(vboxl), 4 );
1184
1185         clist_swin = gtk_scrolled_window_new( NULL, NULL );
1186         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
1187                                        GTK_POLICY_AUTOMATIC,
1188                                        GTK_POLICY_AUTOMATIC);
1189
1190         clist = gtk_clist_new_with_titles( ATTRIB_N_COLS, titles );
1191         gtk_container_add( GTK_CONTAINER(clist_swin), clist );
1192         gtk_clist_set_selection_mode( GTK_CLIST(clist), GTK_SELECTION_BROWSE );
1193         gtk_clist_set_compare_func( GTK_CLIST(clist), edit_person_attrib_compare_func );
1194         gtk_clist_set_auto_sort( GTK_CLIST(clist), TRUE );
1195         gtk_clist_set_column_width( GTK_CLIST(clist), ATTRIB_COL_NAME, ATTRIB_COL_WIDTH_NAME );
1196         gtk_clist_set_column_width( GTK_CLIST(clist), ATTRIB_COL_VALUE, ATTRIB_COL_WIDTH_VALUE );
1197
1198         for( i = 0; i < ATTRIB_N_COLS; i++ )
1199                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button, GTK_CAN_FOCUS);
1200
1201         /* Data entry area */
1202 #ifndef MAEMO
1203         table = gtk_table_new( 4, 2, FALSE);
1204         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
1205         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1206 #else
1207         table = gtk_table_new( 2, 4, FALSE);
1208         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1209         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
1210         gtk_clist_column_titles_hide(GTK_CLIST(clist));
1211 #endif
1212         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
1213         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1214         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
1215
1216         /* First row */
1217         top = 0;
1218 #ifndef MAEMO
1219         label = gtk_label_new(_("Name"));
1220         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1221         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1222
1223         entry_name = gtk_entry_new();
1224         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1225
1226         /* Next row */
1227         ++top;
1228         label = gtk_label_new(_("Value"));
1229         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1230         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1231
1232         entry_value = gtk_entry_new();
1233         gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1234 #else
1235         label = gtk_label_new(_("Name"));
1236         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
1237         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1238
1239         entry_name = gtk_entry_new();
1240         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, 0, 1, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1241
1242         /* Next row */
1243         ++top;
1244         label = gtk_label_new(_("Value"));
1245         gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
1246         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1247
1248         entry_value = gtk_entry_new();
1249         gtk_table_attach(GTK_TABLE(table), entry_value, 3, 4, 0, 1, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1250 #endif
1251         /* Button box */
1252         vboxb = gtk_vbox_new( FALSE, 4 );
1253         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
1254
1255         vbuttonbox = gtk_vbutton_box_new();
1256         gtk_button_box_set_layout( GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START );
1257         gtk_box_set_spacing( GTK_BOX(vbuttonbox), 8 );
1258         gtk_container_set_border_width( GTK_CONTAINER(vbuttonbox), 4 );
1259         gtk_container_add( GTK_CONTAINER(vboxb), vbuttonbox );
1260
1261         /* Buttons */
1262         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1263         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDel );
1264
1265         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
1266         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonMod );
1267
1268         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
1269         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonAdd );
1270         
1271         gtk_widget_set_sensitive(buttonDel,FALSE);
1272         gtk_widget_set_sensitive(buttonMod,FALSE);
1273         gtk_widget_set_sensitive(buttonAdd,FALSE);
1274
1275         gtk_widget_show_all(vbox);
1276
1277         /* Event handlers */
1278         g_signal_connect( G_OBJECT(clist), "select_row",
1279                           G_CALLBACK( edit_person_attrib_list_selected), NULL );
1280         g_signal_connect( G_OBJECT(buttonDel), "clicked",
1281                           G_CALLBACK( edit_person_attrib_delete ), NULL );
1282         g_signal_connect( G_OBJECT(buttonMod), "clicked",
1283                           G_CALLBACK( edit_person_attrib_modify ), NULL );
1284         g_signal_connect( G_OBJECT(buttonAdd), "clicked",
1285                           G_CALLBACK( edit_person_attrib_add ), NULL );
1286         g_signal_connect(G_OBJECT(entry_name), "changed",
1287                          G_CALLBACK(edit_person_entry_att_changed), NULL);
1288         g_signal_connect(G_OBJECT(entry_name), "key_press_event",
1289                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
1290         g_signal_connect(G_OBJECT(entry_value), "key_press_event",
1291                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
1292
1293         personeditdlg.clist_attrib  = clist;
1294         personeditdlg.entry_atname  = entry_name;
1295         personeditdlg.entry_atvalue = entry_value;
1296         personeditdlg.attrib_add = buttonAdd;
1297         personeditdlg.attrib_del = buttonDel;
1298         personeditdlg.attrib_mod = buttonMod;
1299 }
1300
1301 static void addressbook_edit_person_create( GtkWidget *parent, gboolean *cancelled ) {
1302         if (prefs_common.addressbook_use_editaddress_dialog)
1303                 addressbook_edit_person_dialog_create( cancelled );
1304         else
1305                 addressbook_edit_person_widgetset_create( parent, cancelled );
1306         addressbook_edit_person_page_basic( PAGE_BASIC, _( "_User Data" ) );
1307         addressbook_edit_person_page_email( PAGE_EMAIL, _( "_Email Addresses" ) );
1308 #ifdef USE_LDAP
1309         if (personeditdlg.ldap)
1310                 addressbook_edit_person_page_attrib_ldap(&personeditdlg, PAGE_ATTRIBUTES, _("O_ther Attributes"));
1311         else
1312 #endif
1313                 addressbook_edit_person_page_attrib( PAGE_ATTRIBUTES, _( "O_ther Attributes" ) );
1314         gtk_widget_show_all( personeditdlg.container );
1315 }
1316
1317 /*
1318 * Return list of email items.
1319 */
1320 static GList *edit_person_build_email_list() {
1321         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
1322         GList *listEMail = NULL;
1323         ItemEMail *email;
1324         gint row = 0;
1325         while( (email = gtk_clist_get_row_data( clist, row )) ) {
1326                 listEMail = g_list_append( listEMail, email );
1327                 row++;
1328         }
1329         return listEMail;
1330 }
1331
1332 /*
1333 * Return list of attributes.
1334 */
1335 static GList *edit_person_build_attrib_list() {
1336         GtkCList *clist = GTK_CLIST(personeditdlg.clist_attrib);
1337         GList *listAttrib = NULL;
1338         UserAttribute *attrib;
1339         gint row = 0;
1340         while( (attrib = gtk_clist_get_row_data( clist, row )) ) {
1341                 listAttrib = g_list_append( listAttrib, attrib );
1342                 row++;
1343         }
1344         return listAttrib;
1345 }
1346
1347 static void update_sensitivity(void)
1348 {
1349         gtk_widget_set_sensitive(personeditdlg.entry_name,    !personeditdlg.read_only);
1350         gtk_widget_set_sensitive(personeditdlg.entry_first,   !personeditdlg.read_only);
1351         gtk_widget_set_sensitive(personeditdlg.entry_last,    !personeditdlg.read_only);
1352         gtk_widget_set_sensitive(personeditdlg.entry_nick,    !personeditdlg.read_only && !personeditdlg.ldap);
1353         gtk_widget_set_sensitive(personeditdlg.entry_email,   !personeditdlg.read_only);
1354         gtk_widget_set_sensitive(personeditdlg.entry_alias,   !personeditdlg.read_only && !personeditdlg.ldap);
1355         gtk_widget_set_sensitive(personeditdlg.entry_remarks, !personeditdlg.read_only && !personeditdlg.ldap);
1356         gtk_widget_set_sensitive(personeditdlg.email_up,      !personeditdlg.read_only);
1357         gtk_widget_set_sensitive(personeditdlg.email_down,    !personeditdlg.read_only);
1358         gtk_widget_set_sensitive(personeditdlg.email_del,     !personeditdlg.read_only);
1359         gtk_widget_set_sensitive(personeditdlg.email_mod,     !personeditdlg.read_only);
1360         gtk_widget_set_sensitive(personeditdlg.email_add,     !personeditdlg.read_only);
1361         gtk_widget_set_sensitive(personeditdlg.entry_atname,  !personeditdlg.read_only);
1362         gtk_widget_set_sensitive(personeditdlg.entry_atvalue, !personeditdlg.read_only);
1363         gtk_widget_set_sensitive(personeditdlg.attrib_add,    !personeditdlg.read_only);
1364         gtk_widget_set_sensitive(personeditdlg.attrib_del,    !personeditdlg.read_only);
1365         gtk_widget_set_sensitive(personeditdlg.attrib_mod,    !personeditdlg.read_only);
1366 }
1367
1368 static void addressbook_edit_person_flush_transient( void )
1369 {
1370         ItemPerson *person = current_person;
1371         EditAddressPostUpdateCallback callback = edit_person_close_post_update_cb;
1372
1373         /* reset transient data */
1374         current_abf = NULL;
1375         current_person = NULL;
1376         current_parent_folder = NULL;
1377         edit_person_close_post_update_cb = NULL;
1378
1379         /* post action to perform on addressbook side */
1380         if (callback)
1381                 callback( person );
1382 }
1383
1384 void addressbook_edit_person_invalidate( AddressBookFile *abf, ItemFolder *parent_folder,
1385                                                                                  ItemPerson *person )
1386 {
1387         if (current_abf == NULL &&
1388                 current_person == NULL &&
1389                 current_parent_folder == NULL)
1390                 /* edit address form is already hidden */
1391                 return;
1392
1393         /* unconditional invalidation or invalidating the currently edited item */
1394         if ( ( abf == NULL && person == NULL && parent_folder == NULL )
1395                 || (current_abf == abf ||
1396                         current_person == person ||
1397                         current_parent_folder == parent_folder))
1398                 addressbook_edit_person_close( TRUE );
1399 }
1400
1401 static gboolean addressbook_edit_person_close( gboolean cancelled )
1402 {
1403         GList *listEMail = NULL;
1404         GList *listAttrib = NULL;
1405         GError *error = NULL;
1406         gchar *cn = NULL;
1407
1408         listEMail = edit_person_build_email_list();
1409         listAttrib = edit_person_build_attrib_list();
1410         if( cancelled ) {
1411                 addritem_free_list_email( listEMail );
1412                 addritem_free_list_attribute( listAttrib );
1413                 gtk_clist_clear( GTK_CLIST(personeditdlg.clist_email) );
1414                 gtk_clist_clear( GTK_CLIST(personeditdlg.clist_attrib) );
1415
1416                 if (!prefs_common.addressbook_use_editaddress_dialog)
1417                         gtk_widget_hide( personeditdlg.container );
1418
1419                 /* no callback, as we're discarding the form */
1420                 edit_person_close_post_update_cb = NULL;
1421                 addressbook_edit_person_flush_transient();
1422
1423                 /* set focus to the address list (this is done by the post_update_cb usually) */
1424                 addressbook_address_list_set_focus();
1425                 return FALSE;
1426         }
1427
1428 #ifndef MAEMO
1429         cn = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_name), 0, -1 );
1430 #else
1431         {
1432                 gchar *first = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_first), 0, -1 );
1433                 gchar *last = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_last), 0, -1 );
1434                 cn = g_strdup_printf("%s%s%s", first, (first && last && *first && *last)?" ":"", last);
1435                 g_free(first);
1436                 g_free(last);
1437         }
1438 #endif
1439         if( current_person && current_abf ) {
1440                 /* Update email/attribute list for existing current_person */
1441                 addrbook_update_address_list( current_abf, current_person, listEMail );
1442                 addrbook_update_attrib_list( current_abf, current_person, listAttrib );
1443         }
1444         else {
1445                 /* Create new current_person and email/attribute list */
1446                 if( cn == NULL || *cn == '\0' ) {
1447                         /* Wasting our time */
1448                         if( listEMail == NULL && listAttrib == NULL ) cancelled = TRUE;
1449                 }
1450                 if( ! cancelled && current_abf ) {
1451                         current_person = addrbook_add_address_list( current_abf, current_parent_folder, listEMail );
1452                         addrbook_add_attrib_list( current_abf, current_person, listAttrib );
1453                 }
1454         }
1455         listEMail = NULL;
1456         listAttrib = NULL;
1457
1458         if( ! cancelled ) {
1459                 /* Set current_person stuff */          
1460
1461                 gchar *name;
1462                 addritem_person_set_common_name( current_person, cn );
1463                 if (personeditdlg.picture_set) {
1464                         GdkPixbuf * pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(personeditdlg.image));
1465                         name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1466                                                         ADDRITEM_ID(current_person), ".png", NULL );
1467                         gdk_pixbuf_save(pixbuf, name, "png", &error, NULL);
1468                         if (error) {
1469                                 alertpanel_error(_("Failed to save image: \n%s"),
1470                                                 error->message);
1471                                 g_error_free(error);
1472                         }
1473                         addritem_person_set_picture( current_person, ADDRITEM_ID(current_person) ) ;
1474                         g_free( name );
1475                 } else {
1476                         name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1477                                                         ADDRITEM_ID(current_person), ".png", NULL );
1478                         g_unlink(name);
1479                         g_free(name);
1480                 }
1481                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_first), 0, -1 );
1482                 addritem_person_set_first_name( current_person, name );
1483                 g_free( name );
1484                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_last), 0, -1 );
1485                 addritem_person_set_last_name( current_person, name );
1486                 g_free( name );
1487                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_nick), 0, -1 );
1488                 addritem_person_set_nick_name( current_person, name );
1489                 g_free( name );
1490         }
1491         g_free( cn );
1492
1493         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_email) );
1494         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_attrib) );
1495
1496         if (!prefs_common.addressbook_use_editaddress_dialog)
1497                 gtk_widget_hide( personeditdlg.container );
1498
1499         addressbook_edit_person_flush_transient();
1500
1501         return TRUE;
1502 }
1503  
1504 /*
1505 * Edit person.
1506 * Enter: abf    Address book.
1507 *        parent Parent folder for person (or NULL if adding to root folder). Argument is
1508 *               only required for new objects).
1509 *        person Person to edit, or NULL for a new person object.
1510 *        pgMail If TRUE, E-Mail page will be activated.
1511 * Return: Edited object, or NULL if cancelled.*/
1512 ItemPerson *addressbook_edit_person( AddressBookFile *abf, ItemFolder *parent_folder, ItemPerson *person,
1513                                                                          gboolean pgMail, GtkWidget *parent_container,
1514                                                                          void (*post_update_cb) (ItemPerson *person),
1515                                                                          gboolean get_focus) {
1516         static gboolean cancelled;
1517         GError *error = NULL;
1518         GdkPixbuf *pixbuf = NULL;
1519         /* set transient data */
1520         current_abf = abf;
1521         current_person = person;
1522         current_parent_folder = parent_folder;
1523         edit_person_close_post_update_cb = post_update_cb;
1524         personeditdlg.ldap = (abf && abf->type == ADBOOKTYPE_LDAP)? TRUE : FALSE;
1525
1526         if( personeditdlg.container ) {
1527                 gtk_widget_destroy(personeditdlg.container);
1528                 personeditdlg.container = NULL;
1529         }
1530         addressbook_edit_person_create(parent_container, &cancelled);
1531
1532         /* typically, get focus when dialog mode is enabled, or when editing a new address */
1533         if( get_focus ) {
1534                 gtk_widget_grab_focus(personeditdlg.ok_btn);
1535                 gtk_widget_grab_focus(personeditdlg.entry_name);
1536         }
1537         
1538         personeditdlg.read_only = (current_abf == NULL);
1539         update_sensitivity();
1540
1541         gtk_widget_show(personeditdlg.container);
1542         if (prefs_common.addressbook_use_editaddress_dialog)
1543                 manage_window_set_transient(GTK_WINDOW(personeditdlg.container));
1544         else
1545                 if (get_focus)
1546                         addressbook_address_list_disable_some_actions();
1547
1548         /* Clear all fields */
1549         personeditdlg.rowIndEMail = -1;
1550         personeditdlg.rowIndAttrib = -1;
1551         edit_person_status_show( "" );
1552         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_email) );
1553         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_attrib) );
1554         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_name), "" );
1555         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_first), "" );
1556         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_last), "" );
1557         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_nick), "" );
1558
1559         personeditdlg.editNew = FALSE;
1560         if( current_person ) {
1561                 gchar *filename;
1562
1563                 if( ADDRITEM_NAME(current_person) )
1564                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_name), ADDRITEM_NAME(person) );
1565
1566                 if( current_person->picture ) { 
1567                         filename = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1568                                                         current_person->picture, ".png", NULL );
1569                         if (is_file_exist(filename)) {
1570                                 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1571                                 if (error) {
1572                                         debug_print("Failed to import image: \n%s",
1573                                                         error->message);
1574                                         g_error_free(error);
1575                                         goto no_img;
1576                                 }
1577                                 personeditdlg.picture_set = TRUE;
1578                         } else {
1579                                 goto no_img;
1580                         }
1581                         gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
1582                 } else {
1583 no_img:
1584                         addressbook_edit_person_clear_picture();
1585                 }
1586                 g_free(filename);
1587                 if (pixbuf) {
1588                         g_object_unref(pixbuf);
1589                         pixbuf = NULL;
1590                 }
1591
1592                 if( current_person->firstName )
1593                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_first), current_person->firstName );
1594                 if( current_person->lastName )
1595                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_last), current_person->lastName );
1596                 if( current_person->nickName )
1597                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_nick), current_person->nickName );
1598                 edit_person_load_email( current_person );
1599                 edit_person_load_attrib( current_person );
1600                 gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_atvalue), "");
1601         }
1602         else {
1603                 personeditdlg.editNew = TRUE;
1604         }
1605
1606         /* Select appropriate start page */
1607         if( pgMail ) {
1608                 gtk_notebook_set_current_page( GTK_NOTEBOOK(personeditdlg.notebook), PAGE_EMAIL );
1609         }
1610         else {
1611                 gtk_notebook_set_current_page( GTK_NOTEBOOK(personeditdlg.notebook), PAGE_BASIC );
1612         }
1613
1614         gtk_clist_select_row( GTK_CLIST(personeditdlg.clist_email), 0, 0 );
1615         gtk_clist_select_row( GTK_CLIST(personeditdlg.clist_attrib), 0, 0 );
1616         edit_person_email_clear( NULL );
1617         if (current_person)
1618                 edit_person_email_list_selected(GTK_CLIST(personeditdlg.clist_email), 0, 0, NULL, NULL);
1619
1620         edit_person_attrib_clear( NULL );
1621
1622         if (prefs_common.addressbook_use_editaddress_dialog) {
1623
1624         gtk_main();
1625                 gtk_widget_hide( personeditdlg.container );
1626
1627                 if (!addressbook_edit_person_close( cancelled ))
1628                         return NULL;
1629         }
1630
1631         return current_person;
1632 }
1633
1634 /*
1635 * End of Source.
1636 */
1637