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