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