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