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