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