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