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