bdbf3a8f3574d04319658f60ba4012128d30c882
[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_widget_realize(window);
923
924         g_signal_connect(G_OBJECT(window), "delete_event",
925                          G_CALLBACK(addressbook_close), NULL);
926         g_signal_connect(G_OBJECT(window), "size_allocate",
927                          G_CALLBACK(addressbook_size_allocate_cb), NULL);
928         g_signal_connect(G_OBJECT(window), "key_press_event",
929                          G_CALLBACK(key_pressed), NULL);
930         MANAGE_WINDOW_SIGNALS_CONNECT(window);
931
932         vbox = gtk_vbox_new(FALSE, 0);
933         gtk_container_add(GTK_CONTAINER(window), vbox);
934
935         /* Menu bar */
936         ui_manager = gtk_ui_manager_new();
937         action_group = cm_menu_create_action_group_full(ui_manager,"Menu", addressbook_entries,
938                         G_N_ELEMENTS(addressbook_entries), NULL);
939         gtk_action_group_add_actions(action_group, addressbook_tree_popup_entries,
940                         G_N_ELEMENTS(addressbook_tree_popup_entries), NULL);
941         gtk_action_group_add_actions(action_group, addressbook_list_popup_entries,
942                         G_N_ELEMENTS(addressbook_list_popup_entries), NULL);
943
944         MENUITEM_ADDUI_MANAGER(ui_manager, "/", "Menu", NULL, GTK_UI_MANAGER_MENUBAR)
945
946         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu", "Book", "Book", GTK_UI_MANAGER_MENU)
947         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu", "Edit", "Edit", GTK_UI_MANAGER_MENU)
948         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu", "Tools", "Tools", GTK_UI_MANAGER_MENU)
949         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu", "Help", "Help", GTK_UI_MANAGER_MENU)
950
951 /* Book menu */
952         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "NewBook", "Book/NewBook", GTK_UI_MANAGER_MENUITEM)
953         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "NewFolder", "Book/NewFolder", GTK_UI_MANAGER_MENUITEM)
954         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "NewVCard", "Book/NewVCard", GTK_UI_MANAGER_MENUITEM)
955 #ifdef USE_JPILOT
956         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "NewJPilot", "Book/NewJPilot", GTK_UI_MANAGER_MENUITEM)
957 #endif
958 #ifdef USE_LDAP
959         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "NewLDAPServer", "Book/NewLDAPServer", GTK_UI_MANAGER_MENUITEM)
960 #endif
961         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "Separator1", "Book/---", GTK_UI_MANAGER_SEPARATOR)
962         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "EditBook", "Book/EditBook", GTK_UI_MANAGER_MENUITEM)
963         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "DeleteBook", "Book/DeleteBook", GTK_UI_MANAGER_MENUITEM)
964         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "Separator2", "Book/---", GTK_UI_MANAGER_SEPARATOR)
965         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "Save", "Book/Save", GTK_UI_MANAGER_MENUITEM)
966         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Book", "Close", "Book/Close", GTK_UI_MANAGER_MENUITEM)
967
968 /* Address menu */
969         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "SelectAll", "Address/SelectAll", GTK_UI_MANAGER_MENUITEM)
970         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Separator1", "Address/---", GTK_UI_MANAGER_SEPARATOR)
971         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Cut", "Address/Cut", GTK_UI_MANAGER_MENUITEM)
972         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Copy", "Address/Copy", GTK_UI_MANAGER_MENUITEM)
973         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Paste", "Address/Paste", GTK_UI_MANAGER_MENUITEM)
974         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Separator2", "Address/---", GTK_UI_MANAGER_SEPARATOR)
975         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Edit", "Address/Edit", GTK_UI_MANAGER_MENUITEM)
976         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Delete", "Address/Delete", GTK_UI_MANAGER_MENUITEM)
977         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Separator3", "Address/---", GTK_UI_MANAGER_SEPARATOR)
978         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "NewAddress", "Address/NewAddress", GTK_UI_MANAGER_MENUITEM)
979         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "NewGroup", "Address/NewGroup", GTK_UI_MANAGER_MENUITEM)
980         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Separator4", "Address/---", GTK_UI_MANAGER_SEPARATOR)
981         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Mailto", "Address/Mailto", GTK_UI_MANAGER_MENUITEM)
982         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Edit", "Merge", "Address/Merge", GTK_UI_MANAGER_MENUITEM)
983
984 /* Tools menu */
985         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "ImportLDIF", "Tools/ImportLDIF", GTK_UI_MANAGER_MENUITEM)
986         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "ImportMutt", "Tools/ImportMutt", GTK_UI_MANAGER_MENUITEM)
987         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "ImportPine", "Tools/ImportPine", GTK_UI_MANAGER_MENUITEM)
988         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "Separator1", "Tools/---", GTK_UI_MANAGER_SEPARATOR)
989         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "ExportHTML", "Tools/ExportHTML", GTK_UI_MANAGER_MENUITEM)
990         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "ExportLDIF", "Tools/ExportLDIF", GTK_UI_MANAGER_MENUITEM)
991         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "Separator2", "Tools/---", GTK_UI_MANAGER_SEPARATOR)
992         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "FindDuplicates", "Tools/FindDuplicates", GTK_UI_MANAGER_MENUITEM)
993         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Tools", "EditAttrs", "Tools/EditAttrs", GTK_UI_MANAGER_MENUITEM)
994
995 /* Help menu */
996         MENUITEM_ADDUI_MANAGER(ui_manager, "/Menu/Help", "About", "Help/About", GTK_UI_MANAGER_MENUITEM)
997
998         gtk_window_add_accel_group(GTK_WINDOW(window),
999                         gtk_ui_manager_get_accel_group(ui_manager));
1000
1001         menubar = gtk_ui_manager_get_widget(ui_manager, "/Menu");
1002
1003         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
1004
1005         vbox2 = gtk_vbox_new(FALSE, BORDER_WIDTH);
1006         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
1007         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
1008
1009         ctree_swin = gtk_scrolled_window_new(NULL, NULL);
1010         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ctree_swin),
1011                                        GTK_POLICY_AUTOMATIC,
1012                                        GTK_POLICY_AUTOMATIC);
1013         gtk_widget_set_size_request(ctree_swin, COL_FOLDER_WIDTH + 20, -1);
1014
1015         /* Address index */
1016         ctree = gtk_sctree_new_with_titles(N_INDEX_COLS, 0, index_titles);
1017         gtk_widget_set_can_focus(GTK_CMCLIST(ctree)->column[0].button, FALSE);
1018
1019         gtk_container_add(GTK_CONTAINER(ctree_swin), ctree);
1020         gtk_cmclist_set_selection_mode(GTK_CMCLIST(ctree), GTK_SELECTION_BROWSE);
1021         gtk_cmclist_set_column_width(GTK_CMCLIST(ctree), 0, COL_FOLDER_WIDTH);
1022         gtk_cmctree_set_expander_style(GTK_CMCTREE(ctree),
1023                              GTK_CMCTREE_EXPANDER_TRIANGLE);
1024         gtk_sctree_set_stripes(GTK_SCTREE(ctree), prefs_common.use_stripes_in_summaries);
1025         gtk_cmctree_set_indent(GTK_CMCTREE(ctree), CTREE_INDENT);
1026         gtk_cmclist_set_compare_func(GTK_CMCLIST(ctree),
1027                                    addressbook_treenode_compare_func);
1028
1029         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
1030                          G_CALLBACK(addressbook_tree_selected), NULL);
1031         g_signal_connect(G_OBJECT(ctree), "button_press_event",
1032                          G_CALLBACK(addressbook_tree_button_pressed),
1033                          NULL);
1034         g_signal_connect(G_OBJECT(ctree), "button_release_event",
1035                          G_CALLBACK(addressbook_tree_button_released),
1036                          NULL);
1037         /* TEMPORARY */
1038         g_signal_connect(G_OBJECT(ctree), "select_row",
1039                          G_CALLBACK(addressbook_select_row_tree), NULL);
1040
1041         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
1042                           addressbook_drag_types, 1,
1043                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
1044         g_signal_connect(G_OBJECT(ctree), "drag_motion",
1045                          G_CALLBACK(addressbook_drag_motion_cb),
1046                          ctree);
1047         g_signal_connect(G_OBJECT(ctree), "drag_leave",
1048                          G_CALLBACK(addressbook_drag_leave_cb),
1049                          ctree);
1050         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
1051                          G_CALLBACK(addressbook_drag_received_cb),
1052                          ctree);
1053         g_signal_connect(G_OBJECT(ctree), "focus_in_event",
1054                 G_CALLBACK(addressbook_address_index_focus_evt_in), NULL);
1055         g_signal_connect(G_OBJECT(ctree), "focus_out_event",
1056                 G_CALLBACK(addressbook_address_index_focus_evt_out), NULL);
1057
1058         clist_vbox = gtk_vbox_new(FALSE, 4);
1059
1060         clist_swin = gtk_scrolled_window_new(NULL, NULL);
1061         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
1062                                        GTK_POLICY_AUTOMATIC,
1063                                        GTK_POLICY_AUTOMATIC);
1064         gtk_box_pack_start(GTK_BOX(clist_vbox), clist_swin, TRUE, TRUE, 0);
1065
1066         /* Address list */
1067         clist = gtk_sctree_new_with_titles(N_LIST_COLS, 0, list_titles);
1068         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
1069         gtk_cmclist_set_selection_mode(GTK_CMCLIST(clist), GTK_SELECTION_MULTIPLE);
1070         gtk_cmctree_set_expander_style(GTK_CMCTREE(clist),
1071                              GTK_CMCTREE_EXPANDER_TRIANGLE);
1072         gtk_sctree_set_stripes(GTK_SCTREE(ctree), prefs_common.use_stripes_in_summaries);
1073         gtk_cmctree_set_indent(GTK_CMCTREE(clist), CTREE_INDENT);
1074         gtk_cmclist_set_column_width(GTK_CMCLIST(clist), COL_NAME,
1075                                    COL_NAME_WIDTH);
1076         gtk_cmclist_set_column_width(GTK_CMCLIST(clist), COL_ADDRESS,
1077                                    COL_ADDRESS_WIDTH);
1078         gtk_widget_set_size_request(clist, -1, 80);
1079
1080         addressbook_sort_list(GTK_CMCLIST(clist), COL_NAME, GTK_SORT_ASCENDING);
1081         g_signal_connect(G_OBJECT(GTK_CMCLIST(clist)->column[COL_NAME].button),
1082                 "clicked", G_CALLBACK(addressbook_name_clicked), clist);
1083         g_signal_connect(G_OBJECT(GTK_CMCLIST(clist)->column[COL_ADDRESS].button),
1084                 "clicked", G_CALLBACK(addressbook_address_clicked), clist);
1085         g_signal_connect(G_OBJECT(GTK_CMCLIST(clist)->column[COL_REMARKS].button),
1086                 "clicked", G_CALLBACK(addressbook_remarks_clicked), clist);
1087         g_signal_connect(G_OBJECT(clist), "focus_in_event",
1088                 G_CALLBACK(addressbook_address_list_focus_evt_in), NULL);
1089         g_signal_connect(G_OBJECT(clist), "focus_out_event",
1090                 G_CALLBACK(addressbook_address_list_focus_evt_out), NULL);
1091
1092         for (i = 0; i < N_LIST_COLS; i++)
1093                 gtk_widget_set_can_focus(GTK_CMCLIST(clist)->column[i].button,
1094                                          FALSE);
1095
1096         g_signal_connect(G_OBJECT(clist), "tree_select_row",
1097                          G_CALLBACK(addressbook_list_row_selected), NULL);
1098         g_signal_connect(G_OBJECT(clist), "tree_unselect_row",
1099                          G_CALLBACK(addressbook_list_row_unselected), NULL);
1100         g_signal_connect(G_OBJECT(clist), "button_press_event",
1101                          G_CALLBACK(addressbook_list_button_pressed),
1102                          NULL);
1103         g_signal_connect(G_OBJECT(clist), "button_release_event",
1104                          G_CALLBACK(addressbook_list_button_released),
1105                          NULL);
1106         g_signal_connect(G_OBJECT(clist), "tree_expand",
1107                          G_CALLBACK(addressbook_person_expand_node), NULL );
1108         g_signal_connect(G_OBJECT(clist), "tree_collapse",
1109                          G_CALLBACK(addressbook_person_collapse_node), NULL );
1110         g_signal_connect(G_OBJECT(clist), "start_drag",
1111                          G_CALLBACK(addressbook_start_drag), NULL);
1112         g_signal_connect(G_OBJECT(clist), "drag_data_get",
1113                          G_CALLBACK(addressbook_drag_data_get), NULL);  
1114         hbox = gtk_hbox_new(FALSE, 4);
1115         gtk_box_pack_start(GTK_BOX(clist_vbox), hbox, FALSE, FALSE, 0);
1116
1117         label = gtk_label_new(_("Search"));
1118         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1119
1120         entry = gtk_entry_new();
1121         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1122
1123         address_completion_register_entry(GTK_ENTRY(entry), FALSE);
1124
1125         g_signal_connect(G_OBJECT(entry), "key_press_event",
1126                          G_CALLBACK(addressbook_entry_key_pressed),
1127                          NULL);
1128         g_signal_connect(G_OBJECT(entry), "activate",
1129                          G_CALLBACK(addressbook_entry_activated), NULL);
1130
1131         if (!prefs_common.addressbook_use_editaddress_dialog) {
1132                 editaddress_vbox = gtk_vbox_new(FALSE, 4);
1133                 vpaned = gtk_vpaned_new();
1134                 gtk_paned_pack1(GTK_PANED(vpaned), clist_vbox, FALSE, FALSE);
1135                 gtk_paned_pack2(GTK_PANED(vpaned), editaddress_vbox, TRUE, FALSE);
1136         } else {
1137                 vpaned = NULL;
1138                 editaddress_vbox = NULL;
1139         }
1140         hpaned = gtk_hpaned_new();
1141         gtk_box_pack_start(GTK_BOX(vbox2), hpaned, TRUE, TRUE, 0);
1142         gtk_paned_pack1(GTK_PANED(hpaned), ctree_swin, FALSE, FALSE);
1143         if (prefs_common.addressbook_use_editaddress_dialog)
1144                 gtk_paned_pack2(GTK_PANED(hpaned), clist_vbox, TRUE, FALSE);
1145         else
1146                 gtk_paned_pack2(GTK_PANED(hpaned), vpaned, TRUE, FALSE);
1147
1148         /* Status bar */
1149         hsbox = gtk_hbox_new(FALSE, 0);
1150         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
1151         statusbar = gtk_statusbar_new();
1152         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
1153
1154         /* Button panel */
1155         hbbox = gtk_hbutton_box_new();
1156         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
1157         gtk_box_set_spacing(GTK_BOX(hbbox), 2);
1158         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 4);
1159         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
1160
1161         gtkut_stock_button_add_help(hbbox, &help_btn);
1162
1163         edit_btn = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1164         gtk_widget_set_can_default(edit_btn, TRUE);
1165         gtk_box_pack_start(GTK_BOX(hbbox), edit_btn, TRUE, TRUE, 0);
1166         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1167         gtk_widget_set_can_default(del_btn, TRUE);
1168         gtk_box_pack_start(GTK_BOX(hbbox), del_btn, TRUE, TRUE, 0);
1169         reg_btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
1170         gtk_widget_set_can_default(reg_btn, TRUE);
1171         gtk_box_pack_start(GTK_BOX(hbbox), reg_btn, TRUE, TRUE, 0);
1172
1173
1174         lup_btn = gtk_button_new_from_stock(GTK_STOCK_FIND);
1175         gtk_widget_set_can_default(lup_btn, TRUE);
1176         gtk_box_pack_start(GTK_BOX(hbox), lup_btn, TRUE, TRUE, 0);
1177
1178         g_signal_connect(G_OBJECT(help_btn), "clicked",
1179                          G_CALLBACK(manual_open_with_anchor_cb),
1180                          MANUAL_ANCHOR_ADDRBOOK);
1181
1182         g_signal_connect(G_OBJECT(edit_btn), "clicked",
1183                          G_CALLBACK(addressbook_edit_clicked), NULL);
1184         g_signal_connect(G_OBJECT(del_btn), "clicked",
1185                          G_CALLBACK(addressbook_del_clicked), NULL);
1186         g_signal_connect(G_OBJECT(reg_btn), "clicked",
1187                          G_CALLBACK(addressbook_reg_clicked), NULL);
1188         g_signal_connect(G_OBJECT(lup_btn), "clicked",
1189                          G_CALLBACK(addressbook_lup_clicked), NULL);
1190
1191         to_btn = gtk_button_new_with_label
1192                 (prefs_common_translated_header_name("To:"));
1193         gtk_widget_set_can_default(to_btn, TRUE);
1194         gtk_box_pack_start(GTK_BOX(hbbox), to_btn, TRUE, TRUE, 0);
1195         cc_btn = gtk_button_new_with_label
1196                 (prefs_common_translated_header_name("Cc:"));
1197         gtk_widget_set_can_default(cc_btn, TRUE);
1198         gtk_box_pack_start(GTK_BOX(hbbox), cc_btn, TRUE, TRUE, 0);
1199         bcc_btn = gtk_button_new_with_label
1200                 (prefs_common_translated_header_name("Bcc:"));
1201         gtk_widget_set_can_default(bcc_btn, TRUE);
1202         gtk_box_pack_start(GTK_BOX(hbbox), bcc_btn, TRUE, TRUE, 0);
1203
1204         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
1205         gtk_widget_set_can_default(close_btn, TRUE);
1206         gtk_box_pack_start(GTK_BOX(hbbox), close_btn, TRUE, TRUE, 0);
1207
1208         g_signal_connect(G_OBJECT(to_btn), "clicked",
1209                          G_CALLBACK(addressbook_to_clicked),
1210                          GINT_TO_POINTER(COMPOSE_TO));
1211         g_signal_connect(G_OBJECT(cc_btn), "clicked",
1212                          G_CALLBACK(addressbook_to_clicked),
1213                          GINT_TO_POINTER(COMPOSE_CC));
1214         g_signal_connect(G_OBJECT(bcc_btn), "clicked",
1215                          G_CALLBACK(addressbook_to_clicked),
1216                          GINT_TO_POINTER(COMPOSE_BCC));
1217         g_signal_connect(G_OBJECT(close_btn), "clicked",
1218                          G_CALLBACK(addressbook_close_clicked), NULL);
1219
1220         /* Build icons for interface */
1221
1222         /* Build control tables */
1223         addrbookctl_build_map(window);
1224         addrbookctl_build_iflist();
1225         addrbookctl_build_ifselect();
1226
1227         addrbook.clist   = NULL;
1228
1229         /* Add each interface into the tree as a root level folder */
1230         nodeIf = _addressInterfaceList_;
1231         while( nodeIf ) {
1232                 AdapterInterface *adapter = nodeIf->data;
1233                 AddressInterface *iface = adapter->interface;
1234                 nodeIf = g_list_next(nodeIf);
1235
1236                 if(iface->useInterface) {
1237                         AddressTypeControlItem *atci = adapter->atci;
1238                         text = atci->displayName;
1239                         adapter->treeNode =
1240                                 gtk_sctree_insert_node( GTK_CMCTREE(ctree),
1241                                         NULL, NULL, &text, FOLDER_SPACING,
1242                                         interfacexpm,
1243                                         interfacexpm,
1244                                         FALSE, FALSE );
1245                         cm_menu_set_sensitive_full(ui_manager, atci->menuCommand, adapter->haveLibrary );
1246                         gtk_cmctree_node_set_row_data_full(
1247                                 GTK_CMCTREE(ctree), adapter->treeNode, adapter,
1248                                 addressbook_free_treenode );
1249                 }
1250         }
1251
1252         /* Popup menu */
1253
1254         MENUITEM_ADDUI_MANAGER(ui_manager, "/", "Popups", NULL, GTK_UI_MANAGER_MENUBAR);
1255         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups", "ABTreePopup", "ABTreePopup", GTK_UI_MANAGER_MENU)
1256         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "EditBook", "ABTreePopup/EditBook", GTK_UI_MANAGER_MENUITEM)
1257         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "DeleteBook", "ABTreePopup/DeleteBook", GTK_UI_MANAGER_MENUITEM)
1258         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "Separator1", "ABTreePopup/---", GTK_UI_MANAGER_SEPARATOR)
1259         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "NewBook", "ABTreePopup/NewBook", GTK_UI_MANAGER_MENUITEM)
1260         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "NewFolder", "ABTreePopup/NewFolder", GTK_UI_MANAGER_MENUITEM)
1261         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "NewGroup", "ABTreePopup/NewGroup", GTK_UI_MANAGER_MENUITEM)
1262         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "Separator2", "ABTreePopup/---", GTK_UI_MANAGER_SEPARATOR)
1263         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "Cut", "ABTreePopup/Cut", GTK_UI_MANAGER_MENUITEM)
1264         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "Copy", "ABTreePopup/Copy", GTK_UI_MANAGER_MENUITEM)
1265         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABTreePopup", "Paste", "ABTreePopup/Paste", GTK_UI_MANAGER_MENUITEM)
1266         
1267         tree_popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1268                                 gtk_ui_manager_get_widget(ui_manager, "/Popups/ABTreePopup")));
1269
1270         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups", "ABListPopup", "ABListPopup", GTK_UI_MANAGER_MENU)
1271         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "SelectAll", "ABListPopup/SelectAll", GTK_UI_MANAGER_MENUITEM)
1272         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Separator1", "ABListPopup/---", GTK_UI_MANAGER_SEPARATOR)
1273         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Edit", "ABListPopup/Edit", GTK_UI_MANAGER_MENUITEM)
1274         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Delete", "ABListPopup/Delete", GTK_UI_MANAGER_MENUITEM)
1275         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Separator2", "ABListPopup/---", GTK_UI_MANAGER_SEPARATOR)
1276         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "NewAddress", "ABListPopup/NewAddress", GTK_UI_MANAGER_MENUITEM)
1277         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "NewGroup", "ABListPopup/NewGroup", GTK_UI_MANAGER_MENUITEM)
1278         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Separator3", "ABListPopup/---", GTK_UI_MANAGER_SEPARATOR)
1279         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Cut", "ABListPopup/Cut", GTK_UI_MANAGER_MENUITEM)
1280         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Copy", "ABListPopup/Copy", GTK_UI_MANAGER_MENUITEM)
1281         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Paste", "ABListPopup/Paste", GTK_UI_MANAGER_MENUITEM)
1282         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Separator4", "ABListPopup/---", GTK_UI_MANAGER_SEPARATOR)
1283         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Mailto", "ABListPopup/Mailto", GTK_UI_MANAGER_MENUITEM)
1284 #ifdef USE_LDAP
1285         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "BrowseEntry", "ABListPopup/BrowseEntry", GTK_UI_MANAGER_MENUITEM)
1286 #endif
1287         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popups/ABListPopup", "Merge", "ABListPopup/Merge", GTK_UI_MANAGER_MENUITEM)
1288         list_popup = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
1289                                 gtk_ui_manager_get_widget(ui_manager, "/Popups/ABListPopup")));
1290
1291         addrbook.window  = window;
1292         addrbook.hpaned  = hpaned;
1293         addrbook.vpaned  = vpaned;
1294         addrbook.menubar = menubar;
1295         addrbook.ctree   = ctree;
1296         addrbook.ctree_swin
1297                          = ctree_swin;
1298         addrbook.editaddress_vbox = editaddress_vbox;
1299         addrbook.clist   = clist;
1300         addrbook.label   = label;
1301         addrbook.entry   = entry;
1302         addrbook.statusbar = statusbar;
1303         addrbook.status_cid = gtk_statusbar_get_context_id(
1304                         GTK_STATUSBAR(statusbar), "Addressbook Window" );
1305
1306         addrbook.help_btn = help_btn;
1307         addrbook.edit_btn = edit_btn;
1308         addrbook.del_btn = del_btn;
1309         addrbook.reg_btn = reg_btn;
1310         addrbook.lup_btn = lup_btn;
1311         addrbook.to_btn  = to_btn;
1312         addrbook.cc_btn  = cc_btn;
1313         addrbook.bcc_btn = bcc_btn;
1314
1315         addrbook.tree_popup   = tree_popup;
1316         addrbook.list_popup   = list_popup;
1317         addrbook.ui_manager   = ui_manager;
1318
1319         addrbook.listSelected = NULL;
1320
1321         if (!geometry.min_height) {
1322                 geometry.min_width = ADDRESSBOOK_WIDTH;
1323                 geometry.min_height = ADDRESSBOOK_HEIGHT;
1324         }
1325
1326         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
1327                                       GDK_HINT_MIN_SIZE);
1328         gtk_widget_set_size_request(window, prefs_common.addressbookwin_width,
1329                                     prefs_common.addressbookwin_height);
1330 #ifdef G_OS_WIN32
1331         gtk_window_move(GTK_WINDOW(window), 48, 48);
1332 #endif
1333
1334         if (!prefs_common.addressbook_use_editaddress_dialog) {
1335                 if (prefs_common.addressbook_vpaned_pos > 0)
1336                         gtk_paned_set_position(GTK_PANED(vpaned), 
1337                                 prefs_common.addressbook_vpaned_pos);
1338         }       
1339         if (prefs_common.addressbook_hpaned_pos > 0)
1340                 gtk_paned_set_position(GTK_PANED(hpaned), 
1341                         prefs_common.addressbook_hpaned_pos);
1342
1343
1344         gtk_widget_show_all(window);
1345 }
1346
1347 /**
1348  * Close address book window and save to file(s).
1349  */
1350 static gint addressbook_close( void ) {
1351         address_completion_end(addrbook.window);
1352         if (!prefs_common.addressbook_use_editaddress_dialog)
1353                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
1354         
1355         addressbook_pane_save_position();
1356         
1357         gtk_widget_hide(addrbook.window);
1358         addressbook_export_to_file();
1359         return TRUE;
1360 }
1361
1362 /**
1363  * Display message in status line.
1364  * \param msg Message to display.
1365  */
1366 static void addressbook_status_show( gchar *msg ) {
1367         if( addrbook.statusbar != NULL ) {
1368                 gtk_statusbar_pop(
1369                         GTK_STATUSBAR(addrbook.statusbar),
1370                         addrbook.status_cid );
1371                 if( msg ) {
1372                         gtk_statusbar_push(
1373                                 GTK_STATUSBAR(addrbook.statusbar),
1374                                 addrbook.status_cid, msg );
1375                 }
1376         }
1377 }
1378
1379 static void addressbook_ds_show_message( AddressDataSource *ds ) {
1380         gint retVal;
1381         gchar *name;
1382         gchar *desc;
1383         *addressbook_msgbuf = '\0';
1384         if( ds ) {
1385                 name = addrindex_ds_get_name( ds );
1386                 retVal = addrindex_ds_get_status_code( ds );
1387                 if( retVal == MGU_SUCCESS ) {
1388                         g_snprintf( addressbook_msgbuf,
1389                                     sizeof(addressbook_msgbuf), "%s", name );
1390                 }
1391                 else {
1392                         desc = addressbook_err2string( _lutErrorsGeneral_, retVal );
1393                         g_snprintf( addressbook_msgbuf, 
1394                             sizeof(addressbook_msgbuf), "%s: %s", name, desc );
1395                 }
1396         }
1397         addressbook_status_show( addressbook_msgbuf );
1398 }
1399
1400 static void addressbook_edit_clicked(GtkButton *button, gpointer data)
1401 {
1402         addressbook_edit_address_cb(NULL, NULL);
1403 }
1404
1405 static gboolean find_person(AddrSelectItem *item_a, ItemPerson *person)
1406 {
1407         return ((ItemPerson *)item_a->addressItem == person)?0:-1;
1408 }
1409
1410 /*
1411 * Delete one or more objects from address list.
1412 */
1413 static void addressbook_del_clicked(GtkButton *button, gpointer data)
1414 {
1415         GtkCMCTree *clist = GTK_CMCTREE(addrbook.clist);
1416         GtkCMCTree *ctree = GTK_CMCTREE(addrbook.ctree);
1417         AddressObject *pobj;
1418         AdapterDSource *ads = NULL;
1419         GtkCMCTreeNode *nodeList;
1420         gboolean procFlag;
1421         AlertValue aval;
1422         AddressBookFile *abf = NULL;
1423         AddressDataSource *ds = NULL;
1424         AddressInterface *iface;
1425         AddrItemObject *aio;
1426         AddrSelectItem *item;
1427         GList *list, *node;
1428         gboolean refreshList = FALSE;
1429         
1430         pobj = gtk_cmctree_node_get_row_data(ctree, addrbook.opened );
1431         cm_return_if_fail(pobj != NULL);
1432
1433         /* Test whether anything selected for deletion */
1434         nodeList = addrbook.listSelected;
1435
1436         aio = gtk_cmctree_node_get_row_data( clist, nodeList );
1437         if( aio == NULL) return;
1438         ds = addressbook_find_datasource( addrbook.treeSelected );
1439         if( ds == NULL ) return;
1440
1441         /* Test for read only */
1442         iface = ds->interface;
1443         if( iface->readOnly ) {
1444                 alertpanel( _("Delete address(es)"),
1445                         _("This address data is readonly and cannot be deleted."),
1446                         GTK_STOCK_CLOSE, NULL, NULL, ALERTFOCUS_FIRST);
1447                 return;
1448         }
1449
1450         /* Test whether Ok to proceed */
1451         procFlag = FALSE;
1452         if( pobj->type == ADDR_DATASOURCE ) {
1453                 ads = ADAPTER_DSOURCE(pobj);
1454                 if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
1455         }
1456         else if( pobj->type == ADDR_ITEM_FOLDER ) {
1457                 procFlag = TRUE;
1458         }
1459         else if( pobj->type == ADDR_ITEM_GROUP ) {
1460                 procFlag = TRUE;
1461         }
1462         if( ! procFlag ) return;
1463         abf = ds->rawDataSource;
1464         if( abf == NULL ) return;
1465
1466         gtk_cmclist_freeze(GTK_CMCLIST(addrbook.clist));
1467         g_signal_handlers_block_by_func
1468                 (G_OBJECT(addrbook.clist),
1469                  G_CALLBACK(addressbook_list_row_unselected), NULL);
1470
1471         /* Process deletions */
1472         if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
1473                 GList *groups = NULL, *persons = NULL, *emails = NULL;
1474                 gboolean group_delete = TRUE;
1475                 /* Items inside folders */
1476                 list = addrselect_get_list( _addressSelect_ );
1477                 /* Confirm deletion */
1478                 node = list;
1479                 while( node ) {
1480                         item = node->data;
1481                         node = g_list_next( node );
1482                         aio = ( AddrItemObject * ) item->addressItem;
1483                         if( aio->type == ITEMTYPE_PERSON || aio->type == ITEMTYPE_EMAIL ) {
1484                                 group_delete = FALSE;
1485                                 break;
1486                         }
1487                 }
1488                 if (group_delete) {
1489                         aval = alertpanel( _("Delete group"),
1490                                         _("Really delete the group(s)?\n"
1491                                           "The addresses it contains will not be lost."),
1492                                         GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND );
1493                         if( aval != G_ALERTALTERNATE ) {
1494                                 goto thaw_ret;
1495                         }
1496                 } else {
1497                         aval = alertpanel( _("Delete address(es)"),
1498                                         _("Really delete the address(es)?"),
1499                                         GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND );
1500                         if( aval != G_ALERTALTERNATE ) {
1501                                 goto thaw_ret;
1502                         }
1503                 }
1504         
1505         /* first, set lists of groups and persons to remove */
1506                 node = list;
1507                 while( node ) {
1508                         item = node->data;
1509                         node = g_list_next( node );
1510                         aio = ( AddrItemObject * ) item->addressItem;
1511                         if (!aio)
1512                                 continue;
1513                         if( aio->type == ITEMTYPE_GROUP ) {
1514                                 groups = g_list_prepend(groups, item);
1515                         }
1516                         else if( aio->type == ITEMTYPE_PERSON ) {
1517                                 persons = g_list_prepend(persons, item);
1518                         }
1519                 }
1520         /* then set list of emails to remove *if* they're not children of
1521          * persons to remove */
1522                 node = list;
1523                 while( node ) {
1524                         item = node->data;
1525                         node = g_list_next( node );
1526                         aio = ( AddrItemObject * ) item->addressItem;
1527                         if (!aio)
1528                                 continue;
1529                         if( aio->type == ITEMTYPE_EMAIL ) {
1530                                 ItemEMail *sitem = ( ItemEMail * ) aio;
1531                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(sitem);
1532                                 if (!g_list_find_custom(persons, person, (GCompareFunc)(find_person))) {
1533                                         emails = g_list_prepend(emails, item);
1534                                 }
1535                                 /* else, the email will be removed via the parent person */
1536                         }
1537                 }
1538         /* then delete groups */
1539                 node = groups;
1540                 while( node ) {
1541                         item = node->data;
1542                         node = g_list_next( node );
1543                         aio = ( AddrItemObject * ) item->addressItem;
1544                         if (!aio)
1545                                 continue;
1546                         if( aio->type == ITEMTYPE_GROUP ) {
1547                                 ItemGroup *item = ( ItemGroup * ) aio;
1548                                 GtkCMCTreeNode *nd = NULL;
1549                                 nd = addressbook_find_group_node( addrbook.opened, item );
1550                                 item = addrbook_remove_group( abf, item );
1551                                 if( item ) {
1552                                         addritem_free_item_group( item );
1553                                 }
1554                                 /* Remove group from parent node */
1555                                 gtk_cmctree_remove_node( ctree, nd );
1556                                 refreshList = TRUE;
1557                         }
1558                 }
1559         /* then delete persons */
1560                 node = persons;
1561                 while( node ) {
1562                         item = node->data;
1563                         node = g_list_next( node );
1564                         aio = ( AddrItemObject * ) item->addressItem;
1565                         if (!aio)
1566                                 continue;
1567                         if( aio->type == ITEMTYPE_PERSON ) {
1568                                 ItemPerson *item = ( ItemPerson * ) aio;
1569                                 item->status = DELETE_ENTRY; 
1570                                 addressbook_folder_remove_one_person( clist, item );
1571                                 if (pobj->type == ADDR_ITEM_FOLDER)
1572                                         addritem_folder_remove_person(ADAPTER_FOLDER(pobj)->itemFolder, item);
1573                                 item = addrbook_remove_person( abf, item );
1574 #ifdef USE_LDAP
1575                                 if (ds && ds->type == ADDR_IF_LDAP) {
1576                                         LdapServer *server = ds->rawDataSource;
1577                                         ldapsvr_set_modified(server, TRUE);
1578                                         ldapsvr_update_book(server, item);
1579                                 }
1580 #endif
1581                                 if( item ) {
1582                                         addritem_person_remove_picture(item);
1583                                         addritem_free_item_person( item );
1584                                 }
1585                         }
1586                 }
1587         /* then delete emails */
1588                 node = emails;
1589                 while( node ) {
1590                         item = node->data;
1591                         node = g_list_next( node );
1592                         aio = ( AddrItemObject * ) item->addressItem;
1593                         if (!aio)
1594                                 continue;
1595
1596                         if( aio->type == ITEMTYPE_EMAIL ) {
1597                                 ItemEMail *sitem = ( ItemEMail * ) aio;
1598                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(sitem);
1599                                 sitem = addrbook_person_remove_email( abf, person, sitem );
1600                                 if( sitem ) {
1601                                         addrcache_remove_email(abf->addressCache, sitem);
1602                                         addritem_free_item_email( sitem );
1603                                 }
1604                                 addressbook_folder_refresh_one_person( clist, person );
1605                         }
1606                 }
1607                 g_list_free( groups );
1608                 g_list_free( persons );
1609                 g_list_free( emails );
1610                 g_list_free( list );
1611                 addressbook_list_select_clear();
1612                 if( refreshList ) {
1613                         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1614                         addressbook_set_clist(
1615                                 gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree),
1616                                         addrbook.opened),
1617                                 TRUE);
1618                 }
1619                 addrbook_set_dirty(abf, TRUE);
1620                 addressbook_export_to_file();
1621                 addressbook_list_menu_setup();
1622                 goto thaw_ret;
1623         }
1624         else if( pobj->type == ADDR_ITEM_GROUP ) {
1625                 /* Items inside groups */
1626                 list = addrselect_get_list( _addressSelect_ );
1627                 node = list;
1628                 while( node ) {
1629                         item = node->data;
1630                         node = g_list_next( node );
1631                         aio = ( AddrItemObject * ) item->addressItem;
1632                         if( aio->type == ITEMTYPE_EMAIL ) {
1633                                 ItemEMail *item = ( ItemEMail * ) aio;
1634                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1635                                 item = addrbook_person_remove_email( abf, person, item );
1636                                 if( item ) {
1637                                         addritem_free_item_email( item );
1638                                 }
1639                         }
1640                 }
1641                 g_list_free( list );
1642                 addressbook_list_select_clear();
1643                 gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1644                 addressbook_set_clist(
1645                         gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree),
1646                                 addrbook.opened),
1647                         TRUE);
1648                 
1649                 addrbook_set_dirty(abf, TRUE);
1650                 addressbook_export_to_file();
1651                 addressbook_list_menu_setup();
1652                 goto thaw_ret;
1653         }
1654
1655         gtk_cmctree_node_set_row_data( clist, nodeList, NULL );
1656         gtk_cmctree_remove_node( clist, nodeList );
1657 thaw_ret:
1658         gtk_cmclist_thaw(GTK_CMCLIST(addrbook.clist));
1659         g_signal_handlers_unblock_by_func
1660                 (G_OBJECT(addrbook.clist),
1661                  G_CALLBACK(addressbook_list_row_unselected), NULL);
1662 }
1663
1664 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
1665 {
1666         addressbook_new_address_cb( NULL, NULL );
1667 }
1668
1669 static gchar *addressbook_format_address( AddrItemObject * aio ) {
1670         gchar *buf = NULL;
1671         gchar *name = NULL;
1672         gchar *address = NULL;
1673
1674         if( aio->type == ITEMTYPE_EMAIL ) {
1675                 ItemPerson *person = NULL;
1676                 ItemEMail *email = ( ItemEMail * ) aio;
1677
1678                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
1679                 if( email->address ) {
1680                         if( ADDRITEM_NAME(email) ) {
1681                                 name = ADDRITEM_NAME(email);
1682                                 if( *name == '\0' ) {
1683                                         name = ADDRITEM_NAME(person);
1684                                 }
1685                         }
1686                         else if( ADDRITEM_NAME(person) ) {
1687                                 name = ADDRITEM_NAME(person);
1688                         }
1689                         else {
1690                                 buf = g_strdup( email->address );
1691                         }
1692                         address = email->address;
1693                 }
1694         }
1695         else if( aio->type == ITEMTYPE_PERSON ) {
1696                 ItemPerson *person = ( ItemPerson * ) aio;
1697                 GList *node = person->listEMail;
1698
1699                 name = ADDRITEM_NAME(person);
1700                 if( node ) {
1701                         ItemEMail *email = ( ItemEMail * ) node->data;
1702                         address = email->address;
1703                 }
1704         }
1705         if( address ) {
1706                 if( name && name[0] != '\0' ) {
1707                         if( strchr_with_skip_quote( name, '"', ',' ) )
1708                                 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
1709                         else
1710                                 buf = g_strdup_printf( "%s <%s>", name, address );
1711                 }
1712                 else {
1713                         buf = g_strdup( address );
1714                 }
1715         }
1716
1717         return buf;
1718 }
1719
1720 static void addressbook_to_clicked(GtkButton *button, gpointer data)
1721 {
1722         GList *list, *node;
1723         Compose *compose;
1724         AddrSelectItem *item;
1725         AddrItemObject *aio;
1726         gchar *addr;
1727
1728         compose = addrbook.target_compose;
1729         if( ! compose ) return;
1730
1731         /* Nothing selected, but maybe there is something in text entry */
1732         addr = (char *)gtk_entry_get_text( GTK_ENTRY( addrbook.entry) );
1733         if ( addr ) {
1734                 compose_entry_append(
1735                         compose, addr, (ComposeEntryType)data , PREF_NONE);
1736         }
1737
1738         /* Select from address list */
1739         list = addrselect_get_list( _addressSelect_ );
1740         node = list;
1741         if (node) {
1742                 while( node ) {
1743                         item = node->data;
1744                         node = g_list_next( node );
1745                         aio = item->addressItem;
1746                         if( aio->type == ITEMTYPE_PERSON ||
1747                             aio->type == ITEMTYPE_EMAIL ) {
1748                                 addr = addressbook_format_address( aio );
1749                                 compose_entry_append(
1750                                         compose, addr, (ComposeEntryType) data, PREF_NONE );
1751                                 g_free( addr );
1752                         }
1753                         else if( aio->type == ITEMTYPE_GROUP ) {
1754                                 ItemGroup *group = ( ItemGroup * ) aio;
1755                                 GList *nodeMail = group->listEMail;
1756                                 while( nodeMail ) {
1757                                         ItemEMail *email = nodeMail->data;
1758
1759                                         addr = addressbook_format_address(
1760                                                         ( AddrItemObject * ) email );
1761                                         compose_entry_append(
1762                                                 compose, addr, (ComposeEntryType) data, PREF_NONE );
1763                                         g_free( addr );
1764                                         nodeMail = g_list_next( nodeMail );
1765                                 }
1766                         }
1767                 }
1768         } else {
1769                 AddressObject *obj = NULL;
1770
1771                 obj = gtk_cmctree_node_get_row_data( GTK_CMCTREE(addrbook.ctree), addrbook.treeSelected );
1772         
1773                 if( obj && obj->type == ADDR_ITEM_GROUP ) {
1774                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
1775                         GList *nodeMail = itemGroup->listEMail;
1776                         while( nodeMail ) {
1777                                 ItemEMail *email = nodeMail->data;
1778
1779                                 addr = addressbook_format_address(
1780                                                 ( AddrItemObject * ) email );
1781                                 compose_entry_append(
1782                                         compose, addr, (ComposeEntryType) data, PREF_NONE );
1783                                 g_free( addr );
1784                                 nodeMail = g_list_next( nodeMail );
1785                         }
1786                 }
1787         }
1788         g_list_free( list );
1789 }
1790
1791 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
1792         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/EditBook",   sensitive );
1793         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/DeleteBook", sensitive );
1794         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/NewFolder",  sensitive );
1795
1796         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/SelectAll",    TRUE );
1797         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Cut",    sensitive );
1798         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Copy",   sensitive );
1799         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Paste",  sensitive );
1800
1801         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/NewAddress", sensitive );
1802         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/NewGroup",   sensitive );
1803         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Mailto",     sensitive );
1804         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Merge",      sensitive );
1805         gtk_widget_set_sensitive( addrbook.edit_btn, sensitive );
1806         gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
1807 }
1808
1809 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCMCTreeNode *node ) {
1810         gboolean canEdit = FALSE;
1811         gboolean canDelete = TRUE;
1812         gboolean canAdd = FALSE;
1813         gboolean canEditTr = TRUE;
1814         gboolean editAddress = FALSE;
1815         gboolean canExport = TRUE;
1816         AddressTypeControlItem *atci = NULL;
1817         AddressDataSource *ds = NULL;
1818         AddressInterface *iface = NULL;
1819
1820         if( obj == NULL ) return;
1821         if( obj->type == ADDR_INTERFACE ) {
1822                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
1823                 iface = adapter->interface;
1824                 if( iface ) {
1825                         if( iface->haveLibrary ) {
1826                                 /* Enable appropriate File / New command */
1827                                 atci = adapter->atci;
1828                                 cm_menu_set_sensitive_full(addrbook.ui_manager, atci->menuCommand, TRUE );
1829                         }
1830                 }
1831                 canEditTr = canExport = FALSE;
1832         }
1833         else if( obj->type == ADDR_DATASOURCE ) {
1834                 AdapterDSource *ads = ADAPTER_DSOURCE(obj);
1835                 ds = ads->dataSource;
1836                 iface = ds->interface;
1837                 if( ! iface->readOnly ) {
1838                         canAdd = canEdit = editAddress = canDelete = TRUE;
1839                 }
1840                 if( ! iface->haveLibrary ) {
1841                         canAdd = canEdit = editAddress = canExport = canDelete = FALSE;
1842                 }
1843         }
1844         else if( obj->type == ADDR_ITEM_FOLDER ) {
1845                 ds = addressbook_find_datasource( addrbook.treeSelected );
1846                 if( ds ) {
1847                         iface = ds->interface;
1848                         if( iface->readOnly ) {
1849                                 canEditTr = FALSE;
1850                                 canDelete = FALSE;
1851                         }
1852                         else {
1853                                 canAdd = editAddress = TRUE;
1854                         }
1855                 }
1856         }
1857         else if( obj->type == ADDR_ITEM_GROUP ) {
1858                 ds = addressbook_find_datasource( addrbook.treeSelected );
1859                 if( ds ) {
1860                         iface = ds->interface;
1861                         if( ! iface->readOnly ) {
1862                                 editAddress = TRUE;
1863                         }
1864                 }
1865         }
1866
1867         if( addrbook.listSelected == NULL )
1868                 canEdit = FALSE;
1869
1870         /* Enable add */
1871         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/NewAddress", editAddress );
1872         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/NewGroup",   canAdd );
1873         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/NewFolder",  canAdd );
1874         gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
1875
1876         /* Enable edit */
1877         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Edit",   canEdit );
1878         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Delete", canDelete );
1879         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
1880         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1881
1882         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/EditBook",      canEditTr );
1883         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/DeleteBook",    canEditTr );
1884
1885         /* Export data */
1886         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Tools/ExportHTML", canExport );
1887         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Tools/ExportLDIF", canExport );
1888 }
1889
1890 /**
1891  * Address book tree callback function that responds to selection of tree
1892  * items.
1893  *
1894  * \param ctree  Tree widget.
1895  * \param node   Node that was selected.
1896  * \param column Column number where selected occurred.
1897  * \param data   Pointer to user data.
1898  */
1899 static void addressbook_tree_selected(GtkCMCTree *ctree, GtkCMCTreeNode *node,
1900                                       gint column, gpointer data)
1901 {
1902         AddressObject *obj = NULL;
1903         AdapterDSource *ads = NULL;
1904         AddressDataSource *ds = NULL;
1905         ItemFolder *rootFolder = NULL;
1906         AddressObjectType aot;
1907
1908         addrbook.treeSelected = node;
1909         addrbook.listSelected = NULL;
1910         addressbook_status_show( "" );
1911         if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
1912
1913         if( node ) obj = gtk_cmctree_node_get_row_data( ctree, node );
1914         if( obj == NULL ) {
1915                 addressbook_set_clist(NULL, TRUE);
1916                 return;
1917         }
1918         addrbook.opened = node;
1919
1920         if( obj->type == ADDR_DATASOURCE ) {
1921                 /* Read from file */
1922                 static gboolean tVal = TRUE;
1923
1924                 ads = ADAPTER_DSOURCE(obj);
1925
1926                 ds = ads->dataSource;
1927                 if( ds == NULL ) return;                
1928
1929                 if( addrindex_ds_get_modify_flag( ds ) ) {
1930                         addrindex_ds_read_data( ds );
1931                 }
1932
1933                 if( ! addrindex_ds_get_read_flag( ds ) ) {
1934                         addrindex_ds_read_data( ds );
1935                 }
1936                 addressbook_ds_show_message( ds );
1937
1938                 if( ! addrindex_ds_get_access_flag( ds ) ) {
1939                         /* Remove existing folders and groups */
1940                         gtk_cmclist_freeze( GTK_CMCLIST(ctree) );
1941                         addressbook_tree_remove_children( ctree, node );
1942                         gtk_cmclist_thaw( GTK_CMCLIST(ctree) );
1943
1944                         /* Load folders into the tree */
1945                         rootFolder = addrindex_ds_get_root_folder( ds );
1946                         if( ds && ds->type == ADDR_IF_JPILOT ) {
1947                                 aot = ADDR_CATEGORY;
1948                         }
1949                         else if( ds && ds->type == ADDR_IF_LDAP ) {
1950                                 aot = ADDR_LDAP_QUERY;
1951                         }
1952                         else {
1953                                 aot = ADDR_ITEM_FOLDER;
1954                         }
1955                         addressbook_node_add_folder( node, ds, rootFolder, aot );
1956                         addrindex_ds_set_access_flag( ds, &tVal );
1957                         gtk_cmctree_expand( ctree, node );
1958                 }
1959         } else {
1960                 addressbook_set_clist(NULL, TRUE);
1961         }
1962
1963         /* Update address list */
1964         g_signal_handlers_block_by_func
1965                 (G_OBJECT(ctree),
1966                  G_CALLBACK(addressbook_tree_selected), NULL);
1967         addressbook_set_clist( obj, FALSE );
1968         g_signal_handlers_unblock_by_func
1969                 (G_OBJECT(ctree),
1970                  G_CALLBACK(addressbook_tree_selected), NULL);
1971         if (!prefs_common.addressbook_use_editaddress_dialog)
1972                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
1973
1974         /* Setup main menu selections */
1975         addressbook_menubar_set_sensitive( FALSE );
1976         addressbook_menuitem_set_sensitive( obj, node );
1977         addressbook_list_select_clear();
1978         addressbook_list_menu_setup();
1979         return;
1980 }
1981
1982 /**
1983  * Setup address list popup menu items. Items are enabled or disabled as
1984  * required.
1985  */
1986 static void addressbook_list_menu_setup( void ) {
1987         GtkCMCTree *clist = NULL;
1988         AddressObject *pobj = NULL;
1989         AddressObject *obj = NULL;
1990         AdapterDSource *ads = NULL;
1991         AddressInterface *iface = NULL;
1992         AddressDataSource *ds = NULL;
1993         GList *list;
1994         AddrItemObject *aio;
1995         AddrSelectItem *item;
1996         gboolean canEdit = FALSE;
1997         gboolean canDelete = FALSE;
1998         gboolean canCut = FALSE;
1999         gboolean canCopy = FALSE;
2000         gboolean canPaste = FALSE;
2001         gboolean canBrowse = FALSE;
2002         gboolean canMerge = FALSE;
2003
2004         pobj = gtk_cmctree_node_get_row_data( GTK_CMCTREE(addrbook.ctree), addrbook.treeSelected );
2005         if( pobj == NULL ) return;
2006
2007         clist = GTK_CMCTREE(addrbook.clist);
2008         obj = gtk_cmctree_node_get_row_data( clist, addrbook.listSelected );
2009         if( obj == NULL ) canEdit = FALSE;
2010
2011         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
2012         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/SelectAll", TRUE );
2013
2014         if( pobj->type == ADDR_DATASOURCE ) {
2015                 /* Parent object is a data source */
2016                 ads = ADAPTER_DSOURCE(pobj);
2017                 ds = ads->dataSource;
2018                 if (!ds)
2019                         return;
2020                 iface = ds->interface;
2021                 if (!iface)
2022                         return;
2023                 if( ! iface->readOnly ) {
2024                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/NewAddress", TRUE );
2025                         if (iface->type != ADDR_IF_LDAP)
2026                                 cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/NewGroup", TRUE );
2027                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2028                         if( obj )
2029                                 canEdit = TRUE;
2030                         canDelete = canEdit;
2031                 }
2032         }
2033         else if( pobj->type != ADDR_INTERFACE ) {
2034                 /* Parent object is not an interface */
2035                 ds = addressbook_find_datasource( addrbook.treeSelected );
2036                 if (!ds)
2037                         return;
2038                 iface = ds->interface;
2039                 if (!iface)
2040                         return;
2041                 if( ! iface->readOnly ) {
2042                         /* Folder or group */
2043                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
2044                                 cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/NewAddress", TRUE );
2045                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2046                                 if( obj ) canEdit = TRUE;
2047                         }
2048                         /* Folder */
2049                         if( pobj->type == ADDR_ITEM_FOLDER ) {
2050                                 if (iface->type != ADDR_IF_LDAP)
2051                                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/NewGroup", TRUE );
2052                                 if( obj ) canEdit = TRUE;
2053                         }
2054                         canDelete = canEdit;
2055                 }
2056                 if( iface->type == ADDR_IF_LDAP ) {
2057                         if( obj ) canBrowse = TRUE;
2058                         canEdit = TRUE;
2059                         canDelete = TRUE;
2060                 }
2061         }
2062
2063         if( iface ) {
2064                 /* Enable cut and paste */
2065                 if( ! addrclip_is_empty( _clipBoard_ ) )
2066                         canPaste = TRUE;
2067                 if( ! addrselect_test_empty( _addressSelect_ ) )
2068                         canCut = TRUE;
2069                 /* Enable copy if something is selected */
2070                 if( ! addrselect_test_empty( _addressSelect_ ) )
2071                         canCopy = TRUE;
2072         }
2073
2074         /* Disable edit or browse if more than one row selected */
2075         if( GTK_CMCLIST(clist)->selection && GTK_CMCLIST(clist)->selection->next ) {
2076                 canEdit = FALSE;
2077                 canBrowse = FALSE;
2078         }
2079
2080         /* Allow merging persons or emails are selected */
2081         list = _addressSelect_->listSelect;
2082         if (list && list->next ) {
2083                 item = list->data;
2084                 aio = ( AddrItemObject * ) item->addressItem;
2085                 if( aio->type == ITEMTYPE_EMAIL ||
2086                                 aio->type == ITEMTYPE_PERSON ) {
2087                         canMerge = TRUE;
2088                 }
2089         }
2090
2091         /* Forbid write changes when read-only */
2092         if( iface && iface->readOnly ) {
2093                 canCut = FALSE;
2094                 canDelete = FALSE;
2095                 canPaste = FALSE;
2096                 canMerge = FALSE;
2097         }
2098
2099         /* Now go finalize menu items */
2100         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/Edit",   canEdit );
2101         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/Delete", canDelete );
2102
2103         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/Cut",           canCut );
2104         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/Copy",          canCopy );
2105         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/Paste",         canPaste );
2106
2107         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/Mailto",       canCopy );
2108         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/Merge",        canMerge );
2109
2110         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Cut",           canCut );
2111         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Copy",          canCopy );
2112         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Paste",         canPaste );
2113
2114         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Edit",    canEdit );
2115         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Delete",  canDelete );
2116         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Mailto", canCopy );
2117         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Merge",  canMerge );
2118
2119         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
2120         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
2121
2122         if (addrbook.target_compose) {
2123                 gtk_widget_set_sensitive(addrbook.to_btn, obj ? TRUE : FALSE);  
2124                 gtk_widget_set_sensitive(addrbook.cc_btn, obj ? TRUE : FALSE);
2125                 gtk_widget_set_sensitive(addrbook.bcc_btn, obj ? TRUE : FALSE);
2126         }
2127 #ifdef USE_LDAP
2128         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/BrowseEntry",    canBrowse );
2129 #endif
2130 }
2131
2132 static void addressbook_select_row_tree (GtkCMCTree     *ctree,
2133                                          GtkCMCTreeNode *node,
2134                                          gint            column,
2135                                          gpointer        data)
2136 {
2137 }
2138
2139 /**
2140  * Add list of items into tree node below specified tree node.
2141  * \param treeNode  Tree node.
2142  * \param ds        Data source.
2143  * \param listItems List of items.
2144  */
2145 static void addressbook_treenode_add_list(
2146         GtkCMCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
2147 {
2148         GList *node;
2149
2150         node = listItems;
2151         while( node ) {
2152                 AddrItemObject *aio;
2153                 GtkCMCTreeNode *nn;
2154
2155                 aio = node->data;
2156                 if( ADDRESS_OBJECT_TYPE(aio) == ADDR_ITEM_GROUP ) {
2157                         ItemGroup *group;
2158
2159                         group = ( ItemGroup * ) aio;
2160                         nn = addressbook_node_add_group( treeNode, ds, group );
2161                         if (nn == NULL) {
2162                                 g_message("error adding addressbook group\n");
2163                         }
2164                 }
2165                 else if( ADDRESS_OBJECT_TYPE(aio) == ADDR_ITEM_FOLDER ) {
2166                         ItemFolder *folder;
2167
2168                         folder = ( ItemFolder * ) aio;
2169                         nn = addressbook_node_add_folder(
2170                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
2171                         if (nn == NULL) {
2172                                 g_message("error adding addressbook folder\n");
2173                         }
2174                 }
2175                 node = g_list_next( node );
2176         }
2177 }
2178
2179 static void addressbook_select_all_cb( GtkAction *action, gpointer data ) {
2180         gtk_cmclist_select_all(GTK_CMCLIST(addrbook.clist));
2181 }
2182
2183 /**
2184  * Cut from address list widget.
2185  */
2186 static void addressbook_clip_cut_cb( GtkAction *action, gpointer data ) {
2187         _clipBoard_->cutFlag = TRUE;
2188         addrclip_clear( _clipBoard_ );
2189         addrclip_add( _clipBoard_, _addressSelect_ );
2190         /* addrclip_list_show( _clipBoard_, stdout ); */
2191 }
2192
2193 /**
2194  * Copy from address list widget.
2195  */
2196 static void addressbook_clip_copy_cb(GtkAction *action, gpointer data) {
2197         _clipBoard_->cutFlag = FALSE;
2198         addrclip_clear( _clipBoard_ );
2199         addrclip_add( _clipBoard_, _addressSelect_ );
2200         /* addrclip_list_show( _clipBoard_, stdout ); */
2201 }
2202
2203 /**
2204  * Paste clipboard into address list widget.
2205  */
2206 static void addressbook_clip_paste_cb( GtkAction *action, gpointer data ) {
2207         GtkCMCTree *ctree = GTK_CMCTREE( addrbook.ctree );
2208         AddressObject *pobj = NULL;
2209         AddressDataSource *ds = NULL;
2210         AddressBookFile *abf = NULL;
2211         ItemFolder *folder = NULL;
2212         GList *folderGroup = NULL;
2213
2214         ds = addressbook_find_datasource( GTK_CMCTREE_NODE(addrbook.treeSelected) );
2215         if( ds == NULL ) return;
2216         if( addrindex_ds_get_readonly( ds ) ) {
2217                 alertpanel_error( _("Cannot paste. Target address book is readonly.") );
2218                 return;
2219         }
2220
2221         pobj = gtk_cmctree_node_get_row_data( ctree, addrbook.treeSelected );
2222         if( pobj ) {
2223                 if( pobj->type == ADDR_ITEM_FOLDER ) {
2224                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
2225                 }
2226                 else if( pobj->type == ADDR_ITEM_GROUP ) {
2227                         alertpanel_error( _("Cannot paste into an address group.") );
2228                         return;
2229                 }
2230         }
2231
2232         /* Get an address book */
2233         abf = addressbook_get_book_file();
2234         if( abf == NULL ) return;
2235
2236         if( _clipBoard_->cutFlag ) {
2237                 /* Paste/Cut */
2238                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
2239
2240                 /* Remove all groups and folders in clipboard from tree node */
2241                 addressbook_treenode_remove_item();
2242
2243                 /* Remove all "cut" items */
2244                 addrclip_delete_item( _clipBoard_ );
2245
2246                 /* Clear clipboard - cut items??? */
2247                 addrclip_clear( _clipBoard_ );
2248         }
2249         else {
2250                 /* Paste/Copy */
2251                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
2252         }
2253
2254         /* addrclip_list_show( _clipBoard_, stdout ); */
2255         if( folderGroup ) {
2256                 /* Update tree by inserting node for each folder or group */
2257                 addressbook_treenode_add_list(
2258                         addrbook.treeSelected, ds, folderGroup );
2259                 gtk_cmctree_expand( ctree, addrbook.treeSelected );
2260                 g_list_free( folderGroup );
2261                 folderGroup = NULL;
2262         }
2263
2264         /* Display items pasted */
2265         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened );
2266         addressbook_set_clist(
2267                 gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree),
2268                         addrbook.opened),
2269                 TRUE);
2270         
2271
2272 }
2273
2274 /**
2275  * Add current treenode object to clipboard. Note that widget only allows
2276  * one entry from the tree list to be selected.
2277  */
2278 static void addressbook_treenode_to_clipboard( void ) {
2279         AddressObject *obj = NULL;
2280         AddressDataSource *ds = NULL;
2281         AddrSelectItem *item;
2282         GtkCMCTree *ctree = GTK_CMCTREE( addrbook.ctree );
2283         GtkCMCTreeNode *node;
2284
2285         node = addrbook.treeSelected;
2286         if( node == NULL ) return;
2287         obj = gtk_cmctree_node_get_row_data( ctree, node );
2288         if( obj == NULL ) return;
2289
2290         ds = addressbook_find_datasource( node );
2291         if( ds == NULL ) return;
2292
2293         item = NULL;
2294         if( obj->type == ADDR_ITEM_FOLDER ) {
2295                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2296                 ItemFolder *folder = adapter->itemFolder;
2297
2298                 item = addrselect_create_node( obj );
2299                 item->uid = g_strdup( ADDRITEM_ID(folder) );
2300         }
2301         else if( obj->type == ADDR_ITEM_GROUP ) {
2302                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2303                 ItemGroup *group = adapter->itemGroup;
2304
2305                 item = addrselect_create_node( obj );
2306                 item->uid = g_strdup( ADDRITEM_ID(group) );
2307         }
2308         else if( obj->type == ADDR_DATASOURCE ) {
2309                 /* Data source */
2310                 item = addrselect_create_node( obj );
2311                 item->uid = NULL;
2312         }
2313
2314         if( item ) {
2315                 /* Clear existing list and add item into list */
2316                 gchar *cacheID;
2317
2318                 addressbook_list_select_clear();
2319                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2320                 addrselect_list_add( _addressSelect_, item, cacheID );
2321                 g_free( cacheID );
2322         }
2323 }
2324
2325 /**
2326  * Cut from tree widget.
2327  */
2328 static void addressbook_treenode_cut_cb( GtkAction *action, gpointer data ) {
2329         _clipBoard_->cutFlag = TRUE;
2330         addressbook_treenode_to_clipboard();
2331         addrclip_clear( _clipBoard_ );
2332         addrclip_add( _clipBoard_, _addressSelect_ );
2333         /* addrclip_list_show( _clipBoard_, stdout ); */
2334 }
2335
2336 /**
2337  * Copy from tree widget.
2338  */
2339 static void addressbook_treenode_copy_cb( GtkAction *action, gpointer data ) {
2340         _clipBoard_->cutFlag = FALSE;
2341         addressbook_treenode_to_clipboard();
2342         addrclip_clear( _clipBoard_ );
2343         addrclip_add( _clipBoard_, _addressSelect_ );
2344         /* addrclip_list_show( _clipBoard_, stdout ); */
2345 }
2346
2347 /**
2348  * Paste clipboard into address tree widget.
2349  */
2350 static void addressbook_treenode_paste_cb( GtkAction *action, gpointer data ) {
2351         addressbook_clip_paste_cb(NULL,NULL);
2352 }
2353
2354 /**
2355  * Clear selected entries in clipboard.
2356  */
2357 static void addressbook_list_select_clear( void ) {
2358         addrselect_list_clear( _addressSelect_ );
2359 }
2360
2361 /**
2362  * Add specified address item to selected address list.
2363  * \param aio Address item object.
2364  * \param ds  Datasource.
2365  */
2366 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
2367         gchar *cacheID;
2368
2369         if( ds == NULL ) return;
2370         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2371         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
2372         g_free( cacheID );
2373 }
2374
2375 /**
2376  * Remove specified address item from selected address list.
2377  * \param aio Address item object.
2378  */
2379 static void addressbook_list_select_remove( AddrItemObject *aio ) {
2380         addrselect_list_remove( _addressSelect_, aio );
2381 }
2382
2383 /**
2384  * Invoke EMail compose window with addresses in selected address list.
2385  */
2386 static void addressbook_mail_to_cb( GtkAction *action, gpointer data ) {
2387         GList *listAddress;
2388
2389         if( ! addrselect_test_empty( _addressSelect_ ) ) {
2390                 listAddress = addrselect_build_list( _addressSelect_ );
2391                 compose_new_with_list( NULL, listAddress );
2392                 g_list_free_full( listAddress, g_free );
2393                 listAddress = NULL;
2394         }
2395 }
2396
2397 static void addressbook_merge_list( AddrSelectList *list ) {
2398         GtkCMCTree *ctree = GTK_CMCTREE(addrbook.ctree);
2399         GtkCMCTree *clist = GTK_CMCTREE(addrbook.clist);
2400         AddressObject *pobj;
2401         AddressDataSource *ds = NULL;
2402
2403         pobj = gtk_cmctree_node_get_row_data(ctree, addrbook.opened );
2404         cm_return_if_fail(pobj != NULL);
2405
2406         ds = addressbook_find_datasource( addrbook.treeSelected );
2407         if( ds == NULL ) return;
2408
2409         addrmerge_merge(clist, pobj, ds, list);
2410 }
2411
2412 /**
2413  * Merge selected entries in the address list
2414  */
2415 static void addressbook_merge_cb( GtkAction *action, gpointer data ) {
2416         if( addrselect_test_empty( _addressSelect_ ) )
2417                 return;
2418
2419         addressbook_merge_list( _addressSelect_ );
2420 }
2421
2422 static void addressbook_list_row_selected( GtkCMCTree *clist,
2423                                            GtkCMCTreeNode *node,
2424                                            gint column,
2425                                            gpointer data )
2426 {
2427         AddrItemObject *aio = NULL;
2428         AddressObject *pobj = NULL;
2429         AdapterDSource *ads = NULL;
2430         AddressDataSource *ds = NULL;
2431
2432         addrbook.listSelected = node;
2433
2434         pobj = gtk_cmctree_node_get_row_data( GTK_CMCTREE(addrbook.ctree), addrbook.treeSelected );
2435         if( pobj == NULL ) return;
2436
2437         if( pobj->type == ADDR_DATASOURCE ) {
2438                 ads = ADAPTER_DSOURCE(pobj);
2439                 ds = ads->dataSource;
2440         }
2441         else if( pobj->type != ADDR_INTERFACE ) {
2442                 ds = addressbook_find_datasource( addrbook.treeSelected );
2443         }
2444
2445         aio = gtk_cmctree_node_get_row_data( clist, node );
2446         if( aio ) {
2447                 /* g_print( "list select: %d : '%s'\n", aio->type, aio->name ); */
2448                 addressbook_list_select_add( aio, ds );
2449         }
2450
2451         addressbook_list_menu_setup();
2452
2453         if (!addrbook.target_compose && !prefs_common.addressbook_use_editaddress_dialog) {
2454                 AddressObject *obj = gtk_cmctree_node_get_row_data( clist, addrbook.listSelected );
2455
2456                 if (obj && obj->type != ADDR_ITEM_GROUP)
2457                         addressbook_edit_address(NULL, 0, NULL, FALSE);
2458         }
2459 }
2460
2461 static void addressbook_list_row_unselected( GtkCMCTree *ctree,
2462                                              GtkCMCTreeNode *node,
2463                                              gint column,
2464                                              gpointer data )
2465 {
2466         AddrItemObject *aio;
2467
2468         aio = gtk_cmctree_node_get_row_data( ctree, node );
2469         if( aio != NULL ) {
2470                 /* g_print( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
2471                 addressbook_list_select_remove( aio );
2472         }
2473
2474         if (!prefs_common.addressbook_use_editaddress_dialog)
2475                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
2476 }
2477
2478 static void addressbook_entry_activated(GtkWidget *widget, gpointer data)
2479 {
2480         addressbook_lup_clicked(NULL, NULL);
2481 }
2482
2483 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
2484                                                 GdkEventButton *event,
2485                                                 gpointer data)
2486 {
2487         if( ! event ) return FALSE;
2488         if( event->window != GTK_CMCLIST(widget)->clist_window ) return FALSE;
2489
2490         addressbook_list_menu_setup();
2491
2492         if( event->button == 3 ) {
2493                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
2494                        event->button, event->time );
2495         } else if (event->button == 1) {
2496                 if (event->type == GDK_2BUTTON_PRESS) {
2497                         if (prefs_common.add_address_by_click &&
2498                             addrbook.target_compose)
2499                                 addressbook_to_clicked(NULL, GINT_TO_POINTER(COMPOSE_TO));
2500                         else
2501                                 if (prefs_common.addressbook_use_editaddress_dialog)
2502                                         addressbook_edit_address_cb(NULL, NULL);
2503                                 else {
2504                                         GtkCMCTree *clist = GTK_CMCTREE(addrbook.clist);
2505                                         AddressObject *obj = gtk_cmctree_node_get_row_data( clist, addrbook.listSelected );
2506                                         if( obj && obj->type == ADDR_ITEM_GROUP )
2507                                                 addressbook_edit_address_cb(NULL, NULL);
2508                                 }
2509                 }
2510         }
2511
2512         return FALSE;
2513 }
2514
2515 static gboolean addressbook_list_button_released(GtkWidget *widget,
2516                                                  GdkEventButton *event,
2517                                                  gpointer data)
2518 {
2519         return FALSE;
2520 }
2521
2522 static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
2523                                                 GdkEventButton *event,
2524                                                 gpointer data)
2525 {
2526         GtkCMCList *clist = GTK_CMCLIST(ctree);
2527         gint row, column;
2528         AddressObject *obj = NULL;
2529         AdapterDSource *ads = NULL;
2530         AddressInterface *iface = NULL;
2531         AddressDataSource *ds = NULL;
2532         gboolean canEdit = FALSE;
2533         gboolean canDelete = FALSE;
2534         gboolean canCut = FALSE;
2535         gboolean canCopy = FALSE;
2536         gboolean canPaste = FALSE;
2537         gboolean canTreeCut = FALSE;
2538         gboolean canTreeCopy = FALSE;
2539         gboolean canTreePaste = FALSE;
2540         gboolean canLookup = FALSE;
2541         GtkCMCTreeNode *node = NULL;
2542         
2543         if( ! event ) return FALSE;
2544 /*      if( ! event || event->type != GDK_BUTTON_PRESS) return FALSE;*/
2545
2546         if( event->window != clist->clist_window )
2547                 return FALSE;
2548
2549         if (event->button == 1) {
2550                 if (event->type == GDK_2BUTTON_PRESS) {
2551                         if( gtk_cmclist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
2552                                 gtkut_clist_set_focus_row(clist, row);
2553                                 obj = gtk_cmclist_get_row_data( clist, row );
2554                         }
2555                         if( obj == NULL )
2556                                 return FALSE;
2557
2558                         if (obj->type == ADDR_ITEM_GROUP ||
2559                                         obj->type == ADDR_DATASOURCE) {
2560                                 /* edit group */
2561                                 addressbook_treenode_edit_cb(NULL, NULL);
2562                         } else {
2563                                 /* expand pr collapse */
2564                                 node = gtk_cmctree_node_nth(GTK_CMCTREE(ctree), row);
2565                                 gtk_cmctree_toggle_expansion(GTK_CMCTREE(ctree), node);
2566                         }
2567                         return FALSE;
2568                 }
2569         }
2570
2571         addressbook_menubar_set_sensitive( FALSE );
2572
2573         if( gtk_cmclist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
2574                 gtkut_clist_set_focus_row(clist, row);
2575                 obj = gtk_cmclist_get_row_data( clist, row );
2576         }
2577
2578         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
2579
2580         if( obj == NULL )
2581                 return FALSE;
2582         node = gtk_cmctree_node_nth(GTK_CMCTREE(clist), row);
2583
2584         if( ! addrclip_is_empty( _clipBoard_ ) )
2585                 canTreePaste = TRUE;
2586
2587         if (obj->type == ADDR_INTERFACE) {
2588                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
2589                 iface = adapter->interface;
2590                 if( !iface )
2591                         goto just_set_sens;
2592                 if( !iface->readOnly && iface->type == ADDR_IF_BOOK) {
2593                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/NewBook", TRUE );
2594                 }
2595                 if( iface->externalQuery )
2596                         canLookup = TRUE;
2597         }
2598         if (obj->type == ADDR_DATASOURCE) {
2599                 canLookup = TRUE;
2600                 ads = ADAPTER_DSOURCE(obj);
2601                 ds = ads->dataSource;
2602                 if( !ds )
2603                         goto just_set_sens;
2604                 iface = ds->interface;
2605                 if( !iface )
2606                         goto just_set_sens;
2607                 if( !iface->readOnly ) {
2608                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/NewFolder", TRUE );
2609                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/NewGroup", TRUE );
2610                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2611                 }
2612                 canDelete = TRUE;
2613                 canEdit = TRUE;
2614                 canTreeCopy = TRUE;
2615         }
2616         else if (obj->type == ADDR_ITEM_FOLDER) {
2617                 canLookup = TRUE;
2618                 ds = addressbook_find_datasource( node );
2619                 if( !ds )
2620                         goto just_set_sens;
2621                 iface = ds->interface;
2622                 if( !iface )
2623                         goto just_set_sens;
2624                 if( !iface->readOnly ) {
2625                         canEdit = TRUE;
2626                         canDelete = TRUE;
2627                         canTreeCut = TRUE;
2628                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/NewFolder", TRUE );
2629                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/NewGroup", TRUE );
2630                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2631                 }
2632                 canTreeCopy = TRUE;
2633
2634                 if( iface->externalQuery ) {
2635                         /* Enable deletion of LDAP folder */
2636                         canDelete = TRUE;
2637                 }
2638         }
2639         else if (obj->type == ADDR_ITEM_GROUP) {
2640                 canLookup = TRUE;
2641                 ds = addressbook_find_datasource( node );
2642                 if( !ds )
2643                         goto just_set_sens;
2644                 iface = ds->interface;
2645                 if( !iface )
2646                         goto just_set_sens;
2647                 if( ! iface->readOnly ) {
2648                         canEdit = TRUE;
2649                         canDelete = TRUE;
2650                         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABListPopup/NewAddress", TRUE );
2651                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2652                 }
2653         }
2654
2655         if( canEdit && !addrselect_test_empty( _addressSelect_ ) )
2656                 canCut = TRUE;
2657         if( ! addrselect_test_empty( _addressSelect_ ) )
2658                 canCopy = TRUE;
2659         if( ! addrclip_is_empty( _clipBoard_ ) )
2660                 canPaste = TRUE;
2661
2662         /* Forbid write changes when read-only */
2663         if( iface && iface->readOnly ) {
2664                 canTreeCut = FALSE;
2665                 canTreePaste = FALSE;
2666                 canCut = FALSE;
2667                 canPaste = FALSE;
2668         }
2669
2670 just_set_sens:
2671         /* Enable edit */
2672         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/EditBook",   canEdit );
2673         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/DeleteBook", canDelete );
2674         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/Cut",    canTreeCut );
2675         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/Copy",   canTreeCopy );
2676         cm_menu_set_sensitive_full( addrbook.ui_manager, "Popups/ABTreePopup/Paste",  canTreePaste );
2677
2678         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/EditBook",          canEdit );
2679         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Book/DeleteBook",        canDelete );
2680         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Cut",           canCut );
2681         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Copy",          canCopy );
2682         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Paste",         canPaste );
2683
2684         addressbook_show_buttons(addrbook.target_compose == NULL, canLookup,
2685                         addrbook.target_compose != NULL);
2686
2687         if( event->button == 3 )
2688                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
2689                                event->button, event->time);
2690
2691         return FALSE;
2692 }
2693
2694 static gboolean addressbook_tree_button_released(GtkWidget *ctree,
2695                                                  GdkEventButton *event,
2696                                                  gpointer data)
2697 {
2698         gtkut_ctree_set_focus_row(GTK_CMCTREE(addrbook.ctree), addrbook.opened);
2699         return FALSE;
2700 }
2701
2702 static void addressbook_new_folder_cb(GtkAction *action, gpointer data)
2703 {
2704         GtkCMCTree *ctree = GTK_CMCTREE(addrbook.ctree);
2705         AddressObject *obj = NULL;
2706         AddressDataSource *ds = NULL;
2707         AddressBookFile *abf = NULL;
2708         ItemFolder *parentFolder = NULL;
2709         ItemFolder *folder = NULL;
2710
2711         if( ! addrbook.treeSelected ) return;
2712         obj = gtk_cmctree_node_get_row_data( ctree, addrbook.treeSelected );
2713         if( obj == NULL ) return;
2714         ds = addressbook_find_datasource( addrbook.treeSelected );
2715         if( ds == NULL ) return;
2716
2717         if( obj->type == ADDR_DATASOURCE ) {
2718                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2719         }
2720         else if( obj->type == ADDR_ITEM_FOLDER ) {
2721                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2722         }
2723         else {
2724                 return;
2725         }
2726
2727         abf = ds->rawDataSource;
2728         if( abf == NULL ) return;
2729         folder = addressbook_edit_folder( abf, parentFolder, NULL );
2730         if( folder ) {
2731                 GtkCMCTreeNode *nn;
2732                 nn = addressbook_node_add_folder(
2733                         addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
2734                 if (nn == NULL) {
2735                         g_message("error adding addressbook folder\n");
2736                 }
2737                 gtk_cmctree_expand( ctree, addrbook.treeSelected );
2738                 if( addrbook.treeSelected == addrbook.opened )
2739                         addressbook_set_clist(obj, TRUE);
2740         }
2741 }
2742
2743 static void addressbook_new_group_cb(GtkAction *action, gpointer data)
2744 {
2745         GtkCMCTree *ctree = GTK_CMCTREE(addrbook.ctree);
2746         AddressObject *obj = NULL;
2747         AddressDataSource *ds = NULL;
2748         AddressBookFile *abf = NULL;
2749         ItemFolder *parentFolder = NULL;
2750         ItemGroup *group = NULL;
2751
2752         if( ! addrbook.treeSelected ) return;
2753         obj = gtk_cmctree_node_get_row_data(ctree, addrbook.treeSelected);
2754         if( obj == NULL ) return;
2755         ds = addressbook_find_datasource( addrbook.treeSelected );
2756         if( ds == NULL ) return;
2757
2758         if( obj->type == ADDR_DATASOURCE ) {
2759                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2760         }
2761         else if( obj->type == ADDR_ITEM_FOLDER ) {
2762                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2763         }
2764         else {
2765                 return;
2766         }
2767
2768         abf = ds->rawDataSource;
2769         if( abf == NULL ) return;
2770         group = addressbook_edit_group( abf, parentFolder, NULL );
2771         if( group ) {
2772                 GtkCMCTreeNode *nn;
2773                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
2774                 if (nn == NULL) {
2775                         g_message("error adding addressbook group\n");
2776                 }
2777                 gtk_cmctree_expand( ctree, addrbook.treeSelected );
2778                 if( addrbook.treeSelected == addrbook.opened )
2779                         addressbook_set_clist(obj, TRUE);
2780         }
2781 }
2782
2783 static void addressbook_change_node_name(GtkCMCTreeNode *node, const gchar *name)
2784 {
2785         GtkCMCTree *ctree = GTK_CMCTREE(addrbook.ctree);
2786         gchar *text[1];
2787         guint8 spacing;
2788         GdkPixbuf *pix_cl, *pix_op;
2789         gboolean is_leaf, expanded;
2790
2791         gtk_cmctree_get_node_info(ctree, node, text, &spacing,
2792                                 &pix_cl, &pix_op,
2793                                 &is_leaf, &expanded);
2794         gtk_cmctree_set_node_info(ctree, node, name, spacing,
2795                                 pix_cl, pix_op,
2796                                 is_leaf, expanded);
2797 }
2798
2799 /**
2800  * Edit data source.
2801  * \param obj  Address object to edit.
2802  * \param node Node in tree.
2803  * \return New name of data source.
2804  */
2805 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCMCTreeNode *node ) {
2806         gchar *newName = NULL;
2807         AddressDataSource *ds = NULL;
2808         AddressInterface *iface = NULL;
2809         AdapterDSource *ads = NULL;
2810
2811         ds = addressbook_find_datasource( node );
2812         if( ds == NULL ) return NULL;
2813         iface = ds->interface;
2814         if( ! iface->haveLibrary ) return NULL;
2815
2816         /* Read data from data source */
2817         if( addrindex_ds_get_modify_flag( ds ) ) {
2818                 addrindex_ds_read_data( ds );
2819         }
2820
2821         if( ! addrindex_ds_get_read_flag( ds ) ) {
2822                 addrindex_ds_read_data( ds );
2823         }
2824
2825         /* Handle edit */
2826         ads = ADAPTER_DSOURCE(obj);
2827         if( ads->subType == ADDR_BOOK ) {
2828                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
2829         }
2830         else if( ads->subType == ADDR_VCARD ) {
2831                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
2832         }
2833 #ifdef USE_JPILOT
2834         else if( ads->subType == ADDR_JPILOT ) {
2835                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
2836         }
2837 #endif
2838 #ifdef USE_LDAP
2839         else if( ads->subType == ADDR_LDAP ) {
2840                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
2841         }
2842 #endif
2843         else {
2844                 return NULL;
2845         }
2846         newName = obj->name;
2847         return newName;
2848 }
2849
2850 /*
2851 * Edit an object that is in the address tree area.
2852 */
2853 static void addressbook_treenode_edit_cb(GtkAction *action, gpointer data)
2854 {
2855         GtkCMCTree *ctree = GTK_CMCTREE(addrbook.ctree);
2856         AddressObject *obj;
2857         AddressDataSource *ds = NULL;
2858         AddressBookFile *abf = NULL;
2859         GtkCMCTreeNode *node = NULL, *parentNode = NULL;
2860         gchar *name = NULL;
2861
2862         if( ! addrbook.treeSelected ) return;
2863         node = addrbook.treeSelected;
2864         if( GTK_CMCTREE_ROW(node)->level == 1 ) return;
2865         obj = gtk_cmctree_node_get_row_data( ctree, node );
2866         if( obj == NULL ) return;
2867         parentNode = GTK_CMCTREE_ROW(node)->parent;
2868
2869         ds = addressbook_find_datasource( node );
2870         if( ds == NULL ) return;
2871
2872         if( obj->type == ADDR_DATASOURCE ) {
2873                 name = addressbook_edit_datasource( obj, node );
2874                 if( name == NULL ) return;
2875         }
2876         else {
2877                 abf = ds->rawDataSource;
2878                 if( abf == NULL ) return;
2879                 if( obj->type == ADDR_ITEM_FOLDER ) {
2880                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2881                         ItemFolder *item = adapter->itemFolder;
2882                         ItemFolder *parentFolder = NULL;
2883                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2884                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2885                         name = ADDRITEM_NAME(item);
2886                 }
2887                 else if( obj->type == ADDR_ITEM_GROUP ) {
2888                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2889                         ItemGroup *item = adapter->itemGroup;
2890                         ItemFolder *parentFolder = NULL;
2891                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2892                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2893                         name = ADDRITEM_NAME(item);
2894                 }
2895         }
2896         if( name && parentNode ) {
2897                 /* Update node in tree view */
2898                 addressbook_change_node_name( node, name );
2899                 gtk_sctree_sort_node(ctree, parentNode);
2900                 gtk_cmctree_expand( ctree, node );
2901                 gtk_sctree_select( GTK_SCTREE( ctree), node );
2902         }
2903 }
2904
2905 typedef enum {
2906         ADDRTREE_DEL_NONE,
2907         ADDRTREE_DEL_DATA,
2908         ADDRTREE_DEL_FOLDER_ONLY,
2909         ADDRTREE_DEL_FOLDER_ADDR
2910 } TreeItemDelType ;
2911
2912 /**
2913  * Delete an item from the tree widget.
2914  * \param data   Data passed in.
2915  * \param action Action.
2916  * \param widget Widget issuing callback.
2917  */
2918 static void addressbook_treenode_delete_cb(GtkAction *action, gpointer data)
2919 {
2920         GtkCMCTree *ctree = GTK_CMCTREE(addrbook.ctree);
2921         GtkCMCTreeNode *node = NULL;
2922         AddressObject *obj;
2923         gchar *message;
2924         AlertValue aval;
2925         AddrBookBase *adbase;
2926         AddressCache *cache;
2927         AdapterDSource *ads = NULL;
2928         AddressInterface *iface = NULL;
2929         AddressDataSource *ds = NULL;
2930         gboolean remFlag = FALSE;
2931         TreeItemDelType delType;
2932
2933         if( ! addrbook.treeSelected ) return;
2934         node = addrbook.treeSelected;
2935         if( GTK_CMCTREE_ROW(node)->level == 1 ) return;
2936
2937         obj = gtk_cmctree_node_get_row_data( ctree, node );
2938         cm_return_if_fail(obj != NULL);
2939
2940         if( obj->type == ADDR_DATASOURCE ) {
2941                 ads = ADAPTER_DSOURCE(obj);
2942
2943                 ds = ads->dataSource;
2944                 if( ds == NULL ) return;
2945         }
2946         else {
2947                 /* Must be folder or something else */
2948                 ds = addressbook_find_datasource( node );
2949                 if( ds == NULL ) return;
2950
2951                 /* Only allow deletion from non-readOnly */
2952                 iface = ds->interface;
2953                 if( iface->readOnly ) {
2954                         /* Allow deletion of query results */
2955                         if( ! iface->externalQuery ) return;
2956                 }
2957         }
2958
2959         /* Confirm deletion */
2960         delType = ADDRTREE_DEL_NONE;
2961         if( obj->type == ADDR_ITEM_FOLDER ) {
2962                 if( iface && iface->externalQuery ) {
2963                         message = g_strdup_printf( _(
2964                                 "Do you want to delete the query " \
2965                                 "results and addresses in '%s'?" ),
2966                                 obj->name );
2967                         aval = alertpanel( _("Delete"), message,
2968                                 GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND );
2969                         g_free(message);
2970                         if( aval == G_ALERTALTERNATE ) {
2971                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2972                         }
2973                 }
2974                 else {
2975                         message = g_strdup_printf
2976                                 ( _( "Do you want to delete '%s'? "
2977                                      "If you delete the folder only, the addresses it contains will be moved into the parent folder." ),
2978                                  obj->name );
2979                         aval = alertpanel( _("Delete folder"), message,
2980                                 GTK_STOCK_CANCEL, _("Delete _folder only"), _("Delete folder and _addresses"), ALERTFOCUS_SECOND);
2981                         g_free(message);
2982                         if( aval == G_ALERTALTERNATE ) {
2983                                 delType = ADDRTREE_DEL_FOLDER_ONLY;
2984                         }
2985                         else if( aval == G_ALERTOTHER ) {
2986                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2987                         }
2988                 }
2989         }
2990         else if( obj->type == ADDR_ITEM_GROUP ) {
2991                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2992                                             "The addresses it contains will not be lost."), obj->name);
2993                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2994                                 GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND);
2995                 g_free(message);
2996                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_FOLDER_ONLY;
2997         } else {
2998                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2999                                             "The addresses it contains will be lost."), obj->name);
3000                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
3001                                 GTK_STOCK_DELETE, NULL, ALERTFOCUS_SECOND);
3002                 g_free(message);
3003                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_DATA;
3004         }
3005         if( delType == ADDRTREE_DEL_NONE ) return;
3006
3007         /* Proceed with deletion */
3008         if( obj->type == ADDR_DATASOURCE ) {
3009                 /* Remove node from tree */
3010                 gtk_cmctree_remove_node( ctree, node );
3011                 
3012                 if (delType == ADDRTREE_DEL_DATA &&
3013                     ds->interface && ds->interface->type == ADDR_IF_BOOK)
3014                         addrbook_delete_book_file((AddressBookFile *) ds->rawDataSource);
3015                         
3016                 /* Remove data source. */
3017                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
3018                         addrindex_free_datasource( ds );
3019                 }
3020                 return;
3021         }
3022
3023         /* Get reference to cache */
3024         adbase = ( AddrBookBase * ) ds->rawDataSource;
3025         if( adbase == NULL ) return;
3026         cache = adbase->addressCache;
3027
3028         /* Remove query results folder */
3029         if( iface && iface->externalQuery ) {
3030                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
3031                 ItemFolder *folder = adapter->itemFolder;
3032
3033                 adapter->itemFolder = NULL;
3034                 /*
3035                 g_print( "remove folder for ::%s::\n", obj->name );
3036                 g_print( "      folder name ::%s::\n", ADDRITEM_NAME(folder) );
3037                 g_print( "-------------- remove results\n" );
3038                 */
3039                 addrindex_remove_results( ds, folder );
3040                 /* g_print( "-------------- remove node\n" ); */
3041                 gtk_cmctree_remove_node( ctree, node );
3042                 return;
3043         }
3044
3045         /* Code below is valid for regular address book deletion */
3046         if( obj->type == ADDR_ITEM_FOLDER ) {
3047                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
3048                 ItemFolder *item = adapter->itemFolder;
3049
3050                 if( delType == ADDRTREE_DEL_FOLDER_ONLY ) {
3051                         /* Remove folder only */
3052                         item = addrcache_remove_folder( cache, item );
3053                         if( item ) {
3054                                 addritem_free_item_folder( item );
3055                                 addressbook_move_nodes_up( ctree, node );
3056                                 remFlag = TRUE;
3057                         }
3058                 }
3059                 else if( delType == ADDRTREE_DEL_FOLDER_ADDR ) {
3060                         /* Remove folder and addresses */
3061                         item = addrcache_remove_folder_delete( cache, item );
3062                         if( item ) {
3063                                 addritem_free_item_folder( item );
3064                                 remFlag = TRUE;
3065                         }
3066                 }
3067         }
3068         else if( obj->type == ADDR_ITEM_GROUP ) {
3069                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
3070                 ItemGroup *item = adapter->itemGroup;
3071
3072                 item = addrcache_remove_group( cache, item );
3073                 if( item ) {
3074                         addritem_free_item_group( item );
3075                         remFlag = TRUE;
3076                 }
3077         }
3078
3079         if( remFlag ) {
3080                 /* Remove node. */
3081                 gtk_cmctree_remove_node(ctree, node );
3082         }
3083 }
3084
3085 static void addressbook_new_address_from_book_post_cb( ItemPerson *person )
3086 {
3087         if( person && addrbook.treeSelected == addrbook.opened ) {
3088                 person->status = ADD_ENTRY;
3089                 gtk_cmclist_unselect_all( GTK_CMCLIST(addrbook.clist) );
3090                 addressbook_folder_refresh_one_person(
3091                         GTK_CMCTREE(addrbook.clist), person );
3092         }
3093         addressbook_address_list_set_focus();
3094 }
3095
3096 static void addressbook_new_address_from_folder_post_cb( ItemPerson *person )
3097 {
3098         if( person && addrbook.treeSelected == addrbook.opened) {
3099                 person->status = ADD_ENTRY;
3100                 gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
3101                 addressbook_set_clist(
3102                         gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree),
3103                                 addrbook.opened),
3104                         TRUE);
3105         }
3106         addressbook_address_list_set_focus();
3107 }
3108
3109 /**
3110  * Label (a format string) that is used to name each folder.
3111  */
3112 static gchar *_queryFolderLabel_ = N_( "Search '%s'" );
3113
3114 /**
3115  * Search ctree widget callback function.
3116  * \param  pA Pointer to node.
3117  * \param  pB Pointer to data item being sought.
3118  * \return Zero (0) if folder found.
3119  */
3120 static int addressbook_treenode_find_folder_cb( gconstpointer pA, gconstpointer pB ) {
3121         AddressObject *aoA;
3122
3123         aoA = ( AddressObject * ) pA;
3124         if( aoA->type == ADDR_ITEM_FOLDER ) {
3125                 ItemFolder *folder, *fld;
3126
3127                 fld = ADAPTER_FOLDER(aoA)->itemFolder;
3128                 folder = ( ItemFolder * ) pB;
3129                 if( fld == folder ) return 0;   /* Found folder */
3130         }
3131         return 1;
3132 }
3133
3134 static ItemFolder * addressbook_setup_subf(
3135                 AddressDataSource *ds, gchar *title,
3136                 GtkCMCTreeNode *pNode )
3137 {
3138         AddrBookBase *adbase;
3139         AddressCache *cache;
3140         ItemFolder *folder;
3141         GtkCMCTree *ctree;
3142         GtkCMCTreeNode *nNode;
3143         gchar *name;
3144         AddressObjectType aoType = ADDR_NONE;
3145         GList *children;
3146         /* Setup a query */
3147         if( *title == '\0' || strlen( title ) < 1 ) return NULL;
3148
3149         if( ds && ds->type == ADDR_IF_LDAP ) {
3150 #if USE_LDAP
3151                 aoType = ADDR_LDAP_QUERY;
3152 #endif
3153         }
3154         else {
3155                 return NULL;
3156         }
3157
3158         ctree = GTK_CMCTREE(addrbook.ctree);
3159         /* Get reference to address cache */    
3160         adbase = ( AddrBookBase * ) ds->rawDataSource;
3161         cache = adbase->addressCache;
3162         
3163         if ((children = addrcache_get_list_folder(cache)) != NULL) {
3164                 GList *cur = children;
3165                 for (; cur; cur = cur->next) {
3166                         ItemFolder *child = (ItemFolder *) cur->data;
3167                         if (!g_strcmp0(ADDRITEM_NAME(child), title)) {
3168                                 nNode = gtk_cmctree_find_by_row_data_custom(
3169                                         ctree, NULL, child,
3170                                         addressbook_treenode_find_folder_cb );
3171                                 if( nNode ) {
3172                                         addrindex_remove_results( ds, child );
3173                                         while( child->listPerson ) {
3174                                                 ItemPerson *item = ( ItemPerson * ) child->listPerson->data;
3175                                                 item = addrcache_remove_person( cache, item );
3176                                                 if( item ) {
3177                                                         addritem_free_item_person( item );
3178                                                         item = NULL;
3179                                                 }
3180                                         }
3181                                         gtk_sctree_select( GTK_SCTREE(ctree), nNode );
3182                                         addrbook.treeSelected = nNode;
3183                                 }       
3184                                 return child;
3185                         }
3186                 }
3187         }
3188         
3189         /* Create a folder */
3190         folder = addrcache_add_new_folder( cache, NULL );
3191         name = g_strdup_printf( "%s", title );
3192         addritem_folder_set_name( folder, name );
3193         addritem_folder_set_remarks( folder, "" );
3194         g_free( name );
3195
3196         /* Now let's see the folder */
3197         nNode = addressbook_node_add_folder( pNode, ds, folder, aoType );
3198         gtk_cmctree_expand( ctree, pNode );
3199         if( nNode ) {
3200                 gtk_sctree_select( GTK_SCTREE(ctree), nNode );
3201                 addrbook.treeSelected = nNode;
3202                 return folder;
3203         }
3204         return NULL;
3205 }
3206
3207 static void addressbook_new_address_cb( GtkAction *action, gpointer data ) {
3208         AddressObject *pobj = NULL;
3209         AddressDataSource *ds = NULL;
3210         AddressBookFile *abf = NULL;
3211         debug_print("adding address\n");
3212         pobj = gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree), addrbook.treeSelected);
3213         if( pobj == NULL ) {
3214                 debug_print("no row data\n");
3215                 return;
3216         }
3217         ds = addressbook_find_datasource( GTK_CMCTREE_NODE(addrbook.treeSelected) );
3218         if( ds == NULL ) {
3219                 debug_print("no datasource\n");
3220                 return;
3221         }
3222
3223         abf = ds->rawDataSource;
3224         if( abf == NULL ) {
3225                 g_print("no addressbook file\n");
3226                 return;
3227         }
3228
3229         if( pobj->type == ADDR_DATASOURCE ) {
3230                 if (ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ||
3231                     ADAPTER_DSOURCE(pobj)->subType == ADDR_LDAP) {
3232                         ItemPerson *person;
3233                         ItemFolder *folder = NULL;
3234 #ifdef USE_LDAP
3235                         if (abf && abf->type == ADBOOKTYPE_LDAP) {
3236                                 GtkCMCTreeNode *parentNode;
3237                                 ds = addressbook_find_datasource( GTK_CMCTREE_NODE( addrbook.treeSelected ) );
3238                                 if( ds == NULL ) return;
3239
3240                                 /* We must have a datasource that is an external interface */
3241                                 if( ! ds->interface->haveLibrary ) return;
3242                                 if( ! ds->interface->externalQuery ) return;
3243
3244                                 if( pobj->type == ADDR_ITEM_FOLDER ) {
3245                                         parentNode = GTK_CMCTREE_ROW(GTK_CMCTREE_NODE( addrbook.treeSelected ) )->parent;
3246                                 }
3247                                 else {
3248                                         parentNode = GTK_CMCTREE_NODE( addrbook.treeSelected );
3249                                 }
3250                                 folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3251
3252                                 ds = addressbook_find_datasource( GTK_CMCTREE_NODE(addrbook.treeSelected) );
3253                                 if (ds)
3254                                         abf = ds->rawDataSource;
3255                         }
3256 #endif
3257                         person = addressbook_edit_person( abf, folder, NULL, FALSE,
3258                                                                   addrbook.editaddress_vbox,
3259                                                                   addressbook_new_address_from_book_post_cb,
3260                                                                   TRUE );
3261 #ifdef USE_LDAP
3262                         if (ds && abf && abf->type == ADBOOKTYPE_LDAP) {
3263                                 LdapServer *server = ds->rawDataSource;
3264                                 ldapsvr_set_modified(server, TRUE);
3265                                 ldapsvr_update_book(server, NULL);
3266                                 if (server->retVal != LDAPRC_SUCCESS) {
3267                                         alertpanel( _("Add address(es)"),
3268                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3269                                                 GTK_STOCK_CLOSE, NULL, NULL, ALERTFOCUS_FIRST );
3270                                         server->retVal = LDAPRC_SUCCESS;
3271                                         return;
3272                                 }
3273                         }
3274 #endif
3275                         if (prefs_common.addressbook_use_editaddress_dialog)
3276                                 addressbook_new_address_from_book_post_cb( person );
3277                 }
3278         }
3279         else if( pobj->type == ADDR_ITEM_FOLDER ) {
3280                 /* New address */
3281                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
3282                 ItemPerson *person;
3283 #ifdef USE_LDAP
3284                 if (abf && abf->type == ADBOOKTYPE_LDAP) {
3285                         GtkCMCTreeNode *parentNode;
3286                         ds = addressbook_find_datasource( GTK_CMCTREE_NODE( addrbook.treeSelected ) );
3287                         if( ds == NULL ) return;
3288
3289                         /* We must have a datasource that is an external interface */
3290                         if( ! ds->interface->haveLibrary ) return;
3291                         if( ! ds->interface->externalQuery ) return;
3292
3293                         if( pobj->type == ADDR_ITEM_FOLDER ) {
3294                                 parentNode = GTK_CMCTREE_ROW(GTK_CMCTREE_NODE( addrbook.treeSelected ) )->parent;
3295                         }
3296                         else {
3297                                 parentNode = GTK_CMCTREE_NODE( addrbook.treeSelected );
3298                         }
3299                         folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3300                         if (!folder)
3301                                 return;
3302
3303                         ds = addressbook_find_datasource( GTK_CMCTREE_NODE(addrbook.treeSelected) );
3304                         if (ds)
3305                                 abf = ds->rawDataSource;
3306                 }
3307 #endif
3308                 person = addressbook_edit_person( abf, folder, NULL, FALSE,
3309                                                           addrbook.editaddress_vbox,
3310                                                           addressbook_new_address_from_folder_post_cb,
3311                                                           TRUE );
3312 #ifdef USE_LDAP
3313                 if (ds && abf && abf->type == ADBOOKTYPE_LDAP) {
3314                         LdapServer *server = ds->rawDataSource;
3315                         ldapsvr_set_modified(server, TRUE);
3316                         ldapsvr_update_book(server, NULL);
3317                         if (server->retVal != LDAPRC_SUCCESS) {
3318                                 alertpanel( _("Add address(es)"),
3319                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3320                                         GTK_STOCK_CLOSE, NULL, NULL, ALERTFOCUS_FIRST );
3321                                 return;
3322                         }
3323                 }
3324 #endif
3325                 if (prefs_common.addressbook_use_editaddress_dialog)
3326                         addressbook_new_address_from_folder_post_cb( person );
3327         }
3328         else if( pobj->type == ADDR_ITEM_GROUP ) {
3329                 /* New address in group */
3330                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
3331                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
3332                 if (addrbook.treeSelected == addrbook.opened) {
3333                         /* Change node name in tree. */
3334                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
3335                         gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
3336                         addressbook_set_clist(
3337                                 gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree),
3338                                         addrbook.opened),
3339                                 TRUE);
3340                 }
3341         }
3342 }
3343
3344 /**
3345  * Search for specified child group node in address index tree.
3346  * \param parent Parent node.
3347  * \param group  Group to find.
3348  */
3349 static GtkCMCTreeNode *addressbook_find_group_node( GtkCMCTreeNode *parent, ItemGroup *group ) {
3350         GtkCMCTreeNode *node = NULL;
3351         GtkCMCTreeRow *currRow;
3352
3353         currRow = GTK_CMCTREE_ROW( parent );
3354         if( currRow ) {
3355                 node = currRow->children;
3356                 while( node ) {
3357                         AddressObject *obj;
3358
3359                         obj = gtk_cmctree_node_get_row_data( GTK_CMCTREE(addrbook.ctree), node );
3360                         if(obj && obj->type == ADDR_ITEM_GROUP ) {
3361                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
3362                                 if( g == group ) return node;
3363                         }
3364                         currRow = GTK_CMCTREE_ROW(node);
3365                         node = currRow->sibling;
3366                 }
3367         }
3368         return NULL;
3369 }
3370
3371 static AddressBookFile *addressbook_get_book_file() {
3372         AddressBookFile *abf = NULL;
3373         AddressDataSource *ds = NULL;
3374
3375         ds = addressbook_find_datasource( addrbook.treeSelected );
3376         if( ds == NULL ) return NULL;
3377         if( ds->type == ADDR_IF_BOOK || ds->type == ADDR_IF_LDAP ) abf = ds->rawDataSource;
3378         return abf;
3379 }
3380
3381 static void addressbook_tree_remove_children( GtkCMCTree *ctree, GtkCMCTreeNode *parent ) {
3382         GtkCMCTreeNode *node;
3383         GtkCMCTreeRow *row;
3384
3385         /* Remove existing folders and groups */
3386         row = GTK_CMCTREE_ROW( parent );
3387         if( row ) {
3388                 while( (node = row->children) ) {
3389                         gtk_cmctree_remove_node( ctree, node );
3390                 }
3391         }
3392 }
3393
3394 static void addressbook_move_nodes_up( GtkCMCTree *ctree, GtkCMCTreeNode *node ) {
3395         GtkCMCTreeNode *parent, *child;
3396         GtkCMCTreeRow *currRow;
3397         currRow = GTK_CMCTREE_ROW( node );
3398         if( currRow ) {
3399                 parent = currRow->parent;
3400                 while( (child = currRow->children) ) {
3401                         gtk_cmctree_move( ctree, child, parent, node );
3402                 }
3403                 gtk_sctree_sort_node( ctree, parent );
3404         }
3405 }
3406
3407 static void addressbook_edit_address_post_cb( ItemPerson *person )
3408 {
3409         if( person ) {
3410 #ifdef USE_LDAP
3411                 AddressBookFile *abf = addressbook_get_book_file();
3412
3413                 if (abf && abf->type == ADBOOKTYPE_LDAP) {
3414                         if (g_strcmp0(person->nickName, ADDRITEM_NAME(person)))
3415                                 addritem_person_set_nick_name( person, ADDRITEM_NAME(person));
3416                 }
3417 #endif
3418                 addressbook_folder_refresh_one_person( GTK_CMCTREE(addrbook.clist), person );
3419                 invalidate_address_completion();
3420         }
3421         addressbook_address_list_set_focus();
3422 }
3423
3424 void addressbook_address_list_set_focus( void )
3425 {
3426         if (!prefs_common.addressbook_use_editaddress_dialog) {
3427                 gtk_window_set_focus(GTK_WINDOW(addrbook.window), addrbook.clist);
3428                 addressbook_list_menu_setup();
3429         }
3430 }
3431
3432 void addressbook_address_list_disable_some_actions(void)
3433 {
3434         /* disable address copy/pasting when editing contact's detail (embedded form) */
3435         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Cut",   FALSE );
3436         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Copy",  FALSE );
3437         cm_menu_set_sensitive_full( addrbook.ui_manager, "Menu/Edit/Paste", FALSE );
3438 }
3439
3440 static void addressbook_edit_address_cb( GtkAction *action, gpointer data ) {
3441         addressbook_edit_address(data, 0, NULL, TRUE);
3442 }
3443         
3444 static void addressbook_edit_address( gpointer data, guint action, GtkWidget *widget,
3445                                                                           gboolean force_focus ) {
3446         GtkCMCTree *clist = GTK_CMCTREE(addrbook.clist);
3447         GtkCMCTree *ctree;
3448         AddressObject *obj = NULL, *pobj = NULL;
3449         AddressDataSource *ds = NULL;
3450         GtkCMCTreeNode *node = NULL, *parentNode = NULL;
3451         gchar *name = NULL;
3452         AddressBookFile *abf = NULL;
3453
3454         if( addrbook.listSelected == NULL ) return;
3455         obj = gtk_cmctree_node_get_row_data( clist, addrbook.listSelected );
3456         cm_return_if_fail(obj != NULL);
3457
3458         ctree = GTK_CMCTREE( addrbook.ctree );
3459         pobj = gtk_cmctree_node_get_row_data( ctree, addrbook.treeSelected );
3460
3461         ds = addressbook_find_datasource( GTK_CMCTREE_NODE(addrbook.treeSelected) );
3462         if( ds == NULL ) return;
3463
3464         abf = addressbook_get_book_file();
3465         
3466         if( obj->type == ADDR_ITEM_EMAIL ) {
3467                 ItemEMail *email = ( ItemEMail * ) obj;
3468
3469                 if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
3470                         /* Edit parent group */
3471                         AdapterGroup *adapter = ADAPTER_GROUP(pobj);
3472                         ItemGroup *itemGrp = adapter->itemGroup;
3473                         if( abf == NULL ) return;
3474                         if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3475                         name = ADDRITEM_NAME(itemGrp);
3476                         node = addrbook.treeSelected;
3477                         parentNode = GTK_CMCTREE_ROW(node)->parent;
3478                 }
3479                 else {
3480                         /* Edit person - email page */
3481                         ItemPerson *person;
3482                         person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3483                         if  ( addressbook_edit_person( abf, NULL, person, TRUE, addrbook.editaddress_vbox,
3484                                                                                    addressbook_edit_address_post_cb,
3485                                                                                    (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3486                                   != NULL ) { 
3487 #ifdef USE_LDAP
3488                                 if (abf && abf->type == ADBOOKTYPE_LDAP) {
3489                                         ldapsvr_set_modified( (LdapServer *) abf, TRUE );
3490                                         person->status = UPDATE_ENTRY;
3491                                 }
3492 #endif
3493                                 if (prefs_common.addressbook_use_editaddress_dialog)
3494                                         addressbook_edit_address_post_cb( person );
3495                         }
3496                         return;
3497                 }
3498         }
3499         else if( obj->type == ADDR_ITEM_PERSON ) {
3500                 /* Edit person - basic page */
3501                 ItemPerson *person = ( ItemPerson * ) obj;
3502                 if( addressbook_edit_person( abf, NULL, person, FALSE, addrbook.editaddress_vbox,
3503                                                                           addressbook_edit_address_post_cb,
3504                                                                           (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3505                         != NULL ) {
3506 #ifdef USE_LDAP
3507                                 if (abf && abf->type == ADBOOKTYPE_LDAP) {
3508                                         ldapsvr_set_modified( (LdapServer *) abf, TRUE );
3509                                         person->status = UPDATE_ENTRY;
3510                                 }
3511 #endif
3512                                 if (prefs_common.addressbook_use_editaddress_dialog)
3513                                         addressbook_edit_address_post_cb( person );
3514                 }
3515                 return;
3516         }
3517         else if( obj->type == ADDR_ITEM_GROUP ) {
3518                 ItemGroup *itemGrp = ( ItemGroup * ) obj;
3519                 if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3520                 parentNode = addrbook.treeSelected;
3521                 node = addressbook_find_group_node( parentNode, itemGrp );
3522                 name = ADDRITEM_NAME(itemGrp);
3523                 invalidate_address_completion();
3524         }
3525         else {
3526                 return;
3527         }
3528
3529         /* Update tree node with node name */
3530         if( node == NULL ) return;
3531         addressbook_change_node_name( node, name );
3532         gtk_sctree_sort_node( ctree, parentNode );
3533         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened ); 
3534         addressbook_set_clist(
3535                 gtk_cmctree_node_get_row_data(GTK_CMCTREE(addrbook.ctree),
3536                         addrbook.opened),
3537                 TRUE);
3538 }
3539
3540 static void addressbook_delete_address_cb(GtkAction *action, gpointer data)
3541 {
3542         addressbook_del_clicked(NULL, NULL);
3543 }
3544
3545 static void close_cb(GtkAction *action, gpointer data)
3546 {
3547         addressbook_close();
3548 }
3549
3550 static void addressbook_file_save_cb( GtkAction *action, gpointer data ) {
3551         addressbook_export_to_file();
3552 }
3553
3554 static void addressbook_person_expand_node( GtkCMCTree *ctree, GList *node, gpointer *data ) {
3555         if( node ) {
3556                 ItemPerson *person = gtk_cmctree_node_get_row_data( ctree, GTK_CMCTREE_NODE(node) );
3557                 if( person ) addritem_person_set_opened( person, TRUE );
3558         }
3559 }
3560
3561 static void addressbook_person_collapse_node( GtkCMCTree *ctree, GList *node, gpointer *data ) {
3562         if( node ) {
3563                 ItemPerson *person = gtk_cmctree_node_get_row_data( ctree, GTK_CMCTREE_NODE(node) );
3564                 if( person ) addritem_person_set_opened( person, FALSE );
3565         }
3566 }
3567
3568 static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
3569         gchar *str = NULL;
3570         gchar *eMailAlias = ADDRITEM_NAME(email);
3571         if( eMailAlias && *eMailAlias != '\0' ) {
3572                 if( person ) {
3573                         str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
3574                 }
3575                 else {
3576                         str = g_strdup( eMailAlias );
3577                 }
3578         }
3579         return str;
3580 }
3581
3582 static gboolean addressbook_match_item(const gchar *name,
3583                                        const gchar *email_alias,
3584                                        const gchar *addr,
3585                                        const gchar *remarks,
3586                                        const gchar *str)
3587 {
3588         if (!name)
3589                 return FALSE;
3590         if (!str || str[0] == '\0')
3591                 return TRUE;
3592         if (strcasestr(name, str))
3593                 return TRUE;
3594         else if (email_alias && strcasestr(email_alias, str))
3595                 return TRUE;
3596         else if (addr && strcasestr(addr, str))
3597                 return TRUE;
3598         else if (remarks && strcasestr(remarks, str))
3599                 return TRUE;
3600
3601         return FALSE;
3602 }
3603
3604 static void addressbook_load_group( GtkCMCTree *clist, ItemGroup *itemGroup ) {
3605         GList *items = itemGroup->listEMail;
3606         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3607         const gchar *search_str = gtk_entry_get_text(GTK_ENTRY(addrbook.entry));
3608         for( ; items != NULL; items = g_list_next( items ) ) {
3609                 GtkCMCTreeNode *nodeEMail = NULL;
3610                 gchar *text[N_LIST_COLS];
3611                 ItemEMail *email = items->data;
3612                 ItemPerson *person;
3613                 gchar *str = NULL;
3614
3615                 if( ! email ) continue;
3616
3617                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3618
3619                 if( !addressbook_match_item(ADDRITEM_NAME(person),
3620                                             ADDRITEM_NAME(email),
3621                                             email->address, email->remarks,
3622                                             search_str))
3623                         continue;
3624
3625                 str = addressbook_format_item_clist( person, email );
3626                 if( str ) {
3627                         text[COL_NAME] = addressbook_set_col_name_guard(str);
3628                 }
3629                 else {
3630                         text[COL_NAME] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
3631                 }
3632                 text[COL_ADDRESS] = email->address;
3633                 text[COL_REMARKS] = email->remarks;
3634                 nodeEMail = gtk_sctree_insert_node(
3635                                 clist, NULL, NULL,
3636                                 text, FOLDER_SPACING,
3637                                 atci->iconXpm,
3638                                 atci->iconXpmOpen,
3639                                 FALSE, FALSE );
3640                 gtk_cmctree_node_set_row_data( clist, nodeEMail, email );
3641                 g_free( str );
3642                 str = NULL;
3643         }
3644 }
3645
3646 gchar *addressbook_set_col_name_guard(gchar *value)
3647 {
3648         gchar *ret = "<not set>";
3649         gchar *tmp = g_strdup(value);
3650         g_strstrip(tmp);
3651         if (tmp !=NULL && *tmp != '\0')
3652                 ret = value;
3653         g_free(tmp);
3654         return ret;
3655 }
3656
3657 static void addressbook_folder_load_one_person(
3658                 GtkCMCTree *clist, ItemPerson *person,
3659                 AddressTypeControlItem *atci,
3660                 AddressTypeControlItem *atciMail )
3661 {
3662         GtkCMCTreeNode *nodePerson = NULL;
3663         GtkCMCTreeNode *nodeEMail = NULL;
3664         gchar *text[N_LIST_COLS];
3665         gboolean flgFirst = TRUE, haveAddr = FALSE;
3666         GList *node;
3667 #ifdef USE_LDAP
3668         AddressBookFile *abf = addressbook_get_book_file();
3669 #endif
3670
3671         if( person == NULL ) return;
3672
3673         text[COL_NAME] = "";
3674         node = person->listEMail;
3675         while( node ) {
3676                 ItemEMail *email = node->data;
3677                 gchar *eMailAddr = NULL;
3678                 node = g_list_next( node );
3679
3680                 text[COL_ADDRESS] = email->address;
3681                 text[COL_REMARKS] = email->remarks;
3682                 eMailAddr = ADDRITEM_NAME(email);
3683                 if( eMailAddr && *eMailAddr == '\0' ) eMailAddr = NULL;
3684                 if( flgFirst ) {
3685                         /* First email belongs with person */
3686                         gchar *str = addressbook_format_item_clist( person, email );
3687                         if( str ) {
3688                                 text[COL_NAME] = addressbook_set_col_name_guard(str);
3689                         }
3690 #ifdef USE_LDAP
3691                         else if( abf && abf->type == ADBOOKTYPE_LDAP && 
3692                                  person && person->nickName ) {
3693                                 if (person->nickName) {
3694                                         if (strcmp(person->nickName, "") != 0) {
3695                                                 text[COL_NAME] = addressbook_set_col_name_guard(person->nickName);
3696                                         }
3697                                         else {
3698                                                 text[COL_NAME] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
3699                                         }
3700                                 }
3701                         }
3702 #endif
3703                         else {
3704                                 text[COL_NAME] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
3705                         }
3706                         nodePerson = gtk_sctree_insert_node(
3707                                         clist, NULL, NULL,
3708                                         text, FOLDER_SPACING,
3709                                         atci->iconXpm,
3710                                         atci->iconXpmOpen,
3711                                         FALSE, person->isOpened );
3712                         g_free( str );
3713                         str = NULL;
3714                         gtk_cmctree_node_set_row_data(clist, nodePerson, person );
3715                 }
3716                 else {
3717                         /* Subsequent email is a child node of person */
3718                         text[COL_NAME] = ADDRITEM_NAME(email);
3719                         nodeEMail = gtk_sctree_insert_node(
3720                                         clist, nodePerson, NULL,
3721                                         text, FOLDER_SPACING,
3722                                         atciMail->iconXpm,
3723                                         atciMail->iconXpmOpen,
3724                                         FALSE, TRUE );
3725                         gtk_cmctree_node_set_row_data(clist, nodeEMail, email );
3726                 }
3727                 flgFirst = FALSE;
3728                 haveAddr = TRUE;