2007-01-18 [wwp] 2.7.1cvs27
[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         if (!prefs_common.addressbook_use_editaddress_dialog
819                         && !address_list_has_focus)
820                 addressbook_address_list_disable_some_actions();
821         return FALSE;
822 }
823
824 static gboolean addressbook_address_list_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
825                                                                                          gpointer data)
826 {
827         address_list_has_focus = TRUE;
828         return FALSE;
829 }
830
831 static gboolean addressbook_address_list_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
832                                                                                          gpointer data)
833 {
834         address_list_has_focus = FALSE;
835         if (!prefs_common.addressbook_use_editaddress_dialog
836                         && !address_index_has_focus)
837                 addressbook_address_list_disable_some_actions();
838         return FALSE;
839 }
840
841 /* save hpane and vpane's handle position when it moves */
842 static void addressbook_pane_notify_position(GObject *gobject,
843                                                                                         GParamSpec      *arg1,
844                                             gpointer     user_data)
845 {
846         prefs_common.addressbook_hpaned_pos = gtk_paned_get_position(GTK_PANED(addrbook.hpaned));
847         prefs_common.addressbook_vpaned_pos = gtk_paned_get_position(GTK_PANED(addrbook.vpaned));
848 }
849
850 /*
851 * Create the address book widgets. The address book contains two CTree widgets: the
852 * address index tree on the left and the address list on the right.
853 *
854 * The address index tree displays a hierarchy of interfaces and groups. Each node in
855 * this tree is linked to an address Adapter. Adapters have been created for interfaces,
856 * data sources and folder objects.
857 *
858 * The address list displays group, person and email objects. These items are linked
859 * directly to ItemGroup, ItemPerson and ItemEMail objects inside the address book data
860 * sources.
861 *
862 * In the tradition of MVC architecture, the data stores have been separated from the
863 * GUI components. The addrindex.c file provides the interface to all data stores.
864 */
865 static void addressbook_create(void)
866 {
867         GtkWidget *window;
868         GtkWidget *vbox;
869         GtkWidget *menubar;
870         GtkWidget *vbox2;
871         GtkWidget *ctree_swin;
872         GtkWidget *ctree;
873         GtkWidget *editaddress_vbox;
874         GtkWidget *clist_vbox;
875         GtkWidget *clist_swin;
876         GtkWidget *clist;
877         GtkWidget *hpaned;
878         GtkWidget *vpaned;
879         GtkWidget *hbox;
880         GtkWidget *label;
881         GtkWidget *entry;
882         GtkWidget *statusbar;
883         GtkWidget *hbbox;
884         GtkWidget *hsbox;
885         GtkWidget *help_btn;
886         GtkWidget *del_btn;
887         GtkWidget *edit_btn;
888         GtkWidget *reg_btn;
889         GtkWidget *lup_btn;
890         GtkWidget *to_btn;
891         GtkWidget *cc_btn;
892         GtkWidget *bcc_btn;
893         GtkWidget *close_btn;
894         GtkWidget *tree_popup;
895         GtkWidget *list_popup;
896         GtkItemFactory *tree_factory;
897         GtkItemFactory *list_factory;
898         GtkItemFactory *menu_factory;
899         gint n_entries;
900         GList *nodeIf;
901
902         gchar *index_titles[N_INDEX_COLS];
903         gchar *text;
904         gint i;
905
906         static GdkGeometry geometry;
907
908         debug_print("Creating addressbook window...\n");
909
910         index_titles[COL_SOURCES] = _("Sources");
911
912         /* Address book window */
913         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
914         gtk_window_set_title(GTK_WINDOW(window), _("Address book"));
915         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
916         gtk_widget_realize(window);
917
918         g_signal_connect(G_OBJECT(window), "delete_event",
919                          G_CALLBACK(addressbook_close), NULL);
920         g_signal_connect(G_OBJECT(window), "size_allocate",
921                          G_CALLBACK(addressbook_size_allocate_cb), NULL);
922         g_signal_connect(G_OBJECT(window), "key_press_event",
923                          G_CALLBACK(key_pressed), NULL);
924         MANAGE_WINDOW_SIGNALS_CONNECT(window);
925
926         vbox = gtk_vbox_new(FALSE, 0);
927         gtk_container_add(GTK_CONTAINER(window), vbox);
928
929         /* Menu bar */
930         n_entries = sizeof(addressbook_entries) /
931                 sizeof(addressbook_entries[0]);
932         menubar = menubar_create(window, addressbook_entries, n_entries,
933                                  "<AddressBook>", NULL);
934         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
935         menu_factory = gtk_item_factory_from_widget(menubar);
936
937         vbox2 = gtk_vbox_new(FALSE, BORDER_WIDTH);
938         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
939         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
940
941         ctree_swin = gtk_scrolled_window_new(NULL, NULL);
942         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ctree_swin),
943                                        GTK_POLICY_AUTOMATIC,
944                                        GTK_POLICY_AUTOMATIC);
945         gtk_widget_set_size_request(ctree_swin, COL_FOLDER_WIDTH + 20, -1);
946
947         /* Address index */
948         ctree = gtk_sctree_new_with_titles(N_INDEX_COLS, 0, index_titles);
949         gtk_container_add(GTK_CONTAINER(ctree_swin), ctree);
950         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
951         gtk_clist_set_column_width(GTK_CLIST(ctree), 0, COL_FOLDER_WIDTH);
952         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
953         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
954                                      GTK_CTREE_EXPANDER_SQUARE);
955         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
956         gtk_clist_set_compare_func(GTK_CLIST(ctree),
957                                    addressbook_treenode_compare_func);
958
959         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
960                          G_CALLBACK(addressbook_tree_selected), NULL);
961         g_signal_connect(G_OBJECT(ctree), "button_press_event",
962                          G_CALLBACK(addressbook_tree_button_pressed),
963                          NULL);
964         g_signal_connect(G_OBJECT(ctree), "button_release_event",
965                          G_CALLBACK(addressbook_tree_button_released),
966                          NULL);
967         /* TEMPORARY */
968         g_signal_connect(G_OBJECT(ctree), "select_row",
969                          G_CALLBACK(addressbook_select_row_tree), NULL);
970
971         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
972                           addressbook_drag_types, 1,
973                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
974         g_signal_connect(G_OBJECT(ctree), "drag_motion",
975                          G_CALLBACK(addressbook_drag_motion_cb),
976                          ctree);
977         g_signal_connect(G_OBJECT(ctree), "drag_leave",
978                          G_CALLBACK(addressbook_drag_leave_cb),
979                          ctree);
980         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
981                          G_CALLBACK(addressbook_drag_received_cb),
982                          ctree);
983         g_signal_connect(G_OBJECT(ctree), "focus_in_event",
984                 G_CALLBACK(addressbook_address_index_focus_evt_in), NULL);
985         g_signal_connect(G_OBJECT(ctree), "focus_out_event",
986                 G_CALLBACK(addressbook_address_index_focus_evt_out), NULL);
987
988         clist_vbox = gtk_vbox_new(FALSE, 4);
989
990         clist_swin = gtk_scrolled_window_new(NULL, NULL);
991         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
992                                        GTK_POLICY_AUTOMATIC,
993                                        GTK_POLICY_AUTOMATIC);
994         gtk_box_pack_start(GTK_BOX(clist_vbox), clist_swin, TRUE, TRUE, 0);
995
996         /* Address list */
997         clist = gtk_sctree_new_with_titles(N_LIST_COLS, 0, list_titles);
998         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
999         gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_EXTENDED);
1000         gtk_ctree_set_line_style(GTK_CTREE(clist), GTK_CTREE_LINES_NONE);
1001         gtk_ctree_set_expander_style(GTK_CTREE(clist), GTK_CTREE_EXPANDER_SQUARE);
1002         gtk_ctree_set_indent(GTK_CTREE(clist), CTREE_INDENT);
1003         gtk_clist_set_column_width(GTK_CLIST(clist), COL_NAME,
1004                                    COL_NAME_WIDTH);
1005         gtk_clist_set_column_width(GTK_CLIST(clist), COL_ADDRESS,
1006                                    COL_ADDRESS_WIDTH);
1007         gtk_widget_set_size_request(clist, -1, 80);
1008
1009         addressbook_sort_list(GTK_CLIST(clist), COL_NAME, GTK_SORT_ASCENDING);
1010         g_signal_connect(G_OBJECT(GTK_CLIST(clist)->column[COL_NAME].button),
1011                 "clicked", G_CALLBACK(addressbook_name_clicked), clist);
1012         g_signal_connect(G_OBJECT(GTK_CLIST(clist)->column[COL_ADDRESS].button),
1013                 "clicked", G_CALLBACK(addressbook_address_clicked), clist);
1014         g_signal_connect(G_OBJECT(GTK_CLIST(clist)->column[COL_REMARKS].button),
1015                 "clicked", G_CALLBACK(addressbook_remarks_clicked), clist);
1016         g_signal_connect(G_OBJECT(clist), "focus_in_event",
1017                 G_CALLBACK(addressbook_address_list_focus_evt_in), NULL);
1018         g_signal_connect(G_OBJECT(clist), "focus_out_event",
1019                 G_CALLBACK(addressbook_address_list_focus_evt_out), NULL);
1020
1021         for (i = 0; i < N_LIST_COLS; i++)
1022                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
1023                                        GTK_CAN_FOCUS);
1024
1025         g_signal_connect(G_OBJECT(clist), "tree_select_row",
1026                          G_CALLBACK(addressbook_list_row_selected), NULL);
1027         g_signal_connect(G_OBJECT(clist), "tree_unselect_row",
1028                          G_CALLBACK(addressbook_list_row_unselected), NULL);
1029         g_signal_connect(G_OBJECT(clist), "button_press_event",
1030                          G_CALLBACK(addressbook_list_button_pressed),
1031                          NULL);
1032         g_signal_connect(G_OBJECT(clist), "button_release_event",
1033                          G_CALLBACK(addressbook_list_button_released),
1034                          NULL);
1035         g_signal_connect(G_OBJECT(clist), "tree_expand",
1036                          G_CALLBACK(addressbook_person_expand_node), NULL );
1037         g_signal_connect(G_OBJECT(clist), "tree_collapse",
1038                          G_CALLBACK(addressbook_person_collapse_node), NULL );
1039         g_signal_connect(G_OBJECT(clist), "start_drag",
1040                          G_CALLBACK(addressbook_start_drag), NULL);
1041         g_signal_connect(G_OBJECT(clist), "drag_data_get",
1042                          G_CALLBACK(addressbook_drag_data_get), NULL);  
1043         hbox = gtk_hbox_new(FALSE, 4);
1044         gtk_box_pack_start(GTK_BOX(clist_vbox), hbox, FALSE, FALSE, 0);
1045
1046         label = gtk_label_new(_("Lookup name:"));
1047         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1048
1049         entry = gtk_entry_new();
1050         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1051
1052         address_completion_register_entry(GTK_ENTRY(entry), FALSE);
1053
1054         g_signal_connect(G_OBJECT(entry), "key_press_event",
1055                          G_CALLBACK(addressbook_entry_key_pressed),
1056                          NULL);
1057
1058         if (!prefs_common.addressbook_use_editaddress_dialog) {
1059                 editaddress_vbox = gtk_vbox_new(FALSE, 4);
1060                 vpaned = gtk_vpaned_new();
1061                 gtk_paned_pack1(GTK_PANED(vpaned), clist_vbox, FALSE, FALSE);
1062                 gtk_paned_pack2(GTK_PANED(vpaned), editaddress_vbox, TRUE, FALSE);
1063         } else {
1064                 vpaned = NULL;
1065                 editaddress_vbox = NULL;
1066         }
1067         hpaned = gtk_hpaned_new();
1068         gtk_box_pack_start(GTK_BOX(vbox2), hpaned, TRUE, TRUE, 0);
1069         gtk_paned_pack1(GTK_PANED(hpaned), ctree_swin, FALSE, FALSE);
1070         if (prefs_common.addressbook_use_editaddress_dialog)
1071                 gtk_paned_pack2(GTK_PANED(hpaned), clist_vbox, TRUE, FALSE);
1072         else
1073                 gtk_paned_pack2(GTK_PANED(hpaned), vpaned, TRUE, FALSE);
1074
1075         /* Status bar */
1076         hsbox = gtk_hbox_new(FALSE, 0);
1077         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
1078         statusbar = gtk_statusbar_new();
1079         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
1080
1081         /* Button panel */
1082         hbbox = gtk_hbutton_box_new();
1083         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
1084         gtk_box_set_spacing(GTK_BOX(hbbox), 2);
1085         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 4);
1086         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
1087
1088         gtkut_stock_button_add_help(hbbox, &help_btn);
1089
1090         edit_btn = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1091         GTK_WIDGET_SET_FLAGS(edit_btn, GTK_CAN_DEFAULT);
1092         gtk_box_pack_start(GTK_BOX(hbbox), edit_btn, TRUE, TRUE, 0);
1093         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1094         GTK_WIDGET_SET_FLAGS(del_btn, GTK_CAN_DEFAULT);
1095         gtk_box_pack_start(GTK_BOX(hbbox), del_btn, TRUE, TRUE, 0);
1096         reg_btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
1097         GTK_WIDGET_SET_FLAGS(reg_btn, GTK_CAN_DEFAULT);
1098         gtk_box_pack_start(GTK_BOX(hbbox), reg_btn, TRUE, TRUE, 0);
1099
1100
1101         lup_btn = gtk_button_new_from_stock(GTK_STOCK_FIND);
1102         GTK_WIDGET_SET_FLAGS(lup_btn, GTK_CAN_DEFAULT);
1103         gtk_box_pack_start(GTK_BOX(hbox), lup_btn, TRUE, TRUE, 0);
1104
1105         g_signal_connect(G_OBJECT(help_btn), "clicked",
1106                          G_CALLBACK(manual_open_with_anchor_cb),
1107                          MANUAL_ANCHOR_ADDRBOOK);
1108
1109         g_signal_connect(G_OBJECT(edit_btn), "clicked",
1110                          G_CALLBACK(addressbook_edit_clicked), NULL);
1111         g_signal_connect(G_OBJECT(del_btn), "clicked",
1112                          G_CALLBACK(addressbook_del_clicked), NULL);
1113         g_signal_connect(G_OBJECT(reg_btn), "clicked",
1114                          G_CALLBACK(addressbook_reg_clicked), NULL);
1115         g_signal_connect(G_OBJECT(lup_btn), "clicked",
1116                          G_CALLBACK(addressbook_lup_clicked), NULL);
1117
1118         to_btn = gtk_button_new_with_label
1119                 (prefs_common.trans_hdr ? _("To:") : "To:");
1120         GTK_WIDGET_SET_FLAGS(to_btn, GTK_CAN_DEFAULT);
1121         gtk_box_pack_start(GTK_BOX(hbbox), to_btn, TRUE, TRUE, 0);
1122         cc_btn = gtk_button_new_with_label
1123                 (prefs_common.trans_hdr ? _("Cc:") : "Cc:");
1124         GTK_WIDGET_SET_FLAGS(cc_btn, GTK_CAN_DEFAULT);
1125         gtk_box_pack_start(GTK_BOX(hbbox), cc_btn, TRUE, TRUE, 0);
1126         bcc_btn = gtk_button_new_with_label
1127                 (prefs_common.trans_hdr ? _("Bcc:") : "Bcc:");
1128         GTK_WIDGET_SET_FLAGS(bcc_btn, GTK_CAN_DEFAULT);
1129         gtk_box_pack_start(GTK_BOX(hbbox), bcc_btn, TRUE, TRUE, 0);
1130
1131         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
1132         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
1133         gtk_box_pack_start(GTK_BOX(hbbox), close_btn, TRUE, TRUE, 0);
1134
1135         g_signal_connect(G_OBJECT(to_btn), "clicked",
1136                          G_CALLBACK(addressbook_to_clicked),
1137                          GINT_TO_POINTER(COMPOSE_TO));
1138         g_signal_connect(G_OBJECT(cc_btn), "clicked",
1139                          G_CALLBACK(addressbook_to_clicked),
1140                          GINT_TO_POINTER(COMPOSE_CC));
1141         g_signal_connect(G_OBJECT(bcc_btn), "clicked",
1142                          G_CALLBACK(addressbook_to_clicked),
1143                          GINT_TO_POINTER(COMPOSE_BCC));
1144         g_signal_connect(G_OBJECT(close_btn), "clicked",
1145                          G_CALLBACK(addressbook_close_clicked), NULL);
1146
1147         /* Build icons for interface */
1148         stock_pixmap_gdk( window, STOCK_PIXMAP_INTERFACE,
1149                           &interfacexpm, &interfacexpmmask );
1150
1151         /* Build control tables */
1152         addrbookctl_build_map(window);
1153         addrbookctl_build_iflist();
1154         addrbookctl_build_ifselect();
1155
1156         addrbook.clist   = NULL;
1157
1158         /* Add each interface into the tree as a root level folder */
1159         nodeIf = _addressInterfaceList_;
1160         while( nodeIf ) {
1161                 AdapterInterface *adapter = nodeIf->data;
1162                 AddressInterface *iface = adapter->interface;
1163                 nodeIf = g_list_next(nodeIf);
1164
1165                 if(iface->useInterface) {
1166                         AddressTypeControlItem *atci = adapter->atci;
1167                         text = atci->displayName;
1168                         adapter->treeNode =
1169                                 gtk_sctree_insert_node( GTK_CTREE(ctree),
1170                                         NULL, NULL, &text, FOLDER_SPACING,
1171                                         interfacexpm, interfacexpmmask,
1172                                         interfacexpm, interfacexpmmask,
1173                                         FALSE, FALSE );
1174                         menu_set_sensitive( menu_factory, atci->menuCommand, adapter->haveLibrary );
1175                         gtk_ctree_node_set_row_data_full(
1176                                 GTK_CTREE(ctree), adapter->treeNode, adapter,
1177                                 addressbook_free_treenode );
1178                 }
1179         }
1180
1181         /* Popup menu */
1182         n_entries = sizeof(addressbook_tree_popup_entries) /
1183                 sizeof(addressbook_tree_popup_entries[0]);
1184         tree_popup = menu_create_items(addressbook_tree_popup_entries,
1185                                        n_entries,
1186                                        "<AddressBookTree>", &tree_factory,
1187                                        NULL);
1188         n_entries = sizeof(addressbook_list_popup_entries) /
1189                 sizeof(addressbook_list_popup_entries[0]);
1190         list_popup = menu_create_items(addressbook_list_popup_entries,
1191                                        n_entries,
1192                                        "<AddressBookList>", &list_factory,
1193                                        NULL);
1194
1195         addrbook.window  = window;
1196         addrbook.hpaned  = hpaned;
1197         addrbook.vpaned  = vpaned;
1198         addrbook.menubar = menubar;
1199         addrbook.ctree   = ctree;
1200         addrbook.ctree_swin
1201                          = ctree_swin;
1202         addrbook.editaddress_vbox = editaddress_vbox;
1203         addrbook.clist   = clist;
1204         addrbook.label   = label;
1205         addrbook.entry   = entry;
1206         addrbook.statusbar = statusbar;
1207         addrbook.status_cid = gtk_statusbar_get_context_id(
1208                         GTK_STATUSBAR(statusbar), "Addressbook Window" );
1209
1210         addrbook.help_btn = help_btn;
1211         addrbook.edit_btn = edit_btn;
1212         addrbook.del_btn = del_btn;
1213         addrbook.reg_btn = reg_btn;
1214         addrbook.lup_btn = lup_btn;
1215         addrbook.to_btn  = to_btn;
1216         addrbook.cc_btn  = cc_btn;
1217         addrbook.bcc_btn = bcc_btn;
1218
1219         addrbook.tree_popup   = tree_popup;
1220         addrbook.list_popup   = list_popup;
1221         addrbook.tree_factory = tree_factory;
1222         addrbook.list_factory = list_factory;
1223         addrbook.menu_factory = menu_factory;
1224
1225         addrbook.listSelected = NULL;
1226
1227         if (!geometry.min_height) {
1228                 geometry.min_width = ADDRESSBOOK_WIDTH;
1229                 geometry.min_height = ADDRESSBOOK_HEIGHT;
1230         }
1231
1232         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
1233                                       GDK_HINT_MIN_SIZE);
1234         gtk_widget_set_size_request(window, prefs_common.addressbookwin_width,
1235                                     prefs_common.addressbookwin_height);
1236
1237         if (!prefs_common.addressbook_use_editaddress_dialog) {
1238                 gtk_paned_set_position(GTK_PANED(vpaned), prefs_common.addressbook_vpaned_pos);
1239                 g_signal_connect(G_OBJECT(vpaned), "notify::position",
1240                                 G_CALLBACK(addressbook_pane_notify_position), NULL);
1241         }       
1242         gtk_paned_set_position(GTK_PANED(hpaned), prefs_common.addressbook_hpaned_pos);
1243         g_signal_connect(G_OBJECT(hpaned), "notify::position",
1244                         G_CALLBACK(addressbook_pane_notify_position), NULL);
1245
1246         gtk_widget_show_all(window);
1247 }
1248
1249 /**
1250  * Close address book window and save to file(s).
1251  */
1252 static gint addressbook_close( void ) {
1253         address_completion_end(addrbook.window);
1254         if (!prefs_common.addressbook_use_editaddress_dialog)
1255                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
1256         gtk_widget_hide(addrbook.window);
1257         addressbook_export_to_file();
1258         return TRUE;
1259 }
1260
1261 /**
1262  * Display message in status line.
1263  * \param msg Message to display.
1264  */
1265 static void addressbook_status_show( gchar *msg ) {
1266         if( addrbook.statusbar != NULL ) {
1267                 gtk_statusbar_pop(
1268                         GTK_STATUSBAR(addrbook.statusbar),
1269                         addrbook.status_cid );
1270                 if( msg ) {
1271                         gtk_statusbar_push(
1272                                 GTK_STATUSBAR(addrbook.statusbar),
1273                                 addrbook.status_cid, msg );
1274                 }
1275         }
1276 }
1277
1278 static void addressbook_ds_status_message( AddressDataSource *ds, gchar *msg ) {
1279         *addressbook_msgbuf = '\0';
1280         if( ds ) {
1281                 gchar *name;
1282
1283                 name = addrindex_ds_get_name( ds );
1284                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
1285                             "%s: %s", name, msg );
1286         }
1287         else {
1288                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
1289                             "%s", msg );
1290         }
1291         addressbook_status_show( addressbook_msgbuf );
1292 }
1293
1294 static void addressbook_ds_show_message( AddressDataSource *ds ) {
1295         gint retVal;
1296         gchar *name;
1297         gchar *desc;
1298         *addressbook_msgbuf = '\0';
1299         if( ds ) {
1300                 name = addrindex_ds_get_name( ds );
1301                 retVal = addrindex_ds_get_status_code( ds );
1302                 if( retVal == MGU_SUCCESS ) {
1303                         g_snprintf( addressbook_msgbuf,
1304                                     sizeof(addressbook_msgbuf), "%s", name );
1305                 }
1306                 else {
1307                         desc = addressbook_err2string( _lutErrorsGeneral_, retVal );
1308                         g_snprintf( addressbook_msgbuf, 
1309                             sizeof(addressbook_msgbuf), "%s: %s", name, desc );
1310                 }
1311         }
1312         addressbook_status_show( addressbook_msgbuf );
1313 }
1314
1315 static void addressbook_button_set_sensitive(void)
1316 {
1317         gboolean to_sens  = FALSE;
1318         gboolean cc_sens  = FALSE;
1319         gboolean bcc_sens = FALSE;
1320
1321         if (!addrbook.window) return;
1322
1323         if (addrbook.target_compose) {
1324                 to_sens = TRUE;
1325                 cc_sens = TRUE;
1326                 bcc_sens = TRUE;
1327         }
1328
1329         gtk_widget_set_sensitive(addrbook.to_btn, to_sens);
1330         gtk_widget_set_sensitive(addrbook.cc_btn, cc_sens);
1331         gtk_widget_set_sensitive(addrbook.bcc_btn, bcc_sens);
1332 }
1333
1334 static void addressbook_edit_clicked(GtkButton *button, gpointer data)
1335 {
1336         addressbook_edit_address_cb(NULL, 0, NULL);
1337 }
1338
1339 /*
1340 * Delete one or more objects from address list.
1341 */
1342 static void addressbook_del_clicked(GtkButton *button, gpointer data)
1343 {
1344         GtkCTree *clist = GTK_CTREE(addrbook.clist);
1345         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1346         AddressObject *pobj;
1347         AdapterDSource *ads = NULL;
1348         GtkCTreeNode *nodeList;
1349         gboolean procFlag;
1350         AlertValue aval;
1351         AddressBookFile *abf = NULL;
1352         AddressDataSource *ds = NULL;
1353         AddressInterface *iface;
1354         AddrItemObject *aio;
1355         AddrSelectItem *item;
1356         GList *list, *node;
1357         gboolean refreshList = FALSE;
1358         
1359         pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened );
1360         g_return_if_fail(pobj != NULL);
1361
1362         /* Test whether anything selected for deletion */
1363         nodeList = addrbook.listSelected;
1364
1365         aio = gtk_ctree_node_get_row_data( clist, nodeList );
1366         if( aio == NULL) return;
1367         ds = addressbook_find_datasource( addrbook.treeSelected );
1368         if( ds == NULL ) return;
1369
1370         /* Test for read only */
1371         iface = ds->interface;
1372         if( iface->readOnly ) {
1373                 alertpanel( _("Delete address(es)"),
1374                         _("This address data is readonly and cannot be deleted."),
1375                         GTK_STOCK_CLOSE, NULL, NULL );
1376                 return;
1377         }
1378
1379         /* Test whether Ok to proceed */
1380         procFlag = FALSE;
1381         if( pobj->type == ADDR_DATASOURCE ) {
1382                 ads = ADAPTER_DSOURCE(pobj);
1383                 if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
1384         }
1385         else if( pobj->type == ADDR_ITEM_FOLDER ) {
1386                 procFlag = TRUE;
1387         }
1388         else if( pobj->type == ADDR_ITEM_GROUP ) {
1389                 procFlag = TRUE;
1390         }
1391         if( ! procFlag ) return;
1392         abf = ds->rawDataSource;
1393         if( abf == NULL ) return;
1394
1395
1396         /* Process deletions */
1397         if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
1398                 gboolean group_delete = TRUE;
1399                 /* Items inside folders */
1400                 list = addrselect_get_list( _addressSelect_ );
1401                 /* Confirm deletion */
1402                 node = list;
1403                 while( node ) {
1404                         item = node->data;
1405                         node = g_list_next( node );
1406                         aio = ( AddrItemObject * ) item->addressItem;
1407                         if( aio->type == ADDR_ITEM_PERSON || aio->type == ADDR_ITEM_EMAIL ) {
1408                                 group_delete = FALSE;
1409                                 break;
1410                         }
1411                 }
1412                 if (group_delete) {
1413                         aval = alertpanel( _("Delete group"),
1414                                         _("Really delete the group(s)?\n"
1415                                           "The addresses it contains will not be lost."),
1416                                         GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
1417                         if( aval != G_ALERTALTERNATE ) return;
1418                 } else {
1419                         aval = alertpanel( _("Delete address(es)"),
1420                                         _("Really delete the address(es)?"),
1421                                         GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
1422                         if( aval != G_ALERTALTERNATE ) return;
1423                 }
1424
1425                 node = list;
1426                 while( node ) {
1427                         item = node->data;
1428                         node = g_list_next( node );
1429                         aio = ( AddrItemObject * ) item->addressItem;
1430                         if( aio->type == ADDR_ITEM_GROUP ) {
1431                                 ItemGroup *item = ( ItemGroup * ) aio;
1432                                 GtkCTreeNode *nd = NULL;
1433
1434                                 nd = addressbook_find_group_node( addrbook.opened, item );
1435                                 item = addrbook_remove_group( abf, item );
1436                                 if( item ) {
1437                                         addritem_free_item_group( item );
1438                                 }
1439                                 /* Remove group from parent node */
1440                                 gtk_ctree_remove_node( ctree, nd );
1441                                 refreshList = TRUE;
1442                         }
1443                         else if( aio->type == ADDR_ITEM_PERSON ) {
1444                                 ItemPerson *item = ( ItemPerson * ) aio;
1445                                 addressbook_folder_remove_one_person( clist, item );
1446                                 if (pobj->type == ADDR_ITEM_FOLDER)
1447                                         addritem_folder_remove_person(ADAPTER_FOLDER(pobj)->itemFolder, item);
1448                                 item = addrbook_remove_person( abf, item );
1449                                 if( item ) {
1450                                         addritem_free_item_person( item );
1451                                 }
1452                         }
1453                         else if( aio->type == ADDR_ITEM_EMAIL ) {
1454                                 ItemEMail *item = ( ItemEMail * ) aio;
1455                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1456                                 item = addrbook_person_remove_email( abf, person, item );
1457                                 if( item ) {
1458                                         addrcache_remove_email(abf->addressCache, item);
1459                                         addritem_free_item_email( item );
1460                                 }
1461                                 addressbook_folder_refresh_one_person( clist, person );
1462                         }
1463                 }
1464                 g_list_free( list );
1465                 addressbook_list_select_clear();
1466                 if( refreshList ) {
1467                         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1468                         addressbook_set_clist(
1469                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
1470                                         addrbook.opened),
1471                                 TRUE);
1472                 }
1473                 addrbook_set_dirty(abf, TRUE);
1474                 addressbook_export_to_file();
1475                 addressbook_list_menu_setup();
1476                 return;
1477         }
1478         else if( pobj->type == ADDR_ITEM_GROUP ) {
1479                 /* Items inside groups */
1480                 list = addrselect_get_list( _addressSelect_ );
1481                 node = list;
1482                 while( node ) {
1483                         item = node->data;
1484                         node = g_list_next( node );
1485                         aio = ( AddrItemObject * ) item->addressItem;
1486                         if( aio->type == ADDR_ITEM_EMAIL ) {
1487                                 ItemEMail *item = ( ItemEMail * ) aio;
1488                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1489                                 item = addrbook_person_remove_email( abf, person, item );
1490                                 if( item ) {
1491                                         addritem_free_item_email( item );
1492                                 }
1493                         }
1494                 }
1495                 g_list_free( list );
1496                 addressbook_list_select_clear();
1497                 gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1498                 addressbook_set_clist(
1499                         gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
1500                                 addrbook.opened),
1501                         TRUE);
1502                 
1503                 addrbook_set_dirty(abf, TRUE);
1504                 addressbook_export_to_file();
1505                 addressbook_list_menu_setup();
1506                 return;
1507         }
1508
1509         gtk_ctree_node_set_row_data( clist, nodeList, NULL );
1510         gtk_ctree_remove_node( clist, nodeList );
1511
1512 }
1513
1514 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
1515 {
1516         addressbook_new_address_cb( NULL, 0, NULL );
1517 }
1518
1519 gchar *addressbook_format_address( AddrItemObject * aio ) {
1520         gchar *buf = NULL;
1521         gchar *name = NULL;
1522         gchar *address = NULL;
1523
1524         if( aio->type == ADDR_ITEM_EMAIL ) {
1525                 ItemPerson *person = NULL;
1526                 ItemEMail *email = ( ItemEMail * ) aio;
1527
1528                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
1529                 if( email->address ) {
1530                         if( ADDRITEM_NAME(email) ) {
1531                                 name = ADDRITEM_NAME(email);
1532                                 if( *name == '\0' ) {
1533                                         name = ADDRITEM_NAME(person);
1534                                 }
1535                         }
1536                         else if( ADDRITEM_NAME(person) ) {
1537                                 name = ADDRITEM_NAME(person);
1538                         }
1539                         else {
1540                                 buf = g_strdup( email->address );
1541                         }
1542                         address = email->address;
1543                 }
1544         }
1545         else if( aio->type == ADDR_ITEM_PERSON ) {
1546                 ItemPerson *person = ( ItemPerson * ) aio;
1547                 GList *node = person->listEMail;
1548
1549                 name = ADDRITEM_NAME(person);
1550                 if( node ) {
1551                         ItemEMail *email = ( ItemEMail * ) node->data;
1552                         address = email->address;
1553                 }
1554         }
1555         if( address ) {
1556                 if( name && name[0] != '\0' ) {
1557                         if( strchr_with_skip_quote( name, '"', ',' ) )
1558                                 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
1559                         else
1560                                 buf = g_strdup_printf( "%s <%s>", name, address );
1561                 }
1562                 else {
1563                         buf = g_strdup( address );
1564                 }
1565         }
1566
1567         return buf;
1568 }
1569
1570 static void addressbook_to_clicked(GtkButton *button, gpointer data)
1571 {
1572         GList *list, *node;
1573         Compose *compose;
1574         AddrSelectItem *item;
1575         AddrItemObject *aio;
1576         gchar *addr;
1577
1578         compose = addrbook.target_compose;
1579         if( ! compose ) return;
1580
1581         /* Nothing selected, but maybe there is something in text entry */
1582         addr = (char *)gtk_entry_get_text( GTK_ENTRY( addrbook.entry) );
1583         if ( addr ) {
1584                 compose_entry_append(
1585                         compose, addr, (ComposeEntryType)data );
1586         }
1587
1588         /* Select from address list */
1589         list = addrselect_get_list( _addressSelect_ );
1590         node = list;
1591         if (node) {
1592                 while( node ) {
1593                         item = node->data;
1594                         node = g_list_next( node );
1595                         aio = item->addressItem;
1596                         if( aio->type == ADDR_ITEM_PERSON ||
1597                             aio->type == ADDR_ITEM_EMAIL ) {
1598                                 addr = addressbook_format_address( aio );
1599                                 compose_entry_append(
1600                                         compose, addr, (ComposeEntryType) data );
1601                                 g_free( addr );
1602                         }
1603                         else if( aio->type == ADDR_ITEM_GROUP ) {
1604                                 ItemGroup *group = ( ItemGroup * ) aio;
1605                                 GList *nodeMail = group->listEMail;
1606                                 while( nodeMail ) {
1607                                         ItemEMail *email = nodeMail->data;
1608
1609                                         addr = addressbook_format_address(
1610                                                         ( AddrItemObject * ) email );
1611                                         compose_entry_append(
1612                                                 compose, addr, (ComposeEntryType) data );
1613                                         g_free( addr );
1614                                         nodeMail = g_list_next( nodeMail );
1615                                 }
1616                         }
1617                 }
1618         } else {
1619                 AddressObject *obj = NULL;
1620
1621                 obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1622         
1623                 if( obj && obj->type == ADDR_ITEM_GROUP ) {
1624                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
1625                         GList *nodeMail = itemGroup->listEMail;
1626                         while( nodeMail ) {
1627                                 ItemEMail *email = nodeMail->data;
1628
1629                                 addr = addressbook_format_address(
1630                                                 ( AddrItemObject * ) email );
1631                                 compose_entry_append(
1632                                         compose, addr, (ComposeEntryType) data );
1633                                 g_free( addr );
1634                                 nodeMail = g_list_next( nodeMail );
1635                         }
1636                 }
1637         }
1638         g_list_free( list );
1639 }
1640
1641 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
1642         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",   sensitive );
1643         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book", sensitive );
1644         menu_set_sensitive( addrbook.menu_factory, "/Book/New Folder",  sensitive );
1645
1646         menu_set_sensitive( addrbook.menu_factory, "/Address/Select all",    TRUE );
1647         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",    sensitive );
1648         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",   sensitive );
1649         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",  sensitive );
1650
1651         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", sensitive );
1652         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   sensitive );
1653         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To",     sensitive );
1654         gtk_widget_set_sensitive( addrbook.reg_btn, sensitive );
1655         gtk_widget_set_sensitive( addrbook.edit_btn, sensitive );
1656         gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
1657 }
1658
1659 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCTreeNode *node ) {
1660         gboolean canEdit = FALSE;
1661         gboolean canDelete = TRUE;
1662         gboolean canAdd = FALSE;
1663         gboolean canEditTr = TRUE;
1664         gboolean editAddress = FALSE;
1665         gboolean canExport = TRUE;
1666         AddressTypeControlItem *atci = NULL;
1667         AddressDataSource *ds = NULL;
1668         AddressInterface *iface = NULL;
1669
1670         if( obj == NULL ) return;
1671         if( obj->type == ADDR_INTERFACE ) {
1672                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
1673                 iface = adapter->interface;
1674                 if( iface ) {
1675                         if( iface->haveLibrary ) {
1676                                 /* Enable appropriate File / New command */
1677                                 atci = adapter->atci;
1678                                 menu_set_sensitive( addrbook.menu_factory, atci->menuCommand, TRUE );
1679                         }
1680                 }
1681                 canEditTr = canExport = FALSE;
1682         }
1683         else if( obj->type == ADDR_DATASOURCE ) {
1684                 AdapterDSource *ads = ADAPTER_DSOURCE(obj);
1685                 ds = ads->dataSource;
1686                 iface = ds->interface;
1687                 if( ! iface->readOnly ) {
1688                         canAdd = canEdit = editAddress = canDelete = TRUE;
1689                 }
1690                 if( ! iface->haveLibrary ) {
1691                         canAdd = canEdit = editAddress = canExport = canDelete = FALSE;
1692                 }
1693         }
1694         else if( obj->type == ADDR_ITEM_FOLDER ) {
1695                 ds = addressbook_find_datasource( addrbook.treeSelected );
1696                 if( ds ) {
1697                         iface = ds->interface;
1698                         if( iface->readOnly ) {
1699                                 canEditTr = FALSE;
1700                                 canDelete = FALSE;
1701                         }
1702                         else {
1703                                 canAdd = editAddress = TRUE;
1704                         }
1705                 }
1706         }
1707         else if( obj->type == ADDR_ITEM_GROUP ) {
1708                 ds = addressbook_find_datasource( addrbook.treeSelected );
1709                 if( ds ) {
1710                         iface = ds->interface;
1711                         if( ! iface->readOnly ) {
1712                                 editAddress = TRUE;
1713                         }
1714                 }
1715         }
1716
1717         if( addrbook.listSelected == NULL ) canEdit = FALSE;
1718
1719         /* Enable add */
1720         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", editAddress );
1721         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   canAdd );
1722         menu_set_sensitive( addrbook.menu_factory, "/Book/New Folder",  canAdd );
1723         gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
1724
1725         /* Enable edit */
1726         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1727         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canDelete );
1728         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
1729         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1730
1731         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",      canEditTr );
1732         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book",    canEditTr );
1733
1734         /* Export data */
1735         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export HTML...", canExport );
1736         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export LDIF...", canExport );
1737 }
1738
1739 static void addressbook_list_menu_setup( void );
1740
1741 /**
1742  * Address book tree callback function that responds to selection of tree
1743  * items.
1744  *
1745  * \param ctree  Tree widget.
1746  * \param node   Node that was selected.
1747  * \param column Column number where selected occurred.
1748  * \param data   Pointer to user data.
1749  */
1750 static void addressbook_tree_selected(GtkCTree *ctree, GtkCTreeNode *node,
1751                                       gint column, gpointer data)
1752 {
1753         AddressObject *obj = NULL;
1754         AdapterDSource *ads = NULL;
1755         AddressDataSource *ds = NULL;
1756         ItemFolder *rootFolder = NULL;
1757         AddressObjectType aot;
1758
1759         addrbook.treeSelected = node;
1760         addrbook.listSelected = NULL;
1761         addressbook_status_show( "" );
1762         if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
1763
1764         if( node ) obj = gtk_ctree_node_get_row_data( ctree, node );
1765         if( obj == NULL ) {
1766                 addressbook_set_clist(NULL, TRUE);
1767                 return;
1768         }
1769         addrbook.opened = node;
1770
1771         if( obj->type == ADDR_DATASOURCE ) {
1772                 /* Read from file */
1773                 static gboolean tVal = TRUE;
1774
1775                 ads = ADAPTER_DSOURCE(obj);
1776                 if( ads == NULL ) return;
1777                 ds = ads->dataSource;
1778                 if( ds == NULL ) return;                
1779
1780                 if( addrindex_ds_get_modify_flag( ds ) ) {
1781                         addrindex_ds_read_data( ds );
1782                 }
1783
1784                 if( ! addrindex_ds_get_read_flag( ds ) ) {
1785                         addrindex_ds_read_data( ds );
1786                 }
1787                 addressbook_ds_show_message( ds );
1788
1789                 if( ! addrindex_ds_get_access_flag( ds ) ) {
1790                         /* Remove existing folders and groups */
1791                         gtk_clist_freeze( GTK_CLIST(ctree) );
1792                         addressbook_tree_remove_children( ctree, node );
1793                         gtk_clist_thaw( GTK_CLIST(ctree) );
1794
1795                         /* Load folders into the tree */
1796                         rootFolder = addrindex_ds_get_root_folder( ds );
1797                         if( ds->type == ADDR_IF_JPILOT ) {
1798                                 aot = ADDR_CATEGORY;
1799                         }
1800                         else if( ds->type == ADDR_IF_LDAP ) {
1801                                 aot = ADDR_LDAP_QUERY;
1802                         }
1803                         else {
1804                                 aot = ADDR_ITEM_FOLDER;
1805                         }
1806                         addressbook_node_add_folder( node, ds, rootFolder, aot );
1807                         addrindex_ds_set_access_flag( ds, &tVal );
1808                         gtk_ctree_expand( ctree, node );
1809                 }
1810         } else {
1811                 addressbook_set_clist(NULL, TRUE);
1812         }
1813
1814         /* Update address list */
1815         g_signal_handlers_block_by_func
1816                 (G_OBJECT(ctree),
1817                  G_CALLBACK(addressbook_tree_selected), NULL);
1818         addressbook_set_clist( obj, FALSE );
1819         g_signal_handlers_unblock_by_func
1820                 (G_OBJECT(ctree),
1821                  G_CALLBACK(addressbook_tree_selected), NULL);
1822         if (!prefs_common.addressbook_use_editaddress_dialog)
1823                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
1824
1825         /* Setup main menu selections */
1826         addressbook_menubar_set_sensitive( FALSE );
1827         addressbook_list_menu_setup();
1828         addressbook_menuitem_set_sensitive( obj, node );
1829
1830         addressbook_list_select_clear();
1831         addressbook_list_menu_setup();
1832         return;
1833 }
1834
1835 /**
1836  * Setup address list popup menu items. Items are enabled or disabled as
1837  * required.
1838  */
1839 static void addressbook_list_menu_setup( void ) {
1840         GtkCTree *clist = NULL;
1841         AddressObject *pobj = NULL;
1842         AddressObject *obj = NULL;
1843         AdapterDSource *ads = NULL;
1844         AddressInterface *iface = NULL;
1845         AddressDataSource *ds = NULL;
1846         gboolean canEdit = FALSE;
1847         gboolean canDelete = FALSE;
1848         gboolean canCut = FALSE;
1849         gboolean canCopy = FALSE;
1850         gboolean canPaste = FALSE;
1851         gboolean canBrowse = FALSE;
1852
1853         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1854         if( pobj == NULL ) return;
1855
1856         clist = GTK_CTREE(addrbook.clist);
1857         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1858         if( obj == NULL ) canEdit = FALSE;
1859
1860         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
1861         menu_set_sensitive( addrbook.list_factory, "/Select all", TRUE );
1862
1863         if( pobj->type == ADDR_DATASOURCE ) {
1864                 /* Parent object is a data source */
1865                 ads = ADAPTER_DSOURCE(pobj);
1866                 ds = ads->dataSource;
1867                 iface = ds->interface;
1868                 if( ! iface->readOnly ) {
1869                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1870                         menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1871                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1872                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1873                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1874                         if( obj ) canEdit = TRUE;
1875                 }
1876                 canDelete = canEdit;
1877         }
1878         else if( pobj->type != ADDR_INTERFACE ) {
1879                 /* Parent object is not an interface */
1880                 ds = addressbook_find_datasource( addrbook.treeSelected );
1881                 iface = ds->interface;
1882                 if( ! iface->readOnly ) {
1883                         /* Folder or group */
1884                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
1885                                 menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1886                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1887                                 if( obj ) canEdit = TRUE;
1888                         }
1889                         /* Folder */
1890                         if( pobj->type == ADDR_ITEM_FOLDER ) {
1891                                 menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1892                                 if( obj ) canEdit = TRUE;
1893                         }
1894                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1895                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1896                         canDelete = canEdit;
1897                 }
1898                 if( iface->type == ADDR_IF_LDAP ) {
1899                         if( obj ) canBrowse = TRUE;
1900                         canEdit = TRUE;
1901                         canDelete = FALSE;
1902                 }
1903         }
1904         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
1905
1906         /* Disable edit or browse if more than one row selected */
1907         if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) {
1908                 canEdit = FALSE;
1909                 canBrowse = FALSE;
1910         }
1911
1912         /* Now go finalize menu items */
1913         menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
1914         menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
1915
1916         menu_set_sensitive( addrbook.list_factory, "/Cut",           canCut );
1917         menu_set_sensitive( addrbook.list_factory, "/Copy",          canCopy );
1918         menu_set_sensitive( addrbook.list_factory, "/Paste",         canPaste );
1919 /*      menu_set_sensitive( addrbook.list_factory, "/Paste Address", canPaste );*/
1920
1921         menu_set_sensitive( addrbook.list_factory, "/Mail To",       canCopy );
1922
1923         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
1924         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
1925         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
1926 /*      menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );*/
1927
1928         menu_set_sensitive( addrbook.tree_factory, "/Cut",             canCut );
1929         menu_set_sensitive( addrbook.tree_factory, "/Copy",            canCopy );
1930         menu_set_sensitive( addrbook.tree_factory, "/Paste",           canPaste );
1931
1932         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",    canEdit );
1933         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete",  canDelete );
1934         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To", canCopy );
1935
1936         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
1937         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1938
1939 #ifdef USE_LDAP
1940         menu_set_sensitive( addrbook.list_factory, "/Browse Entry",    canBrowse );
1941 #endif
1942 }
1943
1944 static void addressbook_select_row_tree (GtkCTree       *ctree,
1945                                          GtkCTreeNode   *node,
1946                                          gint            column,
1947                                          gpointer        data)
1948 {
1949 }
1950
1951 /**
1952  * Add list of items into tree node below specified tree node.
1953  * \param treeNode  Tree node.
1954  * \param ds        Data source.
1955  * \param listItems List of items.
1956  */
1957 static void addressbook_treenode_add_list(
1958         GtkCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
1959 {
1960         GList *node;
1961
1962         node = listItems;
1963         while( node ) {
1964                 AddrItemObject *aio;
1965                 GtkCTreeNode *nn;
1966
1967                 aio = node->data;
1968                 if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_GROUP ) {
1969                         ItemGroup *group;
1970
1971                         group = ( ItemGroup * ) aio;
1972                         nn = addressbook_node_add_group( treeNode, ds, group );
1973                 }
1974                 else if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_FOLDER ) {
1975                         ItemFolder *folder;
1976
1977                         folder = ( ItemFolder * ) aio;
1978                         nn = addressbook_node_add_folder(
1979                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
1980                 }
1981                 node = g_list_next( node );
1982         }
1983 }
1984
1985 static void addressbook_select_all_cb( void ) {
1986         gtk_clist_select_all(GTK_CLIST(addrbook.clist));
1987 }
1988
1989 /**
1990  * Cut from address list widget.
1991  */
1992 static void addressbook_clip_cut_cb( void ) {
1993         _clipBoard_->cutFlag = TRUE;
1994         addrclip_clear( _clipBoard_ );
1995         addrclip_add( _clipBoard_, _addressSelect_ );
1996         /* addrclip_list_show( _clipBoard_, stdout ); */
1997 }
1998
1999 /**
2000  * Copy from address list widget.
2001  */
2002 static void addressbook_clip_copy_cb( void ) {
2003         _clipBoard_->cutFlag = FALSE;
2004         addrclip_clear( _clipBoard_ );
2005         addrclip_add( _clipBoard_, _addressSelect_ );
2006         /* addrclip_list_show( _clipBoard_, stdout ); */
2007 }
2008
2009 /**
2010  * Paste clipboard into address list widget.
2011  */
2012 static void addressbook_clip_paste_cb( void ) {
2013         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2014         AddressObject *pobj = NULL;
2015         AddressDataSource *ds = NULL;
2016         AddressBookFile *abf = NULL;
2017         ItemFolder *folder = NULL;
2018         GList *folderGroup = NULL;
2019
2020         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2021         if( ds == NULL ) return;
2022         if( addrindex_ds_get_readonly( ds ) ) {
2023                 addressbook_ds_status_message(
2024                         ds, _( "Cannot paste. Target address book is readonly." ) );
2025                 return;
2026         }
2027
2028         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2029         if( pobj ) {
2030                 if( pobj->type == ADDR_ITEM_FOLDER ) {
2031                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
2032                 }
2033                 else if( pobj->type == ADDR_ITEM_GROUP ) {
2034                         addressbook_ds_status_message(
2035                                 ds, _( "Cannot paste into an address group." ) );
2036                         return;
2037                 }
2038         }
2039
2040         /* Get an address book */
2041         abf = addressbook_get_book_file();
2042         if( abf == NULL ) return;
2043
2044         if( _clipBoard_->cutFlag ) {
2045                 /* Paste/Cut */
2046                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
2047
2048                 /* Remove all groups and folders in clipboard from tree node */
2049                 addressbook_treenode_remove_item();
2050
2051                 /* Remove all "cut" items */
2052                 addrclip_delete_item( _clipBoard_ );
2053
2054                 /* Clear clipboard - cut items??? */
2055                 addrclip_clear( _clipBoard_ );
2056         }
2057         else {
2058                 /* Paste/Copy */
2059                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
2060         }
2061
2062         /* addrclip_list_show( _clipBoard_, stdout ); */
2063         if( folderGroup ) {
2064                 /* Update tree by inserting node for each folder or group */
2065                 addressbook_treenode_add_list(
2066                         addrbook.treeSelected, ds, folderGroup );
2067                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2068                 g_list_free( folderGroup );
2069                 folderGroup = NULL;
2070         }
2071
2072         /* Display items pasted */
2073         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened );
2074         addressbook_set_clist(
2075                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2076                         addrbook.opened),
2077                 TRUE);
2078         
2079
2080 }
2081
2082 /**
2083  * Add current treenode object to clipboard. Note that widget only allows
2084  * one entry from the tree list to be selected.
2085  */
2086 static void addressbook_treenode_to_clipboard( void ) {
2087         AddressObject *obj = NULL;
2088         AddressDataSource *ds = NULL;
2089         AddrSelectItem *item;
2090         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2091         GtkCTreeNode *node;
2092
2093         node = addrbook.treeSelected;
2094         if( node == NULL ) return;
2095         obj = gtk_ctree_node_get_row_data( ctree, node );
2096         if( obj == NULL ) return;
2097
2098         ds = addressbook_find_datasource( node );
2099         if( ds == NULL ) return;
2100
2101         item = NULL;
2102         if( obj->type == ADDR_ITEM_FOLDER ) {
2103                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2104                 ItemFolder *folder = adapter->itemFolder;
2105
2106                 item = addrselect_create_node( obj );
2107                 item->uid = g_strdup( ADDRITEM_ID(folder) );
2108         }
2109         else if( obj->type == ADDR_ITEM_GROUP ) {
2110                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2111                 ItemGroup *group = adapter->itemGroup;
2112
2113                 item = addrselect_create_node( obj );
2114                 item->uid = g_strdup( ADDRITEM_ID(group) );
2115         }
2116         else if( obj->type == ADDR_DATASOURCE ) {
2117                 /* Data source */
2118                 item = addrselect_create_node( obj );
2119                 item->uid = NULL;
2120         }
2121
2122         if( item ) {
2123                 /* Clear existing list and add item into list */
2124                 gchar *cacheID;
2125
2126                 addressbook_list_select_clear();
2127                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2128                 addrselect_list_add( _addressSelect_, item, cacheID );
2129                 g_free( cacheID );
2130         }
2131 }
2132
2133 /**
2134  * Cut from tree widget.
2135  */
2136 static void addressbook_treenode_cut_cb( void ) {
2137         _clipBoard_->cutFlag = TRUE;
2138         addressbook_treenode_to_clipboard();
2139         addrclip_clear( _clipBoard_ );
2140         addrclip_add( _clipBoard_, _addressSelect_ );
2141         /* addrclip_list_show( _clipBoard_, stdout ); */
2142 }
2143
2144 /**
2145  * Copy from tree widget.
2146  */
2147 static void addressbook_treenode_copy_cb( void ) {
2148         _clipBoard_->cutFlag = FALSE;
2149         addressbook_treenode_to_clipboard();
2150         addrclip_clear( _clipBoard_ );
2151         addrclip_add( _clipBoard_, _addressSelect_ );
2152         /* addrclip_list_show( _clipBoard_, stdout ); */
2153 }
2154
2155 /**
2156  * Paste clipboard into address tree widget.
2157  */
2158 static void addressbook_treenode_paste_cb( void ) {
2159         addressbook_clip_paste_cb();
2160 }
2161
2162 /**
2163  * Clear selected entries in clipboard.
2164  */
2165 static void addressbook_list_select_clear( void ) {
2166         addrselect_list_clear( _addressSelect_ );
2167 }
2168
2169 /**
2170  * Add specified address item to selected address list.
2171  * \param aio Address item object.
2172  * \param ds  Datasource.
2173  */
2174 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
2175         gchar *cacheID;
2176
2177         if( ds == NULL ) return;
2178         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2179         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
2180         g_free( cacheID );
2181 }
2182
2183 /**
2184  * Remove specified address item from selected address list.
2185  * \param aio Address item object.
2186  */
2187 static void addressbook_list_select_remove( AddrItemObject *aio ) {
2188         addrselect_list_remove( _addressSelect_, aio );
2189 }
2190
2191 /**
2192  * Invoke EMail compose window with addresses in selected address list.
2193  */
2194 static void addressbook_mail_to_cb( void ) {
2195         GList *listAddress;
2196
2197         if( ! addrselect_test_empty( _addressSelect_ ) ) {
2198                 listAddress = addrselect_build_list( _addressSelect_ );
2199                 compose_new_with_list( NULL, listAddress );
2200                 mgu_free_dlist( listAddress );
2201                 listAddress = NULL;
2202         }
2203 }
2204
2205 static void addressbook_list_row_selected( GtkCTree *clist,
2206                                            GtkCTreeNode *node,
2207                                            gint column,
2208                                            gpointer data )
2209 {
2210         GtkEntry *entry = GTK_ENTRY(addrbook.entry);
2211         AddrItemObject *aio = NULL;
2212         AddressObject *pobj = NULL;
2213         AdapterDSource *ads = NULL;
2214         AddressDataSource *ds = NULL;
2215
2216         gtk_entry_set_text( entry, "" );
2217         addrbook.listSelected = node;
2218
2219         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
2220         if( pobj == NULL ) return;
2221
2222         if( pobj->type == ADDR_DATASOURCE ) {
2223                 ads = ADAPTER_DSOURCE(pobj);
2224                 ds = ads->dataSource;
2225         }
2226         else if( pobj->type != ADDR_INTERFACE ) {
2227                 ds = addressbook_find_datasource( addrbook.treeSelected );
2228         }
2229
2230         aio = gtk_ctree_node_get_row_data( clist, node );
2231         if( aio ) {
2232                 /* printf( "list select: %d : '%s'\n", aio->type, aio->name ); */
2233                 addressbook_list_select_add( aio, ds );
2234         }
2235
2236         addressbook_list_menu_setup();
2237
2238         if (!addrbook.target_compose && !prefs_common.addressbook_use_editaddress_dialog)
2239                 addressbook_edit_address(NULL, 0, NULL, FALSE);
2240 }
2241
2242 static void addressbook_list_row_unselected( GtkCTree *ctree,
2243                                              GtkCTreeNode *node,
2244                                              gint column,
2245                                              gpointer data )
2246 {
2247         AddrItemObject *aio;
2248
2249         aio = gtk_ctree_node_get_row_data( ctree, node );
2250         if( aio != NULL ) {
2251                 /* printf( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
2252                 addressbook_list_select_remove( aio );
2253         }
2254
2255         if (!prefs_common.addressbook_use_editaddress_dialog)
2256                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
2257 }
2258
2259 /* from gdkevents.c */
2260 #define DOUBLE_CLICK_TIME 250
2261
2262 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
2263                                                 GdkEventButton *event,
2264                                                 gpointer data)
2265 {
2266         static guint32 lasttime = 0;
2267         if( ! event ) return FALSE;
2268
2269         addressbook_list_menu_setup();
2270
2271         if( event->button == 3 ) {
2272                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
2273                        event->button, event->time );
2274         } else if (event->button == 1) {
2275                 if (event->time - lasttime < DOUBLE_CLICK_TIME) {
2276                         if (prefs_common.add_address_by_click &&
2277                             addrbook.target_compose)
2278                                 addressbook_to_clicked(NULL, GINT_TO_POINTER(COMPOSE_TO));
2279                         else
2280                                 if (prefs_common.addressbook_use_editaddress_dialog)
2281                                 addressbook_edit_address_cb(NULL, 0, NULL);
2282
2283                         lasttime = 0;
2284                 } else
2285                         lasttime = event->time;
2286         }
2287
2288         return FALSE;
2289 }
2290
2291 static gboolean addressbook_list_button_released(GtkWidget *widget,
2292                                                  GdkEventButton *event,
2293                                                  gpointer data)
2294 {
2295         return FALSE;
2296 }
2297
2298 static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
2299                                                 GdkEventButton *event,
2300                                                 gpointer data)
2301 {
2302         GtkCList *clist = GTK_CLIST(ctree);
2303         gint row, column;
2304         AddressObject *obj = NULL;
2305         AdapterDSource *ads = NULL;
2306         AddressInterface *iface = NULL;
2307         AddressDataSource *ds = NULL;
2308         gboolean canEdit = FALSE;
2309         gboolean canDelete = FALSE;
2310         gboolean canCut = FALSE;
2311         gboolean canCopy = FALSE;
2312         gboolean canPaste = FALSE;
2313         gboolean canTreeCut = FALSE;
2314         gboolean canTreeCopy = FALSE;
2315         gboolean canTreePaste = FALSE;
2316         gboolean canLookup = FALSE;
2317         GtkCTreeNode *node = NULL;
2318
2319         if( ! event ) return FALSE;
2320         addressbook_menubar_set_sensitive( FALSE );
2321
2322         if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
2323                 gtkut_clist_set_focus_row(clist, row);
2324                 obj = gtk_clist_get_row_data( clist, row );
2325         }
2326
2327         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
2328
2329         if( obj == NULL ) return FALSE;
2330         node = gtk_ctree_node_nth(GTK_CTREE(clist), row);
2331
2332         if( ! addrclip_is_empty( _clipBoard_ ) ) {
2333                 canTreePaste = TRUE;
2334         }
2335
2336         if (obj->type == ADDR_INTERFACE) {
2337                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
2338                 iface = adapter->interface;
2339                 canEdit = FALSE;
2340                 canDelete = FALSE;
2341                 canTreeCopy = FALSE;
2342                 if( iface->readOnly ) {
2343                         canTreePaste = FALSE;
2344                 }
2345                 else {
2346                         menu_set_sensitive( addrbook.tree_factory, "/New Book", TRUE );
2347                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2348                 }
2349                 if( iface->externalQuery ) canLookup = TRUE;
2350         }
2351         if (obj->type == ADDR_DATASOURCE) {
2352                 ads = ADAPTER_DSOURCE(obj);
2353                 ds = ads->dataSource;
2354                 if (!ds)
2355                         goto just_set_sens;
2356                 iface = ds->interface;
2357                 if (!iface)
2358                         goto just_set_sens;
2359                 canEdit = TRUE;
2360                 canDelete = TRUE;
2361                 if( iface->readOnly ) {
2362                         canTreePaste = FALSE;
2363                 }
2364                 else {
2365                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2366                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2367                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2368                 }
2369                 canTreeCopy = TRUE;
2370                 if( iface->externalQuery ) canLookup = TRUE;
2371         }
2372         else if (obj->type == ADDR_ITEM_FOLDER) {
2373                 ds = addressbook_find_datasource( node );
2374                 if (!ds) {
2375                         goto just_set_sens;
2376                 }
2377                 iface = ds->interface;
2378                 if (!iface)
2379                         goto just_set_sens;
2380                 if( iface->readOnly ) {
2381                         canTreePaste = FALSE;
2382                 }
2383                 else {
2384                         canEdit = TRUE;
2385                         canDelete = TRUE;
2386                         canTreeCut = TRUE;
2387                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2388                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2389                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2390                 }
2391                 canTreeCopy = TRUE;
2392
2393                 if( iface->externalQuery ) {
2394                         /* Enable deletion of LDAP folder */
2395                         canLookup = TRUE;
2396                         canDelete = TRUE;
2397                 }
2398         }
2399         else if (obj->type == ADDR_ITEM_GROUP) {
2400                 ds = addressbook_find_datasource( node );
2401                 if (!ds)
2402                         goto just_set_sens;
2403                 iface = ds->interface;
2404                 if (!iface)
2405                         goto just_set_sens;
2406                 if( ! iface->readOnly ) {
2407                         canEdit = TRUE;
2408                         canDelete = TRUE;
2409                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
2410                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2411                 }
2412         }
2413         else if (obj->type == ADDR_INTERFACE) {
2414                 canTreePaste = FALSE;
2415         }
2416
2417         if( canEdit ) {
2418                 if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
2419         }
2420         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
2421         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
2422
2423 just_set_sens:
2424         /* Enable edit */
2425         menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
2426         menu_set_sensitive( addrbook.tree_factory, "/Delete", canDelete );
2427         menu_set_sensitive( addrbook.tree_factory, "/Cut",    canTreeCut );
2428         menu_set_sensitive( addrbook.tree_factory, "/Copy",   canTreeCopy );
2429         menu_set_sensitive( addrbook.tree_factory, "/Paste",  canTreePaste );
2430
2431         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",          canEdit );
2432         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book",        canEdit );
2433         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
2434         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
2435         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
2436 /*      menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );*/
2437
2438         addressbook_show_buttons(addrbook.target_compose == NULL, canLookup, addrbook.target_compose != NULL);
2439         if( event->button == 3 ) {
2440                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
2441                                event->button, event->time);
2442         }
2443
2444         return FALSE;
2445 }
2446
2447 static gboolean addressbook_tree_button_released(GtkWidget *ctree,
2448                                                  GdkEventButton *event,
2449                                                  gpointer data)
2450 {
2451         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree), addrbook.opened);
2452         return FALSE;
2453 }
2454
2455 static void addressbook_new_folder_cb(gpointer data, guint action,
2456                                       GtkWidget *widget)
2457 {
2458         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2459         AddressObject *obj = NULL;
2460         AddressDataSource *ds = NULL;
2461         AddressBookFile *abf = NULL;
2462         ItemFolder *parentFolder = NULL;
2463         ItemFolder *folder = NULL;
2464
2465         if( ! addrbook.treeSelected ) return;
2466         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2467         if( obj == NULL ) return;
2468         ds = addressbook_find_datasource( addrbook.treeSelected );
2469         if( ds == NULL ) return;
2470
2471         if( obj->type == ADDR_DATASOURCE ) {
2472                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2473         }
2474         else if( obj->type == ADDR_ITEM_FOLDER ) {
2475                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2476         }
2477         else {
2478                 return;
2479         }
2480
2481         abf = ds->rawDataSource;
2482         if( abf == NULL ) return;
2483         folder = addressbook_edit_folder( abf, parentFolder, NULL );
2484         if( folder ) {
2485                 GtkCTreeNode *nn;
2486                 nn = addressbook_node_add_folder(
2487                         addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
2488                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2489                 if( addrbook.treeSelected == addrbook.opened )
2490                         addressbook_set_clist(obj, TRUE);
2491         }
2492
2493 }
2494
2495 static void addressbook_new_group_cb(gpointer data, guint action,
2496                                      GtkWidget *widget)
2497 {
2498         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2499         AddressObject *obj = NULL;
2500         AddressDataSource *ds = NULL;
2501         AddressBookFile *abf = NULL;
2502         ItemFolder *parentFolder = NULL;
2503         ItemGroup *group = NULL;
2504
2505         if( ! addrbook.treeSelected ) return;
2506         obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
2507         if( obj == NULL ) return;
2508         ds = addressbook_find_datasource( addrbook.treeSelected );
2509         if( ds == NULL ) return;
2510
2511         if( obj->type == ADDR_DATASOURCE ) {
2512                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2513         }
2514         else if( obj->type == ADDR_ITEM_FOLDER ) {
2515                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2516         }
2517         else {
2518                 return;
2519         }
2520
2521         abf = ds->rawDataSource;
2522         if( abf == NULL ) return;
2523         group = addressbook_edit_group( abf, parentFolder, NULL );
2524         if( group ) {
2525                 GtkCTreeNode *nn;
2526                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
2527                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2528                 if( addrbook.treeSelected == addrbook.opened )
2529                         addressbook_set_clist(obj, TRUE);
2530         }
2531
2532 }
2533
2534 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
2535 {
2536         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2537         gchar *text[1];
2538         guint8 spacing;
2539         GdkPixmap *pix_cl, *pix_op;
2540         GdkBitmap *mask_cl, *mask_op;
2541         gboolean is_leaf, expanded;
2542
2543         gtk_ctree_get_node_info(ctree, node, text, &spacing,
2544                                 &pix_cl, &mask_cl, &pix_op, &mask_op,
2545                                 &is_leaf, &expanded);
2546         gtk_sctree_set_node_info(ctree, node, name, spacing,
2547                                 pix_cl, mask_cl, pix_op, mask_op,
2548                                 is_leaf, expanded);
2549 }
2550
2551 /**
2552  * Edit data source.
2553  * \param obj  Address object to edit.
2554  * \param node Node in tree.
2555  * \return New name of data source.
2556  */
2557 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
2558         gchar *newName = NULL;
2559         AddressDataSource *ds = NULL;
2560         AddressInterface *iface = NULL;
2561         AdapterDSource *ads = NULL;
2562
2563         ds = addressbook_find_datasource( node );
2564         if( ds == NULL ) return NULL;
2565         iface = ds->interface;
2566         if( ! iface->haveLibrary ) return NULL;
2567
2568         /* Read data from data source */
2569         if( addrindex_ds_get_modify_flag( ds ) ) {
2570                 addrindex_ds_read_data( ds );
2571         }
2572
2573         if( ! addrindex_ds_get_read_flag( ds ) ) {
2574                 addrindex_ds_read_data( ds );
2575         }
2576
2577         /* Handle edit */
2578         ads = ADAPTER_DSOURCE(obj);
2579         if( ads->subType == ADDR_BOOK ) {
2580                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
2581         }
2582         else if( ads->subType == ADDR_VCARD ) {
2583                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
2584         }
2585 #ifdef USE_JPILOT
2586         else if( ads->subType == ADDR_JPILOT ) {
2587                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
2588         }
2589 #endif
2590 #ifdef USE_LDAP
2591         else if( ads->subType == ADDR_LDAP ) {
2592                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
2593         }
2594 #endif
2595         else {
2596                 return NULL;
2597         }
2598         newName = obj->name;
2599         return newName;
2600 }
2601
2602 /*
2603 * Edit an object that is in the address tree area.
2604 */
2605 static void addressbook_treenode_edit_cb(gpointer data, guint action,
2606                                        GtkWidget *widget)
2607 {
2608         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2609         AddressObject *obj;
2610         AddressDataSource *ds = NULL;
2611         AddressBookFile *abf = NULL;
2612         GtkCTreeNode *node = NULL, *parentNode = NULL;
2613         gchar *name = NULL;
2614
2615         if( ! addrbook.treeSelected ) return;
2616         node = addrbook.treeSelected;
2617         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2618         obj = gtk_ctree_node_get_row_data( ctree, node );
2619         if( obj == NULL ) return;
2620         parentNode = GTK_CTREE_ROW(node)->parent;
2621
2622         ds = addressbook_find_datasource( node );
2623         if( ds == NULL ) return;
2624
2625         if( obj->type == ADDR_DATASOURCE ) {
2626                 name = addressbook_edit_datasource( obj, node );
2627                 if( name == NULL ) return;
2628         }
2629         else {
2630                 abf = ds->rawDataSource;
2631                 if( abf == NULL ) return;
2632                 if( obj->type == ADDR_ITEM_FOLDER ) {
2633                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2634                         ItemFolder *item = adapter->itemFolder;
2635                         ItemFolder *parentFolder = NULL;
2636                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2637                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2638                         name = ADDRITEM_NAME(item);
2639                 }
2640                 else if( obj->type == ADDR_ITEM_GROUP ) {
2641                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2642                         ItemGroup *item = adapter->itemGroup;
2643                         ItemFolder *parentFolder = NULL;
2644                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2645                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2646                         name = ADDRITEM_NAME(item);
2647                 }
2648         }
2649         if( name && parentNode ) {
2650                 /* Update node in tree view */
2651                 addressbook_change_node_name( node, name );
2652                 gtk_sctree_sort_node(ctree, parentNode);
2653                 gtk_ctree_expand( ctree, node );
2654                 gtk_sctree_select( GTK_SCTREE( ctree), node );
2655         }
2656 }
2657
2658 typedef enum {
2659         ADDRTREE_DEL_NONE,
2660         ADDRTREE_DEL_DATA,
2661         ADDRTREE_DEL_FOLDER_ONLY,
2662         ADDRTREE_DEL_FOLDER_ADDR
2663 } TreeItemDelType ;
2664
2665 /**
2666  * Delete an item from the tree widget.
2667  * \param data   Data passed in.
2668  * \param action Action.
2669  * \param widget Widget issuing callback.
2670  */
2671 static void addressbook_treenode_delete_cb(
2672                 gpointer data, guint action, GtkWidget *widget )
2673 {
2674         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2675         GtkCTreeNode *node = NULL;
2676         AddressObject *obj;
2677         gchar *message;
2678         AlertValue aval;
2679         AddrBookBase *adbase;
2680         AddressCache *cache;
2681         AdapterDSource *ads = NULL;
2682         AddressInterface *iface = NULL;
2683         AddressDataSource *ds = NULL;
2684         gboolean remFlag = FALSE;
2685         TreeItemDelType delType;
2686
2687         if( ! addrbook.treeSelected ) return;
2688         node = addrbook.treeSelected;
2689         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2690
2691         obj = gtk_ctree_node_get_row_data( ctree, node );
2692         g_return_if_fail(obj != NULL);
2693
2694         if( obj->type == ADDR_DATASOURCE ) {
2695                 ads = ADAPTER_DSOURCE(obj);
2696                 if( ads == NULL ) return;
2697                 ds = ads->dataSource;
2698                 if( ds == NULL ) return;
2699         }
2700         else {
2701                 /* Must be folder or something else */
2702                 ds = addressbook_find_datasource( node );
2703                 if( ds == NULL ) return;
2704
2705                 /* Only allow deletion from non-readOnly */
2706                 iface = ds->interface;
2707                 if( iface->readOnly ) {
2708                         /* Allow deletion of query results */
2709                         if( ! iface->externalQuery ) return;
2710                 }
2711         }
2712
2713         /* Confirm deletion */
2714         delType = ADDRTREE_DEL_NONE;
2715         if( obj->type == ADDR_ITEM_FOLDER ) {
2716                 if( iface->externalQuery ) {
2717                         message = g_strdup_printf( _(
2718                                 "Do you want to delete the query " \
2719                                 "results and addresses in '%s' ?" ),
2720                                 obj->name );
2721                         aval = alertpanel( _("Delete"), message,
2722                                 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
2723                         g_free(message);
2724                         if( aval == G_ALERTALTERNATE ) {
2725                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2726                         }
2727                 }
2728                 else {
2729                         message = g_strdup_printf
2730                                 ( _( "Do you want to delete '%s' ?"
2731                                      "If you delete the folder only, the addresses it contains will be moved into the parent folder." ),
2732                                  obj->name );
2733                         aval = alertpanel( _("Delete folder"), message,
2734                                 GTK_STOCK_CANCEL, _("+Delete _folder only"), _("Delete folder and _addresses"));
2735                         g_free(message);
2736                         if( aval == G_ALERTALTERNATE ) {
2737                                 delType = ADDRTREE_DEL_FOLDER_ONLY;
2738                         }
2739                         else if( aval == G_ALERTOTHER ) {
2740                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2741                         }
2742                 }
2743         }
2744         else if( obj->type == ADDR_ITEM_GROUP ) {
2745                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2746                                             "The addresses it contains will not be lost."), obj->name);
2747                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2748                                 "+" GTK_STOCK_DELETE, NULL);
2749                 g_free(message);
2750                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_FOLDER_ONLY;
2751         } else {
2752                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2753                                             "The addresses it contains will be lost."), obj->name);
2754                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2755                                 "+" GTK_STOCK_DELETE, NULL);
2756                 g_free(message);
2757                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_DATA;
2758         }
2759         if( delType == ADDRTREE_DEL_NONE ) return;
2760
2761         /* Proceed with deletion */
2762         if( obj->type == ADDR_DATASOURCE ) {
2763                 /* Remove node from tree */
2764                 gtk_ctree_remove_node( ctree, node );
2765         
2766                 /* Remove data source. */
2767                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
2768                         addrindex_free_datasource( ds );
2769                 }
2770                 return;
2771         }
2772
2773         /* Get reference to cache */
2774         adbase = ( AddrBookBase * ) ds->rawDataSource;
2775         if( adbase == NULL ) return;
2776         cache = adbase->addressCache;
2777
2778         /* Remove query results folder */
2779         if( iface->externalQuery ) {
2780                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2781                 ItemFolder *folder = adapter->itemFolder;
2782
2783                 adapter->itemFolder = NULL;
2784                 /*
2785                 printf( "remove folder for ::%s::\n", obj->name );
2786                 printf( "      folder name ::%s::\n", ADDRITEM_NAME(folder) );
2787                 printf( "-------------- remove results\n" );
2788                 */
2789                 addrindex_remove_results( ds, folder );
2790                 /* printf( "-------------- remove node\n" ); */
2791                 gtk_ctree_remove_node( ctree, node );
2792                 return;
2793         }
2794
2795         /* Code below is valid for regular address book deletion */
2796         if( obj->type == ADDR_ITEM_FOLDER ) {
2797                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2798                 ItemFolder *item = adapter->itemFolder;
2799
2800                 if( delType == ADDRTREE_DEL_FOLDER_ONLY ) {
2801                         /* Remove folder only */
2802                         item = addrcache_remove_folder( cache, item );
2803                         if( item ) {
2804                                 addritem_free_item_folder( item );
2805                                 addressbook_move_nodes_up( ctree, node );
2806                                 remFlag = TRUE;
2807                         }
2808                 }
2809                 else if( delType == ADDRTREE_DEL_FOLDER_ADDR ) {
2810                         /* Remove folder and addresses */
2811                         item = addrcache_remove_folder_delete( cache, item );
2812                         if( item ) {
2813                                 addritem_free_item_folder( item );
2814                                 remFlag = TRUE;
2815                         }
2816                 }
2817         }
2818         else if( obj->type == ADDR_ITEM_GROUP ) {
2819                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2820                 ItemGroup *item = adapter->itemGroup;
2821
2822                 item = addrcache_remove_group( cache, item );
2823                 if( item ) {
2824                         addritem_free_item_group( item );
2825                         remFlag = TRUE;
2826                 }
2827         }
2828
2829         if( remFlag ) {
2830                 /* Remove node. */
2831                 gtk_ctree_remove_node(ctree, node );
2832         }
2833 }
2834
2835 void addressbook_new_address_from_book_post_cb( ItemPerson *person )
2836 {
2837         if( person && addrbook.treeSelected == addrbook.opened ) {
2838                 gtk_clist_unselect_all( GTK_CLIST(addrbook.clist) );
2839                 addressbook_folder_refresh_one_person(
2840                         GTK_CTREE(addrbook.clist), person );
2841         }
2842         addressbook_address_list_set_focus();
2843 }
2844
2845 void addressbook_new_address_from_folder_post_cb( ItemPerson *person )
2846 {
2847         if( person && addrbook.treeSelected == addrbook.opened) {
2848                 gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
2849                 addressbook_set_clist(
2850                         gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2851                                 addrbook.opened),
2852                         TRUE);
2853         }
2854         addressbook_address_list_set_focus();
2855 }
2856
2857 static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
2858         AddressObject *pobj = NULL;
2859         AddressDataSource *ds = NULL;
2860         AddressBookFile *abf = NULL;
2861
2862         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
2863         if( pobj == NULL ) return;
2864         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2865         if( ds == NULL ) return;
2866
2867         abf = ds->rawDataSource;
2868         if( abf == NULL ) return;
2869
2870         if( pobj->type == ADDR_DATASOURCE ) {
2871                 if( ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ) {
2872                         /* New address */
2873                         ItemPerson *person = addressbook_edit_person( abf, NULL, NULL, FALSE,
2874                                                                                                                   addrbook.editaddress_vbox,
2875                                                                                                                   addressbook_new_address_from_book_post_cb,
2876                                                                                                                   TRUE );
2877                         if (prefs_common.addressbook_use_editaddress_dialog)
2878                                 addressbook_new_address_from_book_post_cb( person );
2879                 }
2880         }
2881         else if( pobj->type == ADDR_ITEM_FOLDER ) {
2882                 /* New address */
2883                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
2884                 ItemPerson *person = addressbook_edit_person( abf, folder, NULL, FALSE,
2885                                                                                                           addrbook.editaddress_vbox,
2886                                                                                                           addressbook_new_address_from_folder_post_cb,
2887                                                                                                           TRUE );
2888                 if (prefs_common.addressbook_use_editaddress_dialog)
2889                         addressbook_new_address_from_folder_post_cb( person );
2890         }
2891         else if( pobj->type == ADDR_ITEM_GROUP ) {
2892                 /* New address in group */
2893                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
2894                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
2895                 if (addrbook.treeSelected == addrbook.opened) {
2896                         /* Change node name in tree. */
2897                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
2898                         gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
2899                         addressbook_set_clist(
2900                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2901                                         addrbook.opened),
2902                                 TRUE);
2903                 }
2904         }
2905 }
2906
2907 /**
2908  * Search for specified child group node in address index tree.
2909  * \param parent Parent node.
2910  * \param group  Group to find.
2911  */
2912 static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
2913         GtkCTreeNode *node = NULL;
2914         GtkCTreeRow *currRow;
2915
2916         currRow = GTK_CTREE_ROW( parent );
2917         if( currRow ) {
2918                 node = currRow->children;
2919                 while( node ) {
2920                         AddressObject *obj;
2921
2922                         obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
2923                         if( obj->type == ADDR_ITEM_GROUP ) {
2924                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
2925                                 if( g == group ) return node;
2926                         }
2927                         currRow = GTK_CTREE_ROW(node);
2928                         node = currRow->sibling;
2929                 }
2930         }
2931         return NULL;
2932 }
2933
2934 static AddressBookFile *addressbook_get_book_file() {
2935         AddressBookFile *abf = NULL;
2936         AddressDataSource *ds = NULL;
2937
2938         ds = addressbook_find_datasource( addrbook.treeSelected );
2939         if( ds == NULL ) return NULL;
2940         if( ds->type == ADDR_IF_BOOK ) abf = ds->rawDataSource;
2941         return abf;
2942 }
2943
2944 static void addressbook_tree_remove_children( GtkCTree *ctree, GtkCTreeNode *parent ) {
2945         GtkCTreeNode *node;
2946         GtkCTreeRow *row;
2947
2948         /* Remove existing folders and groups */
2949         row = GTK_CTREE_ROW( parent );
2950         if( row ) {
2951                 while( (node = row->children) ) {
2952                         gtk_ctree_remove_node( ctree, node );
2953                 }
2954         }
2955 }
2956
2957 static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
2958         GtkCTreeNode *parent, *child;
2959         GtkCTreeRow *currRow;
2960         currRow = GTK_CTREE_ROW( node );
2961         if( currRow ) {
2962                 parent = currRow->parent;
2963                 while( (child = currRow->children) ) {
2964                         gtk_ctree_move( ctree, child, parent, node );
2965                 }
2966                 gtk_sctree_sort_node( ctree, parent );
2967         }
2968 }
2969
2970 void addressbook_edit_address_post_cb( ItemPerson *person )
2971 {
2972         if( person ) {
2973                 addressbook_folder_refresh_one_person( GTK_CTREE(addrbook.clist), person );
2974                 invalidate_address_completion();
2975         }
2976         addressbook_address_list_set_focus();
2977 }
2978
2979 void addressbook_address_list_set_focus( void )
2980 {
2981         if (!prefs_common.addressbook_use_editaddress_dialog) {
2982                 gtk_window_set_focus(GTK_WINDOW(addrbook.window), addrbook.clist);
2983                 addressbook_list_menu_setup();
2984         }
2985 }
2986
2987 void addressbook_address_list_disable_some_actions(void)
2988 {
2989         /* disable address copy/pasting when editing contact's detail (embedded form) */
2990         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",   FALSE );
2991         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",  FALSE );
2992         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste", FALSE );
2993
2994         /* we're already editing contact's detail here */
2995         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",  FALSE );
2996         gtk_widget_set_sensitive( addrbook.edit_btn, FALSE );
2997 }
2998
2999 static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget *widget ) {
3000         addressbook_edit_address(data, action, widget, TRUE);
3001 }
3002         
3003 static void addressbook_edit_address( gpointer data, guint action, GtkWidget *widget,
3004                                                                           gboolean force_focus ) {
3005         GtkCTree *clist = GTK_CTREE(addrbook.clist);
3006         GtkCTree *ctree;
3007         AddressObject *obj = NULL, *pobj = NULL;
3008         AddressDataSource *ds = NULL;
3009         GtkCTreeNode *node = NULL, *parentNode = NULL;
3010         gchar *name = NULL;
3011         AddressBookFile *abf = NULL;
3012
3013         if( addrbook.listSelected == NULL ) return;
3014         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
3015         g_return_if_fail(obj != NULL);
3016
3017         ctree = GTK_CTREE( addrbook.ctree );
3018         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
3019         node = gtk_ctree_find_by_row_data( ctree, addrbook.treeSelected, obj );
3020
3021         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3022         if( ds == NULL ) return;
3023
3024         abf = addressbook_get_book_file();
3025         
3026         if( obj->type == ADDR_ITEM_EMAIL ) {
3027                 ItemEMail *email = ( ItemEMail * ) obj;
3028                 if( email == NULL ) return;
3029                 if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
3030                         /* Edit parent group */
3031                         AdapterGroup *adapter = ADAPTER_GROUP(pobj);
3032                         ItemGroup *itemGrp = adapter->itemGroup;
3033                         if( abf == NULL ) return;
3034                         if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3035                         name = ADDRITEM_NAME(itemGrp);
3036                         node = addrbook.treeSelected;
3037                         parentNode = GTK_CTREE_ROW(node)->parent;
3038                 }
3039                 else {
3040                         /* Edit person - email page */
3041                         ItemPerson *person;
3042                         person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3043                         if  ( addressbook_edit_person( abf, NULL, person, TRUE, addrbook.editaddress_vbox,
3044                                                                                    addressbook_edit_address_post_cb,
3045                                                                                    (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3046                                   != NULL ) {
3047                                 if (prefs_common.addressbook_use_editaddress_dialog)
3048                                         addressbook_edit_address_post_cb( person );
3049                         }
3050                         return;
3051                 }
3052         }
3053         else if( obj->type == ADDR_ITEM_PERSON ) {
3054                 /* Edit person - basic page */
3055                 ItemPerson *person = ( ItemPerson * ) obj;
3056                 if( addressbook_edit_person( abf, NULL, person, FALSE, addrbook.editaddress_vbox,
3057                                                                           addressbook_edit_address_post_cb,
3058                                                                           (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3059                         != NULL ) {
3060                         if (prefs_common.addressbook_use_editaddress_dialog)
3061                                 addressbook_edit_address_post_cb( person );
3062                 }
3063                 return;
3064         }
3065         else if( obj->type == ADDR_ITEM_GROUP ) {
3066                 ItemGroup *itemGrp = ( ItemGroup * ) obj;
3067                 if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3068                 parentNode = addrbook.treeSelected;
3069                 node = addressbook_find_group_node( parentNode, itemGrp );
3070                 name = ADDRITEM_NAME(itemGrp);
3071                 invalidate_address_completion();
3072         }
3073         else {
3074                 return;
3075         }
3076
3077         /* Update tree node with node name */
3078         if( node == NULL ) return;
3079         addressbook_change_node_name( node, name );
3080         gtk_sctree_sort_node( ctree, parentNode );
3081         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened ); 
3082         addressbook_set_clist(
3083                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
3084                         addrbook.opened),
3085                 TRUE);
3086 }
3087
3088 static void addressbook_delete_address_cb(gpointer data, guint action,
3089                                           GtkWidget *widget)
3090 {
3091         gtk_clist_freeze(GTK_CLIST(addrbook.clist));
3092         addressbook_del_clicked(NULL, NULL);
3093         gtk_clist_thaw(GTK_CLIST(addrbook.clist));
3094 }
3095
3096 static void close_cb(gpointer data, guint action, GtkWidget *widget)
3097 {
3098         addressbook_close();
3099 }
3100
3101 static void addressbook_file_save_cb( gpointer data, guint action, GtkWidget *widget ) {
3102         addressbook_export_to_file();
3103 }
3104
3105 static void addressbook_person_expand_node( GtkCTree *ctree, GList *node, gpointer *data ) {
3106         if( node ) {
3107                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
3108                 if( person ) addritem_person_set_opened( person, TRUE );
3109         }
3110 }
3111
3112 static void addressbook_person_collapse_node( GtkCTree *ctree, GList *node, gpointer *data ) {
3113         if( node ) {
3114                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
3115                 if( person ) addritem_person_set_opened( person, FALSE );
3116         }
3117 }
3118
3119 static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
3120         gchar *str = NULL;
3121         gchar *eMailAlias = ADDRITEM_NAME(email);
3122         if( eMailAlias && *eMailAlias != '\0' ) {
3123                 if( person ) {
3124                         str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
3125                 }
3126                 else {
3127                         str = g_strdup( eMailAlias );
3128                 }
3129         }
3130         return str;
3131 }
3132
3133 static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
3134         GList *items = itemGroup->listEMail;
3135         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3136         for( ; items != NULL; items = g_list_next( items ) ) {
3137                 GtkCTreeNode *nodeEMail = NULL;
3138                 gchar *text[N_LIST_COLS];
3139                 ItemEMail *email = items->data;
3140                 ItemPerson *person;
3141                 gchar *str = NULL;
3142
3143                 if( ! email ) continue;
3144
3145                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3146                 str = addressbook_format_item_clist( person, email );
3147                 if( str ) {
3148                         text[COL_NAME] = str;
3149                 }
3150                 else {
3151                         text[COL_NAME] = ADDRITEM_NAME(person);
3152                 }
3153                 text[COL_ADDRESS] = email->address;
3154                 text[COL_REMARKS] = email->remarks;
3155                 nodeEMail = gtk_sctree_insert_node(
3156                                 clist, NULL, NULL,
3157                                 text, FOLDER_SPACING,
3158                                 atci->iconXpm, atci->maskXpm,
3159                                 atci->iconXpmOpen, atci->maskXpmOpen,
3160                                 FALSE, FALSE );
3161                 gtk_ctree_node_set_row_data( clist, nodeEMail, email );
3162                 g_free( str );
3163                 str = NULL;
3164         }
3165 }
3166
3167 static void addressbook_folder_load_one_person(
3168                 GtkCTree *clist, ItemPerson *person,
3169                 AddressTypeControlItem *atci,
3170                 AddressTypeControlItem *atciMail )
3171 {
3172         GtkCTreeNode *nodePerson = NULL;
3173         GtkCTreeNode *nodeEMail = NULL;
3174         gchar *text[N_LIST_COLS];
3175         gboolean flgFirst = TRUE, haveAddr = FALSE;
3176         GList *node;
3177
3178         if( person == NULL ) return;
3179
3180         text[COL_NAME] = "";
3181         node = person->listEMail;
3182         while( node ) {
3183                 ItemEMail *email = node->data;
3184                 gchar *eMailAddr = NULL;
3185                 node = g_list_next( node );
3186
3187                 text[COL_ADDRESS] = email->address;
3188                 text[COL_REMARKS] = email->remarks;
3189                 eMailAddr = ADDRITEM_NAME(email);
3190                 if( eMailAddr && *eMailAddr == '\0' ) eMailAddr = NULL;
3191                 if( flgFirst ) {
3192                         /* First email belongs with person */
3193                         gchar *str = addressbook_format_item_clist( person, email );
3194                         if( str ) {
3195                                 text[COL_NAME] = str;
3196                         }
3197                         else {
3198                                 text[COL_NAME] = ADDRITEM_NAME(person);
3199                         }
3200                         nodePerson = gtk_sctree_insert_node(
3201                                         clist, NULL, NULL,
3202                                         text, FOLDER_SPACING,
3203                                         atci->iconXpm, atci->maskXpm,
3204                                         atci->iconXpmOpen, atci->maskXpmOpen,
3205                                         FALSE, person->isOpened );
3206                         g_free( str );
3207                         str = NULL;
3208                         gtk_ctree_node_set_row_data(clist, nodePerson, person );
3209                 }
3210                 else {
3211                         /* Subsequent email is a child node of person */
3212                         text[COL_NAME] = ADDRITEM_NAME(email);
3213                         nodeEMail = gtk_sctree_insert_node(
3214                                         clist, nodePerson, NULL,
3215                                         text, FOLDER_SPACING,
3216                                         atciMail->iconXpm, atciMail->maskXpm,
3217                                         atciMail->iconXpmOpen, atciMail->maskXpmOpen,
3218                                         FALSE, TRUE );
3219                         gtk_ctree_node_set_row_data(clist, nodeEMail, email );
3220                 }
3221                 flgFirst = FALSE;
3222                 haveAddr = TRUE;
3223         }
3224         if( ! haveAddr ) {
3225                 /* Have name without EMail */
3226                 text[COL_NAME] = ADDRITEM_NAME(person);
3227                 text[COL_ADDRESS] = "";
3228                 text[COL_REMARKS] = "";
3229                 nodePerson = gtk_sctree_insert_node(
3230                                 clist, NULL, NULL,
3231                                 text, FOLDER_SPACING,
3232                                 atci->iconXpm, atci->maskXpm,
3233                                 atci->iconXpmOpen, atci->maskXpmOpen,
3234                                 FALSE, person->isOpened );
3235                 gtk_ctree_node_set_row_data(clist, nodePerson, person );
3236         }
3237         return;
3238 }
3239
3240 static void addressbook_folder_load_person( GtkCTree *clist, ItemFolder *itemFolder ) {
3241         GList *items;
3242         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
3243         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3244
3245         if( atci == NULL ) return;
3246         if( atciMail == NULL ) return;
3247
3248         /* Load email addresses */
3249         items = addritem_folder_get_person_list( itemFolder );
3250         for( ; items != NULL; items = g_list_next( items ) ) {
3251                 addressbook_folder_load_one_person( clist, items->data, atci, atciMail );
3252         }
3253         /* Free up the list */
3254         mgu_clear_list( items );
3255         g_list_free( items );
3256 }
3257
3258 static void addressbook_folder_remove_node( GtkCTree *clist, GtkCTreeNode *node ) { 
3259         addrbook.listSelected = NULL;
3260         gtk_ctree_remove_node( clist, node );
3261         addressbook_menubar_set_sensitive( FALSE );
3262         addressbook_menuitem_set_sensitive(
3263                 gtk_ctree_node_get_row_data(
3264                         GTK_CTREE(clist), addrbook.treeSelected ),