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