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