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