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