New address book.
authorMatch Grun <match@dimensional.com>
Sat, 1 Sep 2001 23:48:06 +0000 (23:48 +0000)
committerMatch Grun <match@dimensional.com>
Sat, 1 Sep 2001 23:48:06 +0000 (23:48 +0000)
39 files changed:
src/Makefile.am
src/addrbook.c [new file with mode: 0644]
src/addrbook.h [new file with mode: 0644]
src/addrcache.c [new file with mode: 0644]
src/addrcache.h [new file with mode: 0644]
src/addressadd.c [new file with mode: 0644]
src/addressadd.h [new file with mode: 0644]
src/addressbook.c
src/addressbook.h
src/addressitem.h
src/addrindex.c [new file with mode: 0644]
src/addrindex.h [new file with mode: 0644]
src/addritem.c [new file with mode: 0644]
src/addritem.h [new file with mode: 0644]
src/editaddress.c [new file with mode: 0644]
src/editaddress.h [new file with mode: 0644]
src/editbook.c [new file with mode: 0644]
src/editbook.h [new file with mode: 0644]
src/editgroup.c [new file with mode: 0644]
src/editgroup.h [new file with mode: 0644]
src/editjpilot.c
src/editjpilot.h
src/editldap.c
src/editldap.h
src/editldap_basedn.c
src/editvcard.c
src/editvcard.h
src/jpilot.c
src/jpilot.h
src/main.c
src/mgutils.c
src/mgutils.h
src/procmime.c
src/summaryview.c
src/syldap.c
src/syldap.h
src/textview.c
src/vcard.c
src/vcard.h

index 413637a..823f87f 100644 (file)
@@ -93,14 +93,22 @@ sylpheed_SOURCES = \
        quote_fmt_lex.l quote_fmt_lex.h \
        quote_fmt_parse.y quote_fmt.h \
        addressitem.h \
+       addritem.c addritem.h \
+       addrcache.c addrcache.h \
+       addrbook.c addrbook.h \
+       addrindex.c addrindex.h \
        mgutils.c mgutils.h \
        vcard.c vcard.h \
        jpilot.c jpilot.h \
        syldap.c syldap.h \
+       editbook.c editbook.h \
+       editgroup.c editgroup.h \
+       editaddress.c editaddress.h \
        editvcard.c editvcard.h \
        editjpilot.c editjpilot.h \
        editldap.c editldap.h \
        editldap_basedn.c editldap_basedn.h \
+       addressadd.c addressadd.h \
        gtkspell.c gtkspell.h gtkxtext.h \
        template.c template.h \
        prefs_templates.c prefs_templates.h \
@@ -151,6 +159,9 @@ EXTRA_DIST = \
        pixmaps/stock_paste.xpm \
        pixmaps/tb_address_book.xpm \
        pixmaps/sylpheed-logo.xpm \
+       pixmaps/interface.xpm \
+       pixmaps/book.xpm \
+       pixmaps/address.xpm \
        pixmaps/vcard.xpm \
        pixmaps/jpilot.xpm \
        pixmaps/category.xpm \
diff --git a/src/addrbook.c b/src/addrbook.c
new file mode 100644 (file)
index 0000000..8e9098e
--- /dev/null
@@ -0,0 +1,2114 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001 Match Grun
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * General functions for accessing external address book files.
+ */
+
+#include <stdio.h>
+#include <glib.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <math.h>
+#include <setjmp.h>
+
+#include "xml.h"
+#include "mgutils.h"
+#include "addritem.h"
+#include "addrcache.h"
+#include "addrbook.h"
+
+#ifndef DEV_STANDALONE
+#include "prefs.h"
+#endif
+
+#define ADDRBOOK_MAX_SEARCH_COUNT 1000
+#define ADDRBOOK_PREFIX           "addrbook-"
+#define ADDRBOOK_SUFFIX           ".xml"
+#define FILE_NUMDIGITS            6
+
+#define ID_TIME_OFFSET            998000000
+/*
+* Create new address book.
+*/
+AddressBookFile *addrbook_create_book() {
+       AddressBookFile *book;
+       gint t;
+       book = g_new0( AddressBookFile, 1 );
+       book->name = NULL;
+       book->path = NULL;
+       book->fileName = NULL;
+       book->retVal = MGU_SUCCESS;
+       book->addressCache = addrcache_create();
+
+       book->tempList = NULL;
+       book->readFlag = FALSE;
+       book->dirtyFlag = FALSE;
+       book->modifyFlag = TRUE;
+       book->accessFlag = FALSE;
+       book->tempHash = NULL;
+       return book;
+}
+
+/*
+* Specify name to be used.
+*/
+void addrbook_set_name( AddressBookFile *book, const gchar *value ) {
+       g_return_if_fail( book != NULL );
+       book->name = mgu_replace_string( book->name, value );
+}
+void addrbook_set_path( AddressBookFile *book, const gchar *value ) {
+       g_return_if_fail( book != NULL );
+       book->path = mgu_replace_string( book->path, value );
+       book->dirtyFlag = TRUE;
+}
+void addrbook_set_file( AddressBookFile *book, const gchar *value ) {
+       g_return_if_fail( book != NULL );
+       book->fileName = mgu_replace_string( book->fileName, value );
+       book->dirtyFlag = TRUE;
+}
+void addrbook_set_accessed( AddressBookFile *book, const gboolean value ) {
+       g_return_if_fail( book != NULL );
+       book->accessFlag = value;
+}
+gboolean addrbook_get_modified( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return book->modifyFlag;
+}
+gboolean addrbook_get_accessed( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return book->accessFlag;
+}
+gboolean addrbook_get_read_flag( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return book->readFlag;
+}
+gint addrbook_get_status( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return book->retVal;
+}
+ItemFolder *addrbook_get_root_folder( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return addrcache_get_root_folder( book->addressCache );
+}
+GList *addrbook_get_list_folder( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return addrcache_get_list_folder( book->addressCache );
+}
+GList *addrbook_get_list_person( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return addrcache_get_list_person( book->addressCache );
+}
+gchar *addrbook_get_name( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return book->name;
+}
+
+static gint addrcache_free_item_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+               addritem_free_item_person( ( ItemPerson * ) obj );
+       }
+       else if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+               addritem_free_item_group( ( ItemGroup * ) obj );
+       }
+       else if( ADDRITEM_TYPE(obj) == ITEMTYPE_FOLDER ) {
+               addritem_free_item_folder( ( ItemFolder * ) obj );
+       }
+       key = NULL;
+       value = NULL;
+       return 0;
+}
+
+/*
+* Free hash table of address book items.
+*/
+static void addrcache_free_item_hash( GHashTable *table ) {
+       g_return_if_fail( table != NULL );
+       g_hash_table_freeze( table );
+       g_hash_table_foreach_remove( table, addrcache_free_item_vis, NULL );
+       g_hash_table_thaw( table );
+       g_hash_table_destroy( table );
+}
+
+/*
+* Empty address book.
+*/
+void addrbook_empty_book( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+
+       // Free up folders and hash table
+       addrcache_clear( book->addressCache );
+
+       g_list_free( book->tempList );
+       book->tempList = NULL;
+
+       // Reset to initial state
+       book->retVal = MGU_SUCCESS;
+       book->tempHash = NULL;
+       book->readFlag = FALSE;
+       book->dirtyFlag = FALSE;
+       book->modifyFlag = FALSE;
+       book->accessFlag = FALSE;
+}
+
+/*
+* Free address book.
+*/
+void addrbook_free_book( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+
+       g_free( book->name );
+       g_free( book->path );
+       g_free( book->fileName );
+       book->name = NULL;
+       book->path = NULL;
+       book->fileName = NULL;
+
+       // Free up folders and hash table
+       addrcache_free( book->addressCache );
+       book->addressCache = NULL;
+
+       g_list_free( book->tempList );
+       book->tempList = NULL;
+
+       book->retVal = MGU_SUCCESS;
+       book->tempHash = NULL;
+       book->readFlag = FALSE;
+       book->dirtyFlag = FALSE;
+       book->modifyFlag = FALSE;
+       book->accessFlag = FALSE;
+
+       g_free( book );
+}
+
+/*
+* Print list of items.
+*/
+void addrbook_print_item_list( GList *list, FILE *stream ) {
+       GList *node = list;
+       while( node ) {
+               AddrItemObject *obj = node->data;
+               if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+                       addritem_print_item_person( ( ItemPerson * ) obj, stream );
+               }
+               else if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+                       addritem_print_item_group( ( ItemGroup * ) obj, stream );
+               }
+               else if( ADDRITEM_TYPE(obj) == ITEMTYPE_FOLDER ) {
+                       addritem_print_item_folder( ( ItemFolder * ) obj, stream );
+               }
+               node = g_list_next( node );
+       }
+       fprintf( stream, "\t---\n" );
+}
+
+/*
+* Print address book.
+*/
+void addrbook_print_book( AddressBookFile *book, FILE *stream ) {
+       g_return_if_fail( book != NULL );
+       fprintf( stream, "AddressBook:\n" );
+       fprintf( stream, "\tname  : '%s'\n", book->name );
+       fprintf( stream, "\tpath  : '%s'\n", book->path );
+       fprintf( stream, "\tfile  : '%s'\n", book->fileName );
+       fprintf( stream, "\tstatus: %d : '%s'\n", book->retVal, mgu_error2string( book->retVal ) );
+       addrcache_print( book->addressCache, stream );
+}
+
+/*
+* Dump entire address book traversing folders.
+*/
+void addrbook_dump_book( AddressBookFile *book, FILE *stream ) {
+       GList *node;
+       ItemFolder *folder;
+       g_return_if_fail( book != NULL );
+       addrbook_print_book( book, stream );
+       folder = book->addressCache->rootFolder;
+       addritem_print_item_folder( folder, stream );
+}
+
+/*
+* Remove group from address book.
+* param: group Group to remove.
+* return: Group, or NULL if not found. Note that object should still be freed.
+*/
+ItemGroup *addrbook_remove_group( AddressBookFile *book, ItemGroup *group ) {
+       ItemGroup *item;
+       g_return_if_fail( book != NULL );
+       item = addrcache_remove_group( book->addressCache, group );
+       if( item ) book->dirtyFlag = TRUE;
+       return item;
+}
+
+/*
+* Remove specified person from address book.
+* param: person        Person to remove.
+* return: Person, or NULL if not found. Note that object should still be freed.
+*/
+ItemPerson *addrbook_remove_person( AddressBookFile *book, ItemPerson *person ) {
+       ItemPerson *item;
+       g_return_if_fail( book != NULL );
+       item = addrcache_remove_person( book->addressCache, person );
+       if( item ) book->dirtyFlag = TRUE;
+       return item;
+}
+
+/*
+* Remove email address in address book for specified person.
+* param: person        Person.
+*        email EMail to remove.
+* return: EMail object, or NULL if not found. Note that object should still be freed.
+*/
+ItemEMail *addrbook_person_remove_email( AddressBookFile *book, ItemPerson *person, ItemEMail *email ) {
+       ItemEMail *item;
+       g_return_if_fail( book != NULL );
+       item = addrcache_person_remove_email( book->addressCache, person, email );
+       if( item ); book->dirtyFlag = TRUE;
+       return item;
+}
+
+/* **********************************************************************
+* Read/Write XML data file...
+* ===========================
+* Notes:
+* 1)   The address book is structured as follows:
+*
+*              address-book
+*                      person
+*                              address-list
+*                                      address
+*                              attribute-list
+*                                      attribute
+*                      group
+*                              member-list
+*                                      member
+*                      folder
+*                              item-list
+*                                      item
+*
+* 2)   This sequence of elements was chosen so that the most important
+*      elements (person and their email addresses) appear first.
+*
+* 3)   Groups then appear. When groups are loaded, person's email
+*      addresses have already been loaded and can be found.
+*
+* 4)   Finally folders are loaded. Any forward and backward references
+*      to folders, groups and persons in the folders are resolved after
+*      loading.
+*
+* ***********************************************************************
+*/
+
+// Element tag names
+#define AB_ELTAG_ADDRESS         "address"
+#define AB_ELTAG_ATTRIBUTE       "attribute"
+#define AB_ELTAG_ATTRIBUTE_LIST  "attribute-list"
+#define AB_ELTAG_ADDRESS_LIST    "address-list"
+#define AB_ELTAG_MEMBER          "member"
+#define AB_ELTAG_MEMBER_LIST     "member-list"
+#define AB_ELTAG_ITEM            "item"
+#define AB_ELTAG_ITEM_LIST       "item-list"
+#define AB_ELTAG_ADDRESS_BOOK    "address-book"
+#define AB_ELTAG_PERSON          "person"
+#define AB_ELTAG_GROUP           "group"
+#define AB_ELTAG_FOLDER          "folder"
+
+// Attribute tag names
+#define AB_ATTAG_TYPE            "type"
+#define AB_ATTAG_UID             "uid"
+#define AB_ATTAG_NAME            "name"
+#define AB_ATTAG_REMARKS         "remarks"
+#define AB_ATTAG_FIRST_NAME      "first-name"
+#define AB_ATTAG_LAST_NAME       "last-name"
+#define AB_ATTAG_NICK_NAME       "nick-name"
+#define AB_ATTAG_COMMON_NAME     "cn"
+#define AB_ATTAG_ALIAS           "alias"
+#define AB_ATTAG_EMAIL           "email"
+#define AB_ATTAG_EID             "eid"
+#define AB_ATTAG_PID             "pid"
+
+// Attribute values
+#define AB_ATTAG_VAL_PERSON      "person"
+#define AB_ATTAG_VAL_GROUP       "group"
+#define AB_ATTAG_VAL_FOLDER      "folder"
+
+/*
+* Parse address item for person.
+*/
+static void addrbook_parse_address( AddressBookFile *book, XMLFile *file, ItemPerson *person ) {
+       GList *attr;
+       gchar *name, *value;
+       ItemEMail *email = NULL;
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( ! email ) email = addritem_create_item_email();
+               if( strcmp( name, AB_ATTAG_UID ) == 0 ) {
+                       ADDRITEM_ID(email) = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_ALIAS ) == 0 ) {
+                       ADDRITEM_NAME(email) = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_EMAIL ) == 0 ) {
+                       email->address = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_REMARKS ) == 0 ) {
+                       email->remarks = g_strdup( value );
+               }
+               attr = g_list_next( attr );
+       }
+       if( email ) {
+               if( person ) {
+                       addrcache_person_add_email( book->addressCache, person, email );
+               }
+               else {
+                       addritem_free_item_email( email );
+                       email = NULL;
+               }
+       }
+}
+
+/*
+* Parse email address list.
+*/
+static void addrbook_parse_addr_list( AddressBookFile *book, XMLFile *file, ItemPerson *person ){
+       GList *attr;
+       guint prev_level;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               if( xml_compare_tag( file, AB_ELTAG_ADDRESS ) ) {
+                       attr = xml_get_current_tag_attr(file);
+                       addrbook_parse_address( book, file, person );
+                       addrbook_parse_addr_list( book, file, person );
+               }
+       }
+}
+
+/*
+* Parse attibute for person.
+*/
+static void addrbook_parse_attribute( XMLFile *file, ItemPerson *person ) {
+       GList *attr;
+       gchar *name, *value;
+       gchar *element;
+       UserAttribute *uAttr = NULL;
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( ! uAttr ) uAttr = addritem_create_attribute();
+               if( strcmp( name, AB_ATTAG_UID ) == 0 ) {
+                       addritem_attrib_set_id( uAttr, value );
+               }
+               else if( strcmp( name, AB_ATTAG_NAME ) == 0 ) {
+                       addritem_attrib_set_name( uAttr, value );
+               }
+               attr = g_list_next( attr );
+       }
+
+       element = xml_get_element( file );
+       addritem_attrib_set_value( uAttr, element );
+
+       if( uAttr ) {
+               if( person ) {
+                       addritem_person_add_attribute( person, uAttr );
+               }
+               else {
+                       addritem_free_attribute( uAttr );
+                       uAttr = NULL;
+               }
+       }
+}
+
+/*
+* Parse attribute list.
+*/
+static void addrbook_parse_attr_list( AddressBookFile *book, XMLFile *file, ItemPerson *person ){
+       GList *attr;
+       guint prev_level;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               if( xml_compare_tag( file, AB_ELTAG_ATTRIBUTE ) ) {
+                       attr = xml_get_current_tag_attr(file);
+                       addrbook_parse_attribute( file, person );
+                       addrbook_parse_attr_list( book, file, person );
+               }
+       }
+}
+
+/*
+* Parse person.
+*/
+static void addrbook_parse_person( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *name, *value;
+       ItemPerson *person = NULL;
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( ! person ) person = addritem_create_item_person();
+               if( strcmp( name, AB_ATTAG_UID ) == 0 ) {
+                       ADDRITEM_ID(person) = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_FIRST_NAME ) == 0 ) {
+                       person->firstName = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_LAST_NAME ) == 0 ) {
+                       person->lastName = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_NICK_NAME ) == 0 ) {
+                       person->nickName = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_COMMON_NAME ) == 0 ) {
+                       ADDRITEM_NAME(person) = g_strdup( value );
+               }
+               attr = g_list_next( attr );
+       }
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_ADDRESS_LIST ) ) {
+               addrbook_parse_addr_list( book, file, person );
+               if( person ) {
+                       addrcache_hash_add_person( book->addressCache, person );
+               }
+       }
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_ATTRIBUTE_LIST ) ) {
+               addrbook_parse_attr_list( book, file, person );
+       }
+}
+
+/*
+* Parse group member.
+*/
+static void addrbook_parse_member( AddressBookFile *book, XMLFile *file, ItemGroup *group ) {
+       GList *attr;
+       gchar *name, *value;
+       gchar *pid = NULL, *eid = NULL;
+       ItemEMail *email = NULL;
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( strcmp( name, AB_ATTAG_PID ) == 0 ) {
+                       pid = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_EID ) == 0 ) {
+                       eid = g_strdup( value );
+               }
+               attr = g_list_next( attr );
+       }
+       email = addrcache_get_email( book->addressCache, pid, eid );
+       if( email ) {
+               if( group ) {
+                       addrcache_group_add_email( book->addressCache, group, email );
+               }
+               else {
+                       addritem_free_item_email( email );
+                       email = NULL;
+               }
+       }
+}
+
+/*
+* Parse group member list.
+*/
+static void addrbook_parse_member_list( AddressBookFile *book, XMLFile *file, ItemGroup *group ){
+       GList *attr;
+       guint prev_level;
+       gchar *element;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               if( xml_compare_tag( file, AB_ELTAG_MEMBER ) ) {
+                       attr = xml_get_current_tag_attr(file);
+                       addrbook_parse_member( book, file, group );
+                       addrbook_parse_member_list( book, file, group );
+               }
+               else {
+                       attr = xml_get_current_tag_attr( file );
+               }
+       }
+}
+
+/*
+* Parse group.
+*/
+static void addrbook_parse_group( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *name, *value;
+       ItemGroup *group = NULL;
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( ! group ) group = addritem_create_item_group();
+               if( strcmp( name, AB_ATTAG_UID ) == 0 ) {
+                       ADDRITEM_ID(group) = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_NAME ) == 0 ) {
+                       ADDRITEM_NAME(group) = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_REMARKS ) == 0 ) {
+                       group->remarks = g_strdup( value );
+               }
+               attr = g_list_next( attr );
+       }
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_MEMBER_LIST ) ) {
+               if( group ) {
+                       addrcache_hash_add_group( book->addressCache, group );
+               }
+               addrbook_parse_member_list( book, file, group );
+       }
+}
+
+/*
+* Parse folder item.
+*/
+static void addrbook_parse_folder_item( AddressBookFile *book, XMLFile *file, ItemFolder *folder ) {
+       GList *attr;
+       gchar *name, *value;
+       gchar *uid = NULL;
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( strcmp( name, AB_ATTAG_UID ) == 0 ) {
+                       uid = g_strdup( value );
+               }
+               attr = g_list_next( attr );
+       }
+       if( folder ) {
+               if( uid ) {
+                       folder->listItems = g_list_append( folder->listItems, uid );
+               }
+       }
+}
+
+/*
+* Parse folder item list.
+*/
+static void addrbook_parse_folder_list( AddressBookFile *book, XMLFile *file, ItemFolder *folder ){
+       GList *attr;
+       guint prev_level;
+       gchar *element;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               if( xml_compare_tag( file, AB_ELTAG_ITEM ) ) {
+                       attr = xml_get_current_tag_attr(file);
+                       addrbook_parse_folder_item( book, file, folder );
+                       addrbook_parse_folder_list( book, file, folder );
+               }
+               else {
+                       attr = xml_get_current_tag_attr( file );
+               }
+       }
+}
+
+/*
+* Parse folder.
+*/
+static void addrbook_parse_folder( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *name, *value;
+       ItemFolder *folder = NULL;
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( ! folder ) {
+                       folder = addritem_create_item_folder();
+               }
+               if( strcmp( name, AB_ATTAG_UID ) == 0 ) {
+                       ADDRITEM_ID(folder) = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_NAME ) == 0 ) {
+                       ADDRITEM_NAME(folder) = g_strdup( value );
+               }
+               else if( strcmp( name, AB_ATTAG_REMARKS ) == 0 ) {
+                       folder->remarks = g_strdup( value );
+               }
+               attr = g_list_next( attr );
+       }
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_ITEM_LIST ) ) {
+               if( folder ) {
+                       if( addrcache_hash_add_folder( book->addressCache, folder ) ) {
+                               book->tempList = g_list_append( book->tempList, folder );
+                               ADDRITEM_PARENT(folder) = NULL; // We will resolve folder later
+                       }
+               }
+               addrbook_parse_folder_list( book, file, folder );
+       }
+}
+
+/*
+* Parse address book.
+* Return: TRUE if data read successfully, FALSE if error reading data.
+*/
+static gboolean addrbook_read_tree( AddressBookFile *book, XMLFile *file ) {
+       gboolean retVal;
+       GList *attr;
+       gchar *name, *value;
+
+       book->retVal = MGU_BAD_FORMAT;
+       if( xml_get_dtd( file ) ) {
+               return FALSE;
+       }
+       if( xml_parse_next_tag( file ) ) {
+               longjmp( book->jumper, 1 );
+       }
+       if( ! xml_compare_tag( file, AB_ELTAG_ADDRESS_BOOK ) ) {
+               return FALSE;
+       }
+
+       attr = xml_get_current_tag_attr(file);
+       while( attr ) {
+               name = ((XMLAttr *)attr->data)->name;
+               value = ((XMLAttr *)attr->data)->value;
+               if( strcmp( name, AB_ATTAG_NAME ) == 0 ) {
+                       addrbook_set_name( book, value );
+               }
+               attr = g_list_next( attr );
+       }
+
+       retVal = TRUE;
+       for (;;) {
+               if (! file->level ) break;
+               // Get next item tag (person, group or folder)
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if( xml_compare_tag( file, AB_ELTAG_PERSON ) ) {
+                       addrbook_parse_person( book, file );
+               }
+               else if( xml_compare_tag( file, AB_ELTAG_GROUP ) ) {
+                       addrbook_parse_group( book, file );
+               }
+               else if( xml_compare_tag( file, AB_ELTAG_FOLDER ) ) {
+                       addrbook_parse_folder( book, file );
+               }
+       }
+       if( retVal ) book->retVal = MGU_SUCCESS;
+       return retVal;
+}
+
+/*
+* Resolve folder items visitor function.
+*/
+static void addrbook_res_items_vis( gpointer key, gpointer value, gpointer data ) {
+       AddressBookFile *book = data;
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       ItemFolder *rootFolder = book->addressCache->rootFolder;
+       if( obj->parent == NULL ) {
+               if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+                       rootFolder->listPerson = g_list_append( rootFolder->listPerson, obj );
+                       ADDRITEM_PARENT(obj) = ADDRITEM_OBJECT(rootFolder);
+               }
+               else if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+                       rootFolder->listGroup = g_list_append( rootFolder->listGroup, obj );
+                       ADDRITEM_PARENT(obj) = ADDRITEM_OBJECT(rootFolder);
+               }
+       }
+}
+
+/*
+* Resolve folder items. Lists of UID's are replaced with pointers to data items.
+*/
+static void addrbook_resolve_folder_items( AddressBookFile *book ) {
+       GList *nodeFolder = NULL;
+       GList *listRemove = NULL;
+       GList *node = NULL;
+       ItemFolder *rootFolder = book->addressCache->rootFolder;
+       nodeFolder = book->tempList;
+       while( nodeFolder ) {
+               ItemFolder *folder = nodeFolder->data;
+               listRemove = NULL;
+               node = folder->listItems;
+               while( node ) {
+                       gchar *uid = node->data;
+                       AddrItemObject *aio = addrcache_get_object( book->addressCache, uid );
+                       if( aio ) {
+                               if( aio->type == ITEMTYPE_FOLDER ) {
+                                       ItemFolder *item = ( ItemFolder * ) aio;
+                                       folder->listFolder = g_list_append( folder->listFolder, item );
+                                       ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
+                                       addrcache_hash_add_folder( book->addressCache, folder );
+                               }
+                               else if( aio->type == ITEMTYPE_PERSON ) {
+                                       ItemPerson *item = ( ItemPerson * ) aio;
+                                       folder->listPerson = g_list_append( folder->listPerson, item );
+                                       ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
+                               }
+                               else if( aio->type == ITEMTYPE_GROUP ) {
+                                       ItemGroup *item = ( ItemGroup * ) aio;
+                                       folder->listGroup = g_list_append( folder->listGroup, item );
+                                       ADDRITEM_PARENT(item) = ADDRITEM_OBJECT(folder);
+                               }
+                               // Replace data with pointer to item
+                               g_free( uid );
+                               node->data = aio;
+                       }
+                       else {
+                               // Not found, append to remove list.
+                               listRemove = g_list_append( listRemove, uid );
+                       }
+                       node = g_list_next( node );
+               }
+               rootFolder->listFolder = g_list_append( rootFolder->listFolder, folder );
+
+               // Process remove list
+               node = listRemove;
+               while( node ) {
+                       gchar *uid = node->data;
+                       folder->listItems = g_list_remove( folder->listItems, uid );
+                       g_free( uid );
+                       node = g_list_next( node );
+               }
+               g_list_free( listRemove );
+               nodeFolder = g_list_next( nodeFolder );
+       }
+
+       // Remove folders with parents.
+       listRemove = NULL;
+       node = rootFolder->listFolder;
+       while( node ) {
+               ItemFolder *folder = ( ItemFolder * ) node->data;
+               if( ADDRITEM_PARENT(folder) ) {
+                       // Remove folders with parents
+                       listRemove = g_list_append( listRemove, folder );
+               }
+               else {
+                       // Add to root folder
+                       ADDRITEM_PARENT(folder) = ADDRITEM_OBJECT(book->addressCache->rootFolder);
+               }
+               node = g_list_next( node );
+       }
+
+       // Process remove list
+       node = listRemove;
+       while( node ) {
+               rootFolder->listFolder = g_list_remove( rootFolder->listFolder, node->data );
+               node = g_list_next( node );
+       }
+       g_list_free( listRemove );
+
+       // Move all unparented persons and groups into root folder
+       g_hash_table_foreach( book->addressCache->itemHash, addrbook_res_items_vis, book );
+
+       // Free up some more
+       nodeFolder = book->tempList;
+       while( nodeFolder ) {
+               ItemFolder *folder = nodeFolder->data;
+               g_list_free( folder->listItems );
+               folder->listItems = NULL;
+               nodeFolder = g_list_next( nodeFolder );
+       }
+       g_list_free( book->tempList );
+       book->tempList = NULL;
+
+}
+
+/*
+* Read address book file.
+*/
+gint addrbook_read_data( AddressBookFile *book ) {
+       XMLFile *file = NULL;
+       gchar *fileSpec = NULL;
+       g_return_if_fail( book != NULL );
+
+       fileSpec = g_strconcat( book->path, G_DIR_SEPARATOR_S, book->fileName, NULL );
+       book->retVal = MGU_OPEN_FILE;
+       book->accessFlag = FALSE;
+       book->modifyFlag = FALSE;
+       file = xml_open_file( fileSpec );
+       g_free( fileSpec );
+       if( file ) {
+               book->tempList = NULL;
+
+               // Trap for parsing errors.
+               if( setjmp( book->jumper ) ) {
+                       xml_close_file( file );
+                       return book->retVal;
+               }
+               addrbook_read_tree( book, file );
+               xml_close_file( file );
+
+               // Resolve folder items
+               addrbook_resolve_folder_items( book );
+               book->tempList = NULL;
+               book->readFlag = TRUE;
+               book->dirtyFlag = FALSE;
+       }
+       return book->retVal;
+}
+
+static void addrbook_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
+       gint i;
+       for( i = 0; i < lvl; i++ ) fputs( "  ", fp );
+       fputs( "<", fp );
+       fputs( name, fp );
+}
+
+static void addrbook_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
+       gint i;
+       for( i = 0; i < lvl; i++ ) fputs( "  ", fp );
+       fputs( "</", fp );
+       fputs( name, fp );
+       fputs( ">\n", fp );
+}
+
+static void addrbook_write_attr( FILE *fp, gchar *name, gchar *value ) {
+       fputs( " ", fp );
+       fputs( name, fp );
+       fputs( "=\"", fp );
+       xml_file_put_escape_str( fp, value );
+       fputs( "\"", fp );
+}
+
+/*
+* Write file hash table visitor function.
+*/
+static void addrbook_write_item_person_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       FILE *fp = ( FILE * ) data;
+       GList *node;
+       if( ! obj ) return;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+               ItemPerson *person = ( ItemPerson * ) value;
+               if( person ) {
+                       addrbook_write_elem_s( fp, 1, AB_ELTAG_PERSON );
+                       addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(person) );
+                       addrbook_write_attr( fp, AB_ATTAG_FIRST_NAME, person->firstName );
+                       addrbook_write_attr( fp, AB_ATTAG_LAST_NAME, person->lastName );
+                       addrbook_write_attr( fp, AB_ATTAG_NICK_NAME, person->nickName );
+                       addrbook_write_attr( fp, AB_ATTAG_COMMON_NAME, ADDRITEM_NAME(person) );
+                       fputs( " >\n", fp);
+
+                       // Output email addresses
+                       addrbook_write_elem_s( fp, 2, AB_ELTAG_ADDRESS_LIST );
+                       fputs( ">\n", fp );
+                       node = person->listEMail;
+                       while ( node ) {
+                               ItemEMail *email = node->data;
+                               addrbook_write_elem_s( fp, 3, AB_ELTAG_ADDRESS );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(email) );
+                               addrbook_write_attr( fp, AB_ATTAG_ALIAS, ADDRITEM_NAME(email) );
+                               addrbook_write_attr( fp, AB_ATTAG_EMAIL, email->address );
+                               addrbook_write_attr( fp, AB_ATTAG_REMARKS, email->remarks );
+                               fputs( " />\n", fp);
+                               node = g_list_next( node );
+                       }
+                       addrbook_write_elem_e( fp, 2, AB_ELTAG_ADDRESS_LIST );
+
+                       // Output user attributes
+                       addrbook_write_elem_s( fp, 2, AB_ELTAG_ATTRIBUTE_LIST );
+                       fputs( ">\n", fp );
+                       node = person->listAttrib;
+                       while ( node ) {
+                               UserAttribute *attrib = node->data;
+                               addrbook_write_elem_s( fp, 3, AB_ELTAG_ATTRIBUTE );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, attrib->uid );
+                               addrbook_write_attr( fp, AB_ATTAG_NAME, attrib->name );
+                               fputs( " >", fp);
+                               xml_file_put_escape_str( fp, attrib->value );
+                               addrbook_write_elem_e( fp, 0, AB_ELTAG_ATTRIBUTE );
+                               node = g_list_next( node );
+                       }
+                       addrbook_write_elem_e( fp, 2, AB_ELTAG_ATTRIBUTE_LIST );
+                       addrbook_write_elem_e( fp, 1, AB_ELTAG_PERSON );
+               }
+       }
+}
+
+/*
+* Output all groups in folder.
+*/
+static void addrbook_write_folder_group( ItemFolder *parent, FILE *fp ) {
+       GList *nodeGrp = parent->listGroup;
+       GList *node;
+       while( nodeGrp ) {
+               ItemGroup *group = nodeGrp->data;
+               if( group ) {
+                       addrbook_write_elem_s( fp, 1, AB_ELTAG_GROUP );
+                       addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(group) );
+                       addrbook_write_attr( fp, AB_ATTAG_NAME, ADDRITEM_NAME(group) );
+                       addrbook_write_attr( fp, AB_ATTAG_REMARKS, group->remarks );
+                       fputs( " >\n", fp );
+
+                       // Output email address links
+                       addrbook_write_elem_s( fp, 2, AB_ELTAG_MEMBER_LIST );
+                       fputs( ">\n", fp );
+                       node = group->listEMail;
+                       while ( node ) {
+                               ItemEMail *email = node->data;
+                               ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
+                               addrbook_write_elem_s( fp, 3, AB_ELTAG_MEMBER );
+                               addrbook_write_attr( fp, AB_ATTAG_PID, ADDRITEM_ID(person) );
+                               addrbook_write_attr( fp, AB_ATTAG_EID, ADDRITEM_ID(email) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+                       addrbook_write_elem_e( fp, 2, AB_ELTAG_MEMBER_LIST );
+                       addrbook_write_elem_e( fp, 1, AB_ELTAG_GROUP );
+               }
+               nodeGrp = g_list_next( nodeGrp );
+       }
+       node = parent->listFolder;
+       while( node ) {
+               ItemFolder *folder = node->data;
+               addrbook_write_folder_group( folder, fp );
+               node = g_list_next( node );
+       }
+}
+
+/*
+* Write file hash table visitor function.
+*/
+static void addrbook_write_item_group_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       FILE *fp = ( FILE * ) data;
+       GList *node;
+       if( ! obj ) return;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+               ItemGroup *group = ( ItemGroup * ) value;
+               if( group ) {
+                       addrbook_write_elem_s( fp, 1, AB_ELTAG_GROUP );
+                       addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(group) );
+                       addrbook_write_attr( fp, AB_ATTAG_NAME, ADDRITEM_NAME(group) );
+                       addrbook_write_attr( fp, AB_ATTAG_REMARKS, group->remarks );
+                       fputs( " >\n", fp );
+
+                       // Output email address links
+                       addrbook_write_elem_s( fp, 2, AB_ELTAG_MEMBER_LIST );
+                       fputs( ">\n", fp );
+                       node = group->listEMail;
+                       while ( node ) {
+                               ItemEMail *email = node->data;
+                               ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
+                               addrbook_write_elem_s( fp, 3, AB_ELTAG_MEMBER );
+                               addrbook_write_attr( fp, AB_ATTAG_PID, ADDRITEM_ID(person) );
+                               addrbook_write_attr( fp, AB_ATTAG_EID, ADDRITEM_ID(email) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+                       addrbook_write_elem_e( fp, 2, AB_ELTAG_MEMBER_LIST );
+                       addrbook_write_elem_e( fp, 1, AB_ELTAG_GROUP );
+               }
+       }
+}
+
+/*
+* Output all folders in folder.
+*/
+static void addrbook_write_folder_folder( ItemFolder *parent, FILE *fp ) {
+       GList *nodeFold = parent->listFolder;
+       GList *node;
+       while( nodeFold ) {
+               ItemFolder *folder = nodeFold->data;
+               addrbook_write_folder_folder( folder, fp );
+               if( folder ) {
+                       addrbook_write_elem_s( fp, 1, AB_ELTAG_FOLDER );
+                       addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(folder) );
+                       addrbook_write_attr( fp, AB_ATTAG_NAME, ADDRITEM_NAME(folder) );
+                       addrbook_write_attr( fp, AB_ATTAG_REMARKS, folder->remarks );
+                       fputs( " >\n", fp );
+                       addrbook_write_elem_s( fp, 2, AB_ELTAG_ITEM_LIST );
+                       fputs( ">\n", fp );
+
+                       // Output persons
+                       node = folder->listPerson;
+                       while ( node ) {
+                               ItemPerson *item = node->data;
+                               addrbook_write_elem_s( fp, 2, AB_ELTAG_ITEM );
+                               addrbook_write_attr( fp, AB_ATTAG_TYPE,  AB_ATTAG_VAL_PERSON );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(item ) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+
+                       // Output groups
+                       node = folder->listGroup;
+                       while ( node ) {
+                               ItemGroup *item = node->data;
+                               addrbook_write_elem_s( fp, 2, AB_ELTAG_ITEM );
+                               addrbook_write_attr( fp, AB_ATTAG_TYPE, AB_ATTAG_VAL_GROUP );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(item ) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+
+                       // Output folders
+                       node = folder->listFolder;
+                       while ( node ) {
+                               ItemFolder *item = node->data;
+                               addrbook_write_elem_s( fp, 2, AB_ELTAG_ITEM );
+                               addrbook_write_attr( fp, AB_ATTAG_TYPE, AB_ATTAG_VAL_FOLDER );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(item ) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+                       addrbook_write_elem_e( fp, 2, AB_ELTAG_ITEM_LIST );
+                       addrbook_write_elem_e( fp, 1, AB_ELTAG_FOLDER );
+               }
+               nodeFold = g_list_next( nodeFold );
+       }
+}
+
+/*
+* Write file hash table visitor function.
+*/
+static void addrbook_write_item_folder_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       FILE *fp = ( FILE * ) data;
+       GList *node;
+       if( ! obj ) return;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_FOLDER ) {
+               ItemFolder *folder = ( ItemFolder * ) value;
+               if( folder ) {
+                       addrbook_write_elem_s( fp, 1, AB_ELTAG_FOLDER );
+                       addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(folder) );
+                       addrbook_write_attr( fp, AB_ATTAG_NAME, ADDRITEM_NAME(folder) );
+                       addrbook_write_attr( fp, AB_ATTAG_REMARKS, folder->remarks );
+                       fputs( " >\n", fp );
+                       addrbook_write_elem_s( fp, 2, AB_ELTAG_ITEM_LIST );
+                       fputs( ">\n", fp );
+
+                       // Output persons
+                       node = folder->listPerson;
+                       while ( node ) {
+                               ItemPerson *item = node->data;
+                               addrbook_write_elem_s( fp, 3, AB_ELTAG_ITEM );
+                               addrbook_write_attr( fp, AB_ATTAG_TYPE,  AB_ATTAG_VAL_PERSON );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(item ) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+
+                       // Output groups
+                       node = folder->listGroup;
+                       while ( node ) {
+                               ItemGroup *item = node->data;
+                               addrbook_write_elem_s( fp, 3, AB_ELTAG_ITEM );
+                               addrbook_write_attr( fp, AB_ATTAG_TYPE, AB_ATTAG_VAL_GROUP );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(item ) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+
+                       // Output folders
+                       node = folder->listFolder;
+                       while ( node ) {
+                               ItemFolder *item = node->data;
+                               addrbook_write_elem_s( fp, 3, AB_ELTAG_ITEM );
+                               addrbook_write_attr( fp, AB_ATTAG_TYPE, AB_ATTAG_VAL_FOLDER );
+                               addrbook_write_attr( fp, AB_ATTAG_UID, ADDRITEM_ID(item ) );
+                               fputs( " />\n", fp );
+                               node = g_list_next( node );
+                       }
+                       addrbook_write_elem_e( fp, 2, AB_ELTAG_ITEM_LIST );
+                       addrbook_write_elem_e( fp, 1, AB_ELTAG_FOLDER );
+               }
+       }
+}
+
+/*
+* Output address book data to specified file.
+* return: Status code.
+*/
+gint addrbook_write_to( AddressBookFile *book, gchar *newFile ) {
+       FILE *fp;
+       gchar *fileSpec;
+#ifndef DEV_STANDALONE
+       PrefFile *pfile;
+#endif
+
+       g_return_if_fail( book != NULL );
+       g_return_if_fail( newFile != NULL );
+
+       fileSpec = g_strconcat( book->path, G_DIR_SEPARATOR_S, newFile, NULL );
+
+       book->retVal = MGU_OPEN_FILE;
+#ifdef DEV_STANDALONE
+       fp = fopen( fileSpec, "w" );
+       g_free( fileSpec );
+       if( fp ) {
+               fputs( "<?xml version=\"1.0\" ?>\n", fp );
+#else
+       pfile = prefs_write_open( fileSpec );
+       g_free( fileSpec );
+       if( pfile ) {
+               fp = pfile->fp;
+               fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
+                               conv_get_current_charset_str() );
+#endif
+               addrbook_write_elem_s( fp, 0, AB_ELTAG_ADDRESS_BOOK );
+               addrbook_write_attr( fp, AB_ATTAG_NAME, book->name );
+               fputs( " >\n", fp );
+
+               // Output all persons
+               g_hash_table_foreach( book->addressCache->itemHash, addrbook_write_item_person_vis, fp );
+
+               // Output all groups
+               g_hash_table_foreach( book->addressCache->itemHash, addrbook_write_item_group_vis, fp );
+               // addrbook_write_folder_group( book->rootFolder, fp );
+
+               // Output all folders
+               g_hash_table_foreach( book->addressCache->itemHash, addrbook_write_item_folder_vis, fp );
+               // addrbook_write_folder_folder( book->rootFolder, fp );
+
+               addrbook_write_elem_e( fp, 0, AB_ELTAG_ADDRESS_BOOK );
+               book->retVal = MGU_SUCCESS;
+#ifdef DEV_STANDALONE
+               fclose( fp );
+#else
+               if( prefs_write_close( pfile ) < 0 ) {
+                       book->retVal = MGU_ERROR_WRITE;
+               }
+#endif
+       }
+
+       fileSpec = NULL;
+       return book->retVal;
+}
+
+/*
+* Output address book data to original file.
+* return: Status code.
+*/
+gint addrbook_save_data( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+
+       book->retVal = MGU_NO_FILE;
+       if( book->fileName == NULL || *book->fileName == '\0' ) return book->retVal;
+       if( book->path == NULL || *book->path == '\0' ) return book->retVal;
+
+       addrbook_write_to( book, book->fileName );
+       if( book->retVal == MGU_SUCCESS ) {
+               book->dirtyFlag = FALSE;
+       }
+       return book->retVal;
+}
+
+/* **********************************************************************
+* Address book edit interface functions...
+* ***********************************************************************
+*/
+
+/*
+* Move person's email item.
+* param: book       Address book.
+*        person     Person.
+*        itemMove   Item to move.
+*        itemTarget Target item before which to move item.
+*/
+ItemEMail *addrbook_move_email_before( AddressBookFile *book, ItemPerson *person,
+                       ItemEMail *itemMove, ItemEMail *itemTarget )
+{
+       ItemEMail *email = NULL;
+       g_return_if_fail( book != NULL );
+       email = addritem_move_email_before( person, itemMove, itemTarget );
+       if( email ) {
+               book->dirtyFlag = TRUE;
+       }
+       return email;
+}
+
+/*
+* Move person's email item.
+* param: book       Address book.
+*        person     Person.
+*        itemMove   Item to move.
+*        itemTarget Target item after which to move item.
+*/
+ItemEMail *addrbook_move_email_after( AddressBookFile *book, ItemPerson *person,
+                       ItemEMail *itemMove, ItemEMail *itemTarget )
+{
+       ItemEMail *email = NULL;
+       g_return_if_fail( book != NULL );
+       email = addritem_move_email_after( person, itemMove, itemTarget );
+       if( email ) {
+               book->dirtyFlag = TRUE;
+       }
+       return email;
+}
+
+/*
+* Hash table visitor function.
+*/
+static gboolean addrbook_free_simple_hash_vis( gpointer *key, gpointer *value, gpointer *data ) {
+       g_free( key );
+       key = NULL;
+       value = NULL;
+       return TRUE;
+}
+
+/*
+* Update address book email list for specified person.
+* Enter: book      Address book.
+*        person    Person to update.
+*        listEMail New list of email addresses.
+* Note: The existing email addresses are replaced with the new addresses. Any references
+* to old addresses in the groups are re-linked to the new addresses. All old addresses
+* linked to the person are removed.
+*/
+void addrbook_update_address_list( AddressBookFile *book, ItemPerson *person, GList *listEMail ) {
+       GList *node;
+       GList *oldData;
+       GList *listGroup;
+
+       g_return_if_fail( book != NULL );
+       g_return_if_fail( person != NULL );
+
+       // Remember old list
+       oldData = person->listEMail;
+
+       // Attach new address list to person.
+       node = listEMail;
+       while( node ) {
+               ItemEMail *email = node->data;
+               if( ADDRITEM_ID(email) == NULL ) {
+                       // Allocate an ID
+                       addrcache_id_email( book->addressCache, email );
+               }
+               ADDRITEM_PARENT(email) = ADDRITEM_OBJECT(person);
+               node = g_list_next( node );
+       }
+       person->listEMail = listEMail;
+
+       // Get groups where person's email is listed
+       listGroup = addrcache_get_group_for_person( book->addressCache, person );
+       if( listGroup ) {
+               GHashTable *hashEMail;
+               GList *nodeGrp;
+
+               // Load hash table with new address entries
+               hashEMail = g_hash_table_new( g_str_hash, g_str_equal );
+               node = listEMail;
+               while( node ) {
+                       ItemEMail *email = node->data;
+                       gchar *addr = g_strdup( email->address );
+                       g_strdown( addr );
+                       if( ! g_hash_table_lookup( hashEMail, addr ) ) {
+                               g_hash_table_insert( hashEMail, addr, email );
+                       }
+                       node = g_list_next( node );
+               }
+
+               // Re-parent new addresses to existing groups, where email address match.
+               nodeGrp = listGroup;
+               while( nodeGrp ) {
+                       ItemGroup *group = ( ItemGroup * ) nodeGrp->data;
+                       GList *groupEMail = group->listEMail;
+                       GList *nodeGrpEM;
+                       GList *listRemove = NULL;
+
+                       // Process each email item linked to group
+                       nodeGrpEM = groupEMail;
+                       while( nodeGrpEM ) {
+                               ItemEMail *emailGrp = ( ItemEMail * ) nodeGrpEM->data;
+                               if( ADDRITEM_PARENT(emailGrp) == ADDRITEM_OBJECT(person) ) {
+                                       // Found an email address for this person
+                                       ItemEMail *emailNew = NULL;
+                                       gchar *addr = g_strdup( emailGrp->address );
+                                       g_strdown( addr );
+                                       emailNew = ( ItemEMail * ) g_hash_table_lookup( hashEMail, addr );
+                                       g_free( addr );
+                                       if( emailNew ) {
+                                               // Point to this entry
+                                               nodeGrpEM->data = emailNew;
+                                       }
+                                       else {
+                                               // Mark for removal
+                                               listRemove = g_list_append( listRemove, emailGrp );
+                                       }
+                               }
+                               // Move on to next email link
+                               nodeGrpEM = g_list_next( nodeGrpEM );
+                       }
+
+                       // Process all removed links in current group
+                       nodeGrpEM = listRemove;
+                       while( nodeGrpEM ) {
+                               ItemEMail *emailGrp = nodeGrpEM->data;
+                               groupEMail = g_list_remove( groupEMail, emailGrp );
+                               nodeGrpEM = g_list_next( nodeGrpEM );
+                       }
+
+                       // Move on to next group
+                       nodeGrp = g_list_next( nodeGrp );
+
+               }
+
+               // Clear hash table
+               g_hash_table_foreach_remove( hashEMail, ( GHRFunc ) addrbook_free_simple_hash_vis, NULL );
+               g_hash_table_destroy( hashEMail );
+               hashEMail = NULL;
+               g_list_free( listGroup );
+               listGroup = NULL;
+       }
+
+       // Free up old data
+       addritem_free_list_email( oldData );
+       oldData = NULL;
+       book->dirtyFlag = TRUE;
+
+}
+
+/*
+* Add person and address data to address book.
+* Enter: book      Address book.
+*        folder    Folder where to add person, or NULL for root folder.
+*        listEMail New list of email addresses.
+* Return: Person added.
+* Note: A new person is created with specified list of email addresses. All objects inserted
+* into address book.
+*/
+ItemPerson *addrbook_add_address_list( AddressBookFile *book, ItemFolder *folder, GList *listEMail ) {
+       ItemPerson *person;
+       ItemFolder *f = folder;
+       GList *node;
+
+       g_return_if_fail( book != NULL );
+
+       if( ! f ) f = book->addressCache->rootFolder;
+       person = addritem_create_item_person();
+       addrcache_id_person( book->addressCache, person );
+       addrcache_folder_add_person( book->addressCache, f, person );
+
+       node = listEMail;
+       while( node ) {
+               ItemEMail *email = node->data;
+               if( ADDRITEM_ID(email) == NULL ) {
+                       addrcache_id_email( book->addressCache, email );
+               }
+               addrcache_person_add_email( book->addressCache, person, email );
+               node = g_list_next( node );
+       }
+       book->dirtyFlag = TRUE;
+       return person;
+}
+
+/*
+* Load hash table visitor function.
+*/
+static void addrbook_load_hash_table_email_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_EMAIL ) {
+               GHashTable *table = ( GHashTable * ) data;
+               gchar *newKey = g_strdup( key );
+               ItemEMail *email = ( ItemEMail * ) obj;
+               if( ! g_hash_table_lookup( table, newKey ) ) {
+                       g_hash_table_insert( table, newKey, email );
+               }
+       }
+}
+
+/*
+* Load hash table with links to email addresses.
+*/
+static void addrbook_load_hash_table_email( AddressBookFile *book, GHashTable *table ) {
+       g_return_if_fail( book != NULL );
+       g_return_if_fail( table != NULL );
+       g_hash_table_foreach( book->addressCache->itemHash, addrbook_load_hash_table_email_vis, table );
+}
+
+/*
+* Build available email list visitor function.
+*/
+static void addrbook_build_avail_email_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+               AddressBookFile *book = data;
+               ItemPerson *person = ( ItemPerson * ) obj;
+               GList *node = person->listEMail;
+               while( node ) {
+                       ItemEMail *email = node->data;
+                       gchar *newKey = g_strdup( ADDRITEM_ID(email) );
+                       if( ! g_hash_table_lookup( book->tempHash, ADDRITEM_ID(email) ) ) {
+                               book->tempList = g_list_append( book->tempList, email );
+                       }
+                       node = g_list_next( node );
+               }
+       }
+}
+
+/*
+* Return link list of available email items (which have not already been linked to
+* groups). Note that the list contains references to items and should be g_free()
+* when done. Do *NOT* attempt to used the addrbook_free_xxx() functions... this will
+* destroy the addressbook data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrbook_get_available_email_list( AddressBookFile *book, ItemGroup *group ) {
+       GList *list = NULL;
+       GHashTable *table;
+
+       g_return_if_fail( book != NULL );
+
+       // Load hash table with group email entries
+       table = g_hash_table_new( g_str_hash, g_str_equal );
+       if( group ) {
+               list = group->listEMail;
+               while( list ) {
+                       ItemEMail *email = list->data;
+                       g_hash_table_insert( table, ADDRITEM_ID(email), email );
+                       list = g_list_next( list );
+               }
+       }
+
+       // Build list of available email addresses which exclude those already in groups
+       book->tempList = NULL;
+       book->tempHash = table;
+       g_hash_table_foreach( book->addressCache->itemHash, addrbook_build_avail_email_vis, book );
+       list = book->tempList;
+       book->tempList = NULL;
+       book->tempHash = NULL;
+
+       // Clear hash table
+       g_hash_table_destroy( table );
+       table = NULL;
+
+       return list;
+}
+
+/*
+* Update address book email list for specified group.
+* Enter: book      Address book.
+*        group     group to update.
+*        listEMail New list of email addresses. This should *NOT* be g_free() when done.
+* Note: The existing email addresses are replaced with the new addresses. Any references
+* to old addresses in the groups are re-linked to the new addresses. All old addresses
+* linked to the person are removed.
+*/
+void addrbook_update_group_list( AddressBookFile *book, ItemGroup *group, GList *listEMail ) {
+       GList *oldData;
+
+       g_return_if_fail( book != NULL );
+       g_return_if_fail( group != NULL );
+
+       // Remember old list
+       oldData = group->listEMail;
+       group->listEMail = listEMail;
+       mgu_clear_list( oldData );
+       oldData = NULL;
+       book->dirtyFlag = TRUE;
+}
+
+/*
+* Add group and email list to address book.
+* Enter: book      Address book.
+*        folder    Parent folder, or NULL for root folder.
+*        listEMail New list of email addresses. This should *NOT* be g_free() when done.
+* Return: Group object.
+* Note: The existing email addresses are replaced with the new addresses. Any references
+* to old addresses in the groups are re-linked to the new addresses. All old addresses
+* linked to the person are removed.
+*/
+ItemGroup *addrbook_add_group_list( AddressBookFile *book, ItemFolder *folder, GList *listEMail ) {
+       ItemGroup *group = NULL;
+       ItemFolder *f = folder;
+       g_return_if_fail( book != NULL );
+
+       if( ! f ) f = book->addressCache->rootFolder;
+       group = addritem_create_item_group();
+       addrcache_id_group( book->addressCache, group );
+       addrcache_folder_add_group( book->addressCache, f, group );
+       group->listEMail = listEMail;
+       book->dirtyFlag = TRUE;
+       return group;
+}
+
+/*
+* Add new folder to address book.
+* Enter: book   Address book.
+*        parent        Parent folder.
+* Return: Folder that was added. This should *NOT* be g_free() when done.
+*/
+ItemFolder *addrbook_add_new_folder( AddressBookFile *book, ItemFolder *parent ) {
+       ItemFolder *folder = NULL;
+       ItemFolder *p = parent;
+       g_return_if_fail( book != NULL );
+
+       if( ! p ) p = book->addressCache->rootFolder;
+       folder = addritem_create_item_folder();
+       addrcache_id_folder( book->addressCache, folder );
+       if( addrcache_hash_add_folder( book->addressCache, folder ) ) {
+               p->listFolder = g_list_append( p->listFolder, folder );
+               ADDRITEM_PARENT(folder) = ADDRITEM_OBJECT(p);
+               book->dirtyFlag = TRUE;
+       }
+       else {
+               addritem_free_item_folder( folder );
+               folder = NULL;
+       }
+       return folder;
+}
+
+/*
+* Update address book attribute list for specified person.
+* Enter: book       Address book.
+*        person     Person to update.
+*        listAttrib New list of attributes.
+* Note: The existing email addresses are replaced with the new addresses. All old attributes
+* linked to the person are removed.
+*/
+void addrbook_update_attrib_list( AddressBookFile *book, ItemPerson *person, GList *listAttrib ) {
+       GList *node;
+       GList *oldData;
+
+       g_return_if_fail( book != NULL );
+       g_return_if_fail( person != NULL );
+
+       // Remember old list
+       oldData = person->listAttrib;
+
+       // Attach new address list to person.
+       node = listAttrib;
+       while( node ) {
+               UserAttribute *attrib = node->data;
+               if( attrib->uid == NULL ) {
+                       // Allocate an ID
+                       addrcache_id_attribute( book->addressCache, attrib );
+               }
+               node = g_list_next( node );
+       }
+       person->listAttrib = listAttrib;
+
+       // Free up old data
+       addritem_free_list_attribute( oldData );
+       oldData = NULL;
+       book->dirtyFlag = TRUE;
+
+}
+
+/*
+* Add attribute data for person to address book.
+* Enter: book       Address book.
+*        person     New person object.
+*        listAttrib New list of attributes.
+* Note: Only attributes are inserted into address book.
+*/
+void addrbook_add_attrib_list( AddressBookFile *book, ItemPerson *person, GList *listAttrib ) {
+       GList *node;
+       g_return_if_fail( book != NULL );
+       g_return_if_fail( person != NULL );
+
+       node = listAttrib;
+       while( node ) {
+               UserAttribute *attrib = node->data;
+               if( attrib->uid == NULL ) {
+                       addrcache_id_attribute( book->addressCache, attrib );
+               }
+               addritem_person_add_attribute( person, attrib );
+               node = g_list_next( node );
+       }
+       book->dirtyFlag = TRUE;
+}
+
+/*
+* Return address book file for specified object.
+* Enter: aio Book item object.
+* Return: Address book, or NULL if not found.
+*/
+AddressBookFile *addrbook_item_get_bookfile( AddrItemObject *aio ) {
+       AddressBookFile *book = NULL;
+       if( aio ) {
+               ItemFolder *parent = NULL;
+               ItemFolder *root = NULL;
+               if( aio->type == ITEMTYPE_EMAIL ) {
+                       ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(aio);
+                       if( person ) {
+                               parent = ( ItemFolder * ) ADDRITEM_PARENT(person);
+                       }
+               }
+               else {
+                       parent = ( ItemFolder * ) ADDRITEM_PARENT(aio);
+               }
+               if( parent ) {
+                       root = addrcache_find_root_folder( parent );
+               }
+               if( root ) {
+                       book = ( AddressBookFile * ) ADDRITEM_PARENT(root);
+               }
+       }
+       return book;
+}
+
+/*
+* Remove folder from address book. Children are re-parented to parent folder.
+* param: folder Folder to remove.
+* return: Folder, or NULL if not found. Note that object should still be freed.
+*/
+ItemFolder *addrbook_remove_folder( AddressBookFile *book, ItemFolder *folder ) {
+       ItemFolder *f;
+       g_return_if_fail( book != NULL );
+       f = addrcache_remove_folder( book->addressCache, folder );
+       if( f ) book->dirtyFlag = TRUE;
+       return f;
+}
+
+/*
+* Remove folder from address book. Children are deleted.
+* param: folder Folder to remove.
+* return: Folder, or NULL if not found. Note that object should still be freed.
+*/
+ItemFolder *addrbook_remove_folder_delete( AddressBookFile *book, ItemFolder *folder ) {
+       ItemFolder *f;
+       g_return_if_fail( book != NULL );
+       f = addrcache_remove_folder_delete( book->addressCache, folder );
+       if( f ) book->dirtyFlag = TRUE;
+       return f;
+}
+
+#define WORK_BUFLEN     1024
+#define ADDRBOOK_DIGITS "0123456789"
+
+/*
+* Return list of existing address book files.
+* Enter: book Address book file.
+* Return: File list.
+*/
+GList *addrbook_get_bookfile_list( AddressBookFile *book ) {
+       gchar *adbookdir;
+       DIR *dp;
+       struct dirent *entry;
+       struct stat statbuf;
+       gchar buf[ WORK_BUFLEN ];
+       gchar numbuf[ WORK_BUFLEN ];
+       gint len, lenpre, lensuf, lennum;
+       long int val, maxval;
+       GList *fileList = NULL;
+
+       g_return_if_fail( book != NULL );
+
+       if( book->path == NULL || *book->path == '\0' ) {
+               book->retVal = MGU_NO_PATH;
+               return NULL;
+       }
+
+       strcpy( buf, book->path );
+       len = strlen( buf );
+       if( len > 0 ) {
+               if( buf[ len-1 ] != G_DIR_SEPARATOR ) {
+                       buf[ len ] = G_DIR_SEPARATOR;
+                       buf[ ++len ] = '\0';
+               }
+       }
+
+       adbookdir = g_strdup( buf );
+       strcat( buf, ADDRBOOK_PREFIX );
+
+       if( ( dp = opendir( adbookdir ) ) == NULL ) {
+               book->retVal = MGU_OPEN_DIRECTORY;
+               g_free( adbookdir );
+               return NULL;
+       }
+
+       lenpre = strlen( ADDRBOOK_PREFIX );
+       lensuf = strlen( ADDRBOOK_SUFFIX );
+       lennum = FILE_NUMDIGITS + lenpre;
+       maxval = -1;
+
+       while( ( entry = readdir( dp ) ) != NULL ) {
+               gchar **endptr = NULL;
+               gint i;
+               gboolean flg;
+
+               strcpy( buf, adbookdir );
+               strcat( buf, entry->d_name );
+               stat( buf, &statbuf );
+               if( S_IFREG & statbuf.st_mode ) {
+                       if( strncmp( entry->d_name, ADDRBOOK_PREFIX, lenpre ) == 0 ) {
+                               if( strncmp( (entry->d_name) + lennum, ADDRBOOK_SUFFIX, lensuf ) == 0 ) {
+                                       strncpy( numbuf, (entry->d_name) + lenpre, FILE_NUMDIGITS );
+                                       flg = TRUE;
+                                       for( i = 0; i < FILE_NUMDIGITS; i++ ) {
+                                               if( ! strchr( ADDRBOOK_DIGITS, numbuf[i] ) ) {
+                                                       flg = FALSE;
+                                                       break;
+                                               }
+                                       }
+                                       if( flg ) {
+                                               // Get value
+                                               val = strtol( numbuf, &endptr, 10 );
+                                               if( endptr  && val > -1 ) {
+                                                       if( val > maxval ) maxval = val;
+                                                       fileList = g_list_append( fileList, g_strdup( entry->d_name ) );
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+       closedir( dp );
+       g_free( adbookdir );
+
+       book->maxValue = maxval; 
+       book->retVal = MGU_SUCCESS;
+       return fileList;
+}
+
+/*
+* Return file name for specified file number.
+* Enter:  fileNum File number.
+* Return: File name, or NULL if file number too large. Should be g_free() when done.
+*/
+gchar *addrbook_gen_new_file_name( gint fileNum ) {
+       gchar fmt[ 30 ];
+       gchar buf[ WORK_BUFLEN ];
+       gint n = fileNum;
+       long int nmax;
+
+       if( n < 1 ) n = 1;
+       nmax = -1 + (long int) pow( 10, FILE_NUMDIGITS );
+       if( fileNum > nmax ) return NULL;
+       sprintf( fmt, "%%s%%0%dd%%s", FILE_NUMDIGITS );
+       sprintf( buf, fmt, ADDRBOOK_PREFIX, n, ADDRBOOK_SUFFIX );
+       return g_strdup( buf );
+}
+
+/* **********************************************************************
+* Address book test functions...
+* ***********************************************************************
+*/
+
+static void addrbook_show_attribs( GList *attr ) {
+       while( attr ) {
+               gchar *name = ((XMLAttr *)attr->data)->name;
+               gchar *value = ((XMLAttr *)attr->data)->value;
+               printf( "\tn/v = %s : %s\n", name, value );
+               attr = g_list_next( attr );
+       }
+       printf( "\t---\n" );
+}
+
+/*
+* Test email address list.
+*/
+static void addrbook_chkparse_addr_list( AddressBookFile *book, XMLFile *file ){
+       guint prev_level;
+       GList *attr;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               attr = xml_get_current_tag_attr(file);
+               // addrbook_show_attribs( attr );
+               if( xml_compare_tag( file, AB_ELTAG_ADDRESS ) ) {
+                       addrbook_chkparse_addr_list( book, file );
+               }
+       }
+}
+
+/*
+* Test user attributes for person.
+*/
+static void addrbook_chkparse_attribute( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *element;
+
+       attr = xml_get_current_tag_attr(file);
+       // addrbook_show_attribs( attr );
+       element = xml_get_element( file );
+       // printf( "\t\tattrib value : %s\n", element );
+}
+
+/*
+* Test attribute list.
+*/
+static void addrbook_chkparse_attr_list( AddressBookFile *book, XMLFile *file ){
+       guint prev_level;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               if( xml_compare_tag( file, AB_ELTAG_ATTRIBUTE ) ) {
+                       addrbook_chkparse_attribute( book, file );
+                       addrbook_chkparse_attr_list( book, file );
+               }
+       }
+}
+
+/*
+* Test person.
+*/
+static void addrbook_chkparse_person( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *name, *value;
+
+       attr = xml_get_current_tag_attr(file);
+       // addrbook_show_attribs( attr );
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_ADDRESS_LIST ) ) {
+               addrbook_chkparse_addr_list( book, file );
+       }
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_ATTRIBUTE_LIST ) ) {
+               addrbook_chkparse_attr_list( book, file );
+       }
+}
+
+/*
+* Test group member list.
+*/
+static void addrbook_chkparse_member_list( AddressBookFile *book, XMLFile *file ){
+       GList *attr;
+       guint prev_level;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               if( xml_compare_tag( file, AB_ELTAG_MEMBER ) ) {
+                       attr = xml_get_current_tag_attr(file);
+                       // addrbook_show_attribs( attr );
+                       addrbook_chkparse_member_list( book, file );
+               }
+               else {
+                       attr = xml_get_current_tag_attr( file );
+                       // addrbook_show_attribs( attr );
+               }
+       }
+}
+
+/*
+* Test group.
+*/
+static void addrbook_chkparse_group( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *name, *value;
+
+       attr = xml_get_current_tag_attr(file);
+       // addrbook_show_attribs( attr );
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_MEMBER_LIST ) ) {
+               addrbook_chkparse_member_list( book, file );
+       }
+}
+
+/*
+* Test folder item list.
+*/
+static void addrbook_chkparse_folder_list( AddressBookFile *book, XMLFile *file ){
+       GList *attr;
+       guint prev_level;
+
+       for (;;) {
+               prev_level = file->level;
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if (file->level < prev_level) return;
+               if( xml_compare_tag( file, AB_ELTAG_ITEM ) ) {
+                       attr = xml_get_current_tag_attr(file);
+                       // addrbook_show_attribs( attr );
+                       addrbook_chkparse_folder_list( book, file );
+               }
+               else {
+                       attr = xml_get_current_tag_attr( file );
+                       // addrbook_show_attribs( attr );
+               }
+       }
+}
+
+/*
+* Test folder.
+*/
+static void addrbook_chkparse_folder( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *name, *value;
+
+       attr = xml_get_current_tag_attr(file);
+       // addrbook_show_attribs( attr );
+       if( xml_parse_next_tag( file ) ) {      // Consume closing tag
+               longjmp( book->jumper, 1 );
+       }
+       if( xml_compare_tag( file, AB_ELTAG_ITEM_LIST ) ) {
+               addrbook_chkparse_folder_list( book, file );
+       }
+}
+
+/*
+* Test address book.
+*/
+static gboolean addrbook_chkread_tree( AddressBookFile *book, XMLFile *file ) {
+       GList *attr;
+       gchar *name, *value;
+       gboolean retVal;
+
+       if( xml_get_dtd( file ) ) {
+               return FALSE;
+       }
+       if( xml_parse_next_tag( file ) ) {
+               return FALSE;
+       }
+
+       if( ! xml_compare_tag( file, AB_ELTAG_ADDRESS_BOOK ) ) {
+               return FALSE;
+       }
+
+       attr = xml_get_current_tag_attr(file);
+       // addrbook_show_attribs( attr );
+
+       retVal = TRUE;
+       for (;;) {
+               if (! file->level ) break;
+               // Get item tag
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               // Get next tag (person, group or folder)
+               if( xml_parse_next_tag( file ) ) {
+                       longjmp( book->jumper, 1 );
+               }
+               if( xml_compare_tag( file, AB_ELTAG_PERSON ) ) {
+                       addrbook_chkparse_person( book, file );
+               }
+               else if( xml_compare_tag( file, AB_ELTAG_GROUP ) ) {
+                       addrbook_chkparse_group( book, file );
+               }
+               else if( xml_compare_tag( file, AB_ELTAG_FOLDER ) ) {
+                       addrbook_chkparse_folder( book, file );
+               }
+               else {
+                       // Item not recognized
+                       retVal = FALSE;
+               }
+       }
+       return retVal;
+}
+
+/*
+* Test address book file by parsing contents.
+* Enter: book     Address book file to check.
+*        fileName File name to check.
+* Return: MGU_SUCCESS if file appears to be valid format.
+*/
+gint addrbook_test_read_file( AddressBookFile *book, gchar *fileName ) {
+       XMLFile *file = NULL;
+       gchar *fileSpec = NULL;
+
+       g_return_if_fail( book != NULL );
+
+       fileSpec = g_strconcat( book->path, G_DIR_SEPARATOR_S, fileName, NULL );
+       book->retVal = MGU_OPEN_FILE;
+       file = xml_open_file( fileSpec );
+       g_free( fileSpec );
+       if( file ) {
+               book->retVal = MGU_BAD_FORMAT;
+               if( setjmp( book->jumper ) ) {
+                       // printf( "Caught Ya!!!\n" );
+                       xml_close_file( file );
+                       return book->retVal;
+               }
+               if( addrbook_chkread_tree( book, file ) ) {
+                       book->retVal = MGU_SUCCESS;
+               }
+               xml_close_file( file );
+       }
+       return book->retVal;
+}
+
+/*
+* Return link list of all persons in address book.  Note that the list contains
+* references to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
+* this will destroy the addressbook data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrbook_get_all_persons( AddressBookFile *book ) {
+       g_return_if_fail( book != NULL );
+       return addrcache_get_all_persons( book->addressCache );
+}
+
+/*
+* Add person and address data to address book.
+* Enter: book      Address book.
+*        folder    Folder where to add person, or NULL for root folder.
+*        name      Common name.
+*        address   EMail address.
+*        remarks   Remarks.
+* Return: Person added. Do not *NOT* to use the addrbook_free_xxx() functions...
+* this will destroy the address book data.
+*/
+ItemPerson *addrbook_add_contact( AddressBookFile *book, ItemFolder *folder, const gchar *name,
+               const gchar *address, const gchar *remarks )
+{
+       ItemPerson *person = NULL;
+       g_return_if_fail( book != NULL );
+       person = addrcache_add_contact( book->addressCache, folder, name, address, remarks );
+       if( person ) book->dirtyFlag = TRUE;
+       return person;
+}
+
+/*
+* End of Source.
+*/
+
diff --git a/src/addrbook.h b/src/addrbook.h
new file mode 100644 (file)
index 0000000..3f6002e
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright           (C) 2001 Match Grun
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ *             (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * Definitions necessary to access external address book files.
+ */
+
+#ifndef __ADDRBOOK_H__
+#define __ADDRBOOK_H__
+
+#include <stdio.h>
+#include <glib.h>
+#include <setjmp.h>
+
+#include "addritem.h"
+#include "addrcache.h"
+
+// Address book file.
+typedef struct _AddressBookFile AddressBookFile;
+struct _AddressBookFile {
+       gchar *name;
+       gchar *path;
+       gchar *fileName;
+       AddressCache *addressCache;
+       gint  retVal;
+       gint  maxValue;
+       GList *tempList;
+       GHashTable *tempHash;
+       gboolean readFlag;
+       gboolean dirtyFlag;
+       gboolean modifyFlag;
+       gboolean accessFlag;
+       jmp_buf jumper;
+};
+
+// Function prototypes
+
+AddressBookFile *addrbook_create_book  ( void );
+void addrbook_empty_book               ( AddressBookFile *book );
+void addrbook_free_book                        ( AddressBookFile *book );
+void addrbook_print_book               ( AddressBookFile *book, FILE *stream );
+void addrbook_dump_hash                        ( AddressBookFile *book, FILE *stream );
+void addrbook_dump_book                        ( AddressBookFile *book, FILE *stream );
+void addrbook_set_name                 ( AddressBookFile *book, const gchar *value );
+void addrbook_set_path                 ( AddressBookFile *book, const gchar *value );
+void addrbook_set_file                 ( AddressBookFile *book, const gchar *value );
+void addrbook_set_accessed             ( AddressBookFile *book, const gboolean value );
+gboolean addrbook_get_modified         ( AddressBookFile *book );
+gboolean addrbook_get_accessed         ( AddressBookFile *book );
+gboolean addrbook_get_read_flag                ( AddressBookFile *book );
+gint addrbook_get_status               ( AddressBookFile *book );
+ItemFolder *addrbook_get_root_folder   ( AddressBookFile *book );
+GList *addrbook_get_list_folder                ( AddressBookFile *book );
+GList *addrbook_get_list_person                ( AddressBookFile *book );
+gchar *addrbook_get_name               ( AddressBookFile *book );
+
+ItemPerson *addrbook_remove_person     ( AddressBookFile *book, ItemPerson *person );
+ItemGroup *addrbook_remove_group       ( AddressBookFile *book, ItemGroup *group );
+ItemEMail *addrbook_person_remove_email        ( AddressBookFile *book, ItemPerson *person,
+                                         ItemEMail *email );
+
+gint addrbook_read_data                        ( AddressBookFile *book );
+gint addrbook_save_data                        ( AddressBookFile *book );
+
+ItemEMail *addrbook_move_email_before  ( AddressBookFile *book, ItemPerson *person,
+                                         ItemEMail *itemMove, ItemEMail *itemTarget );
+ItemEMail *addrbook_move_email_after   ( AddressBookFile *book, ItemPerson *person,
+                                         ItemEMail *itemMove, ItemEMail *itemTarget );
+
+void addrbook_update_address_list      ( AddressBookFile *book, ItemPerson *person,
+                                         GList *listEMail );
+ItemPerson *addrbook_add_address_list  ( AddressBookFile *book, ItemFolder *folder,
+                                         GList *listEMail );
+GList *addrbook_get_available_email_list( AddressBookFile *book, ItemGroup *group );
+void addrbook_update_group_list                ( AddressBookFile *book, ItemGroup *group,
+                                         GList *listEMail );
+ItemGroup *addrbook_add_group_list     ( AddressBookFile *book, ItemFolder *folder,
+                                         GList *listEMail );
+ItemFolder *addrbook_add_new_folder    ( AddressBookFile *book, ItemFolder *parent );
+
+ItemFolder *addrbook_remove_folder     ( AddressBookFile *book, ItemFolder *folder );
+ItemFolder *addrbook_remove_folder_delete( AddressBookFile *book, ItemFolder *folder );
+
+GList *addrbook_get_bookfile_list      ( AddressBookFile *book );
+gchar *addrbook_gen_new_file_name      ( gint fileNum );
+gint addrbook_test_read_file           ( AddressBookFile *book, gchar *fileName );
+
+GList *addrbook_get_all_persons                ( AddressBookFile *book );
+
+ItemPerson *addrbook_add_contact       ( AddressBookFile *book, ItemFolder *folder,
+                                         const gchar *name, const gchar *address,
+                                         const gchar *remarks );
+
+#endif /* __ADDRBOOK_H__ */
+
+/*
+* End of Source.
+*/
+
+
diff --git a/src/addrcache.c b/src/addrcache.c
new file mode 100644 (file)
index 0000000..c9bc33b
--- /dev/null
@@ -0,0 +1,1164 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001 Match Grun
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * Functions to maintain address cache.
+ */
+
+#include <stdio.h>
+#include <sys/stat.h>
+#include <glib.h>
+
+// #include "mgutils.h"
+#include "addritem.h"
+#include "addrcache.h"
+
+#define ID_TIME_OFFSET             998000000
+#define ADDRCACHE_MAX_SEARCH_COUNT 1000
+
+/*
+* Create new address cache.
+*/
+AddressCache *addrcache_create() {
+       AddressCache *cache;
+       gint t;
+
+       cache = g_new0( AddressCache, 1 );
+       cache->itemHash = g_hash_table_new( g_str_hash, g_str_equal );
+
+       cache->dataRead = FALSE;
+       cache->modified = FALSE;
+       cache->modifyTime = 0;
+
+       // Generate the next ID using system time
+       cache->nextID = 1;
+       t = time( NULL );
+       if( t > 0 ) {
+               cache->nextID = t - ID_TIME_OFFSET;
+       }
+
+       cache->tempList = NULL;
+       cache->rootFolder = addritem_create_item_folder();
+       cache->rootFolder->isRoot = TRUE;
+       ADDRITEM_PARENT(cache->rootFolder) = NULL;
+       return cache;
+}
+
+/*
+* Properties.
+*/
+ItemFolder *addrcache_get_root_folder( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+       return cache->rootFolder;
+}
+GList *addrcache_get_list_folder( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+       return cache->rootFolder->listFolder;
+}
+GList *addrcache_get_list_person( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+       return cache->rootFolder->listPerson;
+}
+
+/*
+* Generate next ID.
+*/
+void addrcache_next_id( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+       cache->nextID++;
+}
+
+/*
+* Refresh internal variables. This can be used force a reload.
+*/
+void addrcache_refresh( AddressCache *cache ) {
+       cache->dataRead = FALSE;
+       cache->modified = TRUE;
+       cache->modifyTime = 0;
+}
+
+/*
+* Free hash table visitor function.
+*/
+static gint addrcache_free_item_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+               addritem_free_item_person( ( ItemPerson * ) obj );
+       }
+       else if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+               addritem_free_item_group( ( ItemGroup * ) obj );
+       }
+       else if( ADDRITEM_TYPE(obj) == ITEMTYPE_FOLDER ) {
+               addritem_free_item_folder( ( ItemFolder * ) obj );
+       }
+       key = NULL;
+       value = NULL;
+       return 0;
+}
+
+/*
+* Free hash table of address cache items.
+*/
+static void addrcache_free_item_hash( GHashTable *table ) {
+       g_return_if_fail( table != NULL );
+       g_hash_table_freeze( table );
+       g_hash_table_foreach_remove( table, addrcache_free_item_vis, NULL );
+       g_hash_table_thaw( table );
+       g_hash_table_destroy( table );
+}
+
+/*
+* Free up folders and groups.
+*/
+static void addrcache_free_all_folders( ItemFolder *parent ) {
+       GList *node = parent->listFolder;
+       while( node ) {
+               ItemFolder *folder = node->data;
+               addrcache_free_all_folders( folder );
+               node = g_list_next( node );
+       }
+       g_list_free( parent->listPerson );
+       g_list_free( parent->listGroup );
+       g_list_free( parent->listFolder );
+       parent->listPerson = NULL;
+       parent->listGroup = NULL;
+       parent->listFolder = NULL;
+}
+
+/*
+* Clear the address cache.
+*/
+void addrcache_clear( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+
+       // Free up folders and hash table
+       addrcache_free_all_folders( cache->rootFolder );
+       addrcache_free_item_hash( cache->itemHash );
+       cache->itemHash = NULL;
+       ADDRITEM_PARENT(cache->rootFolder) = NULL;
+       addritem_free_item_folder( cache->rootFolder );
+       cache->rootFolder = NULL;
+       g_list_free( cache->tempList );
+       cache->tempList = NULL;
+
+       // Reset to initial state
+       cache->itemHash = g_hash_table_new( g_str_hash, g_str_equal );
+       cache->rootFolder = addritem_create_item_folder();
+       cache->rootFolder->isRoot = TRUE;
+       ADDRITEM_PARENT(cache->rootFolder) = NULL;
+
+       addrcache_refresh( cache );
+
+}
+
+/*
+* Free address cache.
+*/
+void addrcache_free( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+
+       addrcache_free_all_folders( cache->rootFolder );
+       addrcache_free_item_hash( cache->itemHash );
+       cache->itemHash = NULL;
+       ADDRITEM_PARENT(cache->rootFolder) = NULL;
+       addritem_free_item_folder( cache->rootFolder );
+       cache->rootFolder = NULL;
+       g_list_free( cache->tempList );
+       cache->tempList = NULL;
+       g_free( cache );
+}
+
+/*
+* Check whether file has changed by comparing with cache.
+* return: TRUE if file has changed.
+*/
+gboolean addrcache_check_file( AddressCache *cache, gchar *path ) {
+       gboolean retVal;
+       struct stat filestat;
+       retVal = TRUE;
+       if( path ) {
+               if( 0 == lstat( path, &filestat ) ) {
+                       if( filestat.st_mtime == cache->modifyTime ) retVal = FALSE;
+               }
+       }
+       return retVal;
+}
+
+/*
+* Save file time to cache.
+* return: TRUE if time marked.
+*/
+gboolean addrcache_mark_file( AddressCache *cache, gchar *path ) {
+       gboolean retVal = FALSE;
+       struct stat filestat;
+       if( path ) {
+               if( 0 == lstat( path, &filestat ) ) {
+                       cache->modifyTime = filestat.st_mtime;
+                       retVal = TRUE;
+               }
+       }
+       return retVal;
+}
+
+/*
+* Print list of items.
+*/
+void addrcache_print_item_list( GList *list, FILE *stream ) {
+       GList *node = list;
+       while( node ) {
+               AddrItemObject *obj = node->data;
+               if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+                       addritem_print_item_person( ( ItemPerson * ) obj, stream );
+               }
+               else if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+                       addritem_print_item_group( ( ItemGroup * ) obj, stream );
+               }
+               else if( ADDRITEM_TYPE(obj) == ITEMTYPE_FOLDER ) {
+                       addritem_print_item_folder( ( ItemFolder * ) obj, stream );
+               }
+               node = g_list_next( node );
+       }
+       fprintf( stream, "\t---\n" );
+}
+
+/*
+* Print item hash table visitor function.
+*/
+static void addrcache_print_item_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       FILE *stream = ( FILE * ) data;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+               addritem_print_item_person( ( ItemPerson * ) obj, stream );
+       }
+       else if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+               addritem_print_item_group( ( ItemGroup * ) obj, stream );
+       }
+       else if( ADDRITEM_TYPE(obj) == ITEMTYPE_FOLDER ) {
+               addritem_print_item_folder( ( ItemFolder * ) obj, stream );
+       }
+}
+
+/*
+* Dump entire address cache hash table contents.
+*/
+void addrcache_print( AddressCache *cache, FILE *stream ) {
+       g_return_if_fail( cache != NULL );
+       fprintf( stream, "AddressCache:\n" );
+       fprintf( stream, "next id  : %d\n", cache->nextID );
+       fprintf( stream, "mod time : %d\n", cache->modifyTime );
+       fprintf( stream, "modified : %s\n", cache->modified ? "yes" : "no" );
+       fprintf( stream, "data read: %s\n", cache->dataRead ? "yes" : "no" );
+}
+
+/*
+* Dump entire address cache hash table contents.
+*/
+void addrcache_dump_hash( AddressCache *cache, FILE *stream ) {
+       g_return_if_fail( cache != NULL );
+       addrcache_print( cache, stream );
+       g_hash_table_foreach( cache->itemHash, addrcache_print_item_vis, stream );
+}
+
+/*
+ * Allocate ID for person.
+ */
+void addrcache_id_person( AddressCache *cache, ItemPerson *person ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( person != NULL );
+       if( ADDRITEM_ID(person) ) return;
+       addrcache_next_id( cache );
+       ADDRITEM_ID(person) = g_strdup_printf( "%d", cache->nextID );
+}
+
+/*
+ * Allocate ID for group.
+ */
+void addrcache_id_group( AddressCache *cache, ItemGroup *group ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( group != NULL );
+       if( ADDRITEM_ID(group) ) return;
+       addrcache_next_id( cache );
+       ADDRITEM_ID(group) = g_strdup_printf( "%d", cache->nextID );
+}
+
+/*
+ * Allocate ID for folder.
+ */
+void addrcache_id_folder( AddressCache *cache, ItemFolder *folder ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( folder != NULL );
+       if( ADDRITEM_ID(folder) ) return;
+       addrcache_next_id( cache );
+       ADDRITEM_ID(folder) = g_strdup_printf( "%d", cache->nextID );
+}
+
+/*
+ * Allocate ID for email address.
+ */
+void addrcache_id_email( AddressCache *cache, ItemEMail *email ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( email != NULL );
+       if( ADDRITEM_ID(email) ) return;
+       addrcache_next_id( cache );
+       ADDRITEM_ID(email) = g_strdup_printf( "%d", cache->nextID );
+}
+
+/*
+ * Allocate ID for user attribute.
+ */
+void addrcache_id_attribute( AddressCache *cache, UserAttribute *attrib ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( attrib != NULL );
+       if( attrib->uid ) return;
+       addrcache_next_id( cache );
+       attrib->uid = g_strdup_printf( "%d", cache->nextID );
+}
+
+/*
+* Add person to hash table.
+* return: TRUE if item added.
+*/
+gboolean addrcache_hash_add_person( AddressCache *cache, ItemPerson *person ) {
+       if( g_hash_table_lookup( cache->itemHash, ADDRITEM_ID(person) ) ) {
+               return FALSE;
+       }
+       g_hash_table_insert( cache->itemHash, ADDRITEM_ID(person), person );
+       return TRUE;
+}
+
+/*
+* Add group to hash table.
+* return: TRUE if item added.
+*/
+gboolean addrcache_hash_add_group( AddressCache *cache, ItemGroup *group ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( group != NULL );
+       if( g_hash_table_lookup( cache->itemHash, ADDRITEM_ID(group) ) ) {
+               return FALSE;
+       }
+       g_hash_table_insert( cache->itemHash, ADDRITEM_ID(group), group );
+       return TRUE;
+}
+
+/*
+* Add folder to hash table.
+* return: TRUE if item added.
+*/
+gboolean addrcache_hash_add_folder( AddressCache *cache, ItemFolder *folder ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( folder != NULL );
+       if( g_hash_table_lookup( cache->itemHash, ADDRITEM_ID(folder) ) ) {
+               return FALSE;
+       }
+       g_hash_table_insert( cache->itemHash, ADDRITEM_ID(folder), folder );
+       return TRUE;
+}
+
+/*
+* Add person to specified folder in cache.
+*/
+gboolean addrcache_folder_add_person( AddressCache *cache, ItemFolder *folder, ItemPerson *item ) {
+       gboolean retVal = FALSE;
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( folder != NULL );
+       g_return_if_fail( item != NULL );
+       retVal = addrcache_hash_add_person( cache, item );
+       if( retVal ) {
+               addritem_folder_add_person( folder, item );
+       }
+       return retVal;
+}
+
+/*
+* Add folder to specified folder in cache.
+*/
+gboolean addrcache_folder_add_folder( AddressCache *cache, ItemFolder *folder, ItemFolder *item ) {
+       gboolean retVal = FALSE;
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( folder != NULL );
+       g_return_if_fail( item != NULL );
+       retVal = addrcache_hash_add_folder( cache, item );
+       if( retVal ) {
+               addritem_folder_add_folder( folder, item );
+       }
+       return TRUE;
+}
+
+/*
+* Add folder to specified folder in cache.
+*/
+gboolean addrcache_folder_add_group( AddressCache *cache, ItemFolder *folder, ItemGroup *item ) {
+       gboolean retVal = FALSE;
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( folder != NULL );
+       g_return_if_fail( item != NULL );
+       retVal = addrcache_hash_add_group( cache, item );
+       if( retVal ) {
+               addritem_folder_add_group( folder, item );
+       }
+       return retVal;
+}
+
+/*
+* Add person to address cache.
+* return: TRUE if item added.
+*/
+gboolean addrcache_add_person( AddressCache *cache, ItemPerson *person ) {
+       gboolean retVal = FALSE;
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( person != NULL );
+       retVal = addrcache_hash_add_person( cache, person );
+       if( retVal ) {
+               addritem_folder_add_person( cache->rootFolder, person );
+       }
+       return retVal;
+}
+
+/*
+* Add EMail address to person.
+* return: TRUE if item added.
+*/
+gboolean addrcache_person_add_email( AddressCache *cache, ItemPerson *person, ItemEMail *email ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( person != NULL );
+       g_return_if_fail( email != NULL );
+       addritem_person_add_email( person, email );
+       return TRUE;
+}
+
+/*
+* Add group to address cache.
+* return: TRUE if item added.
+*/
+gboolean addrcache_add_group( AddressCache *cache, ItemGroup *group ) {
+       gboolean retVal = FALSE;
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( group != NULL );
+       retVal = addrcache_hash_add_group( cache, group );
+       if( retVal ) {
+               addritem_folder_add_group( cache->rootFolder, group );
+       }
+       return retVal;
+}
+
+/*
+* Add EMail address to person.
+* return: TRUE if item added.
+*/
+gboolean addrcache_group_add_email( AddressCache *cache, ItemGroup *group, ItemEMail *email ) {
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( group != NULL );
+       g_return_if_fail( email != NULL );
+       addritem_group_add_email( group, email );
+       return TRUE;
+}
+
+/*
+* Add folder to address cache.
+* return: TRUE if item added.
+*/
+gboolean addrcache_add_folder( AddressCache *cache, ItemFolder *folder ) {
+       gboolean retVal = FALSE;
+       g_return_if_fail( cache != NULL );
+       g_return_if_fail( folder != NULL );
+       retVal = addrcache_hash_add_folder( cache, folder );
+       if( retVal ) {
+               addritem_folder_add_folder( cache->rootFolder, folder );
+       }
+       return retVal;
+}
+
+/*
+* Return pointer to object (either person or group) for specified ID.
+* param: uid Object ID.
+* return: Object, or NULL if not found.
+*/
+AddrItemObject *addrcache_get_object( AddressCache *cache, const gchar *uid ) {
+       AddrItemObject *obj = NULL;
+       gchar *uidH;
+
+       g_return_if_fail( cache != NULL );
+       if( uid == NULL || *uid == '\0' ) return NULL;
+       obj = ( AddrItemObject * ) g_hash_table_lookup( cache->itemHash, uid );
+       if( obj ) {
+               // Check for matching UID
+               uidH = ADDRITEM_ID(obj);
+               if( uidH ) {
+                       if( strcmp( uidH, uid ) == 0 ) return obj;
+               }
+       }
+       return NULL;
+}
+
+/*
+* Return pointer for specified object ID.
+* param: uid Object ID.
+* return: Person object, or NULL if not found.
+*/
+ItemPerson *addrcache_get_person( AddressCache *cache, const gchar *uid ) {
+       ItemPerson *person = NULL;
+       AddrItemObject *obj = addrcache_get_object( cache, uid );
+       if( obj ) {
+               if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+                       person = ( ItemPerson * ) obj;
+               }
+       }
+       return person;
+}
+
+/*
+* Return pointer for specified object ID.
+* param: uid group ID.
+* return: Group object, or NULL if not found.
+*/
+ItemGroup *addrcache_get_group( AddressCache *cache, const gchar *uid ) {
+       ItemGroup *group = NULL;
+       AddrItemObject *obj = addrcache_get_object( cache, uid );
+       if( obj ) {
+               if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+                       group = ( ItemGroup * ) obj;
+               }
+       }
+       return group;
+}
+
+/*
+* Find email address in address cache.
+* param: uid   Object ID for person.
+*        eid   EMail ID.
+* return: email object for specified object ID and email ID, or NULL if not found.
+*/
+ItemEMail *addrcache_get_email( AddressCache *cache, const gchar *uid, const gchar *eid ) {
+       AddrItemObject *objP;
+       if( eid == NULL || *eid == '\0' ) return NULL;
+
+       objP = addrcache_get_object( cache, uid );
+       if( objP ) {
+               if( ADDRITEM_TYPE(objP) == ITEMTYPE_PERSON ) {
+                       // Sequential search through email addresses
+                       ItemPerson *person = ( ItemPerson * ) objP;
+                       GList *nodeMail = person->listEMail;
+                       while( nodeMail ) {
+                               AddrItemObject *objE = nodeMail->data;
+                               gchar *ide = ADDRITEM_ID(objE);
+                               if( ide ) {
+                                       if( strcmp( ide, eid ) == 0 ) {
+                                               return ( ItemEMail * ) objE;
+                                       }
+                               }
+                               nodeMail = g_list_next( nodeMail );
+                       }
+               }
+       }
+       return NULL;
+}
+
+/*
+* Remove attribute from person.
+* param: uid   Object ID for person.
+*        aid   Attribute ID.
+* return: UserAttribute object, or NULL if not found. Note that object should still be freed.
+*/
+UserAttribute *addrcache_person_remove_attrib_id( AddressCache *cache, const gchar *uid, const gchar *aid ) {
+       UserAttribute *attrib = NULL;
+       ItemPerson *person;
+       if( aid == NULL || *aid == '\0' ) return NULL;
+
+       person = addrcache_get_person( cache, uid );
+       if( person ) {
+               attrib = addritem_person_remove_attrib_id( person, aid );
+       }
+       return attrib;
+}
+
+/*
+* Remove attribute from person.
+* param: person        Person.
+*        attrib        Attribute to remove.
+* return: UserAttribute object. Note that object should still be freed.
+*/
+UserAttribute *addrcache_person_remove_attribute( AddressCache *cache, ItemPerson *person, UserAttribute *attrib ) {
+       UserAttribute *found = NULL;
+       g_return_if_fail( cache != NULL );
+       if( person && attrib ) {
+               found = addritem_person_remove_attribute( person, attrib );
+       }
+       return found;
+}
+
+/*
+* Remove group from address cache for specified ID.
+* param: uid Object ID.
+* return: Group, or NULL if not found. Note that object should still be freed.
+*/
+ItemGroup *addrcache_remove_group_id( AddressCache *cache, const gchar *uid ) {
+       AddrItemObject *obj = NULL;
+
+       g_return_if_fail( cache != NULL );
+       if( uid == NULL || *uid == '\0' ) return NULL;
+       obj = ( AddrItemObject * ) g_hash_table_lookup( cache->itemHash, uid );
+       if( obj ) {
+               if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+                       ItemGroup *group = ( ItemGroup * ) obj;
+                       ItemFolder *parent = ( ItemFolder * ) ADDRITEM_PARENT(group);
+                       if( ! parent ) parent = cache->rootFolder;
+                       // Remove group from parent's list and hash table
+                       parent->listGroup = g_list_remove( parent->listGroup, group );
+                       g_hash_table_remove( cache->itemHash, uid );
+                       return ( ItemGroup * ) obj;
+               }
+       }
+       return NULL;
+}
+
+/*
+* Remove group from address cache.
+* param: group Group to remove.
+* return: Group, or NULL if not found. Note that object should still be freed.
+*/
+ItemGroup *addrcache_remove_group( AddressCache *cache, ItemGroup *group ) {
+       AddrItemObject *obj = NULL;
+
+       g_return_if_fail( cache != NULL );
+       if( group ) {
+               gchar *uid = ADDRITEM_ID(group);
+               if( uid == NULL || *uid == '\0' ) return NULL;
+               obj = ( AddrItemObject * ) g_hash_table_lookup( cache->itemHash, uid );
+               if( obj ) {
+                       ItemGroup *item = ( ItemGroup * ) obj;
+                       ItemFolder *parent = ( ItemFolder * ) ADDRITEM_PARENT(group);
+                       if( ! parent ) parent = cache->rootFolder;
+
+                       // Remove group from parent's list and hash table
+                       parent->listGroup = g_list_remove( parent->listGroup, obj );
+                       g_hash_table_remove( cache->itemHash, uid );
+                       return group;
+               }
+       }
+       return NULL;
+}
+
+/*
+* Remove person's email address from all groups in folder.
+*/
+static void addrcache_foldergrp_rem_person( ItemFolder *folder, ItemPerson *person ) {
+       GList *nodeGrp = folder->listGroup;
+       while( nodeGrp ) {
+               ItemGroup *group = nodeGrp->data;
+               if( group ) {
+                       // Remove each email address that belongs to the person from the list
+                       GList *node = person->listEMail;
+                       while( node ) {
+                               group->listEMail = g_list_remove( group->listEMail, node->data );
+                               node = g_list_next( node );
+                       }
+               }
+               nodeGrp = g_list_next( nodeGrp );
+       }
+}
+
+/*
+* Remove person from address cache for specified ID. Note that person still retains
+* their EMail addresses. Also, links to these email addresses will be severed from
+* the group.
+* param: uid Object ID.
+* return: Person, or NULL if not found. Note that object should still be freed.
+*/
+ItemPerson *addrcache_remove_person_id( AddressCache *cache, const gchar *uid ) {
+       AddrItemObject *obj = NULL;
+
+       g_return_if_fail( cache != NULL );
+       if( uid == NULL || *uid == '\0' ) return NULL;
+       obj = ( AddrItemObject * ) g_hash_table_lookup( cache->itemHash, uid );
+       if( obj ) {
+               if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+                       // Remove person's email addresses from all groups where
+                       // referenced and from hash table.
+                       ItemPerson *person = ( ItemPerson * ) obj;
+                       ItemFolder *parent = ( ItemFolder * ) ADDRITEM_PARENT(person);
+                       if( ! parent ) parent = cache->rootFolder;
+                       // Remove emails from groups, remove from parent's list
+                       // and hash table
+                       addrcache_foldergrp_rem_person( parent, person );
+                       parent->listPerson = g_list_remove( parent->listPerson, person );
+                       g_hash_table_remove( cache->itemHash, uid );
+                       return person;
+               }
+       }
+       return NULL;
+}
+
+/*
+* Remove specified person from address cache.
+* param: person        Person to remove.
+* return: Person, or NULL if not found. Note that object should still be freed.
+*/
+ItemPerson *addrcache_remove_person( AddressCache *cache, ItemPerson *person ) {
+       AddrItemObject *obj = NULL;
+
+       g_return_if_fail( cache != NULL );
+       if( person ) {
+               gchar *uid = ADDRITEM_ID(person);
+               if( uid == NULL || *uid == '\0' ) return NULL;
+               obj = ( AddrItemObject * ) g_hash_table_lookup( cache->itemHash, uid );
+               if( obj ) {
+                       if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+                               // Remove person's email addresses from all groups where
+                               // referenced and from hash table.
+                               ItemFolder *parent = ( ItemFolder * ) ADDRITEM_PARENT(person);
+                               if( ! parent ) parent = cache->rootFolder;
+                               addrcache_foldergrp_rem_person( parent, person );
+                               parent->listPerson = g_list_remove( parent->listPerson, person );
+                               g_hash_table_remove( cache->itemHash, uid );
+                               return person;
+                       }
+               }
+       }
+       return NULL;
+}
+
+/*
+* Remove email from group item hash table visitor function.
+*/
+static void addrcache_allgrp_rem_email_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       ItemEMail *email = ( ItemEMail * ) data;
+       if( ! email ) return;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+               ItemGroup *group = ( ItemGroup * ) value;
+               if( group ) {
+                       // Remove each email address that belongs to the person from the list
+                       group->listEMail = g_list_remove( group->listEMail, email );
+               }
+       }
+}
+
+/*
+* Remove email address in address cache for specified ID.
+* param: uid   Object ID for person.
+*        eid   EMail ID.
+* return: EMail object, or NULL if not found. Note that object should still be freed.
+*/
+ItemEMail *addrcache_person_remove_email_id( AddressCache *cache, const gchar *uid, const gchar *eid ) {
+       ItemEMail *email = NULL;
+       ItemPerson *person;
+       if( eid == NULL || *eid == '\0' ) return NULL;
+
+       person = addrcache_get_person( cache, uid );
+       if( person ) {
+               email = addritem_person_remove_email_id( person, eid );
+               if( email ) {
+                       // Remove email from all groups.
+                       g_hash_table_foreach( cache->itemHash, addrcache_allgrp_rem_email_vis, email );
+
+                       // Remove email from person's address list
+                       if( person->listEMail ) {
+                               person->listEMail = g_list_remove( person->listEMail, email );
+                       }
+                       // Unlink reference to person.
+                       ADDRITEM_PARENT(email) = NULL;
+               }
+       }
+       return email;
+}
+
+/*
+* Remove email address in address cache for specified person.
+* param: person        Person.
+*        email EMail to remove.
+* return: EMail object, or NULL if not found. Note that object should still be freed.
+*/
+ItemEMail *addrcache_person_remove_email( AddressCache *cache, ItemPerson *person, ItemEMail *email ) {
+       ItemEMail *found = NULL;
+       g_return_if_fail( cache != NULL );
+       if( person && email ) {
+               found = addritem_person_remove_email( person, email );
+               if( found ) {
+                       // Remove email from all groups.
+                       g_hash_table_foreach( cache->itemHash, addrcache_allgrp_rem_email_vis, email );
+
+                       // Remove email from person's address list
+                       if( person->listEMail ) {
+                               person->listEMail = g_list_remove( person->listEMail, email );
+                       }
+                       // Unlink reference to person.
+                       ADDRITEM_PARENT(email) = NULL;
+               }
+       }
+       return found;
+}
+
+/*
+* Return link list of address items for root level folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to use the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_folder_get_address_list( AddressCache *cache, ItemFolder *folder ) {
+       GList *list = NULL;
+       GList *node = NULL;
+       ItemFolder *f = folder;
+       g_return_if_fail( cache != NULL );
+
+       if( ! f ) f = cache->rootFolder;
+       node = f->listPerson;
+       while( node ) {
+               list = g_list_append( list, node->data );
+               node = g_list_next( node );
+       }
+       node = f->listGroup;
+       while( node ) {
+               list = g_list_append( list, node->data );
+               node = g_list_next( node );
+       }
+       return list;
+}
+
+/*
+* Return link list of persons for specified folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to use the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_folder_get_person_list( AddressCache *cache, ItemFolder *folder ) {
+       GList *list = NULL;
+       GList *node = NULL;
+       ItemFolder *f = folder;
+       g_return_if_fail( cache != NULL );
+
+       if( ! f ) f = cache->rootFolder;
+       return addritem_folder_get_person_list( f );
+}
+
+/*
+* Return link list of group items for specified folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to use the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_folder_get_group_list( AddressCache *cache, ItemFolder *folder ) {
+       GList *node = NULL;
+       GList *list = NULL;
+       ItemFolder *f = folder;
+       g_return_if_fail( cache != NULL );
+       if( ! f ) f = cache->rootFolder;
+       return addritem_folder_get_group_list( f );
+}
+
+/*
+* Return link list of folder items for specified folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to used the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_folder_get_folder_list( AddressCache *cache, ItemFolder *folder ) {
+       GList *node = NULL;
+       GList *list = NULL;
+       ItemFolder *f = folder;
+       g_return_if_fail( cache != NULL );
+       if( ! f ) f = cache->rootFolder;
+       node = f->listFolder;
+       while( node ) {
+               list = g_list_append( list, node->data );
+               node = g_list_next( node );
+       }
+       return list;
+}
+
+/*
+* Return link list of address items for root level folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to used the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_get_address_list( AddressCache *cache ) {
+       GList *list = NULL;
+       g_return_if_fail( cache != NULL );
+       return addrcache_folder_get_address_list( cache, cache->rootFolder );
+}
+
+/*
+* Return link list of persons for root level folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to used the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_get_person_list( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+       return addritem_folder_get_person_list( cache->rootFolder );
+}
+
+/*
+* Return link list of group items in root level folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to used the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_get_group_list( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+       return cache->rootFolder->listGroup;
+}
+
+/*
+* Return link list of folder items in root level folder. Note that the list contains
+* references to items and should be g_free() when done. Do *NOT* attempt to used the
+* addrcache_free_xxx() functions... this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_get_folder_list( AddressCache *cache ) {
+       g_return_if_fail( cache != NULL );
+       return cache->rootFolder->listFolder;
+}
+
+/*
+* Group visitor function.
+*/
+static void addrcache_get_grp_person_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_GROUP ) {
+               AddressCache *cache = data;
+               ItemGroup *group = ( ItemGroup * ) obj;
+               ItemPerson *person = ( ItemPerson * ) cache->tempList->data;
+               GList *node = group->listEMail;
+               while( node ) {
+                       ItemEMail *email = ( ItemEMail * ) node->data;
+                       if( ADDRITEM_PARENT(email) == ADDRITEM_OBJECT(person) ) {
+                               if( ! g_list_find( cache->tempList, group ) ) {
+                                       cache->tempList = g_list_append( cache->tempList, group );
+                               }
+                       }
+                       node = g_list_next( node );
+               }
+       }
+}
+
+/*
+* Return link list of groups which contain a reference to specified person's email
+* address.
+*/
+GList *addrcache_get_group_for_person( AddressCache *cache, ItemPerson *person ) {
+       GList *list = NULL;
+       g_return_if_fail( cache != NULL );
+       cache->tempList = NULL;
+       cache->tempList = g_list_append( cache->tempList, person );
+       g_hash_table_foreach( cache->itemHash, addrcache_get_grp_person_vis, cache );
+       cache->tempList = g_list_remove( cache->tempList, person );
+       list = cache->tempList;
+       cache->tempList = NULL;
+       return list;
+}
+
+/*
+* Find root folder for specified folder.
+* Enter: folder Folder to search.
+* Return: root folder, or NULL if not found.
+*/
+ItemFolder *addrcache_find_root_folder( ItemFolder *folder ) {
+       ItemFolder *item = folder;
+       gint count = 0;
+       while( item ) {
+               if( item->isRoot ) break;
+               if( ++count > ADDRCACHE_MAX_SEARCH_COUNT ) {
+                       item = NULL;
+                       break;
+               }
+               item = ( ItemFolder * ) ADDRITEM_PARENT(folder);
+       }
+       return item;
+}
+
+/*
+* Get all person visitor function.
+*/
+static void addrcache_get_all_persons_vis( gpointer key, gpointer value, gpointer data ) {
+       AddrItemObject *obj = ( AddrItemObject * ) value;
+       if( ADDRITEM_TYPE(obj) == ITEMTYPE_PERSON ) {
+               AddressCache *cache = data;
+               cache->tempList = g_list_append( cache->tempList, obj );
+       }
+}
+
+/*
+* Return link list of all persons in address cache.  Note that the list contains
+* references to items. Do *NOT* attempt to use the addrcache_free_xxx() functions...
+* this will destroy the address cache data!
+* Return: List of items, or NULL if none.
+*/
+GList *addrcache_get_all_persons( AddressCache *cache ) {
+       GList *list = NULL;
+       g_return_if_fail( cache != NULL );
+
+       cache->tempList = NULL;
+       g_hash_table_foreach( cache->itemHash, addrcache_get_all_persons_vis, cache );
+       list = cache->tempList;
+       cache->tempList = NULL;
+       return list;
+}
+
+/*
+* Remove folder from cache. Children are re-parented to parent folder.
+* param: folder Folder to remove.
+* return: Folder, or NULL if not found. Note that object should still be freed.
+*/
+ItemFolder *addrcache_remove_folder( AddressCache *cache, ItemFolder *folder ) {
+       AddrItemObject *obj = NULL;
+
+       g_return_if_fail( cache != NULL );
+       if( folder ) {
+               gchar *uid = ADDRITEM_ID(folder);
+               if( uid == NULL || *uid == '\0' ) return NULL;
+               obj = ( AddrItemObject * ) g_hash_table_lookup( cache->itemHash, uid );
+               if( obj ) {
+                       ItemFolder *parent = ( ItemFolder * ) ADDRITEM_PARENT(folder);
+                       GList *node;
+                       AddrItemObject *aio;
+                       if( ! parent ) parent = cache->rootFolder;
+
+                       // Re-parent children in folder
+                       node = folder->listFolder;
+                       while( node ) {
+                               aio = ( AddrItemObject * ) node->data;
+                               parent->listFolder = g_list_append( parent->listFolder, aio );
+                               aio->parent = ADDRITEM_OBJECT(parent);
+                               node = g_list_next( node );
+                       }
+                       node = folder->listPerson;
+                       while( node ) {
+                               aio = ( AddrItemObject * ) node->data;
+                               parent->listPerson = g_list_append( parent->listPerson, aio );
+                               aio->parent = ADDRITEM_OBJECT(parent);
+                               node = g_list_next( node );
+                       }
+                       node = folder->listGroup;
+                       while( node ) {
+                               aio = ( AddrItemObject * ) node->data;
+                               parent->listGroup = g_list_append( parent->listGroup, aio );
+                               aio->parent = ADDRITEM_OBJECT(parent);
+                               node = g_list_next( node );
+                       }
+
+                       // Remove folder from parent's list and hash table
+                       parent->listFolder = g_list_remove( parent->listFolder, folder );
+                       ADDRITEM_PARENT(folder) = NULL;
+                       g_hash_table_remove( cache->itemHash, uid );
+                       return folder;
+               }
+       }
+       return NULL;
+}
+
+/*
+* Remove folder from cache. Children are deleted.
+* param: folder Folder to remove.
+* return: Folder, or NULL if not found. Note that object should still be freed.
+*/
+ItemFolder *addrcache_remove_folder_delete( AddressCache *cache, ItemFolder *folder ) {
+       AddrItemObject *obj = NULL;
+
+       g_return_if_fail( cache != NULL );
+       if( folder ) {
+               gchar *uid = ADDRITEM_ID(folder);
+               if( uid == NULL || *uid == '\0' ) return NULL;
+               obj = ( AddrItemObject * ) g_hash_table_lookup( cache->itemHash, uid );
+               if( obj ) {
+                       ItemFolder *parent = ( ItemFolder * ) ADDRITEM_PARENT(folder);
+                       GList *node;
+                       AddrItemObject *aio;
+                       if( ! parent ) parent = cache->rootFolder;
+
+                       // Remove groups
+                       while( folder->listGroup ) {
+                               ItemGroup *item = ( ItemGroup * ) folder->listGroup->data;
+                               item = addrcache_remove_group( cache, item );
+                               if( item ) {
+                                       addritem_free_item_group( item );
+                                       item = NULL;
+                               }
+                       }
+
+                       while( folder->listPerson ) {
+                               ItemPerson *item = ( ItemPerson * ) folder->listPerson->data;
+                               item = addrcache_remove_person( cache, item );
+                               if( item ) {
+                                       addritem_free_item_person( item );
+                                       item = NULL;
+                               }
+                       }
+
+                       // Recursive deletion of folder
+                       while( folder->listFolder ) {
+                               ItemFolder *item = ( ItemFolder * ) folder->listFolder->data;
+                               item = addrcache_remove_folder_delete( cache, item );
+                               if( item ) {
+                                       addritem_free_item_folder( item );
+                                       item = NULL;
+                               }
+                       }
+
+                       // Remove folder from parent's list and hash table
+                       parent->listFolder = g_list_remove( parent->listFolder, folder );
+                       ADDRITEM_PARENT(folder) = NULL;
+                       g_hash_table_remove( cache->itemHash, uid );
+                       return folder;
+               }
+       }
+       return NULL;
+}
+
+/*
+* Add person and address data to cache.
+* Enter: cache     Cache.
+*        folder    Folder where to add person, or NULL for root folder.
+*        name      Common name.
+*        address   EMail address.
+*        remarks   Remarks.
+* Return: Person added. Do not *NOT* to use the addrbook_free_xxx() functions...
+* this will destroy the address book data.
+*/
+ItemPerson *addrcache_add_contact( AddressCache *cache, ItemFolder *folder, const gchar *name,
+               const gchar *address, const gchar *remarks )
+{
+       ItemPerson *person = NULL;
+       ItemEMail *email = NULL;
+       ItemFolder *f = folder;
+
+       g_return_if_fail( cache != NULL );
+
+       if( ! f ) f = cache->rootFolder;
+
+       // Create person object
+       person = addritem_create_item_person();
+       addritem_person_set_common_name( person, name );
+       addrcache_id_person( cache, person );
+       addrcache_folder_add_person( cache, f, person );
+
+       // Create email object
+       email = addritem_create_item_email();
+       addritem_email_set_address( email, address );
+       addritem_email_set_remarks( email, remarks );
+       addrcache_id_email( cache, email );
+       addritem_person_add_email( person, email );
+
+       return person;
+}
+
+/*
+* End of Source.
+*/
+
diff --git a/src/addrcache.h b/src/addrcache.h
new file mode 100644 (file)
index 0000000..2dce5f1
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001 Match Grun
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * Definitions for address cache.
+ */
+
+#ifndef __ADDRCACHE_H__
+#define __ADDRCACHE_H__
+
+#include <time.h>
+#include <stdio.h>
+#include <glib.h>
+#include "addritem.h"
+
+// Address book file.
+typedef struct _AddressCache AddressCache;
+struct _AddressCache {
+       gint       nextID;
+       gboolean   dataRead;
+       gboolean   modified;
+       time_t     modifyTime;
+       GHashTable *itemHash;
+       GList      *tempList;
+       ItemFolder *rootFolder;
+};
+
+// Function prototypes
+AddressCache *addrcache_create();
+ItemFolder *addrcache_get_root_folder  ( AddressCache *cache );
+GList *addrcache_get_list_folder       ( AddressCache *cache );
+GList *addrcache_get_list_person       ( AddressCache *cache );
+
+void addrcache_refresh                 ( AddressCache *cache );
+void addrcache_empty                   ( AddressCache *cache );
+void addrcache_free                    ( AddressCache *cache );
+gboolean addrcache_check_file          ( AddressCache *cache, gchar *path );
+gboolean addrcache_mark_file           ( AddressCache *cache, gchar *path );
+
+void addrcache_print_item_list         ( GList *list, FILE *stream );
+void addrcache_print                   ( AddressCache *cache, FILE *stream );
+void addrcache_dump_hash               ( AddressCache *cache, FILE *stream );
+
+void addrcache_id_person               ( AddressCache *cache, ItemPerson *person );
+void addrcache_id_group                        ( AddressCache *cache, ItemGroup *group );
+void addrcache_id_folder               ( AddressCache *cache, ItemFolder *folder );
+void addrcache_id_email                        ( AddressCache *cache, ItemEMail *email );
+void addrcache_id_attribute            ( AddressCache *cache, UserAttribute *attrib );
+
+gboolean addrcache_hash_add_person     ( AddressCache *cache, ItemPerson *person );
+gboolean addrcache_hash_add_group      ( AddressCache *cache, ItemGroup *group );
+gboolean addrcache_hash_add_folder     ( AddressCache *cache, ItemFolder *folder );
+
+gboolean addrcache_folder_add_person   ( AddressCache *cache, ItemFolder *folder, ItemPerson *item );
+gboolean addrcache_folder_add_folder   ( AddressCache *cache, ItemFolder *folder, ItemFolder *item );
+gboolean addrcache_folder_add_group    ( AddressCache *cache, ItemFolder *folder, ItemGroup *item );
+
+gboolean addrcache_add_person          ( AddressCache *cache, ItemPerson *person );
+gboolean addrcache_add_group           ( AddressCache *cache, ItemGroup *group );
+gboolean addrcache_person_add_email    ( AddressCache *cache, ItemPerson *person, ItemEMail *email );
+gboolean addrcache_group_add_email     ( AddressCache *cache, ItemGroup *group, ItemEMail *email );
+gboolean addrcache_add_folder          ( AddressCache *cache, ItemFolder *folder );
+
+AddrItemObject *addrcache_get_object   ( AddressCache *cache, const gchar *uid );
+ItemPerson *addrcache_get_person       ( AddressCache *cache, const gchar *uid );
+ItemGroup *addrcache_get_group         ( AddressCache *cache, const gchar *uid );
+ItemEMail *addrcache_get_email         ( AddressCache *cache, const gchar *uid, const gchar *eid );
+
+UserAttribute *addrcache_person_remove_attrib_id       ( AddressCache *cache, const gchar *uid,
+                                                         const gchar *aid );
+UserAttribute *addrcache_person_remove_attribute       ( AddressCache *cache, ItemPerson *person,
+                                                         UserAttribute *attrib );
+
+ItemGroup *addrcache_remove_group_id           ( AddressCache *cache, const gchar *uid );
+ItemGroup *addrcache_remove_group              ( AddressCache *cache, ItemGroup *group );
+
+ItemPerson *addrcache_remove_person_id         ( AddressCache *cache, const gchar *uid );
+ItemPerson *addrcache_remove_person            ( AddressCache *cache, ItemPerson *person );
+ItemEMail *addrcache_person_remove_email_id    ( AddressCache *cache, const gchar *uid, const gchar *eid );
+ItemEMail *addrcache_person_remove_email       ( AddressCache *cache, ItemPerson *person, ItemEMail *email );
+
+GList *addrcache_folder_get_address_list       ( AddressCache *cache, ItemFolder *folder );
+GList *addrcache_folder_get_person_list                ( AddressCache *cache, ItemFolder *folder );
+GList *addrcache_folder_get_group_list         ( AddressCache *cache, ItemFolder *folder );
+GList *addrcache_folder_get_folder_list                ( AddressCache *cache, ItemFolder *folder );
+
+GList *addrcache_get_address_list              ( AddressCache *cache );
+GList *addrcache_get_person_list               ( AddressCache *cache );
+GList *addrcache_get_group_list                        ( AddressCache *cache );
+GList *addrcache_get_folder_list               ( AddressCache *cache );
+
+GList *addrcache_get_group_for_person          ( AddressCache *cache, ItemPerson *person );
+
+ItemFolder *addrcache_find_root_folder         ( ItemFolder *folder );
+GList *addrcache_get_all_persons               ( AddressCache *cache );
+
+ItemFolder *addrcache_remove_folder            ( AddressCache *cache, ItemFolder *folder );
+ItemFolder *addrcache_remove_folder_delete     ( AddressCache *cache, ItemFolder *folder );
+
+ItemPerson *addrcache_add_contact              ( AddressCache *cache, ItemFolder *folder,
+                                                 const gchar *name, const gchar *address,
+                                                 const gchar *remarks ); 
+
+#endif /* __ADDRCACHE_H__ */
+
+/*
+* End of Source.
+*/
+
diff --git a/src/addressadd.c b/src/addressadd.c
new file mode 100644 (file)
index 0000000..dfeaf0d
--- /dev/null
@@ -0,0 +1,401 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001 Match Grun
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * Add address to address book dialog.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "defs.h"
+
+#include <glib.h>
+#include <gdk/gdkkeysyms.h>
+#include <gtk/gtkwindow.h>
+#include <gtk/gtksignal.h>
+#include <gtk/gtkhbox.h>
+#include <gtk/gtklabel.h>
+#include <gtk/gtkentry.h>
+#include <gtk/gtkhbbox.h>
+#include <gtk/gtkbutton.h>
+
+#include "intl.h"
+#include "gtkutils.h"
+#include "prefs_common.h"
+#include "addressadd.h"
+#include "addritem.h"
+#include "addrbook.h"
+#include "addrindex.h"
+
+#include "pixmaps/book.xpm"
+#include "pixmaps/dir-open.xpm"
+
+typedef struct {
+       AddressBookFile *book;
+       ItemFolder      *folder;
+} FolderInfo;
+
+static struct _AddressAdd_dlg {
+       GtkWidget *window;
+       GtkWidget *label_name;
+       GtkWidget *label_address;
+       GtkWidget *label_remarks;
+       GtkWidget *tree_folder;
+       GtkWidget *ok_btn;
+       GtkWidget *cancel_btn;
+       GtkWidget *statusbar;
+       gint status_cid;
+       FolderInfo *fiSelected;
+} addressadd_dlg;
+
+static GdkPixmap *folderXpm;
+static GdkBitmap *folderXpmMask;
+static GdkPixmap *bookXpm;
+static GdkBitmap *bookXpmMask;
+
+static gboolean addressadd_cancelled;
+
+static FolderInfo *addressadd_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
+{
+       FolderInfo *fi = g_new0( FolderInfo, 1 );
+       fi->book   = abf;
+       fi->folder = folder;
+       return fi;
+}
+
+static void addressadd_free_folderinfo( FolderInfo *fi ) {
+       fi->book   = NULL;
+       fi->folder = NULL;
+       g_free( fi );
+}
+
+/*
+* Edit functions.
+*/
+static void addressadd_status_show( gchar *msg ) {
+       if( addressadd_dlg.statusbar != NULL ) {
+               gtk_statusbar_pop( GTK_STATUSBAR(addressadd_dlg.statusbar), addressadd_dlg.status_cid );
+               if( msg ) {
+                       gtk_statusbar_push( GTK_STATUSBAR(addressadd_dlg.statusbar), addressadd_dlg.status_cid, msg );
+               }
+       }
+}
+
+static gint addressadd_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
+       addressadd_cancelled = TRUE;
+       gtk_main_quit();
+       return TRUE;
+}
+
+static void addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
+       if (event && event->keyval == GDK_Escape) {
+               addressadd_cancelled = TRUE;
+               gtk_main_quit();
+       }
+}
+
+static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
+       addressadd_cancelled = FALSE;
+       gtk_main_quit();
+}
+
+static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
+       addressadd_cancelled = TRUE;
+       gtk_main_quit();
+}
+
+static void addressadd_folder_select( GtkCTree *ctree, gint row, gint column,
+                                       GdkEvent *event, gpointer data )
+{
+       addressadd_dlg.fiSelected = gtk_clist_get_row_data( GTK_CLIST(ctree), row );
+}
+
+static void addressadd_tree_button( GtkCTree *ctree, GdkEventButton *event, gpointer data ) {
+       if( ! event ) return;
+       if( event->button == 1 ) {
+               // Handle double click
+               if( event->type == GDK_2BUTTON_PRESS ) {
+                       addressadd_cancelled = FALSE;
+                       gtk_main_quit();
+               }
+       }
+}
+
+static void addressadd_create( void ) {
+       GtkWidget *window;
+       GtkWidget *vbox;
+       GtkWidget *table;
+       GtkWidget *label;
+       GtkWidget *label_name;
+       GtkWidget *label_addr;
+       GtkWidget *label_rems;
+       GtkWidget *tree_folder;
+       GtkWidget *vlbox;
+       GtkWidget *tree_win;
+       GtkWidget *hlbox;
+       GtkWidget *hbbox;
+       GtkWidget *hsep;
+       GtkWidget *ok_btn;
+       GtkWidget *cancel_btn;
+       GtkWidget *check_btn;
+       GtkWidget *file_btn;
+       GtkWidget *hsbox;
+       GtkWidget *statusbar;
+       gint top;
+
+       window = gtk_window_new(GTK_WINDOW_DIALOG);
+       gtk_widget_set_usize( window, 300, 400 );
+       gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
+       gtk_window_set_title( GTK_WINDOW(window), _("Add Address to Book") );
+       gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
+       gtk_window_set_modal( GTK_WINDOW(window), TRUE );
+       gtk_signal_connect( GTK_OBJECT(window), "delete_event",
+                           GTK_SIGNAL_FUNC(addressadd_delete_event), NULL );
+       gtk_signal_connect( GTK_OBJECT(window), "key_press_event",
+                           GTK_SIGNAL_FUNC(addressadd_key_pressed), NULL );
+
+       vbox = gtk_vbox_new(FALSE, 8);
+       gtk_container_add(GTK_CONTAINER(window), vbox);
+       gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
+
+       table = gtk_table_new(3, 2, FALSE);
+       gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
+       gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
+       gtk_table_set_row_spacings(GTK_TABLE(table), 8);
+       gtk_table_set_col_spacings(GTK_TABLE(table), 8);
+
+       // First row
+       top = 0;
+       label = gtk_label_new(_("Name"));
+       gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
+       gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+
+       label_name = gtk_label_new("");
+       gtk_table_attach(GTK_TABLE(table), label_name, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
+       gtk_misc_set_alignment(GTK_MISC(label_name), 0, 0.5);
+
+       // Second row
+       top = 1;
+       label = gtk_label_new(_("Address"));
+       gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
+       gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
+
+       label_addr = gtk_label_new("");
+       gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
+       gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
+
+       // Third row
+       top = 2;
+       label = gtk_label_new(_("Remarks"));
+       gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
+       gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+
+       label_rems = gtk_label_new("");
+       gtk_table_attach(GTK_TABLE(table), label_rems, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
+       gtk_misc_set_alignment(GTK_MISC(label_rems), 0, 0.5);
+
+       // Address book/folder tree
+       vlbox = gtk_vbox_new(FALSE, 8);
+       gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
+       gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 );
+
+       tree_win = gtk_scrolled_window_new( NULL, NULL );
+       gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
+                                       GTK_POLICY_AUTOMATIC,
+                                       GTK_POLICY_ALWAYS );
+       gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
+
+       tree_folder = gtk_ctree_new( 1, 0 );
+       gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
+       gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
+       gtk_clist_set_column_title( GTK_CLIST(tree_folder), 0, _( "Select Address Book Folder" ) );
+       gtk_ctree_set_line_style( GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED );
+       gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
+       gtk_ctree_set_expander_style( GTK_CTREE(tree_folder), GTK_CTREE_EXPANDER_SQUARE );
+       gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
+       gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
+
+       // Status line
+       hsbox = gtk_hbox_new(FALSE, 0);
+       gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
+       statusbar = gtk_statusbar_new();
+       gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
+
+       // Button panel
+       gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
+                               &cancel_btn, _("Cancel"), NULL, NULL);
+       gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
+       gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
+       gtk_widget_grab_default(ok_btn);
+
+       hsep = gtk_hseparator_new();
+       gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
+
+       gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
+                          GTK_SIGNAL_FUNC(addressadd_ok), NULL);
+       gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
+                          GTK_SIGNAL_FUNC(addressadd_cancel), NULL);
+       gtk_signal_connect(GTK_OBJECT(tree_folder), "select_row",
+                          GTK_SIGNAL_FUNC(addressadd_folder_select), NULL);
+       gtk_signal_connect(GTK_OBJECT(tree_folder), "button_press_event",
+                          GTK_SIGNAL_FUNC(addressadd_tree_button), NULL);
+
+       gtk_widget_show_all(vbox);
+
+       addressadd_dlg.window        = window;
+       addressadd_dlg.label_name    = label_name;
+       addressadd_dlg.label_address = label_addr;
+       addressadd_dlg.label_remarks = label_rems;
+       addressadd_dlg.tree_folder   = tree_folder;
+       addressadd_dlg.ok_btn        = ok_btn;
+       addressadd_dlg.cancel_btn    = cancel_btn;
+       addressadd_dlg.statusbar     = statusbar;
+       addressadd_dlg.status_cid    = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Address Add" );
+
+       gtk_widget_show_all( window );
+
+       PIXMAP_CREATE( window, bookXpm, bookXpmMask, book_xpm );
+       PIXMAP_CREATE( window, folderXpm, folderXpmMask, DIRECTORY_OPEN_XPM );
+}
+
+static void addressadd_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
+                                       FolderInfo *fiParent )
+{
+       GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
+       GList *list;
+       ItemFolder *folder;
+       gchar *fName;
+       gchar **name;
+       GtkCTreeNode *node;
+       FolderInfo *fi;
+
+       list = parentFolder->listFolder;
+       while( list ) {
+               folder = list->data;
+               fName = g_strdup( ADDRITEM_NAME(folder) );
+               name = &fName;
+               node = gtk_ctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
+                               folderXpm, folderXpmMask, folderXpm, folderXpmMask,
+                               FALSE, TRUE );
+               g_free( fName );
+               fi = addressadd_create_folderinfo( fiParent->book, folder );
+               gtk_ctree_node_set_row_data_full( tree, node, fi,
+                               ( GtkDestroyNotify ) addressadd_free_folderinfo );
+               addressadd_load_folder( node, folder, fi );
+               list = g_list_next( list );
+       }
+}
+
+static void addressadd_load_data( AddressIndex *addrIndex ) {
+       AddressDataSource *ds;
+       GList *list, *nodeDS;
+       gchar **name;
+       gchar *dsName;
+       ItemFolder *rootFolder;
+       AddressBookFile *abf;
+       FolderInfo *fi;
+       GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
+       GtkCTreeNode *node;
+
+       gtk_clist_clear( GTK_CLIST( tree ) );
+       list = addrindex_get_interface_list( addrIndex );
+       while( list ) {
+               AddressInterface *interface = list->data;
+               if( interface->type == ADDR_IF_BOOK ) {
+                       nodeDS = interface->listSource;
+                       while( nodeDS ) {
+                               ds = nodeDS->data;
+                               dsName = g_strdup( addrindex_ds_get_name( ds ) );
+
+                               // Read address book
+                               if( ! addrindex_ds_get_read_flag( ds ) ) {
+                                       addrindex_ds_read_data( ds );
+                               }
+
+                               // Add node for address book
+                               abf = ds->rawDataSource;
+                               name = &dsName;
+                               node = gtk_ctree_insert_node( tree, NULL, NULL,
+                                               name, FOLDER_SPACING, bookXpm,
+                                               bookXpmMask, bookXpm, bookXpmMask,
+                                               FALSE, TRUE );
+                               g_free( dsName );
+
+                               fi = addressadd_create_folderinfo( abf, NULL );
+                               gtk_ctree_node_set_row_data_full( tree, node, fi,
+                                               ( GtkDestroyNotify ) addressadd_free_folderinfo );
+
+                               rootFolder = addrindex_ds_get_root_folder( ds );
+                               addressadd_load_folder( node, rootFolder, fi );
+
+                               nodeDS = g_list_next( nodeDS );
+                       }
+               }
+               list = g_list_next( list );
+       }
+}
+
+gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, const gchar *address, const gchar *remarks ) {
+       gboolean retVal = FALSE;
+       ItemPerson *person = NULL;
+
+       addressadd_cancelled = FALSE;
+       if( ! addressadd_dlg.window ) addressadd_create();
+       gtk_widget_grab_focus(addressadd_dlg.ok_btn);
+       gtk_widget_show(addressadd_dlg.window);
+       manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
+
+       addressadd_dlg.fiSelected = NULL;
+       addressadd_status_show( "" );
+       addressadd_load_data( addrIndex );
+       gtk_clist_select_row( GTK_CLIST( addressadd_dlg.tree_folder ), 0, 0 );
+       gtk_widget_show(addressadd_dlg.window);
+
+       gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_name ), "" );
+       gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
+       gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_remarks ), "" );
+       if( name )
+               gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_name ), name );
+       if( address )
+               gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
+       if( remarks )
+               gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_remarks ), remarks );
+
+       gtk_main();
+       gtk_widget_hide( addressadd_dlg.window );
+
+       if( ! addressadd_cancelled ) {
+               if( addressadd_dlg.fiSelected ) {
+                       FolderInfo *fi = addressadd_dlg.fiSelected;
+                       person = addrbook_add_contact( fi->book, fi->folder, name, address, remarks );
+                       if( person ) retVal = TRUE;
+               }
+       }
+
+       gtk_clist_clear( GTK_CLIST( addressadd_dlg.tree_folder ) );
+
+       return retVal;
+}
+
+/*
+* End of Source.
+*/
+
diff --git a/src/addressadd.h b/src/addressadd.h
new file mode 100644 (file)
index 0000000..20e90fc
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001 Match Grun
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * Add address to address book dialog.
+ */
+
+#ifndef __ADDRESS_ADD_H__
+#define __ADDRESS_ADD_H__
+
+#include "addrindex.h"
+
+// Function prototypes
+gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, const gchar *address, const gchar *remarks );
+
+#endif /* __ADDRESS_ADD_H__ */
+
+/*
+* End of Source.
+*/
+
index 53ee299..becf2ec 100644 (file)
 #include "about.h"
 #include "addr_compl.h"
 
+#include "mgutils.h"
 #include "addressitem.h"
+#include "addritem.h"
+#include "addrcache.h"
+#include "addrbook.h"
+#include "addrindex.h"
 #include "vcard.h"
 #include "editvcard.h"
+#include "editgroup.h"
+#include "editaddress.h"
+#include "editbook.h"
 
 #ifdef USE_JPILOT
 #include "jpilot.h"
 #include "syldap.h"
 #include "editldap.h"
 
-// Interval to check for LDAP search results
-// #define ADDRESSBOOK_LDAP_TIMER_INTERVAL     100
-#define ADDRESSBOOK_LDAP_BUSYMSG       "Busy"
-
+#define ADDRESSBOOK_LDAP_BUSYMSG "Busy"
 #endif
 
 #include "pixmaps/dir-close.xpm"
 #include "pixmaps/dir-open.xpm"
 #include "pixmaps/group.xpm"
+#include "pixmaps/interface.xpm"
+#include "pixmaps/book.xpm"
+#include "pixmaps/address.xpm"
 #include "pixmaps/vcard.xpm"
-#ifdef USE_JPILOT
 #include "pixmaps/jpilot.xpm"
 #include "pixmaps/category.xpm"
-#endif
-#ifdef USE_LDAP
 #include "pixmaps/ldap.xpm"
-#endif
-
-// XML tag names for top level folders
-#define ADDRESS_TAG_COMMON    "common_address"
-#define ADDRESS_TAG_PERSONAL  "personal_address"
-#define ADDRESS_TAG_VCARD     "vcard_list"
-#ifdef USE_JPILOT
-#define ADDRESS_TAG_JPILOT    "jpilot_list"
-#endif
-#ifdef USE_LDAP
-#define ADDRESS_TAG_LDAP      "ldap_list"
-#endif
 
 typedef enum
 {
@@ -112,7 +105,7 @@ typedef enum
 } AddressBookColumnPos;
 
 #define N_COLS 3
-#define COL_NAME_WIDTH         144
+#define COL_NAME_WIDTH         164
 #define COL_ADDRESS_WIDTH      156
 
 #define COL_FOLDER_WIDTH       170
@@ -127,6 +120,12 @@ static GdkPixmap *folderopenxpm;
 static GdkBitmap *folderopenxpmmask;
 static GdkPixmap *groupxpm;
 static GdkBitmap *groupxpmmask;
+static GdkPixmap *interfacexpm;
+static GdkBitmap *interfacexpmmask;
+static GdkPixmap *bookxpm;
+static GdkBitmap *bookxpmmask;
+static GdkPixmap *addressxpm;
+static GdkBitmap *addressxpmmask;
 static GdkPixmap *vcardxpm;
 static GdkBitmap *vcardxpmmask;
 #ifdef USE_JPILOT
@@ -140,28 +139,24 @@ static GdkPixmap *ldapxpm;
 static GdkBitmap *ldapxpmmask;
 #endif
 
-// Pilot library indicator (set at run-time)
-static _have_pilot_library_;
-
-// LDAP library indicator (set at run-time)
-static _have_ldap_library_;
-
 // Message buffer
 static gchar addressbook_msgbuf[ ADDRESSBOOK_MSGBUF_SIZE ];
 
-static AddressBook addrbook;
+// Address list selection
+static GList *_addressListSelection_ = NULL;
 
-static struct _AddressEdit
-{
-       GtkWidget *window;
-       GtkWidget *name_entry;
-       GtkWidget *addr_entry;
-       GtkWidget *rem_entry;
-       GtkWidget *ok_btn;
-       GtkWidget *cancel_btn;
-} addredit;
-
-static void addressbook_create                 (gboolean show);
+// Address index file and interfaces
+static AddressIndex *_addressIndex_ = NULL;
+static GList *_addressInterfaceList_ = NULL;
+static GList *_addressIFaceSelection_ = NULL;
+#define ADDRESSBOOK_IFACE_SELECTION "1/y,3/y,4/y,2/n"
+
+static AddressBook_win addrbook;
+
+static GHashTable *_addressBookTypeHash_ = NULL;
+static GList *_addressBookTypeList_ = NULL;
+
+static void addressbook_create                 ( void );
 static gint addressbook_close                  (void);
 static void addressbook_button_set_sensitive   (void);
 
@@ -184,6 +179,20 @@ static void addressbook_list_selected              (GtkCList       *clist,
                                                 gint            column,
                                                 GdkEvent       *event,
                                                 gpointer        data);
+static void addressbook_list_row_selected      (GtkCTree       *clist,
+                                                GtkCTreeNode   *node,
+                                                gint            column,
+                                                gpointer        data);
+static void addressbook_list_row_unselected    (GtkCTree       *clist,
+                                                GtkCTreeNode   *node,
+                                                gint            column,
+                                                gpointer        data);
+static void addressbook_person_expand_node     (GtkCTree       *ctree,
+                                                GList          *node,
+                                                gpointer       *data );
+static void addressbook_person_collapse_node   (GtkCTree       *ctree,
+                                                GList          *node,
+                                                gpointer       *data );
 static void addressbook_entry_gotfocus         (GtkWidget      *widget);
 
 #if 0
@@ -211,29 +220,15 @@ static void addressbook_new_folder_cb             (gpointer        data,
 static void addressbook_new_group_cb           (gpointer        data,
                                                 guint           action,
                                                 GtkWidget      *widget);
-static void addressbook_edit_folder_cb         (gpointer        data,
+static void addressbook_treenode_edit_cb       (gpointer        data,
                                                 guint           action,
                                                 GtkWidget      *widget);
-static void addressbook_delete_folder_cb       (gpointer        data,
+static void addressbook_treenode_delete_cb     (gpointer        data,
                                                 guint           action,
                                                 GtkWidget      *widget);
 
 static void addressbook_change_node_name       (GtkCTreeNode   *node,
                                                 const gchar    *name);
-static void addressbook_edit_group             (GtkCTreeNode   *group_node);
-
-static void addressbook_edit_address_create    (gboolean       *cancelled);
-static void edit_address_ok                    (GtkWidget      *widget,
-                                                gboolean       *cancelled);
-static void edit_address_cancel                        (GtkWidget      *widget,
-                                                gboolean       *cancelled);
-static gint edit_address_delete_event          (GtkWidget      *widget,
-                                                GdkEventAny    *event,
-                                                gboolean       *cancelled);
-static void edit_address_key_pressed           (GtkWidget      *widget,
-                                                GdkEventKey    *event,
-                                                gboolean       *cancelled);
-static AddressItem *addressbook_edit_address   (AddressItem    *item);
 
 static void addressbook_new_address_cb         (gpointer        data,
                                                 guint           action,
@@ -248,58 +243,62 @@ static void addressbook_delete_address_cb (gpointer        data,
 static void close_cb                           (gpointer        data,
                                                 guint           action,
                                                 GtkWidget      *widget);
+static void addressbook_file_save_cb           (gpointer        data,
+                                                guint           action,
+                                                GtkWidget      *widget);
 
-// VCard edit stuff
+// Data source edit stuff
+static void addressbook_new_book_cb            ( gpointer      data,
+                                                 guint         action,
+                                                 GtkWidget     *widget );
 static void addressbook_new_vcard_cb           ( gpointer      data,
                                                  guint         action,
                                                  GtkWidget     *widget );
 
 #ifdef USE_JPILOT
-// JPilot edit stuff
 static void addressbook_new_jpilot_cb          ( gpointer      data,
                                                  guint         action,
                                                  GtkWidget     *widget );
 #endif
 
 #ifdef USE_LDAP
-// LDAP edit stuff
 static void addressbook_new_ldap_cb            ( gpointer      data,
                                                  guint         action,
                                                  GtkWidget     *widget );
 #endif
 
-static AddressItem *addressbook_parse_address  (const gchar    *str);
-static void addressbook_append_to_compose_entry        (AddressItem    *item,
-                                                ComposeEntryType type);
-
 static void addressbook_set_clist              (AddressObject  *obj);
 
-static void addressbook_read_file              (void);
-static void addressbook_get_tree               (XMLFile        *file,
-                                                GtkCTreeNode   *node,
-                                                const gchar    *folder_tag);
-static void addressbook_add_objs               (XMLFile        *file,
-                                                GtkCTreeNode   *node);
+static void addressbook_load_tree              (void);
+void addressbook_read_file                     (void);
 
 static GtkCTreeNode *addressbook_add_object    (GtkCTreeNode   *node,
                                                 AddressObject  *obj);
-static void addressbook_delete_object          (AddressObject  *obj);
-static AddressObject *addressbook_find_object_by_name
+static AddressDataSource *addressbook_find_datasource
+                                               (GtkCTreeNode   *node );
+
+static AddressBookFile *addressbook_get_book_file();
+
+static GtkCTreeNode *addressbook_node_add_folder
                                                (GtkCTreeNode   *node,
-                                                const gchar    *name);
+                                               AddressDataSource *ds,
+                                               ItemFolder      *itemFolder,
+                                               AddressObjectType otype );
+static GtkCTreeNode *addressbook_node_add_group (GtkCTreeNode  *node,
+                                               AddressDataSource *ds,
+                                               ItemGroup       *itemGroup );
+static GtkCTreeNode *addressbook_node_add_category
+                                               (GtkCTreeNode   *node,
+                                                AddressDataSource *ds,
+                                                ItemFolder     *itemFolder );
+static void addressbook_tree_remove_children   (GtkCTree       *ctree,
+                                               GtkCTreeNode    *parent );
+static void addressbook_move_nodes_up          (GtkCTree       *ctree,
+                                               GtkCTreeNode    *node );
+static GtkCTreeNode *addressbook_find_group_node (GtkCTreeNode *parent,
+                                               ItemGroup       *group );
 
-static AddressItem *addressbook_parse_item     (XMLFile        *file);
-static void addressbook_xml_recursive_write    (GtkCTreeNode   *node,
-                                                FILE           *fp);
-static void addressbook_node_write_begin       (GtkCTreeNode   *node,
-                                                FILE           *fp);
-static void addressbook_node_write_end         (GtkCTreeNode   *node,
-                                                FILE           *fp);
-static void addressbook_write_items            (FILE           *fp,
-                                                GList          *items,
-                                                guint           level);
-static void tab_indent_out                     (FILE           *fp,
-                                                guint           level);
+static void addressbook_delete_object          (AddressObject  *obj);
 
 static void key_pressed                                (GtkWidget      *widget,
                                                 GdkEventKey    *event,
@@ -307,67 +306,89 @@ static void key_pressed                           (GtkWidget      *widget,
 static gint addressbook_list_compare_func      (GtkCList       *clist,
                                                 gconstpointer   ptr1,
                                                 gconstpointer   ptr2);
-static gint addressbook_obj_name_compare       (gconstpointer   a,
-                                                gconstpointer   b);
+//static gint addressbook_obj_name_compare     (gconstpointer   a,
+//                                              gconstpointer   b);
 
-static AddressVCard *addressbook_parse_vcard   ( XMLFile       *file );
-static void addressbook_write_vcard            ( FILE          *fp,
-                                               AddressVCard    *vcard,
-                                               guint           level );
+static void addressbook_book_show_message      ( AddressBookFile *book );
 static void addressbook_vcard_show_message     ( VCardFile *vcf );
-
 #ifdef USE_JPILOT
-static AddressJPilot *addressbook_parse_jpilot ( XMLFile       *file );
-static void addressbook_write_jpilot           ( FILE          *fp,
-                                               AddressJPilot   *jpilot,
-                                               guint           level );
 static void addressbook_jpilot_show_message    ( JPilotFile *jpf );
 #endif
 #ifdef USE_LDAP
-static AddressLDAP *addressbook_parse_ldap     ( XMLFile       *file );
-static void addressbook_write_ldap             ( FILE          *fp,
-                                               AddressLDAP     *ldapi,
-                                               guint           level );
 static void addressbook_ldap_show_message      ( SyldapServer *server );
 #endif
 
+// LUT's and IF stuff
+static void addressbook_free_adapter           ( GtkCTreeNode  *node );
+static void addressbook_free_child_adapters    ( GtkCTreeNode  *node );
+AddressTypeControlItem *addrbookctl_lookup     ( gint          ot );
+AddressTypeControlItem *addrbookctl_lookup_iface( AddressIfType        ifType );
+
+void addrbookctl_build_map                     ( GtkWidget     *window );
+void addrbookctl_build_iflist                  ( void );
+AdapterInterface *addrbookctl_find_interface   ( AddressIfType ifType );
+void addrbookctl_build_ifselect();
+
+static void addrbookctl_free_interface         ( AdapterInterface      *adapter );
+static void addrbookctl_free_datasource                ( AdapterDSource        *adapter );
+static void addrbookctl_free_folder            ( AdapterFolder *adapter );
+static void addrbookctl_free_group             ( AdapterGroup  *adapter );
+
+static void addressbook_list_select_clear      ( void );
+static void addressbook_list_select_add                ( AddressObject *obj );
+static void addressbook_list_select_remove     ( AddressObject *obj );
+
 static GtkItemFactoryEntry addressbook_entries[] =
 {
-       {N_("/_File"),                  NULL, NULL, 0, "<Branch>"},
-       {N_("/_File/New _Address"),     "<alt>N", addressbook_new_address_cb, 0, NULL},
-       {N_("/_File/New _Group"),       "<alt>G", addressbook_new_group_cb,   0, NULL},
-       {N_("/_File/New _Folder"),      "<alt>R", addressbook_new_folder_cb,  0, NULL},
-       {N_("/_File/New _V-Card"),      "<alt>D", addressbook_new_vcard_cb,  0, NULL},
+       {N_("/_File"),                  NULL,           NULL, 0, "<Branch>"},
+       {N_("/_File/New _Book"),        "<alt>B",       addressbook_new_book_cb,        0, NULL},
+       {N_("/_File/New _V-Card"),      "<alt>D",       addressbook_new_vcard_cb,       0, NULL},
 #ifdef USE_JPILOT
-       {N_("/_File/New _J-Pilot"),     "<alt>J", addressbook_new_jpilot_cb,  0, NULL},
+       {N_("/_File/New _J-Pilot"),     "<alt>J",       addressbook_new_jpilot_cb,      0, NULL},
 #endif
 #ifdef USE_LDAP
-       {N_("/_File/New _Server"),      "<alt>S", addressbook_new_ldap_cb,  0, NULL},
-#endif
-       {N_("/_File/---"),              NULL, NULL, 0, "<Separator>"},
-       {N_("/_File/_Edit"),            "<alt>Return", addressbook_edit_address_cb, 0, NULL},
-       {N_("/_File/_Delete"),          NULL, addressbook_delete_address_cb, 0, NULL},
-       {N_("/_File/---"),              NULL, NULL, 0, "<Separator>"},
-       {N_("/_File/_Close"),           "<alt>W", close_cb, 0, NULL},
-       {N_("/_Help"),                  NULL, NULL, 0, "<LastBranch>"},
-       {N_("/_Help/_About"),           NULL, about_show, 0, NULL}
+       {N_("/_File/New _Server"),      "<alt>S",       addressbook_new_ldap_cb,        0, NULL},
+#endif
+       {N_("/_File/---"),              NULL,           NULL, 0, "<Separator>"},
+       {N_("/_File/_Edit"),            NULL,           addressbook_treenode_edit_cb,   0, NULL},
+       {N_("/_File/_Delete"),          NULL,           addressbook_treenode_delete_cb, 0, NULL},
+       {N_("/_File/---"),              NULL,           NULL, 0, "<Separator>"},
+       {N_("/_File/_Save"),            "<alt>V",       addressbook_file_save_cb,       0, NULL},
+       {N_("/_File/_Close"),           "<alt>W",       close_cb, 0, NULL},
+       {N_("/_Address"),               NULL,           NULL, 0, "<Branch>"},
+       {N_("/_Address/New _Address"),  "<alt>N",       addressbook_new_address_cb,     0, NULL},
+       {N_("/_Address/New _Group"),    "<alt>G",       addressbook_new_group_cb,       0, NULL},
+       {N_("/_Address/New _Folder"),   "<alt>R",       addressbook_new_folder_cb,      0, NULL},
+       {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>"},
+       {N_("/_Address/_Edit"),         "<alt>Return",  addressbook_edit_address_cb,    0, NULL},
+       {N_("/_Address/_Delete"),       NULL,           addressbook_delete_address_cb,  0, NULL},
+       {N_("/_Help"),                  NULL,           NULL, 0, "<LastBranch>"},
+       {N_("/_Help/_About"),           NULL,           about_show, 0, NULL}
 };
 
+// New options to be added.
+/*
+       {N_("/_Edit"),                  NULL,           NULL, 0, "<Branch>"},
+       {N_("/_Edit/C_ut"),             "<ctl>X",       NULL,                           0, NULL},
+       {N_("/_Edit/_Copy"),            "<ctl>C",       NULL,                           0, NULL},
+       {N_("/_Edit/_Paste"),           "<ctl>V",       NULL,                           0, NULL},
+       {N_("/_Tools"),                 NULL,           NULL, 0, "<Branch>"},
+       {N_("/_Tools/Import _Mozilla"), NULL,           NULL,                           0, NULL},
+       {N_("/_Tools/Import _LDIF"),    NULL,           NULL,                           0, NULL},
+       {N_("/_Tools/Import _V-Card"),  NULL,           NULL,                           0, NULL},
+       {N_("/_Tools/---"),             NULL,           NULL, 0, "<Separator>"},
+       {N_("/_Tools/Export _LDIF"),    NULL,           NULL,                           0, NULL},
+       {N_("/_Tools/Export V-_Card"),  NULL,           NULL,                           0, NULL},
+*/
+
 static GtkItemFactoryEntry addressbook_tree_popup_entries[] =
 {
        {N_("/New _Address"),   NULL, addressbook_new_address_cb, 0, NULL},
        {N_("/New _Group"),     NULL, addressbook_new_group_cb,   0, NULL},
        {N_("/New _Folder"),    NULL, addressbook_new_folder_cb,  0, NULL},
-       {N_("/New _V-Card"),    NULL, addressbook_new_vcard_cb,   0, NULL},
-#ifdef USE_JPILOT
-       {N_("/New _J-Pilot"),   NULL, addressbook_new_jpilot_cb,  0, NULL},
-#endif
-#ifdef USE_LDAP
-       {N_("/New _Server"),    NULL, addressbook_new_ldap_cb,  0, NULL},
-#endif
        {N_("/---"),            NULL, NULL, 0, "<Separator>"},
-       {N_("/_Edit"),          NULL, addressbook_edit_folder_cb,   0, NULL},
-       {N_("/_Delete"),        NULL, addressbook_delete_folder_cb, 0, NULL}
+       {N_("/_Edit"),          NULL, addressbook_treenode_edit_cb,   0, NULL},
+       {N_("/_Delete"),        NULL, addressbook_treenode_delete_cb, 0, NULL}
 };
 
 static GtkItemFactoryEntry addressbook_list_popup_entries[] =
@@ -383,12 +404,13 @@ static GtkItemFactoryEntry addressbook_list_popup_entries[] =
 void addressbook_open(Compose *target)
 {
        if (!addrbook.window) {
-               addressbook_create(TRUE);
                addressbook_read_file();
-               addrbook.open_folder = TRUE;
+               addressbook_create();
+               addressbook_load_tree();
                gtk_ctree_select(GTK_CTREE(addrbook.ctree),
                                 GTK_CTREE_NODE(GTK_CLIST(addrbook.ctree)->row_list));
-       } else
+       }
+       
                gtk_widget_hide(addrbook.window);
 
        gtk_widget_show_all(addrbook.window);
@@ -408,8 +430,31 @@ Compose *addressbook_get_target_compose(void)
        return addrbook.target_compose;
 }
 
-static void addressbook_create(gboolean show)
-{
+void addressbook_refresh( void ) {
+       if( addrbook.window ) {
+               if( addrbook.treeSelected ) {
+                       gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
+               }
+       }
+       addressbook_export_to_file();
+}
+
+/*
+* Create the address book widgets. The address book contains two CTree widgets: the
+* address index tree on the left and the address list on the right.
+*
+* The address index tree displays a hierarchy of interfaces and groups. Each node in
+* this tree is linked to an address Adapter. Adapters have been created for interfaces,
+* data sources and folder objects.
+*
+* The address list displays group, person and email objects. These items are linked
+* directly to ItemGroup, ItemPerson and ItemEMail objects inside the address book data
+* sources.
+*
+* In the tradition of MVC architecture, the data stores have been separated from the
+* GUI components. The addrindex.c file provides the interface to all data stores.
+*/
+static void addressbook_create( void ) {
        GtkWidget *window;
        GtkWidget *vbox;
        GtkWidget *menubar;
@@ -439,6 +484,9 @@ static void addressbook_create(gboolean show)
        GtkItemFactory *list_factory;
        GtkItemFactory *menu_factory;
        gint n_entries;
+       GList *nodeIf;
+       AdapterInterface *adapter;
+       AddressTypeControlItem *atci;
 
        gchar *titles[N_COLS] = {_("Name"), _("E-Mail address"), _("Remarks")};
        gchar *text;
@@ -446,14 +494,10 @@ static void addressbook_create(gboolean show)
 
        debug_print("Creating addressbook window...\n");
 
-       // Global flag if we have library installed (at run-time)
-       _have_pilot_library_ = FALSE;
-       _have_ldap_library_ = FALSE;
-
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(window), _("Address book"));
        gtk_widget_set_usize(window, ADDRESSBOOK_WIDTH, ADDRESSBOOK_HEIGHT);
-       //gtk_container_set_border_width(GTK_CONTAINER(window), BORDER_WIDTH);
+       gtk_container_set_border_width(GTK_CONTAINER(window), BORDER_WIDTH);
        gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, TRUE);
        gtk_widget_realize(window);
 
@@ -486,6 +530,7 @@ static void addressbook_create(gboolean show)
                                       GTK_POLICY_ALWAYS);
        gtk_widget_set_usize(ctree_swin, COL_FOLDER_WIDTH + 40, -1);
 
+       // Address index
        ctree = gtk_ctree_new(1, 0);
        gtk_container_add(GTK_CONTAINER(ctree_swin), ctree);
        gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
@@ -514,9 +559,13 @@ static void addressbook_create(gboolean show)
                                       GTK_POLICY_ALWAYS);
        gtk_box_pack_start(GTK_BOX(clist_vbox), clist_swin, TRUE, TRUE, 0);
 
-       clist = gtk_clist_new_with_titles(N_COLS, titles);
+       // Address list
+       clist = gtk_ctree_new_with_titles(N_COLS, 0, titles);
        gtk_container_add(GTK_CONTAINER(clist_swin), clist);
        gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_EXTENDED);
+       gtk_ctree_set_line_style(GTK_CTREE(clist), GTK_CTREE_LINES_DOTTED);
+       gtk_ctree_set_expander_style(GTK_CTREE(clist), GTK_CTREE_EXPANDER_SQUARE);
+       gtk_ctree_set_indent(GTK_CTREE(clist), CTREE_INDENT);
        gtk_clist_set_column_width(GTK_CLIST(clist), COL_NAME,
                                   COL_NAME_WIDTH);
        gtk_clist_set_column_width(GTK_CLIST(clist), COL_ADDRESS,
@@ -528,14 +577,22 @@ static void addressbook_create(gboolean show)
                GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
                                       GTK_CAN_FOCUS);
 
-       gtk_signal_connect(GTK_OBJECT(clist), "select_row",
-                          GTK_SIGNAL_FUNC(addressbook_list_selected), NULL);
+       gtk_signal_connect(GTK_OBJECT(clist), "tree_select_row",
+                          GTK_SIGNAL_FUNC(addressbook_list_row_selected), NULL);
+       gtk_signal_connect(GTK_OBJECT(clist), "tree_unselect_row",
+                          GTK_SIGNAL_FUNC(addressbook_list_row_unselected), NULL);
        gtk_signal_connect(GTK_OBJECT(clist), "button_press_event",
                           GTK_SIGNAL_FUNC(addressbook_list_button_pressed),
                           NULL);
        gtk_signal_connect(GTK_OBJECT(clist), "button_release_event",
                           GTK_SIGNAL_FUNC(addressbook_list_button_released),
                           NULL);
+       gtk_signal_connect(GTK_OBJECT(clist), "select_row",
+                          GTK_SIGNAL_FUNC(addressbook_list_selected), NULL);
+       gtk_signal_connect(GTK_OBJECT(clist), "tree_expand",
+                          GTK_SIGNAL_FUNC(addressbook_person_expand_node), NULL );
+       gtk_signal_connect(GTK_OBJECT(clist), "tree_collapse",
+                          GTK_SIGNAL_FUNC(addressbook_person_collapse_node), NULL );
 
        hbox = gtk_hbox_new(FALSE, 4);
        gtk_box_pack_start(GTK_BOX(clist_vbox), hbox, FALSE, FALSE, 0);
@@ -612,77 +669,32 @@ static void addressbook_create(gboolean show)
                           GTK_SIGNAL_FUNC(addressbook_to_clicked),
                           GINT_TO_POINTER(COMPOSE_BCC));
 
-       PIXMAP_CREATE(window, folderxpm, folderxpmmask, DIRECTORY_CLOSE_XPM);
-       PIXMAP_CREATE(window, folderopenxpm, folderopenxpmmask,
-                     DIRECTORY_OPEN_XPM);
-       PIXMAP_CREATE(window, groupxpm, groupxpmmask, group_xpm);
-       PIXMAP_CREATE(window, vcardxpm, vcardxpmmask, vcard_xpm);
-#ifdef USE_JPILOT
-       PIXMAP_CREATE(window, jpilotxpm, jpilotxpmmask, jpilot_xpm);
-       PIXMAP_CREATE(window, categoryxpm, categoryxpmmask, category_xpm);
-#endif
-#ifdef USE_LDAP
-       PIXMAP_CREATE(window, ldapxpm, ldapxpmmask, ldap_xpm);
-#endif
-
-       text = _("Common address");
-       addrbook.common =
-               gtk_ctree_insert_node(GTK_CTREE(ctree),
-                                     NULL, NULL, &text, FOLDER_SPACING,
-                                     folderxpm, folderxpmmask,
-                                     folderopenxpm, folderopenxpmmask,
-                                     FALSE, FALSE);
-       text = _("Personal address");
-       addrbook.personal =
-               gtk_ctree_insert_node(GTK_CTREE(ctree),
-                                     NULL, NULL, &text, FOLDER_SPACING,
-                                     folderxpm, folderxpmmask,
-                                     folderopenxpm, folderopenxpmmask,
-                                     FALSE, FALSE);
-
-       text = _("V-Card");
-       addrbook.vcard =
-               gtk_ctree_insert_node(GTK_CTREE(ctree),
-                                     NULL, NULL, &text, FOLDER_SPACING,
-                                     folderxpm, folderxpmmask,
-                                     folderopenxpm, folderopenxpmmask,
-                                     FALSE, FALSE);
-
-#ifdef USE_JPILOT
-       text = _("J-Pllot");
-       addrbook.jpilot =
-               gtk_ctree_insert_node(GTK_CTREE(ctree),
-                                     NULL, NULL, &text, FOLDER_SPACING,
-                                     folderxpm, folderxpmmask,
-                                     folderopenxpm, folderopenxpmmask,
-                                     FALSE, FALSE);
-       if( jpilot_test_pilot_lib() ) {
-               _have_pilot_library_ = TRUE;
-               menu_set_sensitive( menu_factory, "/File/New J-Pilot", TRUE );
-       }
-       else {
-               menu_set_sensitive( menu_factory, "/File/New J-Pilot", FALSE );
-       }
-#endif
-
-#ifdef USE_LDAP
-       text = _("Directory");
-       addrbook.ldap =
-               gtk_ctree_insert_node(GTK_CTREE(ctree),
-                                     NULL, NULL, &text, FOLDER_SPACING,
-                                     folderxpm, folderxpmmask,
-                                     folderopenxpm, folderopenxpmmask,
-                                     FALSE, FALSE);
-       if( syldap_test_ldap_lib() ) {
-               _have_ldap_library_ = TRUE;
-               menu_set_sensitive( menu_factory, "/File/New Server", TRUE );
-       }
-       else {
-               menu_set_sensitive( menu_factory, "/File/New Server", FALSE );
-       }
-#endif
-
-       /* popup menu */
+       // Build icons for interface
+       PIXMAP_CREATE( window, interfacexpm, interfacexpmmask, interface_xpm );
+
+       // Build control tables
+       addrbookctl_build_map( window );
+       addrbookctl_build_iflist();
+       addrbookctl_build_ifselect();
+
+       // Add each interface into the tree as a root level folder
+       nodeIf = _addressInterfaceList_;
+       while( nodeIf ) {
+               adapter = nodeIf->data;
+               nodeIf = g_list_next( nodeIf );
+               atci = adapter->atci;
+               text = atci->displayName;
+               adapter->treeNode =
+                       gtk_ctree_insert_node( GTK_CTREE(ctree),
+                               NULL, NULL, &text, FOLDER_SPACING,
+                               interfacexpm, interfacexpmmask,
+                               interfacexpm, interfacexpmmask,
+                               FALSE, FALSE );
+               menu_set_sensitive( menu_factory, atci->menuCommand, adapter->haveLibrary );
+               gtk_ctree_node_set_row_data( GTK_CTREE(ctree), adapter->treeNode, adapter );
+       }
+
+       // Popup menu
        n_entries = sizeof(addressbook_tree_popup_entries) /
                sizeof(addressbook_tree_popup_entries[0]);
        tree_popup = menu_create_items(addressbook_tree_popup_entries,
@@ -719,18 +731,16 @@ static void addressbook_create(gboolean show)
        addrbook.list_factory = list_factory;
        addrbook.menu_factory = menu_factory;
 
+       addrbook.listSelected = NULL;
        address_completion_start(window);
+       gtk_widget_show_all(window);
 
-       if (show) 
-               gtk_widget_show_all(window);
 }
 
 static gint addressbook_close(void)
 {
        gtk_widget_hide(addrbook.window);
        addressbook_export_to_file();
-       /* tell addr_compl that there's a new addressbook file */
-       invalidate_address_completion();
        return TRUE;
 }
 
@@ -743,6 +753,30 @@ static void addressbook_status_show( gchar *msg ) {
        }
 }
 
+static void addressbook_ds_show_message( AddressDataSource *ds ) {
+       gint retVal;
+       gchar *name;
+       *addressbook_msgbuf = '\0';
+       if( ds ) {
+               name = addrindex_ds_get_name( ds );
+               retVal = addrindex_ds_get_status_code( ds );
+               if( retVal == MGU_SUCCESS ) {
+                       if( ds ) {
+                               sprintf( addressbook_msgbuf, "%s", name );
+                       }
+               }
+               else {
+                       if( ds == NULL ) {
+                               sprintf( addressbook_msgbuf, "%s", mgu_error2string( retVal ) );
+                       }
+                       else {
+                               sprintf( addressbook_msgbuf, "%s: %s", name, mgu_error2string( retVal ) );
+                       }
+               }
+       }
+       addressbook_status_show( addressbook_msgbuf );
+}
+
 static void addressbook_button_set_sensitive(void)
 {
        gboolean to_sens  = FALSE;
@@ -763,217 +797,206 @@ static void addressbook_button_set_sensitive(void)
        gtk_widget_set_sensitive(addrbook.bcc_btn, bcc_sens);
 }
 
+/*
+* Delete one or more objects from address list.
+*/
 static void addressbook_del_clicked(GtkButton *button, gpointer data)
 {
-       GtkCList *clist = GTK_CLIST(addrbook.clist);
+       GtkCTree *clist = GTK_CTREE(addrbook.clist);
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
        AddressObject *pobj, *obj;
-       GList *cur, *next;
-       gint row;
-       gboolean remFlag;
-
-       if (!clist->selection) {
-               addressbook_delete_folder_cb(NULL, 0, NULL);
-               return;
-       }
+       AdapterDSource *ads = NULL;
+       GtkCTreeNode *nodeList;
+       gboolean remFlag, procFlag;
+       AlertValue aval;
+       AddressBookFile *abf = NULL;
+       AddressDataSource *ds = NULL;
 
-       pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened);
+       pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened );
        g_return_if_fail(pobj != NULL);
 
-       if (alertpanel(_("Delete address(es)"),
-                      _("Really delete the address(es)?"),
-                      _("Yes"), _("No"), NULL) != G_ALERTDEFAULT)
-               return;
+       nodeList = addrbook.listSelected;
+       obj = gtk_ctree_node_get_row_data( clist, nodeList );
+       if( obj == NULL) return;
+       ds = addressbook_find_datasource( addrbook.treeSelected );
+       if( ds == NULL ) return;
 
-       for (cur = clist->selection; cur != NULL; cur = next) {
-               next = cur->next;
-               row = GPOINTER_TO_INT(cur->data);
-               remFlag = FALSE;
+       procFlag = FALSE;
+       if( pobj->type == ADDR_DATASOURCE ) {
+               ads = ADAPTER_DSOURCE(pobj);
+               if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
+       }
+       else if( pobj->type == ADDR_ITEM_FOLDER ) {
+               procFlag = TRUE;
+       }
+       else if( pobj->type == ADDR_ITEM_GROUP ) {
+               procFlag = TRUE;
+       }
+       if( ! procFlag ) return;
+       abf = ds->rawDataSource;
+       if( abf == NULL ) return;
 
-               obj = gtk_clist_get_row_data(clist, row);
-               if (!obj) continue;
+       // Confirm deletion
+       aval = alertpanel( _("Delete address(es)"),
+                       _("Really delete the address(es)?"),
+                       _("Yes"), _("No"), NULL );
+       if( aval != G_ALERTDEFAULT ) return;
 
-               if (pobj->type == ADDR_GROUP) {
-                       AddressGroup *group = ADDRESS_GROUP(pobj);
-                       group->items = g_list_remove(group->items, obj);
-               } else if (pobj->type == ADDR_FOLDER) {
-                       AddressFolder *folder = ADDRESS_FOLDER(pobj);
+       // Process deletions
+       if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
+               // Items inside folders
+               GList *node;
+               node = _addressListSelection_;
+               while( node ) {
+                       AddrItemObject *aio = node->data;
+                       node = g_list_next( node );
+                       if( aio->type == ADDR_ITEM_GROUP ) {
+                               ItemGroup *item = ( ItemGroup * ) aio;
+                               GtkCTreeNode *nd = NULL;
 
-                       folder->items = g_list_remove(folder->items, obj);
-                       if (obj->type == ADDR_GROUP) {
-                               remFlag = TRUE;
-                       }
-                       else if (obj->type == ADDR_VCARD) {
-                               remFlag = TRUE;
+                               nd = addressbook_find_group_node( addrbook.opened, item );
+                               item = addrbook_remove_group( abf, item );
+                               if( item ) {
+                                       addritem_free_item_group( item );
+                                       item = NULL;
+                               }
+                               // Remove group from parent node
+                               gtk_ctree_remove_node( ctree, nd );
                        }
-                       else if (obj->type == ADDR_JPILOT) {
-                               remFlag = TRUE;
+                       else if( aio->type == ADDR_ITEM_PERSON ) {
+                               ItemPerson *item = ( ItemPerson * ) aio;
+                               item = addrbook_remove_person( abf, item );
+                               if( item ) {
+                                       addritem_free_item_person( item );
+                                       item = NULL;
+                               }
                        }
-                       else if (obj->type == ADDR_LDAP) {
-                               remFlag = TRUE;
+                       else if( aio->type == ADDR_ITEM_EMAIL ) {
+                               ItemEMail *item = ( ItemEMail * ) aio;
+                               ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
+                               item = addrbook_person_remove_email( abf, person, item );
+                               if( item ) {
+                                       addritem_free_item_email( item );
+                                       item = NULL;
+                               }
                        }
-
-                       if( remFlag ) {
-                               GtkCTreeNode *node;
-                               node = gtk_ctree_find_by_row_data
-                                       (ctree, addrbook.opened, obj);
-                               if (node) gtk_ctree_remove_node(ctree, node);
+               }
+               addressbook_list_select_clear();
+               gtk_ctree_select( ctree, addrbook.opened);
+               return;
+       }
+       else if( pobj->type == ADDR_ITEM_GROUP ) {
+               // Items inside groups
+               GList *node;
+               node = _addressListSelection_;
+               while( node ) {
+                       AddrItemObject *aio = node->data;
+                       node = g_list_next( node );
+                       if( aio->type == ADDR_ITEM_EMAIL ) {
+                               ItemEMail *item = ( ItemEMail * ) aio;
+                               ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
+                               item = addrbook_person_remove_email( abf, person, item );
+                               if( item ) {
+                                       addritem_print_item_email( item, stdout );
+                                       addritem_free_item_email( item );
+                                       item = NULL;
+                               }
                        }
-               } else
-                       continue;
+               }
+               addressbook_list_select_clear();
+               gtk_ctree_select( ctree, addrbook.opened);
+               return;
+       }
 
-               addressbook_delete_object(obj);
+       gtk_ctree_node_set_row_data( clist, nodeList, NULL );
+       gtk_ctree_remove_node( clist, nodeList );
+       addressbook_list_select_remove( obj );
 
-               gtk_clist_remove(clist, row);
-       }
 }
 
 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
 {
-       GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
-       GtkEntry *entry = GTK_ENTRY(addrbook.entry);
-       AddressObject *obj;
-       AddressItem *item;
-       gchar *str;
-
-       if (*gtk_entry_get_text(entry) == '\0') {
-               addressbook_new_address_cb(NULL, 0, NULL);
-               return;
-       }
-       if (!addrbook.opened) return;
-
-       obj = gtk_ctree_node_get_row_data(ctree, addrbook.opened);
-       if (!obj) return;
+       addressbook_new_address_cb( NULL, 0, NULL );
+}
 
-       g_return_if_fail(obj->type == ADDR_GROUP || obj->type == ADDR_FOLDER);
+gchar *addressbook_format_address( AddressObject * obj ) {
+       gchar *buf = NULL;
+       gchar *name = NULL;
+       gchar *address = NULL;
 
-       str = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
+       if( obj->type == ADDR_ITEM_EMAIL ) {
+               ItemPerson *person = NULL;
+               ItemEMail *email = ( ItemEMail * ) obj;
 
-       item = addressbook_parse_address(str);
-       g_free(str);
-       if (item) {
-               if (addressbook_find_object_by_name
-                       (addrbook.opened, item->name) != NULL) {
-                       addressbook_delete_object(ADDRESS_OBJECT(item));
-                       item = NULL;
-               } else if (addressbook_edit_address(item) == NULL) {
-                       addressbook_delete_object(ADDRESS_OBJECT(item));
-                       return;
+               person = ( ItemPerson * ) ADDRITEM_PARENT(email);
+               if( email->address ) {
+                       if( ADDRITEM_NAME(email) ) {
+                               name = ADDRITEM_NAME(email);
+                               if( *name == '\0' ) {
+                                       name = ADDRITEM_NAME(person);
+                               }
+                       }
+                       else if( ADDRITEM_NAME(person) ) {
+                               name = ADDRITEM_NAME(person);
+                       }
+                       else {
+                               buf = g_strdup( email->address );
+                       }
+                       address = email->address;
                }
        }
+       else if( obj->type == ADDR_ITEM_PERSON ) {
+               ItemPerson *person = ( ItemPerson * ) obj;
+               GList *node = person->listEMail;
 
-       if (!item) {
-               item = addressbook_edit_address(NULL);
-               if (!item) return;
-       }
-
-       if (addressbook_find_object_by_name(addrbook.opened, item->name)) {
-               addressbook_delete_object(ADDRESS_OBJECT(item));
-               return;
+               name = ADDRITEM_NAME(person);
+               if( node ) {
+                       ItemEMail *email = ( ItemEMail * ) node->data;
+                       address = email->address;
+               }
        }
-
-       addressbook_add_object(addrbook.opened, ADDRESS_OBJECT(item));
-       addrbook.open_folder = TRUE;
-       gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
-}
-
-static AddressItem *addressbook_parse_address(const gchar *str)
-{
-       gchar *name    = NULL;
-       gchar *address = NULL;
-       AddressItem *item;
-       gchar *buf;
-       gchar *start, *end;
-
-       Xalloca(buf, strlen(str) + 1, return NULL);
-
-       strcpy(buf, str);
-       g_strstrip(buf);
-       if (*buf == '\0') return NULL;
-
-       if ((start = strchr(buf, '<'))) {
-               if (start > buf) {
-                       *start = '\0';
-                       g_strstrip(buf);
-                       if (*buf != '\0')
-                               name = g_strdup(buf);
+       if( address ) {
+               if( name ) {
+                       buf = g_strdup_printf( "%s <%s>", name, address );
                }
-               start++;
-               if ((end = strchr(start, '>'))) {
-                       *end = '\0';
-                       g_strstrip(start);
-                       if (*start != '\0')
-                               address = g_strdup(start);
+               else {
+                       buf = g_strdup( address );
                }
-       } else
-               name = g_strdup(buf);
-
-       if (!name && !address) return NULL;
-
-       item = mgu_create_address();
-       ADDRESS_OBJECT_TYPE(item) = ADDR_ITEM;
-       item->name    = name;
-       item->address = address;
-       item->remarks = NULL;
+       }
 
-       return item;
+       return buf;
 }
 
 static void addressbook_to_clicked(GtkButton *button, gpointer data)
 {
-       GtkCList *clist = GTK_CLIST(addrbook.clist);
-       GList *cur;
-
+       GList *node = _addressListSelection_;
        if (!addrbook.target_compose) return;
-
-       for (cur = clist->selection; cur != NULL; cur = cur->next) {
-               AddressObject *obj;
-
-               obj = gtk_clist_get_row_data(clist,
-                                            GPOINTER_TO_INT(cur->data));
-               if (!obj) return;
-
-               if (obj->type == ADDR_ITEM) {
-                       addressbook_append_to_compose_entry
-                               (ADDRESS_ITEM(obj), (ComposeEntryType)data);
-               } else if (obj->type == ADDR_GROUP) {
-                       AddressGroup *group;
-                       GList *cur_item;
-
-                       group = ADDRESS_GROUP(obj);
-                       for (cur_item = group->items; cur_item != NULL;
-                            cur_item = cur_item->next) {
-                               if (ADDRESS_OBJECT(cur_item->data)->type
-                                   != ADDR_ITEM)
-                                       continue;
-                               addressbook_append_to_compose_entry
-                                       (ADDRESS_ITEM(cur_item->data),
-                                        (ComposeEntryType)data);
+       while( node ) {
+               AddressObject *obj = node->data;
+               Compose *compose = addrbook.target_compose;
+               node = g_list_next( node );
+               if( obj->type == ADDR_ITEM_PERSON || obj->type == ADDR_ITEM_EMAIL ) {
+                       gchar *addr = addressbook_format_address( obj );
+                       compose_entry_append( compose, addr, (ComposeEntryType) data );
+                       g_free( addr );
+                       addr = NULL;
+               }
+               else if( obj->type == ADDR_ITEM_GROUP ) {
+                       ItemGroup *group = ( ItemGroup * ) obj;
+                       GList *nodeMail = group->listEMail;
+                       while( nodeMail ) {
+                               ItemEMail *email = nodeMail->data;
+                               gchar *addr = addressbook_format_address( ( AddressObject * ) email );
+                               compose_entry_append( compose, addr, (ComposeEntryType) data );
+                               g_free( addr );
+                               nodeMail = g_list_next( nodeMail );
                        }
                }
        }
 }
 
-static void addressbook_append_to_compose_entry(AddressItem *item,
-                                               ComposeEntryType type)
-{
-       Compose *compose = addrbook.target_compose;
-
-       if (item->name && item->address) {
-               gchar *buf;
-
-               buf = g_strdup_printf
-                       ("%s <%s>", item->name, item->address);
-               compose_entry_append(compose, buf, type);
-               g_free(buf);
-       } else if (item->address)
-               compose_entry_append(compose, item->address, type);
-}
-
 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
-       menu_set_sensitive( addrbook.menu_factory, "/File/New Address", sensitive );
-       menu_set_sensitive( addrbook.menu_factory, "/File/New Group",   sensitive );
-       menu_set_sensitive( addrbook.menu_factory, "/File/New Folder",  sensitive );
+       menu_set_sensitive( addrbook.menu_factory, "/File/New Book",    sensitive );
        menu_set_sensitive( addrbook.menu_factory, "/File/New V-Card",  sensitive );
 #ifdef USE_JPILOT
        menu_set_sensitive( addrbook.menu_factory, "/File/New J-Pilot", sensitive );
@@ -981,356 +1004,320 @@ static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
 #ifdef USE_LDAP
        menu_set_sensitive( addrbook.menu_factory, "/File/New Server",  sensitive );
 #endif
+       menu_set_sensitive( addrbook.menu_factory, "/File/Edit",        sensitive );
+       menu_set_sensitive( addrbook.menu_factory, "/File/Delete",      sensitive );
+
+       menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", sensitive );
+       menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   sensitive );
+       menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  sensitive );
        gtk_widget_set_sensitive( addrbook.reg_btn, sensitive );
        gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
 }
 
 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCTreeNode *node ) {
-       gboolean canEdit = TRUE;
-       if( obj->type == ADDR_FOLDER ) {
-               if( node == addrbook.common ) {
-                       canEdit = FALSE;
+       gboolean canEdit = FALSE;
+       gboolean canAdd = FALSE;
+       gboolean canEditTr = TRUE;
+       gboolean editAddress = FALSE;
+       AddressTypeControlItem *atci = NULL;
+       AddressDataSource *ds = NULL;
+       AddressInterface *iface = NULL;
+
+       if( obj == NULL ) return;
+       if( obj->type == ADDR_INTERFACE ) {
+               AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
+               iface = adapter->interface;
+               if( iface ) {
+                       if( iface->haveLibrary ) {
+                               // Enable appropriate File / New command
+                               atci = adapter->atci;
+                               menu_set_sensitive( addrbook.menu_factory, atci->menuCommand, TRUE );
+                       }
                }
-               if( node == addrbook.personal ) {
-                       canEdit = FALSE;
+               canEditTr = FALSE;
+       }
+       else if( obj->type == ADDR_DATASOURCE ) {
+               AdapterDSource *ads = ADAPTER_DSOURCE(obj);
+               ds = ads->dataSource;
+               iface = ds->interface;
+               if( ! iface->readOnly ) {
+                       canAdd = canEdit = editAddress = TRUE;
                }
-               if( node == addrbook.vcard ) {
-                       canEdit = FALSE;
-                       menu_set_sensitive( addrbook.menu_factory, "/File/New V-Card", TRUE );
+               if( ! iface->haveLibrary ) {
+                       canAdd = canEdit = editAddress = FALSE;
                }
-#ifdef USE_JPILOT
-               else if( node == addrbook.jpilot ) {
-                       canEdit = FALSE;
-                       if( _have_pilot_library_ ) {
-                               menu_set_sensitive( addrbook.menu_factory, "/File/New J-Pilot", TRUE );
+       }
+       else if( obj->type == ADDR_ITEM_FOLDER ) {
+               ds = addressbook_find_datasource( addrbook.treeSelected );
+               if( ds ) {
+                       iface = ds->interface;
+                       if( ! iface->readOnly ) {
+                               canAdd = editAddress = TRUE;
                        }
                }
-#endif
-#ifdef USE_LDAP
-               else if( node == addrbook.ldap ) {
-                       canEdit = FALSE;
-                       if( _have_ldap_library_ ) {
-                               menu_set_sensitive( addrbook.menu_factory, "/File/New Server", TRUE );
+       }
+       else if( obj->type == ADDR_ITEM_GROUP ) {
+               ds = addressbook_find_datasource( addrbook.treeSelected );
+               if( ds ) {
+                       iface = ds->interface;
+                       if( ! iface->readOnly ) {
+                               editAddress = TRUE;
                        }
                }
-#endif
-               else {
-                       menu_set_sensitive( addrbook.menu_factory, "/File/New Address", TRUE );
-                       menu_set_sensitive( addrbook.menu_factory, "/File/New Group",   TRUE );
-                       menu_set_sensitive( addrbook.menu_factory, "/File/New Folder",  TRUE );
-                       gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
-                       gtk_widget_set_sensitive( addrbook.del_btn, TRUE );
-               }
-       }
-       else if( obj->type == ADDR_GROUP ) {
-               menu_set_sensitive( addrbook.menu_factory, "/File/New Address", TRUE );
-               gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
-               gtk_widget_set_sensitive( addrbook.del_btn, TRUE );
-       }
-#ifdef USE_JPILOT
-       else if( obj->type == ADDR_JPILOT ) {
-               if( ! _have_pilot_library_ ) canEdit = FALSE;
-       }
-       else if( obj->type == ADDR_CATEGORY ) {
-               canEdit = FALSE;
-       }
-#endif
-#ifdef USE_LDAP
-       else if( obj->type == ADDR_LDAP ) {
-               if( ! _have_ldap_library_ ) canEdit = FALSE;
        }
-#endif
-       menu_set_sensitive( addrbook.menu_factory, "/File/Edit",    canEdit );
-       menu_set_sensitive( addrbook.menu_factory, "/File/Delete",  canEdit );
+
+       if( addrbook.listSelected == NULL ) canEdit = FALSE;
+
+       // Enable add
+       menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", editAddress );
+       menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   canAdd );
+       menu_set_sensitive( addrbook.menu_factory, "/Address/New Folder",  canAdd );
+       gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
+
+       // Enable edit
+       menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
+       menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canEdit );
+       gtk_widget_set_sensitive( addrbook.del_btn, canEdit );
+
+       menu_set_sensitive( addrbook.menu_factory, "/File/Edit",      canEditTr );
+       menu_set_sensitive( addrbook.menu_factory, "/File/Delete",    canEditTr );
 }
 
 static void addressbook_tree_selected(GtkCTree *ctree, GtkCTreeNode *node,
                                      gint column, gpointer data)
 {
-       AddressObject *obj;
+       AddressObject *obj = NULL;
+       AdapterDSource *ads = NULL;
+       AddressDataSource *ds = NULL;
+       ItemFolder *rootFolder = NULL;
 
-       addrbook.selected = node;
-       addrbook.open_folder = FALSE;
+       addrbook.treeSelected = node;
+       addrbook.listSelected = NULL;
        addressbook_status_show( "" );
-       if( addrbook.entry != NULL ) {
-               gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
-       }
+       if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
 
-       obj = gtk_ctree_node_get_row_data(ctree, node);
+       if( addrbook.clist ) gtk_clist_clear( GTK_CLIST(addrbook.clist) );
+       if( node ) obj = gtk_ctree_node_get_row_data( ctree, node );
        if( obj == NULL ) return;
 
        addrbook.opened = node;
 
-       if(     obj->type == ADDR_GROUP || obj->type == ADDR_FOLDER ||
-               obj->type == ADDR_VCARD || obj->type == ADDR_JPILOT ||
-               obj->type == ADDR_CATEGORY || obj->type == ADDR_LDAP ) {
-               addressbook_set_clist(obj);
-       }
-
-       if( obj->type == ADDR_VCARD ) {
+       if( obj->type == ADDR_DATASOURCE ) {
                // Read from file
-               VCardFile *vcf;
-               vcf = ADDRESS_VCARD(obj)->cardFile;
-               vcard_read_data( vcf );
-               addressbook_vcard_show_message( vcf );
-               ADDRESS_VCARD(obj)->items = vcard_get_address_list( vcf );
-               addressbook_set_clist( obj );
-       }
-#ifdef USE_JPILOT
-       else if( obj->type == ADDR_JPILOT ) {
-               if( _have_pilot_library_ ) {
-                       // Read from file
-                       JPilotFile *jpf;
-                       GList *catList, *catNode;
-                       AddressCategory *acat;
-                       GtkCTreeNode *childNode, *nextNode;
-                       GtkCTreeRow *currRow;
-
-                       jpf = ADDRESS_JPILOT(obj)->pilotFile;
-                       addressbook_jpilot_show_message( jpf );
-                       if( jpilot_get_modified( jpf ) ) {
-                               jpilot_read_data( jpf );
-                               catList = jpilot_get_category_items( jpf );
-
-                               // Remove existing categories
-                               currRow = GTK_CTREE_ROW( node );
-                               if( currRow ) {
-                                       while( nextNode = currRow->children ) {
-                                               gtk_ctree_remove_node( ctree, nextNode );
-                                       }
-                               }
+               static gboolean tVal = TRUE;
 
-                               // Load new categories into the tree.
-                               catNode = catList;
-                               while( catNode ) {
-                                       AddressItem *item = catNode->data;
-                                       acat = g_new(AddressCategory, 1);
-                                       ADDRESS_OBJECT_TYPE(acat) = ADDR_CATEGORY;
-                                       acat->name = g_strdup( item->name );
-                                       acat->items = NULL;
-                                       acat->pilotFile = jpf;
-                                       acat->category = item;
-                                       catNode = g_list_next( catNode );
-                                       addressbook_add_object(node, ADDRESS_OBJECT(acat));
-                               }
+               ads = ADAPTER_DSOURCE(obj);
+               if( ads == NULL ) return;
+               ds = ads->dataSource;
+               if( ds == NULL ) return;                
 
-                               ADDRESS_JPILOT(obj)->items = catList;
-                       }
-                       addressbook_set_clist( obj );
+               if( addrindex_ds_get_modify_flag( ds ) ) {
+                       addrindex_ds_read_data( ds );
                }
-       }
-       else if( obj->type == ADDR_CATEGORY ) {
-               if( _have_pilot_library_ ) {
-                       // Read from file
-                       JPilotFile *jpf;
 
-                       jpf = ADDRESS_JPILOT(obj)->pilotFile;
-                       if( jpilot_get_modified( jpf ) ) {
-                               // Force parent to be reloaded
-                               gtk_ctree_select( GTK_CTREE(addrbook.ctree), GTK_CTREE_ROW(node)->parent);
-                               gtk_ctree_expand( GTK_CTREE(addrbook.ctree), GTK_CTREE_ROW(node)->parent);
+               if( ! addrindex_ds_get_read_flag( ds ) ) {
+                       addrindex_ds_read_data( ds );
+               }
+               addressbook_ds_show_message( ds );
+
+               if( ! addrindex_ds_get_access_flag( ds ) ) {
+                       // Remove existing folders and groups
+                       gtk_clist_freeze( GTK_CLIST(ctree) );
+                       addressbook_tree_remove_children( ctree, node );
+                       gtk_clist_thaw( GTK_CLIST(ctree) );
+
+                       // Load folders into the tree
+                       rootFolder = addrindex_ds_get_root_folder( ds );
+                       if( ds->type == ADDR_IF_JPILOT ) {
+                               addressbook_node_add_folder( node, ds, rootFolder, ADDR_CATEGORY );
                        }
                        else {
-                               AddressItem *item = NULL;
-                               AddressCategory *acat = ADDRESS_CATEGORY(obj);
-                               if( acat ) item = acat->category;
-                               if( item ) {
-                                       ADDRESS_CATEGORY(obj)->items =
-                                               jpilot_get_address_list_cat( jpf, item->categoryID );
-                               }
-                               addressbook_set_clist( obj );
-                       }
-               }
-       }
-#endif
-#ifdef USE_LDAP
-       else if( obj->type == ADDR_LDAP ) {
-               if( _have_ldap_library_ ) {
-                       // Read from cache
-                       SyldapServer *server;
-                       server = ADDRESS_LDAP(obj)->ldapServer;
-                       addressbook_ldap_show_message( server );
-                       if( ! server->busyFlag ) {
-                               ADDRESS_LDAP(obj)->items = syldap_get_address_list( server );
-                               addressbook_set_clist( obj );
+                               addressbook_node_add_folder( node, ds, rootFolder, ADDR_ITEM_FOLDER );
                        }
+                       addrindex_ds_set_access_flag( ds, &tVal );
+                       gtk_ctree_expand( ctree, node );
                }
        }
-#endif
+
+       // Update address list
+       addressbook_set_clist( obj );
 
        // Setup main menu selections
        addressbook_menubar_set_sensitive( FALSE );
        addressbook_menuitem_set_sensitive( obj, node );
+
+       addressbook_list_select_clear();
+
 }
 
 static void addressbook_list_selected(GtkCList *clist, gint row, gint column,
                                      GdkEvent *event, gpointer data)
 {
-       GtkEntry *entry = GTK_ENTRY(addrbook.entry);
-       AddressObject *obj;
-       GList *cur;
-
        if (event && event->type == GDK_2BUTTON_PRESS) {
+               // Handle double click
                if (prefs_common.add_address_by_click &&
                    addrbook.target_compose)
                        addressbook_to_clicked(NULL, NULL);
                else
                        addressbook_edit_address_cb(NULL, 0, NULL);
-               return;
        }
+}
 
-#if 0
-       gtk_signal_handler_block_by_func
-               (GTK_OBJECT(entry),
-                GTK_SIGNAL_FUNC(addressbook_entry_changed), NULL);
-#endif          
-
-       gtk_entry_set_text(entry, "");
-
-       for (cur = clist->selection; cur != NULL; cur = cur->next) {
-               obj = gtk_clist_get_row_data(clist,
-                                            GPOINTER_TO_INT(cur->data));
-               g_return_if_fail(obj != NULL);
-
-               if (obj->type == ADDR_ITEM) {
-                       AddressItem *item;
-
-                       item = ADDRESS_ITEM(obj);
-                       if (item->name && item->address) {
-                               gchar *buf;
-
-                               buf = g_strdup_printf
-                                       ("%s <%s>", item->name, item->address);
-                               if (*gtk_entry_get_text(entry) != '\0')
-                                       gtk_entry_append_text(entry, ", ");
-                               gtk_entry_append_text(entry, buf);
-                               g_free(buf);
-                       } else if (item->address) {
-                               if (*gtk_entry_get_text(entry) != '\0')
-                                       gtk_entry_append_text(entry, ", ");
-                               gtk_entry_append_text(entry, item->address);
+static void addressbook_list_select_show() {
+       GList *node = _addressListSelection_;
+       gchar *addr = NULL;
+       printf( "show selection...>>>\n" );
+       while( node != NULL ) {
+               AddressObject *obj = ( AddressObject * ) node->data;
+               if( obj ) {
+                       printf( "- %d : '%s'\n", obj->type, obj->name );
+                       if( obj->type == ADDR_ITEM_GROUP ) {
+                               ItemGroup *group = ( ItemGroup * ) obj;
+                               GList *node = group->listEMail;
+                               while( node ) {
+                                       ItemEMail *email = node->data;
+                                       addr = addressbook_format_address( ( AddressObject * ) email );
+                                       if( addr ) {
+                                               printf( "\tgrp >%s<\n", addr );
+                                               g_free( addr );
+                                       }
+                                       node = g_list_next( node );
+                               }
+                       }
+                       else {
+                               addr = addressbook_format_address( obj );
+                               if( addr ) {
+                                       printf( "\t>%s<\n", addr );
+                                       g_free( addr );
+                               }
                        }
                }
+               else {
+                       printf( "- NULL" );
+               }
+               node = g_list_next( node );
        }
-
-#if 0
-       gtk_signal_handler_unblock_by_func
-               (GTK_OBJECT(entry),
-                GTK_SIGNAL_FUNC(addressbook_entry_changed), NULL);
-#endif          
+       printf( "show selection...<<<\n" );
 }
 
-#if 0
-static void addressbook_entry_changed(GtkWidget *widget)
-{
-       GtkCList *clist = GTK_CLIST(addrbook.clist);
-       GtkEntry *entry = GTK_ENTRY(addrbook.entry);
-       const gchar *str;
-       gint len;
-       gint row;
-
-       //if (clist->selection && clist->selection->next) return;
-
-       str = gtk_entry_get_text(entry);
-       if (*str == '\0') {
-               gtk_clist_unselect_all(clist);
-               return;
+static void addressbook_list_select_clear() {
+       if( _addressListSelection_ ) {
+               g_list_free( _addressListSelection_ );
        }
-       len = strlen(str);
-
-       for (row = 0; row < clist->rows; row++) {
-               AddressObject *obj;
-               const gchar *name;
-
-               obj = ADDRESS_OBJECT(gtk_clist_get_row_data(clist, row));
-               if (!obj) continue;
-               if (obj->type == ADDR_ITEM)
-                       name = ADDRESS_ITEM(obj)->name;
-               else if (obj->type == ADDR_GROUP)
-                       name = ADDRESS_GROUP(obj)->name;
-               else
-                       continue;
+       _addressListSelection_ = NULL;
+}
 
-               if (name && !strncasecmp(name, str, len)) {
-                       gtk_clist_unselect_all(clist);
-                       gtk_clist_select_row(clist, row, -1);
-                       return;
+static void addressbook_list_select_add( AddressObject *obj ) {
+       GList *node;
+       if( obj ) {
+               if(     obj->type == ADDR_ITEM_PERSON ||
+                       obj->type == ADDR_ITEM_EMAIL ||
+                       obj->type == ADDR_ITEM_GROUP ) {
+                       if( ! g_list_find( _addressListSelection_, obj ) ) {
+                               _addressListSelection_ = g_list_append( _addressListSelection_, obj );
+                       }
                }
        }
-
-       gtk_clist_unselect_all(clist);
+       // addressbook_list_select_show();
 }
-#endif
 
-static void addressbook_entry_gotfocus( GtkWidget *widget ) {
-       gtk_editable_select_region( GTK_EDITABLE(addrbook.entry), 0, -1 );
+static void addressbook_list_select_remove( AddressObject *obj ) {
+       if( obj == NULL ) return;
+       if( _addressListSelection_ ) {
+               _addressListSelection_ = g_list_remove( _addressListSelection_, obj );
+       }
+       // addressbook_list_select_show();
 }
 
-static void addressbook_list_button_pressed(GtkWidget *widget,
-                                           GdkEventButton *event,
-                                           gpointer data)
-{
-       GtkCList *clist = GTK_CLIST(widget);
-       gint row, column;
-       gint tRow, tCol;
-       AddressObject *obj, *pobj;
-
-       if (!event) return;
-
-       obj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
-                                         addrbook.opened);
-       g_return_if_fail(obj != NULL);
-
-       pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.selected);
-       if( pobj ) {
-               if(     pobj->type == ADDR_VCARD ||
-                       pobj->type == ADDR_JPILOT ||
-                       pobj->type == ADDR_CATEGORY ||
-                       pobj->type == ADDR_LDAP ) {
-                       menu_set_sensitive(addrbook.menu_factory, "/File/Edit", FALSE);
-                       menu_set_sensitive(addrbook.menu_factory, "/File/Delete", FALSE);
+static void addressbook_list_row_selected( GtkCTree *clist, GtkCTreeNode *node, gint column, gpointer data ) {
+       GtkEntry *entry = GTK_ENTRY(addrbook.entry);
+       AddressObject *obj = NULL;
+       AddressObject *pobj = NULL;
+       AdapterDSource *ads = NULL;
+       AddressInterface *iface = NULL;
+       AddressDataSource *ds = NULL;
+       gboolean canEdit = FALSE;
+       gboolean canDelete = FALSE;
+
+       gtk_entry_set_text( entry, "" );
+       addrbook.listSelected = node;
+       obj = gtk_ctree_node_get_row_data( clist, node );
+       if( obj != NULL ) {
+               // printf( "list select: %d : '%s'\n", obj->type, obj->name );
+               addressbook_list_select_add( obj );
+       }
+
+       pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
+       if( pobj == NULL ) return;
+
+       menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
+
+       if( pobj->type == ADDR_DATASOURCE ) {
+               ads = ADAPTER_DSOURCE(pobj);
+               ds = ads->dataSource;
+               iface = ds->interface;
+               if( ! iface->readOnly ) {
+                       canEdit = TRUE;
+                       menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
+                       menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
+                       menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
+                       gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
+               }
+       }
+       else if( pobj->type != ADDR_INTERFACE ) {
+               ds = addressbook_find_datasource( addrbook.treeSelected );
+               iface = ds->interface;
+               if( ! iface->readOnly ) {
+                       if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
+                               canEdit = TRUE;
+                               menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
+                               gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
+                       }
+                       if( pobj->type == ADDR_ITEM_FOLDER ) {
+                               canEdit = TRUE;
+                               menu_set_sensitive( addrbook.list_factory, "/New Folder", TRUE );
+                               menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
+                       }
                }
        }
+       if( obj == NULL ) canEdit = FALSE;
+       canDelete = canEdit;
+       if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) canEdit = FALSE;
 
-       if (event->button != 3) return;
-       menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.list_popup));
+       menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
+       menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
 
-       if (gtk_clist_get_selection_info
-               (clist, event->x, event->y, &row, &column)) {
-               GtkCListRow *clist_row;
+       menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
+       menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canDelete );
 
-               clist_row = g_list_nth(clist->row_list, row)->data;
-               if (clist_row->state != GTK_STATE_SELECTED) {
-                       gtk_clist_unselect_all(clist);
-                       gtk_clist_select_row(clist, row, column);
-               }
-               gtkut_clist_set_focus_row(clist, row);
+       gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
 
-               if(     obj->type != ADDR_VCARD &&
-                       obj->type != ADDR_JPILOT &&
-                       obj->type != ADDR_CATEGORY &&
-                       obj->type != ADDR_LDAP ) {
-                       menu_set_sensitive(addrbook.list_factory, "/Edit", TRUE);
-                       menu_set_sensitive(addrbook.list_factory, "/Delete", TRUE);
-               }
+}
+
+static void addressbook_list_row_unselected( GtkCTree *clist, GtkCTreeNode *node, gint column, gpointer data ) {
+       GtkEntry *entry = GTK_ENTRY(addrbook.entry);
+       AddressObject *obj;
+
+       obj = gtk_ctree_node_get_row_data( clist, node );
+       if( obj != NULL ) {
+               // printf( "list unselect: %d : '%s'\n", obj->type, obj->name );
+               addressbook_list_select_remove( obj );
        }
+}
 
-       if( !(  addrbook.opened == addrbook.vcard ||
-               addrbook.opened == addrbook.jpilot ||
-               addrbook.opened == addrbook.ldap ) ) {
+static void addressbook_entry_gotfocus( GtkWidget *widget ) {
+       gtk_editable_select_region( GTK_EDITABLE(addrbook.entry), 0, -1 );
+}
 
-               if( obj->type == ADDR_FOLDER || obj->type == ADDR_GROUP ) {
-                       menu_set_sensitive(addrbook.list_factory, "/New Address", TRUE);
-                       gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
-                       gtk_widget_set_sensitive( addrbook.del_btn, TRUE );
-               }
-               if (obj->type == ADDR_FOLDER) {
-                       menu_set_sensitive(addrbook.list_factory, "/New Folder", TRUE);
-                       menu_set_sensitive(addrbook.list_factory, "/New Group", TRUE);
-               }
+static void addressbook_list_button_pressed(GtkWidget *widget,
+                                           GdkEventButton *event,
+                                           gpointer data)
+{
+       if( ! event ) return;
+       if( event->button == 3 ) {
+               gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
+                      event->button, event->time );
        }
-       gtk_menu_popup(GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
-                      event->button, event->time);
 }
 
 static void addressbook_list_button_released(GtkWidget *widget,
@@ -1345,94 +1332,70 @@ static void addressbook_tree_button_pressed(GtkWidget *ctree,
 {
        GtkCList *clist = GTK_CLIST(ctree);
        gint row, column;
-       AddressObject *obj;
+       AddressObject *obj = NULL;
        GtkCTreeNode *node;
+       AdapterDSource *ads = NULL;
+       AddressInterface *iface = NULL;
+       AddressDataSource *ds = NULL;
+       AddressTypeControlItem *atci = NULL;
+       gboolean canEdit = FALSE;
 
-       if (!event) return;
-       if (event->button == 1) {
-               addrbook.open_folder = TRUE;
-               return;
+       if( ! event ) return;
+       addressbook_menubar_set_sensitive( FALSE );
+/* */
+       if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
+               gtk_clist_select_row( clist, row, column );
+               gtkut_clist_set_focus_row(clist, row);
+               obj = gtk_clist_get_row_data( clist, row );
        }
-       if (event->button != 3) return;
-
-       if (!gtk_clist_get_selection_info
-               (clist, event->x, event->y, &row, &column)) return;
-       gtk_clist_select_row(clist, row, column);
-
-       obj = gtk_clist_get_row_data(clist, row);
-       g_return_if_fail(obj != NULL);
-
+/* */
        menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
 
-       if (obj->type == ADDR_FOLDER) {
-               node = gtk_ctree_node_nth(GTK_CTREE(ctree), row);
-               if( node == addrbook.vcard ) {
-                       menu_set_sensitive(addrbook.tree_factory, "/New V-Card", TRUE);
-               }
-#ifdef USE_JPILOT
-               else if( node == addrbook.jpilot ) {
-                       if( _have_pilot_library_ ) {
-                               menu_set_sensitive(addrbook.tree_factory, "/New J-Pilot", TRUE);
-                       }
-               }
-#endif
-#ifdef USE_LDAP
-               else if( node == addrbook.ldap ) {
-                       if( _have_ldap_library_ ) {
-                               menu_set_sensitive(addrbook.tree_factory, "/New Server", TRUE);
-                       }
-               }
-#endif
-               else {
-                       menu_set_sensitive(addrbook.tree_factory, "/New Address", TRUE);
-                       menu_set_sensitive(addrbook.tree_factory, "/New Folder", TRUE);
-                       menu_set_sensitive(addrbook.tree_factory, "/New Group", TRUE);
-                       if (node && GTK_CTREE_ROW(node)->level >= 2) {
-                               menu_set_sensitive(addrbook.tree_factory, "/Edit", TRUE);
-                               menu_set_sensitive(addrbook.tree_factory, "/Delete", TRUE);
-                       }
+       if( obj == NULL ) return;
+       if (obj->type == ADDR_DATASOURCE) {
+               ads = ADAPTER_DSOURCE(obj);
+               ds = ads->dataSource;
+               iface = ds->interface;
+               canEdit = TRUE;
+               if( ! iface->readOnly ) {
+                       menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
+                       menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
+                       menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
                        gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
-                       gtk_widget_set_sensitive( addrbook.del_btn, TRUE );
-               }
-       }
-       else if (obj->type == ADDR_GROUP) {
-               menu_set_sensitive(addrbook.tree_factory, "/New Address", TRUE);
-               menu_set_sensitive(addrbook.tree_factory, "/Edit", TRUE);
-               menu_set_sensitive(addrbook.tree_factory, "/Delete", TRUE);
-               gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
-               gtk_widget_set_sensitive( addrbook.del_btn, TRUE );
-       }
-       else if (obj->type == ADDR_VCARD) {
-               menu_set_sensitive(addrbook.tree_factory, "/Edit", TRUE);
-               menu_set_sensitive(addrbook.tree_factory, "/Delete", TRUE);
-       }
-#ifdef USE_JPILOT
-       else if (obj->type == ADDR_JPILOT) {
-               if( _have_pilot_library_ ) {
-                       menu_set_sensitive(addrbook.tree_factory, "/Edit", TRUE);
-                       menu_set_sensitive(addrbook.tree_factory, "/Delete", TRUE);
                }
        }
-       else if (obj->type == ADDR_CATEGORY) {
-               if( _have_pilot_library_ ) {
-                       menu_set_sensitive(addrbook.tree_factory, "/Edit", FALSE);
-                       menu_set_sensitive(addrbook.tree_factory, "/Delete", FALSE);
+       else if (obj->type == ADDR_ITEM_FOLDER) {
+               ds = addressbook_find_datasource( addrbook.treeSelected );
+               iface = ds->interface;
+               if( ! iface->readOnly ) {
+                       canEdit = TRUE;
+                       menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
+                       menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
+                       menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
+                       gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
                }
        }
-#endif
-#ifdef USE_LDAP
-       else if (obj->type == ADDR_LDAP) {
-               if( _have_ldap_library_ ) {
-                       menu_set_sensitive(addrbook.tree_factory, "/Edit", TRUE);
-                       menu_set_sensitive(addrbook.tree_factory, "/Delete", TRUE);
+       else if (obj->type == ADDR_ITEM_GROUP) {
+               ds = addressbook_find_datasource( addrbook.treeSelected );
+               iface = ds->interface;
+               if( ! iface->readOnly ) {
+                       canEdit = TRUE;
+                       menu_set_sensitive( addrbook.tree_factory, "/New Address", TRUE );
+                       gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
                }
        }
-#endif
-       else {
-               return;
+
+       // Enable edit
+       menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
+       menu_set_sensitive( addrbook.tree_factory, "/Delete", canEdit );
+       menu_set_sensitive( addrbook.menu_factory, "/File/Edit",   canEdit );
+       menu_set_sensitive( addrbook.menu_factory, "/File/Delete", canEdit );
+
+       if( event->button == 3 ) {
+               gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
+                              event->button, event->time);
        }
-       gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
-                      event->button, event->time);
+
 }
 
 static void addressbook_tree_button_released(GtkWidget *ctree,
@@ -1456,90 +1419,76 @@ static void addressbook_new_folder_cb(gpointer data, guint action,
                                      GtkWidget *widget)
 {
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
-       AddressObject *obj;
-       AddressFolder *folder;
-       gchar *new_folder;
-
-       if (!addrbook.selected) return;
+       AddressObject *obj = NULL;
+       AddressDataSource *ds = NULL;
+       AddressBookFile *abf = NULL;
+       ItemFolder *parentFolder = NULL;
+       ItemFolder *folder = NULL;
+
+       if( ! addrbook.treeSelected ) return;
+       obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
+       if( obj == NULL ) return;
+       ds = addressbook_find_datasource( addrbook.treeSelected );
+       if( ds == NULL ) return;
 
-       obj = gtk_ctree_node_get_row_data(ctree, addrbook.selected);
-       g_return_if_fail(obj != NULL);
-       if (obj->type != ADDR_FOLDER) return;
-
-       new_folder = input_dialog(_("New folder"),
-                                 _("Input the name of new folder:"),
-                                 _("NewFolder"));
-       if (!new_folder) return;
-       g_strstrip(new_folder);
-       if (*new_folder == '\0') {
-               g_free(new_folder);
-               return;
+       if( obj->type == ADDR_DATASOURCE ) {
+               if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
        }
-
-       if (gtk_ctree_find_by_row_data_custom(ctree, addrbook.selected,
-                                             new_folder,
-                                             addressbook_obj_name_compare)) {
-               alertpanel_error(_("The name already exists."));
-               g_free(new_folder);
+       else if( obj->type == ADDR_ITEM_FOLDER ) {
+               parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
+       }
+       else {
                return;
        }
 
-       folder = g_new(AddressFolder, 1);
-       ADDRESS_OBJECT_TYPE(folder) = ADDR_FOLDER;
-       folder->name = g_strdup(new_folder);
-       folder->items = NULL;
-
-       addressbook_add_object(addrbook.selected, ADDRESS_OBJECT(folder));
-
-       g_free(new_folder);
+       abf = ds->rawDataSource;
+       if( abf == NULL ) return;
+       folder = addressbook_edit_folder( abf, parentFolder, NULL );
+       if( folder ) {
+               GtkCTreeNode *nn;
+               nn = addressbook_node_add_folder( addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
+               gtk_ctree_expand( ctree, addrbook.treeSelected );
+               if( addrbook.treeSelected == addrbook.opened ) addressbook_set_clist(obj);
+       }
 
-       if (addrbook.selected == addrbook.opened)
-               addressbook_set_clist(obj);
 }
 
 static void addressbook_new_group_cb(gpointer data, guint action,
                                     GtkWidget *widget)
 {
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
-       AddressObject *obj;
-       AddressGroup *group;
-       gchar *new_group;
-
-       if (!addrbook.selected) return;
+       AddressObject *obj = NULL;
+       AddressDataSource *ds = NULL;
+       AddressBookFile *abf = NULL;
+       ItemFolder *parentFolder = NULL;
+       ItemGroup *group = NULL;
+
+       if( ! addrbook.treeSelected ) return;
+       obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
+       if( obj == NULL ) return;
+       ds = addressbook_find_datasource( addrbook.treeSelected );
+       if( ds == NULL ) return;
 
-       obj = gtk_ctree_node_get_row_data(ctree, addrbook.selected);
-       g_return_if_fail(obj != NULL);
-       if (obj->type != ADDR_FOLDER) return;
-
-       new_group = input_dialog(_("New group"),
-                                _("Input the name of new group:"),
-                                 _("NewGroup"));
-       if (!new_group) return;
-       g_strstrip(new_group);
-       if (*new_group == '\0') {
-               g_free(new_group);
-               return;
+       if( obj->type == ADDR_DATASOURCE ) {
+               if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
        }
-
-       if (gtk_ctree_find_by_row_data_custom(ctree, addrbook.selected,
-                                             new_group,
-                                             addressbook_obj_name_compare)) {
-               alertpanel_error(_("The name already exists."));
-               g_free(new_group);
+       else if( obj->type == ADDR_ITEM_FOLDER ) {
+               parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
+       }
+       else {
                return;
        }
 
-       group = g_new(AddressGroup, 1);
-       ADDRESS_OBJECT_TYPE(group) = ADDR_GROUP;
-       group->name = g_strdup(new_group);
-       group->items = NULL;
-
-       addressbook_add_object(addrbook.selected, ADDRESS_OBJECT(group));
-
-       g_free(new_group);
+       abf = ds->rawDataSource;
+       if( abf == NULL ) return;
+       group = addressbook_edit_group( abf, parentFolder, NULL );
+       if( group ) {
+               GtkCTreeNode *nn;
+               nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
+               gtk_ctree_expand( ctree, addrbook.treeSelected );
+               if( addrbook.treeSelected == addrbook.opened ) addressbook_set_clist(obj);
+       }
 
-       if (addrbook.selected == addrbook.opened)
-               addressbook_set_clist(obj);
 }
 
 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
@@ -1559,460 +1508,388 @@ static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
                                is_leaf, expanded);
 }
 
-static void addressbook_edit_group(GtkCTreeNode *group_node)
-{
-       GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
-       GtkCList *clist = GTK_CLIST(addrbook.clist);
-       AddressObject *obj;
-       AddressGroup *group;
-       gchar *new_name;
-       GtkCTreeNode *node;
-
-       if (!group_node && clist->selection) {
-               obj = gtk_clist_get_row_data(clist,
-                                            GPOINTER_TO_INT(clist->selection->data));
-               g_return_if_fail(obj != NULL);
-               if (obj->type != ADDR_GROUP) return;
-               node = gtk_ctree_find_by_row_data
-                       (ctree, addrbook.selected, obj);
-               if (!node) return;
-       } else {
-               if (group_node)
-                       node = group_node;
-               else
-                       node = addrbook.selected;
-               obj = gtk_ctree_node_get_row_data(ctree, node);
-               g_return_if_fail(obj != NULL);
-               if (obj->type != ADDR_GROUP) return;
-       }
+/*
+* Edit data source.
+* Enter: obj   Address object to edit.
+*        node  Node in tree.
+* Return: New name of data source.
+*/
+static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
+       gchar *newName = NULL;
+       AddressDataSource *ds = NULL;
+       AddressInterface *iface = NULL;
+       AdapterDSource *ads = NULL;
 
-       group = ADDRESS_GROUP(obj);
+       ds = addressbook_find_datasource( node );
+       if( ds == NULL ) return NULL;
+       iface = ds->interface;
+       if( ! iface->haveLibrary ) return NULL;
 
-       new_name = input_dialog(_("Edit group"),
-                               _("Input the new name of group:"),
-                               group->name);
-       if (!new_name) return;
-       g_strstrip(new_name);
-       if (*new_name == '\0') {
-               g_free(new_name);
-               return;
+       // Read data from data source
+       if( ! addrindex_ds_get_read_flag( ds ) ) {
+               addrindex_ds_read_data( ds );
        }
 
-       if (gtk_ctree_find_by_row_data_custom(ctree, addrbook.selected,
-                                             new_name,
-                                             addressbook_obj_name_compare)) {
-               alertpanel_error(_("The name already exists."));
-               g_free(new_name);
-               return;
+       // Handle edit
+       ads = ADAPTER_DSOURCE(obj);
+       if( ads->subType == ADDR_BOOK ) {
+                if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
        }
-
-       g_free(group->name);
-       group->name = g_strdup(new_name);
-
-       addressbook_change_node_name(node, new_name);
-       gtk_ctree_sort_node(ctree, GTK_CTREE_ROW(node)->parent);
-
-       g_free(new_name);
-
-       addrbook.open_folder = TRUE;
-       gtk_ctree_select(ctree, addrbook.opened);
+       else if( ads->subType == ADDR_VCARD ) {
+                       if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
+       }
+#ifdef USE_JPILOT
+       else if( ads->subType == ADDR_JPILOT ) {
+                if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
+       }
+#endif
+#ifdef USE_LDAP
+       else if( ads->subType == ADDR_LDAP ) {
+               if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
+       }
+#endif
+       else {
+               return NULL;
+       }
+       newName = obj->name;
+       return newName;
 }
 
-static void addressbook_edit_folder_cb(gpointer data, guint action,
+/*
+* Edit an object that is in the address tree area.
+*/
+static void addressbook_treenode_edit_cb(gpointer data, guint action,
                                       GtkWidget *widget)
 {
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
        AddressObject *obj;
-       AddressFolder *folder;
-       gchar *new_name = NULL;
+       AddressDataSource *ds = NULL;
+       AddressBookFile *abf = NULL;
        GtkCTreeNode *node = NULL, *parentNode = NULL;
+       gchar *name = NULL;
 
-       if (!addrbook.selected) return;
-       if (GTK_CTREE_ROW(addrbook.selected)->level == 1) return;
-
-       obj = gtk_ctree_node_get_row_data(ctree, addrbook.selected);
-       g_return_if_fail(obj != NULL);
-       g_return_if_fail(obj->type == ADDR_FOLDER || obj->type == ADDR_GROUP ||
-                       obj->type == ADDR_VCARD || obj->type == ADDR_JPILOT ||
-                       obj->type == ADDR_CATEGORY || obj->type == ADDR_LDAP );
+       if( ! addrbook.treeSelected ) return;
+       node = addrbook.treeSelected;
+       if( GTK_CTREE_ROW(node)->level == 1 ) return;
+       obj = gtk_ctree_node_get_row_data( ctree, node );
+       if( obj == NULL ) return;
+       parentNode = GTK_CTREE_ROW(node)->parent;
 
-       if (obj->type == ADDR_GROUP) {
-               addressbook_edit_group(addrbook.selected);
-               return;
-       }
+       ds = addressbook_find_datasource( node );
+       if( ds == NULL ) return;
 
-       if( obj->type == ADDR_VCARD ) {
-                AddressVCard *vcard = ADDRESS_VCARD(obj);
-                if( addressbook_edit_vcard( vcard ) == NULL ) return;
-               new_name = vcard->name;
-               parentNode = addrbook.vcard;
-       }
-#ifdef USE_JPILOT
-       else if( obj->type == ADDR_JPILOT ) {
-                AddressJPilot *jpilot = ADDRESS_JPILOT(obj);
-               if( ! _have_pilot_library_ ) return;
-                if( addressbook_edit_jpilot( jpilot ) == NULL ) return;
-               new_name = jpilot->name;
-               parentNode = addrbook.jpilot;
-       }
-#endif
-#ifdef USE_LDAP
-       else if( obj->type == ADDR_LDAP ) {
-                AddressLDAP *ldapi = ADDRESS_LDAP(obj);
-               if( ! _have_ldap_library_ ) return;
-                if( addressbook_edit_ldap( ldapi ) == NULL ) return;
-               new_name = ldapi->name;
-               parentNode = addrbook.ldap;
+       if( obj->type == ADDR_DATASOURCE ) {
+               name = addressbook_edit_datasource( obj, node );
+               if( name == NULL ) return;
        }
-#endif
-
-       if( new_name && parentNode) {
+       else {
+               abf = ds->rawDataSource;
+               if( abf == NULL ) return;
+               if( obj->type == ADDR_ITEM_FOLDER ) {
+                       AdapterFolder *adapter = ADAPTER_FOLDER(obj);
+                       ItemFolder *item = adapter->itemFolder;
+                       ItemFolder *parentFolder = NULL;
+                       parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
+                       if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
+                       name = ADDRITEM_NAME(item);
+               }
+               else if( obj->type == ADDR_ITEM_GROUP ) {
+                       AdapterGroup *adapter = ADAPTER_GROUP(obj);
+                       ItemGroup *item = adapter->itemGroup;
+                       ItemFolder *parentFolder = NULL;
+                       parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
+                       if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
+                       name = ADDRITEM_NAME(item);
+               }
+       }
+       if( name && parentNode ) {
                // Update node in tree view
-               node = gtk_ctree_find_by_row_data( ctree, addrbook.selected, obj );
-               if( ! node ) return;
-               addressbook_change_node_name( node, new_name );
+               addressbook_change_node_name( node, name );
                gtk_ctree_sort_node(ctree, parentNode);
-               addrbook.open_folder = TRUE;
-               gtk_ctree_select( GTK_CTREE(addrbook.ctree), node );
-               return;
+               gtk_ctree_expand( ctree, node );
+               gtk_ctree_select( ctree, node );
        }
-
-       folder = ADDRESS_FOLDER(obj);
-       new_name = input_dialog(_("Edit folder"),
-                               _("Input the new name of folder:"),
-                               folder->name);
-
-       if (!new_name) return;
-       g_strstrip(new_name);
-       if (*new_name == '\0') {
-               g_free(new_name);
-               return;
-       }
-
-       if (gtk_ctree_find_by_row_data_custom(ctree, addrbook.selected,
-                                             new_name,
-                                             addressbook_obj_name_compare)) {
-               alertpanel_error(_("The name already exists."));
-               g_free(new_name);
-               return;
-       }
-
-       g_free(folder->name);
-       folder->name = g_strdup(new_name);
-
-       addressbook_change_node_name(addrbook.selected, new_name);
-       gtk_ctree_sort_node(ctree, GTK_CTREE_ROW(addrbook.selected)->parent);
-
-       g_free(new_name);
 }
 
-static void addressbook_delete_folder_cb(gpointer data, guint action,
+/*
+* Delete an item from the tree widget.
+*/
+static void addressbook_treenode_delete_cb(gpointer data, guint action,
                                         GtkWidget *widget)
 {
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
-       AddressObject *obj, *pobj;
-       gchar *name;
+       GtkCTreeNode *node = NULL;
+       AddressObject *obj;
        gchar *message;
        AlertValue aval;
+       AddressBookFile *abf = NULL;
+       AdapterDSource *ads = NULL;
+       AddressInterface *iface = NULL;
+       AddressDataSource *ds = NULL;
+       gboolean remFlag = FALSE;
 
-       if (!addrbook.selected) return;
-       if (GTK_CTREE_ROW(addrbook.selected)->level == 1) return;
+       if( ! addrbook.treeSelected ) return;
+       node = addrbook.treeSelected;
+       if( GTK_CTREE_ROW(node)->level == 1 ) return;
 
-       obj = gtk_ctree_node_get_row_data(ctree, addrbook.selected);
+       obj = gtk_ctree_node_get_row_data( ctree, node );
        g_return_if_fail(obj != NULL);
 
-       if (obj->type == ADDR_GROUP)
-               name = ADDRESS_GROUP(obj)->name;
-       else if (obj->type == ADDR_FOLDER)
-               name = ADDRESS_FOLDER(obj)->name;
-       else if (obj->type == ADDR_VCARD)
-               name = ADDRESS_VCARD(obj)->name;
-#ifdef USE_JPILOT
-       else if (obj->type == ADDR_JPILOT) {
-               if( ! _have_pilot_library_ ) return;
-               name = ADDRESS_JPILOT(obj)->name;
-       }
-#endif
-#ifdef USE_LDAP
-       else if (obj->type == ADDR_LDAP) {
-               if( ! _have_ldap_library_ ) return;
-               name = ADDRESS_LDAP(obj)->name;
+       if( obj->type == ADDR_DATASOURCE ) {
+               ads = ADAPTER_DSOURCE(obj);
+               if( ads == NULL ) return;
+               ds = ads->dataSource;
+               if( ds == NULL ) return;
        }
-#endif
-       else
-               return;
-
-       message = g_strdup_printf(_("Really delete `%s' ?"), name);
-       aval = alertpanel(_("Delete"), message, _("Yes"), _("No"), NULL);
-       g_free(message);
-       if (aval != G_ALERTDEFAULT) return;
-
-       pobj = gtk_ctree_node_get_row_data
-               (ctree, GTK_CTREE_ROW(addrbook.selected)->parent);
-       if (!pobj) return;
-       g_return_if_fail(pobj->type == ADDR_FOLDER);
-       ADDRESS_FOLDER(pobj)->items =
-               g_list_remove(ADDRESS_FOLDER(pobj)->items, obj);
-
-       addressbook_delete_object(obj);
-       addrbook.open_folder = TRUE;
-       gtk_ctree_remove_node(ctree, addrbook.selected);
-       addrbook.open_folder = FALSE;
-}
-
-#define SET_LABEL_AND_ENTRY(str, entry, top) \
-{ \
-       label = gtk_label_new(str); \
-       gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), \
-                        GTK_FILL, 0, 0, 0); \
-       gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); \
- \
-       entry = gtk_entry_new(); \
-       gtk_table_attach(GTK_TABLE(table), entry, 1, 2, top, (top + 1), \
-                        GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); \
-}
-
-static void addressbook_edit_address_create(gboolean *cancelled)
-{
-       GtkWidget *window;
-       GtkWidget *vbox;
-       GtkWidget *table;
-       GtkWidget *label;
-       GtkWidget *name_entry;
-       GtkWidget *addr_entry;
-       GtkWidget *rem_entry;
-       GtkWidget *hbbox;
-       GtkWidget *ok_btn;
-       GtkWidget *cancel_btn;
-
-       debug_print("Creating edit_address window...\n");
-
-       window = gtk_window_new(GTK_WINDOW_DIALOG);
-       gtk_widget_set_usize(window, 400, -1);
-       gtk_container_set_border_width(GTK_CONTAINER(window), 8);
-       gtk_window_set_title(GTK_WINDOW(window), _("Edit address"));
-       gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
-       gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
-       gtk_signal_connect(GTK_OBJECT(window), "delete_event",
-                          GTK_SIGNAL_FUNC(edit_address_delete_event),
-                          cancelled);
-       gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
-                          GTK_SIGNAL_FUNC(edit_address_key_pressed),
-                          cancelled);
-
-       vbox = gtk_vbox_new(FALSE, 8);
-       gtk_container_add(GTK_CONTAINER(window), vbox);
-
-       table = gtk_table_new(3, 2, FALSE);
-       gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
-       gtk_table_set_row_spacings(GTK_TABLE(table), 8);
-       gtk_table_set_col_spacings(GTK_TABLE(table), 8);
-
-       SET_LABEL_AND_ENTRY(_("Name"),    name_entry, 0);
-       SET_LABEL_AND_ENTRY(_("Address"), addr_entry, 1);
-       SET_LABEL_AND_ENTRY(_("Remarks"), rem_entry,  2);
+       else {
+               // Must be folder or something else
+               ds = addressbook_find_datasource( node );
+               if( ds == NULL ) return;
 
-       gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
-                               &cancel_btn, _("Cancel"), NULL, NULL);
-       gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
-       gtk_widget_grab_default(ok_btn);
+               // Only allow deletion from non-readOnly data sources
+               iface = ds->interface;
+               if( iface->readOnly ) return;
+       }
 
-       gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
-                          GTK_SIGNAL_FUNC(edit_address_ok), cancelled);
-       gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
-                          GTK_SIGNAL_FUNC(edit_address_cancel), cancelled);
+       // Confirm deletion
+       if( obj->type == ADDR_ITEM_FOLDER ) {
+               message = g_strdup_printf( _(
+                               "Do you want to delete the folder AND all addresses in `%s' ? \n" \
+                               "If deleting the folder only, addresses will be moved into parent folder." ),
+                               obj->name );
+               aval = alertpanel( _("Delete"), message, _("Folder only"), _("Folder and Addresses"), _("Cancel") );
+               g_free(message);
+               if( aval == G_ALERTOTHER ) return;
+       }
+       else {
+               message = g_strdup_printf(_("Really delete `%s' ?"), obj->name);
+               aval = alertpanel(_("Delete"), message, _("Yes"), _("No"), NULL);
+               g_free(message);
+               if (aval != G_ALERTDEFAULT) return;
+       }
 
-       gtk_widget_show_all(vbox);
+       // Proceed with deletion
+       if( obj->type == ADDR_DATASOURCE ) {
+               // Remove data source.
+               if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
+                       addressbook_free_child_adapters( node );
+                       remFlag = TRUE;
+               }
+       }
+       else {
+               abf = addressbook_get_book_file();
+               if( abf == NULL ) return;
+       }
+
+       if( obj->type == ADDR_ITEM_FOLDER ) {
+               AdapterFolder *adapter = ADAPTER_FOLDER(obj);
+               ItemFolder *item = adapter->itemFolder;
+               if( aval == G_ALERTDEFAULT ) {
+                       // Remove folder only
+                       item = addrbook_remove_folder( abf, item );
+                       if( item ) {
+                               addritem_free_item_folder( item );
+                               addressbook_move_nodes_up( ctree, node );
+                               remFlag = TRUE;
+                       }
+               }
+               else if( aval == G_ALERTALTERNATE ) {
+                       // Remove folder and addresses
+                       item = addrbook_remove_folder_delete( abf, item );
+                       if( item ) {
+                               addritem_free_item_folder( item );
+                               addressbook_free_child_adapters( node );
+                               remFlag = TRUE;
+                       }
+               }
+       }
+       else if( obj->type == ADDR_ITEM_GROUP ) {
+               AdapterGroup *adapter = ADAPTER_GROUP(obj);
+               ItemGroup *item = adapter->itemGroup;
 
-       addredit.window     = window;
-       addredit.name_entry = name_entry;
-       addredit.addr_entry = addr_entry;
-       addredit.rem_entry  = rem_entry;
-       addredit.ok_btn     = ok_btn;
-       addredit.cancel_btn = cancel_btn;
-}
+               item = addrbook_remove_group( abf, item );
+               if( item ) {
+                       addritem_free_item_group( item );
+                       remFlag = TRUE;
+               }
+       }
 
-static void edit_address_ok(GtkWidget *widget, gboolean *cancelled)
-{
-       *cancelled = FALSE;
-       gtk_main_quit();
+       if( remFlag ) {
+               // Free up adapter and remove node.
+               addressbook_free_adapter( node );
+               gtk_ctree_remove_node(ctree, node );
+       }
 }
 
-static void edit_address_cancel(GtkWidget *widget, gboolean *cancelled)
-{
-       *cancelled = TRUE;
-       gtk_main_quit();
-}
+static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
+       AddressObject *pobj = NULL;
+       AddressDataSource *ds = NULL;
+       AddressBookFile *abf = NULL;
 
-static gint edit_address_delete_event(GtkWidget *widget, GdkEventAny *event,
-                                     gboolean *cancelled)
-{
-       *cancelled = TRUE;
-       gtk_main_quit();
+       pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
+       if( pobj == NULL ) return;
+       ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
+       if( ds == NULL ) return;
 
-       return TRUE;
-}
+       abf = ds->rawDataSource;
+       if( abf == NULL ) return;
 
-static void edit_address_key_pressed(GtkWidget *widget, GdkEventKey *event,
-                                    gboolean *cancelled)
-{
-       if (event && event->keyval == GDK_Escape) {
-               *cancelled = TRUE;
-               gtk_main_quit();
+       if( pobj->type == ADDR_DATASOURCE ) {
+               if( ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ) {
+                       // New address
+                       ItemPerson *person = addressbook_edit_person( abf, NULL, NULL, FALSE );
+                       if( person ) {
+                               if( addrbook.treeSelected == addrbook.opened ) {
+                                       gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
+                               }
+                       }
+               }
+       }
+       else if( pobj->type == ADDR_ITEM_FOLDER ) {
+               // New address
+               ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
+               ItemPerson *person = addressbook_edit_person( abf, folder, NULL, FALSE );
+               if( person ) {
+                       if (addrbook.treeSelected == addrbook.opened) {
+                               gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
+                       }
+               }
+       }
+       else if( pobj->type == ADDR_ITEM_GROUP ) {
+               // New address in group
+               ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
+               if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
+               if (addrbook.treeSelected == addrbook.opened) {
+                       // Change node name in tree.
+                       addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
+                       gtk_ctree_select( GTK_CTREE(addrbook.ctree), addrbook.opened );
+               }
        }
 }
 
-static AddressItem *addressbook_edit_address(AddressItem *item)
-{
-       static gboolean cancelled;
-       const gchar *str;
-
-       if (!addredit.window)
-               addressbook_edit_address_create(&cancelled);
-       gtk_widget_grab_focus(addredit.ok_btn);
-       gtk_widget_grab_focus(addredit.name_entry);
-       gtk_widget_show(addredit.window);
-       manage_window_set_transient(GTK_WINDOW(addredit.window));
-
-       gtk_entry_set_text(GTK_ENTRY(addredit.name_entry), "");
-       gtk_entry_set_text(GTK_ENTRY(addredit.addr_entry), "");
-       gtk_entry_set_text(GTK_ENTRY(addredit.rem_entry),  "");
+/*
+* Search for specified group in address index tree.
+*/
+static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
+       GtkCTreeNode *node = NULL;
+       GtkCTreeRow *currRow;
 
-       if (item) {
-               if (item->name)
-                       gtk_entry_set_text(GTK_ENTRY(addredit.name_entry),
-                                          item->name);
-               if (item->address)
-                       gtk_entry_set_text(GTK_ENTRY(addredit.addr_entry),
-                                          item->address);
-               if (item->remarks)
-                       gtk_entry_set_text(GTK_ENTRY(addredit.rem_entry),
-                                          item->remarks);
+       currRow = GTK_CTREE_ROW( parent );
+       if( currRow ) {
+               node = currRow->children;
+               while( node ) {
+                       AddressObject *obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
+                       if( obj->type == ADDR_ITEM_GROUP ) {
+                               ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
+                               if( g == group ) return node;
+                       }
+                       currRow = GTK_CTREE_ROW(node);
+                       node = currRow->sibling;
+               }
        }
+       return NULL;
+}
 
-       gtk_main();
-       gtk_widget_hide(addredit.window);
-       if (cancelled == TRUE) return NULL;
+static AddressBookFile *addressbook_get_book_file() {
+       AddressBookFile *abf = NULL;
+       AddressDataSource *ds = NULL;
+       ds = addressbook_find_datasource( addrbook.treeSelected );
+       if( ds == NULL ) return;
+       if( ds->type == ADDR_IF_BOOK ) abf = ds->rawDataSource;
+       return abf;
+}
 
-       str = gtk_entry_get_text(GTK_ENTRY(addredit.name_entry));
-       if (*str == '\0') return NULL;
+static void addressbook_tree_remove_children( GtkCTree *ctree, GtkCTreeNode *parent ) {
+       GtkCTreeNode *node;
+       GtkCTreeRow *row;
 
-       if (!item) {
-               item = mgu_create_address();
-               ADDRESS_OBJECT_TYPE(item) = ADDR_ITEM;
+       // Remove existing folders and groups
+       row = GTK_CTREE_ROW( parent );
+       if( row ) {
+               while( node = row->children ) {
+                       gtk_ctree_remove_node( ctree, node );
+               }
        }
-
-       g_free(item->name);
-       item->name = g_strdup(str);
-
-       str = gtk_entry_get_text(GTK_ENTRY(addredit.addr_entry));
-       g_free(item->address);
-       if (*str == '\0')
-               item->address = NULL;
-       else
-               item->address = g_strdup(str);
-
-       str = gtk_entry_get_text(GTK_ENTRY(addredit.rem_entry));
-       g_free(item->remarks);
-       if (*str == '\0')
-               item->remarks = NULL;
-       else
-               item->remarks = g_strdup(str);
-
-       return item;
 }
 
-static void addressbook_new_address_cb(gpointer data, guint action,
-                                      GtkWidget *widget)
-{
-       AddressItem *item;
-
-       item = addressbook_edit_address(NULL);
-
-       if (item) {
-               addressbook_add_object(addrbook.selected,
-                                      ADDRESS_OBJECT(item));
-               if (addrbook.selected == addrbook.opened) {
-                       addrbook.open_folder = TRUE;
-                       gtk_ctree_select(GTK_CTREE(addrbook.ctree),
-                                        addrbook.opened);
+static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
+       GtkCTreeNode *parent, *child;
+       GtkCTreeRow *currRow;
+       currRow = GTK_CTREE_ROW( node );
+       if( currRow ) {
+               parent = currRow->parent;
+               while( child = currRow->children ) {
+                       gtk_ctree_move( ctree, child, parent, node );
                }
+               gtk_ctree_sort_node( ctree, parent );
        }
 }
 
-static void addressbook_edit_address_cb(gpointer data, guint action,
-                                       GtkWidget *widget)
-{
-       GtkCList *clist = GTK_CLIST(addrbook.clist);
+static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget *widget ) {
+       GtkCTree *clist = GTK_CTREE(addrbook.clist);
        GtkCTree *ctree;
-       AddressObject *obj, *pobj;
+       AddressObject *obj = NULL, *pobj = NULL;
+       AddressDataSource *ds = NULL;
        GtkCTreeNode *node = NULL, *parentNode = NULL;
-       gchar *nodeName;
-
-       if (!clist->selection) {
-               addressbook_edit_folder_cb(NULL, 0, NULL);
-               return;
-       }
+       gchar *name = NULL;
+       AddressBookFile *abf = NULL;
 
-       obj = gtk_clist_get_row_data(clist, GPOINTER_TO_INT(clist->selection->data));
+       if( addrbook.listSelected == NULL ) return;
+       obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
        g_return_if_fail(obj != NULL);
 
-       pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.selected);
-
-       if (obj->type == ADDR_ITEM) {
-               AddressItem *item = ADDRESS_ITEM(obj);
-
-               if( pobj ) {
-                       // Prevent edit of readonly items
-                       if(     pobj->type == ADDR_VCARD ||
-                               pobj->type == ADDR_JPILOT ||
-                               pobj->type == ADDR_CATEGORY ||
-                               pobj->type == ADDR_LDAP ) return;
+               ctree = GTK_CTREE( addrbook.ctree );
+       pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
+       node = gtk_ctree_find_by_row_data( ctree, addrbook.treeSelected, obj );
+
+       ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
+       if( ds == NULL ) return;
+
+       abf = addressbook_get_book_file();
+       if( abf == NULL ) return;
+       if( obj->type == ADDR_ITEM_EMAIL ) {
+               ItemEMail *email = ( ItemEMail * ) obj;
+               ItemPerson *person;
+               if( email == NULL ) return;
+               if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
+                       // Edit parent group
+                       AdapterGroup *adapter = ADAPTER_GROUP(pobj);
+                       ItemGroup *itemGrp = adapter->itemGroup;
+                       if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
+                       name = ADDRITEM_NAME(itemGrp);
+                       node = addrbook.treeSelected;
+                       parentNode = GTK_CTREE_ROW(node)->parent;
+               }
+               else {
+                       // Edit person - email page
+                       person = ( ItemPerson * ) ADDRITEM_PARENT(email);
+                       if( addressbook_edit_person( abf, NULL, person, TRUE ) == NULL ) return;
+                       gtk_ctree_select( ctree, addrbook.opened );
+                       return;
                }
-
-               if (addressbook_edit_address(item) == NULL) return;
-
-               addrbook.open_folder = TRUE;
-               gtk_ctree_select(GTK_CTREE(addrbook.ctree), addrbook.opened);
-               return;
-       }
-       else if (obj->type == ADDR_GROUP) {
-               addressbook_edit_group(NULL);
-               return;
-       }
-       else if( obj->type == ADDR_VCARD ) {
-               AddressVCard *vcard = ADDRESS_VCARD(obj);
-               if( addressbook_edit_vcard( vcard ) == NULL ) return;
-               nodeName = vcard->name;
-               parentNode = addrbook.vcard;
        }
-#ifdef USE_JPILOT
-       else if( obj->type == ADDR_JPILOT ) {
-               AddressJPilot *jpilot = ADDRESS_JPILOT(obj);
-               if( addressbook_edit_jpilot( jpilot ) == NULL ) return;
-               nodeName = jpilot->name;
-               parentNode = addrbook.jpilot;
+       else if( obj->type == ADDR_ITEM_PERSON ) {
+               // Edit person - basic page
+               ItemPerson *person = ( ItemPerson * ) obj;
+               if( addressbook_edit_person( abf, NULL, person, FALSE ) == NULL ) return;
+               gtk_ctree_select( ctree, addrbook.opened);
+               return;
        }
-#endif
-#ifdef USE_LDAP
-               else if( obj->type == ADDR_LDAP ) {
-               AddressLDAP *ldapi = ADDRESS_LDAP(obj);
-               if( addressbook_edit_ldap( ldapi ) == NULL ) return;
-               nodeName = ldapi->name;
-               parentNode = addrbook.ldap;
+       else if( obj->type == ADDR_ITEM_GROUP ) {
+               ItemGroup *itemGrp = ( ItemGroup * ) obj;
+               if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
+               parentNode = addrbook.treeSelected;
+               node = addressbook_find_group_node( parentNode, itemGrp );
+               name = ADDRITEM_NAME(itemGrp);
        }
-#endif
        else {
                return;
        }
 
        // Update tree node with node name
-               ctree = GTK_CTREE( addrbook.ctree );
-       node = gtk_ctree_find_by_row_data( ctree, addrbook.selected, obj );
-       if( ! node ) return;
-       addressbook_change_node_name( node, nodeName );
-       gtk_ctree_sort_node(ctree, parentNode );
-       addrbook.open_folder = TRUE;
+       if( node == NULL ) return;
+       addressbook_change_node_name( node, name );
+       gtk_ctree_sort_node( ctree, parentNode );
        gtk_ctree_select( ctree, addrbook.opened );
 }
 
@@ -2027,707 +1904,828 @@ static void close_cb(gpointer data, guint action, GtkWidget *widget)
        addressbook_close();
 }
 
-static void addressbook_set_clist(AddressObject *obj)
-{
-       GtkCList *clist = GTK_CLIST(addrbook.clist);
-       GList *items;
-       gchar *text[N_COLS];
+static void addressbook_file_save_cb( gpointer data, guint action, GtkWidget *widget ) {
+       addressbook_export_to_file();
+}
 
-       if (!obj) {
-               gtk_clist_clear(clist);
-               return;
+static void addressbook_person_expand_node( GtkCTree *ctree, GList *node, gpointer *data ) {
+       if( node ) {
+               ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
+               if( person ) addritem_person_set_opened( person, TRUE );
        }
+}
 
-       gtk_clist_freeze(clist);
-       gtk_clist_clear(clist);
-
-       if (obj->type == ADDR_GROUP)
-               items = ADDRESS_GROUP(obj)->items;
-       else if (obj->type == ADDR_FOLDER) {
-               items = ADDRESS_FOLDER(obj)->items;
-       }
-       else if (obj->type == ADDR_VCARD) {
-               items = ADDRESS_VCARD(obj)->items;
-       }
-#ifdef USE_JPILOT
-       else if (obj->type == ADDR_JPILOT) {
-               items = ADDRESS_JPILOT(obj)->items;
-       }
-       else if (obj->type == ADDR_CATEGORY) {
-               items = ADDRESS_CATEGORY(obj)->items;
+static void addressbook_person_collapse_node( GtkCTree *ctree, GList *node, gpointer *data ) {
+       if( node ) {
+               ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
+               if( person ) addritem_person_set_opened( person, FALSE );
        }
-#endif
-#ifdef USE_LDAP
-       else if (obj->type == ADDR_LDAP) {
-               items = ADDRESS_LDAP(obj)->items;
+}
+
+static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
+       gchar *str = NULL;
+       gchar *eMailAlias = ADDRITEM_NAME(email);
+       if( eMailAlias && *eMailAlias != '\0' ) {
+               if( person ) {
+                       str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
+               }
+               else {
+                       str = g_strdup( eMailAlias );
+               }
        }
-#endif
-       else {
-               gtk_clist_thaw(clist);
-               return;
+       return str;
+}
+
+static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
+       GList *items = itemGroup->listEMail;
+       AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
+       for( ; items != NULL; items = g_list_next( items ) ) {
+               GtkCTreeNode *nodeEMail = NULL;
+               gchar *text[N_COLS];
+               ItemEMail *email = items->data;
+               ItemPerson *person;
+               gchar *str = NULL;
+
+               if( ! email ) continue;
+
+               person = ( ItemPerson * ) ADDRITEM_PARENT(email);
+               str = addressbook_format_item_clist( person, email );
+               if( str ) {
+                       text[COL_NAME] = str;
+               }
+               else {
+                       text[COL_NAME] = ADDRITEM_NAME(person);
+               }
+               text[COL_ADDRESS] = email->address;
+               text[COL_REMARKS] = email->remarks;
+               nodeEMail = gtk_ctree_insert_node(
+                               clist, NULL, NULL,
+                               text, FOLDER_SPACING,
+                               atci->iconXpm, atci->maskXpm,
+                               atci->iconXpmOpen, atci->maskXpmOpen,
+                               FALSE, FALSE );
+               gtk_ctree_node_set_row_data( clist, nodeEMail, email );
+               g_free( str );
+               str = NULL;
        }
+}
 
-       for (; items != NULL; items = items->next) {
-               AddressObject *iobj;
+static void addressbook_folder_load_person( GtkCTree *clist, ItemFolder *itemFolder ) {
+       GList *items;
+       AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
+       AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
+
+       if( atci == NULL ) return;
+       if( atciMail == NULL ) return;
+
+       // Load email addresses
+       items = addritem_folder_get_person_list( itemFolder );
+       for( ; items != NULL; items = g_list_next( items ) ) {
+               GtkCTreeNode *nodePerson = NULL;
+               GtkCTreeNode *nodeEMail = NULL;
+               gchar *text[N_COLS];
+               gboolean flgFirst = TRUE, haveAddr = FALSE;
                gint row;
-               iobj = ADDRESS_OBJECT(items->data);
-               if( iobj == NULL ) continue;
+               ItemPerson *person;
+               GList *node;
 
-               if (iobj->type == ADDR_GROUP) {
-                       AddressGroup *group;
+               person = ( ItemPerson * ) items->data;
+               if( person == NULL ) continue;
 
-                       group = ADDRESS_GROUP(iobj);
-                       text[COL_NAME]    = group->name;
-                       text[COL_ADDRESS] = NULL;
-                       text[COL_REMARKS] = NULL;
-                       row = gtk_clist_append(clist, text);
-                       gtk_clist_set_pixtext(clist, row, COL_NAME,
-                                             group->name, 4,
-                                             groupxpm, groupxpmmask);
-                       gtk_clist_set_row_data(clist, row, iobj);
-               } if (iobj->type == ADDR_VCARD) {
-                       AddressVCard *vcard;
-
-                       vcard = ADDRESS_VCARD(iobj);
-                       text[COL_NAME]    = vcard->name;
-                       text[COL_ADDRESS] = NULL;
-                       text[COL_REMARKS] = NULL;
-                       row = gtk_clist_append(clist, text);
-                       gtk_clist_set_pixtext(clist, row, COL_NAME,
-                                             vcard->name, 4,
-                                             vcardxpm, vcardxpmmask);
-                       gtk_clist_set_row_data(clist, row, iobj);
-#ifdef USE_JPILOT
-               } if (iobj->type == ADDR_JPILOT) {
-                       AddressJPilot *jpilot;
+               text[COL_NAME] = NULL;
+               node = person->listEMail;
+               while( node ) {
+                       ItemEMail *email = node->data;
+                       gchar *eMailAddr = NULL;
+                       node = g_list_next( node );
 
-                       jpilot = ADDRESS_JPILOT(iobj);
-                       text[COL_NAME]    = jpilot->name;
-                       text[COL_ADDRESS] = NULL;
-                       text[COL_REMARKS] = NULL;
-                       row = gtk_clist_append(clist, text);
-                       gtk_clist_set_pixtext(clist, row, COL_NAME,
-                                             jpilot->name, 4,
-                                             jpilotxpm, jpilotxpmmask);
-                       gtk_clist_set_row_data(clist, row, iobj);
-               } if (iobj->type == ADDR_CATEGORY) {
-                       AddressCategory *category;
-
-                       category = ADDRESS_CATEGORY(iobj);
-                       text[COL_NAME]    = category->name;
+                       text[COL_ADDRESS] = email->address;
+                       text[COL_REMARKS] = email->remarks;
+                       eMailAddr = ADDRITEM_NAME(email);
+                       if( *eMailAddr == '\0' ) eMailAddr = NULL;
+                       if( flgFirst ) {
+                               // First email belongs with person
+                               gchar *str = addressbook_format_item_clist( person, email );
+                               if( str ) {
+                                       text[COL_NAME] = str;
+                               }
+                               else {
+                                       text[COL_NAME] = ADDRITEM_NAME(person);
+                               }
+                               nodePerson = gtk_ctree_insert_node(
+                                               clist, NULL, NULL,
+                                               text, FOLDER_SPACING,
+                                               atci->iconXpm, atci->maskXpm,
+                                               atci->iconXpmOpen, atci->maskXpmOpen,
+                                               FALSE, person->isOpened );
+                               g_free( str );
+                               str = NULL;
+                               gtk_ctree_node_set_row_data(clist, nodePerson, person );
+                       }
+                       else {
+                               // Subsequent email is a child node of person
+                               text[COL_NAME] = ADDRITEM_NAME(email);
+                               nodeEMail = gtk_ctree_insert_node(
+                                               clist, nodePerson, NULL,
+                                               text, FOLDER_SPACING,
+                                               atciMail->iconXpm, atciMail->maskXpm,
+                                               atciMail->iconXpmOpen, atciMail->maskXpmOpen,
+                                               FALSE, TRUE );
+                               gtk_ctree_node_set_row_data(clist, nodeEMail, email );
+                       }
+                       flgFirst = FALSE;
+                       haveAddr = TRUE;
+               }
+               if( ! haveAddr ) {
+                       // Have name without EMail
+                       text[COL_NAME] = ADDRITEM_NAME(person);
                        text[COL_ADDRESS] = NULL;
                        text[COL_REMARKS] = NULL;
-                       row = gtk_clist_append(clist, text);
-                       gtk_clist_set_pixtext(clist, row, COL_NAME,
-                                             category->name, 4,
-                                             categoryxpm, categoryxpmmask);
-                       gtk_clist_set_row_data(clist, row, iobj);
-#endif
-#ifdef USE_LDAP
-               } if (iobj->type == ADDR_LDAP) {
-                       AddressLDAP *ldapi;
+                       nodePerson = gtk_ctree_insert_node(
+                                       clist, NULL, NULL,
+                                       text, FOLDER_SPACING,
+                                       atci->iconXpm, atci->maskXpm,
+                                       atci->iconXpmOpen, atci->maskXpmOpen,
+                                       FALSE, person->isOpened );
+                       gtk_ctree_node_set_row_data(clist, nodePerson, person );
+               }
+               gtk_ctree_sort_node(GTK_CTREE(clist), NULL);
+       }
+       // Free up the list
+       mgu_clear_list( items );
+       g_list_free( items );
+}
+
+static void addressbook_folder_load_group( GtkCTree *clist, ItemFolder *itemFolder ) {
+       GList *items;
+       AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
+
+       // Load any groups
+       if( ! atci ) return;
+       items = addritem_folder_get_group_list( itemFolder );
+       for( ; items != NULL; items = g_list_next( items ) ) {
+               GtkCTreeNode *nodeGroup = NULL;
+               gchar *text[N_COLS];
+               ItemGroup *group = items->data;
+               if( group == NULL ) continue;
+               text[COL_NAME] = ADDRITEM_NAME(group);
+               text[COL_ADDRESS] = NULL;
+               text[COL_REMARKS] = NULL;
+               nodeGroup = gtk_ctree_insert_node(clist, NULL, NULL,
+                                     text, FOLDER_SPACING,
+                                     atci->iconXpm, atci->maskXpm,
+                                     atci->iconXpmOpen, atci->maskXpmOpen,
+                                     FALSE, FALSE);
+               gtk_ctree_node_set_row_data(clist, nodeGroup, group );
+               gtk_ctree_sort_node(clist, NULL);
+       }
+       // Free up the list
+       mgu_clear_list( items );
+       g_list_free( items );
+}
+
+/*
+ * Load data sources into list.
+ */
+static void addressbook_node_load_datasource( GtkCTree *clist, AddressObject *obj ) {
+       AdapterInterface *adapter;
+       AddressInterface *iface;
+       AddressTypeControlItem *atci = NULL;
+       AddressDataSource *ds;
+       GtkCTreeNode *newNode, *node;
+       GtkCTreeRow *row;
+       GtkCell *cell = NULL;
+       gchar *text[N_COLS];
+
+       adapter = ADAPTER_INTERFACE(obj);
+       if( adapter == NULL ) return;
+       iface = adapter->interface;
+       atci = adapter->atci;
+       if( atci == NULL ) return;
 
-                       ldapi = ADDRESS_LDAP(iobj);
-                       text[COL_NAME]    = ldapi->name;
+       // Create nodes in list copying values for data sources in tree
+       row = GTK_CTREE_ROW( adapter->treeNode );
+       if( row ) {
+               node = row->children;
+               while( node ) {
+                       gpointer data = gtk_ctree_node_get_row_data( clist, node );
+                       row = GTK_CTREE_ROW( node );
+                       cell = ( ( GtkCListRow * )row )->cell;
+                       text[COL_NAME] = cell->u.text;
                        text[COL_ADDRESS] = NULL;
                        text[COL_REMARKS] = NULL;
-                       row = gtk_clist_append(clist, text);
-                       gtk_clist_set_pixtext(clist, row, COL_NAME,
-                                             ldapi->name, 4,
-                                             ldapxpm, ldapxpmmask);
-                       gtk_clist_set_row_data(clist, row, iobj);
-#endif
-               } else if (iobj->type == ADDR_ITEM) {
-                       AddressItem *item;
+                       newNode = gtk_ctree_insert_node( clist, NULL, NULL,
+                                     text, FOLDER_SPACING,
+                                     atci->iconXpm, atci->maskXpm,
+                                     atci->iconXpmOpen, atci->maskXpmOpen,
+                                     FALSE, FALSE);
+                       gtk_ctree_node_set_row_data( clist, newNode, data );
+                       node = row->sibling;
 
-                       item = ADDRESS_ITEM(iobj);
-                       text[COL_NAME]    = item->name;
-                       text[COL_ADDRESS] = item->address;
-                       text[COL_REMARKS] = item->remarks;
-                       row = gtk_clist_append(clist, text);
-                       gtk_clist_set_row_data(clist, row, iobj);
                }
        }
-
-       gtk_clist_sort(clist);
-       gtk_clist_thaw(clist);
+       gtk_ctree_sort_node( clist, NULL );
 }
 
-static void addressbook_read_file(void)
-{
-       XMLFile *file;
-       gchar *path;
-
-       debug_print(_("Reading addressbook file..."));
-
-       path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ADDRESS_BOOK, NULL);
-       if ((file = xml_open_file(path)) == NULL) {
-               debug_print(_("%s doesn't exist.\n"), path);
-               g_free(path);
-               addressbook_get_tree(NULL, addrbook.common, ADDRESS_TAG_COMMON);
-               addressbook_get_tree(NULL, addrbook.personal, ADDRESS_TAG_PERSONAL);
-               addressbook_get_tree(NULL, addrbook.vcard, ADDRESS_TAG_VCARD);
-#ifdef USE_JPILOT
-               addressbook_get_tree(NULL, addrbook.jpilot, ADDRESS_TAG_JPILOT);
-#endif
-#ifdef USE_LDAP
-               addressbook_get_tree(NULL, addrbook.ldap, ADDRESS_TAG_LDAP);
-#endif
-               return;
+static AddressDataSource *addressbook_find_datasource( GtkCTreeNode *node ) {
+       AddressDataSource *ds = NULL;
+       AddressObject *ao;
+
+       g_return_if_fail(addrbook.ctree != NULL);
+
+       while( node ) {
+               if( GTK_CTREE_ROW(node)->level < 2 ) return NULL;
+               ao = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
+               if( ao ) {
+//                     printf( "ao->type = %d\n", ao->type );
+                       if( ao->type == ADDR_DATASOURCE ) {
+                               AdapterDSource *ads = ADAPTER_DSOURCE(ao);
+//                             printf( "found it\n" );
+                               ds = ads->dataSource;
+                               break;
+                       }
+               }
+               node = GTK_CTREE_ROW(node)->parent;
        }
-       g_free(path);
+       return ds;
+}
 
-       xml_get_dtd(file);
+/*
+* Load address list widget with children of specified object.
+* Enter: obj   Parent object to be loaded.
+*/
+static void addressbook_set_clist( AddressObject *obj ) {
+       GtkCTree *ctreelist = GTK_CTREE(addrbook.clist);
+       GtkCList *clist = GTK_CLIST(addrbook.clist);
+       AddressDataSource *ds = NULL;
+       AdapterDSource *ads = NULL;
 
-       if (xml_parse_next_tag(file) < 0 ||
-           xml_compare_tag(file, "addressbook") == FALSE) {
-               g_warning("Invalid addressbook data\n");
-               xml_close_file(file);
+       if( obj == NULL ) {
+               gtk_clist_clear(clist);
                return;
        }
 
-       addressbook_get_tree(file, addrbook.common, ADDRESS_TAG_COMMON);
-       addressbook_get_tree(file, addrbook.personal, ADDRESS_TAG_PERSONAL);
-       addressbook_get_tree(file, addrbook.vcard, ADDRESS_TAG_VCARD);
-#ifdef USE_JPILOT
-       addressbook_get_tree(file, addrbook.jpilot, ADDRESS_TAG_JPILOT);
-#endif
-#ifdef USE_LDAP
-       addressbook_get_tree(file, addrbook.ldap, ADDRESS_TAG_LDAP);
-#endif
-
-       xml_close_file(file);
-
-       debug_print(_("done.\n"));
-}
-
-static void addressbook_get_tree(XMLFile *file, GtkCTreeNode *node,
-                                const gchar *folder_tag)
-{
-       AddressFolder *folder;
-
-       g_return_if_fail(node != NULL);
+       if( obj->type == ADDR_INTERFACE ) {
+               // printf( "set_clist: loading datasource...\n" );
+               // addressbook_node_load_datasource( clist, obj );
+               return;
+       }
 
-       folder = g_new(AddressFolder, 1);
-       ADDRESS_OBJECT(folder)->type = ADDR_FOLDER;
-       folder->name = g_strdup(folder_tag);
-       folder->items = NULL;
-       gtk_ctree_node_set_row_data(GTK_CTREE(addrbook.ctree), node, folder);
+       gtk_clist_freeze(clist);
+       gtk_clist_clear(clist);
 
-       if (file) {
-               if (xml_parse_next_tag(file) < 0 ||
-                   xml_compare_tag(file, folder_tag) == FALSE) {
-                       g_warning("Invalid addressbook data\n");
-                       return;
+       if( obj->type == ADDR_DATASOURCE ) {
+               ads = ADAPTER_DSOURCE(obj);
+               ds = ADAPTER_DSOURCE(obj)->dataSource;
+               if( ds ) {
+                       // Load root folder
+                       ItemFolder *rootFolder = NULL;
+                       rootFolder = addrindex_ds_get_root_folder( ds );
+                       addressbook_folder_load_person( ctreelist, addrindex_ds_get_root_folder( ds ) );
+                       addressbook_folder_load_group( ctreelist, addrindex_ds_get_root_folder( ds ) );
+               }
+       }
+       else {
+               if( obj->type == ADDR_ITEM_GROUP ) {
+                       // Load groups
+                       ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
+                       addressbook_load_group( ctreelist, itemGroup );
+               }
+               else if( obj->type == ADDR_ITEM_FOLDER ) {
+                       // Load folders
+                       ItemFolder *itemFolder = ADAPTER_FOLDER(obj)->itemFolder;
+                       addressbook_folder_load_person( ctreelist, itemFolder );
+                       addressbook_folder_load_group( ctreelist, itemFolder );
                }
        }
 
-       if (file) addressbook_add_objs(file, node);
+       gtk_clist_sort(clist);
+       gtk_clist_thaw(clist);
 }
 
-static void addressbook_add_objs(XMLFile *file, GtkCTreeNode *node)
-{
-       GList *attr;
-       guint prev_level;
-       GtkCTreeNode *new_node;
-
-       for (;;) {
-               prev_level = file->level;
-               if (xml_parse_next_tag(file) < 0) return;
-               if (file->level < prev_level) return;
-
-               if (xml_compare_tag(file, "group")) {
-                       AddressGroup *group;
-
-                       group = g_new(AddressGroup, 1);
-                       ADDRESS_OBJECT_TYPE(group) = ADDR_GROUP;
-                       attr = xml_get_current_tag_attr(file);
-                       if (attr)
-                               group->name = g_strdup(((XMLAttr *)attr->data)->value);
-                       else
-                               group->name = NULL;
-                       group->items = NULL;
-
-                       new_node = addressbook_add_object
-                               (node, ADDRESS_OBJECT(group));
-
-                       addressbook_add_objs(file, new_node);
-               } else if (xml_compare_tag(file, "folder")) {
-                       AddressFolder *folder;
-
-                       folder = g_new(AddressFolder, 1);
-                       ADDRESS_OBJECT_TYPE(folder) = ADDR_FOLDER;
-                       attr = xml_get_current_tag_attr(file);
-                       if (attr)
-                               folder->name = g_strdup(((XMLAttr *)attr->data)->value);
-                       else
-                               folder->name = NULL;
-                       folder->items = NULL;
-
-                       new_node = addressbook_add_object
-                               (node, ADDRESS_OBJECT(folder));
-
-                       addressbook_add_objs(file, new_node);
-               }
-               else if( xml_compare_tag( file, "vcard" ) ) {
-                       AddressVCard *vcard;
-                       vcard = addressbook_parse_vcard( file );
-                       if( ! vcard ) return;
-                       new_node = addressbook_add_object
-                               (node, ADDRESS_OBJECT(vcard));
+/*
+* Free adaptor for specified node.
+*/
+static void addressbook_free_adapter( GtkCTreeNode *node ) {
+       AddressObject *ao;
+
+       g_return_if_fail(addrbook.ctree != NULL);
+
+       if( node ) {
+               if( GTK_CTREE_ROW(node)->level < 2 ) return;
+               ao = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
+               if( ao == NULL ) return;
+               if( ao->type == ADDR_INTERFACE ) {
+                       AdapterInterface *ai = ADAPTER_INTERFACE(ao);
+                       addrbookctl_free_interface( ai );
                }
-#ifdef USE_JPILOT
-               else if( xml_compare_tag( file, "jpilot" ) ) {
-                       AddressJPilot *jpilot;
-                       jpilot = addressbook_parse_jpilot( file );
-                       if( ! jpilot ) return;
-                       new_node = addressbook_add_object
-                               (node, ADDRESS_OBJECT(jpilot));
+               else if( ao->type == ADDR_DATASOURCE ) {
+                       AdapterDSource *ads = ADAPTER_DSOURCE(ao);
+                       addrbookctl_free_datasource( ads );
                }
-#endif
-#ifdef USE_LDAP
-               else if( xml_compare_tag( file, "server" ) ) {
-                       AddressLDAP *ldapi;
-                       ldapi = addressbook_parse_ldap( file );
-                       if( ! ldapi ) return;
-                       new_node = addressbook_add_object
-                               (node, ADDRESS_OBJECT(ldapi));
+               else if( ao->type == ADDR_ITEM_FOLDER ) {
+                       AdapterFolder *af = ADAPTER_FOLDER(ao);
+                       addrbookctl_free_folder( af );
                }
-#endif
-               else if (xml_compare_tag(file, "item")) {
-                       AddressItem *item;
-
-                       item = addressbook_parse_item(file);
-                       if (!item) return;
-                       new_node = addressbook_add_object
-                               (node, ADDRESS_OBJECT(item));
-               } else {
-                       g_warning("Invalid tag\n");
-                       return;
+               else if( ao->type == ADDR_ITEM_GROUP ) {
+                       AdapterGroup *ag = ADAPTER_GROUP(ao);
+                       addrbookctl_free_group( ag );
                }
+               gtk_ctree_node_set_row_data( GTK_CTREE(addrbook.ctree), node, NULL );
        }
 }
 
-static GtkCTreeNode *addressbook_add_object(GtkCTreeNode *node,
-                                           AddressObject *obj)
-{
-       GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
-       GtkCTreeNode *added;
-       AddressObject *pobj;
-
-       g_return_val_if_fail(node != NULL, NULL);
-       g_return_val_if_fail(obj  != NULL, NULL);
-
-       pobj = gtk_ctree_node_get_row_data(ctree, node);
-       g_return_val_if_fail(pobj != NULL, NULL);
+/*
+* Free all children adapters.
+*/
+static void addressbook_free_child_adapters( GtkCTreeNode *node ) {
+       GtkCTreeNode *parent, *child;
+       GtkCTreeRow *currRow;
 
-       if (pobj->type == ADDR_ITEM) {
-               g_warning("Parent object mustn't be an item.\n");
-               return NULL;
+       if( node == NULL ) return;
+       currRow = GTK_CTREE_ROW( node );
+       if( currRow ) {
+               parent = currRow->parent;
+               child = currRow->children;
+               while( child ) {
+                       addressbook_free_child_adapters( child );
+                       addressbook_free_adapter( child );
+                       currRow = GTK_CTREE_ROW( child );
+                       child = currRow->sibling;
+               }
        }
-       if (pobj->type == ADDR_FOLDER &&
-           (obj->type == ADDR_GROUP || obj->type == ADDR_FOLDER))
-               gtk_ctree_expand(ctree, node);
-
-       if (pobj->type == ADDR_FOLDER && obj->type == ADDR_VCARD )
-               gtk_ctree_expand(ctree, node);
+}
 
-&nbs