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