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