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