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