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