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