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