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