2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Match Grun
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * General functions for accessing address index file.
35 #include "addrcache.h"
37 #include "addrindex.h"
40 #ifndef DEV_STANDALONE
55 #define TAG_ADDRESS_INDEX "addressbook"
57 #define TAG_IF_ADDRESS_BOOK "book_list"
58 #define TAG_IF_VCARD "vcard_list"
59 #define TAG_IF_JPILOT "jpilot_list"
60 #define TAG_IF_LDAP "ldap_list"
62 #define TAG_DS_ADDRESS_BOOK "book"
63 #define TAG_DS_VCARD "vcard"
64 #define TAG_DS_JPILOT "jpilot"
65 #define TAG_DS_LDAP "server"
67 // XML Attribute names
68 #define ATTAG_BOOK_NAME "name"
69 #define ATTAG_BOOK_FILE "file"
71 #define ATTAG_VCARD_NAME "name"
72 #define ATTAG_VCARD_FILE "file"
74 #define ATTAG_JPILOT_NAME "name"
75 #define ATTAG_JPILOT_FILE "file"
76 #define ATTAG_JPILOT_CUSTOM_1 "custom-1"
77 #define ATTAG_JPILOT_CUSTOM_2 "custom-2"
78 #define ATTAG_JPILOT_CUSTOM_3 "custom-3"
79 #define ATTAG_JPILOT_CUSTOM_4 "custom-4"
80 #define ATTAG_JPILOT_CUSTOM "custom-"
82 #define ATTAG_LDAP_NAME "name"
83 #define ATTAG_LDAP_HOST "host"
84 #define ATTAG_LDAP_PORT "port"
85 #define ATTAG_LDAP_BASE_DN "base-dn"
86 #define ATTAG_LDAP_BIND_DN "bind-dn"
87 #define ATTAG_LDAP_BIND_PASS "bind-pass"
88 #define ATTAG_LDAP_CRITERIA "criteria"
89 #define ATTAG_LDAP_MAX_ENTRY "max-entry"
90 #define ATTAG_LDAP_TIMEOUT "timeout"
92 #define DISP_NEW_COMMON "Common Address"
93 #define DISP_NEW_PERSONAL "Personal Address"
96 #define TAG_IF_OLD_COMMON "common_address"
97 #define TAG_IF_OLD_PERSONAL "personal_address"
99 #define DISP_OLD_COMMON "Common Address"
100 #define DISP_OLD_PERSONAL "Personal Address"
102 typedef struct _AddressIfAttr AddressIfAttrib;
103 struct _AddressIfAttr {
109 * Build interface with default values.
111 static AddressInterface *addrindex_create_interface( gint type, gchar *name, gchar *tagIf, gchar *tagDS ) {
112 AddressInterface *iface = g_new0( AddressInterface, 1 );
113 ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
114 ADDRITEM_ID(iface) = NULL;
115 ADDRITEM_NAME(iface) = g_strdup( name );
116 ADDRITEM_PARENT(iface) = NULL;
117 ADDRITEM_SUBTYPE(iface) = type;
119 iface->name = g_strdup( name );
120 iface->listTag = g_strdup( tagIf );
121 iface->itemTag = g_strdup( tagDS );
122 iface->legacyFlag = FALSE;
123 iface->haveLibrary = TRUE;
124 iface->useInterface = TRUE;
125 iface->readOnly = TRUE;
126 iface->getAccessFlag = NULL;
127 iface->getModifyFlag = NULL;
128 iface->getReadFlag = NULL;
129 iface->getStatusCode = NULL;
130 iface->getReadData = NULL;
131 iface->getRootFolder = NULL;
132 iface->getListFolder = NULL;
133 iface->getListPerson = NULL;
134 iface->getAllPersons = NULL;
135 iface->getName = NULL;
136 iface->listSource = NULL;
141 * Build table of interfaces.
143 static void addrindex_build_if_list( AddressIndex *addrIndex ) {
144 AddressInterface *iface;
146 iface = addrindex_create_interface( ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK, TAG_DS_ADDRESS_BOOK );
147 iface->readOnly = FALSE;
148 iface->getModifyFlag = ( void * ) addrbook_get_modified;
149 iface->getAccessFlag = ( void * ) addrbook_get_accessed;
150 iface->getReadFlag = ( void * ) addrbook_get_read_flag;
151 iface->getStatusCode = ( void * ) addrbook_get_status;
152 iface->getReadData = ( void * ) addrbook_read_data;
153 iface->getRootFolder = ( void * ) addrbook_get_root_folder;
154 iface->getListFolder = ( void * ) addrbook_get_list_folder;
155 iface->getListPerson = ( void * ) addrbook_get_list_person;
156 iface->getAllPersons = ( void * ) addrbook_get_all_persons;
157 iface->getName = ( void * ) addrbook_get_name;
158 iface->setAccessFlag = ( void * ) addrbook_set_accessed;
159 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
160 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
162 iface = addrindex_create_interface( ADDR_IF_VCARD, "V-Card", TAG_IF_VCARD, TAG_DS_VCARD );
163 iface->getModifyFlag = ( void * ) vcard_get_modified;
164 iface->getAccessFlag = ( void * ) vcard_get_accessed;
165 iface->getReadFlag = ( void * ) vcard_get_read_flag;
166 iface->getStatusCode = ( void * ) vcard_get_status;
167 iface->getReadData = ( void * ) vcard_read_data;
168 iface->getRootFolder = ( void * ) vcard_get_root_folder;
169 iface->getListFolder = ( void * ) vcard_get_list_folder;
170 iface->getListPerson = ( void * ) vcard_get_list_person;
171 iface->getAllPersons = ( void * ) vcard_get_all_persons;
172 iface->getName = ( void * ) vcard_get_name;
173 iface->setAccessFlag = ( void * ) vcard_set_accessed;
174 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
175 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
177 iface = addrindex_create_interface( ADDR_IF_JPILOT, "J-Pilot", TAG_IF_JPILOT, TAG_DS_JPILOT );
179 iface->haveLibrary = jpilot_test_pilot_lib();
180 iface->useInterface = iface->haveLibrary;
181 iface->getModifyFlag = ( void * ) jpilot_get_modified;
182 iface->getAccessFlag = ( void * ) jpilot_get_accessed;
183 iface->getReadFlag = ( void * ) jpilot_get_read_flag;
184 iface->getStatusCode = ( void * ) jpilot_get_status;
185 iface->getReadData = ( void * ) jpilot_read_data;
186 iface->getRootFolder = ( void * ) jpilot_get_root_folder;
187 iface->getListFolder = ( void * ) jpilot_get_list_folder;
188 iface->getListPerson = ( void * ) jpilot_get_list_person;
189 iface->getAllPersons = ( void * ) jpilot_get_all_persons;
190 iface->getName = ( void * ) jpilot_get_name;
191 iface->setAccessFlag = ( void * ) jpilot_set_accessed;
193 iface->useInterface = FALSE;
194 iface->haveLibrary = FALSE;
196 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
197 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
199 iface = addrindex_create_interface( ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
201 iface->haveLibrary = syldap_test_ldap_lib();
202 iface->useInterface = iface->haveLibrary;
203 iface->getAccessFlag = ( void * ) syldap_get_accessed;
204 // iface->getModifyFlag = ( void * ) syldap_get_modified;
205 // iface->getReadFlag = ( void * ) syldap_get_read_flag;
206 iface->getStatusCode = ( void * ) syldap_get_status;
207 iface->getReadData = ( void * ) syldap_read_data;
208 iface->getRootFolder = ( void * ) syldap_get_root_folder;
209 iface->getListFolder = ( void * ) syldap_get_list_folder;
210 iface->getListPerson = ( void * ) syldap_get_list_person;
211 iface->getName = ( void * ) syldap_get_name;
212 iface->setAccessFlag = ( void * ) syldap_set_accessed;
214 iface->useInterface = FALSE;
215 iface->haveLibrary = FALSE;
217 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
218 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
220 // Two old legacy data sources
221 iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - common", TAG_IF_OLD_COMMON, NULL );
222 iface->legacyFlag = TRUE;
223 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
224 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
226 iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - personal", TAG_IF_OLD_PERSONAL, NULL );
227 iface->legacyFlag = TRUE;
228 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
229 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
234 * Free name-value pairs.
236 static void addrindex_free_attributes( GList *list ) {
239 AddressIfAttrib *nv = node->data;
240 g_free( nv->name ); nv->name = NULL;
241 g_free( nv->value ); nv->value = NULL;
244 node = g_list_next( node );
250 * Free up data source.
252 void addrindex_free_datasource( AddressIndex *addrIndex, AddressDataSource *ds ) {
253 AddressInterface *iface = NULL;
254 g_return_if_fail( addrIndex != NULL );
255 g_return_if_fail( ds != NULL );
257 if( ds->interface == NULL ) {
258 iface = addrindex_get_interface( addrIndex, ds->type );
260 if( iface == NULL ) return;
262 if( iface->useInterface ) {
263 if( iface->type == ADDR_IF_BOOK ) {
264 AddressBookFile *abf = ds->rawDataSource;
266 addrbook_free_book( abf );
269 else if( iface->type == ADDR_IF_VCARD ) {
270 VCardFile *vcf = ds->rawDataSource;
276 else if( iface->type == ADDR_IF_JPILOT ) {
277 JPilotFile *jpf = ds->rawDataSource;
284 else if( iface->type == ADDR_IF_LDAP ) {
285 SyldapServer *server = ds->rawDataSource;
287 syldap_free( server );
293 GList *list = ds->rawDataSource;
294 addrindex_free_attributes( list );
297 g_free( ADDRITEM_ID(addrIndex) );
298 g_free( ADDRITEM_NAME(addrIndex) );
300 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
301 ADDRITEM_ID(addrIndex) = NULL;
302 ADDRITEM_NAME(addrIndex) = NULL;
303 ADDRITEM_PARENT(addrIndex) = NULL;
304 ADDRITEM_SUBTYPE(addrIndex) = 0;
305 ds->type = ADDR_IF_NONE;
306 ds->rawDataSource = NULL;
307 ds->interface = NULL;
309 ds->type = ADDR_IF_NONE;
310 ds->rawDataSource = NULL;
311 ds->interface = NULL;
315 static void addrindex_free_all_datasources( AddressInterface *iface ) {
316 GList *node = iface->listSource;
318 AddressDataSource *ds = node->data;
319 if( iface->useInterface ) {
320 if( iface->type == ADDR_IF_BOOK ) {
321 AddressBookFile *abf = ds->rawDataSource;
323 addrbook_free_book( abf );
326 else if( iface->type == ADDR_IF_VCARD ) {
327 VCardFile *vcf = ds->rawDataSource;
333 else if( iface->type == ADDR_IF_JPILOT ) {
334 JPilotFile *jpf = ds->rawDataSource;
341 else if( iface->type == ADDR_IF_LDAP ) {
342 SyldapServer *server = ds->rawDataSource;
344 syldap_free( server );
350 GList *list = ds->rawDataSource;
351 addrindex_free_attributes( list );
354 ds->type = ADDR_IF_NONE;
355 ds->rawDataSource = NULL;
356 ds->interface = NULL;
359 node = g_list_next( node );
363 static void addrindex_free_interface( AddressInterface *iface ) {
364 addrindex_free_all_datasources( iface );
366 g_free( ADDRITEM_ID(iface) );
367 g_free( ADDRITEM_NAME(iface) );
368 g_free( iface->name );
369 g_free( iface->listTag );
370 g_free( iface->itemTag );
372 ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
373 ADDRITEM_ID(iface) = NULL;
374 ADDRITEM_NAME(iface) = NULL;
375 ADDRITEM_PARENT(iface) = NULL;
376 ADDRITEM_SUBTYPE(iface) = 0;
377 iface->type = ADDR_IF_NONE;
379 iface->listTag = NULL;
380 iface->itemTag = NULL;
381 iface->legacyFlag = FALSE;
382 iface->useInterface = FALSE;
383 iface->haveLibrary = FALSE;
385 g_list_free( iface->listSource );
386 iface->listSource = NULL;
392 AddressIndex *addrindex_create_index() {
393 AddressIndex *addrIndex = g_new0( AddressIndex, 1 );
395 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_INDEX;
396 ADDRITEM_ID(addrIndex) = NULL;
397 ADDRITEM_NAME(addrIndex) = g_strdup( "Address Index" );
398 ADDRITEM_PARENT(addrIndex) = NULL;
399 ADDRITEM_SUBTYPE(addrIndex) = 0;
400 addrIndex->filePath = NULL;
401 addrIndex->fileName = NULL;
402 addrIndex->retVal = MGU_SUCCESS;
403 addrIndex->needsConversion = FALSE;
404 addrIndex->wasConverted = FALSE;
405 addrIndex->conversionError = FALSE;
406 addrIndex->interfaceList = NULL;
407 addrIndex->lastType = ADDR_IF_NONE;
408 addrIndex->dirtyFlag = FALSE;
409 addrindex_build_if_list( addrIndex );
414 * Specify file to be used.
416 void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
417 g_return_if_fail( addrIndex != NULL );
418 addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
420 void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
421 g_return_if_fail( addrIndex != NULL );
422 addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
424 void addrindex_set_dirty( AddressIndex *addrIndex, const gboolean value ) {
425 g_return_if_fail( addrIndex != NULL );
426 addrIndex->dirtyFlag = value;
430 * Return list of interfaces.
432 GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
433 g_return_val_if_fail( addrIndex != NULL, NULL );
434 return addrIndex->interfaceList;
440 void addrindex_free_index( AddressIndex *addrIndex ) {
443 g_return_if_fail( addrIndex != NULL );
445 g_free( ADDRITEM_ID(addrIndex) );
446 g_free( ADDRITEM_NAME(addrIndex) );
447 g_free( addrIndex->filePath );
448 g_free( addrIndex->fileName );
449 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
450 ADDRITEM_ID(addrIndex) = NULL;
451 ADDRITEM_NAME(addrIndex) = NULL;
452 ADDRITEM_PARENT(addrIndex) = NULL;
453 ADDRITEM_SUBTYPE(addrIndex) = 0;
454 addrIndex->filePath = NULL;
455 addrIndex->fileName = NULL;
456 addrIndex->retVal = MGU_SUCCESS;
457 addrIndex->needsConversion = FALSE;
458 addrIndex->wasConverted = FALSE;
459 addrIndex->conversionError = FALSE;
460 addrIndex->lastType = ADDR_IF_NONE;
461 addrIndex->dirtyFlag = FALSE;
462 node = addrIndex->interfaceList;
464 AddressInterface *iface = node->data;
465 addrindex_free_interface( iface );
466 node = g_list_next( node );
468 g_list_free( addrIndex->interfaceList );
469 addrIndex->interfaceList = NULL;
474 * Print address index.
476 void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
477 g_return_if_fail( addrIndex != NULL );
478 fprintf( stream, "AddressIndex:\n" );
479 fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
480 fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
481 fprintf( stream, "\t status: %d : '%s'\n", addrIndex->retVal, mgu_error2string( addrIndex->retVal ) );
482 fprintf( stream, "\tconverted: '%s'\n", addrIndex->wasConverted ? "yes" : "no" );
483 fprintf( stream, "\tcvt error: '%s'\n", addrIndex->conversionError ? "yes" : "no" );
484 fprintf( stream, "\t---\n" );
488 * Retrieve specified interface from index.
490 AddressInterface *addrindex_get_interface( AddressIndex *addrIndex, AddressIfType ifType ) {
491 AddressInterface *retVal = NULL;
494 g_return_val_if_fail( addrIndex != NULL, NULL );
496 node = addrIndex->interfaceList;
498 AddressInterface *iface = node->data;
499 node = g_list_next( node );
500 if( iface->type == ifType ) {
508 AddressDataSource *addrindex_create_datasource() {
509 AddressDataSource *ds = NULL;
510 ds = g_new0( AddressDataSource, 1 );
511 ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
512 ADDRITEM_ID(ds) = NULL;
513 ADDRITEM_NAME(ds) = NULL;
514 ADDRITEM_PARENT(ds) = NULL;
515 ADDRITEM_SUBTYPE(ds) = 0;
516 ds->type = ADDR_IF_NONE;
517 ds->rawDataSource = NULL;
518 ds->interface = NULL;
523 * Add data source to index.
524 * Enter: addrIndex Address index object.
525 * ifType Interface type to add.
526 * dataSource Actual data source to add.
527 * Return: TRUE if data source was added.
528 * Note: The raw data object (for example, AddressBookFile or VCardFile object) should be
529 * supplied as the dataSource argument.
531 AddressDataSource *addrindex_index_add_datasource( AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource ) {
532 AddressInterface *iface;
533 AddressDataSource *ds = NULL;
535 g_return_val_if_fail( addrIndex != NULL, NULL );
536 g_return_val_if_fail( dataSource != NULL, NULL );
538 iface = addrindex_get_interface( addrIndex, ifType );
540 ds = addrindex_create_datasource();
541 ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
543 ds->rawDataSource = dataSource;
544 ds->interface = iface;
545 iface->listSource = g_list_append( iface->listSource, ds );
546 addrIndex->dirtyFlag = TRUE;
552 * Remove data source from index.
553 * Enter: addrIndex Address index object.
554 * dataSource Data source to remove.
555 * Return: Data source if removed, or NULL if data source was not found in
556 * index. Note the this object must still be freed.
558 AddressDataSource *addrindex_index_remove_datasource( AddressIndex *addrIndex, AddressDataSource *dataSource ) {
559 AddressDataSource *retVal = FALSE;
560 AddressInterface *iface;
562 g_return_val_if_fail( addrIndex != NULL, NULL );
563 g_return_val_if_fail( dataSource != NULL, NULL );
565 iface = addrindex_get_interface( addrIndex, dataSource->type );
567 iface->listSource = g_list_remove( iface->listSource, dataSource );
568 addrIndex->dirtyFlag = TRUE;
569 dataSource->interface = NULL;
575 static AddressInterface *addrindex_tag_get_interface( AddressIndex *addrIndex, gchar *tag, AddressIfType ifType ) {
576 AddressInterface *retVal = NULL;
577 GList *node = addrIndex->interfaceList;
580 AddressInterface *iface = node->data;
581 node = g_list_next( node );
583 if( strcmp( iface->listTag, tag ) == 0 ) {
589 if( iface->type == ifType ) {
598 static AddressInterface *addrindex_tag_get_datasource( AddressIndex *addrIndex, AddressIfType ifType, gchar *tag ) {
599 AddressInterface *retVal = NULL;
600 GList *node = addrIndex->interfaceList;
603 AddressInterface *iface = node->data;
604 node = g_list_next( node );
605 if( iface->type == ifType && iface->itemTag ) {
606 if( strcmp( iface->itemTag, tag ) == 0 ) {
615 /* **********************************************************************
616 * Interface XML parsing functions.
617 * ***********************************************************************
619 static void show_attribs( GList *attr ) {
621 gchar *name = ((XMLAttr *)attr->data)->name;
622 gchar *value = ((XMLAttr *)attr->data)->value;
623 printf( "\tattr value : %s :%s:\n", name, value );
624 attr = g_list_next( attr );
629 static void addrindex_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
631 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
636 static void addrindex_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
638 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
644 static void addrindex_write_attr( FILE *fp, gchar *name, gchar *value ) {
648 xml_file_put_escape_str( fp, value );
653 * Return list of name-value pairs.
655 static GList *addrindex_read_attributes( XMLFile *file ) {
662 attr = xml_get_current_tag_attr( file );
664 name = ((XMLAttr *)attr->data)->name;
665 value = ((XMLAttr *)attr->data)->value;
666 nv = g_new0( AddressIfAttrib, 1 );
667 nv->name = g_strdup( name );
668 nv->value = g_strdup( value );
669 list = g_list_append( list, nv );
670 attr = g_list_next( attr );
676 * Output name-value pairs.
678 static void addrindex_write_attributes( FILE *fp, gchar *tag, GList *list, gint lvl ) {
682 addrindex_write_elem_s( fp, lvl, tag );
686 addrindex_write_attr( fp, nv->name, nv->value );
687 node = g_list_next( node );
693 static void addrindex_print_attributes( GList *list, FILE *stream ) {
696 AddressIfAttrib *nv = node->data;
697 fprintf( stream, "%s : %s\n", nv->name, nv->value );
698 node = g_list_next( node );
702 static AddressDataSource *addrindex_parse_book( XMLFile *file ) {
703 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
704 AddressBookFile *abf;
707 abf = addrbook_create_book();
708 attr = xml_get_current_tag_attr( file );
710 gchar *name = ((XMLAttr *)attr->data)->name;
711 gchar *value = ((XMLAttr *)attr->data)->value;
712 if( strcmp( name, ATTAG_BOOK_NAME ) == 0 ) {
713 addrbook_set_name( abf, value );
715 else if( strcmp( name, ATTAG_BOOK_FILE ) == 0) {
716 addrbook_set_file( abf, value );
718 attr = g_list_next( attr );
720 ds->rawDataSource = abf;
724 static void addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
725 AddressBookFile *abf = ds->rawDataSource;
727 addrindex_write_elem_s( fp, lvl, TAG_DS_ADDRESS_BOOK );
728 addrindex_write_attr( fp, ATTAG_BOOK_NAME, abf->name );
729 addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName );
730 fputs( " />\n", fp );
734 static AddressDataSource *addrindex_parse_vcard( XMLFile *file ) {
735 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
739 vcf = vcard_create();
740 attr = xml_get_current_tag_attr( file );
742 gchar *name = ((XMLAttr *)attr->data)->name;
743 gchar *value = ((XMLAttr *)attr->data)->value;
744 if( strcmp( name, ATTAG_VCARD_NAME ) == 0 ) {
745 vcard_set_name( vcf, value );
747 else if( strcmp( name, ATTAG_VCARD_FILE ) == 0) {
748 vcard_set_file( vcf, value );
750 attr = g_list_next( attr );
752 ds->rawDataSource = vcf;
756 static void addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
757 VCardFile *vcf = ds->rawDataSource;
759 addrindex_write_elem_s( fp, lvl, TAG_DS_VCARD );
760 addrindex_write_attr( fp, ATTAG_VCARD_NAME, vcf->name );
761 addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path );
762 fputs( " />\n", fp );
767 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
768 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
772 jpf = jpilot_create();
773 attr = xml_get_current_tag_attr( file );
775 gchar *name = ((XMLAttr *)attr->data)->name;
776 gchar *value = ((XMLAttr *)attr->data)->value;
777 if( strcmp( name, ATTAG_JPILOT_NAME ) == 0 ) {
778 jpilot_set_name( jpf, value );
780 else if( strcmp( name, ATTAG_JPILOT_FILE ) == 0 ) {
781 jpilot_set_file( jpf, value );
783 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_1 ) == 0 ) {
784 jpilot_add_custom_label( jpf, value );
786 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_2 ) == 0 ) {
787 jpilot_add_custom_label( jpf, value );
789 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_3 ) == 0 ) {
790 jpilot_add_custom_label( jpf, value );
792 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_4 ) == 0 ) {
793 jpilot_add_custom_label( jpf, value );
795 attr = g_list_next( attr );
797 ds->rawDataSource = jpf;
801 static void addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
802 JPilotFile *jpf = ds->rawDataSource;
806 GList *customLbl = jpilot_get_custom_labels( jpf );
807 addrindex_write_elem_s( fp, lvl, TAG_DS_JPILOT );
808 addrindex_write_attr( fp, ATTAG_JPILOT_NAME, jpf->name );
809 addrindex_write_attr( fp, ATTAG_JPILOT_FILE, jpf->path );
814 sprintf( name, "%s%d", ATTAG_JPILOT_CUSTOM, ind );
815 addrindex_write_attr( fp, name, node->data );
817 node = g_list_next( node );
819 fputs( " />\n", fp );
823 // Just read/write name-value pairs
824 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
825 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
826 GList *list = addrindex_read_attributes( file );
827 ds->rawDataSource = list;
831 static void addrindex_write_jpilot( FILE *fp, AddressDataSource *ds, gint lvl ) {
832 GList *list = ds->rawDataSource;
834 addrindex_write_attributes( fp, TAG_DS_JPILOT, list, lvl );
840 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
841 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
842 SyldapServer *server;
845 server = syldap_create();
846 attr = xml_get_current_tag_attr( file );
848 gchar *name = ((XMLAttr *)attr->data)->name;
849 gchar *value = ((XMLAttr *)attr->data)->value;
850 gint ivalue = atoi( value );
851 if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
852 syldap_set_name( server, value );
854 else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
855 syldap_set_host( server, value );
857 else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
858 syldap_set_port( server, ivalue );
860 else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
861 syldap_set_base_dn( server, value );
863 else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
864 syldap_set_bind_dn( server, value );
866 else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
867 syldap_set_bind_password( server, value );
869 else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
870 syldap_set_search_criteria( server, value );
872 else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
873 syldap_set_max_entries( server, ivalue );
875 else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
876 syldap_set_timeout( server, ivalue );
878 attr = g_list_next( attr );
881 ds->rawDataSource = server;
885 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
886 SyldapServer *server = ds->rawDataSource;
890 addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP );
891 addrindex_write_attr( fp, ATTAG_LDAP_NAME, server->name );
892 addrindex_write_attr( fp, ATTAG_LDAP_HOST, server->hostName );
894 sprintf( value, "%d", server->port );
895 addrindex_write_attr( fp, ATTAG_LDAP_PORT, value );
897 addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, server->baseDN );
898 addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, server->bindDN );
899 addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, server->bindPass );
900 addrindex_write_attr( fp, ATTAG_LDAP_CRITERIA, server->searchCriteria );
902 sprintf( value, "%d", server->maxEntries );
903 addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value );
904 sprintf( value, "%d", server->timeOut );
905 addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value );
911 // Just read/write name-value pairs
912 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
913 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
914 GList *list = addrindex_read_attributes( file );
915 ds->rawDataSource = list;
919 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
920 GList *list = ds->rawDataSource;
922 addrindex_write_attributes( fp, TAG_DS_LDAP, list, lvl );
927 /* **********************************************************************
928 * Address index I/O functions.
929 * ***********************************************************************
931 static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
936 AddressInterface *iface = NULL, *dsIFace = NULL;
937 AddressDataSource *ds;
940 prev_level = file->level;
941 xml_parse_next_tag( file );
942 if( file->level < prev_level ) return;
944 xtag = xml_get_current_tag( file );
945 // printf( "tag : %s\n", xtag->tag );
947 iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
949 addrIndex->lastType = iface->type;
950 if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
951 // printf( "found : %s\n", iface->name );
954 dsIFace = addrindex_tag_get_datasource( addrIndex, addrIndex->lastType, xtag->tag );
956 // Add data source to list
957 // printf( "\tdata source: %s\n", dsIFace->name );
959 if( addrIndex->lastType == ADDR_IF_BOOK ) {
960 ds = addrindex_parse_book( file );
961 if( ds->rawDataSource ) {
962 addrbook_set_path( ds->rawDataSource, addrIndex->filePath );
963 // addrbook_print_book( ds->rawDataSource, stdout );
966 else if( addrIndex->lastType == ADDR_IF_VCARD ) {
967 ds = addrindex_parse_vcard( file );
968 // if( ds->rawDataSource ) {
969 // vcard_print_file( ds->rawDataSource, stdout );
972 else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
973 ds = addrindex_parse_jpilot( file );
975 if( ds->rawDataSource ) {
976 jpilot_print_file( ds->rawDataSource, stdout );
977 // addrindex_print_attributes( ds->rawDataSource, stdout );
981 else if( addrIndex->lastType == ADDR_IF_LDAP ) {
982 ds = addrindex_parse_ldap( file );
984 if( ds->rawDataSource ) {
985 syldap_print_data( ds->rawDataSource, stdout );
986 // addrindex_print_attributes( ds->rawDataSource, stdout );
991 ds->type = addrIndex->lastType;
992 ds->interface = dsIFace;
993 dsIFace->listSource = g_list_append( dsIFace->listSource, ds );
995 // printf( "=============================\n\n" );
999 element = xml_get_element( file );
1000 attr = xml_get_current_tag_attr( file );
1001 if( _interfaceLast_ && ! _interfaceLast_->legacyFlag ) {
1002 show_attribs( attr );
1003 printf( "\ttag value : %s :\n", element );
1006 addrindex_read_index( addrIndex, file );
1010 static gint addrindex_read_file( AddressIndex *addrIndex ) {
1011 XMLFile *file = NULL;
1012 gchar *fileSpec = NULL;
1014 g_return_val_if_fail( addrIndex != NULL, -1 );
1016 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1017 addrIndex->retVal = MGU_NO_FILE;
1018 file = xml_open_file( fileSpec );
1021 if( file == NULL ) {
1022 // fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName );
1023 return addrIndex->retVal;
1026 addrIndex->retVal = MGU_BAD_FORMAT;
1027 if( xml_get_dtd( file ) == 0 ) {
1028 if( xml_parse_next_tag( file ) == 0 ) {
1029 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1030 addrindex_read_index( addrIndex, file );
1031 addrIndex->retVal = MGU_SUCCESS;
1035 xml_close_file( file );
1037 return addrIndex->retVal;
1040 static void addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1041 GList *nodeIF, *nodeDS;
1043 gint lvlItem = 1 + lvlList;
1045 nodeIF = addrIndex->interfaceList;
1047 AddressInterface *iface = nodeIF->data;
1048 if( ! iface->legacyFlag ) {
1049 nodeDS = iface->listSource;
1050 addrindex_write_elem_s( fp, lvlList, iface->listTag );
1053 AddressDataSource *ds = nodeDS->data;
1055 if( iface->type == ADDR_IF_BOOK ) {
1056 addrindex_write_book( fp, ds, lvlItem );
1058 if( iface->type == ADDR_IF_VCARD ) {
1059 addrindex_write_vcard( fp, ds, lvlItem );
1061 if( iface->type == ADDR_IF_JPILOT ) {
1062 addrindex_write_jpilot( fp, ds, lvlItem );
1064 if( iface->type == ADDR_IF_LDAP ) {
1065 addrindex_write_ldap( fp, ds, lvlItem );
1068 nodeDS = g_list_next( nodeDS );
1070 addrindex_write_elem_e( fp, lvlList, iface->listTag );
1072 nodeIF = g_list_next( nodeIF );
1077 * Write data to specified file.
1078 * Enter: addrIndex Address index object.
1079 * newFile New file name.
1080 * return: Status code, from addrIndex->retVal.
1081 * Note: File will be created in directory specified by addrIndex.
1083 gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1086 #ifndef DEV_STANDALONE
1090 g_return_val_if_fail( addrIndex != NULL, -1 );
1092 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1093 addrIndex->retVal = MGU_OPEN_FILE;
1094 #ifdef DEV_STANDALONE
1095 fp = fopen( fileSpec, "w" );
1098 fputs( "<?xml version=\"1.0\" ?>\n", fp );
1100 pfile = prefs_write_open( fileSpec );
1104 fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
1105 conv_get_current_charset_str() );
1107 addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
1110 addrindex_write_index( addrIndex, fp );
1111 addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
1113 addrIndex->retVal = MGU_SUCCESS;
1114 #ifdef DEV_STANDALONE
1117 if( prefs_write_close( pfile ) < 0 ) {
1118 addrIndex->retVal = MGU_ERROR_WRITE;
1124 return addrIndex->retVal;
1128 * Save address index data to original file.
1129 * return: Status code, from addrIndex->retVal.
1131 gint addrindex_save_data( AddressIndex *addrIndex ) {
1132 g_return_val_if_fail( addrIndex != NULL, -1 );
1134 addrIndex->retVal = MGU_NO_FILE;
1135 if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1136 if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1138 addrindex_write_to( addrIndex, addrIndex->fileName );
1139 if( addrIndex->retVal == MGU_SUCCESS ) {
1140 addrIndex->dirtyFlag = FALSE;
1142 return addrIndex->retVal;
1146 * Save all address book files which may have changed.
1147 * Return: Status code, set if there was a problem saving data.
1149 gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1150 gint retVal = MGU_SUCCESS;
1151 GList *nodeIf, *nodeDS;
1153 nodeIf = addrIndex->interfaceList;
1155 AddressInterface *iface = nodeIf->data;
1156 if( iface->type == ADDR_IF_BOOK ) {
1157 nodeDS = iface->listSource;
1159 AddressDataSource *ds = nodeDS->data;
1160 AddressBookFile *abf = ds->rawDataSource;
1161 if( abf->dirtyFlag ) {
1162 if( abf->readFlag ) {
1163 addrbook_save_data( abf );
1164 if( abf->retVal != MGU_SUCCESS ) {
1165 retVal = abf->retVal;
1169 nodeDS = g_list_next( nodeDS );
1173 nodeIf = g_list_next( nodeIf );
1179 /* **********************************************************************
1180 * Address book conversion to new format.
1181 * ***********************************************************************
1184 #define ELTAG_IF_OLD_FOLDER "folder"
1185 #define ELTAG_IF_OLD_GROUP "group"
1186 #define ELTAG_IF_OLD_ITEM "item"
1187 #define ELTAG_IF_OLD_NAME "name"
1188 #define ELTAG_IF_OLD_ADDRESS "address"
1189 #define ELTAG_IF_OLD_REMARKS "remarks"
1190 #define ATTAG_IF_OLD_NAME "name"
1192 #define TEMPNODE_ROOT 0
1193 #define TEMPNODE_FOLDER 1
1194 #define TEMPNODE_GROUP 2
1195 #define TEMPNODE_ADDRESS 3
1197 typedef struct _AddressCvt_Node AddressCvtNode;
1198 struct _AddressCvt_Node {
1207 * Parse current address item.
1209 static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1214 nn = g_new0( AddressCvtNode, 1 );
1215 nn->type = TEMPNODE_ADDRESS;
1218 level = file->level;
1221 xml_parse_next_tag(file);
1222 if (file->level < level) return nn;
1224 element = xml_get_element( file );
1225 if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1226 nn->name = g_strdup( element );
1228 if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1229 nn->address = g_strdup( element );
1231 if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1232 nn->remarks = g_strdup( element );
1234 xml_parse_next_tag(file);
1239 * Create a temporary node below specified node.
1241 static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1243 nn = g_new0( AddressCvtNode, 1 );
1245 nn->name = g_strdup( name );
1246 nn->remarks = g_strdup( rem );
1247 node->list = g_list_append( node->list, nn );
1252 * Process current temporary node.
1254 static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1257 AddressCvtNode *newNode = NULL;
1262 prev_level = file->level;
1263 xml_parse_next_tag( file );
1264 if (file->level < prev_level) return;
1268 if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1269 attr = xml_get_current_tag_attr(file);
1271 name = ((XMLAttr *)attr->data)->name;
1272 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1273 value = ((XMLAttr *)attr->data)->value;
1276 newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1277 addrindex_add_obj( file, newNode );
1280 else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1281 attr = xml_get_current_tag_attr(file);
1283 name = ((XMLAttr *)attr->data)->name;
1284 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1285 value = ((XMLAttr *)attr->data)->value;
1288 newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1289 addrindex_add_obj( file, newNode );
1291 else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
1292 newNode = addrindex_parse_item( file );
1293 node->list = g_list_append( node->list, newNode );
1296 // printf( "invalid: !!! \n" );
1297 attr = xml_get_current_tag_attr( file );
1303 * Consume all nodes below current tag.
1305 static void addrindex_consume_tree( XMLFile *file ) {
1312 prev_level = file->level;
1313 xml_parse_next_tag( file );
1314 if (file->level < prev_level) return;
1316 xtag = xml_get_current_tag( file );
1317 // printf( "tag : %s\n", xtag->tag );
1318 element = xml_get_element( file );
1319 attr = xml_get_current_tag_attr( file );
1320 // show_attribs( attr );
1321 // printf( "\ttag value : %s :\n", element );
1322 addrindex_consume_tree( file );
1327 * Print temporary tree.
1329 static void addrindex_print_node( AddressCvtNode *node, FILE *stream ) {
1332 fprintf( stream, "Node:\ttype :%d:\n", node->type );
1333 fprintf( stream, "\tname :%s:\n", node->name );
1334 fprintf( stream, "\taddr :%s:\n", node->address );
1335 fprintf( stream, "\trems :%s:\n", node->remarks );
1337 fprintf( stream, "\t--list----\n" );
1341 AddressCvtNode *lNode = list->data;
1342 list = g_list_next( list );
1343 addrindex_print_node( lNode, stream );
1345 fprintf( stream, "\t==list-%d==\n", node->type );
1349 * Free up temporary tree.
1351 static void addrindex_free_node( AddressCvtNode *node ) {
1352 GList *list = node->list;
1355 AddressCvtNode *lNode = list->data;
1356 list = g_list_next( list );
1357 addrindex_free_node( lNode );
1359 node->type = TEMPNODE_ROOT;
1360 g_free( node->name );
1361 g_free( node->address );
1362 g_free( node->remarks );
1363 g_list_free( node->list );
1368 * Process address book for specified node.
1370 static void addrindex_process_node(
1371 AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
1372 ItemGroup *parentGrp, ItemFolder *folderGrp )
1375 ItemFolder *itemFolder = NULL;
1376 ItemGroup *itemGParent = parentGrp;
1377 ItemFolder *itemGFolder = folderGrp;
1378 AddressCache *cache = abf->addressCache;
1380 if( node->type == TEMPNODE_ROOT ) {
1381 itemFolder = parent;
1383 else if( node->type == TEMPNODE_FOLDER ) {
1384 itemFolder = addritem_create_item_folder();
1385 addritem_folder_set_name( itemFolder, node->name );
1386 addrcache_id_folder( cache, itemFolder );
1387 addrcache_folder_add_folder( cache, parent, itemFolder );
1390 else if( node->type == TEMPNODE_GROUP ) {
1391 ItemGroup *itemGroup;
1394 // Create a folder for group
1395 fName = g_strdup_printf( "Cvt - %s", node->name );
1396 itemGFolder = addritem_create_item_folder();
1397 addritem_folder_set_name( itemGFolder, fName );
1398 addrcache_id_folder( cache, itemGFolder );
1399 addrcache_folder_add_folder( cache, parent, itemGFolder );
1402 // Add group into folder
1403 itemGroup = addritem_create_item_group();
1404 addritem_group_set_name( itemGroup, node->name );
1405 addrcache_id_group( cache, itemGroup );
1406 addrcache_folder_add_group( cache, itemGFolder, itemGroup );
1407 itemGParent = itemGroup;
1409 else if( node->type == TEMPNODE_ADDRESS ) {
1410 ItemPerson *itemPerson;
1411 ItemEMail *itemEMail;
1413 // Create person and email objects
1414 itemPerson = addritem_create_item_person();
1415 addritem_person_set_common_name( itemPerson, node->name );
1416 addrcache_id_person( cache, itemPerson );
1417 itemEMail = addritem_create_item_email();
1418 addritem_email_set_address( itemEMail, node->address );
1419 addritem_email_set_remarks( itemEMail, node->remarks );
1420 addrcache_id_email( cache, itemEMail );
1421 addrcache_person_add_email( cache, itemPerson, itemEMail );
1423 // Add person into appropriate folder
1425 addrcache_folder_add_person( cache, itemGFolder, itemPerson );
1428 addrcache_folder_add_person( cache, parent, itemPerson );
1431 // Add email address only into group
1433 addrcache_group_add_email( cache, parentGrp, itemEMail );
1439 AddressCvtNode *lNode = list->data;
1440 list = g_list_next( list );
1441 addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
1446 * Process address book to specified file number.
1448 static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
1449 gboolean retVal = FALSE;
1450 AddressBookFile *abf = NULL;
1451 AddressCvtNode *rootNode = NULL;
1452 gchar *newFile = NULL;
1453 GList *fileList = NULL;
1457 rootNode = g_new0( AddressCvtNode, 1 );
1458 rootNode->type = TEMPNODE_ROOT;
1459 rootNode->name = g_strdup( "root" );
1460 rootNode->list = NULL;
1461 addrindex_add_obj( file, rootNode );
1462 // addrindex_print_node( rootNode, stdout );
1464 // Create new address book
1465 abf = addrbook_create_book();
1466 addrbook_set_name( abf, displayName );
1467 addrbook_set_path( abf, addrIndex->filePath );
1469 // Determine next available file number
1470 fileList = addrbook_get_bookfile_list( abf );
1472 fileNum = 1 + abf->maxValue;
1474 g_list_free( fileList );
1477 newFile = addrbook_gen_new_file_name( fileNum );
1479 addrbook_set_file( abf, newFile );
1482 addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
1484 // addrbook_dump_book( abf, stdout );
1485 addrbook_save_data( abf );
1486 addrIndex->retVal = abf->retVal;
1487 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1489 addrbook_free_book( abf );
1491 addrindex_free_node( rootNode );
1494 // Create entries in address index
1496 abf = addrbook_create_book();
1497 addrbook_set_name( abf, displayName );
1498 addrbook_set_path( abf, addrIndex->filePath );
1499 addrbook_set_file( abf, newFile );
1500 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1507 * Process tree converting data.
1509 static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
1517 prev_level = file->level;
1518 xml_parse_next_tag( file );
1519 if (file->level < prev_level) return;
1521 xtag = xml_get_current_tag( file );
1522 // printf( "tag : %d : %s\n", prev_level, xtag->tag );
1523 if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
1524 if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
1525 addrIndex->needsConversion = FALSE;
1526 addrIndex->wasConverted = TRUE;
1531 if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
1532 if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
1533 addrIndex->needsConversion = FALSE;
1534 addrIndex->wasConverted = TRUE;
1539 element = xml_get_element( file );
1540 attr = xml_get_current_tag_attr( file );
1541 // show_attribs( attr );
1542 // printf( "\ttag value : %s :\n", element );
1543 addrindex_consume_tree( file );
1547 static gint addrindex_convert_data( AddressIndex *addrIndex ) {
1548 XMLFile *file = NULL;
1551 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1552 addrIndex->retVal = MGU_NO_FILE;
1553 file = xml_open_file( fileSpec );
1556 if( file == NULL ) {
1557 // fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName );
1558 return addrIndex->retVal;
1561 addrIndex->retVal = MGU_BAD_FORMAT;
1562 if( xml_get_dtd( file ) == 0 ) {
1563 if( xml_parse_next_tag( file ) == 0 ) {
1564 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1565 addrindex_convert_tree( addrIndex, file );
1569 xml_close_file( file );
1570 return addrIndex->retVal;
1574 * Create a new address book file.
1576 static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
1577 gboolean retVal = FALSE;
1578 AddressBookFile *abf = NULL;
1579 gchar *newFile = NULL;
1580 GList *fileList = NULL;
1583 // Create new address book
1584 abf = addrbook_create_book();
1585 addrbook_set_name( abf, displayName );
1586 addrbook_set_path( abf, addrIndex->filePath );
1588 // Determine next available file number
1589 fileList = addrbook_get_bookfile_list( abf );
1591 fileNum = 1 + abf->maxValue;
1593 g_list_free( fileList );
1596 newFile = addrbook_gen_new_file_name( fileNum );
1598 addrbook_set_file( abf, newFile );
1601 addrbook_save_data( abf );
1602 addrIndex->retVal = abf->retVal;
1603 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1604 addrbook_free_book( abf );
1607 // Create entries in address index
1609 abf = addrbook_create_book();
1610 addrbook_set_name( abf, displayName );
1611 addrbook_set_path( abf, addrIndex->filePath );
1612 addrbook_set_file( abf, newFile );
1613 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1620 * Read data for address index performing a conversion if necesary.
1621 * Enter: addrIndex Address index object.
1622 * return: Status code, from addrIndex->retVal.
1623 * Note: New address book files will be created in directory specified by
1624 * addrIndex. Three files will be created, for the following:
1625 * "Common addresses"
1626 * "Personal addresses"
1627 * "Gathered addresses" - a new address book.
1629 gint addrindex_read_data( AddressIndex *addrIndex ) {
1630 g_return_val_if_fail( addrIndex != NULL, -1 );
1632 addrIndex->conversionError = FALSE;
1633 addrindex_read_file( addrIndex );
1634 if( addrIndex->retVal == MGU_SUCCESS ) {
1635 if( addrIndex->needsConversion ) {
1636 if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
1637 addrIndex->conversionError = TRUE;
1640 addrIndex->conversionError = TRUE;
1643 addrIndex->dirtyFlag = TRUE;
1645 return addrIndex->retVal;
1649 * Create new address books for a new address index.
1650 * Enter: addrIndex Address index object.
1651 * return: Status code, from addrIndex->retVal.
1652 * Note: New address book files will be created in directory specified by
1653 * addrIndex. Three files will be created, for the following:
1654 * "Common addresses"
1655 * "Personal addresses"
1656 * "Gathered addresses" - a new address book.
1658 gint addrindex_create_new_books( AddressIndex *addrIndex ) {
1661 g_return_val_if_fail( addrIndex != NULL, -1 );
1663 flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
1665 flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
1666 addrIndex->dirtyFlag = TRUE;
1668 return addrIndex->retVal;
1671 /* **********************************************************************
1672 * New interface stuff.
1673 * ***********************************************************************
1677 * Return modified flag for specified data source.
1679 gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
1680 gboolean retVal = FALSE;
1681 AddressInterface *iface;
1683 if( ds == NULL ) return retVal;
1684 iface = ds->interface;
1685 if( iface == NULL ) return retVal;
1686 if( iface->getModifyFlag ) {
1687 retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
1693 * Return accessed flag for specified data source.
1695 gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
1696 gboolean retVal = FALSE;
1697 AddressInterface *iface;
1699 if( ds == NULL ) return retVal;
1700 iface = ds->interface;
1701 if( iface == NULL ) return retVal;
1702 if( iface->getAccessFlag ) {
1703 retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
1709 * Return data read flag for specified data source.
1711 gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
1712 gboolean retVal = TRUE;
1713 AddressInterface *iface;
1715 if( ds == NULL ) return retVal;
1716 iface = ds->interface;
1717 if( iface == NULL ) return retVal;
1718 if( iface->getReadFlag ) {
1719 retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
1725 * Return status code for specified data source.
1727 gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
1728 gint retVal = MGU_SUCCESS;
1729 AddressInterface *iface;
1731 if( ds == NULL ) return retVal;
1732 iface = ds->interface;
1733 if( iface == NULL ) return retVal;
1734 if( iface->getStatusCode ) {
1735 retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
1741 * Return data read flag for specified data source.
1743 gint addrindex_ds_read_data( AddressDataSource *ds ) {
1744 gint retVal = MGU_SUCCESS;
1745 AddressInterface *iface;
1747 if( ds == NULL ) return retVal;
1748 iface = ds->interface;
1749 if( iface == NULL ) return retVal;
1750 if( iface->getReadData ) {
1751 retVal = ( iface->getReadData ) ( ds->rawDataSource );
1757 * Return data read flag for specified data source.
1759 ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
1760 ItemFolder *retVal = NULL;
1761 AddressInterface *iface;
1763 if( ds == NULL ) return retVal;
1764 iface = ds->interface;
1765 if( iface == NULL ) return retVal;
1766 if( iface->getRootFolder ) {
1767 retVal = ( iface->getRootFolder ) ( ds->rawDataSource );
1773 * Return list of folders for specified data source.
1775 GList *addrindex_ds_get_list_folder( AddressDataSource *ds ) {
1776 GList *retVal = FALSE;
1777 AddressInterface *iface;
1779 if( ds == NULL ) return retVal;
1780 iface = ds->interface;
1781 if( iface == NULL ) return retVal;
1782 if( iface->getListFolder ) {
1783 retVal = ( iface->getListFolder ) ( ds->rawDataSource );
1789 * Return list of persons in root folder for specified data source.
1791 GList *addrindex_ds_get_list_person( AddressDataSource *ds ) {
1792 GList *retVal = FALSE;
1793 AddressInterface *iface;
1795 if( ds == NULL ) return retVal;
1796 iface = ds->interface;
1797 if( iface == NULL ) return retVal;
1798 if( iface->getListPerson ) {
1799 retVal = ( iface->getListPerson ) ( ds->rawDataSource );
1805 * Return name for specified data source.
1807 gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
1808 gchar *retVal = FALSE;
1809 AddressInterface *iface;
1811 if( ds == NULL ) return retVal;
1812 iface = ds->interface;
1813 if( iface == NULL ) return retVal;
1814 if( iface->getName ) {
1815 retVal = ( iface->getName ) ( ds->rawDataSource );
1821 * Set the access flag inside the data source.
1823 void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
1824 AddressInterface *iface;
1826 if( ds == NULL ) return;
1827 iface = ds->interface;
1828 if( iface == NULL ) return;
1829 if( iface->setAccessFlag ) {
1830 ( iface->setAccessFlag ) ( ds->rawDataSource, value );
1835 * Return read only flag for specified data source.
1837 gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
1838 AddressInterface *iface;
1839 if( ds == NULL ) return TRUE;
1840 iface = ds->interface;
1841 if( iface == NULL ) return TRUE;
1842 return iface->readOnly;
1846 * Return list of all persons for specified data source.
1848 GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
1849 GList *retVal = NULL;
1850 AddressInterface *iface;
1852 if( ds == NULL ) return retVal;
1853 iface = ds->interface;
1854 if( iface == NULL ) return retVal;
1855 if( iface->getAllPersons ) {
1856 retVal = ( iface->getAllPersons ) ( ds->rawDataSource );