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