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