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