Add export LDIF.
[claws.git] / src / addressbook.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 <gdk/gdkkeysyms.h>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtksignal.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkscrolledwindow.h>
32 #include <gtk/gtkhpaned.h>
33 #include <gtk/gtkhbox.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtkctree.h>
37 #include <gtk/gtkclist.h>
38 #include <gtk/gtktable.h>
39 #include <gtk/gtkhbbox.h>
40 #include <gtk/gtkbutton.h>
41 #include <gtk/gtkmenu.h>
42 #include <gtk/gtkmenuitem.h>
43 #include <gtk/gtkitemfactory.h>
44 #include <string.h>
45 #include <setjmp.h>
46
47 #include "intl.h"
48 #include "main.h"
49 #include "addressbook.h"
50 #include "manage_window.h"
51 #include "prefs_common.h"
52 #include "alertpanel.h"
53 #include "inputdialog.h"
54 #include "menu.h"
55 #include "stock_pixmap.h"
56 #include "xml.h"
57 #include "prefs_gtk.h"
58 #include "procmime.h"
59 #include "utils.h"
60 #include "gtkutils.h"
61 #include "codeconv.h"
62 #include "about.h"
63 #include "addr_compl.h"
64
65 #include "mgutils.h"
66 #include "addressitem.h"
67 #include "addritem.h"
68 #include "addrcache.h"
69 #include "addrbook.h"
70 #include "addrindex.h"
71 #include "addressadd.h"
72 #include "vcard.h"
73 #include "editvcard.h"
74 #include "editgroup.h"
75 #include "editaddress.h"
76 #include "editbook.h"
77 #include "importldif.h"
78 #include "importmutt.h"
79 #include "importpine.h"
80
81 #ifdef USE_JPILOT
82 #include "jpilot.h"
83 #include "editjpilot.h"
84 #endif
85
86 #ifdef USE_LDAP
87 #include <pthread.h>
88 #include "ldapserver.h"
89 #include "editldap.h"
90
91 #define ADDRESSBOOK_LDAP_BUSYMSG "Busy"
92 #endif
93
94 #include "addrselect.h"
95 #include "addrclip.h"
96 #include "addrgather.h"
97 #include "adbookbase.h"
98 #include "exphtmldlg.h"
99 #include "expldifdlg.h"
100
101 typedef enum
102 {
103         COL_NAME        = 0,
104         COL_ADDRESS     = 1,
105         COL_REMARKS     = 2
106 } AddressBookColumnPos;
107
108 #define N_COLS  3
109 #define COL_NAME_WIDTH          164
110 #define COL_ADDRESS_WIDTH       156
111
112 #define COL_FOLDER_WIDTH        170
113 #define ADDRESSBOOK_WIDTH       640
114 #define ADDRESSBOOK_HEIGHT      360
115
116 #define ADDRESSBOOK_MSGBUF_SIZE 2048
117
118 static GdkPixmap *folderxpm;
119 static GdkBitmap *folderxpmmask;
120 static GdkPixmap *folderopenxpm;
121 static GdkBitmap *folderopenxpmmask;
122 static GdkPixmap *groupxpm;
123 static GdkBitmap *groupxpmmask;
124 static GdkPixmap *interfacexpm;
125 static GdkBitmap *interfacexpmmask;
126 static GdkPixmap *bookxpm;
127 static GdkBitmap *bookxpmmask;
128 static GdkPixmap *addressxpm;
129 static GdkBitmap *addressxpmmask;
130 static GdkPixmap *vcardxpm;
131 static GdkBitmap *vcardxpmmask;
132 static GdkPixmap *jpilotxpm;
133 static GdkBitmap *jpilotxpmmask;
134 static GdkPixmap *categoryxpm;
135 static GdkBitmap *categoryxpmmask;
136 static GdkPixmap *ldapxpm;
137 static GdkBitmap *ldapxpmmask;
138
139 /* Message buffer */
140 static gchar addressbook_msgbuf[ ADDRESSBOOK_MSGBUF_SIZE ];
141
142 /* Address list selection */
143 static AddrSelectList *_addressSelect_ = NULL;
144 static AddressClipboard *_clipBoard_ = NULL;
145
146 /* Address index file and interfaces */
147 static AddressIndex *_addressIndex_ = NULL;
148 static GList *_addressInterfaceList_ = NULL;
149 static GList *_addressIFaceSelection_ = NULL;
150 #define ADDRESSBOOK_IFACE_SELECTION "1/y,3/y,4/y,2/n"
151
152 static AddressBook_win addrbook;
153
154 static GHashTable *_addressBookTypeHash_ = NULL;
155 static GList *_addressBookTypeList_ = NULL;
156
157 static void addressbook_create                  (void);
158 static gint addressbook_close                   (void);
159 static void addressbook_button_set_sensitive    (void);
160
161 /* callback functions */
162 static void addressbook_del_clicked             (GtkButton      *button,
163                                                  gpointer        data);
164 static void addressbook_reg_clicked             (GtkButton      *button,
165                                                  gpointer        data);
166 static void addressbook_to_clicked              (GtkButton      *button,
167                                                  gpointer        data);
168 static void addressbook_lup_clicked             (GtkButton      *button,
169                                                  gpointer       data);
170
171 static void addressbook_tree_selected           (GtkCTree       *ctree,
172                                                  GtkCTreeNode   *node,
173                                                  gint            column,
174                                                  gpointer        data);
175 static void addressbook_list_selected           (GtkCList       *clist,
176                                                  gint            row,
177                                                  gint            column,
178                                                  GdkEvent       *event,
179                                                  gpointer        data);
180 static void addressbook_list_row_selected       (GtkCTree       *clist,
181                                                  GtkCTreeNode   *node,
182                                                  gint            column,
183                                                  gpointer        data);
184 static void addressbook_list_row_unselected     (GtkCTree       *clist,
185                                                  GtkCTreeNode   *node,
186                                                  gint            column,
187                                                  gpointer        data);
188 static void addressbook_person_expand_node      (GtkCTree       *ctree,
189                                                  GList          *node,
190                                                  gpointer       *data );
191 static void addressbook_person_collapse_node    (GtkCTree       *ctree,
192                                                  GList          *node,
193                                                  gpointer       *data );
194 static void addressbook_entry_gotfocus          (GtkWidget      *widget);
195
196 #if 0
197 static void addressbook_entry_changed           (GtkWidget      *widget);
198 #endif
199
200 static void addressbook_list_button_pressed     (GtkWidget      *widget,
201                                                  GdkEventButton *event,
202                                                  gpointer        data);
203 static void addressbook_list_button_released    (GtkWidget      *widget,
204                                                  GdkEventButton *event,
205                                                  gpointer        data);
206 static void addressbook_tree_button_pressed     (GtkWidget      *ctree,
207                                                  GdkEventButton *event,
208                                                  gpointer        data);
209 static void addressbook_tree_button_released    (GtkWidget      *ctree,
210                                                  GdkEventButton *event,
211                                                  gpointer        data);
212 static void addressbook_popup_close             (GtkMenuShell   *menu_shell,
213                                                  gpointer        data);
214
215 static void addressbook_new_folder_cb           (gpointer        data,
216                                                  guint           action,
217                                                  GtkWidget      *widget);
218 static void addressbook_new_group_cb            (gpointer        data,
219                                                  guint           action,
220                                                  GtkWidget      *widget);
221 static void addressbook_treenode_edit_cb        (gpointer        data,
222                                                  guint           action,
223                                                  GtkWidget      *widget);
224 static void addressbook_treenode_delete_cb      (gpointer        data,
225                                                  guint           action,
226                                                  GtkWidget      *widget);
227
228 static void addressbook_change_node_name        (GtkCTreeNode   *node,
229                                                  const gchar    *name);
230
231 static void addressbook_new_address_cb          (gpointer        data,
232                                                  guint           action,
233                                                  GtkWidget      *widget);
234 static void addressbook_edit_address_cb         (gpointer        data,
235                                                  guint           action,
236                                                  GtkWidget      *widget);
237 static void addressbook_delete_address_cb       (gpointer        data,
238                                                  guint           action,
239                                                  GtkWidget      *widget);
240
241 static void close_cb                            (gpointer        data,
242                                                  guint           action,
243                                                  GtkWidget      *widget);
244 static void addressbook_file_save_cb            (gpointer        data,
245                                                  guint           action,
246                                                  GtkWidget      *widget);
247
248 /* Data source edit stuff */
249 static void addressbook_new_book_cb             (gpointer        data,
250                                                  guint           action,
251                                                  GtkWidget      *widget);
252 static void addressbook_new_vcard_cb            (gpointer        data,
253                                                  guint           action,
254                                                  GtkWidget      *widget);
255
256 #ifdef USE_JPILOT
257 static void addressbook_new_jpilot_cb           (gpointer        data,
258                                                  guint           action,
259                                                  GtkWidget      *widget);
260 #endif
261
262 #ifdef USE_LDAP
263 static void addressbook_new_ldap_cb             (gpointer        data,
264                                                  guint           action,
265                                                  GtkWidget      *widget);
266 #endif
267
268 static void addressbook_set_clist               (AddressObject  *obj);
269
270 static void addressbook_load_tree               (void);
271 void addressbook_read_file                      (void);
272
273 static GtkCTreeNode *addressbook_add_object     (GtkCTreeNode   *node,
274                                                  AddressObject  *obj);
275 static void addressbook_treenode_remove_item    ( void );
276
277 static AddressDataSource *addressbook_find_datasource
278                                                 (GtkCTreeNode   *node );
279
280 static AddressBookFile *addressbook_get_book_file(void);
281
282 static GtkCTreeNode *addressbook_node_add_folder
283                                                 (GtkCTreeNode   *node,
284                                                 AddressDataSource *ds,
285                                                 ItemFolder      *itemFolder,
286                                                 AddressObjectType otype);
287 static GtkCTreeNode *addressbook_node_add_group (GtkCTreeNode   *node,
288                                                 AddressDataSource *ds,
289                                                 ItemGroup       *itemGroup);
290 static void addressbook_tree_remove_children    (GtkCTree       *ctree,
291                                                 GtkCTreeNode    *parent);
292 static void addressbook_move_nodes_up           (GtkCTree       *ctree,
293                                                 GtkCTreeNode    *node);
294 static GtkCTreeNode *addressbook_find_group_node (GtkCTreeNode  *parent,
295                                                    ItemGroup    *group);
296 static void key_pressed                         (GtkWidget      *widget,
297                                                  GdkEventKey    *event,
298                                                  gpointer        data);
299 static gint addressbook_treenode_compare_func   (GtkCList       *clist,
300                                                  gconstpointer   ptr1,
301                                                  gconstpointer   ptr2);
302 static gint addressbook_list_compare_func       (GtkCList       *clist,
303                                                  gconstpointer   ptr1,
304                                                  gconstpointer   ptr2);
305 static void addressbook_folder_load_one_person  (GtkCTree *clist, 
306                                                  ItemPerson *person,  
307                                                  AddressTypeControlItem *atci, 
308                                                  AddressTypeControlItem *atciMail);
309 static void addressbook_folder_refresh_one_person(GtkCTree *clist, 
310                                                   ItemPerson *person);
311 static void addressbook_folder_remove_one_person(GtkCTree *clist, 
312                                                  ItemPerson *person);
313 static void addressbook_folder_remove_node      (GtkCTree *clist, 
314                                                  GtkCTreeNode *node);
315
316 #ifdef USE_LDAP
317 static void addressbook_ldap_show_message       ( LdapServer *server );
318 #endif
319
320 /* LUT's and IF stuff */
321 static void addressbook_free_treenode           ( gpointer data );
322 AddressTypeControlItem *addrbookctl_lookup      (gint            ot);
323 AddressTypeControlItem *addrbookctl_lookup_iface(AddressIfType   ifType);
324
325 void addrbookctl_build_map                      (GtkWidget      *window);
326 void addrbookctl_build_iflist                   (void);
327 AdapterInterface *addrbookctl_find_interface    (AddressIfType   ifType);
328 void addrbookctl_build_ifselect                 (void);
329
330 static void addrbookctl_free_interface          (AdapterInterface *adapter);
331 static void addrbookctl_free_datasource         (AdapterDSource   *adapter);
332 static void addrbookctl_free_folder             (AdapterFolder    *adapter);
333 static void addrbookctl_free_group              (AdapterGroup     *adapter);
334
335 static void addressbook_list_select_clear       ( void );
336 static void addressbook_list_select_add         ( AddrItemObject    *aio,
337                                                   AddressDataSource *ds );
338 static void addressbook_list_select_remove      ( AddrItemObject    *aio );
339
340 static void addressbook_import_ldif_cb          ( void );
341 static void addressbook_import_mutt_cb          ( void );
342 static void addressbook_import_pine_cb          ( void );
343 static void addressbook_export_html_cb          ( void );
344 static void addressbook_export_ldif_cb          ( void );
345 static void addressbook_clip_cut_cb             ( void );
346 static void addressbook_clip_copy_cb            ( void );
347 static void addressbook_clip_paste_cb           ( void );
348 static void addressbook_clip_paste_address_cb   ( void );
349 static void addressbook_treenode_cut_cb         ( void );
350 static void addressbook_treenode_copy_cb        ( void );
351 static void addressbook_treenode_paste_cb       ( void );
352
353 static GtkItemFactoryEntry addressbook_entries[] =
354 {
355         {N_("/_File"),                  NULL,           NULL, 0, "<Branch>"},
356         {N_("/_File/New _Book"),        "<alt>B",       addressbook_new_book_cb,        0, NULL},
357         {N_("/_File/New _vCard"),       "<alt>D",       addressbook_new_vcard_cb,       0, NULL},
358 #ifdef USE_JPILOT
359         {N_("/_File/New _JPilot"),      "<alt>J",       addressbook_new_jpilot_cb,      0, NULL},
360 #endif
361 #ifdef USE_LDAP
362         {N_("/_File/New _Server"),      "<alt>S",       addressbook_new_ldap_cb,        0, NULL},
363 #endif
364         {N_("/_File/---"),              NULL,           NULL, 0, "<Separator>"},
365         {N_("/_File/_Edit"),            NULL,           addressbook_treenode_edit_cb,   0, NULL},
366         {N_("/_File/_Delete"),          NULL,           addressbook_treenode_delete_cb, 0, NULL},
367         {N_("/_File/---"),              NULL,           NULL, 0, "<Separator>"},
368         {N_("/_File/_Save"),            "<alt>S",       addressbook_file_save_cb,       0, NULL},
369         {N_("/_File/_Close"),           "<alt>W",       close_cb,                       0, NULL},
370         {N_("/_Edit"),                  NULL,           NULL, 0, "<Branch>"},
371         {N_("/_Edit/C_ut"),             "<ctl>X",       addressbook_clip_cut_cb,        0, NULL},
372         {N_("/_Edit/_Copy"),            "<ctl>C",       addressbook_clip_copy_cb,       0, NULL},
373         {N_("/_Edit/_Paste"),           "<ctl>V",       addressbook_clip_paste_cb,      0, NULL},
374         {N_("/_Edit/---"),              NULL,           NULL, 0, "<Separator>"},
375         {N_("/_Edit/Pa_ste Address"),   NULL,           addressbook_clip_paste_address_cb, 0, NULL},
376         {N_("/_Address"),               NULL,           NULL, 0, "<Branch>"},
377         {N_("/_Address/New _Address"),  "<alt>N",       addressbook_new_address_cb,     0, NULL},
378         {N_("/_Address/New _Group"),    "<alt>G",       addressbook_new_group_cb,       0, NULL},
379         {N_("/_Address/New _Folder"),   "<alt>R",       addressbook_new_folder_cb,      0, NULL},
380         {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>"},
381         {N_("/_Address/_Edit"),         "<alt>Return",  addressbook_edit_address_cb,    0, NULL},
382         {N_("/_Address/_Delete"),       NULL,           addressbook_delete_address_cb,  0, NULL},
383         {N_("/_Tools/---"),             NULL,           NULL, 0, "<Separator>"},
384         {N_("/_Tools/Import _LDIF file..."), NULL,      addressbook_import_ldif_cb,     0, NULL},
385         {N_("/_Tools/Import M_utt file..."), NULL,      addressbook_import_mutt_cb,     0, NULL},
386         {N_("/_Tools/Import _Pine file..."), NULL,      addressbook_import_pine_cb,     0, NULL},
387         {N_("/_Tools/---"),             NULL,           NULL, 0, "<Separator>"},
388         {N_("/_Tools/Export _HTML..."), NULL,           addressbook_export_html_cb,     0, NULL},
389         {N_("/_Tools/Export LDI_F..."), NULL,           addressbook_export_ldif_cb,     0, NULL},
390         {N_("/_Help"),                  NULL,           NULL, 0, "<LastBranch>"},
391         {N_("/_Help/_About"),           NULL,           about_show, 0, NULL}
392 };
393
394 static GtkItemFactoryEntry addressbook_tree_popup_entries[] =
395 {
396         {N_("/New _Address"),   NULL, addressbook_new_address_cb, 0, NULL},
397         {N_("/New _Group"),     NULL, addressbook_new_group_cb,   0, NULL},
398         {N_("/New _Folder"),    NULL, addressbook_new_folder_cb,  0, NULL},
399         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
400         {N_("/_Edit"),          NULL, addressbook_treenode_edit_cb,   0, NULL},
401         {N_("/_Delete"),        NULL, addressbook_treenode_delete_cb, 0, NULL},
402         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
403         {N_("/C_ut"),           NULL, addressbook_treenode_cut_cb,    0, NULL},
404         {N_("/_Copy"),          NULL, addressbook_treenode_copy_cb,   0, NULL},
405         {N_("/_Paste"),         NULL, addressbook_treenode_paste_cb,  0, NULL}
406 };
407
408 static GtkItemFactoryEntry addressbook_list_popup_entries[] =
409 {
410         {N_("/New _Address"),   NULL, addressbook_new_address_cb,    0, NULL},
411         {N_("/New _Group"),     NULL, addressbook_new_group_cb,      0, NULL},
412         {N_("/New _Folder"),    NULL, addressbook_new_folder_cb,     0, NULL},
413         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
414         {N_("/_Edit"),          NULL, addressbook_edit_address_cb,   0, NULL},
415         {N_("/_Delete"),        NULL, addressbook_delete_address_cb, 0, NULL},
416         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
417         {N_("/C_ut"),           NULL, addressbook_clip_cut_cb,       0, NULL},
418         {N_("/_Copy"),          NULL, addressbook_clip_copy_cb,      0, NULL},
419         {N_("/_Paste"),         NULL, addressbook_clip_paste_cb,     0, NULL},
420         {N_("/---"),            NULL, NULL, 0, "<Separator>"},
421         {N_("/Pa_ste Address"), NULL, addressbook_clip_paste_address_cb,     0, NULL}
422 };
423
424 /**
425  * Structure of error message table.
426  */
427 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
428 struct _ErrMsgTableEntry {
429         gint    code;
430         gchar   *description;
431 };
432
433 static gchar *_errMsgUnknown_ = N_( "Unknown" );
434
435 /**
436  * Lookup table of error messages for general errors. Note that a NULL
437  * description signifies the end of the table.
438  */
439 static ErrMsgTableEntry _lutErrorsGeneral_[] = {
440         { MGU_SUCCESS,          N_("Success") },
441         { MGU_BAD_ARGS,         N_("Bad arguments") },
442         { MGU_NO_FILE,          N_("File not specified") },
443         { MGU_OPEN_FILE,        N_("Error opening file") },
444         { MGU_ERROR_READ,       N_("Error reading file") },
445         { MGU_EOF,              N_("End of file encountered") },
446         { MGU_OO_MEMORY,        N_("Error allocating memory") },
447         { MGU_BAD_FORMAT,       N_("Bad file format") },
448         { MGU_ERROR_WRITE,      N_("Error writing to file") },
449         { MGU_OPEN_DIRECTORY,   N_("Error opening directory") },
450         { MGU_NO_PATH,          N_("No path specified") },
451         { 0,                    NULL }
452 };
453
454 #ifdef USE_LDAP
455 /**
456  * Lookup table of error messages for LDAP errors.
457  */
458 static ErrMsgTableEntry _lutErrorsLDAP_[] = {
459         { MGU_SUCCESS,          N_("Success") },
460         { MGU_LDAP_CONNECT,     N_("Error connecting to LDAP server") },
461         { MGU_LDAP_INIT,        N_("Error initializing LDAP") },
462         { MGU_LDAP_BIND,        N_("Error binding to LDAP server") },
463         { MGU_LDAP_SEARCH,      N_("Error searching LDAP database") },
464         { MGU_LDAP_TIMEOUT,     N_("Timeout performing LDAP operation") },
465         { MGU_LDAP_CRITERIA,    N_("Error in LDAP search criteria") },
466         { MGU_LDAP_CRITERIA,    N_("Error in LDAP search criteria") },
467         { MGU_LDAP_NOENTRIES,   N_("No LDAP entries found for search criteria") },
468         { 0,                    NULL }
469 };
470 #endif
471
472 /**
473  * Lookup message for specified error code.
474  * \param lut  Lookup table.
475  * \param code Code to lookup.
476  * \return Description associated to code.
477  */
478 static gchar *addressbook_err2string( ErrMsgTableEntry lut[], gint code ) {
479         gchar *desc = NULL;
480         ErrMsgTableEntry entry;
481         gint i;
482
483         for( i = 0; ; i++ ) {
484                 entry = lut[ i ];
485                 if( entry.description == NULL ) break;
486                 if( entry.code == code ) {
487                         desc = entry.description;
488                         break;
489                 }
490         }
491         if( ! desc ) {
492                 desc = _errMsgUnknown_;
493         }
494         return desc;
495 }
496
497 void addressbook_open(Compose *target)
498 {
499         /* Initialize all static members */
500         if( _clipBoard_ == NULL ) {
501                 _clipBoard_ = addrclip_create();
502         }
503         if( _addressIndex_ != NULL ) {
504                 addrclip_set_index( _clipBoard_, _addressIndex_ );
505         }
506         if( _addressSelect_ == NULL ) {
507                 _addressSelect_ = addrselect_list_create();
508         }
509         if (!addrbook.window) {
510                 addressbook_read_file();
511                 addressbook_create();
512                 addressbook_load_tree();
513                 gtk_ctree_select(GTK_CTREE(addrbook.ctree),
514                                  GTK_CTREE_NODE(GTK_CLIST(addrbook.ctree)->row_list));
515         }
516         else {
517                 gtk_widget_hide(addrbook.window);
518         }
519
520         gtk_widget_show_all(addrbook.window);
521         addressbook_set_target_compose(target);
522 }
523
524 void addressbook_destroy() {
525         /* Free up address stuff */
526         if( _addressSelect_ != NULL ) {
527                 addrselect_list_free( _addressSelect_ );
528         }
529         if( _clipBoard_ != NULL ) {
530                 addrclip_free( _clipBoard_ );
531         }
532         if( _addressIndex_ != NULL ) {
533                 addrindex_free_index( _addressIndex_ );
534                 addrindex_teardown( _addressIndex_ );
535         }
536         _addressSelect_ = NULL;
537         _clipBoard_ = NULL;
538         _addressIndex_ = NULL;
539 }
540
541 void addressbook_set_target_compose(Compose *target)
542 {
543         addrbook.target_compose = target;
544         addressbook_button_set_sensitive();
545 }
546
547 Compose *addressbook_get_target_compose(void)
548 {
549         return addrbook.target_compose;
550 }
551
552 void addressbook_refresh(void)
553 {
554         if (addrbook.window) {
555                 if (addrbook.treeSelected) {
556                         gtk_ctree_select(GTK_CTREE(addrbook.ctree),
557                                          addrbook.treeSelected);
558                 }
559         }
560         addressbook_export_to_file();
561 }
562
563 /*
564 * Create the address book widgets. The address book contains two CTree widgets: the
565 * address index tree on the left and the address list on the right.
566 *
567 * The address index tree displays a hierarchy of interfaces and groups. Each node in
568 * this tree is linked to an address Adapter. Adapters have been created for interfaces,
569 * data sources and folder objects.
570 *
571 * The address list displays group, person and email objects. These items are linked
572 * directly to ItemGroup, ItemPerson and ItemEMail objects inside the address book data
573 * sources.
574 *
575 * In the tradition of MVC architecture, the data stores have been separated from the
576 * GUI components. The addrindex.c file provides the interface to all data stores.
577 */
578 static void addressbook_create(void)
579 {
580         GtkWidget *window;
581         GtkWidget *vbox;
582         GtkWidget *menubar;
583         GtkWidget *vbox2;
584         GtkWidget *ctree_swin;
585         GtkWidget *ctree;
586         GtkWidget *clist_vbox;
587         GtkWidget *clist_swin;
588         GtkWidget *clist;
589         GtkWidget *paned;
590         GtkWidget *hbox;
591         GtkWidget *label;
592         GtkWidget *entry;
593         GtkWidget *statusbar;
594         GtkWidget *hbbox;
595         GtkWidget *hsbox;
596         GtkWidget *del_btn;
597         GtkWidget *reg_btn;
598         GtkWidget *lup_btn;
599         GtkWidget *to_btn;
600         GtkWidget *cc_btn;
601         GtkWidget *bcc_btn;
602         GtkWidget *tree_popup;
603         GtkWidget *list_popup;
604         GtkItemFactory *tree_factory;
605         GtkItemFactory *list_factory;
606         GtkItemFactory *menu_factory;
607         gint n_entries;
608         GList *nodeIf;
609
610         gchar *titles[N_COLS];
611         gchar *text;
612         gint i;
613
614         debug_print("Creating addressbook window...\n");
615
616         titles[COL_NAME]    = _("Name");
617         titles[COL_ADDRESS] = _("E-Mail address");
618         titles[COL_REMARKS] = _("Remarks");
619
620         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
621         gtk_window_set_title(GTK_WINDOW(window), _("Address book"));
622         gtk_widget_set_usize(window, ADDRESSBOOK_WIDTH, ADDRESSBOOK_HEIGHT);
623         gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, TRUE);
624         gtk_window_set_wmclass(GTK_WINDOW(window), "addressbook", "Sylpheed");
625         gtk_widget_realize(window);
626
627         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
628                            GTK_SIGNAL_FUNC(addressbook_close), NULL);
629         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
630                            GTK_SIGNAL_FUNC(key_pressed), NULL);
631         MANAGE_WINDOW_SIGNALS_CONNECT(window);
632
633         vbox = gtk_vbox_new(FALSE, 0);
634         gtk_container_add(GTK_CONTAINER(window), vbox);
635
636         n_entries = sizeof(addressbook_entries) /
637                 sizeof(addressbook_entries[0]);
638         menubar = menubar_create(window, addressbook_entries, n_entries,
639                                  "<AddressBook>", NULL);
640         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
641         menu_factory = gtk_item_factory_from_widget(menubar);
642
643         vbox2 = gtk_vbox_new(FALSE, 4);
644         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
645         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
646
647         ctree_swin = gtk_scrolled_window_new(NULL, NULL);
648         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ctree_swin),
649                                        GTK_POLICY_AUTOMATIC,
650                                        GTK_POLICY_ALWAYS);
651         gtk_widget_set_usize(ctree_swin, COL_FOLDER_WIDTH + 40, -1);
652
653         /* Address index */
654         ctree = gtk_ctree_new(1, 0);
655         gtk_container_add(GTK_CONTAINER(ctree_swin), ctree);
656         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
657         gtk_clist_set_column_width(GTK_CLIST(ctree), 0, COL_FOLDER_WIDTH);
658         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
659         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
660                                      GTK_CTREE_EXPANDER_SQUARE);
661         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
662         gtk_clist_set_compare_func(GTK_CLIST(ctree),
663                                    addressbook_treenode_compare_func);
664
665         gtk_signal_connect(GTK_OBJECT(ctree), "tree_select_row",
666                            GTK_SIGNAL_FUNC(addressbook_tree_selected), NULL);
667         gtk_signal_connect(GTK_OBJECT(ctree), "button_press_event",
668                            GTK_SIGNAL_FUNC(addressbook_tree_button_pressed),
669                            NULL);
670         gtk_signal_connect(GTK_OBJECT(ctree), "button_release_event",
671                            GTK_SIGNAL_FUNC(addressbook_tree_button_released),
672                            NULL);
673
674         clist_vbox = gtk_vbox_new(FALSE, 4);
675
676         clist_swin = gtk_scrolled_window_new(NULL, NULL);
677         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
678                                        GTK_POLICY_AUTOMATIC,
679                                        GTK_POLICY_ALWAYS);
680         gtk_box_pack_start(GTK_BOX(clist_vbox), clist_swin, TRUE, TRUE, 0);
681
682         /* Address list */
683         clist = gtk_ctree_new_with_titles(N_COLS, 0, titles);
684         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
685         gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_EXTENDED);
686         gtk_ctree_set_line_style(GTK_CTREE(clist), GTK_CTREE_LINES_NONE);
687         gtk_ctree_set_expander_style(GTK_CTREE(clist), GTK_CTREE_EXPANDER_SQUARE);
688         gtk_ctree_set_indent(GTK_CTREE(clist), CTREE_INDENT);
689         gtk_clist_set_column_width(GTK_CLIST(clist), COL_NAME,
690                                    COL_NAME_WIDTH);
691         gtk_clist_set_column_width(GTK_CLIST(clist), COL_ADDRESS,
692                                    COL_ADDRESS_WIDTH);
693         gtk_clist_set_compare_func(GTK_CLIST(clist),
694                                    addressbook_list_compare_func);
695
696         for (i = 0; i < N_COLS; i++)
697                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
698                                        GTK_CAN_FOCUS);
699
700         gtk_signal_connect(GTK_OBJECT(clist), "tree_select_row",
701                            GTK_SIGNAL_FUNC(addressbook_list_row_selected), NULL);
702         gtk_signal_connect(GTK_OBJECT(clist), "tree_unselect_row",
703                            GTK_SIGNAL_FUNC(addressbook_list_row_unselected), NULL);
704         gtk_signal_connect(GTK_OBJECT(clist), "button_press_event",
705                            GTK_SIGNAL_FUNC(addressbook_list_button_pressed),
706                            NULL);
707         gtk_signal_connect(GTK_OBJECT(clist), "button_release_event",
708                            GTK_SIGNAL_FUNC(addressbook_list_button_released),
709                            NULL);
710         gtk_signal_connect(GTK_OBJECT(clist), "select_row",
711                            GTK_SIGNAL_FUNC(addressbook_list_selected), NULL);
712         gtk_signal_connect(GTK_OBJECT(clist), "tree_expand",
713                            GTK_SIGNAL_FUNC(addressbook_person_expand_node), NULL );
714         gtk_signal_connect(GTK_OBJECT(clist), "tree_collapse",
715                            GTK_SIGNAL_FUNC(addressbook_person_collapse_node), NULL );
716
717         hbox = gtk_hbox_new(FALSE, 4);
718         gtk_box_pack_start(GTK_BOX(clist_vbox), hbox, FALSE, FALSE, 0);
719
720         label = gtk_label_new(_("Name:"));
721         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
722
723         entry = gtk_entry_new();
724         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
725
726         address_completion_register_entry(GTK_ENTRY(entry));
727         gtk_signal_connect(GTK_OBJECT(entry), "focus_in_event",
728                            GTK_SIGNAL_FUNC(addressbook_entry_gotfocus), NULL);
729
730 #if 0
731         gtk_signal_connect(GTK_OBJECT(entry), "changed",
732                            GTK_SIGNAL_FUNC(addressbook_entry_changed), NULL);
733 #endif
734
735         paned = gtk_hpaned_new();
736         gtk_box_pack_start(GTK_BOX(vbox2), paned, TRUE, TRUE, 0);
737         gtk_paned_add1(GTK_PANED(paned), ctree_swin);
738         gtk_paned_add2(GTK_PANED(paned), clist_vbox);
739
740         /* Status bar */
741         hsbox = gtk_hbox_new(FALSE, 0);
742         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
743         statusbar = gtk_statusbar_new();
744         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
745
746         /* Button panel */
747         hbbox = gtk_hbutton_box_new();
748         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
749         gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbbox), 2);
750         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
751
752         del_btn = gtk_button_new_with_label(_("Delete"));
753         GTK_WIDGET_SET_FLAGS(del_btn, GTK_CAN_DEFAULT);
754         gtk_box_pack_start(GTK_BOX(hbbox), del_btn, TRUE, TRUE, 0);
755         reg_btn = gtk_button_new_with_label(_("Add"));
756         GTK_WIDGET_SET_FLAGS(reg_btn, GTK_CAN_DEFAULT);
757         gtk_box_pack_start(GTK_BOX(hbbox), reg_btn, TRUE, TRUE, 0);
758         lup_btn = gtk_button_new_with_label(_("Lookup"));
759         GTK_WIDGET_SET_FLAGS(lup_btn, GTK_CAN_DEFAULT);
760         gtk_box_pack_start(GTK_BOX(hbbox), lup_btn, TRUE, TRUE, 0);
761
762         gtk_signal_connect(GTK_OBJECT(del_btn), "clicked",
763                            GTK_SIGNAL_FUNC(addressbook_del_clicked), NULL);
764         gtk_signal_connect(GTK_OBJECT(reg_btn), "clicked",
765                            GTK_SIGNAL_FUNC(addressbook_reg_clicked), NULL);
766         gtk_signal_connect(GTK_OBJECT(lup_btn), "clicked",
767                            GTK_SIGNAL_FUNC(addressbook_lup_clicked), NULL);
768
769         to_btn = gtk_button_new_with_label
770                 (prefs_common.trans_hdr ? _("To:") : "To:");
771         GTK_WIDGET_SET_FLAGS(to_btn, GTK_CAN_DEFAULT);
772         gtk_box_pack_start(GTK_BOX(hbbox), to_btn, TRUE, TRUE, 0);
773         cc_btn = gtk_button_new_with_label
774                 (prefs_common.trans_hdr ? _("Cc:") : "Cc:");
775         GTK_WIDGET_SET_FLAGS(cc_btn, GTK_CAN_DEFAULT);
776         gtk_box_pack_start(GTK_BOX(hbbox), cc_btn, TRUE, TRUE, 0);
777         bcc_btn = gtk_button_new_with_label
778                 (prefs_common.trans_hdr ? _("Bcc:") : "Bcc:");
779         GTK_WIDGET_SET_FLAGS(bcc_btn, GTK_CAN_DEFAULT);
780         gtk_box_pack_start(GTK_BOX(hbbox), bcc_btn, TRUE, TRUE, 0);
781
782         gtk_signal_connect(GTK_OBJECT(to_btn), "clicked",
783                            GTK_SIGNAL_FUNC(addressbook_to_clicked),
784                            GINT_TO_POINTER(COMPOSE_TO));
785         gtk_signal_connect(GTK_OBJECT(cc_btn), "clicked",
786                            GTK_SIGNAL_FUNC(addressbook_to_clicked),
787                            GINT_TO_POINTER(COMPOSE_CC));
788         gtk_signal_connect(GTK_OBJECT(bcc_btn), "clicked",
789                            GTK_SIGNAL_FUNC(addressbook_to_clicked),
790                            GINT_TO_POINTER(COMPOSE_BCC));
791
792         /* Build icons for interface */
793         stock_pixmap_gdk( window, STOCK_PIXMAP_INTERFACE,
794                           &interfacexpm, &interfacexpmmask );
795
796         /* Build control tables */
797         addrbookctl_build_map(window);
798         addrbookctl_build_iflist();
799         addrbookctl_build_ifselect();
800
801         /* Add each interface into the tree as a root level folder */
802         nodeIf = _addressInterfaceList_;
803         while( nodeIf ) {
804                 AdapterInterface *adapter = nodeIf->data;
805                 AddressInterface *iface = adapter->interface;
806                 nodeIf = g_list_next(nodeIf);
807
808                 if(iface->useInterface) {
809                         AddressTypeControlItem *atci = adapter->atci;
810                         text = atci->displayName;
811                         adapter->treeNode =
812                                 gtk_ctree_insert_node( GTK_CTREE(ctree),
813                                         NULL, NULL, &text, FOLDER_SPACING,
814                                         interfacexpm, interfacexpmmask,
815                                         interfacexpm, interfacexpmmask,
816                                         FALSE, FALSE );
817                         menu_set_sensitive( menu_factory, atci->menuCommand, adapter->haveLibrary );
818                         gtk_ctree_node_set_row_data_full(
819                                 GTK_CTREE(ctree), adapter->treeNode, adapter,
820                                 addressbook_free_treenode );
821                 }
822         }
823
824         /* Popup menu */
825         n_entries = sizeof(addressbook_tree_popup_entries) /
826                 sizeof(addressbook_tree_popup_entries[0]);
827         tree_popup = menu_create_items(addressbook_tree_popup_entries,
828                                        n_entries,
829                                        "<AddressBookTree>", &tree_factory,
830                                        NULL);
831         gtk_signal_connect(GTK_OBJECT(tree_popup), "selection_done",
832                            GTK_SIGNAL_FUNC(addressbook_popup_close), NULL);
833         n_entries = sizeof(addressbook_list_popup_entries) /
834                 sizeof(addressbook_list_popup_entries[0]);
835         list_popup = menu_create_items(addressbook_list_popup_entries,
836                                        n_entries,
837                                        "<AddressBookList>", &list_factory,
838                                        NULL);
839
840         addrbook.window  = window;
841         addrbook.menubar = menubar;
842         addrbook.ctree   = ctree;
843         addrbook.clist   = clist;
844         addrbook.entry   = entry;
845         addrbook.statusbar = statusbar;
846         addrbook.status_cid = gtk_statusbar_get_context_id(
847                         GTK_STATUSBAR(statusbar), "Addressbook Window" );
848
849         addrbook.del_btn = del_btn;
850         addrbook.reg_btn = reg_btn;
851         addrbook.lup_btn = lup_btn;
852         addrbook.to_btn  = to_btn;
853         addrbook.cc_btn  = cc_btn;
854         addrbook.bcc_btn = bcc_btn;
855
856         addrbook.tree_popup   = tree_popup;
857         addrbook.list_popup   = list_popup;
858         addrbook.tree_factory = tree_factory;
859         addrbook.list_factory = list_factory;
860         addrbook.menu_factory = menu_factory;
861
862         addrbook.listSelected = NULL;
863         address_completion_start(window);
864         gtk_widget_show_all(window);
865
866 }
867
868 static gint addressbook_close( void ) {
869         gtk_widget_hide(addrbook.window);
870         addressbook_export_to_file();
871         return TRUE;
872 }
873
874 static void addressbook_status_show( gchar *msg ) {
875         if( addrbook.statusbar != NULL ) {
876                 gtk_statusbar_pop(
877                         GTK_STATUSBAR(addrbook.statusbar),
878                         addrbook.status_cid );
879                 if( msg ) {
880                         gtk_statusbar_push(
881                                 GTK_STATUSBAR(addrbook.statusbar),
882                                 addrbook.status_cid, msg );
883                 }
884         }
885 }
886
887 static void addressbook_ds_status_message( AddressDataSource *ds, gchar *msg ) {
888         *addressbook_msgbuf = '\0';
889         if( ds ) {
890                 gchar *name;
891
892                 name = addrindex_ds_get_name( ds );
893                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
894                             "%s: %s", name, msg );
895         }
896         else {
897                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
898                             "%s", msg );
899         }
900         addressbook_status_show( addressbook_msgbuf );
901 }
902
903 static void addressbook_ds_show_message( AddressDataSource *ds ) {
904         gint retVal;
905         gchar *name;
906         gchar *desc;
907         *addressbook_msgbuf = '\0';
908         if( ds ) {
909                 name = addrindex_ds_get_name( ds );
910                 retVal = addrindex_ds_get_status_code( ds );
911                 if( retVal == MGU_SUCCESS ) {
912                         g_snprintf( addressbook_msgbuf,
913                                     sizeof(addressbook_msgbuf), "%s", name );
914                 }
915                 else {
916                         desc = addressbook_err2string( _lutErrorsGeneral_, retVal );
917                         g_snprintf( addressbook_msgbuf, 
918                             sizeof(addressbook_msgbuf), "%s: %s", name, desc );
919                 }
920         }
921         addressbook_status_show( addressbook_msgbuf );
922 }
923
924 static void addressbook_button_set_sensitive(void)
925 {
926         gboolean to_sens  = FALSE;
927         gboolean cc_sens  = FALSE;
928         gboolean bcc_sens = FALSE;
929
930         if (!addrbook.window) return;
931
932         if (addrbook.target_compose) {
933                 to_sens = TRUE;
934                 cc_sens = TRUE;
935 #ifndef CLAWS           
936                 if (addrbook.target_compose->use_bcc)
937 #endif                  
938                         bcc_sens = TRUE;
939         }
940
941         gtk_widget_set_sensitive(addrbook.lup_btn, FALSE);
942         gtk_widget_set_sensitive(addrbook.to_btn, to_sens);
943         gtk_widget_set_sensitive(addrbook.cc_btn, cc_sens);
944         gtk_widget_set_sensitive(addrbook.bcc_btn, bcc_sens);
945 }
946
947 /*
948 * Delete one or more objects from address list.
949 */
950 static void addressbook_del_clicked(GtkButton *button, gpointer data)
951 {
952         GtkCTree *clist = GTK_CTREE(addrbook.clist);
953         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
954         AddressObject *pobj;
955         AdapterDSource *ads = NULL;
956         GtkCTreeNode *nodeList;
957         gboolean procFlag;
958         AlertValue aval;
959         AddressBookFile *abf = NULL;
960         AddressDataSource *ds = NULL;
961         AddressInterface *iface;
962         AddrItemObject *aio;
963         AddrSelectItem *item;
964         GList *list, *node;
965         gboolean refreshList = FALSE;
966         
967         pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened );
968         g_return_if_fail(pobj != NULL);
969
970         /* Test whether anything selected for deletion */
971         nodeList = addrbook.listSelected;
972         aio = gtk_ctree_node_get_row_data( clist, nodeList );
973         if( aio == NULL) return;
974         ds = addressbook_find_datasource( addrbook.treeSelected );
975         if( ds == NULL ) return;
976
977         /* Test for read only */
978         iface = ds->interface;
979         if( iface->readOnly ) {
980                 alertpanel( _("Delete address(es)"),
981                         _("This address data is readonly and cannot be deleted."),
982                         _("Close"), NULL, NULL );
983                 return;
984         }
985
986         /* Test whether Ok to proceed */
987         procFlag = FALSE;
988         if( pobj->type == ADDR_DATASOURCE ) {
989                 ads = ADAPTER_DSOURCE(pobj);
990                 if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
991         }
992         else if( pobj->type == ADDR_ITEM_FOLDER ) {
993                 procFlag = TRUE;
994         }
995         else if( pobj->type == ADDR_ITEM_GROUP ) {
996                 procFlag = TRUE;
997         }
998         if( ! procFlag ) return;
999         abf = ds->rawDataSource;
1000         if( abf == NULL ) return;
1001
1002         /* Confirm deletion */
1003         aval = alertpanel( _("Delete address(es)"),
1004                         _("Really delete the address(es)?"),
1005                         _("Yes"), _("No"), NULL );
1006         if( aval != G_ALERTDEFAULT ) return;
1007
1008         /* Process deletions */
1009         if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
1010                 /* Items inside folders */
1011                 list = addrselect_get_list( _addressSelect_ );
1012                 node = list;
1013                 while( node ) {
1014                         item = node->data;
1015                         node = g_list_next( node );
1016                         aio = ( AddrItemObject * ) item->addressItem;
1017                         if( aio->type == ADDR_ITEM_GROUP ) {
1018                                 ItemGroup *item = ( ItemGroup * ) aio;
1019                                 GtkCTreeNode *nd = NULL;
1020
1021                                 nd = addressbook_find_group_node( addrbook.opened, item );
1022                                 item = addrbook_remove_group( abf, item );
1023                                 if( item ) {
1024                                         addritem_free_item_group( item );
1025                                 }
1026                                 /* Remove group from parent node */
1027                                 gtk_ctree_remove_node( ctree, nd );
1028                                 refreshList = TRUE;
1029                         }
1030                         else if( aio->type == ADDR_ITEM_PERSON ) {
1031                                 ItemPerson *item = ( ItemPerson * ) aio;
1032                                 addressbook_folder_remove_one_person( clist, item );
1033                                 item = addrbook_remove_person( abf, item );
1034                                 if( item ) {
1035                                         addritem_free_item_person( item );
1036                                 }
1037                         }
1038                         else if( aio->type == ADDR_ITEM_EMAIL ) {
1039                                 ItemEMail *item = ( ItemEMail * ) aio;
1040                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1041                                 item = addrbook_person_remove_email( abf, person, item );
1042                                 if( item ) {
1043                                         addritem_free_item_email( item );
1044                                 }
1045                                 addressbook_folder_refresh_one_person( clist, person );
1046                         }
1047                 }
1048                 g_list_free( list );
1049                 addressbook_list_select_clear();
1050                 if( refreshList ) gtk_ctree_select( ctree, addrbook.opened);
1051                 return;
1052         }
1053         else if( pobj->type == ADDR_ITEM_GROUP ) {
1054                 /* Items inside groups */
1055                 list = addrselect_get_list( _addressSelect_ );
1056                 node = list;
1057                 while( node ) {
1058                         item = node->data;
1059                         node = g_list_next( node );
1060                         aio = ( AddrItemObject * ) item->addressItem;
1061                         if( aio->type == ADDR_ITEM_EMAIL ) {
1062                                 ItemEMail *item = ( ItemEMail * ) aio;
1063                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1064                                 item = addrbook_person_remove_email( abf, person, item );
1065                                 if( item ) {
1066                                         addritem_free_item_email( item );
1067                                 }
1068                         }
1069                 }
1070                 g_list_free( list );
1071                 addressbook_list_select_clear();
1072                 gtk_ctree_select( ctree, addrbook.opened);
1073                 return;
1074         }
1075
1076         gtk_ctree_node_set_row_data( clist, nodeList, NULL );
1077         gtk_ctree_remove_node( clist, nodeList );
1078
1079 }
1080
1081 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
1082 {
1083         addressbook_new_address_cb( NULL, 0, NULL );
1084 }
1085
1086 gchar *addressbook_format_address( AddrItemObject * aio ) {
1087         gchar *buf = NULL;
1088         gchar *name = NULL;
1089         gchar *address = NULL;
1090
1091         if( aio->type == ADDR_ITEM_EMAIL ) {
1092                 ItemPerson *person = NULL;
1093                 ItemEMail *email = ( ItemEMail * ) aio;
1094
1095                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
1096                 if( email->address ) {
1097                         if( ADDRITEM_NAME(email) ) {
1098                                 name = ADDRITEM_NAME(email);
1099                                 if( *name == '\0' ) {
1100                                         name = ADDRITEM_NAME(person);
1101                                 }
1102                         }
1103                         else if( ADDRITEM_NAME(person) ) {
1104                                 name = ADDRITEM_NAME(person);
1105                         }
1106                         else {
1107                                 buf = g_strdup( email->address );
1108                         }
1109                         address = email->address;
1110                 }
1111         }
1112         else if( aio->type == ADDR_ITEM_PERSON ) {
1113                 ItemPerson *person = ( ItemPerson * ) aio;
1114                 GList *node = person->listEMail;
1115
1116                 name = ADDRITEM_NAME(person);
1117                 if( node ) {
1118                         ItemEMail *email = ( ItemEMail * ) node->data;
1119                         address = email->address;
1120                 }
1121         }
1122         if( address ) {
1123                 if( name && name[0] != '\0' ) {
1124                         if( strchr_with_skip_quote( name, '"', ',' ) )
1125                                 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
1126                         else
1127                                 buf = g_strdup_printf( "%s <%s>", name, address );
1128                 }
1129                 else {
1130                         buf = g_strdup( address );
1131                 }
1132         }
1133
1134         return buf;
1135 }
1136
1137 static void addressbook_to_clicked(GtkButton *button, gpointer data)
1138 {
1139         GList *list, *node;
1140         Compose *compose;
1141         AddrSelectItem *item;
1142         AddrItemObject *aio;
1143         gchar *addr;
1144
1145         compose = addrbook.target_compose;
1146         if( ! compose ) return;
1147
1148         /* Nothing selected, but maybe there is something in text entry */
1149         addr = gtk_entry_get_text( GTK_ENTRY( addrbook.entry) );
1150         if ( addr ) {
1151                 compose_entry_append(
1152                         compose, addr, (ComposeEntryType)data );
1153         }
1154
1155         /* Select from address list */
1156         list = addrselect_get_list( _addressSelect_ );
1157         node = list;
1158         while( node ) {
1159                 item = node->data;
1160                 node = g_list_next( node );
1161                 aio = item->addressItem;
1162                 if( aio->type == ADDR_ITEM_PERSON ||
1163                     aio->type == ADDR_ITEM_EMAIL ) {
1164                         addr = addressbook_format_address( aio );
1165                         compose_entry_append(
1166                                 compose, addr, (ComposeEntryType) data );
1167                         g_free( addr );
1168                 }
1169                 else if( aio->type == ADDR_ITEM_GROUP ) {
1170                         ItemGroup *group = ( ItemGroup * ) aio;
1171                         GList *nodeMail = group->listEMail;
1172                         while( nodeMail ) {
1173                                 ItemEMail *email = nodeMail->data;
1174
1175                                 addr = addressbook_format_address(
1176                                                 ( AddrItemObject * ) email );
1177                                 compose_entry_append(
1178                                         compose, addr, (ComposeEntryType) data );
1179                                 g_free( addr );
1180                                 nodeMail = g_list_next( nodeMail );
1181                         }
1182                 }
1183         }
1184         g_list_free( list );
1185 }
1186
1187 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
1188         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",   sensitive );
1189         menu_set_sensitive( addrbook.menu_factory, "/File/Delete", sensitive );
1190
1191         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",    sensitive );
1192         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",   sensitive );
1193         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",  sensitive );
1194         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address",  sensitive );
1195
1196         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", sensitive );
1197         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   sensitive );
1198         menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  sensitive );
1199         gtk_widget_set_sensitive( addrbook.reg_btn, sensitive );
1200         gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
1201 }
1202
1203 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCTreeNode *node ) {
1204         gboolean canEdit = FALSE;
1205         gboolean canAdd = FALSE;
1206         gboolean canEditTr = TRUE;
1207         gboolean editAddress = FALSE;
1208         gboolean canExport = TRUE;
1209         AddressTypeControlItem *atci = NULL;
1210         AddressDataSource *ds = NULL;
1211         AddressInterface *iface = NULL;
1212
1213         if( obj == NULL ) return;
1214         if( obj->type == ADDR_INTERFACE ) {
1215                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
1216                 iface = adapter->interface;
1217                 if( iface ) {
1218                         if( iface->haveLibrary ) {
1219                                 /* Enable appropriate File / New command */
1220                                 atci = adapter->atci;
1221                                 menu_set_sensitive( addrbook.menu_factory, atci->menuCommand, TRUE );
1222                         }
1223                 }
1224                 canEditTr = canExport = FALSE;
1225         }
1226         else if( obj->type == ADDR_DATASOURCE ) {
1227                 AdapterDSource *ads = ADAPTER_DSOURCE(obj);
1228                 ds = ads->dataSource;
1229                 iface = ds->interface;
1230                 if( ! iface->readOnly ) {
1231                         canAdd = canEdit = editAddress = TRUE;
1232                 }
1233                 if( ! iface->haveLibrary ) {
1234                         canAdd = canEdit = editAddress = canExport = FALSE;
1235                 }
1236         }
1237         else if( obj->type == ADDR_ITEM_FOLDER ) {
1238                 ds = addressbook_find_datasource( addrbook.treeSelected );
1239                 if( ds ) {
1240                         iface = ds->interface;
1241                         if( iface->readOnly ) {
1242                                 canEditTr = FALSE;
1243                         }
1244                         else {
1245                                 canAdd = editAddress = TRUE;
1246                         }
1247                 }
1248         }
1249         else if( obj->type == ADDR_ITEM_GROUP ) {
1250                 ds = addressbook_find_datasource( addrbook.treeSelected );
1251                 if( ds ) {
1252                         iface = ds->interface;
1253                         if( ! iface->readOnly ) {
1254                                 editAddress = TRUE;
1255                         }
1256                 }
1257         }
1258
1259         if( addrbook.listSelected == NULL ) canEdit = FALSE;
1260
1261         /* Enable add */
1262         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", editAddress );
1263         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   canAdd );
1264         menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  canAdd );
1265         gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
1266
1267         /* Enable edit */
1268         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1269         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canEdit );
1270         gtk_widget_set_sensitive( addrbook.del_btn, canEdit );
1271
1272         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",      canEditTr );
1273         menu_set_sensitive( addrbook.menu_factory, "/File/Delete",    canEditTr );
1274
1275         /* Export data */
1276         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export HTML...", canExport );
1277         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export LDIF...", canExport );
1278 }
1279
1280 static void addressbook_tree_selected(GtkCTree *ctree, GtkCTreeNode *node,
1281                                       gint column, gpointer data)
1282 {
1283         AddressObject *obj = NULL;
1284         AdapterDSource *ads = NULL;
1285         AddressDataSource *ds = NULL;
1286         ItemFolder *rootFolder = NULL;
1287
1288         addrbook.treeSelected = node;
1289         addrbook.listSelected = NULL;
1290         addressbook_status_show( "" );
1291         if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
1292
1293         if( addrbook.clist ) gtk_clist_clear( GTK_CLIST(addrbook.clist) );
1294         if( node ) obj = gtk_ctree_node_get_row_data( ctree, node );
1295         if( obj == NULL ) return;
1296
1297         addrbook.opened = node;
1298
1299         if( obj->type == ADDR_DATASOURCE ) {
1300                 /* Read from file */
1301                 static gboolean tVal = TRUE;
1302
1303                 ads = ADAPTER_DSOURCE(obj);
1304                 if( ads == NULL ) return;
1305                 ds = ads->dataSource;
1306                 if( ds == NULL ) return;                
1307
1308                 if( addrindex_ds_get_modify_flag( ds ) ) {
1309                         addrindex_ds_read_data( ds );
1310                 }
1311
1312                 if( ! addrindex_ds_get_read_flag( ds ) ) {
1313                         addrindex_ds_read_data( ds );
1314                 }
1315                 addressbook_ds_show_message( ds );
1316
1317                 if( ! addrindex_ds_get_access_flag( ds ) ) {
1318                         /* Remove existing folders and groups */
1319                         gtk_clist_freeze( GTK_CLIST(ctree) );
1320                         addressbook_tree_remove_children( ctree, node );
1321                         gtk_clist_thaw( GTK_CLIST(ctree) );
1322
1323                         /* Load folders into the tree */
1324                         rootFolder = addrindex_ds_get_root_folder( ds );
1325                         if( ds->type == ADDR_IF_JPILOT ) {
1326                                 addressbook_node_add_folder( node, ds, rootFolder, ADDR_CATEGORY );
1327                         }
1328                         else {
1329                                 addressbook_node_add_folder( node, ds, rootFolder, ADDR_ITEM_FOLDER );
1330                         }
1331                         addrindex_ds_set_access_flag( ds, &tVal );
1332                         gtk_ctree_expand( ctree, node );
1333                 }
1334         }
1335
1336         /* Update address list */
1337         addressbook_set_clist( obj );
1338
1339         /* Setup main menu selections */
1340         addressbook_menubar_set_sensitive( FALSE );
1341         addressbook_menuitem_set_sensitive( obj, node );
1342
1343         addressbook_list_select_clear();
1344 }
1345
1346 /*
1347 * Setup address list popup menu.
1348 * Enter: pobj  Parent address object in address tree.
1349 *        obj   Address object in address list.
1350 */
1351 static void addressbook_list_menu_setup( void ) {
1352         GtkCTree *clist = NULL;
1353         AddressObject *pobj = NULL;
1354         AddressObject *obj = NULL;
1355         AdapterDSource *ads = NULL;
1356         AddressInterface *iface = NULL;
1357         AddressDataSource *ds = NULL;
1358         gboolean canEdit = FALSE;
1359         gboolean canDelete = FALSE;
1360         gboolean canCut = FALSE;
1361         gboolean canCopy = FALSE;
1362         gboolean canPaste = FALSE;
1363
1364         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1365         if( pobj == NULL ) return;
1366
1367         clist = GTK_CTREE(addrbook.clist);
1368         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1369         if( obj == NULL ) canEdit = FALSE;
1370
1371         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
1372
1373         if( pobj->type == ADDR_DATASOURCE ) {
1374                 ads = ADAPTER_DSOURCE(pobj);
1375                 ds = ads->dataSource;
1376                 iface = ds->interface;
1377                 if( ! iface->readOnly ) {
1378                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1379                         menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
1380                         menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1381                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1382                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1383                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1384                         if( obj ) canEdit = TRUE;
1385                 }
1386         }
1387         else if( pobj->type != ADDR_INTERFACE ) {
1388                 ds = addressbook_find_datasource( addrbook.treeSelected );
1389                 iface = ds->interface;
1390                 if( ! iface->readOnly ) {
1391                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
1392                                 menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1393                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1394                                 if( obj ) canEdit = TRUE;
1395                         }
1396                         if( pobj->type == ADDR_ITEM_FOLDER ) {
1397                                 menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
1398                                 menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1399                                 if( obj ) canEdit = TRUE;
1400                         }
1401                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1402                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1403                 }
1404         }
1405         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
1406
1407         canDelete = canEdit;
1408         canDelete = canEdit;
1409
1410         /* Disable edit if more than one row selected */
1411         if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) canEdit = FALSE;
1412
1413         menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
1414         menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
1415
1416         menu_set_sensitive( addrbook.list_factory, "/Cut",           canCut );
1417         menu_set_sensitive( addrbook.list_factory, "/Copy",          canCopy );
1418         menu_set_sensitive( addrbook.list_factory, "/Paste",         canPaste );
1419         menu_set_sensitive( addrbook.list_factory, "/Paste Address", canPaste );
1420
1421         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",           canCut );
1422         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",          canCopy );
1423         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",         canPaste );
1424         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );
1425
1426         menu_set_sensitive( addrbook.tree_factory, "/Cut",           canCut );
1427         menu_set_sensitive( addrbook.tree_factory, "/Copy",          canCopy );
1428         menu_set_sensitive( addrbook.tree_factory, "/Paste",         canPaste );
1429
1430         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1431         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canDelete );
1432
1433         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1434 }
1435
1436 static void addressbook_list_selected(GtkCList *clist, gint row, gint column,
1437                                       GdkEvent *event, gpointer data)
1438 {
1439         if (event && event->type == GDK_2BUTTON_PRESS) {
1440                 /* Handle double click */
1441                 if (prefs_common.add_address_by_click &&
1442                     addrbook.target_compose)
1443                         addressbook_to_clicked(NULL, NULL);
1444                 else
1445                         addressbook_edit_address_cb(NULL, 0, NULL);
1446         }
1447 }
1448
1449 /*
1450  * Add list of items into tree node below specified tree node.
1451  * Enter: treeNode  Tree node.
1452  *        ds        Data source.
1453  *        listItems List of items.
1454  */
1455 static void addressbook_treenode_add_list(
1456         GtkCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
1457 {
1458         GList *node;
1459
1460         node = listItems;
1461         while( node ) {
1462                 AddrItemObject *aio;
1463                 GtkCTreeNode *nn;
1464
1465                 aio = node->data;
1466                 if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_GROUP ) {
1467                         ItemGroup *group;
1468
1469                         group = ( ItemGroup * ) aio;
1470                         nn = addressbook_node_add_group( treeNode, ds, group );
1471                 }
1472                 else if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_FOLDER ) {
1473                         ItemFolder *folder;
1474
1475                         folder = ( ItemFolder * ) aio;
1476                         nn = addressbook_node_add_folder(
1477                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
1478                 }
1479                 node = g_list_next( node );
1480         }
1481 }
1482
1483 /*
1484 * Cut from address list widget.
1485 */
1486 static void addressbook_clip_cut_cb( void ) {
1487         _clipBoard_->cutFlag = TRUE;
1488         addrclip_clear( _clipBoard_ );
1489         addrclip_add( _clipBoard_, _addressSelect_ );
1490         /* addrclip_list_show( _clipBoard_, stdout ); */
1491 }
1492
1493 /*
1494 * Copy from address list widget.
1495 */
1496 static void addressbook_clip_copy_cb( void ) {
1497         _clipBoard_->cutFlag = FALSE;
1498         addrclip_clear( _clipBoard_ );
1499         addrclip_add( _clipBoard_, _addressSelect_ );
1500         /* addrclip_list_show( _clipBoard_, stdout ); */
1501 }
1502
1503 /*
1504 * Paste clipboard into address list widget.
1505 */
1506 static void addressbook_clip_paste_cb( void ) {
1507         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
1508         AddressObject *pobj = NULL;
1509         AddressDataSource *ds = NULL;
1510         AddressBookFile *abf = NULL;
1511         ItemFolder *folder = NULL;
1512         GList *folderGroup = NULL;
1513
1514         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
1515         if( ds == NULL ) return;
1516         if( addrindex_ds_get_readonly( ds ) ) {
1517                 addressbook_ds_status_message(
1518                         ds, _( "Cannot paste. Target address book is readonly." ) );
1519                 return;
1520         }
1521
1522         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1523         if( pobj ) {
1524                 if( pobj->type == ADDR_ITEM_FOLDER ) {
1525                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
1526                 }
1527                 else if( pobj->type == ADDR_ITEM_GROUP ) {
1528                         addressbook_ds_status_message(
1529                                 ds, _( "Cannot paste into an address group." ) );
1530                         return;
1531                 }
1532         }
1533
1534         /* Get an address book */
1535         abf = addressbook_get_book_file();
1536         if( abf == NULL ) return;
1537
1538         if( _clipBoard_->cutFlag ) {
1539                 /* Paste/Cut */
1540                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
1541
1542                 /* Remove all groups and folders in clipboard from tree node */
1543                 addressbook_treenode_remove_item();
1544
1545                 /* Remove all "cut" items */
1546                 addrclip_delete_item( _clipBoard_ );
1547
1548                 /* Clear clipboard - cut items??? */
1549                 addrclip_clear( _clipBoard_ );
1550         }
1551         else {
1552                 /* Paste/Copy */
1553                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
1554         }
1555
1556         /* addrclip_list_show( _clipBoard_, stdout ); */
1557         if( folderGroup ) {
1558                 /* Update tree by inserting node for each folder or group */
1559                 addressbook_treenode_add_list(
1560                         addrbook.treeSelected, ds, folderGroup );
1561                 gtk_ctree_expand( ctree, addrbook.treeSelected );
1562                 g_list_free( folderGroup );
1563                 folderGroup = NULL;
1564         }
1565
1566         /* Display items pasted */
1567         gtk_ctree_select( ctree, addrbook.opened );
1568
1569 }
1570
1571 /*
1572 * Paste clipboard email addresses only into address list widget.
1573 */
1574 static void addressbook_clip_paste_address_cb( void ) {
1575         GtkCTree *clist = GTK_CTREE(addrbook.clist);
1576         GtkCTree *ctree;
1577         AddressObject *pobj = NULL;
1578         AddressDataSource *ds = NULL;
1579         AddressBookFile *abf = NULL;
1580         ItemFolder *folder = NULL;
1581         AddrItemObject *aio;
1582         gint cnt;
1583
1584         if( addrbook.listSelected == NULL ) return;
1585
1586         ctree = GTK_CTREE( addrbook.ctree );
1587         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
1588         if( ds == NULL ) return;
1589         if( addrindex_ds_get_readonly( ds ) ) {
1590                 addressbook_ds_status_message(
1591                         ds, _( "Cannot paste. Target address book is readonly." ) );
1592                 return;
1593         }
1594
1595         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1596         if( pobj ) {
1597                 if( pobj->type == ADDR_ITEM_FOLDER ) {
1598                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
1599                 }
1600         }
1601
1602         abf = addressbook_get_book_file();
1603         if( abf == NULL ) return;
1604
1605         cnt = 0;
1606         aio = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1607         if( aio->type == ADDR_ITEM_PERSON ) {
1608                 ItemPerson *person;
1609
1610                 person = ( ItemPerson * ) aio;
1611                 if( _clipBoard_->cutFlag ) {
1612                         /* Paste/Cut */
1613                         cnt = addrclip_paste_person_cut( _clipBoard_, abf, person );
1614
1615                         /* Remove all "cut" items */
1616                         addrclip_delete_address( _clipBoard_ );
1617
1618                         /* Clear clipboard */
1619                         addrclip_clear( _clipBoard_ );
1620                 }
1621                 else {
1622                         /* Paste/Copy */
1623                         cnt = addrclip_paste_person_copy( _clipBoard_, abf, person );
1624                 }
1625                 if( cnt > 0 ) {
1626                         addritem_person_set_opened( person, TRUE );
1627                 }
1628         }
1629
1630         /* Display items pasted */
1631         if( cnt > 0 ) {
1632                 gtk_ctree_select( ctree, addrbook.opened );
1633         }
1634 }
1635
1636 /*
1637 * Add current treenode object to clipboard. Note that widget only allows
1638 * one entry from the tree list to be selected.
1639 */
1640 static void addressbook_treenode_to_clipboard( void ) {
1641         AddressObject *obj = NULL;
1642         AddressDataSource *ds = NULL;
1643         AddrSelectItem *item;
1644         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
1645         GtkCTreeNode *node;
1646
1647         node = addrbook.treeSelected;
1648         if( node == NULL ) return;
1649         obj = gtk_ctree_node_get_row_data( ctree, node );
1650         if( obj == NULL ) return;
1651
1652         ds = addressbook_find_datasource( node );
1653         if( ds == NULL ) return;
1654
1655         item = NULL;
1656         if( obj->type == ADDR_ITEM_FOLDER ) {
1657                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
1658                 ItemFolder *folder = adapter->itemFolder;
1659
1660                 item = addrselect_create_node( obj );
1661                 item->uid = g_strdup( ADDRITEM_ID(folder) );
1662         }
1663         else if( obj->type == ADDR_ITEM_GROUP ) {
1664                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
1665                 ItemGroup *group = adapter->itemGroup;
1666
1667                 item = addrselect_create_node( obj );
1668                 item->uid = g_strdup( ADDRITEM_ID(group) );
1669         }
1670         else if( obj->type == ADDR_DATASOURCE ) {
1671                 /* Data source */
1672                 item = addrselect_create_node( obj );
1673                 item->uid = NULL;
1674         }
1675
1676         if( item ) {
1677                 /* Clear existing list and add item into list */
1678                 gchar *cacheID;
1679
1680                 addressbook_list_select_clear();
1681                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
1682                 addrselect_list_add( _addressSelect_, item, cacheID );
1683                 g_free( cacheID );
1684         }
1685 }
1686
1687 /*
1688 * Cut from tree widget.
1689 */
1690 static void addressbook_treenode_cut_cb( void ) {
1691         _clipBoard_->cutFlag = TRUE;
1692         addressbook_treenode_to_clipboard();
1693         addrclip_clear( _clipBoard_ );
1694         addrclip_add( _clipBoard_, _addressSelect_ );
1695         /* addrclip_list_show( _clipBoard_, stdout ); */
1696 }
1697
1698 /*
1699 * Copy from tree widget.
1700 */
1701 static void addressbook_treenode_copy_cb( void ) {
1702         _clipBoard_->cutFlag = FALSE;
1703         addressbook_treenode_to_clipboard();
1704         addrclip_clear( _clipBoard_ );
1705         addrclip_add( _clipBoard_, _addressSelect_ );
1706         /* addrclip_list_show( _clipBoard_, stdout ); */
1707 }
1708
1709 /*
1710 * Paste clipboard into address tree widget.
1711 */
1712 static void addressbook_treenode_paste_cb( void ) {
1713         addressbook_clip_paste_cb();
1714 }
1715
1716 static void addressbook_list_select_clear( void ) {
1717         addrselect_list_clear( _addressSelect_ );
1718 }
1719
1720 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
1721         gchar *cacheID;
1722
1723         if( ds == NULL ) return;
1724         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
1725         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
1726         g_free( cacheID );
1727 }
1728
1729 static void addressbook_list_select_remove( AddrItemObject *aio ) {
1730         addrselect_list_remove( _addressSelect_, aio );
1731 }
1732
1733 static void addressbook_list_row_selected( GtkCTree *clist,
1734                                            GtkCTreeNode *node,
1735                                            gint column,
1736                                            gpointer data )
1737 {
1738         GtkEntry *entry = GTK_ENTRY(addrbook.entry);
1739         AddrItemObject *aio = NULL;
1740         AddressObject *pobj = NULL;
1741         AdapterDSource *ads = NULL;
1742         AddressDataSource *ds = NULL;
1743
1744         gtk_entry_set_text( entry, "" );
1745         addrbook.listSelected = node;
1746
1747         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1748         if( pobj == NULL ) return;
1749
1750         if( pobj->type == ADDR_DATASOURCE ) {
1751                 ads = ADAPTER_DSOURCE(pobj);
1752                 ds = ads->dataSource;
1753         }
1754         else if( pobj->type != ADDR_INTERFACE ) {
1755                 ds = addressbook_find_datasource( addrbook.treeSelected );
1756         }
1757
1758         aio = gtk_ctree_node_get_row_data( clist, node );
1759         if( aio ) {
1760                 /* printf( "list select: %d : '%s'\n", aio->type, aio->name ); */
1761                 addressbook_list_select_add( aio, ds );
1762         }
1763
1764         addressbook_list_menu_setup();
1765 }
1766
1767 static void addressbook_list_row_unselected( GtkCTree *ctree,
1768                                              GtkCTreeNode *node,
1769                                              gint column,
1770                                              gpointer data )
1771 {
1772         AddrItemObject *aio;
1773
1774         aio = gtk_ctree_node_get_row_data( ctree, node );
1775         if( aio != NULL ) {
1776                 /* printf( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
1777                 addressbook_list_select_remove( aio );
1778         }
1779 }
1780
1781 static void addressbook_entry_gotfocus( GtkWidget *widget ) {
1782         gtk_editable_select_region( GTK_EDITABLE(addrbook.entry), 0, -1 );
1783 }
1784
1785 static void addressbook_list_button_pressed(GtkWidget *widget,
1786                                             GdkEventButton *event,
1787                                             gpointer data)
1788 {
1789         if( ! event ) return;
1790
1791         addressbook_list_menu_setup();
1792
1793         if( event->button == 3 ) {
1794                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
1795                        event->button, event->time );
1796         }
1797 }
1798
1799 static void addressbook_list_button_released(GtkWidget *widget,
1800                                              GdkEventButton *event,
1801                                              gpointer data)
1802 {
1803 }
1804
1805 static void addressbook_tree_button_pressed(GtkWidget *ctree,
1806                                             GdkEventButton *event,
1807                                             gpointer data)
1808 {
1809         GtkCList *clist = GTK_CLIST(ctree);
1810         gint row, column;
1811         AddressObject *obj = NULL;
1812         AdapterDSource *ads = NULL;
1813         AddressInterface *iface = NULL;
1814         AddressDataSource *ds = NULL;
1815         gboolean canEdit = FALSE;
1816         gboolean canCut = FALSE;
1817         gboolean canCopy = FALSE;
1818         gboolean canPaste = FALSE;
1819         gboolean canTreeCut = FALSE;
1820         gboolean canTreeCopy = FALSE;
1821         gboolean canTreePaste = FALSE;
1822
1823         if( ! event ) return;
1824         addressbook_menubar_set_sensitive( FALSE );
1825
1826         if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
1827                 gtk_clist_select_row( clist, row, column );
1828                 gtkut_clist_set_focus_row(clist, row);
1829                 obj = gtk_clist_get_row_data( clist, row );
1830         }
1831
1832         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
1833
1834         if( obj == NULL ) return;
1835
1836         if( ! addrclip_is_empty( _clipBoard_ ) ) {
1837                 canTreePaste = TRUE;
1838         }
1839
1840         if (obj->type == ADDR_DATASOURCE) {
1841                 ads = ADAPTER_DSOURCE(obj);
1842                 ds = ads->dataSource;
1843                 iface = ds->interface;
1844                 canEdit = TRUE;
1845                 if( iface->readOnly ) {
1846                         canTreePaste = FALSE;
1847                 }
1848                 else {
1849                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
1850                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
1851                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
1852                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1853                 }
1854                 canTreeCopy = TRUE;
1855         }
1856         else if (obj->type == ADDR_ITEM_FOLDER) {
1857                 ds = addressbook_find_datasource( addrbook.treeSelected );
1858                 iface = ds->interface;
1859                 if( iface->readOnly ) {
1860                         canTreePaste = FALSE;
1861                 }
1862                 else {
1863                         canEdit = TRUE;
1864                         canTreeCut = TRUE;
1865                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
1866                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
1867                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
1868                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1869                 }
1870                 canTreeCopy = TRUE;
1871         }
1872         else if (obj->type == ADDR_ITEM_GROUP) {
1873                 ds = addressbook_find_datasource( addrbook.treeSelected );
1874                 iface = ds->interface;
1875                 if( ! iface->readOnly ) {
1876                         canEdit = TRUE;
1877                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
1878                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1879                 }
1880         }
1881         else if (obj->type == ADDR_INTERFACE) {
1882                 canTreePaste = FALSE;
1883         }
1884
1885         if( canEdit ) {
1886                 if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1887         }
1888         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
1889         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1890
1891         /* Enable edit */
1892         menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
1893         menu_set_sensitive( addrbook.tree_factory, "/Delete", canEdit );
1894         menu_set_sensitive( addrbook.tree_factory, "/Cut",    canTreeCut );
1895         menu_set_sensitive( addrbook.tree_factory, "/Copy",   canTreeCopy );
1896         menu_set_sensitive( addrbook.tree_factory, "/Paste",  canTreePaste );
1897
1898         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",          canEdit );
1899         menu_set_sensitive( addrbook.menu_factory, "/File/Delete",        canEdit );
1900         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",           canCut );
1901         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",          canCopy );
1902         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",         canPaste );
1903         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );
1904
1905         if( event->button == 3 ) {
1906                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
1907                                event->button, event->time);
1908         }
1909
1910 }
1911
1912 static void addressbook_tree_button_released(GtkWidget *ctree,
1913                                              GdkEventButton *event,
1914                                              gpointer data)
1915 {
1916         gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
1917         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree), addrbook.opened);
1918 }
1919
1920 static void addressbook_popup_close(GtkMenuShell *menu_shell, gpointer data)
1921 {
1922         if (!addrbook.opened) return;
1923
1924         gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
1925         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree),
1926                                   addrbook.opened);
1927 }
1928
1929 static void addressbook_new_folder_cb(gpointer data, guint action,
1930                                       GtkWidget *widget)
1931 {
1932         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1933         AddressObject *obj = NULL;
1934         AddressDataSource *ds = NULL;
1935         AddressBookFile *abf = NULL;
1936         ItemFolder *parentFolder = NULL;
1937         ItemFolder *folder = NULL;
1938
1939         if( ! addrbook.treeSelected ) return;
1940         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1941         if( obj == NULL ) return;
1942         ds = addressbook_find_datasource( addrbook.treeSelected );
1943         if( ds == NULL ) return;
1944
1945         if( obj->type == ADDR_DATASOURCE ) {
1946                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
1947         }
1948         else if( obj->type == ADDR_ITEM_FOLDER ) {
1949                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
1950         }
1951         else {
1952                 return;
1953         }
1954
1955         abf = ds->rawDataSource;
1956         if( abf == NULL ) return;
1957         folder = addressbook_edit_folder( abf, parentFolder, NULL );
1958         if( folder ) {
1959                 GtkCTreeNode *nn;
1960                 nn = addressbook_node_add_folder( addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
1961                 gtk_ctree_expand( ctree, addrbook.treeSelected );
1962                 if( addrbook.treeSelected == addrbook.opened ) addressbook_set_clist(obj);
1963         }
1964
1965 }
1966
1967 static void addressbook_new_group_cb(gpointer data, guint action,
1968                                      GtkWidget *widget)
1969 {
1970         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1971         AddressObject *obj = NULL;
1972         AddressDataSource *ds = NULL;
1973         AddressBookFile *abf = NULL;
1974         ItemFolder *parentFolder = NULL;
1975         ItemGroup *group = NULL;
1976
1977         if( ! addrbook.treeSelected ) return;
1978         obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
1979         if( obj == NULL ) return;
1980         ds = addressbook_find_datasource( addrbook.treeSelected );
1981         if( ds == NULL ) return;
1982
1983         if( obj->type == ADDR_DATASOURCE ) {
1984                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
1985         }
1986         else if( obj->type == ADDR_ITEM_FOLDER ) {
1987                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
1988         }
1989         else {
1990                 return;
1991         }
1992
1993         abf = ds->rawDataSource;
1994         if( abf == NULL ) return;
1995         group = addressbook_edit_group( abf, parentFolder, NULL );
1996         if( group ) {
1997                 GtkCTreeNode *nn;
1998                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
1999                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2000                 if( addrbook.treeSelected == addrbook.opened ) addressbook_set_clist(obj);
2001         }
2002
2003 }
2004
2005 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
2006 {
2007         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2008         gchar *text[1];
2009         guint8 spacing;
2010         GdkPixmap *pix_cl, *pix_op;
2011         GdkBitmap *mask_cl, *mask_op;
2012         gboolean is_leaf, expanded;
2013
2014         gtk_ctree_get_node_info(ctree, node, text, &spacing,
2015                                 &pix_cl, &mask_cl, &pix_op, &mask_op,
2016                                 &is_leaf, &expanded);
2017         gtk_ctree_set_node_info(ctree, node, name, spacing,
2018                                 pix_cl, mask_cl, pix_op, mask_op,
2019                                 is_leaf, expanded);
2020 }
2021
2022 /*
2023 * Edit data source.
2024 * Enter: obj   Address object to edit.
2025 *        node  Node in tree.
2026 * Return: New name of data source.
2027 */
2028 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
2029         gchar *newName = NULL;
2030         AddressDataSource *ds = NULL;
2031         AddressInterface *iface = NULL;
2032         AdapterDSource *ads = NULL;
2033
2034         ds = addressbook_find_datasource( node );
2035         if( ds == NULL ) return NULL;
2036         iface = ds->interface;
2037         if( ! iface->haveLibrary ) return NULL;
2038
2039         /* Read data from data source */
2040         if( addrindex_ds_get_modify_flag( ds ) ) {
2041                 addrindex_ds_read_data( ds );
2042         }
2043
2044         if( ! addrindex_ds_get_read_flag( ds ) ) {
2045                 addrindex_ds_read_data( ds );
2046         }
2047
2048         /* Handle edit */
2049         ads = ADAPTER_DSOURCE(obj);
2050         if( ads->subType == ADDR_BOOK ) {
2051                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
2052         }
2053         else if( ads->subType == ADDR_VCARD ) {
2054                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
2055         }
2056 #ifdef USE_JPILOT
2057         else if( ads->subType == ADDR_JPILOT ) {
2058                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
2059         }
2060 #endif
2061 #ifdef USE_LDAP
2062         else if( ads->subType == ADDR_LDAP ) {
2063                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
2064         }
2065 #endif
2066         else {
2067                 return NULL;
2068         }
2069         newName = obj->name;
2070         return newName;
2071 }
2072
2073 /*
2074 * Edit an object that is in the address tree area.
2075 */
2076 static void addressbook_treenode_edit_cb(gpointer data, guint action,
2077                                        GtkWidget *widget)
2078 {
2079         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2080         AddressObject *obj;
2081         AddressDataSource *ds = NULL;
2082         AddressBookFile *abf = NULL;
2083         GtkCTreeNode *node = NULL, *parentNode = NULL;
2084         gchar *name = NULL;
2085
2086         if( ! addrbook.treeSelected ) return;
2087         node = addrbook.treeSelected;
2088         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2089         obj = gtk_ctree_node_get_row_data( ctree, node );
2090         if( obj == NULL ) return;
2091         parentNode = GTK_CTREE_ROW(node)->parent;
2092
2093         ds = addressbook_find_datasource( node );
2094         if( ds == NULL ) return;
2095
2096         if( obj->type == ADDR_DATASOURCE ) {
2097                 name = addressbook_edit_datasource( obj, node );
2098                 if( name == NULL ) return;
2099         }
2100         else {
2101                 abf = ds->rawDataSource;
2102                 if( abf == NULL ) return;
2103                 if( obj->type == ADDR_ITEM_FOLDER ) {
2104                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2105                         ItemFolder *item = adapter->itemFolder;
2106                         ItemFolder *parentFolder = NULL;
2107                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2108                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2109                         name = ADDRITEM_NAME(item);
2110                 }
2111                 else if( obj->type == ADDR_ITEM_GROUP ) {
2112                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2113                         ItemGroup *item = adapter->itemGroup;
2114                         ItemFolder *parentFolder = NULL;
2115                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2116                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2117                         name = ADDRITEM_NAME(item);
2118                 }
2119         }
2120         if( name && parentNode ) {
2121                 /* Update node in tree view */
2122                 addressbook_change_node_name( node, name );
2123                 gtk_sctree_sort_node(ctree, parentNode);
2124                 gtk_ctree_expand( ctree, node );
2125                 gtk_ctree_select( ctree, node );
2126         }
2127 }
2128
2129 /*
2130 * Delete an item from the tree widget.
2131 */
2132 static void addressbook_treenode_delete_cb(gpointer data, guint action,
2133                                          GtkWidget *widget)
2134 {
2135         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2136         GtkCTreeNode *node = NULL;
2137         AddressObject *obj;
2138         gchar *message;
2139         AlertValue aval;
2140         AddressBookFile *abf = NULL;
2141         AdapterDSource *ads = NULL;
2142         AddressInterface *iface = NULL;
2143         AddressDataSource *ds = NULL;
2144         gboolean remFlag = FALSE;
2145
2146         if( ! addrbook.treeSelected ) return;
2147         node = addrbook.treeSelected;
2148         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2149
2150         obj = gtk_ctree_node_get_row_data( ctree, node );
2151         g_return_if_fail(obj != NULL);
2152
2153         if( obj->type == ADDR_DATASOURCE ) {
2154                 ads = ADAPTER_DSOURCE(obj);
2155                 if( ads == NULL ) return;
2156                 ds = ads->dataSource;
2157                 if( ds == NULL ) return;
2158         }
2159         else {
2160                 /* Must be folder or something else */
2161                 ds = addressbook_find_datasource( node );
2162                 if( ds == NULL ) return;
2163
2164                 /* Only allow deletion from non-readOnly data sources */
2165                 iface = ds->interface;
2166                 if( iface->readOnly ) return;
2167         }
2168
2169         /* Confirm deletion */
2170         if( obj->type == ADDR_ITEM_FOLDER ) {
2171                 message = g_strdup_printf( _(
2172                         "Do you want to delete the folder AND all addresses in `%s' ? \n" \
2173                         "If deleting the folder only, addresses will be moved into parent folder." ),
2174                         obj->name );
2175                 aval = alertpanel( _("Delete"), message,
2176                         _("Folder only"), _("Folder and Addresses"), _("Cancel") );
2177                 g_free(message);
2178                 if( aval == G_ALERTOTHER ) return;
2179         }
2180         else {
2181                 message = g_strdup_printf(_("Really delete `%s' ?"), obj->name);
2182                 aval = alertpanel(_("Delete"), message, _("Yes"), _("No"), NULL);
2183                 g_free(message);
2184                 if (aval != G_ALERTDEFAULT) return;
2185         }
2186
2187         /* Proceed with deletion */
2188         if( obj->type == ADDR_DATASOURCE ) {
2189                 /* Remove data source. */
2190                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
2191                         addrindex_free_datasource( ds );
2192                         remFlag = TRUE;
2193                 }
2194         }
2195         else {
2196                 abf = addressbook_get_book_file();
2197                 if( abf == NULL ) return;
2198         }
2199
2200         if( obj->type == ADDR_ITEM_FOLDER ) {
2201                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2202                 ItemFolder *item = adapter->itemFolder;
2203                 if( aval == G_ALERTDEFAULT ) {
2204                         /* Remove folder only */
2205                         item = addrbook_remove_folder( abf, item );
2206                         if( item ) {
2207                                 addritem_free_item_folder( item );
2208                                 addressbook_move_nodes_up( ctree, node );
2209                                 remFlag = TRUE;
2210                         }
2211                 }
2212                 else if( aval == G_ALERTALTERNATE ) {
2213                         /* Remove folder and addresses */
2214                         item = addrbook_remove_folder_delete( abf, item );
2215                         if( item ) {
2216                                 addritem_free_item_folder( item );
2217                                 remFlag = TRUE;
2218                         }
2219                 }
2220         }
2221         else if( obj->type == ADDR_ITEM_GROUP ) {
2222                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2223                 ItemGroup *item = adapter->itemGroup;
2224
2225                 item = addrbook_remove_group( abf, item );
2226                 if( item ) {
2227                         addritem_free_item_group( item );
2228                         remFlag = TRUE;
2229                 }
2230         }
2231
2232         if( remFlag ) {
2233                 /* Remove node. */
2234                 gtk_ctree_remove_node(ctree, node );
2235         }
2236 }
2237
2238 static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
2239         AddressObject *pobj = NULL;
2240         AddressDataSource *ds = NULL;
2241         AddressBookFile *abf = NULL;
2242
2243         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
2244         if( pobj == NULL ) return;
2245         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2246         if( ds == NULL ) return;
2247
2248         abf = ds->rawDataSource;
2249         if( abf == NULL ) return;
2250
2251         if( pobj->type == ADDR_DATASOURCE ) {
2252                 if( ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ) {
2253                         /* New address */
2254                         ItemPerson *person = addressbook_edit_person( abf, NULL, NULL, FALSE );
2255                         if( person && addrbook.treeSelected == addrbook.opened ) {
2256                                 gtk_clist_unselect_all( GTK_CLIST(addrbook.clist) );
2257                                 addressbook_folder_refresh_one_person( GTK_CTREE(addrbook.clist), person );
2258                         }
2259                 }
2260         }
2261         else if( pobj->type == ADDR_ITEM_FOLDER ) {
2262                 /* New address */
2263                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
2264                 ItemPerson *person = addressbook_edit_person( abf, folder, NULL, FALSE );
2265                 if( person ) {
2266                         if (addrbook.treeSelected == addrbook.opened) {
2267                                 gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
2268                         }
2269                 }
2270         }
2271         else if( pobj->type == ADDR_ITEM_GROUP ) {
2272                 /* New address in group */
2273                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
2274                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
2275                 if (addrbook.treeSelected == addrbook.opened) {
2276                         /* Change node name in tree. */
2277                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
2278                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
2279                 }
2280         }
2281 }
2282
2283 /*
2284 * Search for specified child group node in address index tree.
2285 * Enter: parent Parent node.
2286 *        group  Group to find.
2287 */
2288 static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
2289         GtkCTreeNode *node = NULL;
2290         GtkCTreeRow *currRow;
2291
2292         currRow = GTK_CTREE_ROW( parent );
2293         if( currRow ) {
2294                 node = currRow->children;
2295                 while( node ) {
2296                         AddressObject *obj;
2297
2298                         obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
2299                         if( obj->type == ADDR_ITEM_GROUP ) {
2300                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
2301                                 if( g == group ) return node;
2302                         }
2303                         currRow = GTK_CTREE_ROW(node);
2304                         node = currRow->sibling;
2305                 }
2306         }
2307         return NULL;
2308 }
2309
2310 static AddressBookFile *addressbook_get_book_file() {
2311         AddressBookFile *abf = NULL;
2312         AddressDataSource *ds = NULL;
2313
2314         ds = addressbook_find_datasource( addrbook.treeSelected );
2315         if( ds == NULL ) return NULL;
2316         if( ds->type == ADDR_IF_BOOK ) abf = ds->rawDataSource;
2317         return abf;
2318 }
2319
2320 static void addressbook_tree_remove_children( GtkCTree *ctree, GtkCTreeNode *parent ) {
2321         GtkCTreeNode *node;
2322         GtkCTreeRow *row;
2323
2324         /* Remove existing folders and groups */
2325         row = GTK_CTREE_ROW( parent );
2326         if( row ) {
2327                 while( (node = row->children) ) {
2328                         gtk_ctree_remove_node( ctree, node );
2329                 }
2330         }
2331 }
2332
2333 static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
2334         GtkCTreeNode *parent, *child;
2335         GtkCTreeRow *currRow;
2336         currRow = GTK_CTREE_ROW( node );
2337         if( currRow ) {
2338                 parent = currRow->parent;
2339                 while( (child = currRow->children) ) {
2340                         gtk_ctree_move( ctree, child, parent, node );
2341                 }
2342                 gtk_sctree_sort_node( ctree, parent );
2343         }
2344 }
2345
2346 static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget *widget ) {
2347         GtkCTree *clist = GTK_CTREE(addrbook.clist);
2348         GtkCTree *ctree;
2349         AddressObject *obj = NULL, *pobj = NULL;
2350         AddressDataSource *ds = NULL;
2351         GtkCTreeNode *node = NULL, *parentNode = NULL;
2352         gchar *name = NULL;
2353         AddressBookFile *abf = NULL;
2354
2355         if( addrbook.listSelected == NULL ) return;
2356         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
2357         g_return_if_fail(obj != NULL);
2358
2359         ctree = GTK_CTREE( addrbook.ctree );
2360         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2361         node = gtk_ctree_find_by_row_data( ctree, addrbook.treeSelected, obj );
2362
2363         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2364         if( ds == NULL ) return;
2365
2366         abf = addressbook_get_book_file();
2367         if( abf == NULL ) return;
2368         if( obj->type == ADDR_ITEM_EMAIL ) {
2369                 ItemEMail *email = ( ItemEMail * ) obj;
2370                 if( email == NULL ) return;
2371                 if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
2372                         /* Edit parent group */
2373                         AdapterGroup *adapter = ADAPTER_GROUP(pobj);
2374                         ItemGroup *itemGrp = adapter->itemGroup;
2375                         if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
2376                         name = ADDRITEM_NAME(itemGrp);
2377                         node = addrbook.treeSelected;
2378                         parentNode = GTK_CTREE_ROW(node)->parent;
2379                 }
2380                 else {
2381                         /* Edit person - email page */
2382                         ItemPerson *person;
2383                         person = ( ItemPerson * ) ADDRITEM_PARENT(email);
2384                         if( addressbook_edit_person( abf, NULL, person, TRUE ) == NULL ) return;
2385                         addressbook_folder_refresh_one_person( clist, person );
2386                         invalidate_address_completion();
2387                         return;
2388                 }
2389         }
2390         else if( obj->type == ADDR_ITEM_PERSON ) {
2391                 /* Edit person - basic page */
2392                 ItemPerson *person = ( ItemPerson * ) obj;
2393                 if( addressbook_edit_person( abf, NULL, person, FALSE ) == NULL ) return;
2394                 invalidate_address_completion();
2395                 addressbook_folder_refresh_one_person( clist, person );
2396                 return;
2397         }
2398         else if( obj->type == ADDR_ITEM_GROUP ) {
2399                 ItemGroup *itemGrp = ( ItemGroup * ) obj;
2400                 if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
2401                 parentNode = addrbook.treeSelected;
2402                 node = addressbook_find_group_node( parentNode, itemGrp );
2403                 name = ADDRITEM_NAME(itemGrp);
2404         }
2405         else {
2406                 return;
2407         }
2408
2409         /* Update tree node with node name */
2410         if( node == NULL ) return;
2411         addressbook_change_node_name( node, name );
2412         gtk_sctree_sort_node( ctree, parentNode );
2413         gtk_ctree_select( ctree, addrbook.opened ); 
2414 }
2415
2416 static void addressbook_delete_address_cb(gpointer data, guint action,
2417                                           GtkWidget *widget)
2418 {
2419         addressbook_del_clicked(NULL, NULL);
2420 }
2421
2422 static void close_cb(gpointer data, guint action, GtkWidget *widget)
2423 {
2424         addressbook_close();
2425 }
2426
2427 static void addressbook_file_save_cb( gpointer data, guint action, GtkWidget *widget ) {
2428         addressbook_export_to_file();
2429 }
2430
2431 static void addressbook_person_expand_node( GtkCTree *ctree, GList *node, gpointer *data ) {
2432         if( node ) {
2433                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
2434                 if( person ) addritem_person_set_opened( person, TRUE );
2435         }
2436 }
2437
2438 static void addressbook_person_collapse_node( GtkCTree *ctree, GList *node, gpointer *data ) {
2439         if( node ) {
2440                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
2441                 if( person ) addritem_person_set_opened( person, FALSE );
2442         }
2443 }
2444
2445 static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
2446         gchar *str = NULL;
2447         gchar *eMailAlias = ADDRITEM_NAME(email);
2448         if( eMailAlias && *eMailAlias != '\0' ) {
2449                 if( person ) {
2450                         str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
2451                 }
2452                 else {
2453                         str = g_strdup( eMailAlias );
2454                 }
2455         }
2456         return str;
2457 }
2458
2459 static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
2460         GList *items = itemGroup->listEMail;
2461         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2462         for( ; items != NULL; items = g_list_next( items ) ) {
2463                 GtkCTreeNode *nodeEMail = NULL;
2464                 gchar *text[N_COLS];
2465                 ItemEMail *email = items->data;
2466                 ItemPerson *person;
2467                 gchar *str = NULL;
2468
2469                 if( ! email ) continue;
2470
2471                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
2472                 str = addressbook_format_item_clist( person, email );
2473                 if( str ) {
2474                         text[COL_NAME] = str;
2475                 }
2476                 else {
2477                         text[COL_NAME] = ADDRITEM_NAME(person);
2478                 }
2479                 text[COL_ADDRESS] = email->address;
2480                 text[COL_REMARKS] = email->remarks;
2481                 nodeEMail = gtk_ctree_insert_node(
2482                                 clist, NULL, NULL,
2483                                 text, FOLDER_SPACING,
2484                                 atci->iconXpm, atci->maskXpm,
2485                                 atci->iconXpmOpen, atci->maskXpmOpen,
2486                                 FALSE, FALSE );
2487                 gtk_ctree_node_set_row_data( clist, nodeEMail, email );
2488                 g_free( str );
2489                 str = NULL;
2490         }
2491 }
2492
2493 static void addressbook_folder_load_one_person(
2494                 GtkCTree *clist, ItemPerson *person,
2495                 AddressTypeControlItem *atci,
2496                 AddressTypeControlItem *atciMail )
2497 {
2498         GtkCTreeNode *nodePerson = NULL;
2499         GtkCTreeNode *nodeEMail = NULL;
2500         gchar *text[N_COLS];
2501         gboolean flgFirst = TRUE, haveAddr = FALSE;
2502         GList *node;
2503
2504         if( person == NULL ) return;
2505
2506         text[COL_NAME] = NULL;
2507         node = person->listEMail;
2508         while( node ) {
2509                 ItemEMail *email = node->data;
2510                 gchar *eMailAddr = NULL;
2511                 node = g_list_next( node );
2512
2513                 text[COL_ADDRESS] = email->address;
2514                 text[COL_REMARKS] = email->remarks;
2515                 eMailAddr = ADDRITEM_NAME(email);
2516                 if( eMailAddr && *eMailAddr == '\0' ) eMailAddr = NULL;
2517                 if( flgFirst ) {
2518                         /* First email belongs with person */
2519                         gchar *str = addressbook_format_item_clist( person, email );
2520                         if( str ) {
2521                                 text[COL_NAME] = str;
2522                         }
2523                         else {
2524                                 text[COL_NAME] = ADDRITEM_NAME(person);
2525                         }
2526                         nodePerson = gtk_ctree_insert_node(
2527                                         clist, NULL, NULL,
2528                                         text, FOLDER_SPACING,
2529                                         atci->iconXpm, atci->maskXpm,
2530                                         atci->iconXpmOpen, atci->maskXpmOpen,
2531                                         FALSE, person->isOpened );
2532                         g_free( str );
2533                         str = NULL;
2534                         gtk_ctree_node_set_row_data(clist, nodePerson, person );
2535                 }
2536                 else {
2537                         /* Subsequent email is a child node of person */
2538                         text[COL_NAME] = ADDRITEM_NAME(email);
2539                         nodeEMail = gtk_ctree_insert_node(
2540                                         clist, nodePerson, NULL,
2541                                         text, FOLDER_SPACING,
2542                                         atciMail->iconXpm, atciMail->maskXpm,
2543                                         atciMail->iconXpmOpen, atciMail->maskXpmOpen,
2544                                         FALSE, TRUE );
2545                         gtk_ctree_node_set_row_data(clist, nodeEMail, email );
2546                 }
2547                 flgFirst = FALSE;
2548                 haveAddr = TRUE;
2549         }
2550         if( ! haveAddr ) {
2551                 /* Have name without EMail */
2552                 text[COL_NAME] = ADDRITEM_NAME(person);
2553                 text[COL_ADDRESS] = NULL;
2554                 text[COL_REMARKS] = NULL;
2555                 nodePerson = gtk_ctree_insert_node(
2556                                 clist, NULL, NULL,
2557                                 text, FOLDER_SPACING,
2558                                 atci->iconXpm, atci->maskXpm,
2559                                 atci->iconXpmOpen, atci->maskXpmOpen,
2560                                 FALSE, person->isOpened );
2561                 gtk_ctree_node_set_row_data(clist, nodePerson, person );
2562         }
2563         gtk_sctree_sort_node(GTK_CTREE(clist), NULL);
2564         return;
2565 }
2566
2567 static void addressbook_folder_load_person( GtkCTree *clist, ItemFolder *itemFolder ) {
2568         GList *items;
2569         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
2570         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2571
2572         if( atci == NULL ) return;
2573         if( atciMail == NULL ) return;
2574
2575         /* Load email addresses */
2576         items = addritem_folder_get_person_list( itemFolder );
2577         for( ; items != NULL; items = g_list_next( items ) ) {
2578                 addressbook_folder_load_one_person( clist, items->data, atci, atciMail );
2579         }
2580         /* Free up the list */
2581         mgu_clear_list( items );
2582         g_list_free( items );
2583 }
2584
2585 static void addressbook_folder_remove_node( GtkCTree *clist, GtkCTreeNode *node ) { 
2586         addrbook.listSelected = NULL;
2587         gtk_ctree_remove_node( clist, node );
2588         addressbook_menubar_set_sensitive( FALSE );
2589         addressbook_menuitem_set_sensitive(
2590                 gtk_ctree_node_get_row_data(
2591                         GTK_CTREE(clist), addrbook.treeSelected ),
2592                 addrbook.treeSelected );
2593 }
2594
2595 static void addressbook_folder_refresh_one_person( GtkCTree *clist, ItemPerson *person ) {
2596         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
2597         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2598         GtkCTreeNode *node;
2599         if( atci == NULL ) return;
2600         if( atciMail == NULL ) return;
2601         if( person == NULL ) return;
2602         /* unload the person */
2603         
2604         node = gtk_ctree_find_by_row_data( clist, NULL, person );
2605         if( node )
2606                 addressbook_folder_remove_node( clist, node );
2607         addressbook_folder_load_one_person( clist, person, atci, atciMail );
2608         node = gtk_ctree_find_by_row_data( clist, NULL, person );
2609         if( node ) {
2610                 gtk_ctree_select( clist, node );
2611                 if (!gtk_ctree_node_is_visible( clist, node ) ) 
2612                         gtk_ctree_node_moveto( clist, node, 0, 0, 0 );
2613         }
2614 }
2615
2616 static void addressbook_folder_remove_one_person( GtkCTree *clist, ItemPerson *person ) {
2617         GtkCTreeNode *node;
2618         gint row;
2619         
2620         if( person == NULL ) return;
2621         node = gtk_ctree_find_by_row_data( clist, NULL, person );
2622         row  = gtk_clist_find_row_from_data( GTK_CLIST(clist), person );
2623         if( node ) {
2624                 addressbook_folder_remove_node( clist, node );
2625         }
2626 }
2627
2628 static void addressbook_folder_load_group( GtkCTree *clist, ItemFolder *itemFolder ) {
2629         GList *items;
2630         AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
2631
2632         /* Load any groups */
2633         if( ! atci ) return;
2634         items = addritem_folder_get_group_list( itemFolder );
2635         for( ; items != NULL; items = g_list_next( items ) ) {
2636                 GtkCTreeNode *nodeGroup = NULL;
2637                 gchar *text[N_COLS];
2638                 ItemGroup *group = items->data;
2639                 if( group == NULL ) continue;
2640                 text[COL_NAME] = ADDRITEM_NAME(group);
2641                 text[COL_ADDRESS] = NULL;
2642                 text[COL_REMARKS] = NULL;
2643                 nodeGroup = gtk_ctree_insert_node(clist, NULL, NULL,
2644                                       text, FOLDER_SPACING,
2645                                       atci->iconXpm, atci->maskXpm,
2646                                       atci->iconXpmOpen, atci->maskXpmOpen,
2647                                       FALSE, FALSE);
2648                 gtk_ctree_node_set_row_data(clist, nodeGroup, group );
2649                 gtk_sctree_sort_node(clist, NULL);
2650         }
2651         /* Free up the list */
2652         mgu_clear_list( items );
2653         g_list_free( items );
2654 }
2655
2656 /*
2657 * Search ctree widget callback function.
2658 * Enter: pA Pointer to node.
2659 *        pB Pointer to data item being sought.
2660 * Return: Zero (0) if group found.
2661 */
2662 static int addressbook_treenode_find_group_cb( gconstpointer pA, gconstpointer pB ) {
2663         AddressObject *aoA;
2664
2665         aoA = ( AddressObject * ) pA;
2666         if( aoA->type == ADDR_ITEM_GROUP ) {
2667                 ItemGroup *group, *grp;
2668
2669                 grp = ADAPTER_GROUP(aoA)->itemGroup;
2670                 group = ( ItemGroup * ) pB;
2671                 if( grp == group ) return 0;    /* Found group */
2672         }
2673         return 1;
2674 }
2675
2676 /*
2677 * Search ctree widget callback function.
2678 * Enter: pA Pointer to node.
2679 *        pB Pointer to data item being sought.
2680 * Return: Zero (0) if folder found.
2681 */
2682 static int addressbook_treenode_find_folder_cb( gconstpointer pA, gconstpointer pB ) {
2683         AddressObject *aoA;
2684
2685         aoA = ( AddressObject * ) pA;
2686         if( aoA->type == ADDR_ITEM_FOLDER ) {
2687                 ItemFolder *folder, *fld;
2688
2689                 fld = ADAPTER_FOLDER(aoA)->itemFolder;
2690                 folder = ( ItemFolder * ) pB;
2691                 if( fld == folder ) return 0;   /* Found folder */
2692         }
2693         return 1;
2694 }
2695
2696 /*
2697 * Remove folder and group nodes from tree widget for items contained ("cut")
2698 * in clipboard.
2699 */
2700 static void addressbook_treenode_remove_item( void ) {
2701         GList *node;
2702         AddrSelectItem *cutItem;
2703         AddressCache *cache;
2704         AddrItemObject *aio;
2705         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2706         GtkCTreeNode *tn;
2707
2708         node = _clipBoard_->objectList;
2709         while( node ) {
2710                 cutItem = node->data;
2711                 node = g_list_next( node );
2712                 cache = addrindex_get_cache(
2713                         _clipBoard_->addressIndex, cutItem->cacheID );
2714                 if( cache == NULL ) continue;
2715                 aio = addrcache_get_object( cache, cutItem->uid );
2716                 if( aio ) {
2717                         tn = NULL;
2718                         if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
2719                                 ItemFolder *folder;
2720
2721                                 folder = ( ItemFolder * ) aio;
2722                                 tn = gtk_ctree_find_by_row_data_custom(
2723                                         ctree, NULL, folder,
2724                                         addressbook_treenode_find_folder_cb );
2725                         }
2726                         else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
2727                                 ItemGroup *group;
2728
2729                                 group = ( ItemGroup * ) aio;
2730                                 tn = gtk_ctree_find_by_row_data_custom(
2731                                         ctree, NULL, group,
2732                                         addressbook_treenode_find_group_cb );
2733                         }
2734
2735                         if( tn ) {
2736                                 /* Free up adapter and remove node. */
2737                                 gtk_ctree_remove_node( ctree, tn );
2738                         }
2739                 }
2740         }
2741 }
2742
2743 /*
2744 * Find parent datasource for specified tree node.
2745 * Enter: node Node to test.
2746 * Return: Data source, or NULL if not found.
2747 */
2748 static AddressDataSource *addressbook_find_datasource( GtkCTreeNode *node ) {
2749         AddressDataSource *ds = NULL;
2750         AddressObject *ao;
2751
2752         g_return_val_if_fail(addrbook.ctree != NULL, NULL);
2753
2754         while( node ) {
2755                 if( GTK_CTREE_ROW(node)->level < 2 ) return NULL;
2756                 ao = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
2757                 if( ao ) {
2758                         /* printf( "ao->type = %d\n", ao->type ); */
2759                         if( ao->type == ADDR_DATASOURCE ) {
2760                                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
2761                                 /* printf( "found it\n" ); */
2762                                 ds = ads->dataSource;
2763                                 break;
2764                         }
2765                 }
2766                 node = GTK_CTREE_ROW(node)->parent;
2767         }
2768         return ds;
2769 }
2770
2771 /*
2772 * Load address list widget with children of specified object.
2773 * Enter: obj    Parent object to be loaded.
2774 */
2775 static void addressbook_set_clist( AddressObject *obj ) {
2776         GtkCTree *ctreelist = GTK_CTREE(addrbook.clist);
2777         GtkCList *clist = GTK_CLIST(addrbook.clist);
2778         AddressDataSource *ds = NULL;
2779         AdapterDSource *ads = NULL;
2780
2781         if( obj == NULL ) {
2782                 gtk_clist_clear(clist);
2783                 return;
2784         }
2785
2786         if( obj->type == ADDR_INTERFACE ) {
2787                 /* printf( "set_clist: loading datasource...\n" ); */
2788                 /* addressbook_node_load_datasource( GTK_CTREE(clist), obj ); */
2789                 return;
2790         }
2791
2792         gtk_clist_freeze(clist);
2793         gtk_clist_clear(clist);
2794
2795         if( obj->type == ADDR_DATASOURCE ) {
2796                 ads = ADAPTER_DSOURCE(obj);
2797                 ds = ADAPTER_DSOURCE(obj)->dataSource;
2798                 if( ds ) {
2799                         /* Load root folder */
2800                         ItemFolder *rootFolder = NULL;
2801                         rootFolder = addrindex_ds_get_root_folder( ds );
2802                         addressbook_folder_load_person(
2803                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
2804                         addressbook_folder_load_group(
2805                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
2806                 }
2807         }
2808         else {
2809                 if( obj->type == ADDR_ITEM_GROUP ) {
2810                         /* Load groups */
2811                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
2812                         addressbook_load_group( ctreelist, itemGroup );
2813                 }
2814                 else if( obj->type == ADDR_ITEM_FOLDER ) {
2815                         /* Load folders */
2816                         ItemFolder *itemFolder = ADAPTER_FOLDER(obj)->itemFolder;
2817                         addressbook_folder_load_person( ctreelist, itemFolder );
2818                         addressbook_folder_load_group( ctreelist, itemFolder );
2819                 }
2820         }
2821         /* gtk_clist_sort(clist); */
2822         gtk_clist_thaw(clist);
2823 }
2824
2825 /*
2826 * Call back function to free adaptor. Call back is setup by function
2827 * gtk_ctree_node_set_row_data_full() when node is populated. This function is
2828 * called when the address book tree widget node is removed by calling
2829 * function gtk_ctree_remove_node().
2830 * Enter: data Tree node's row data.
2831 */
2832 static void addressbook_free_treenode( gpointer data ) {
2833         AddressObject *ao;
2834
2835         ao = ( AddressObject * ) data;
2836         if( ao == NULL ) return;
2837         if( ao->type == ADDR_INTERFACE ) {
2838                 AdapterInterface *ai = ADAPTER_INTERFACE(ao);
2839                 addrbookctl_free_interface( ai );
2840         }
2841         else if( ao->type == ADDR_DATASOURCE ) {
2842                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
2843                 addrbookctl_free_datasource( ads );
2844         }
2845         else if( ao->type == ADDR_ITEM_FOLDER ) {
2846                 AdapterFolder *af = ADAPTER_FOLDER(ao);
2847                 addrbookctl_free_folder( af );
2848         }
2849         else if( ao->type == ADDR_ITEM_GROUP ) {
2850                 AdapterGroup *ag = ADAPTER_GROUP(ao);
2851                 addrbookctl_free_group( ag );
2852         }
2853 }
2854
2855 /*
2856 * Create new adaptor for specified data source.
2857 */
2858 AdapterDSource *addressbook_create_ds_adapter( AddressDataSource *ds,
2859                                 AddressObjectType otype, gchar *name )
2860 {
2861         AdapterDSource *adapter = g_new0( AdapterDSource, 1 );
2862         ADDRESS_OBJECT(adapter)->type = ADDR_DATASOURCE;
2863         ADDRESS_OBJECT_NAME(adapter) = g_strdup( name );
2864         adapter->dataSource = ds;
2865         adapter->subType = otype;
2866         return adapter;
2867 }
2868
2869 void addressbook_ads_set_name( AdapterDSource *adapter, gchar *value ) {
2870         ADDRESS_OBJECT_NAME(adapter) =
2871                 mgu_replace_string( ADDRESS_OBJECT_NAME(adapter), value );
2872 }
2873
2874 /*
2875  * Load tree from address index with the initial data.
2876  */
2877 static void addressbook_load_tree( void ) {
2878         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2879         GList *nodeIf, *nodeDS;
2880         AdapterInterface *adapter;
2881         AddressInterface *iface;
2882         AddressTypeControlItem *atci;
2883         AddressDataSource *ds;
2884         AdapterDSource *ads;
2885         GtkCTreeNode *node, *newNode;
2886         gchar *name;
2887
2888         nodeIf = _addressInterfaceList_;
2889         while( nodeIf ) {
2890                 adapter = nodeIf->data;
2891                 node = adapter->treeNode;
2892                 iface = adapter->interface;
2893                 atci = adapter->atci;
2894                 if( iface ) {
2895                         if( iface->useInterface ) {
2896                                 /* Load data sources below interface node */
2897                                 nodeDS = iface->listSource;
2898                                 while( nodeDS ) {
2899                                         ds = nodeDS->data;
2900                                         newNode = NULL;
2901                                         name = addrindex_ds_get_name( ds );
2902                                         ads = addressbook_create_ds_adapter(
2903                                                         ds, atci->objectType, name );
2904                                         newNode = addressbook_add_object(
2905                                                         node, ADDRESS_OBJECT(ads) );
2906                                         nodeDS = g_list_next( nodeDS );
2907                                 }
2908                                 gtk_ctree_expand( ctree, node );
2909                         }
2910                 }
2911                 nodeIf = g_list_next( nodeIf );
2912         }
2913 }
2914
2915 /*
2916  * Convert the old address book to new format.
2917  */
2918 static gboolean addressbook_convert( AddressIndex *addrIndex ) {
2919         gboolean retVal = FALSE;
2920         gboolean errFlag = TRUE;
2921         gchar *msg = NULL;
2922
2923         /* Read old address book, performing conversion */
2924         debug_print( "Reading and converting old address book...\n" );
2925         addrindex_set_file_name( addrIndex, ADDRESSBOOK_OLD_FILE );
2926         addrindex_read_data( addrIndex );
2927         if( addrIndex->retVal == MGU_NO_FILE ) {
2928                 /* We do not have a file - new user */
2929                 debug_print( "New user... create new books...\n" );
2930                 addrindex_create_new_books( addrIndex );
2931                 if( addrIndex->retVal == MGU_SUCCESS ) {
2932                         /* Save index file */
2933                         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
2934                         addrindex_save_data( addrIndex );
2935                         if( addrIndex->retVal == MGU_SUCCESS ) {
2936                                 retVal = TRUE;
2937                                 errFlag = FALSE;
2938                         }
2939                         else {
2940                                 msg = _( "New user, could not save index file." );
2941                         }
2942                 }
2943                 else {
2944                         msg = _( "New user, could not save address book files." );
2945                 }
2946         }
2947         else {
2948                 /* We have an old file */
2949                 if( addrIndex->wasConverted ) {
2950                         /* Converted successfully - save address index */
2951                         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
2952                         addrindex_save_data( addrIndex );
2953                         if( addrIndex->retVal == MGU_SUCCESS ) {
2954                                 msg = _( "Old address book converted successfully." );
2955                                 retVal = TRUE;
2956                                 errFlag = FALSE;
2957                         }
2958                         else {
2959                                 msg = _("Old address book converted,\n"
2960                                         "could not save new address index file" );
2961                         }
2962                 }
2963                 else {
2964                         /* File conversion failed - just create new books */
2965                         debug_print( "File conversion failed... just create new books...\n" );
2966                         addrindex_create_new_books( addrIndex );
2967                         if( addrIndex->retVal == MGU_SUCCESS ) {
2968                                 /* Save index */
2969                                 addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
2970                                 addrindex_save_data( addrIndex );
2971                                 if( addrIndex->retVal == MGU_SUCCESS ) {
2972                                         msg = _("Could not convert address book,\n"
2973                                                 "but created empty new address book files." );
2974                                         retVal = TRUE;
2975                                         errFlag = FALSE;
2976                                 }
2977                                 else {
2978                                         msg = _("Could not convert address book,\n"
2979                                                 "could not create new address book files." );
2980                                 }
2981                         }
2982                         else {
2983                                 msg = _("Could not convert address book\n"
2984                                         "and could not create new address book files." );
2985                         }
2986                 }
2987         }
2988         if( errFlag ) {
2989                 debug_print( "Error\n%s\n", msg );
2990                 alertpanel( _( "Addressbook conversion error" ), msg, _( "Close" ), NULL, NULL );
2991         }
2992         else if( msg ) {
2993                 debug_print( "Warning\n%s\n", msg );
2994                 alertpanel( _( "Addressbook conversion" ), msg, _( "Close" ), NULL, NULL );
2995         }
2996
2997         return retVal;
2998 }
2999
3000 void addressbook_read_file( void ) {
3001         AddressIndex *addrIndex = NULL;
3002
3003         debug_print( "Reading address index...\n" );
3004         if( _addressIndex_ ) {
3005                 debug_print( "address book already read!!!\n" );
3006                 return;
3007         }
3008
3009         addrIndex = addrindex_create_index();
3010         addrindex_initialize( addrIndex );
3011
3012         /* Use new address book index. */
3013         addrindex_set_file_path( addrIndex, get_rc_dir() );
3014         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
3015         addrindex_read_data( addrIndex );
3016         if( addrIndex->retVal == MGU_NO_FILE ) {
3017                 /* Conversion required */
3018                 debug_print( "Converting...\n" );
3019                 if( addressbook_convert( addrIndex ) ) {
3020                         _addressIndex_ = addrIndex;
3021                 }
3022         }
3023         else if( addrIndex->retVal == MGU_SUCCESS ) {
3024                 _addressIndex_ = addrIndex;
3025         }
3026         else {
3027                 /* Error reading address book */
3028                 debug_print( "Could not read address index.\n" );
3029                 addrindex_print_index( addrIndex, stdout );
3030                 alertpanel( _( "Addressbook Error" ),
3031                             _( "Could not read address index" ),
3032                             _( "Close" ), NULL, NULL );
3033         }
3034         debug_print( "done.\n" );
3035 }
3036
3037 /*
3038 * Add object into the address index tree widget.
3039 * Enter: node   Parent node.
3040 *        obj    Object to add.
3041 * Return: Node that was added, or NULL if object not added.
3042 */
3043 static GtkCTreeNode *addressbook_add_object(GtkCTreeNode *node,
3044                                             AddressObject *obj)
3045 {
3046         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
3047         GtkCTreeNode *added;
3048         AddressObject *pobj;
3049         AddressObjectType otype;
3050         AddressTypeControlItem *atci = NULL;
3051
3052         g_return_val_if_fail(node != NULL, NULL);
3053         g_return_val_if_fail(obj  != NULL, NULL);
3054
3055         pobj = gtk_ctree_node_get_row_data(ctree, node);
3056         g_return_val_if_fail(pobj != NULL, NULL);
3057
3058         /* Determine object type to be displayed */
3059         if( obj->type == ADDR_DATASOURCE ) {
3060                 otype = ADAPTER_DSOURCE(obj)->subType;
3061         }
3062         else {
3063                 otype = obj->type;
3064         }
3065
3066         /* Handle any special conditions. */
3067         added = node;
3068         atci = addrbookctl_lookup( otype );
3069         if( atci ) {
3070                 if( atci->showInTree ) {
3071                         /* Add object to tree */
3072                         gchar **name;
3073                         name = &obj->name;
3074                         added = gtk_ctree_insert_node( ctree, node, NULL, name, FOLDER_SPACING,
3075                                 atci->iconXpm, atci->maskXpm, atci->iconXpmOpen, atci->maskXpmOpen,
3076                                 atci->treeLeaf, atci->treeExpand );
3077                         gtk_ctree_node_set_row_data_full( ctree, added, obj,
3078                                 addressbook_free_treenode );
3079                 }
3080         }
3081
3082         gtk_sctree_sort_node(ctree, node);
3083
3084         return added;
3085 }
3086
3087 /*
3088 * Add group into the address index tree.
3089 * Enter: node      Parent node.
3090 *        ds        Data source.
3091 *        itemGroup Group to add.
3092 * Return: Inserted node.
3093 */
3094 static GtkCTreeNode *addressbook_node_add_group( GtkCTreeNode *node, AddressDataSource *ds, ItemGroup *itemGroup ) {
3095         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
3096         GtkCTreeNode *newNode;
3097         AdapterGroup *adapter;
3098         AddressTypeControlItem *atci = NULL;
3099         gchar **name;
3100
3101         if( ds == NULL ) return NULL;
3102         if( node == NULL || itemGroup == NULL ) return NULL;
3103
3104         name = &itemGroup->obj.name;
3105
3106         atci = addrbookctl_lookup( ADDR_ITEM_GROUP );
3107
3108         adapter = g_new0( AdapterGroup, 1 );
3109         ADDRESS_OBJECT_TYPE(adapter) = ADDR_ITEM_GROUP;
3110         ADDRESS_OBJECT_NAME(adapter) = g_strdup( ADDRITEM_NAME(itemGroup) );
3111         adapter->itemGroup = itemGroup;
3112
3113         newNode = gtk_ctree_insert_node( ctree, node, NULL, name, FOLDER_SPACING,
3114                         atci->iconXpm, atci->maskXpm, atci->iconXpm, atci->maskXpm,
3115                         atci->treeLeaf, atci->treeExpand );
3116         gtk_ctree_node_set_row_data_full( ctree, newNode, adapter,
3117                 addressbook_free_treenode );
3118         gtk_sctree_sort_node( ctree, node );
3119         return newNode;
3120 }
3121
3122 /*
3123 * Add folder into the address index tree.
3124 * Enter: node       Parent node.
3125 *        ds         Data source.
3126 *        itemFolder Folder to add.
3127 *        otype      Object type to display.
3128 * Return: Inserted node.
3129 */
3130 static GtkCTreeNode *addressbook_node_add_folder(
3131                 GtkCTreeNode *node, AddressDataSource *ds,
3132                 ItemFolder *itemFolder, AddressObjectType otype )
3133 {
3134         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
3135         GtkCTreeNode *newNode = NULL;
3136         AdapterFolder *adapter;
3137         AddressTypeControlItem *atci = NULL;
3138         GList *listItems = NULL;
3139         gchar **name;
3140         ItemFolder *rootFolder;
3141
3142         if( ds == NULL ) return NULL;
3143         if( node == NULL || itemFolder == NULL ) return NULL;
3144
3145         /* Determine object type */
3146         atci = addrbookctl_lookup( otype );
3147         if( atci == NULL ) return NULL;
3148
3149         rootFolder = addrindex_ds_get_root_folder( ds );
3150         if( itemFolder == rootFolder ) {
3151                 newNode = node;
3152         }
3153         else {
3154                 name = &itemFolder->obj.name;
3155
3156                 adapter = g_new0( AdapterFolder, 1 );
3157                 ADDRESS_OBJECT_TYPE(adapter) = ADDR_ITEM_FOLDER;
3158                 ADDRESS_OBJECT_NAME(adapter) = g_strdup( ADDRITEM_NAME(itemFolder) );
3159                 adapter->itemFolder = itemFolder;
3160
3161                 newNode = gtk_ctree_insert_node( ctree, node, NULL, name, FOLDER_SPACING,
3162                                 atci->iconXpm, atci->maskXpm, atci->iconXpm, atci->maskXpm,
3163                                 atci->treeLeaf, atci->treeExpand );
3164                 if( newNode ) {
3165                         gtk_ctree_node_set_row_data_full( ctree, newNode, adapter,
3166                                 addressbook_free_treenode );
3167                 }
3168         }
3169
3170         listItems = itemFolder->listFolder;
3171         while( listItems ) {
3172                 ItemFolder *item = listItems->data;
3173                 addressbook_node_add_folder( newNode, ds, item, otype );
3174                 listItems = g_list_next( listItems );
3175         }
3176         listItems = itemFolder->listGroup;
3177         while( listItems ) {
3178                 ItemGroup *item = listItems->data;
3179                 addressbook_node_add_group( newNode, ds, item );
3180                 listItems = g_list_next( listItems );
3181         }
3182         gtk_sctree_sort_node( ctree, node );
3183         return newNode;
3184 }
3185
3186 void addressbook_export_to_file( void ) {
3187         if( _addressIndex_ ) {
3188                 /* Save all new address book data */
3189                 debug_print( "Saving address books...\n" );
3190                 addrindex_save_all_books( _addressIndex_ );
3191
3192                 debug_print( "Exporting addressbook to file...\n" );
3193                 addrindex_save_data( _addressIndex_ );
3194                 if( _addressIndex_->retVal != MGU_SUCCESS ) {
3195                         addrindex_print_index( _addressIndex_, stdout );
3196                 }
3197
3198                 /* Notify address completion of new data */
3199                 invalidate_address_completion();
3200         }
3201 }
3202
3203 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
3204 {
3205         if (event && event->keyval == GDK_Escape)
3206                 addressbook_close();
3207 }
3208
3209 /*
3210 * Comparison using cell contents (text in first column). Used for sort
3211 * address index widget.
3212 */
3213 static gint addressbook_treenode_compare_func(
3214         GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 )
3215 {
3216         GtkCell *cell1 = ((GtkCListRow *)ptr1)->cell;
3217         GtkCell *cell2 = ((GtkCListRow *)ptr2)->cell;
3218         gchar *name1 = NULL, *name2 = NULL;
3219         if( cell1 ) name1 = cell1->u.text;
3220         if( cell2 ) name2 = cell2->u.text;
3221         if( ! name1 ) return ( name2 != NULL );
3222         if( ! name2 ) return -1;
3223         return strcasecmp( name1, name2 );
3224 }
3225
3226 /*
3227 * Comparison using object names and types.
3228 */
3229 static gint addressbook_list_compare_func(
3230         GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 )
3231 {
3232         AddrItemObject *aio1 = ((GtkCListRow *)ptr1)->data;
3233         AddrItemObject *aio2 = ((GtkCListRow *)ptr2)->data;
3234         gchar *name1 = NULL, *name2 = NULL;
3235
3236         /* Order by cell contents */
3237         name1 = ADDRITEM_NAME( aio1 );
3238         name2 = ADDRITEM_NAME( aio2 );
3239
3240         if( aio1->type == aio2->type ) {
3241                 /* Order by name */
3242                 if( ! name1 ) return ( name2 != NULL );
3243                 if( ! name2 ) return -1;
3244                 return strcasecmp( name1, name2 );
3245         }
3246         else {
3247                 /* Order groups before person */
3248                 if( aio1->type == ITEMTYPE_GROUP ) {
3249                         return -1;
3250                 }
3251                 else if( aio2->type == ITEMTYPE_GROUP ) {
3252                         return 1;
3253                 }
3254                 return 0;
3255         }
3256 }
3257
3258 static void addressbook_new_book_cb( gpointer data, guint action, GtkWidget *widget ) {
3259         AdapterDSource *ads;
3260         AdapterInterface *adapter;
3261         GtkCTreeNode *newNode;
3262
3263         adapter = addrbookctl_find_interface( ADDR_IF_BOOK );
3264         if( adapter == NULL ) return;
3265         ads = addressbook_edit_book( _addressIndex_, NULL );
3266         if( ads ) {
3267                 newNode = addressbook_add_object( adapter->treeNode, ADDRESS_OBJECT(ads) );
3268                 if( newNode ) {
3269                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), newNode );
3270                         addrbook.treeSelected = newNode;
3271                 }
3272         }
3273 }
3274
3275 static void addressbook_new_vcard_cb( gpointer data, guint action, GtkWidget *widget ) {
3276         AdapterDSource *ads;
3277         AdapterInterface *adapter;
3278         GtkCTreeNode *newNode;
3279
3280         adapter = addrbookctl_find_interface( ADDR_IF_VCARD );
3281         if( adapter == NULL ) return;
3282         ads = addressbook_edit_vcard( _addressIndex_, NULL );
3283         if( ads ) {
3284                 newNode = addressbook_add_object( adapter->treeNode, ADDRESS_OBJECT(ads) );
3285                 if( newNode ) {
3286                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), newNode );
3287                         addrbook.treeSelected = newNode;
3288                 }
3289         }
3290 }
3291
3292 #ifdef USE_JPILOT
3293 static void addressbook_new_jpilot_cb( gpointer data, guint action, GtkWidget *widget ) {
3294         AdapterDSource *ads;
3295         AdapterInterface *adapter;
3296         AddressInterface *iface;
3297         GtkCTreeNode *newNode;
3298
3299         adapter = addrbookctl_find_interface( ADDR_IF_JPILOT );
3300         if( adapter == NULL ) return;
3301         iface = adapter->interface;
3302         if( ! iface->haveLibrary ) return;
3303         ads = addressbook_edit_jpilot( _addressIndex_, NULL );
3304         if( ads ) {
3305