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