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