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