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