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