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