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