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