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