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