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