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