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