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