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