2007-10-10 [colin] 3.0.2cvs53
[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 static void addressbook_edit_person_unset_picture (void *obj, guint action, void *data)
702 {
703         GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
704         gdk_pixbuf_fill(pixbuf, 0xffffff00);
705         personeditdlg.picture_set = FALSE;
706         gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
707         g_object_unref(pixbuf);
708 }
709
710 static GtkItemFactoryEntry editaddr_popup_entries[] =
711 {
712         {N_("/_Unset picture"),         NULL, addressbook_edit_person_unset_picture, 0, NULL},
713 };
714
715 static void addressbook_edit_person_set_picture(GtkWidget *widget, 
716                 GdkEventButton *event, gpointer data)
717 {
718         GError *error = NULL;
719         gchar *filename;
720         int width, height, scalewidth, scaleheight;
721         
722         if (event->button == 1) {
723                 if ( (filename = filesel_select_file_open(_("Choose a picture"), NULL)) ) {
724                         GdkPixbuf *pixbuf = NULL;
725                         gdk_pixbuf_get_file_info(filename, &width, &height);
726
727                         if ( width > 128 || height > 128 ) {
728                                 if (width > height) {
729                                         scaleheight = (height * 128) / width;
730                                         scalewidth = 128;
731                                 }
732                                 else {
733                                         scalewidth = (width * 128) / height;
734                                         scaleheight = 128;
735                                 }
736                                 pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, 
737                                                 scalewidth, scaleheight, TRUE, &error);
738                         } else {
739                                 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
740                         }
741                         if (error) {
742                                 alertpanel_error(_("Failed to import image: \n%s"),
743                                                 error->message);
744                                 g_error_free(error);
745                         }
746                         personeditdlg.picture_set = TRUE;
747                         g_free(filename);
748                         gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
749                         g_object_unref(pixbuf);
750                 }
751         } else {
752                 gtk_menu_popup(GTK_MENU(personeditdlg.editaddr_popupmenu), 
753                                NULL, NULL, NULL, NULL, 
754                                event->button, event->time);
755         }
756 }
757
758 static void addressbook_edit_person_page_basic( gint pageNum, gchar *pageLbl ) {
759         GtkWidget *vbox;
760         GtkWidget *hbox;
761         GtkWidget *table;
762         GtkWidget *label;
763         GtkWidget *ebox_picture;
764         GtkWidget *frame_picture;
765         GtkWidget *entry_name;
766         GtkWidget *entry_fn;
767         GtkWidget *entry_ln;
768         GtkWidget *entry_nn;
769         const gchar *locale;
770         gint top = 0;
771         gint n_entries;
772         GdkPixbuf *pixbuf;
773         vbox = gtk_vbox_new( FALSE, 20 );
774         hbox = gtk_hbox_new( FALSE, 8 );
775
776         gtk_widget_show( vbox );        
777         
778         /* User's picture */
779         ebox_picture = gtk_event_box_new();
780         frame_picture = gtk_frame_new("Photo");
781         
782         /* Room for a photo */
783         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
784         gdk_pixbuf_fill(pixbuf, 0xffffff00);
785         personeditdlg.picture_set = FALSE;
786         personeditdlg.image = gtk_image_new_from_pixbuf(pixbuf);
787         g_object_unref(pixbuf);
788
789         gtk_container_add(GTK_CONTAINER(ebox_picture), personeditdlg.image);
790         gtk_container_add(GTK_CONTAINER(frame_picture), ebox_picture);  
791         gtk_container_add(GTK_CONTAINER( personeditdlg.notebook ), hbox );
792         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
793         gtk_container_set_border_width( GTK_CONTAINER (hbox), BORDER_WIDTH );
794
795         label = gtk_label_new_with_mnemonic( pageLbl );
796         gtk_widget_show( label );
797         
798         gtk_box_pack_start(GTK_BOX(hbox), frame_picture, TRUE, TRUE, 0);
799         
800         gtk_notebook_set_tab_label(
801                 GTK_NOTEBOOK( personeditdlg.notebook ),
802                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
803         
804         g_signal_connect(G_OBJECT(ebox_picture), "button_press_event", 
805                         G_CALLBACK(addressbook_edit_person_set_picture), NULL);
806
807         n_entries = sizeof(editaddr_popup_entries) /
808                 sizeof(editaddr_popup_entries[0]);
809         personeditdlg.editaddr_popupmenu = menu_create_items(editaddr_popup_entries, n_entries,
810                                       "<EditAddrPopupMenu>", &personeditdlg.editaddr_popupfactory,
811                                       NULL);
812
813         table = gtk_table_new( 3, 3, FALSE);
814
815 #define ATTACH_ROW(text, entry) \
816 { \
817         label = gtk_label_new(text); \
818         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
819                          GTK_FILL, 0, 0, 0); \
820         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
821  \
822         entry = gtk_entry_new(); \
823         gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
824                          GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
825         top++; \
826 }
827
828 #define ATTACH_HIDDEN_ROW(text, entry) \
829 { \
830         entry = gtk_entry_new(); \
831 }
832
833 #ifndef MAEMO
834         ATTACH_ROW(_("Display Name"), entry_name);
835 #else
836         ATTACH_HIDDEN_ROW(_("Display Name"), entry_name);
837 #endif
838         locale = conv_get_current_locale();
839         if (locale &&
840             (!g_ascii_strncasecmp(locale, "ja", 2) ||
841              !g_ascii_strncasecmp(locale, "ko", 2) ||
842              !g_ascii_strncasecmp(locale, "zh", 2))) {
843                 ATTACH_ROW(_("Last Name"), entry_ln);
844                 ATTACH_ROW(_("First Name"), entry_fn);
845         } else {
846                 ATTACH_ROW(_("First Name"), entry_fn);
847                 ATTACH_ROW(_("Last Name"), entry_ln);
848         }
849 #ifndef MAEMO
850         ATTACH_ROW(_("Nickname"), entry_nn);
851 #else
852         ATTACH_HIDDEN_ROW(_("Nickname"), entry_nn);
853 #endif
854
855 #undef ATTACH_ROW
856 #undef ATTACH_HIDDEN_ROW
857         gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
858         gtk_box_pack_end(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
859         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
860         gtk_table_set_row_spacings(GTK_TABLE(table), 15);
861         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
862
863         gtk_widget_show_all(vbox);
864         personeditdlg.entry_name  = entry_name;
865         personeditdlg.entry_first = entry_fn;
866         personeditdlg.entry_last  = entry_ln;
867         personeditdlg.entry_nick  = entry_nn;
868 }
869
870 static gboolean email_adding = FALSE, email_saving = FALSE;
871
872 static void edit_person_entry_email_changed (GtkWidget *entry, gpointer data)
873 {
874         gboolean non_empty = gtk_clist_get_row_data(GTK_CLIST(personeditdlg.clist_email), 0) != NULL;
875
876         if (personeditdlg.read_only)
877                 return;
878
879         if (gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email)) == NULL
880         ||  strlen(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email))) == 0) {
881                 gtk_widget_set_sensitive(personeditdlg.email_add,FALSE);
882                 gtk_widget_set_sensitive(personeditdlg.email_mod,FALSE);
883                 email_adding = FALSE;
884                 email_saving = FALSE;
885         } else if (list_find_email(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_email)))) {
886                 gtk_widget_set_sensitive(personeditdlg.email_add,FALSE);
887                 gtk_widget_set_sensitive(personeditdlg.email_mod,non_empty);
888                 email_adding = FALSE;
889                 email_saving = non_empty;
890         } else {
891                 gtk_widget_set_sensitive(personeditdlg.email_add,TRUE);
892                 gtk_widget_set_sensitive(personeditdlg.email_mod,non_empty);
893                 email_adding = TRUE;
894                 email_saving = non_empty;
895         }
896 }
897
898 static gboolean edit_person_entry_email_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
899 {
900         if (event && event->keyval == GDK_Return) {
901                 if (email_saving)
902                         edit_person_email_modify(NULL);         
903                 else if (email_adding)
904                         edit_person_email_add(NULL);
905         }
906         return FALSE;
907 }
908
909
910 static void addressbook_edit_person_page_email( gint pageNum, gchar *pageLbl ) {
911         GtkWidget *vbox;
912         GtkWidget *hbox;
913         GtkWidget *vboxl;
914         GtkWidget *vboxb;
915         GtkWidget *vbuttonbox;
916         GtkWidget *buttonUp;
917         GtkWidget *buttonDown;
918         GtkWidget *buttonDel;
919         GtkWidget *buttonMod;
920         GtkWidget *buttonAdd;
921
922         GtkWidget *table;
923         GtkWidget *label;
924         GtkWidget *clist_swin;
925         GtkWidget *clist;
926         GtkWidget *entry_email;
927         GtkWidget *entry_alias;
928         GtkWidget *entry_remarks;
929         gint top;
930
931         gchar *titles[ EMAIL_N_COLS ];
932         gint i;
933
934         titles[ EMAIL_COL_EMAIL   ] = _("Email Address");
935 #ifndef MAEMO
936         titles[ EMAIL_COL_ALIAS   ] = _("Alias");
937         titles[ EMAIL_COL_REMARKS ] = _("Remarks");
938 #endif
939         vbox = gtk_vbox_new( FALSE, 8 );
940         gtk_widget_show( vbox );
941         gtk_container_add( GTK_CONTAINER( personeditdlg.notebook ), vbox );
942         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
943
944         label = gtk_label_new_with_mnemonic( pageLbl );
945         gtk_widget_show( label );
946         gtk_notebook_set_tab_label(
947                 GTK_NOTEBOOK( personeditdlg.notebook ),
948                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
949
950         /* Split into two areas */
951         hbox = gtk_hbox_new( FALSE, 0 );
952         gtk_container_add( GTK_CONTAINER( vbox ), hbox );
953
954         /* Address list */
955         vboxl = gtk_vbox_new( FALSE, 4 );
956         gtk_container_add( GTK_CONTAINER( hbox ), vboxl );
957         gtk_container_set_border_width( GTK_CONTAINER(vboxl), 4 );
958
959         clist_swin = gtk_scrolled_window_new( NULL, NULL );
960         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
961                                        GTK_POLICY_AUTOMATIC,
962                                        GTK_POLICY_AUTOMATIC);
963
964         clist = gtk_clist_new_with_titles( EMAIL_N_COLS, titles );
965
966         gtk_container_add( GTK_CONTAINER(clist_swin), clist );
967         gtk_clist_set_selection_mode( GTK_CLIST(clist), GTK_SELECTION_BROWSE );
968         gtk_clist_set_column_width( GTK_CLIST(clist), EMAIL_COL_EMAIL, EMAIL_COL_WIDTH_EMAIL );
969         gtk_clist_set_column_width( GTK_CLIST(clist), EMAIL_COL_ALIAS, EMAIL_COL_WIDTH_ALIAS );
970
971         for( i = 0; i < EMAIL_N_COLS; i++ )
972                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button, GTK_CAN_FOCUS);
973
974         /* Data entry area */
975         table = gtk_table_new( 4, 2, FALSE);
976
977 #ifndef MAEMO
978         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
979         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
980 #else
981         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
982         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
983         gtk_clist_column_titles_hide(GTK_CLIST(clist));
984 #endif
985         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
986         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
987         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
988
989         entry_email = gtk_entry_new();
990         entry_alias = gtk_entry_new();
991         entry_remarks = gtk_entry_new();
992
993         /* First row */
994         top = 0;
995         label = gtk_label_new(_("Email Address"));
996         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
997         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
998
999         gtk_table_attach(GTK_TABLE(table), entry_email, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1000
1001 #ifndef MAEMO
1002         /* Next row */
1003         ++top;
1004         label = gtk_label_new(_("Alias"));
1005         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1006         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1007
1008         gtk_table_attach(GTK_TABLE(table), entry_alias, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1009
1010         /* Next row */
1011         ++top;
1012         label = gtk_label_new(_("Remarks"));
1013         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1014         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1015
1016         gtk_table_attach(GTK_TABLE(table), entry_remarks, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1017 #endif
1018
1019         /* Button box */
1020         vboxb = gtk_vbox_new( FALSE, 4 );
1021         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
1022
1023         vbuttonbox = gtk_vbutton_box_new();
1024         gtk_button_box_set_layout( GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START );
1025         gtk_box_set_spacing( GTK_BOX(vbuttonbox), 8 );
1026         gtk_container_set_border_width( GTK_CONTAINER(vbuttonbox), 4 );
1027         gtk_container_add( GTK_CONTAINER(vboxb), vbuttonbox );
1028
1029         /* Buttons */
1030         buttonUp = gtk_button_new_from_stock(GTK_STOCK_GO_UP);
1031         buttonDown = gtk_button_new_from_stock(GTK_STOCK_GO_DOWN);
1032         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1033         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
1034         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
1035         
1036
1037 #ifndef MAEMO
1038         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonUp );
1039
1040         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDown );
1041 #endif
1042         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDel );
1043
1044         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonMod );
1045
1046         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonAdd );
1047
1048         gtk_widget_show_all(vbox);
1049
1050         /* Event handlers */
1051         g_signal_connect( G_OBJECT(clist), "select_row",
1052                           G_CALLBACK( edit_person_email_list_selected), NULL );
1053         g_signal_connect( G_OBJECT(buttonUp), "clicked",
1054                           G_CALLBACK( edit_person_email_move_up ), NULL );
1055         g_signal_connect( G_OBJECT(buttonDown), "clicked",
1056                           G_CALLBACK( edit_person_email_move_down ), NULL );
1057         g_signal_connect( G_OBJECT(buttonDel), "clicked",
1058                           G_CALLBACK( edit_person_email_delete ), NULL );
1059         g_signal_connect( G_OBJECT(buttonMod), "clicked",
1060                           G_CALLBACK( edit_person_email_modify ), NULL );
1061         g_signal_connect( G_OBJECT(buttonAdd), "clicked",
1062                           G_CALLBACK( edit_person_email_add ), NULL );
1063         g_signal_connect(G_OBJECT(entry_email), "changed",
1064                          G_CALLBACK(edit_person_entry_email_changed), NULL);
1065         g_signal_connect(G_OBJECT(entry_email), "key_press_event",
1066                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1067         g_signal_connect(G_OBJECT(entry_alias), "key_press_event",
1068                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1069         g_signal_connect(G_OBJECT(entry_remarks), "key_press_event",
1070                          G_CALLBACK(edit_person_entry_email_pressed), NULL);
1071
1072         personeditdlg.clist_email   = clist;
1073         personeditdlg.entry_email   = entry_email;
1074         personeditdlg.entry_alias   = entry_alias;
1075         personeditdlg.entry_remarks = entry_remarks;
1076         personeditdlg.email_up = buttonUp;
1077         personeditdlg.email_down = buttonDown;
1078         personeditdlg.email_del = buttonDel;
1079         personeditdlg.email_mod = buttonMod;
1080         personeditdlg.email_add = buttonAdd;
1081 }
1082
1083 static gboolean attrib_adding = FALSE, attrib_saving = FALSE;
1084
1085 static void edit_person_entry_att_changed (GtkWidget *entry, gpointer data)
1086 {
1087         gboolean non_empty = gtk_clist_get_row_data(GTK_CLIST(personeditdlg.clist_attrib), 0) != NULL;
1088
1089         if (personeditdlg.read_only || personeditdlg.ldap)
1090                 return;
1091
1092         if (gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_atname)) == NULL
1093         ||  strlen(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_atname))) == 0) {
1094                 gtk_widget_set_sensitive(personeditdlg.attrib_add,FALSE);
1095                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,FALSE);
1096                 attrib_adding = FALSE;
1097                 attrib_saving = FALSE;
1098         } else if (list_find_attribute(gtk_entry_get_text(GTK_ENTRY(personeditdlg.entry_atname)))) {
1099                 gtk_widget_set_sensitive(personeditdlg.attrib_add,FALSE);
1100                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,non_empty);
1101                 attrib_adding = FALSE;
1102                 attrib_saving = non_empty;
1103         } else {
1104                 gtk_widget_set_sensitive(personeditdlg.attrib_add,TRUE);
1105                 gtk_widget_set_sensitive(personeditdlg.attrib_mod,non_empty);
1106                 attrib_adding = TRUE;
1107                 attrib_saving = non_empty;
1108         }
1109 }
1110
1111 static gboolean edit_person_entry_att_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
1112 {
1113         if (event && event->keyval == GDK_Return) {
1114                 if (attrib_saving)
1115                         edit_person_attrib_modify(NULL);
1116                 else if (attrib_adding)
1117                         edit_person_attrib_add(NULL);
1118         }
1119         return FALSE;
1120 }
1121
1122 static void addressbook_edit_person_page_attrib( gint pageNum, gchar *pageLbl ) {
1123         GtkWidget *vbox;
1124         GtkWidget *hbox;
1125         GtkWidget *vboxl;
1126         GtkWidget *vboxb;
1127         GtkWidget *vbuttonbox;
1128         GtkWidget *buttonDel;
1129         GtkWidget *buttonMod;
1130         GtkWidget *buttonAdd;
1131
1132         GtkWidget *table;
1133         GtkWidget *label;
1134         GtkWidget *clist_swin;
1135         GtkWidget *clist;
1136         GtkWidget *entry_name;
1137         GtkWidget *entry_value;
1138         gint top;
1139
1140         gchar *titles[ ATTRIB_N_COLS ];
1141         gint i;
1142
1143         titles[ ATTRIB_COL_NAME  ] = _("Name");
1144         titles[ ATTRIB_COL_VALUE ] = _("Value");
1145
1146         vbox = gtk_vbox_new( FALSE, 8 );
1147         gtk_widget_show( vbox );
1148         gtk_container_add( GTK_CONTAINER( personeditdlg.notebook ), vbox );
1149         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
1150
1151         label = gtk_label_new_with_mnemonic( pageLbl );
1152         gtk_widget_show( label );
1153         gtk_notebook_set_tab_label(
1154                 GTK_NOTEBOOK( personeditdlg.notebook ),
1155                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( personeditdlg.notebook ), pageNum ), label );
1156
1157         /* Split into two areas */
1158         hbox = gtk_hbox_new( FALSE, 0 );
1159         gtk_container_add( GTK_CONTAINER( vbox ), hbox );
1160
1161         /* Attribute list */
1162         vboxl = gtk_vbox_new( FALSE, 4 );
1163         gtk_container_add( GTK_CONTAINER( hbox ), vboxl );
1164         gtk_container_set_border_width( GTK_CONTAINER(vboxl), 4 );
1165
1166         clist_swin = gtk_scrolled_window_new( NULL, NULL );
1167         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
1168                                        GTK_POLICY_AUTOMATIC,
1169                                        GTK_POLICY_AUTOMATIC);
1170
1171         clist = gtk_clist_new_with_titles( ATTRIB_N_COLS, titles );
1172         gtk_container_add( GTK_CONTAINER(clist_swin), clist );
1173         gtk_clist_set_selection_mode( GTK_CLIST(clist), GTK_SELECTION_BROWSE );
1174         gtk_clist_set_compare_func( GTK_CLIST(clist), edit_person_attrib_compare_func );
1175         gtk_clist_set_auto_sort( GTK_CLIST(clist), TRUE );
1176         gtk_clist_set_column_width( GTK_CLIST(clist), ATTRIB_COL_NAME, ATTRIB_COL_WIDTH_NAME );
1177         gtk_clist_set_column_width( GTK_CLIST(clist), ATTRIB_COL_VALUE, ATTRIB_COL_WIDTH_VALUE );
1178
1179         for( i = 0; i < ATTRIB_N_COLS; i++ )
1180                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button, GTK_CAN_FOCUS);
1181
1182         /* Data entry area */
1183 #ifndef MAEMO
1184         table = gtk_table_new( 4, 2, FALSE);
1185         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
1186         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1187 #else
1188         table = gtk_table_new( 2, 4, FALSE);
1189         gtk_box_pack_start(GTK_BOX(vboxl), table, FALSE, FALSE, 0);
1190         gtk_container_add( GTK_CONTAINER(vboxl), clist_swin );
1191         gtk_clist_column_titles_hide(GTK_CLIST(clist));
1192 #endif
1193         gtk_container_set_border_width( GTK_CONTAINER(table), 4 );
1194         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1195         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
1196
1197         /* First row */
1198         top = 0;
1199 #ifndef MAEMO
1200         label = gtk_label_new(_("Name"));
1201         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1202         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1203
1204         entry_name = gtk_entry_new();
1205         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1206
1207         /* Next row */
1208         ++top;
1209         label = gtk_label_new(_("Value"));
1210         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
1211         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1212
1213         entry_value = gtk_entry_new();
1214         gtk_table_attach(GTK_TABLE(table), entry_value, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1215 #else
1216         label = gtk_label_new(_("Name"));
1217         gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
1218         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1219
1220         entry_name = gtk_entry_new();
1221         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, 0, 1, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1222
1223         /* Next row */
1224         ++top;
1225         label = gtk_label_new(_("Value"));
1226         gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
1227         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1228
1229         entry_value = gtk_entry_new();
1230         gtk_table_attach(GTK_TABLE(table), entry_value, 3, 4, 0, 1, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
1231 #endif
1232         /* Button box */
1233         vboxb = gtk_vbox_new( FALSE, 4 );
1234         gtk_box_pack_start(GTK_BOX(hbox), vboxb, FALSE, FALSE, 2);
1235
1236         vbuttonbox = gtk_vbutton_box_new();
1237         gtk_button_box_set_layout( GTK_BUTTON_BOX(vbuttonbox), GTK_BUTTONBOX_START );
1238         gtk_box_set_spacing( GTK_BOX(vbuttonbox), 8 );
1239         gtk_container_set_border_width( GTK_CONTAINER(vbuttonbox), 4 );
1240         gtk_container_add( GTK_CONTAINER(vboxb), vbuttonbox );
1241
1242         /* Buttons */
1243         buttonDel = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1244         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonDel );
1245
1246         buttonMod = gtk_button_new_from_stock(GTK_STOCK_SAVE);
1247         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonMod );
1248
1249         buttonAdd = gtk_button_new_from_stock(GTK_STOCK_ADD);
1250         gtk_container_add( GTK_CONTAINER(vbuttonbox), buttonAdd );
1251         
1252         gtk_widget_set_sensitive(buttonDel,FALSE);
1253         gtk_widget_set_sensitive(buttonMod,FALSE);
1254         gtk_widget_set_sensitive(buttonAdd,FALSE);
1255
1256         gtk_widget_show_all(vbox);
1257
1258         /* Event handlers */
1259         g_signal_connect( G_OBJECT(clist), "select_row",
1260                           G_CALLBACK( edit_person_attrib_list_selected), NULL );
1261         g_signal_connect( G_OBJECT(buttonDel), "clicked",
1262                           G_CALLBACK( edit_person_attrib_delete ), NULL );
1263         g_signal_connect( G_OBJECT(buttonMod), "clicked",
1264                           G_CALLBACK( edit_person_attrib_modify ), NULL );
1265         g_signal_connect( G_OBJECT(buttonAdd), "clicked",
1266                           G_CALLBACK( edit_person_attrib_add ), NULL );
1267         g_signal_connect(G_OBJECT(entry_name), "changed",
1268                          G_CALLBACK(edit_person_entry_att_changed), NULL);
1269         g_signal_connect(G_OBJECT(entry_name), "key_press_event",
1270                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
1271         g_signal_connect(G_OBJECT(entry_value), "key_press_event",
1272                          G_CALLBACK(edit_person_entry_att_pressed), NULL);
1273
1274         personeditdlg.clist_attrib  = clist;
1275         personeditdlg.entry_atname  = entry_name;
1276         personeditdlg.entry_atvalue = entry_value;
1277         personeditdlg.attrib_add = buttonAdd;
1278         personeditdlg.attrib_del = buttonDel;
1279         personeditdlg.attrib_mod = buttonMod;
1280 }
1281
1282 static void addressbook_edit_person_create( GtkWidget *parent, gboolean *cancelled ) {
1283         if (prefs_common.addressbook_use_editaddress_dialog)
1284                 addressbook_edit_person_dialog_create( cancelled );
1285         else
1286                 addressbook_edit_person_widgetset_create( parent, cancelled );
1287         addressbook_edit_person_page_basic( PAGE_BASIC, _( "_User Data" ) );
1288         addressbook_edit_person_page_email( PAGE_EMAIL, _( "_Email Addresses" ) );
1289 #ifdef USE_LDAP
1290         if (personeditdlg.ldap)
1291                 addressbook_edit_person_page_attrib_ldap(&personeditdlg, PAGE_ATTRIBUTES, _("O_ther Attributes"));
1292         else
1293 #endif
1294                 addressbook_edit_person_page_attrib( PAGE_ATTRIBUTES, _( "O_ther Attributes" ) );
1295         gtk_widget_show_all( personeditdlg.container );
1296 }
1297
1298 /*
1299 * Return list of email items.
1300 */
1301 static GList *edit_person_build_email_list() {
1302         GtkCList *clist = GTK_CLIST(personeditdlg.clist_email);
1303         GList *listEMail = NULL;
1304         ItemEMail *email;
1305         gint row = 0;
1306         while( (email = gtk_clist_get_row_data( clist, row )) ) {
1307                 listEMail = g_list_append( listEMail, email );
1308                 row++;
1309         }
1310         return listEMail;
1311 }
1312
1313 /*
1314 * Return list of attributes.
1315 */
1316 static GList *edit_person_build_attrib_list() {
1317         GtkCList *clist = GTK_CLIST(personeditdlg.clist_attrib);
1318         GList *listAttrib = NULL;
1319         UserAttribute *attrib;
1320         gint row = 0;
1321         while( (attrib = gtk_clist_get_row_data( clist, row )) ) {
1322                 listAttrib = g_list_append( listAttrib, attrib );
1323                 row++;
1324         }
1325         return listAttrib;
1326 }
1327
1328 static void update_sensitivity(void)
1329 {
1330         gtk_widget_set_sensitive(personeditdlg.entry_name,    !personeditdlg.read_only);
1331         gtk_widget_set_sensitive(personeditdlg.entry_first,   !personeditdlg.read_only);
1332         gtk_widget_set_sensitive(personeditdlg.entry_last,    !personeditdlg.read_only);
1333         gtk_widget_set_sensitive(personeditdlg.entry_nick,    !personeditdlg.read_only && !personeditdlg.ldap);
1334         gtk_widget_set_sensitive(personeditdlg.entry_email,   !personeditdlg.read_only);
1335         gtk_widget_set_sensitive(personeditdlg.entry_alias,   !personeditdlg.read_only && !personeditdlg.ldap);
1336         gtk_widget_set_sensitive(personeditdlg.entry_remarks, !personeditdlg.read_only && !personeditdlg.ldap);
1337         gtk_widget_set_sensitive(personeditdlg.email_up,      !personeditdlg.read_only);
1338         gtk_widget_set_sensitive(personeditdlg.email_down,    !personeditdlg.read_only);
1339         gtk_widget_set_sensitive(personeditdlg.email_del,     !personeditdlg.read_only);
1340         gtk_widget_set_sensitive(personeditdlg.email_mod,     !personeditdlg.read_only);
1341         gtk_widget_set_sensitive(personeditdlg.email_add,     !personeditdlg.read_only);
1342         gtk_widget_set_sensitive(personeditdlg.entry_atname,  !personeditdlg.read_only);
1343         gtk_widget_set_sensitive(personeditdlg.entry_atvalue, !personeditdlg.read_only);
1344         gtk_widget_set_sensitive(personeditdlg.attrib_add,    !personeditdlg.read_only);
1345         gtk_widget_set_sensitive(personeditdlg.attrib_del,    !personeditdlg.read_only);
1346         gtk_widget_set_sensitive(personeditdlg.attrib_mod,    !personeditdlg.read_only);
1347 }
1348
1349 static void addressbook_edit_person_flush_transient( void )
1350 {
1351         ItemPerson *person = current_person;
1352         EditAddressPostUpdateCallback callback = edit_person_close_post_update_cb;
1353
1354         /* reset transient data */
1355         current_abf = NULL;
1356         current_person = NULL;
1357         current_parent_folder = NULL;
1358         edit_person_close_post_update_cb = NULL;
1359
1360         /* post action to perform on addressbook side */
1361         if (callback)
1362                 callback( person );
1363 }
1364
1365 void addressbook_edit_person_invalidate( AddressBookFile *abf, ItemFolder *parent_folder,
1366                                                                                  ItemPerson *person )
1367 {
1368         if (current_abf == NULL &&
1369                 current_person == NULL &&
1370                 current_parent_folder == NULL)
1371                 /* edit address form is already hidden */
1372                 return;
1373
1374         /* unconditional invalidation or invalidating the currently edited item */
1375         if ( ( abf == NULL && person == NULL && parent_folder == NULL )
1376                 || (current_abf == abf ||
1377                         current_person == person ||
1378                         current_parent_folder == parent_folder))
1379                 addressbook_edit_person_close( TRUE );
1380 }
1381
1382 static gboolean addressbook_edit_person_close( gboolean cancelled )
1383 {
1384         GList *listEMail = NULL;
1385         GList *listAttrib = NULL;
1386         GError *error;
1387         gchar *cn = NULL;
1388
1389         listEMail = edit_person_build_email_list();
1390         listAttrib = edit_person_build_attrib_list();
1391         if( cancelled ) {
1392                 addritem_free_list_email( listEMail );
1393                 addritem_free_list_attribute( listAttrib );
1394                 gtk_clist_clear( GTK_CLIST(personeditdlg.clist_email) );
1395                 gtk_clist_clear( GTK_CLIST(personeditdlg.clist_attrib) );
1396
1397                 if (!prefs_common.addressbook_use_editaddress_dialog)
1398                         gtk_widget_hide( personeditdlg.container );
1399
1400                 /* no callback, as we're discarding the form */
1401                 edit_person_close_post_update_cb = NULL;
1402                 addressbook_edit_person_flush_transient();
1403
1404                 /* set focus to the address list (this is done by the post_update_cb usually) */
1405                 addressbook_address_list_set_focus();
1406                 return FALSE;
1407         }
1408
1409 #ifndef MAEMO
1410         cn = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_name), 0, -1 );
1411 #else
1412         {
1413                 gchar *first = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_first), 0, -1 );
1414                 gchar *last = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_last), 0, -1 );
1415                 cn = g_strdup_printf("%s%s%s", first, (first && last && *first && *last)?" ":"", last);
1416                 g_free(first);
1417                 g_free(last);
1418         }
1419 #endif
1420         if( current_person && current_abf ) {
1421                 /* Update email/attribute list for existing current_person */
1422                 addrbook_update_address_list( current_abf, current_person, listEMail );
1423                 addrbook_update_attrib_list( current_abf, current_person, listAttrib );
1424         }
1425         else {
1426                 /* Create new current_person and email/attribute list */
1427                 if( cn == NULL || *cn == '\0' ) {
1428                         /* Wasting our time */
1429                         if( listEMail == NULL && listAttrib == NULL ) cancelled = TRUE;
1430                 }
1431                 if( ! cancelled && current_abf ) {
1432                         current_person = addrbook_add_address_list( current_abf, current_parent_folder, listEMail );
1433                         addrbook_add_attrib_list( current_abf, current_person, listAttrib );
1434                 }
1435         }
1436         listEMail = NULL;
1437         listAttrib = NULL;
1438
1439         if( ! cancelled ) {
1440                 /* Set current_person stuff */          
1441
1442                 gchar *name;
1443                 addritem_person_set_common_name( current_person, cn );
1444                 if (personeditdlg.picture_set) {
1445                         GdkPixbuf * pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(personeditdlg.image));
1446                         name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1447                                                         ADDRITEM_ID(current_person), ".png", NULL );
1448                         gdk_pixbuf_save(pixbuf, name, "png", &error, NULL);
1449                         if (error) {
1450                                 alertpanel_error(_("Failed to save image: \n%s"),
1451                                                 error->message);
1452                                 g_error_free(error);
1453                         }
1454                         addritem_person_set_picture( current_person, ADDRITEM_ID(current_person) ) ;
1455                         g_free( name );
1456                 } else {
1457                         name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1458                                                         ADDRITEM_ID(current_person), ".png", NULL );
1459                         g_unlink(name);
1460                         g_free(name);
1461                 }
1462                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_first), 0, -1 );
1463                 addritem_person_set_first_name( current_person, name );
1464                 g_free( name );
1465                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_last), 0, -1 );
1466                 addritem_person_set_last_name( current_person, name );
1467                 g_free( name );
1468                 name = gtk_editable_get_chars( GTK_EDITABLE(personeditdlg.entry_nick), 0, -1 );
1469                 addritem_person_set_nick_name( current_person, name );
1470                 g_free( name );
1471         }
1472         g_free( cn );
1473
1474         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_email) );
1475         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_attrib) );
1476
1477         if (!prefs_common.addressbook_use_editaddress_dialog)
1478                 gtk_widget_hide( personeditdlg.container );
1479
1480         addressbook_edit_person_flush_transient();
1481
1482         return TRUE;
1483 }
1484  
1485 /*
1486 * Edit person.
1487 * Enter: abf    Address book.
1488 *        parent Parent folder for person (or NULL if adding to root folder). Argument is
1489 *               only required for new objects).
1490 *        person Person to edit, or NULL for a new person object.
1491 *        pgMail If TRUE, E-Mail page will be activated.
1492 * Return: Edited object, or NULL if cancelled.*/
1493 ItemPerson *addressbook_edit_person( AddressBookFile *abf, ItemFolder *parent_folder, ItemPerson *person,
1494                                                                          gboolean pgMail, GtkWidget *parent_container,
1495                                                                          void (*post_update_cb) (ItemPerson *person),
1496                                                                          gboolean get_focus) {
1497         static gboolean cancelled;
1498         GError *error = NULL;
1499         GdkPixbuf *pixbuf;
1500         /* set transient data */
1501         current_abf = abf;
1502         current_person = person;
1503         current_parent_folder = parent_folder;
1504         edit_person_close_post_update_cb = post_update_cb;
1505         personeditdlg.ldap = (abf && abf->type == ADBOOKTYPE_LDAP)? TRUE : FALSE;
1506
1507         if( personeditdlg.container ) {
1508                 gtk_widget_destroy(personeditdlg.container);
1509                 personeditdlg.container = NULL;
1510         }
1511         addressbook_edit_person_create(parent_container, &cancelled);
1512
1513         /* typically, get focus when dialog mode is enabled, or when editing a new address */
1514         if( get_focus ) {
1515                 gtk_widget_grab_focus(personeditdlg.ok_btn);
1516                 gtk_widget_grab_focus(personeditdlg.entry_name);
1517         }
1518         
1519         personeditdlg.read_only = (current_abf == NULL);
1520         update_sensitivity();
1521
1522         gtk_widget_show(personeditdlg.container);
1523         if (prefs_common.addressbook_use_editaddress_dialog)
1524                 manage_window_set_transient(GTK_WINDOW(personeditdlg.container));
1525         else
1526                 if (get_focus)
1527                         addressbook_address_list_disable_some_actions();
1528
1529         /* Clear all fields */
1530         personeditdlg.rowIndEMail = -1;
1531         personeditdlg.rowIndAttrib = -1;
1532         edit_person_status_show( "" );
1533         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_email) );
1534         gtk_clist_clear( GTK_CLIST(personeditdlg.clist_attrib) );
1535         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_name), "" );
1536         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_first), "" );
1537         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_last), "" );
1538         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_nick), "" );
1539
1540         personeditdlg.editNew = FALSE;
1541         if( current_person ) {
1542                 if( ADDRITEM_NAME(current_person) )
1543                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_name), ADDRITEM_NAME(person) );
1544
1545                 if( current_person->picture ) { 
1546                         gchar *filename = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
1547                                                         current_person->picture, ".png", NULL );
1548                         if (is_file_exist(filename)) {
1549                                 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
1550                                 if (error) {
1551                                         debug_print("Failed to import image: \n%s",
1552                                                         error->message);
1553                                         g_error_free(error);
1554                                         goto no_img;
1555                                 }
1556                                 personeditdlg.picture_set = TRUE;
1557                         } else {
1558                                 goto no_img;
1559                         }
1560                         g_free(filename);
1561                 } else {
1562 no_img:
1563                         personeditdlg.picture_set = FALSE;
1564                         pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 48, 48);
1565                         gdk_pixbuf_fill(pixbuf, 0xffffff00);
1566                 }
1567                 gtk_image_set_from_pixbuf(GTK_IMAGE(personeditdlg.image), pixbuf);
1568                 g_object_unref(pixbuf);
1569                 pixbuf = NULL;
1570
1571                 if( current_person->firstName )
1572                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_first), current_person->firstName );
1573                 if( current_person->lastName )
1574                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_last), current_person->lastName );
1575                 if( current_person->nickName )
1576                         gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_nick), current_person->nickName );
1577                 edit_person_load_email( current_person );
1578                 edit_person_load_attrib( current_person );
1579                 gtk_entry_set_text(GTK_ENTRY(personeditdlg.entry_atvalue), "");
1580         }
1581         else {
1582                 personeditdlg.editNew = TRUE;
1583         }
1584
1585         /* Select appropriate start page */
1586         if( pgMail ) {
1587                 gtk_notebook_set_current_page( GTK_NOTEBOOK(personeditdlg.notebook), PAGE_EMAIL );
1588         }
1589         else {
1590                 gtk_notebook_set_current_page( GTK_NOTEBOOK(personeditdlg.notebook), PAGE_BASIC );
1591         }
1592
1593         gtk_clist_select_row( GTK_CLIST(personeditdlg.clist_email), 0, 0 );
1594         gtk_clist_select_row( GTK_CLIST(personeditdlg.clist_attrib), 0, 0 );
1595         edit_person_email_clear( NULL );
1596         if (current_person)
1597                 edit_person_email_list_selected(GTK_CLIST(personeditdlg.clist_email), 0, 0, NULL, NULL);
1598
1599         edit_person_attrib_clear( NULL );
1600
1601         if (prefs_common.addressbook_use_editaddress_dialog) {
1602
1603         gtk_main();
1604                 gtk_widget_hide( personeditdlg.container );
1605
1606                 if (!addressbook_edit_person_close( cancelled ))
1607                         return NULL;
1608         }
1609
1610         return current_person;
1611 }
1612
1613 /*
1614 * End of Source.
1615 */
1616