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