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