LDAP/UI code separation. Fix mem leak.
[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_gtk.h"
58 #include "procmime.h"
59 #include "utils.h"
60 #include "gtkutils.h"
61 #include "codeconv.h"
62 #include "about.h"
63 #include "addr_compl.h"
64
65 #include "mgutils.h"
66 #include "addressitem.h"
67 #include "addritem.h"
68 #include "addrcache.h"
69 #include "addrbook.h"
70 #include "addrindex.h"
71 #include "addressadd.h"
72 #include "vcard.h"
73 #include "editvcard.h"
74 #include "editgroup.h"
75 #include "editaddress.h"
76 #include "editbook.h"
77 #include "importldif.h"
78 #include "importmutt.h"
79 #include "importpine.h"
80
81 #ifdef USE_JPILOT
82 #include "jpilot.h"
83 #include "editjpilot.h"
84 #endif
85
86 #ifdef USE_LDAP
87 #include <pthread.h>
88 #include "syldap.h"
89 #include "editldap.h"
90
91 #define ADDRESSBOOK_LDAP_BUSYMSG "Busy"
92 #endif
93
94 #include "addrselect.h"
95 #include "addrclip.h"
96 #include "addrgather.h"
97 #include "adbookbase.h"
98 #include "exphtmldlg.h"
99
100 typedef enum
101 {
102         COL_NAME        = 0,
103         COL_ADDRESS     = 1,
104         COL_REMARKS     = 2
105 } AddressBookColumnPos;
106
107 #define N_COLS  3
108 #define COL_NAME_WIDTH          164
109 #define COL_ADDRESS_WIDTH       156
110
111 #define COL_FOLDER_WIDTH        170
112 #define ADDRESSBOOK_WIDTH       640
113 #define ADDRESSBOOK_HEIGHT      360
114
115 #define ADDRESSBOOK_MSGBUF_SIZE 2048
116
117 static GdkPixmap *folderxpm;
118 static GdkBitmap *folderxpmmask;
119 static GdkPixmap *folderopenxpm;
120 static GdkBitmap *folderopenxpmmask;
121 static GdkPixmap *groupxpm;
122 static GdkBitmap *groupxpmmask;
123 static GdkPixmap *interfacexpm;
124 static GdkBitmap *interfacexpmmask;
125 static GdkPixmap *bookxpm;
126 static GdkBitmap *bookxpmmask;
127 static GdkPixmap *addressxpm;
128 static GdkBitmap *addressxpmmask;
129 static GdkPixmap *vcardxpm;
130 static GdkBitmap *vcardxpmmask;
131 static GdkPixmap *jpilotxpm;
132 static GdkBitmap *jpilotxpmmask;
133 static GdkPixmap *categoryxpm;
134 static GdkBitmap *categoryxpmmask;
135 static GdkPixmap *ldapxpm;
136 static GdkBitmap *ldapxpmmask;
137
138 /* Message buffer */
139 static gchar addressbook_msgbuf[ ADDRESSBOOK_MSGBUF_SIZE ];
140
141 /* Address list selection */
142 static AddrSelectList *_addressSelect_ = NULL;
143 static AddressClipboard *_clipBoard_ = NULL;
144
145 /* Address index file and interfaces */
146 static AddressIndex *_addressIndex_ = NULL;
147 static GList *_addressInterfaceList_ = NULL;
148 static GList *_addressIFaceSelection_ = NULL;
149 #define ADDRESSBOOK_IFACE_SELECTION "1/y,3/y,4/y,2/n"
150
151 static AddressBook_win addrbook;
152
153 static GHashTable *_addressBookTypeHash_ = NULL;
154 static GList *_addressBookTypeList_ = NULL;
155
156 static void addressbook_create                  (void);
157 static gint addressbook_close                   (void);
158 static void addressbook_button_set_sensitive    (void);
159
160 /* callback functions */
161 static void addressbook_del_clicked             (GtkButton      *button,
162                                                  gpointer        data);
163 static void addressbook_reg_clicked             (GtkButton      *button,
164                                                  gpointer        data);
165 static void addressbook_to_clicked              (GtkButton      *button,
166                                                  gpointer        data);
167 static void addressbook_lup_clicked             (GtkButton      *button,
168                                                  gpointer       data);
169
170 static void addressbook_tree_selected           (GtkCTree       *ctree,
171                                                  GtkCTreeNode   *node,
172                                                  gint            column,
173                                                  gpointer        data);
174 static void addressbook_list_selected           (GtkCList       *clist,
175                                                  gint            row,
176                                                  gint            column,
177                                                  GdkEvent       *event,
178                                                  gpointer        data);
179 static void addressbook_list_row_selected       (GtkCTree       *clist,
180                                                  GtkCTreeNode   *node,
181                                                  gint            column,
182                                                  gpointer        data);
183 static void addressbook_list_row_unselected     (GtkCTree       *clist,
184                                                  GtkCTreeNode   *node,
185                                                  gint            column,
186                                                  gpointer        data);
187 static void addressbook_person_expand_node      (GtkCTree       *ctree,
188                                                  GList          *node,
189                                                  gpointer       *data );
190 static void addressbook_person_collapse_node    (GtkCTree       *ctree,
191                                                  GList          *node,
192                                                  gpointer       *data );
193 static void addressbook_entry_gotfocus          (GtkWidget      *widget);
194
195 #if 0
196 static void addressbook_entry_changed           (GtkWidget      *widget);
197 #endif
198
199 static void addressbook_list_button_pressed     (GtkWidget      *widget,
200                                                  GdkEventButton *event,
201                                                  gpointer        data);
202 static void addressbook_list_button_released    (GtkWidget      *widget,
203                                                  GdkEventButton *event,
204                                                  gpointer        data);
205 static void addressbook_tree_button_pressed     (GtkWidget      *ctree,
206                                                  GdkEventButton *event,
207                                                  gpointer        data);
208 static void addressbook_tree_button_released    (GtkWidget      *ctree,
209                                                  GdkEventButton *event,
210                                                  gpointer        data);
211 static void addressbook_popup_close             (GtkMenuShell   *menu_shell,
212                                                  gpointer        data);
213
214 static void addressbook_new_folder_cb           (gpointer        data,
215                                                  guint           action,
216                                                  GtkWidget      *widget);
217 static void addressbook_new_group_cb            (gpointer        data,
218                                                  guint           action,
219                                                  GtkWidget      *widget);
220 static void addressbook_treenode_edit_cb        (gpointer        data,
221                                                  guint           action,
222                                                  GtkWidget      *widget);
223 static void addressbook_treenode_delete_cb      (gpointer        data,
224                                                  guint           action,
225                                                  GtkWidget      *widget);
226
227 static void addressbook_change_node_name        (GtkCTreeNode   *node,
228                                                  const gchar    *name);
229
230 static void addressbook_new_address_cb          (gpointer        data,
231                                                  guint           action,
232                                                  GtkWidget      *widget);
233 static void addressbook_edit_address_cb         (gpointer        data,
234                                                  guint           action,
235                                                  GtkWidget      *widget);
236 static void addressbook_delete_address_cb       (gpointer        data,
237                                                  guint           action,
238                                                  GtkWidget      *widget);
239
240 static void close_cb                            (gpointer        data,
241                                                  guint           action,
242                                                  GtkWidget      *widget);
243 static void addressbook_file_save_cb            (gpointer        data,
244                                                  guint           action,
245                                                  GtkWidget      *widget);
246
247 /* Data source edit stuff */
248 static void addressbook_new_book_cb             (gpointer        data,
249                                                  guint           action,
250                                                  GtkWidget      *widget);
251 static void addressbook_new_vcard_cb            (gpointer        data,
252                                                  guint           action,
253                                                  GtkWidget      *widget);
254
255 #ifdef USE_JPILOT
256 static void addressbook_new_jpilot_cb           (gpointer        data,
257                                                  guint           action,
258                                                  GtkWidget      *widget);
259 #endif
260
261 #ifdef USE_LDAP
262 static void addressbook_new_ldap_cb             (gpointer        data,
263                                                  guint           action,
264                                                  GtkWidget      *widget);
265 #endif
266
267 static void addressbook_set_clist               (AddressObject  *obj);
268
269 static void addressbook_load_tree               (void);
270 void addressbook_read_file                      (void);
271
272 static GtkCTreeNode *addressbook_add_object     (GtkCTreeNode   *node,
273                                                  AddressObject  *obj);
274 static void addressbook_treenode_remove_item    ( void );
275
276 static AddressDataSource *addressbook_find_datasource
277                                                 (GtkCTreeNode   *node );
278
279 static AddressBookFile *addressbook_get_book_file(void);
280
281 static GtkCTreeNode *addressbook_node_add_folder
282                                                 (GtkCTreeNode   *node,
283                                                 AddressDataSource *ds,
284                                                 ItemFolder      *itemFolder,
285                                                 AddressObjectType otype);
286 static GtkCTreeNode *addressbook_node_add_group (GtkCTreeNode   *node,
287                                                 AddressDataSource *ds,
288                                                 ItemGroup       *itemGroup);
289 static void addressbook_tree_remove_children    (GtkCTree       *ctree,
290                                                 GtkCTreeNode    *parent);
291 static void addressbook_move_nodes_up           (GtkCTree       *ctree,
292                                                 GtkCTreeNode    *node);
293 static GtkCTreeNode *addressbook_find_group_node (GtkCTreeNode  *parent,
294                                                    ItemGroup    *group);
295 static void key_pressed                         (GtkWidget      *widget,
296                                                  GdkEventKey    *event,
297                                                  gpointer        data);
298 static gint addressbook_treenode_compare_func   (GtkCList       *clist,
299                                                  gconstpointer   ptr1,
300                                                  gconstpointer   ptr2);
301 static gint addressbook_list_compare_func       (GtkCList       *clist,
302                                                  gconstpointer   ptr1,
303                                                  gconstpointer   ptr2);
304
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                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
807                             "%s: %s", name, msg );
808         }
809         else {
810                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
811                             "%s", msg );
812         }
813         addressbook_status_show( addressbook_msgbuf );
814 }
815
816 static void addressbook_ds_show_message( AddressDataSource *ds ) {
817         gint retVal;
818         gchar *name;
819         *addressbook_msgbuf = '\0';
820         if( ds ) {
821                 name = addrindex_ds_get_name( ds );
822                 retVal = addrindex_ds_get_status_code( ds );
823                 if( retVal == MGU_SUCCESS ) {
824                         g_snprintf( addressbook_msgbuf,
825                                     sizeof(addressbook_msgbuf), "%s", name );
826                 }
827                 else {
828                         g_snprintf( addressbook_msgbuf, 
829                                     sizeof(addressbook_msgbuf), "%s: %s", name,
830                                     mgu_error2string( retVal ) );
831                 }
832         }
833         addressbook_status_show( addressbook_msgbuf );
834 }
835
836 static void addressbook_button_set_sensitive(void)
837 {
838         gboolean to_sens  = FALSE;
839         gboolean cc_sens  = FALSE;
840         gboolean bcc_sens = FALSE;
841
842         if (!addrbook.window) return;
843
844         if (addrbook.target_compose) {
845                 to_sens = TRUE;
846                 cc_sens = TRUE;
847 #ifndef CLAWS           
848                 if (addrbook.target_compose->use_bcc)
849 #endif                  
850                         bcc_sens = TRUE;
851         }
852
853         gtk_widget_set_sensitive(addrbook.to_btn, to_sens);
854         gtk_widget_set_sensitive(addrbook.cc_btn, cc_sens);
855         gtk_widget_set_sensitive(addrbook.bcc_btn, bcc_sens);
856 }
857
858 /*
859 * Delete one or more objects from address list.
860 */
861 static void addressbook_del_clicked(GtkButton *button, gpointer data)
862 {
863         GtkCTree *clist = GTK_CTREE(addrbook.clist);
864         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
865         AddressObject *pobj;
866         AdapterDSource *ads = NULL;
867         GtkCTreeNode *nodeList;
868         gboolean procFlag;
869         AlertValue aval;
870         AddressBookFile *abf = NULL;
871         AddressDataSource *ds = NULL;
872         AddressInterface *iface;
873         AddrItemObject *aio;
874         AddrSelectItem *item;
875         GList *list, *node;
876
877         pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened );
878         g_return_if_fail(pobj != NULL);
879
880         /* Test whether anything selected for deletion */
881         nodeList = addrbook.listSelected;
882         aio = gtk_ctree_node_get_row_data( clist, nodeList );
883         if( aio == NULL) return;
884         ds = addressbook_find_datasource( addrbook.treeSelected );
885         if( ds == NULL ) return;
886
887         /* Test for read only */
888         iface = ds->interface;
889         if( iface->readOnly ) {
890                 alertpanel( _("Delete address(es)"),
891                         _("This address data is readonly and cannot be deleted."),
892                         _("Close"), NULL, NULL );
893                 return;
894         }
895
896         /* Test whether Ok to proceed */
897         procFlag = FALSE;
898         if( pobj->type == ADDR_DATASOURCE ) {
899                 ads = ADAPTER_DSOURCE(pobj);
900                 if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
901         }
902         else if( pobj->type == ADDR_ITEM_FOLDER ) {
903                 procFlag = TRUE;
904         }
905         else if( pobj->type == ADDR_ITEM_GROUP ) {
906                 procFlag = TRUE;
907         }
908         if( ! procFlag ) return;
909         abf = ds->rawDataSource;
910         if( abf == NULL ) return;
911
912         /* Confirm deletion */
913         aval = alertpanel( _("Delete address(es)"),
914                         _("Really delete the address(es)?"),
915                         _("Yes"), _("No"), NULL );
916         if( aval != G_ALERTDEFAULT ) return;
917
918         /* Process deletions */
919         if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
920                 /* Items inside folders */
921                 list = addrselect_get_list( _addressSelect_ );
922                 node = list;
923                 while( node ) {
924                         item = node->data;
925                         node = g_list_next( node );
926                         aio = ( AddrItemObject * ) item->addressItem;
927                         if( aio->type == ADDR_ITEM_GROUP ) {
928                                 ItemGroup *item = ( ItemGroup * ) aio;
929                                 GtkCTreeNode *nd = NULL;
930
931                                 nd = addressbook_find_group_node( addrbook.opened, item );
932                                 item = addrbook_remove_group( abf, item );
933                                 if( item ) {
934                                         addritem_free_item_group( item );
935                                 }
936                                 /* Remove group from parent node */
937                                 gtk_ctree_remove_node( ctree, nd );
938                         }
939                         else if( aio->type == ADDR_ITEM_PERSON ) {
940                                 ItemPerson *item = ( ItemPerson * ) aio;
941                                 item = addrbook_remove_person( abf, item );
942                                 if( item ) {
943                                         addritem_free_item_person( item );
944                                 }
945                         }
946                         else if( aio->type == ADDR_ITEM_EMAIL ) {
947                                 ItemEMail *item = ( ItemEMail * ) aio;
948                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
949                                 item = addrbook_person_remove_email( abf, person, item );
950                                 if( item ) {
951                                         addritem_free_item_email( item );
952                                 }
953                         }
954                 }
955                 g_list_free( list );
956                 addressbook_list_select_clear();
957                 gtk_ctree_select( ctree, addrbook.opened);
958                 return;
959         }
960         else if( pobj->type == ADDR_ITEM_GROUP ) {
961                 /* Items inside groups */
962                 list = addrselect_get_list( _addressSelect_ );
963                 node = list;
964                 while( node ) {
965                         item = node->data;
966                         node = g_list_next( node );
967                         aio = ( AddrItemObject * ) item->addressItem;
968                         if( aio->type == ADDR_ITEM_EMAIL ) {
969                                 ItemEMail *item = ( ItemEMail * ) aio;
970                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
971                                 item = addrbook_person_remove_email( abf, person, item );
972                                 if( item ) {
973                                         addritem_free_item_email( item );
974                                 }
975                         }
976                 }
977                 g_list_free( list );
978                 addressbook_list_select_clear();
979                 gtk_ctree_select( ctree, addrbook.opened);
980                 return;
981         }
982
983         gtk_ctree_node_set_row_data( clist, nodeList, NULL );
984         gtk_ctree_remove_node( clist, nodeList );
985
986 }
987
988 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
989 {
990         addressbook_new_address_cb( NULL, 0, NULL );
991 }
992
993 gchar *addressbook_format_address( AddrItemObject * aio ) {
994         gchar *buf = NULL;
995         gchar *name = NULL;
996         gchar *address = NULL;
997
998         if( aio->type == ADDR_ITEM_EMAIL ) {
999                 ItemPerson *person = NULL;
1000                 ItemEMail *email = ( ItemEMail * ) aio;
1001
1002                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
1003                 if( email->address ) {
1004                         if( ADDRITEM_NAME(email) ) {
1005                                 name = ADDRITEM_NAME(email);
1006                                 if( *name == '\0' ) {
1007                                         name = ADDRITEM_NAME(person);
1008                                 }
1009                         }
1010                         else if( ADDRITEM_NAME(person) ) {
1011                                 name = ADDRITEM_NAME(person);
1012                         }
1013                         else {
1014                                 buf = g_strdup( email->address );
1015                         }
1016                         address = email->address;
1017                 }
1018         }
1019         else if( aio->type == ADDR_ITEM_PERSON ) {
1020                 ItemPerson *person = ( ItemPerson * ) aio;
1021                 GList *node = person->listEMail;
1022
1023                 name = ADDRITEM_NAME(person);
1024                 if( node ) {
1025                         ItemEMail *email = ( ItemEMail * ) node->data;
1026                         address = email->address;
1027                 }
1028         }
1029         if( address ) {
1030                 if( name && name[0] != '\0' ) {
1031                         if( strchr_with_skip_quote( name, '"', ',' ) )
1032                                 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
1033                         else
1034                                 buf = g_strdup_printf( "%s <%s>", name, address );
1035                 }
1036                 else {
1037                         buf = g_strdup( address );
1038                 }
1039         }
1040
1041         return buf;
1042 }
1043
1044 static void addressbook_to_clicked(GtkButton *button, gpointer data)
1045 {
1046         GList *list, *node;
1047         Compose *compose;
1048         AddrSelectItem *item;
1049         AddrItemObject *aio;
1050         gchar *addr;
1051
1052         compose = addrbook.target_compose;
1053         if( ! compose ) return;
1054
1055         /* Nothing selected, but maybe there is something in text entry */
1056         addr = gtk_entry_get_text( GTK_ENTRY( addrbook.entry) );
1057         if ( addr ) {
1058                 compose_entry_append(
1059                         compose, addr, (ComposeEntryType)data );
1060         }
1061
1062         /* Select from address list */
1063         list = addrselect_get_list( _addressSelect_ );
1064         node = list;
1065         while( node ) {
1066                 item = node->data;
1067                 node = g_list_next( node );
1068                 aio = item->addressItem;
1069                 if( aio->type == ADDR_ITEM_PERSON ||
1070                     aio->type == ADDR_ITEM_EMAIL ) {
1071                         addr = addressbook_format_address( aio );
1072                         compose_entry_append(
1073                                 compose, addr, (ComposeEntryType) data );
1074                         g_free( addr );
1075                 }
1076                 else if( aio->type == ADDR_ITEM_GROUP ) {
1077                         ItemGroup *group = ( ItemGroup * ) aio;
1078                         GList *nodeMail = group->listEMail;
1079                         while( nodeMail ) {
1080                                 ItemEMail *email = nodeMail->data;
1081
1082                                 addr = addressbook_format_address(
1083                                                 ( AddrItemObject * ) email );
1084                                 compose_entry_append(
1085                                         compose, addr, (ComposeEntryType) data );
1086                                 g_free( addr );
1087                                 nodeMail = g_list_next( nodeMail );
1088                         }
1089                 }
1090         }
1091         g_list_free( list );
1092 }
1093
1094 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
1095         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",   sensitive );
1096         menu_set_sensitive( addrbook.menu_factory, "/File/Delete", sensitive );
1097
1098         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",    sensitive );
1099         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",   sensitive );
1100         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",  sensitive );
1101         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address",  sensitive );
1102
1103         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", sensitive );
1104         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   sensitive );
1105         menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  sensitive );
1106         gtk_widget_set_sensitive( addrbook.reg_btn, sensitive );
1107         gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
1108 }
1109
1110 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCTreeNode *node ) {
1111         gboolean canEdit = FALSE;
1112         gboolean canAdd = FALSE;
1113         gboolean canEditTr = TRUE;
1114         gboolean editAddress = FALSE;
1115         gboolean canExport = TRUE;
1116         AddressTypeControlItem *atci = NULL;
1117         AddressDataSource *ds = NULL;
1118         AddressInterface *iface = NULL;
1119
1120         if( obj == NULL ) return;
1121         if( obj->type == ADDR_INTERFACE ) {
1122                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
1123                 iface = adapter->interface;
1124                 if( iface ) {
1125                         if( iface->haveLibrary ) {
1126                                 /* Enable appropriate File / New command */
1127                                 atci = adapter->atci;
1128                                 menu_set_sensitive( addrbook.menu_factory, atci->menuCommand, TRUE );
1129                         }
1130                 }
1131                 canEditTr = canExport = FALSE;
1132         }
1133         else if( obj->type == ADDR_DATASOURCE ) {
1134                 AdapterDSource *ads = ADAPTER_DSOURCE(obj);
1135                 ds = ads->dataSource;
1136                 iface = ds->interface;
1137                 if( ! iface->readOnly ) {
1138                         canAdd = canEdit = editAddress = TRUE;
1139                 }
1140                 if( ! iface->haveLibrary ) {
1141                         canAdd = canEdit = editAddress = canExport = FALSE;
1142                 }
1143         }
1144         else if( obj->type == ADDR_ITEM_FOLDER ) {
1145                 ds = addressbook_find_datasource( addrbook.treeSelected );
1146                 if( ds ) {
1147                         iface = ds->interface;
1148                         if( iface->readOnly ) {
1149                                 canEditTr = FALSE;
1150                         }
1151                         else {
1152                                 canAdd = editAddress = TRUE;
1153                         }
1154                 }
1155         }
1156         else if( obj->type == ADDR_ITEM_GROUP ) {
1157                 ds = addressbook_find_datasource( addrbook.treeSelected );
1158                 if( ds ) {
1159                         iface = ds->interface;
1160                         if( ! iface->readOnly ) {
1161                                 editAddress = TRUE;
1162                         }
1163                 }
1164         }
1165
1166         if( addrbook.listSelected == NULL ) canEdit = FALSE;
1167
1168         /* Enable add */
1169         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", editAddress );
1170         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   canAdd );
1171         menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  canAdd );
1172         gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
1173
1174         /* Enable edit */
1175         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1176         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canEdit );
1177         gtk_widget_set_sensitive( addrbook.del_btn, canEdit );
1178
1179         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",      canEditTr );
1180         menu_set_sensitive( addrbook.menu_factory, "/File/Delete",    canEditTr );
1181
1182         /* Export data */
1183         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export HTML...", canExport );
1184 }
1185
1186 static void addressbook_tree_selected(GtkCTree *ctree, GtkCTreeNode *node,
1187                                       gint column, gpointer data)
1188 {
1189         AddressObject *obj = NULL;
1190         AdapterDSource *ads = NULL;
1191         AddressDataSource *ds = NULL;
1192         ItemFolder *rootFolder = NULL;
1193
1194         addrbook.treeSelected = node;
1195         addrbook.listSelected = NULL;
1196         addressbook_status_show( "" );
1197         if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
1198
1199         if( addrbook.clist ) gtk_clist_clear( GTK_CLIST(addrbook.clist) );
1200         if( node ) obj = gtk_ctree_node_get_row_data( ctree, node );
1201         if( obj == NULL ) return;
1202
1203         addrbook.opened = node;
1204
1205         if( obj->type == ADDR_DATASOURCE ) {
1206                 /* Read from file */
1207                 static gboolean tVal = TRUE;
1208
1209                 ads = ADAPTER_DSOURCE(obj);
1210                 if( ads == NULL ) return;
1211                 ds = ads->dataSource;
1212                 if( ds == NULL ) return;                
1213
1214                 if( addrindex_ds_get_modify_flag( ds ) ) {
1215                         addrindex_ds_read_data( ds );
1216                 }
1217
1218                 if( ! addrindex_ds_get_read_flag( ds ) ) {
1219                         addrindex_ds_read_data( ds );
1220                 }
1221                 addressbook_ds_show_message( ds );
1222
1223                 if( ! addrindex_ds_get_access_flag( ds ) ) {
1224                         /* Remove existing folders and groups */
1225                         gtk_clist_freeze( GTK_CLIST(ctree) );
1226                         addressbook_tree_remove_children( ctree, node );
1227                         gtk_clist_thaw( GTK_CLIST(ctree) );
1228
1229                         /* Load folders into the tree */
1230                         rootFolder = addrindex_ds_get_root_folder( ds );
1231                         if( ds->type == ADDR_IF_JPILOT ) {
1232                                 addressbook_node_add_folder( node, ds, rootFolder, ADDR_CATEGORY );
1233                         }
1234                         else {
1235                                 addressbook_node_add_folder( node, ds, rootFolder, ADDR_ITEM_FOLDER );
1236                         }
1237                         addrindex_ds_set_access_flag( ds, &tVal );
1238                         gtk_ctree_expand( ctree, node );
1239                 }
1240         }
1241
1242         /* Update address list */
1243         addressbook_set_clist( obj );
1244
1245         /* Setup main menu selections */
1246         addressbook_menubar_set_sensitive( FALSE );
1247         addressbook_menuitem_set_sensitive( obj, node );
1248
1249         addressbook_list_select_clear();
1250 }
1251
1252 /*
1253 * Setup address list popup menu.
1254 * Enter: pobj  Parent address object in address tree.
1255 *        obj   Address object in address list.
1256 */
1257 static void addressbook_list_menu_setup( void ) {
1258         GtkCTree *clist = NULL;
1259         AddressObject *pobj = NULL;
1260         AddressObject *obj = NULL;
1261         AdapterDSource *ads = NULL;
1262         AddressInterface *iface = NULL;
1263         AddressDataSource *ds = NULL;
1264         gboolean canEdit = FALSE;
1265         gboolean canDelete = FALSE;
1266         gboolean canCut = FALSE;
1267         gboolean canCopy = FALSE;
1268         gboolean canPaste = FALSE;
1269
1270         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1271         if( pobj == NULL ) return;
1272
1273         clist = GTK_CTREE(addrbook.clist);
1274         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1275         if( obj == NULL ) canEdit = FALSE;
1276
1277         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
1278
1279         if( pobj->type == ADDR_DATASOURCE ) {
1280                 ads = ADAPTER_DSOURCE(pobj);
1281                 ds = ads->dataSource;
1282                 iface = ds->interface;
1283                 if( ! iface->readOnly ) {
1284                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1285                         menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
1286                         menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1287                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1288                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1289                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1290                         if( obj ) canEdit = TRUE;
1291                 }
1292         }
1293         else if( pobj->type != ADDR_INTERFACE ) {
1294                 ds = addressbook_find_datasource( addrbook.treeSelected );
1295                 iface = ds->interface;
1296                 if( ! iface->readOnly ) {
1297                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
1298                                 menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1299                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1300                                 if( obj ) canEdit = TRUE;
1301                         }
1302                         if( pobj->type == ADDR_ITEM_FOLDER ) {
1303                                 menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
1304                                 menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1305                                 if( obj ) canEdit = TRUE;
1306                         }
1307                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1308                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1309                 }
1310         }
1311         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
1312
1313         canDelete = canEdit;
1314         canDelete = canEdit;
1315
1316         /* Disable edit if more than one row selected */
1317         if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) canEdit = FALSE;
1318
1319         menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
1320         menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
1321
1322         menu_set_sensitive( addrbook.list_factory, "/Cut",           canCut );
1323         menu_set_sensitive( addrbook.list_factory, "/Copy",          canCopy );
1324         menu_set_sensitive( addrbook.list_factory, "/Paste",         canPaste );
1325         menu_set_sensitive( addrbook.list_factory, "/Paste Address", canPaste );
1326
1327         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",           canCut );
1328         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",          canCopy );
1329         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",         canPaste );
1330         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );
1331
1332         menu_set_sensitive( addrbook.tree_factory, "/Cut",           canCut );
1333         menu_set_sensitive( addrbook.tree_factory, "/Copy",          canCopy );
1334         menu_set_sensitive( addrbook.tree_factory, "/Paste",         canPaste );
1335
1336         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1337         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canDelete );
1338
1339         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1340 }
1341
1342 static void addressbook_list_selected(GtkCList *clist, gint row, gint column,
1343                                       GdkEvent *event, gpointer data)
1344 {
1345         if (event && event->type == GDK_2BUTTON_PRESS) {
1346                 /* Handle double click */
1347                 if (prefs_common.add_address_by_click &&
1348                     addrbook.target_compose)
1349                         addressbook_to_clicked(NULL, NULL);
1350                 else
1351                         addressbook_edit_address_cb(NULL, 0, NULL);
1352         }
1353 }
1354
1355 /*
1356  * Add list of items into tree node below specified tree node.
1357  * Enter: treeNode  Tree node.
1358  *        ds        Data source.
1359  *        listItems List of items.
1360  */
1361 static void addressbook_treenode_add_list(
1362         GtkCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
1363 {
1364         GList *node;
1365
1366         node = listItems;
1367         while( node ) {
1368                 AddrItemObject *aio;
1369                 GtkCTreeNode *nn;
1370
1371                 aio = node->data;
1372                 if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_GROUP ) {
1373                         ItemGroup *group;
1374
1375                         group = ( ItemGroup * ) aio;
1376                         nn = addressbook_node_add_group( treeNode, ds, group );
1377                 }
1378                 else if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_FOLDER ) {
1379                         ItemFolder *folder;
1380
1381                         folder = ( ItemFolder * ) aio;
1382                         nn = addressbook_node_add_folder(
1383                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
1384                 }
1385                 node = g_list_next( node );
1386         }
1387 }
1388
1389 /*
1390 * Cut from address list widget.
1391 */
1392 static void addressbook_clip_cut_cb( void ) {
1393         _clipBoard_->cutFlag = TRUE;
1394         addrclip_clear( _clipBoard_ );
1395         addrclip_add( _clipBoard_, _addressSelect_ );
1396         /* addrclip_list_show( _clipBoard_, stdout ); */
1397 }
1398
1399 /*
1400 * Copy from address list widget.
1401 */
1402 static void addressbook_clip_copy_cb( void ) {
1403         _clipBoard_->cutFlag = FALSE;
1404         addrclip_clear( _clipBoard_ );
1405         addrclip_add( _clipBoard_, _addressSelect_ );
1406         /* addrclip_list_show( _clipBoard_, stdout ); */
1407 }
1408
1409 /*
1410 * Paste clipboard into address list widget.
1411 */
1412 static void addressbook_clip_paste_cb( void ) {
1413         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
1414         AddressObject *pobj = NULL;
1415         AddressDataSource *ds = NULL;
1416         AddressBookFile *abf = NULL;
1417         ItemFolder *folder = NULL;
1418         GList *folderGroup = NULL;
1419
1420         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
1421         if( ds == NULL ) return;
1422         if( addrindex_ds_get_readonly( ds ) ) {
1423                 addressbook_ds_status_message(
1424                         ds, _( "Cannot paste. Target address book is readonly." ) );
1425                 return;
1426         }
1427
1428         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1429         if( pobj ) {
1430                 if( pobj->type == ADDR_ITEM_FOLDER ) {
1431                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
1432                 }
1433                 else if( pobj->type == ADDR_ITEM_GROUP ) {
1434                         addressbook_ds_status_message(
1435                                 ds, _( "Cannot paste into an address group." ) );
1436                         return;
1437                 }
1438         }
1439
1440         /* Get an address book */
1441         abf = addressbook_get_book_file();
1442         if( abf == NULL ) return;
1443
1444         if( _clipBoard_->cutFlag ) {
1445                 /* Paste/Cut */
1446                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
1447
1448                 /* Remove all groups and folders in clipboard from tree node */
1449                 addressbook_treenode_remove_item();
1450
1451                 /* Remove all "cut" items */
1452                 addrclip_delete_item( _clipBoard_ );
1453
1454                 /* Clear clipboard - cut items??? */
1455                 addrclip_clear( _clipBoard_ );
1456         }
1457         else {
1458                 /* Paste/Copy */
1459                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
1460         }
1461
1462         /* addrclip_list_show( _clipBoard_, stdout ); */
1463         if( folderGroup ) {
1464                 /* Update tree by inserting node for each folder or group */
1465                 addressbook_treenode_add_list(
1466                         addrbook.treeSelected, ds, folderGroup );
1467                 gtk_ctree_expand( ctree, addrbook.treeSelected );
1468                 g_list_free( folderGroup );
1469                 folderGroup = NULL;
1470         }
1471
1472         /* Display items pasted */
1473         gtk_ctree_select( ctree, addrbook.opened );
1474
1475 }
1476
1477 /*
1478 * Paste clipboard email addresses only into address list widget.
1479 */
1480 static void addressbook_clip_paste_address_cb( void ) {
1481         GtkCTree *clist = GTK_CTREE(addrbook.clist);
1482         GtkCTree *ctree;
1483         AddressObject *pobj = NULL;
1484         AddressDataSource *ds = NULL;
1485         AddressBookFile *abf = NULL;
1486         ItemFolder *folder = NULL;
1487         AddrItemObject *aio;
1488         gint cnt;
1489
1490         if( addrbook.listSelected == NULL ) return;
1491
1492         ctree = GTK_CTREE( addrbook.ctree );
1493         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
1494         if( ds == NULL ) return;
1495         if( addrindex_ds_get_readonly( ds ) ) {
1496                 addressbook_ds_status_message(
1497                         ds, _( "Cannot paste. Target address book is readonly." ) );
1498                 return;
1499         }
1500
1501         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1502         if( pobj ) {
1503                 if( pobj->type == ADDR_ITEM_FOLDER ) {
1504                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
1505                 }
1506         }
1507
1508         abf = addressbook_get_book_file();
1509         if( abf == NULL ) return;
1510
1511         cnt = 0;
1512         aio = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1513         if( aio->type == ADDR_ITEM_PERSON ) {
1514                 ItemPerson *person;
1515
1516                 person = ( ItemPerson * ) aio;
1517                 if( _clipBoard_->cutFlag ) {
1518                         /* Paste/Cut */
1519                         cnt = addrclip_paste_person_cut( _clipBoard_, abf, person );
1520
1521                         /* Remove all "cut" items */
1522                         addrclip_delete_address( _clipBoard_ );
1523
1524                         /* Clear clipboard */
1525                         addrclip_clear( _clipBoard_ );
1526                 }
1527                 else {
1528                         /* Paste/Copy */
1529                         cnt = addrclip_paste_person_copy( _clipBoard_, abf, person );
1530                 }
1531                 if( cnt > 0 ) {
1532                         addritem_person_set_opened( person, TRUE );
1533                 }
1534         }
1535
1536         /* Display items pasted */
1537         if( cnt > 0 ) {
1538                 gtk_ctree_select( ctree, addrbook.opened );
1539         }
1540 }
1541
1542 /*
1543 * Add current treenode object to clipboard. Note that widget only allows
1544 * one entry from the tree list to be selected.
1545 */
1546 static void addressbook_treenode_to_clipboard( void ) {
1547         AddressObject *obj = NULL;
1548         AddressDataSource *ds = NULL;
1549         AddrSelectItem *item;
1550         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
1551         GtkCTreeNode *node;
1552
1553         node = addrbook.treeSelected;
1554         if( node == NULL ) return;
1555         obj = gtk_ctree_node_get_row_data( ctree, node );
1556         if( obj == NULL ) return;
1557
1558         ds = addressbook_find_datasource( node );
1559         if( ds == NULL ) return;
1560
1561         item = NULL;
1562         if( obj->type == ADDR_ITEM_FOLDER ) {
1563                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
1564                 ItemFolder *folder = adapter->itemFolder;
1565
1566                 item = addrselect_create_node( obj );
1567                 item->uid = g_strdup( ADDRITEM_ID(folder) );
1568         }
1569         else if( obj->type == ADDR_ITEM_GROUP ) {
1570                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
1571                 ItemGroup *group = adapter->itemGroup;
1572
1573                 item = addrselect_create_node( obj );
1574                 item->uid = g_strdup( ADDRITEM_ID(group) );
1575         }
1576         else if( obj->type == ADDR_DATASOURCE ) {
1577                 /* Data source */
1578                 item = addrselect_create_node( obj );
1579                 item->uid = NULL;
1580         }
1581
1582         if( item ) {
1583                 /* Clear existing list and add item into list */
1584                 gchar *cacheID;
1585
1586                 addressbook_list_select_clear();
1587                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
1588                 addrselect_list_add( _addressSelect_, item, cacheID );
1589                 g_free( cacheID );
1590         }
1591 }
1592
1593 /*
1594 * Cut from tree widget.
1595 */
1596 static void addressbook_treenode_cut_cb( void ) {
1597         _clipBoard_->cutFlag = TRUE;
1598         addressbook_treenode_to_clipboard();
1599         addrclip_clear( _clipBoard_ );
1600         addrclip_add( _clipBoard_, _addressSelect_ );
1601         /* addrclip_list_show( _clipBoard_, stdout ); */
1602 }
1603
1604 /*
1605 * Copy from tree widget.
1606 */
1607 static void addressbook_treenode_copy_cb( void ) {
1608         _clipBoard_->cutFlag = FALSE;
1609         addressbook_treenode_to_clipboard();
1610         addrclip_clear( _clipBoard_ );
1611         addrclip_add( _clipBoard_, _addressSelect_ );
1612         /* addrclip_list_show( _clipBoard_, stdout ); */
1613 }
1614
1615 /*
1616 * Paste clipboard into address tree widget.
1617 */
1618 static void addressbook_treenode_paste_cb( void ) {
1619         addressbook_clip_paste_cb();
1620 }
1621
1622 static void addressbook_list_select_clear( void ) {
1623         addrselect_list_clear( _addressSelect_ );
1624 }
1625
1626 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
1627         gchar *cacheID;
1628
1629         if( ds == NULL ) return;
1630         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
1631         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
1632         g_free( cacheID );
1633 }
1634
1635 static void addressbook_list_select_remove( AddrItemObject *aio ) {
1636         addrselect_list_remove( _addressSelect_, aio );
1637 }
1638
1639 static void addressbook_list_row_selected( GtkCTree *clist,
1640                                            GtkCTreeNode *node,
1641                                            gint column,
1642                                            gpointer data )
1643 {
1644         GtkEntry *entry = GTK_ENTRY(addrbook.entry);
1645         AddrItemObject *aio = NULL;
1646         AddressObject *pobj = NULL;
1647         AdapterDSource *ads = NULL;
1648         AddressDataSource *ds = NULL;
1649
1650         gtk_entry_set_text( entry, "" );
1651         addrbook.listSelected = node;
1652
1653         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1654         if( pobj == NULL ) return;
1655
1656         if( pobj->type == ADDR_DATASOURCE ) {
1657                 ads = ADAPTER_DSOURCE(pobj);
1658                 ds = ads->dataSource;
1659         }
1660         else if( pobj->type != ADDR_INTERFACE ) {
1661                 ds = addressbook_find_datasource( addrbook.treeSelected );
1662         }
1663
1664         aio = gtk_ctree_node_get_row_data( clist, node );
1665         if( aio ) {
1666                 /* printf( "list select: %d : '%s'\n", aio->type, aio->name ); */
1667                 addressbook_list_select_add( aio, ds );
1668         }
1669
1670         addressbook_list_menu_setup();
1671 }
1672
1673 static void addressbook_list_row_unselected( GtkCTree *ctree,
1674                                              GtkCTreeNode *node,
1675                                              gint column,
1676                                              gpointer data )
1677 {
1678         AddrItemObject *aio;
1679
1680         aio = gtk_ctree_node_get_row_data( ctree, node );
1681         if( aio != NULL ) {
1682                 /* printf( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
1683                 addressbook_list_select_remove( aio );
1684         }
1685 }
1686
1687 static void addressbook_entry_gotfocus( GtkWidget *widget ) {
1688         gtk_editable_select_region( GTK_EDITABLE(addrbook.entry), 0, -1 );
1689 }
1690
1691 static void addressbook_list_button_pressed(GtkWidget *widget,
1692                                             GdkEventButton *event,
1693                                             gpointer data)
1694 {
1695         if( ! event ) return;
1696
1697         addressbook_list_menu_setup();
1698
1699         if( event->button == 3 ) {
1700                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
1701                        event->button, event->time );
1702         }
1703 }
1704
1705 static void addressbook_list_button_released(GtkWidget *widget,
1706                                              GdkEventButton *event,
1707                                              gpointer data)
1708 {
1709 }
1710
1711 static void addressbook_tree_button_pressed(GtkWidget *ctree,
1712                                             GdkEventButton *event,
1713                                             gpointer data)
1714 {
1715         GtkCList *clist = GTK_CLIST(ctree);
1716         gint row, column;
1717         AddressObject *obj = NULL;
1718         AdapterDSource *ads = NULL;
1719         AddressInterface *iface = NULL;
1720         AddressDataSource *ds = NULL;
1721         gboolean canEdit = FALSE;
1722         gboolean canCut = FALSE;
1723         gboolean canCopy = FALSE;
1724         gboolean canPaste = FALSE;
1725         gboolean canTreeCut = FALSE;
1726         gboolean canTreeCopy = FALSE;
1727         gboolean canTreePaste = FALSE;
1728
1729         if( ! event ) return;
1730         addressbook_menubar_set_sensitive( FALSE );
1731
1732         if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
1733                 gtk_clist_select_row( clist, row, column );
1734                 gtkut_clist_set_focus_row(clist, row);
1735                 obj = gtk_clist_get_row_data( clist, row );
1736         }
1737
1738         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
1739
1740         if( obj == NULL ) return;
1741
1742         if( ! addrclip_is_empty( _clipBoard_ ) ) {
1743                 canTreePaste = TRUE;
1744         }
1745
1746         if (obj->type == ADDR_DATASOURCE) {
1747                 ads = ADAPTER_DSOURCE(obj);
1748                 ds = ads->dataSource;
1749                 iface = ds->interface;
1750                 canEdit = TRUE;
1751                 if( iface->readOnly ) {
1752                         canTreePaste = FALSE;
1753                 }
1754                 else {
1755                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
1756                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
1757                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
1758                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1759                 }
1760                 canTreeCopy = TRUE;
1761         }
1762         else if (obj->type == ADDR_ITEM_FOLDER) {
1763                 ds = addressbook_find_datasource( addrbook.treeSelected );
1764                 iface = ds->interface;
1765                 if( iface->readOnly ) {
1766                         canTreePaste = FALSE;
1767                 }
1768                 else {
1769                         canEdit = TRUE;
1770                         canTreeCut = TRUE;
1771                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
1772                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
1773                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
1774                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1775                 }
1776                 canTreeCopy = TRUE;
1777         }
1778         else if (obj->type == ADDR_ITEM_GROUP) {
1779                 ds = addressbook_find_datasource( addrbook.treeSelected );
1780                 iface = ds->interface;
1781                 if( ! iface->readOnly ) {
1782                         canEdit = TRUE;
1783                         menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
1784                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1785                 }
1786         }
1787         else if (obj->type == ADDR_INTERFACE) {
1788                 canTreePaste = FALSE;
1789         }
1790
1791         if( canEdit ) {
1792                 if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1793         }
1794         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
1795         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1796
1797         /* Enable edit */
1798         menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
1799         menu_set_sensitive( addrbook.tree_factory, "/Delete", canEdit );
1800         menu_set_sensitive( addrbook.tree_factory, "/Cut",    canTreeCut );
1801         menu_set_sensitive( addrbook.tree_factory, "/Copy",   canTreeCopy );
1802         menu_set_sensitive( addrbook.tree_factory, "/Paste",  canTreePaste );
1803
1804         menu_set_sensitive( addrbook.menu_factory, "/File/Edit",          canEdit );
1805         menu_set_sensitive( addrbook.menu_factory, "/File/Delete",        canEdit );
1806         menu_set_sensitive( addrbook.menu_factory, "/Edit/Cut",           canCut );
1807         menu_set_sensitive( addrbook.menu_factory, "/Edit/Copy",          canCopy );
1808         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste",         canPaste );
1809         menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );
1810
1811         if( event->button == 3 ) {
1812                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
1813                                event->button, event->time);
1814         }
1815
1816 }
1817
1818 static void addressbook_tree_button_released(GtkWidget *ctree,
1819                                              GdkEventButton *event,
1820                                              gpointer data)
1821 {
1822         gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
1823         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree), addrbook.opened);
1824 }
1825
1826 static void addressbook_popup_close(GtkMenuShell *menu_shell, gpointer data)
1827 {
1828         if (!addrbook.opened) return;
1829
1830         gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
1831         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree),
1832                                   addrbook.opened);
1833 }
1834
1835 static void addressbook_new_folder_cb(gpointer data, guint action,
1836                                       GtkWidget *widget)
1837 {
1838         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1839         AddressObject *obj = NULL;
1840         AddressDataSource *ds = NULL;
1841         AddressBookFile *abf = NULL;
1842         ItemFolder *parentFolder = NULL;
1843         ItemFolder *folder = NULL;
1844
1845         if( ! addrbook.treeSelected ) return;
1846         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
1847         if( obj == NULL ) return;
1848         ds = addressbook_find_datasource( addrbook.treeSelected );
1849         if( ds == NULL ) return;
1850
1851         if( obj->type == ADDR_DATASOURCE ) {
1852                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
1853         }
1854         else if( obj->type == ADDR_ITEM_FOLDER ) {
1855                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
1856         }
1857         else {
1858                 return;
1859         }
1860
1861         abf = ds->rawDataSource;
1862         if( abf == NULL ) return;
1863         folder = addressbook_edit_folder( abf, parentFolder, NULL );
1864         if( folder ) {
1865                 GtkCTreeNode *nn;
1866                 nn = addressbook_node_add_folder( addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
1867                 gtk_ctree_expand( ctree, addrbook.treeSelected );
1868                 if( addrbook.treeSelected == addrbook.opened ) addressbook_set_clist(obj);
1869         }
1870
1871 }
1872
1873 static void addressbook_new_group_cb(gpointer data, guint action,
1874                                      GtkWidget *widget)
1875 {
1876         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1877         AddressObject *obj = NULL;
1878         AddressDataSource *ds = NULL;
1879         AddressBookFile *abf = NULL;
1880         ItemFolder *parentFolder = NULL;
1881         ItemGroup *group = NULL;
1882
1883         if( ! addrbook.treeSelected ) return;
1884         obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
1885         if( obj == NULL ) return;
1886         ds = addressbook_find_datasource( addrbook.treeSelected );
1887         if( ds == NULL ) return;
1888
1889         if( obj->type == ADDR_DATASOURCE ) {
1890                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
1891         }
1892         else if( obj->type == ADDR_ITEM_FOLDER ) {
1893                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
1894         }
1895         else {
1896                 return;
1897         }
1898
1899         abf = ds->rawDataSource;
1900         if( abf == NULL ) return;
1901         group = addressbook_edit_group( abf, parentFolder, NULL );
1902         if( group ) {
1903                 GtkCTreeNode *nn;
1904                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
1905                 gtk_ctree_expand( ctree, addrbook.treeSelected );
1906                 if( addrbook.treeSelected == addrbook.opened ) addressbook_set_clist(obj);
1907         }
1908
1909 }
1910
1911 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
1912 {
1913         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1914         gchar *text[1];
1915         guint8 spacing;
1916         GdkPixmap *pix_cl, *pix_op;
1917         GdkBitmap *mask_cl, *mask_op;
1918         gboolean is_leaf, expanded;
1919
1920         gtk_ctree_get_node_info(ctree, node, text, &spacing,
1921                                 &pix_cl, &mask_cl, &pix_op, &mask_op,
1922                                 &is_leaf, &expanded);
1923         gtk_ctree_set_node_info(ctree, node, name, spacing,
1924                                 pix_cl, mask_cl, pix_op, mask_op,
1925                                 is_leaf, expanded);
1926 }
1927
1928 /*
1929 * Edit data source.
1930 * Enter: obj   Address object to edit.
1931 *        node  Node in tree.
1932 * Return: New name of data source.
1933 */
1934 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
1935         gchar *newName = NULL;
1936         AddressDataSource *ds = NULL;
1937         AddressInterface *iface = NULL;
1938         AdapterDSource *ads = NULL;
1939
1940         ds = addressbook_find_datasource( node );
1941         if( ds == NULL ) return NULL;
1942         iface = ds->interface;
1943         if( ! iface->haveLibrary ) return NULL;
1944
1945         /* Read data from data source */
1946         if( addrindex_ds_get_modify_flag( ds ) ) {
1947                 addrindex_ds_read_data( ds );
1948         }
1949
1950         if( ! addrindex_ds_get_read_flag( ds ) ) {
1951                 addrindex_ds_read_data( ds );
1952         }
1953
1954         /* Handle edit */
1955         ads = ADAPTER_DSOURCE(obj);
1956         if( ads->subType == ADDR_BOOK ) {
1957                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
1958         }
1959         else if( ads->subType == ADDR_VCARD ) {
1960                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
1961         }
1962 #ifdef USE_JPILOT
1963         else if( ads->subType == ADDR_JPILOT ) {
1964                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
1965         }
1966 #endif
1967 #ifdef USE_LDAP
1968         else if( ads->subType == ADDR_LDAP ) {
1969                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
1970         }
1971 #endif
1972         else {
1973                 return NULL;
1974         }
1975         newName = obj->name;
1976         return newName;
1977 }
1978
1979 /*
1980 * Edit an object that is in the address tree area.
1981 */
1982 static void addressbook_treenode_edit_cb(gpointer data, guint action,
1983                                        GtkWidget *widget)
1984 {
1985         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1986         AddressObject *obj;
1987         AddressDataSource *ds = NULL;
1988         AddressBookFile *abf = NULL;
1989         GtkCTreeNode *node = NULL, *parentNode = NULL;
1990         gchar *name = NULL;
1991
1992         if( ! addrbook.treeSelected ) return;
1993         node = addrbook.treeSelected;
1994         if( GTK_CTREE_ROW(node)->level == 1 ) return;
1995         obj = gtk_ctree_node_get_row_data( ctree, node );
1996         if( obj == NULL ) return;
1997         parentNode = GTK_CTREE_ROW(node)->parent;
1998
1999         ds = addressbook_find_datasource( node );
2000         if( ds == NULL ) return;
2001
2002         if( obj->type == ADDR_DATASOURCE ) {
2003                 name = addressbook_edit_datasource( obj, node );
2004                 if( name == NULL ) return;
2005         }
2006         else {
2007                 abf = ds->rawDataSource;
2008                 if( abf == NULL ) return;
2009                 if( obj->type == ADDR_ITEM_FOLDER ) {
2010                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2011                         ItemFolder *item = adapter->itemFolder;
2012                         ItemFolder *parentFolder = NULL;
2013                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2014                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2015                         name = ADDRITEM_NAME(item);
2016                 }
2017                 else if( obj->type == ADDR_ITEM_GROUP ) {
2018                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2019                         ItemGroup *item = adapter->itemGroup;
2020                         ItemFolder *parentFolder = NULL;
2021                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2022                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2023                         name = ADDRITEM_NAME(item);
2024                 }
2025         }
2026         if( name && parentNode ) {
2027                 /* Update node in tree view */
2028                 addressbook_change_node_name( node, name );
2029                 gtk_sctree_sort_node(ctree, parentNode);
2030                 gtk_ctree_expand( ctree, node );
2031                 gtk_ctree_select( ctree, node );
2032         }
2033 }
2034
2035 /*
2036 * Delete an item from the tree widget.
2037 */
2038 static void addressbook_treenode_delete_cb(gpointer data, guint action,
2039                                          GtkWidget *widget)
2040 {
2041         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2042         GtkCTreeNode *node = NULL;
2043         AddressObject *obj;
2044         gchar *message;
2045         AlertValue aval;
2046         AddressBookFile *abf = NULL;
2047         AdapterDSource *ads = NULL;
2048         AddressInterface *iface = NULL;
2049         AddressDataSource *ds = NULL;
2050         gboolean remFlag = FALSE;
2051
2052         if( ! addrbook.treeSelected ) return;
2053         node = addrbook.treeSelected;
2054         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2055
2056         obj = gtk_ctree_node_get_row_data( ctree, node );
2057         g_return_if_fail(obj != NULL);
2058
2059         if( obj->type == ADDR_DATASOURCE ) {
2060                 ads = ADAPTER_DSOURCE(obj);
2061                 if( ads == NULL ) return;
2062                 ds = ads->dataSource;
2063                 if( ds == NULL ) return;
2064         }
2065         else {
2066                 /* Must be folder or something else */
2067                 ds = addressbook_find_datasource( node );
2068                 if( ds == NULL ) return;
2069
2070                 /* Only allow deletion from non-readOnly data sources */
2071                 iface = ds->interface;
2072                 if( iface->readOnly ) return;
2073         }
2074
2075         /* Confirm deletion */
2076         if( obj->type == ADDR_ITEM_FOLDER ) {
2077                 message = g_strdup_printf( _(
2078                         "Do you want to delete the folder AND all addresses in `%s' ? \n" \
2079                         "If deleting the folder only, addresses will be moved into parent folder." ),
2080                         obj->name );
2081                 aval = alertpanel( _("Delete"), message,
2082                         _("Folder only"), _("Folder and Addresses"), _("Cancel") );
2083                 g_free(message);
2084                 if( aval == G_ALERTOTHER ) return;
2085         }
2086         else {
2087                 message = g_strdup_printf(_("Really delete `%s' ?"), obj->name);
2088                 aval = alertpanel(_("Delete"), message, _("Yes"), _("No"), NULL);
2089                 g_free(message);
2090                 if (aval != G_ALERTDEFAULT) return;
2091         }
2092
2093         /* Proceed with deletion */
2094         if( obj->type == ADDR_DATASOURCE ) {
2095                 /* Remove data source. */
2096                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
2097                         addrindex_free_datasource( ds );
2098                         remFlag = TRUE;
2099                 }
2100         }
2101         else {
2102                 abf = addressbook_get_book_file();
2103                 if( abf == NULL ) return;
2104         }
2105
2106         if( obj->type == ADDR_ITEM_FOLDER ) {
2107                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2108                 ItemFolder *item = adapter->itemFolder;
2109                 if( aval == G_ALERTDEFAULT ) {
2110                         /* Remove folder only */
2111                         item = addrbook_remove_folder( abf, item );
2112                         if( item ) {
2113                                 addritem_free_item_folder( item );
2114                                 addressbook_move_nodes_up( ctree, node );
2115                                 remFlag = TRUE;
2116                         }
2117                 }
2118                 else if( aval == G_ALERTALTERNATE ) {
2119                         /* Remove folder and addresses */
2120                         item = addrbook_remove_folder_delete( abf, item );
2121                         if( item ) {
2122                                 addritem_free_item_folder( item );
2123                                 remFlag = TRUE;
2124                         }
2125                 }
2126         }
2127         else if( obj->type == ADDR_ITEM_GROUP ) {
2128                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2129                 ItemGroup *item = adapter->itemGroup;
2130
2131                 item = addrbook_remove_group( abf, item );
2132                 if( item ) {
2133                         addritem_free_item_group( item );
2134                         remFlag = TRUE;
2135                 }
2136         }
2137
2138         if( remFlag ) {
2139                 /* Remove node. */
2140                 gtk_ctree_remove_node(ctree, node );
2141         }
2142 }
2143
2144 static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
2145         AddressObject *pobj = NULL;
2146         AddressDataSource *ds = NULL;
2147         AddressBookFile *abf = NULL;
2148
2149         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
2150         if( pobj == NULL ) return;
2151         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2152         if( ds == NULL ) return;
2153
2154         abf = ds->rawDataSource;
2155         if( abf == NULL ) return;
2156
2157         if( pobj->type == ADDR_DATASOURCE ) {
2158                 if( ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ) {
2159                         /* New address */
2160                         ItemPerson *person = addressbook_edit_person( abf, NULL, NULL, FALSE );
2161                         if( person ) {
2162                                 if( addrbook.treeSelected == addrbook.opened ) {
2163                                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
2164                                 }
2165                         }
2166                 }
2167         }
2168         else if( pobj->type == ADDR_ITEM_FOLDER ) {
2169                 /* New address */
2170                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
2171                 ItemPerson *person = addressbook_edit_person( abf, folder, NULL, FALSE );
2172                 if( person ) {
2173                         if (addrbook.treeSelected == addrbook.opened) {
2174                                 gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
2175                         }
2176                 }
2177         }
2178         else if( pobj->type == ADDR_ITEM_GROUP ) {
2179                 /* New address in group */
2180                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
2181                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
2182                 if (addrbook.treeSelected == addrbook.opened) {
2183                         /* Change node name in tree. */
2184                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
2185                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
2186                 }
2187         }
2188 }
2189
2190 /*
2191 * Search for specified child group node in address index tree.
2192 * Enter: parent Parent node.
2193 *        group  Group to find.
2194 */
2195 static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
2196         GtkCTreeNode *node = NULL;
2197         GtkCTreeRow *currRow;
2198
2199         currRow = GTK_CTREE_ROW( parent );
2200         if( currRow ) {
2201                 node = currRow->children;
2202                 while( node ) {
2203                         AddressObject *obj;
2204
2205                         obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
2206                         if( obj->type == ADDR_ITEM_GROUP ) {
2207                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
2208                                 if( g == group ) return node;
2209                         }
2210                         currRow = GTK_CTREE_ROW(node);
2211                         node = currRow->sibling;
2212                 }
2213         }
2214         return NULL;
2215 }
2216
2217 static AddressBookFile *addressbook_get_book_file() {
2218         AddressBookFile *abf = NULL;
2219         AddressDataSource *ds = NULL;
2220
2221         ds = addressbook_find_datasource( addrbook.treeSelected );
2222         if( ds == NULL ) return NULL;
2223         if( ds->type == ADDR_IF_BOOK ) abf = ds->rawDataSource;
2224         return abf;
2225 }
2226
2227 static void addressbook_tree_remove_children( GtkCTree *ctree, GtkCTreeNode *parent ) {
2228         GtkCTreeNode *node;
2229         GtkCTreeRow *row;
2230
2231         /* Remove existing folders and groups */
2232         row = GTK_CTREE_ROW( parent );
2233         if( row ) {
2234                 while( (node = row->children) ) {
2235                         gtk_ctree_remove_node( ctree, node );
2236                 }
2237         }
2238 }
2239
2240 static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
2241         GtkCTreeNode *parent, *child;
2242         GtkCTreeRow *currRow;
2243         currRow = GTK_CTREE_ROW( node );
2244         if( currRow ) {
2245                 parent = currRow->parent;
2246                 while( (child = currRow->children) ) {
2247                         gtk_ctree_move( ctree, child, parent, node );
2248                 }
2249                 gtk_sctree_sort_node( ctree, parent );
2250         }
2251 }
2252
2253 static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget *widget ) {
2254         GtkCTree *clist = GTK_CTREE(addrbook.clist);
2255         GtkCTree *ctree;
2256         AddressObject *obj = NULL, *pobj = NULL;
2257         AddressDataSource *ds = NULL;
2258         GtkCTreeNode *node = NULL, *parentNode = NULL;
2259         gchar *name = NULL;
2260         AddressBookFile *abf = NULL;
2261
2262         if( addrbook.listSelected == NULL ) return;
2263         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
2264         g_return_if_fail(obj != NULL);
2265
2266         ctree = GTK_CTREE( addrbook.ctree );
2267         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2268         node = gtk_ctree_find_by_row_data( ctree, addrbook.treeSelected, obj );
2269
2270         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2271         if( ds == NULL ) return;
2272
2273         abf = addressbook_get_book_file();
2274         if( abf == NULL ) return;
2275         if( obj->type == ADDR_ITEM_EMAIL ) {
2276                 ItemEMail *email = ( ItemEMail * ) obj;
2277                 if( email == NULL ) return;
2278                 if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
2279                         /* Edit parent group */
2280                         AdapterGroup *adapter = ADAPTER_GROUP(pobj);
2281                         ItemGroup *itemGrp = adapter->itemGroup;
2282                         if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
2283                         name = ADDRITEM_NAME(itemGrp);
2284                         node = addrbook.treeSelected;
2285                         parentNode = GTK_CTREE_ROW(node)->parent;
2286                 }
2287                 else {
2288                         /* Edit person - email page */
2289                         ItemPerson *person;
2290                         person = ( ItemPerson * ) ADDRITEM_PARENT(email);
2291                         if( addressbook_edit_person( abf, NULL, person, TRUE ) == NULL ) return;
2292                         gtk_ctree_select( ctree, addrbook.opened );
2293                         invalidate_address_completion();
2294                         return;
2295                 }
2296         }
2297         else if( obj->type == ADDR_ITEM_PERSON ) {
2298                 /* Edit person - basic page */
2299                 ItemPerson *person = ( ItemPerson * ) obj;
2300                 if( addressbook_edit_person( abf, NULL, person, FALSE ) == NULL ) return;
2301                 gtk_ctree_select( ctree, addrbook.opened);
2302                 invalidate_address_completion();
2303                 return;
2304         }
2305         else if( obj->type == ADDR_ITEM_GROUP ) {
2306                 ItemGroup *itemGrp = ( ItemGroup * ) obj;
2307                 if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
2308                 parentNode = addrbook.treeSelected;
2309                 node = addressbook_find_group_node( parentNode, itemGrp );
2310                 name = ADDRITEM_NAME(itemGrp);
2311         }
2312         else {
2313                 return;
2314         }
2315
2316         /* Update tree node with node name */
2317         if( node == NULL ) return;
2318         addressbook_change_node_name( node, name );
2319         gtk_sctree_sort_node( ctree, parentNode );
2320         gtk_ctree_select( ctree, addrbook.opened );
2321 }
2322
2323 static void addressbook_delete_address_cb(gpointer data, guint action,
2324                                           GtkWidget *widget)
2325 {
2326         addressbook_del_clicked(NULL, NULL);
2327 }
2328
2329 static void close_cb(gpointer data, guint action, GtkWidget *widget)
2330 {
2331         addressbook_close();
2332 }
2333
2334 static void addressbook_file_save_cb( gpointer data, guint action, GtkWidget *widget ) {
2335         addressbook_export_to_file();
2336 }
2337
2338 static void addressbook_person_expand_node( GtkCTree *ctree, GList *node, gpointer *data ) {
2339         if( node ) {
2340                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
2341                 if( person ) addritem_person_set_opened( person, TRUE );
2342         }
2343 }
2344
2345 static void addressbook_person_collapse_node( GtkCTree *ctree, GList *node, gpointer *data ) {
2346         if( node ) {
2347                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
2348                 if( person ) addritem_person_set_opened( person, FALSE );
2349         }
2350 }
2351
2352 static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
2353         gchar *str = NULL;
2354         gchar *eMailAlias = ADDRITEM_NAME(email);
2355         if( eMailAlias && *eMailAlias != '\0' ) {
2356                 if( person ) {
2357                         str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
2358                 }
2359                 else {
2360                         str = g_strdup( eMailAlias );
2361                 }
2362         }
2363         return str;
2364 }
2365
2366 static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
2367         GList *items = itemGroup->listEMail;
2368         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2369         for( ; items != NULL; items = g_list_next( items ) ) {
2370                 GtkCTreeNode *nodeEMail = NULL;
2371                 gchar *text[N_COLS];
2372                 ItemEMail *email = items->data;
2373                 ItemPerson *person;
2374                 gchar *str = NULL;
2375
2376                 if( ! email ) continue;
2377
2378                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
2379                 str = addressbook_format_item_clist( person, email );
2380                 if( str ) {
2381                         text[COL_NAME] = str;
2382                 }
2383                 else {
2384                         text[COL_NAME] = ADDRITEM_NAME(person);
2385                 }
2386                 text[COL_ADDRESS] = email->address;
2387                 text[COL_REMARKS] = email->remarks;
2388                 nodeEMail = gtk_ctree_insert_node(
2389                                 clist, NULL, NULL,
2390                                 text, FOLDER_SPACING,
2391                                 atci->iconXpm, atci->maskXpm,
2392                                 atci->iconXpmOpen, atci->maskXpmOpen,
2393                                 FALSE, FALSE );
2394                 gtk_ctree_node_set_row_data( clist, nodeEMail, email );
2395                 g_free( str );
2396                 str = NULL;
2397         }
2398 }
2399
2400 static void addressbook_folder_load_person( GtkCTree *clist, ItemFolder *itemFolder ) {
2401         GList *items;
2402         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
2403         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
2404
2405         if( atci == NULL ) return;
2406         if( atciMail == NULL ) return;
2407
2408         /* Load email addresses */
2409         items = addritem_folder_get_person_list( itemFolder );
2410         for( ; items != NULL; items = g_list_next( items ) ) {
2411                 GtkCTreeNode *nodePerson = NULL;
2412                 GtkCTreeNode *nodeEMail = NULL;
2413                 gchar *text[N_COLS];
2414                 gboolean flgFirst = TRUE, haveAddr = FALSE;
2415                 ItemPerson *person;
2416                 GList *node;
2417
2418                 person = ( ItemPerson * ) items->data;
2419                 if( person == NULL ) continue;
2420
2421                 text[COL_NAME] = NULL;
2422                 node = person->listEMail;
2423                 while( node ) {
2424                         ItemEMail *email = node->data;
2425                         gchar *eMailAddr = NULL;
2426                         node = g_list_next( node );
2427
2428                         text[COL_ADDRESS] = email->address;
2429                         text[COL_REMARKS] = email->remarks;
2430                         eMailAddr = ADDRITEM_NAME(email);
2431                         if( eMailAddr && *eMailAddr == '\0' ) eMailAddr = NULL;
2432                         if( flgFirst ) {
2433                                 /* First email belongs with person */
2434                                 gchar *str = addressbook_format_item_clist( person, email );
2435                                 if( str ) {
2436                                         text[COL_NAME] = str;
2437                                 }
2438                                 else {
2439                                         text[COL_NAME] = ADDRITEM_NAME(person);
2440                                 }
2441                                 nodePerson = gtk_ctree_insert_node(
2442                                                 clist, NULL, NULL,
2443                                                 text, FOLDER_SPACING,
2444                                                 atci->iconXpm, atci->maskXpm,
2445                                                 atci->iconXpmOpen, atci->maskXpmOpen,
2446                                                 FALSE, person->isOpened );
2447                                 g_free( str );
2448                                 str = NULL;
2449                                 gtk_ctree_node_set_row_data(clist, nodePerson, person );
2450                         }
2451                         else {
2452                                 /* Subsequent email is a child node of person */
2453                                 text[COL_NAME] = ADDRITEM_NAME(email);
2454                                 nodeEMail = gtk_ctree_insert_node(
2455                                                 clist, nodePerson, NULL,
2456                                                 text, FOLDER_SPACING,
2457                                                 atciMail->iconXpm, atciMail->maskXpm,
2458                                                 atciMail->iconXpmOpen, atciMail->maskXpmOpen,
2459                                                 FALSE, TRUE );
2460                                 gtk_ctree_node_set_row_data(clist, nodeEMail, email );
2461                         }
2462                         flgFirst = FALSE;
2463                         haveAddr = TRUE;
2464                 }
2465                 if( ! haveAddr ) {
2466                         /* Have name without EMail */
2467                         text[COL_NAME] = ADDRITEM_NAME(person);
2468                         text[COL_ADDRESS] = NULL;
2469                         text[COL_REMARKS] = NULL;
2470                         nodePerson = gtk_ctree_insert_node(
2471                                         clist, NULL, NULL,
2472                                         text, FOLDER_SPACING,
2473                                         atci->iconXpm, atci->maskXpm,
2474                                         atci->iconXpmOpen, atci->maskXpmOpen,
2475                                         FALSE, person->isOpened );
2476                         gtk_ctree_node_set_row_data(clist, nodePerson, person );
2477                 }
2478                 gtk_sctree_sort_node(GTK_CTREE(clist), NULL);
2479         }
2480         /* Free up the list */
2481         mgu_clear_list( items );
2482         g_list_free( items );
2483 }
2484
2485 static void addressbook_folder_load_group( GtkCTree *clist, ItemFolder *itemFolder ) {
2486         GList *items;
2487         AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
2488
2489         /* Load any groups */
2490         if( ! atci ) return;
2491         items = addritem_folder_get_group_list( itemFolder );
2492         for( ; items != NULL; items = g_list_next( items ) ) {
2493                 GtkCTreeNode *nodeGroup = NULL;
2494                 gchar *text[N_COLS];
2495                 ItemGroup *group = items->data;
2496                 if( group == NULL ) continue;
2497                 text[COL_NAME] = ADDRITEM_NAME(group);
2498                 text[COL_ADDRESS] = NULL;
2499                 text[COL_REMARKS] = NULL;
2500                 nodeGroup = gtk_ctree_insert_node(clist, NULL, NULL,
2501                                       text, FOLDER_SPACING,
2502                                       atci->iconXpm, atci->maskXpm,
2503                                       atci->iconXpmOpen, atci->maskXpmOpen,
2504                                       FALSE, FALSE);
2505                 gtk_ctree_node_set_row_data(clist, nodeGroup, group );
2506                 gtk_sctree_sort_node(clist, NULL);
2507         }
2508         /* Free up the list */
2509         mgu_clear_list( items );
2510         g_list_free( items );
2511 }
2512
2513 /*
2514 * Search ctree widget callback function.
2515 * Enter: pA Pointer to node.
2516 *        pB Pointer to data item being sought.
2517 * Return: Zero (0) if group found.
2518 */
2519 static int addressbook_treenode_find_group_cb( gconstpointer pA, gconstpointer pB ) {
2520         AddressObject *aoA;
2521
2522         aoA = ( AddressObject * ) pA;
2523         if( aoA->type == ADDR_ITEM_GROUP ) {
2524                 ItemGroup *group, *grp;
2525
2526                 grp = ADAPTER_GROUP(aoA)->itemGroup;
2527                 group = ( ItemGroup * ) pB;
2528                 if( grp == group ) return 0;    /* Found group */
2529         }
2530         return 1;
2531 }
2532
2533 /*
2534 * Search ctree widget callback function.
2535 * Enter: pA Pointer to node.
2536 *        pB Pointer to data item being sought.
2537 * Return: Zero (0) if folder found.
2538 */
2539 static int addressbook_treenode_find_folder_cb( gconstpointer pA, gconstpointer pB ) {
2540         AddressObject *aoA;
2541
2542         aoA = ( AddressObject * ) pA;
2543         if( aoA->type == ADDR_ITEM_FOLDER ) {
2544                 ItemFolder *folder, *fld;
2545
2546                 fld = ADAPTER_FOLDER(aoA)->itemFolder;
2547                 folder = ( ItemFolder * ) pB;
2548                 if( fld == folder ) return 0;   /* Found folder */
2549         }
2550         return 1;
2551 }
2552
2553 /*
2554 * Remove folder and group nodes from tree widget for items contained ("cut")
2555 * in clipboard.
2556 */
2557 static void addressbook_treenode_remove_item( void ) {
2558         GList *node;
2559         AddrSelectItem *cutItem;
2560         AddressCache *cache;
2561         AddrItemObject *aio;
2562         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2563         GtkCTreeNode *tn;
2564
2565         node = _clipBoard_->objectList;
2566         while( node ) {
2567                 cutItem = node->data;
2568                 node = g_list_next( node );
2569                 cache = addrindex_get_cache(
2570                         _clipBoard_->addressIndex, cutItem->cacheID );
2571                 if( cache == NULL ) continue;
2572                 aio = addrcache_get_object( cache, cutItem->uid );
2573                 if( aio ) {
2574                         tn = NULL;
2575                         if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
2576                                 ItemFolder *folder;
2577
2578                                 folder = ( ItemFolder * ) aio;
2579                                 tn = gtk_ctree_find_by_row_data_custom(
2580                                         ctree, NULL, folder,
2581                                         addressbook_treenode_find_folder_cb );
2582                         }
2583                         else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
2584                                 ItemGroup *group;
2585
2586                                 group = ( ItemGroup * ) aio;
2587                                 tn = gtk_ctree_find_by_row_data_custom(
2588                                         ctree, NULL, group,
2589                                         addressbook_treenode_find_group_cb );
2590                         }
2591
2592                         if( tn ) {
2593                                 /* Free up adapter and remove node. */
2594                                 gtk_ctree_remove_node( ctree, tn );
2595                         }
2596                 }
2597         }
2598 }
2599
2600 /*
2601 * Find parent datasource for specified tree node.
2602 * Enter: node Node to test.
2603 * Return: Data source, or NULL if not found.
2604 */
2605 static AddressDataSource *addressbook_find_datasource( GtkCTreeNode *node ) {
2606         AddressDataSource *ds = NULL;
2607         AddressObject *ao;
2608
2609         g_return_val_if_fail(addrbook.ctree != NULL, NULL);
2610
2611         while( node ) {
2612                 if( GTK_CTREE_ROW(node)->level < 2 ) return NULL;
2613                 ao = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
2614                 if( ao ) {
2615                         /* printf( "ao->type = %d\n", ao->type ); */
2616                         if( ao->type == ADDR_DATASOURCE ) {
2617                                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
2618                                 /* printf( "found it\n" ); */
2619                                 ds = ads->dataSource;
2620                                 break;
2621                         }
2622                 }
2623                 node = GTK_CTREE_ROW(node)->parent;
2624         }
2625         return ds;
2626 }
2627
2628 /*
2629 * Load address list widget with children of specified object.
2630 * Enter: obj    Parent object to be loaded.
2631 */
2632 static void addressbook_set_clist( AddressObject *obj ) {
2633         GtkCTree *ctreelist = GTK_CTREE(addrbook.clist);
2634         GtkCList *clist = GTK_CLIST(addrbook.clist);
2635         AddressDataSource *ds = NULL;
2636         AdapterDSource *ads = NULL;
2637
2638         if( obj == NULL ) {
2639                 gtk_clist_clear(clist);
2640                 return;
2641         }
2642
2643         if( obj->type == ADDR_INTERFACE ) {
2644                 /* printf( "set_clist: loading datasource...\n" ); */
2645                 /* addressbook_node_load_datasource( GTK_CTREE(clist), obj ); */
2646                 return;
2647         }
2648
2649         gtk_clist_freeze(clist);
2650         gtk_clist_clear(clist);
2651
2652         if( obj->type == ADDR_DATASOURCE ) {
2653                 ads = ADAPTER_DSOURCE(obj);
2654                 ds = ADAPTER_DSOURCE(obj)->dataSource;
2655                 if( ds ) {
2656                         /* Load root folder */
2657                         ItemFolder *rootFolder = NULL;
2658                         rootFolder = addrindex_ds_get_root_folder( ds );
2659                         addressbook_folder_load_person( ctreelist, addrindex_ds_get_root_folder( ds ) );
2660                         addressbook_folder_load_group( ctreelist, addrindex_ds_get_root_folder( ds ) );
2661                 }
2662         }
2663         else {
2664                 if( obj->type == ADDR_ITEM_GROUP ) {
2665                         /* Load groups */
2666                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
2667                         addressbook_load_group( ctreelist, itemGroup );
2668                 }
2669                 else if( obj->type == ADDR_ITEM_FOLDER ) {
2670                         /* Load folders */
2671                         ItemFolder *itemFolder = ADAPTER_FOLDER(obj)->itemFolder;
2672                         addressbook_folder_load_person( ctreelist, itemFolder );
2673                         addressbook_folder_load_group( ctreelist, itemFolder );
2674                 }
2675         }
2676         /* gtk_clist_sort(clist); */
2677         gtk_clist_thaw(clist);
2678 }
2679
2680 /*
2681 * Call back function to free adaptor. Call back is setup by function
2682 * gtk_ctree_node_set_row_data_full() when node is populated. This function is
2683 * called when the address book tree widget node is removed by calling
2684 * function gtk_ctree_remove_node().
2685 * Enter: data Tree node's row data.
2686 */
2687 static void addressbook_free_treenode( gpointer data ) {
2688         AddressObject *ao;
2689
2690         ao = ( AddressObject * ) data;
2691         if( ao == NULL ) return;
2692         if( ao->type == ADDR_INTERFACE ) {
2693                 AdapterInterface *ai = ADAPTER_INTERFACE(ao);
2694                 addrbookctl_free_interface( ai );
2695         }
2696         else if( ao->type == ADDR_DATASOURCE ) {
2697                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
2698                 addrbookctl_free_datasource( ads );
2699         }
2700         else if( ao->type == ADDR_ITEM_FOLDER ) {
2701                 AdapterFolder *af = ADAPTER_FOLDER(ao);
2702                 addrbookctl_free_folder( af );
2703         }
2704         else if( ao->type == ADDR_ITEM_GROUP ) {
2705                 AdapterGroup *ag = ADAPTER_GROUP(ao);
2706                 addrbookctl_free_group( ag );
2707         }
2708 }
2709
2710 /*
2711 * Create new adaptor for specified data source.
2712 */
2713 AdapterDSource *addressbook_create_ds_adapter( AddressDataSource *ds,
2714                                 AddressObjectType otype, gchar *name )
2715 {
2716         AdapterDSource *adapter = g_new0( AdapterDSource, 1 );
2717         ADDRESS_OBJECT(adapter)->type = ADDR_DATASOURCE;
2718         ADDRESS_OBJECT_NAME(adapter) = g_strdup( name );
2719         adapter->dataSource = ds;
2720         adapter->subType = otype;
2721         return adapter;
2722 }
2723
2724 void addressbook_ads_set_name( AdapterDSource *adapter, gchar *value ) {
2725         ADDRESS_OBJECT_NAME(adapter) = mgu_replace_string( ADDRESS_OBJECT_NAME(adapter), value );
2726 }
2727
2728 /*
2729  * Load tree from address index with the initial data.
2730  */
2731 static void addressbook_load_tree( void ) {
2732         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2733         GList *nodeIf, *nodeDS;
2734         AdapterInterface *adapter;
2735         AddressInterface *iface;
2736         AddressTypeControlItem *atci;
2737         AddressDataSource *ds;
2738         AdapterDSource *ads;
2739         GtkCTreeNode *node, *newNode;
2740         gchar *name;
2741
2742         nodeIf = _addressInterfaceList_;
2743         while( nodeIf ) {
2744                 adapter = nodeIf->data;
2745                 node = adapter->treeNode;
2746                 iface = adapter->interface;
2747                 atci = adapter->atci;
2748                 if( iface ) {
2749                         if( iface->useInterface ) {
2750                                 /* Load data sources below interface node */
2751                                 nodeDS = iface->listSource;
2752                                 while( nodeDS ) {
2753                                         ds = nodeDS->data;
2754                                         newNode = NULL;
2755                                         name = addrindex_ds_get_name( ds );
2756                                         ads = addressbook_create_ds_adapter( ds, atci->objectType, name );
2757                                         newNode = addressbook_add_object( node, ADDRESS_OBJECT(ads) );
2758                                         nodeDS = g_list_next( nodeDS );
2759                                 }
2760                                 gtk_ctree_expand( ctree, node );
2761                         }
2762                 }
2763                 nodeIf = g_list_next( nodeIf );
2764         }
2765 }
2766
2767 /*
2768  * Convert the old address book to new format.
2769  */
2770 static gboolean addressbook_convert( AddressIndex *addrIndex ) {
2771         gboolean retVal = FALSE;
2772         gboolean errFlag = TRUE;
2773         gchar *msg = NULL;
2774
2775         /* Read old address book, performing conversion */
2776         debug_print( "Reading and converting old address book...\n" );
2777         addrindex_set_file_name( addrIndex, ADDRESSBOOK_OLD_FILE );
2778         addrindex_read_data( addrIndex );
2779         if( addrIndex->retVal == MGU_NO_FILE ) {
2780                 /* We do not have a file - new user */
2781                 debug_print( "New user... create new books...\n" );
2782                 addrindex_create_new_books( addrIndex );
2783                 if( addrIndex->retVal == MGU_SUCCESS ) {
2784                         /* Save index file */
2785                         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
2786                         addrindex_save_data( addrIndex );
2787                         if( addrIndex->retVal == MGU_SUCCESS ) {
2788                                 retVal = TRUE;
2789                                 errFlag = FALSE;
2790                         }
2791                         else {
2792                                 msg = _( "New user, could not save index file." );
2793                         }
2794                 }
2795                 else {
2796                         msg = _( "New user, could not save address book files." );
2797                 }
2798         }
2799         else {
2800                 /* We have an old file */
2801                 if( addrIndex->wasConverted ) {
2802                         /* Converted successfully - save address index */
2803                         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
2804                         addrindex_save_data( addrIndex );
2805                         if( addrIndex->retVal == MGU_SUCCESS ) {
2806                                 msg = _( "Old address book converted successfully." );
2807                                 retVal = TRUE;
2808                                 errFlag = FALSE;
2809                         }
2810                         else {
2811                                 msg = _("Old address book converted,\n"
2812                                         "could not save new address index file" );
2813                         }
2814                 }
2815                 else {
2816                         /* File conversion failed - just create new books */
2817                         debug_print( "File conversion failed... just create new books...\n" );
2818                         addrindex_create_new_books( addrIndex );
2819                         if( addrIndex->retVal == MGU_SUCCESS ) {
2820                                 /* Save index */
2821                                 addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
2822                                 addrindex_save_data( addrIndex );
2823                                 if( addrIndex->retVal == MGU_SUCCESS ) {
2824                                         msg = _("Could not convert address book,\n"
2825                                                 "but created empty new address book files." );
2826                                         retVal = TRUE;
2827                                         errFlag = FALSE;
2828                                 }
2829                                 else {
2830                                         msg = _("Could not convert address book,\n"
2831                                                 "could not create new address book files." );
2832                                 }
2833                         }
2834                         else {
2835                                 msg = _("Could not convert address book\n"
2836                                         "and could not create new address book files." );
2837                         }
2838                 }
2839         }
2840         if( errFlag ) {
2841                 debug_print( "Error\n%s\n", msg );
2842                 alertpanel( _( "Addressbook conversion error" ), msg, _( "Close" ), NULL, NULL );
2843         }
2844         else if( msg ) {
2845                 debug_print( "Warning\n%s\n", msg );
2846                 alertpanel( _( "Addressbook conversion" ), msg, _( "Close" ), NULL, NULL );
2847         }
2848
2849         return retVal;
2850 }
2851
2852 void addressbook_read_file( void ) {
2853         AddressIndex *addrIndex = NULL;
2854
2855         debug_print( "Reading address index...\n" );
2856         if( _addressIndex_ ) {
2857                 debug_print( "address book already read!!!\n" );
2858                 return;
2859         }
2860
2861         addrIndex = addrindex_create_index();
2862
2863         /* Use new address book index. */
2864         addrindex_set_file_path( addrIndex, get_rc_dir() );
2865         addrindex_set_file_name( addrIndex, ADDRESSBOOK_INDEX_FILE );
2866         addrindex_read_data( addrIndex );
2867         if( addrIndex->retVal == MGU_NO_FILE ) {
2868                 /* Conversion required */
2869                 debug_print( "Converting...\n" );
2870                 if( addressbook_convert( addrIndex ) ) {
2871                         _addressIndex_ = addrIndex;
2872                 }
2873         }
2874         else if( addrIndex->retVal == MGU_SUCCESS ) {
2875                 _addressIndex_ = addrIndex;
2876         }
2877         else {
2878                 /* Error reading address book */
2879                 debug_print( "Could not read address index.\n" );
2880                 addrindex_print_index( addrIndex, stdout );
2881                 alertpanel( _( "Addressbook Error" ),
2882                             _( "Could not read address index" ),
2883                             _( "Close" ), NULL, NULL );
2884         }
2885         debug_print( "done.\n" );
2886 }
2887
2888 /*
2889 * Add object into the address index tree widget.
2890 * Enter: node   Parent node.
2891 *        obj    Object to add.
2892 * Return: Node that was added, or NULL if object not added.
2893 */
2894 static GtkCTreeNode *addressbook_add_object(GtkCTreeNode *node,
2895                                             AddressObject *obj)
2896 {
2897         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2898         GtkCTreeNode *added;
2899         AddressObject *pobj;
2900         AddressObjectType otype;
2901         AddressTypeControlItem *atci = NULL;
2902
2903         g_return_val_if_fail(node != NULL, NULL);
2904         g_return_val_if_fail(obj  != NULL, NULL);
2905
2906         pobj = gtk_ctree_node_get_row_data(ctree, node);
2907         g_return_val_if_fail(pobj != NULL, NULL);
2908
2909         /* Determine object type to be displayed */
2910         if( obj->type == ADDR_DATASOURCE ) {
2911                 otype = ADAPTER_DSOURCE(obj)->subType;
2912         }
2913         else {
2914                 otype = obj->type;
2915         }
2916
2917         /* Handle any special conditions. */
2918         added = node;
2919         atci = addrbookctl_lookup( otype );
2920         if( atci ) {
2921                 if( atci->showInTree ) {
2922                         /* Add object to tree */
2923                         gchar **name;
2924                         name = &obj->name;
2925                         added = gtk_ctree_insert_node( ctree, node, NULL, name, FOLDER_SPACING,
2926                                 atci->iconXpm, atci->maskXpm, atci->iconXpmOpen, atci->maskXpmOpen,
2927                                 atci->treeLeaf, atci->treeExpand );
2928                         gtk_ctree_node_set_row_data_full( ctree, added, obj,
2929                                 addressbook_free_treenode );
2930                 }
2931         }
2932
2933         gtk_sctree_sort_node(ctree, node);
2934
2935         return added;
2936 }
2937
2938 /*
2939 * Add group into the address index tree.
2940 * Enter: node      Parent node.
2941 *        ds        Data source.
2942 *        itemGroup Group to add.
2943 * Return: Inserted node.
2944 */
2945 static GtkCTreeNode *addressbook_node_add_group( GtkCTreeNode *node, AddressDataSource *ds, ItemGroup *itemGroup ) {
2946         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2947         GtkCTreeNode *newNode;
2948         AdapterGroup *adapter;
2949         AddressTypeControlItem *atci = NULL;
2950         gchar **name;
2951
2952         if( ds == NULL ) return NULL;
2953         if( node == NULL || itemGroup == NULL ) return NULL;
2954
2955         name = &itemGroup->obj.name;
2956
2957         atci = addrbookctl_lookup( ADDR_ITEM_GROUP );
2958
2959         adapter = g_new0( AdapterGroup, 1 );
2960         ADDRESS_OBJECT_TYPE(adapter) = ADDR_ITEM_GROUP;
2961         ADDRESS_OBJECT_NAME(adapter) = g_strdup( ADDRITEM_NAME(itemGroup) );
2962         adapter->itemGroup = itemGroup;
2963
2964         newNode = gtk_ctree_insert_node( ctree, node, NULL, name, FOLDER_SPACING,
2965                         atci->iconXpm, atci->maskXpm, atci->iconXpm, atci->maskXpm,
2966                         atci->treeLeaf, atci->treeExpand );
2967         gtk_ctree_node_set_row_data_full( ctree, newNode, adapter,
2968                 addressbook_free_treenode );
2969         gtk_sctree_sort_node( ctree, node );
2970         return newNode;
2971 }
2972
2973 /*
2974 * Add folder into the address index tree.
2975 * Enter: node       Parent node.
2976 *        ds         Data source.
2977 *        itemFolder Folder to add.
2978 *        otype      Object type to display.
2979 * Return: Inserted node.
2980 */
2981 static GtkCTreeNode *addressbook_node_add_folder(
2982                 GtkCTreeNode *node, AddressDataSource *ds, ItemFolder *itemFolder, AddressObjectType otype )
2983 {
2984         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2985         GtkCTreeNode *newNode = NULL;
2986         AdapterFolder *adapter;
2987         AddressTypeControlItem *atci = NULL;
2988         GList *listItems = NULL;
2989         gchar **name;
2990         ItemFolder *rootFolder;
2991
2992         if( ds == NULL ) return NULL;
2993         if( node == NULL || itemFolder == NULL ) return NULL;
2994
2995         /* Determine object type */
2996         atci = addrbookctl_lookup( otype );
2997         if( atci == NULL ) return NULL;
2998
2999         rootFolder = addrindex_ds_get_root_folder( ds );
3000         if( itemFolder == rootFolder ) {
3001                 newNode = node;
3002         }
3003         else {
3004                 name = &itemFolder->obj.name;
3005
3006                 adapter = g_new0( AdapterFolder, 1 );
3007                 ADDRESS_OBJECT_TYPE(adapter) = ADDR_ITEM_FOLDER;
3008                 ADDRESS_OBJECT_NAME(adapter) = g_strdup( ADDRITEM_NAME(itemFolder) );
3009                 adapter->itemFolder = itemFolder;
3010
3011                 newNode = gtk_ctree_insert_node( ctree, node, NULL, name, FOLDER_SPACING,
3012                                 atci->iconXpm, atci->maskXpm, atci->iconXpm, atci->maskXpm,
3013                                 atci->treeLeaf, atci->treeExpand );
3014                 gtk_ctree_node_set_row_data_full( ctree, newNode, adapter,
3015                         addressbook_free_treenode );
3016         }
3017
3018         listItems = itemFolder->listFolder;
3019         while( listItems ) {
3020                 ItemFolder *item = listItems->data;
3021                 addressbook_node_add_folder( newNode, ds, item, otype );
3022                 listItems = g_list_next( listItems );
3023         }
3024         listItems = itemFolder->listGroup;
3025         while( listItems ) {
3026                 ItemGroup *item = listItems->data;
3027                 addressbook_node_add_group( newNode, ds, item );
3028                 listItems = g_list_next( listItems );
3029         }
3030         gtk_sctree_sort_node( ctree, node );
3031         return newNode;
3032 }
3033
3034 void addressbook_export_to_file( void ) {
3035         if( _addressIndex_ ) {
3036                 /* Save all new address book data */
3037                 debug_print( "Saving address books...\n" );
3038                 addrindex_save_all_books( _addressIndex_ );
3039
3040                 debug_print( "Exporting addressbook to file...\n" );
3041                 addrindex_save_data( _addressIndex_ );
3042                 if( _addressIndex_->retVal != MGU_SUCCESS ) {
3043                         addrindex_print_index( _addressIndex_, stdout );
3044                 }
3045
3046                 /* Notify address completion of new data */
3047                 invalidate_address_completion();
3048         }
3049 }
3050
3051 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
3052 {
3053         if (event && event->keyval == GDK_Escape)
3054                 addressbook_close();
3055 }
3056
3057 /*
3058 * Comparison using cell contents (text in first column). Used for sort
3059 * address index widget.
3060 */
3061 static gint addressbook_treenode_compare_func(
3062         GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 )
3063 {
3064         GtkCell *cell1 = ((GtkCListRow *)ptr1)->cell;
3065         GtkCell *cell2 = ((GtkCListRow *)ptr2)->cell;
3066         gchar *name1 = NULL, *name2 = NULL;
3067         if( cell1 ) name1 = cell1->u.text;
3068         if( cell2 ) name2 = cell2->u.text;
3069         if( ! name1 ) return ( name2 != NULL );
3070         if( ! name2 ) return -1;
3071         return strcasecmp( name1, name2 );
3072 }
3073
3074 /*
3075 * Comparison using object names and types.
3076 */
3077 static gint addressbook_list_compare_func(
3078         GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 )
3079 {
3080         AddrItemObject *aio1 = ((GtkCListRow *)ptr1)->data;
3081         AddrItemObject *aio2 = ((GtkCListRow *)ptr2)->data;
3082         gchar *name1 = NULL, *name2 = NULL;
3083
3084         /* Order by cell contents */
3085         name1 = ADDRITEM_NAME( aio1 );
3086         name2 = ADDRITEM_NAME( aio2 );
3087
3088         if( aio1->type == aio2->type ) {
3089                 /* Order by name */
3090                 if( ! name1 ) return ( name2 != NULL );
3091                 if( ! name2 ) return -1;
3092                 return strcasecmp( name1, name2 );
3093         }
3094         else {
3095                 /* Order groups before person */
3096                 if( aio1->type == ITEMTYPE_GROUP ) {
3097                         return -1;
3098                 }
3099                 else if( aio2->type == ITEMTYPE_GROUP ) {
3100                         return 1;
3101                 }
3102                 return 0;
3103         }
3104 }
3105
3106 static void addressbook_new_book_cb( gpointer data, guint action, GtkWidget *widget ) {
3107         AdapterDSource *ads;
3108         AdapterInterface *adapter;
3109         GtkCTreeNode *newNode;
3110
3111         adapter = addrbookctl_find_interface( ADDR_IF_BOOK );
3112         if( adapter == NULL ) return;
3113         ads = addressbook_edit_book( _addressIndex_, NULL );
3114         if( ads ) {
3115                 newNode = addressbook_add_object( adapter->treeNode, ADDRESS_OBJECT(ads) );
3116                 if( newNode ) {
3117                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), newNode );
3118                         addrbook.treeSelected = newNode;
3119                 }
3120         }
3121 }
3122
3123 static void addressbook_new_vcard_cb( gpointer data, guint action, GtkWidget *widget ) {
3124         AdapterDSource *ads;
3125         AdapterInterface *adapter;
3126         GtkCTreeNode *newNode;
3127
3128         adapter = addrbookctl_find_interface( ADDR_IF_VCARD );
3129         if( adapter == NULL ) return;
3130         ads = addressbook_edit_vcard( _addressIndex_, NULL );
3131         if( ads ) {
3132                 newNode = addressbook_add_object( adapter->treeNode, ADDRESS_OBJECT(ads) );
3133                 if( newNode ) {
3134                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), newNode );
3135                         addrbook.treeSelected = newNode;
3136                 }
3137         }
3138 }
3139
3140 #ifdef USE_JPILOT
3141 static void addressbook_new_jpilot_cb( gpointer data, guint action, GtkWidget *widget ) {
3142         AdapterDSource *ads;
3143         AdapterInterface *adapter;
3144         AddressInterface *iface;
3145         GtkCTreeNode *newNode;
3146
3147         adapter = addrbookctl_find_interface( ADDR_IF_JPILOT );
3148         if( adapter == NULL ) return;
3149         iface = adapter->interface;
3150         if( ! iface->haveLibrary ) return;
3151         ads = addressbook_edit_jpilot( _addressIndex_, NULL );
3152         if( ads ) {
3153                 newNode = addressbook_add_object( adapter->treeNode, ADDRESS_OBJECT(ads) );
3154                 if( newNode ) {
3155                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), newNode );
3156                         addrbook.treeSelected = newNode;
3157                 }
3158         }
3159 }
3160 #endif
3161
3162 #ifdef USE_LDAP
3163 static void addressbook_new_ldap_cb( gpointer data, guint action, GtkWidget *widget ) {
3164         AdapterDSource *ads;
3165         AdapterInterface *adapter;
3166         AddressInterface *iface;
3167         GtkCTreeNode *newNode;
3168
3169         adapter = addrbookctl_find_interface( ADDR_IF_LDAP );
3170         if( adapter == NULL ) return;
3171         iface = adapter->interface;
3172         if( ! iface->haveLibrary ) return;
3173         ads = addressbook_edit_ldap( _addressIndex_, NULL );
3174         if( ads ) {
3175                 newNode = addressbook_add_object( adapter->treeNode, ADDRESS_OBJECT(ads) );
3176                 if( newNode ) {
3177                         gtk_ctree_select( GTK_CTREE(addrbook.ctree), newNode );
3178                         addrbook.treeSelected = newNode;
3179                 }
3180         }
3181 }
3182
3183 static void addressbook_ldap_show_message( SyldapServer *svr ) {
3184         gchar *name;
3185         *addressbook_msgbuf = '\0';
3186         if( svr ) {
3187                 name = syldap_get_name( svr );
3188                 if( svr->busyFlag ) {
3189                         g_snprintf( addressbook_msgbuf,
3190                                     sizeof(addressbook_msgbuf), "%s: %s", name,
3191                                     ADDRESSBOOK_LDAP_BUSYMSG );
3192                 }
3193                 else {
3194                         if( svr->retVal == MGU_SUCCESS ) {
3195                                 g_snprintf( addressbook_msgbuf,
3196                                             sizeof(addressbook_msgbuf), "%s",
3197                                             name );
3198                         }
3199                         else {
3200                                 g_snprintf( addressbook_msgbuf,
3201                                             sizeof(addressbook_msgbuf),
3202                                             "%s: %s", name,
3203                                             mgu_error2string( svr->retVal ) );
3204                         }
3205                 }
3206         }
3207         addressbook_status_show( addressbook_msgbuf );
3208 }
3209
3210 static void addressbook_ldap_show_results( SyldapServer *sls ) {
3211         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
3212         AddressObject *obj;
3213         AdapterDSource *ads = NULL;
3214         AddressDataSource *ds = NULL;
3215         AddressInterface *iface = NULL;
3216
3217         if( sls == NULL ) return;
3218         if( ! addrbook.treeSelected ) return;
3219         if( GTK_CTREE_ROW( addrbook.treeSelected )->level == 1 ) return;
3220
3221         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
3222         if( obj == NULL ) return;
3223         if( obj->type == ADDR_DATASOURCE ) {
3224                 ads = ADAPTER_DSOURCE(obj);
3225                 if( ads->subType == ADDR_LDAP ) {
3226                         SyldapServer *server;
3227
3228                         ds = ads->dataSource;
3229                         if( ds == NULL ) return;
3230                         iface = ds->interface;
3231                         if( ! iface->haveLibrary ) return;
3232                         server = ds->rawDataSource;
3233                         if( server == sls ) {
3234                                 /* Read from cache */
3235                                 gtk_widget_show_all(addrbook.window);
3236                                 addressbook_set_clist( obj );
3237                                 addressbook_ldap_show_message( sls );
3238                                 gtk_widget_show_all(addrbook.window);
3239                                 gtk_entry_set_text( GTK_ENTRY(addrbook.entry), "" );
3240                         }
3241                 }
3242         }
3243 }
3244
3245 /*
3246  * LDAP idle function. This function is called during UI idle time while
3247  * an LDAP search is in progress.
3248  * Enter: data Reference to LDAP server object.
3249  */
3250 static void addressbook_ldap_idle( gpointer data ) {
3251         SyldapServer *server;
3252
3253         server = ( SyldapServer * ) data;       
3254         if( ! server->busyFlag ) {
3255                 /* Server has completed search - remove from idle list */
3256                 gtk_idle_remove( server->idleId );
3257
3258                 /* Process callback and free up the thread */
3259                 addressbook_ldap_show_results( server );
3260                 g_free( server->thread );
3261                 server->thread = NULL;
3262         }
3263 }
3264
3265 /*
3266  * Perform lookup for LDAP search.
3267  * Enter: ads     Adapter for data source.
3268  *        sLookup Lookup string.
3269  */
3270 static void addressbook_ldap_lookup( AdapterDSource *ads, gchar *sLookup ) {
3271         AddressDataSource *ds = NULL;
3272         AddressInterface *iface = NULL;
3273         SyldapServer *server;
3274
3275         ds = ads->dataSource;
3276         if( ds == NULL ) return;
3277         iface = ds->interface;
3278         if( ! iface->haveLibrary ) return;
3279         server = ds->rawDataSource;
3280         if( server ) {
3281                 syldap_cancel_read( server );
3282                 if( *sLookup == '\0' || strlen( sLookup ) < 1 ) return;
3283                 syldap_set_search_value( server, sLookup );
3284                 server->idleId = gtk_idle_add(
3285                         ( GtkFunction ) addressbook_ldap_idle, server );
3286                 syldap_read_data_th( server );
3287                 addressbook_ldap_show_message( server );
3288         }
3289 }
3290 #endif
3291
3292 /*
3293  * Lookup button handler.
3294  */
3295 static void addressbook_lup_clicked( GtkButton *button, gpointer