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