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