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