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.
28 #include "addrcache.h"
30 #include "addrindex.h"
33 #ifndef DEV_STANDALONE
47 #define TAG_ADDRESS_INDEX "addressbook"
49 #define TAG_IF_ADDRESS_BOOK "book_list"
50 #define TAG_IF_VCARD "vcard_list"
51 #define TAG_IF_JPILOT "jpilot_list"
52 #define TAG_IF_LDAP "ldap_list"
54 #define TAG_DS_ADDRESS_BOOK "book"
55 #define TAG_DS_VCARD "vcard"
56 #define TAG_DS_JPILOT "jpilot"
57 #define TAG_DS_LDAP "server"
59 // XML Attribute names
60 #define ATTAG_BOOK_NAME "name"
61 #define ATTAG_BOOK_FILE "file"
63 #define ATTAG_VCARD_NAME "name"
64 #define ATTAG_VCARD_FILE "file"
66 #define ATTAG_JPILOT_NAME "name"
67 #define ATTAG_JPILOT_FILE "file"
68 #define ATTAG_JPILOT_CUSTOM_1 "custom-1"
69 #define ATTAG_JPILOT_CUSTOM_2 "custom-2"
70 #define ATTAG_JPILOT_CUSTOM_3 "custom-3"
71 #define ATTAG_JPILOT_CUSTOM_4 "custom-4"
72 #define ATTAG_JPILOT_CUSTOM "custom-"
74 #define ATTAG_LDAP_NAME "name"
75 #define ATTAG_LDAP_HOST "host"
76 #define ATTAG_LDAP_PORT "port"
77 #define ATTAG_LDAP_BASE_DN "base-dn"
78 #define ATTAG_LDAP_BIND_DN "bind-dn"
79 #define ATTAG_LDAP_BIND_PASS "bind-pass"
80 #define ATTAG_LDAP_CRITERIA "criteria"
81 #define ATTAG_LDAP_MAX_ENTRY "max-entry"
82 #define ATTAG_LDAP_TIMEOUT "timeout"
84 #define DISP_NEW_COMMON "Common Address"
85 #define DISP_NEW_PERSONAL "Personal Address"
88 #define TAG_IF_OLD_COMMON "common_address"
89 #define TAG_IF_OLD_PERSONAL "personal_address"
91 #define DISP_OLD_COMMON "Common Address"
92 #define DISP_OLD_PERSONAL "Personal Address"
94 typedef struct _AddressIfAttr AddressIfAttrib;
95 struct _AddressIfAttr {
101 * Build interface with default values.
103 static AddressInterface *addrindex_create_interface( gint type, gchar *name, gchar *tagIf, gchar *tagDS ) {
104 AddressInterface *iface = g_new0( AddressInterface, 1 );
105 ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
106 ADDRITEM_ID(iface) = NULL;
107 ADDRITEM_NAME(iface) = g_strdup( name );
108 ADDRITEM_PARENT(iface) = NULL;
109 ADDRITEM_SUBTYPE(iface) = type;
111 iface->name = g_strdup( name );
112 iface->listTag = g_strdup( tagIf );
113 iface->itemTag = g_strdup( tagDS );
114 iface->legacyFlag = FALSE;
115 iface->haveLibrary = TRUE;
116 iface->useInterface = TRUE;
117 iface->readOnly = TRUE;
118 iface->getAccessFlag = NULL;
119 iface->getModifyFlag = NULL;
120 iface->getReadFlag = NULL;
121 iface->getStatusCode = NULL;
122 iface->getReadData = NULL;
123 iface->getRootFolder = NULL;
124 iface->getListFolder = NULL;
125 iface->getListPerson = NULL;
126 iface->getAllPersons = NULL;
127 iface->getName = NULL;
128 iface->listSource = NULL;
133 * Build table of interfaces.
135 static void addrindex_build_if_list( AddressIndex *addrIndex ) {
136 AddressInterface *iface;
139 iface = addrindex_create_interface( ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK, TAG_DS_ADDRESS_BOOK );
140 iface->readOnly = FALSE;
141 iface->getModifyFlag = ( void * ) addrbook_get_modified;
142 iface->getAccessFlag = ( void * ) addrbook_get_accessed;
143 iface->getReadFlag = ( void * ) addrbook_get_read_flag;
144 iface->getStatusCode = ( void * ) addrbook_get_status;
145 iface->getReadData = ( void * ) addrbook_read_data;
146 iface->getRootFolder = ( void * ) addrbook_get_root_folder;
147 iface->getListFolder = ( void * ) addrbook_get_list_folder;
148 iface->getListPerson = ( void * ) addrbook_get_list_person;
149 iface->getAllPersons = ( void * ) addrbook_get_all_persons;
150 iface->getName = ( void * ) addrbook_get_name;
151 iface->setAccessFlag = ( void * ) addrbook_set_accessed;
152 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
153 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
155 iface = addrindex_create_interface( ADDR_IF_VCARD, "V-Card", TAG_IF_VCARD, TAG_DS_VCARD );
156 iface->getModifyFlag = ( void * ) vcard_get_modified;
157 iface->getAccessFlag = ( void * ) vcard_get_accessed;
158 iface->getReadFlag = ( void * ) vcard_get_read_flag;
159 iface->getStatusCode = ( void * ) vcard_get_status;
160 iface->getReadData = ( void * ) vcard_read_data;
161 iface->getRootFolder = ( void * ) vcard_get_root_folder;
162 iface->getListFolder = ( void * ) vcard_get_list_folder;
163 iface->getListPerson = ( void * ) vcard_get_list_person;
164 iface->getAllPersons = ( void * ) vcard_get_all_persons;
165 iface->getName = ( void * ) vcard_get_name;
166 iface->setAccessFlag = ( void * ) vcard_set_accessed;
167 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
168 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
170 iface = addrindex_create_interface( ADDR_IF_JPILOT, "J-Pilot", TAG_IF_JPILOT, TAG_DS_JPILOT );
172 iface->useInterface = TRUE;
173 iface->haveLibrary = jpilot_test_pilot_lib();
174 iface->getModifyFlag = ( void * ) jpilot_get_modified;
175 iface->getAccessFlag = ( void * ) jpilot_get_accessed;
176 iface->getReadFlag = ( void * ) jpilot_get_read_flag;
177 iface->getStatusCode = ( void * ) jpilot_get_status;
178 iface->getReadData = ( void * ) jpilot_read_data;
179 iface->getRootFolder = ( void * ) jpilot_get_root_folder;
180 iface->getListFolder = ( void * ) jpilot_get_list_folder;
181 iface->getListPerson = ( void * ) jpilot_get_list_person;
182 iface->getAllPersons = ( void * ) jpilot_get_all_persons;
183 iface->getName = ( void * ) jpilot_get_name;
184 iface->setAccessFlag = ( void * ) jpilot_set_accessed;
186 iface->useInterface = FALSE;
187 iface->haveLibrary = FALSE;
189 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
190 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
192 iface = addrindex_create_interface( ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
194 iface->haveLibrary = syldap_test_ldap_lib();
195 iface->getAccessFlag = ( void * ) syldap_get_accessed;
196 // iface->getModifyFlag = ( void * ) syldap_get_modified;
197 // iface->getReadFlag = ( void * ) syldap_get_read_flag;
198 iface->getStatusCode = ( void * ) syldap_get_status;
199 iface->getReadData = ( void * ) syldap_read_data;
200 iface->getRootFolder = ( void * ) syldap_get_root_folder;
201 iface->getListFolder = ( void * ) syldap_get_list_folder;
202 iface->getListPerson = ( void * ) syldap_get_list_person;
203 iface->getName = ( void * ) syldap_get_name;
204 iface->setAccessFlag = ( void * ) syldap_set_accessed;
206 iface->useInterface = FALSE;
207 iface->haveLibrary = FALSE;
209 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
210 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
212 // Two old legacy data sources
213 iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - common", TAG_IF_OLD_COMMON, NULL );
214 iface->legacyFlag = TRUE;
215 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
216 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
218 iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - personal", TAG_IF_OLD_PERSONAL, NULL );
219 iface->legacyFlag = TRUE;
220 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
221 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
226 * Free name-value pairs.
228 static void addrindex_free_attributes( GList *list ) {
231 AddressIfAttrib *nv = node->data;
232 g_free( nv->name ); nv->name = NULL;
233 g_free( nv->value ); nv->value = NULL;
236 node = g_list_next( node );
242 * Free up data source.
244 void addrindex_free_datasource( AddressIndex *addrIndex, AddressDataSource *ds ) {
245 AddressInterface *iface = NULL;
246 g_return_if_fail( addrIndex != NULL );
247 g_return_if_fail( ds != NULL );
249 if( ds->interface == NULL ) {
250 iface = addrindex_get_interface( addrIndex, ds->type );
252 if( iface == NULL ) return;
254 if( iface->useInterface ) {
255 if( iface->type == ADDR_IF_BOOK ) {
256 AddressBookFile *abf = ds->rawDataSource;
258 addrbook_free_book( abf );
261 else if( iface->type == ADDR_IF_VCARD ) {
262 VCardFile *vcf = ds->rawDataSource;
268 else if( iface->type == ADDR_IF_JPILOT ) {
269 JPilotFile *jpf = ds->rawDataSource;
276 else if( iface->type == ADDR_IF_LDAP ) {
277 SyldapServer *server = ds->rawDataSource;
279 syldap_free( server );
285 GList *list = ds->rawDataSource;
286 addrindex_free_attributes( list );
289 g_free( ADDRITEM_ID(addrIndex) );
290 g_free( ADDRITEM_NAME(addrIndex) );
292 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
293 ADDRITEM_ID(addrIndex) = NULL;
294 ADDRITEM_NAME(addrIndex) = NULL;
295 ADDRITEM_PARENT(addrIndex) = NULL;
296 ADDRITEM_SUBTYPE(addrIndex) = 0;
297 ds->type = ADDR_IF_NONE;
298 ds->rawDataSource = NULL;
299 ds->interface = NULL;
301 ds->type = ADDR_IF_NONE;
302 ds->rawDataSource = NULL;
303 ds->interface = NULL;
307 static void addrindex_free_all_datasources( AddressInterface *iface ) {
308 GList *node = iface->listSource;
310 AddressDataSource *ds = node->data;
311 if( iface->useInterface ) {
312 if( iface->type == ADDR_IF_BOOK ) {
313 AddressBookFile *abf = ds->rawDataSource;
315 addrbook_free_book( abf );
318 else if( iface->type == ADDR_IF_VCARD ) {
319 VCardFile *vcf = ds->rawDataSource;
325 else if( iface->type == ADDR_IF_JPILOT ) {
326 JPilotFile *jpf = ds->rawDataSource;
333 else if( iface->type == ADDR_IF_LDAP ) {
334 SyldapServer *server = ds->rawDataSource;
336 syldap_free( server );
342 GList *list = ds->rawDataSource;
343 addrindex_free_attributes( list );
346 ds->type = ADDR_IF_NONE;
347 ds->rawDataSource = NULL;
348 ds->interface = NULL;
351 node = g_list_next( node );
355 static addrindex_free_interface( AddressInterface *iface ) {
356 addrindex_free_all_datasources( iface );
358 g_free( ADDRITEM_ID(iface) );
359 g_free( ADDRITEM_NAME(iface) );
360 g_free( iface->name );
361 g_free( iface->listTag );
362 g_free( iface->itemTag );
364 ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
365 ADDRITEM_ID(iface) = NULL;
366 ADDRITEM_NAME(iface) = NULL;
367 ADDRITEM_PARENT(iface) = NULL;
368 ADDRITEM_SUBTYPE(iface) = 0;
369 iface->type = ADDR_IF_NONE;
371 iface->listTag = NULL;
372 iface->itemTag = NULL;
373 iface->legacyFlag = FALSE;
374 iface->useInterface = FALSE;
375 iface->haveLibrary = FALSE;
377 g_list_free( iface->listSource );
378 iface->listSource = NULL;
384 AddressIndex *addrindex_create_index() {
385 AddressIndex *addrIndex = g_new0( AddressIndex, 1 );
386 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_INDEX;
387 ADDRITEM_ID(addrIndex) = NULL;
388 ADDRITEM_NAME(addrIndex) = g_strdup( "Address Index" );
389 ADDRITEM_PARENT(addrIndex) = NULL;
390 ADDRITEM_SUBTYPE(addrIndex) = 0;
391 addrIndex->filePath = NULL;
392 addrIndex->fileName = NULL;
393 addrIndex->retVal = MGU_SUCCESS;
394 addrIndex->needsConversion = FALSE;
395 addrIndex->wasConverted = FALSE;
396 addrIndex->conversionError = FALSE;
397 addrIndex->interfaceList = NULL;
398 addrIndex->lastType = ADDR_IF_NONE;
399 addrIndex->dirtyFlag = FALSE;
400 addrindex_build_if_list( addrIndex );
405 * Specify file to be used.
407 void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
408 g_return_if_fail( addrIndex != NULL );
409 addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
411 void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
412 g_return_if_fail( addrIndex != NULL );
413 addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
415 void addrindex_set_dirty( AddressIndex *addrIndex, const gboolean value ) {
416 g_return_if_fail( addrIndex != NULL );
417 addrIndex->dirtyFlag = value;
421 * Return list of interfaces.
423 GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
424 g_return_if_fail( addrIndex != NULL );
425 return addrIndex->interfaceList;
431 void addrindex_free_index( AddressIndex *addrIndex ) {
433 g_return_if_fail( addrIndex != NULL );
435 g_free( ADDRITEM_ID(addrIndex) );
436 g_free( ADDRITEM_NAME(addrIndex) );
437 g_free( addrIndex->filePath );
438 g_free( addrIndex->fileName );
439 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
440 ADDRITEM_ID(addrIndex) = NULL;
441 ADDRITEM_NAME(addrIndex) = NULL;
442 ADDRITEM_PARENT(addrIndex) = NULL;
443 ADDRITEM_SUBTYPE(addrIndex) = 0;
444 addrIndex->filePath = NULL;
445 addrIndex->fileName = NULL;
446 addrIndex->retVal = MGU_SUCCESS;
447 addrIndex->needsConversion = FALSE;
448 addrIndex->wasConverted = FALSE;
449 addrIndex->conversionError = FALSE;
450 addrIndex->lastType = ADDR_IF_NONE;
451 addrIndex->dirtyFlag = FALSE;
452 node = addrIndex->interfaceList;
454 AddressInterface *iface = node->data;
455 addrindex_free_interface( iface );
456 node = g_list_next( node );
458 g_list_free( addrIndex->interfaceList );
459 addrIndex->interfaceList = NULL;
464 * Print address index.
466 void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
467 g_return_if_fail( addrIndex != NULL );
468 fprintf( stream, "AddressIndex:\n" );
469 fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
470 fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
471 fprintf( stream, "\t status: %d : '%s'\n", addrIndex->retVal, mgu_error2string( addrIndex->retVal ) );
472 fprintf( stream, "\tconverted: '%s'\n", addrIndex->wasConverted ? "yes" : "no" );
473 fprintf( stream, "\tcvt error: '%s'\n", addrIndex->conversionError ? "yes" : "no" );
474 fprintf( stream, "\t---\n" );
478 * Retrieve specified interface from index.
480 AddressInterface *addrindex_get_interface( AddressIndex *addrIndex, AddressIfType ifType ) {
481 AddressInterface *retVal = NULL;
483 g_return_if_fail( addrIndex != NULL );
484 node = addrIndex->interfaceList;
486 AddressInterface *iface = node->data;
487 node = g_list_next( node );
488 if( iface->type == ifType ) {
496 AddressDataSource *addrindex_create_datasource() {
497 AddressDataSource *ds = NULL;
498 ds = g_new0( AddressDataSource, 1 );
499 ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
500 ADDRITEM_ID(ds) = NULL;
501 ADDRITEM_NAME(ds) = NULL;
502 ADDRITEM_PARENT(ds) = NULL;
503 ADDRITEM_SUBTYPE(ds) = 0;
504 ds->type = ADDR_IF_NONE;
505 ds->rawDataSource = NULL;
506 ds->interface = NULL;
511 * Add data source to index.
512 * Enter: addrIndex Address index object.
513 * ifType Interface type to add.
514 * dataSource Actual data source to add.
515 * Return: TRUE if data source was added.
516 * Note: The raw data object (for example, AddressBookFile or VCardFile object) should be
517 * supplied as the dataSource argument.
519 AddressDataSource *addrindex_index_add_datasource( AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource ) {
520 AddressInterface *iface;
521 AddressDataSource *ds = NULL;
522 g_return_if_fail( addrIndex != NULL );
523 g_return_if_fail( dataSource != NULL );
525 iface = addrindex_get_interface( addrIndex, ifType );
527 ds = addrindex_create_datasource();
528 ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
530 ds->rawDataSource = dataSource;
531 ds->interface = iface;
532 iface->listSource = g_list_append( iface->listSource, ds );
533 addrIndex->dirtyFlag = TRUE;
539 * Remove data source from index.
540 * Enter: addrIndex Address index object.
541 * dataSource Data source to remove.
542 * Return: Data source if removed, or NULL if data source was not found in
543 * index. Note the this object must still be freed.
545 AddressDataSource *addrindex_index_remove_datasource( AddressIndex *addrIndex, AddressDataSource *dataSource ) {
546 AddressDataSource *retVal = FALSE;
547 AddressInterface *iface;
548 g_return_if_fail( addrIndex != NULL );
549 g_return_if_fail( dataSource != NULL );
551 iface = addrindex_get_interface( addrIndex, dataSource->type );
553 iface->listSource = g_list_remove( iface->listSource, dataSource );
554 addrIndex->dirtyFlag = TRUE;
555 dataSource->interface = NULL;
561 static AddressInterface *addrindex_tag_get_interface( AddressIndex *addrIndex, gchar *tag, AddressIfType ifType ) {
562 AddressInterface *retVal = NULL;
563 GList *node = addrIndex->interfaceList;
565 AddressInterface *iface = node->data;
566 node = g_list_next( node );
568 if( strcmp( iface->listTag, tag ) == 0 ) {
574 if( iface->type == ifType ) {
583 static AddressInterface *addrindex_tag_get_datasource( AddressIndex *addrIndex, AddressIfType ifType, gchar *tag ) {
584 AddressInterface *retVal = NULL;
585 GList *node = addrIndex->interfaceList;
587 AddressInterface *iface = node->data;
588 node = g_list_next( node );
589 if( iface->type == ifType && iface->itemTag ) {
590 if( strcmp( iface->itemTag, tag ) == 0 ) {
599 /* **********************************************************************
600 * Interface XML parsing functions.
601 * ***********************************************************************
603 static void show_attribs( GList *attr ) {
605 gchar *name = ((XMLAttr *)attr->data)->name;
606 gchar *value = ((XMLAttr *)attr->data)->value;
607 printf( "\tattr value : %s :%s:\n", name, value );
608 attr = g_list_next( attr );
613 static void addrindex_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
615 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
620 static void addrindex_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
622 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
628 static void addrindex_write_attr( FILE *fp, gchar *name, gchar *value ) {
632 xml_file_put_escape_str( fp, value );
637 * Return list of name-value pairs.
639 static GList *addrindex_read_attributes( XMLFile *file ) {
646 attr = xml_get_current_tag_attr( file );
648 name = ((XMLAttr *)attr->data)->name;
649 value = ((XMLAttr *)attr->data)->value;
650 nv = g_new0( AddressIfAttrib, 1 );
651 nv->name = g_strdup( name );
652 nv->value = g_strdup( value );
653 list = g_list_append( list, nv );
654 attr = g_list_next( attr );
660 * Output name-value pairs.
662 static void addrindex_write_attributes( FILE *fp, gchar *tag, GList *list, gint lvl ) {
666 addrindex_write_elem_s( fp, lvl, tag );
670 addrindex_write_attr( fp, nv->name, nv->value );
671 node = g_list_next( node );
677 static void addrindex_print_attributes( GList *list, FILE *stream ) {
680 AddressIfAttrib *nv = node->data;
681 fprintf( stream, "%s : %s\n", nv->name, nv->value );
682 node = g_list_next( node );
686 static AddressDataSource *addrindex_parse_book( XMLFile *file ) {
687 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
688 AddressBookFile *abf;
691 abf = addrbook_create_book();
692 attr = xml_get_current_tag_attr( file );
694 gchar *name = ((XMLAttr *)attr->data)->name;
695 gchar *value = ((XMLAttr *)attr->data)->value;
696 if( strcmp( name, ATTAG_BOOK_NAME ) == 0 ) {
697 addrbook_set_name( abf, value );
699 else if( strcmp( name, ATTAG_BOOK_FILE ) == 0) {
700 addrbook_set_file( abf, value );
702 attr = g_list_next( attr );
704 ds->rawDataSource = abf;
708 static void addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
709 AddressBookFile *abf = ds->rawDataSource;
711 addrindex_write_elem_s( fp, lvl, TAG_DS_ADDRESS_BOOK );
712 addrindex_write_attr( fp, ATTAG_BOOK_NAME, abf->name );
713 addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName );
714 fputs( " />\n", fp );
718 static AddressDataSource *addrindex_parse_vcard( XMLFile *file ) {
719 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
723 vcf = vcard_create();
724 attr = xml_get_current_tag_attr( file );
726 gchar *name = ((XMLAttr *)attr->data)->name;
727 gchar *value = ((XMLAttr *)attr->data)->value;
728 if( strcmp( name, ATTAG_VCARD_NAME ) == 0 ) {
729 vcard_set_name( vcf, value );
731 else if( strcmp( name, ATTAG_VCARD_FILE ) == 0) {
732 vcard_set_file( vcf, value );
734 attr = g_list_next( attr );
736 ds->rawDataSource = vcf;
740 static void addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
741 VCardFile *vcf = ds->rawDataSource;
743 addrindex_write_elem_s( fp, lvl, TAG_DS_VCARD );
744 addrindex_write_attr( fp, ATTAG_VCARD_NAME, vcf->name );
745 addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path );
746 fputs( " />\n", fp );
751 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
752 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
756 jpf = jpilot_create();
757 attr = xml_get_current_tag_attr( file );
759 gchar *name = ((XMLAttr *)attr->data)->name;
760 gchar *value = ((XMLAttr *)attr->data)->value;
761 if( strcmp( name, ATTAG_JPILOT_NAME ) == 0 ) {
762 jpilot_set_name( jpf, value );
764 else if( strcmp( name, ATTAG_JPILOT_FILE ) == 0 ) {
765 jpilot_set_file( jpf, value );
767 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_1 ) == 0 ) {
768 jpilot_add_custom_label( jpf, value );
770 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_2 ) == 0 ) {
771 jpilot_add_custom_label( jpf, value );
773 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_3 ) == 0 ) {
774 jpilot_add_custom_label( jpf, value );
776 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_4 ) == 0 ) {
777 jpilot_add_custom_label( jpf, value );
779 attr = g_list_next( attr );
781 ds->rawDataSource = jpf;
785 static void addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
786 JPilotFile *jpf = ds->rawDataSource;
790 GList *customLbl = jpilot_get_custom_labels( jpf );
791 addrindex_write_elem_s( fp, lvl, TAG_DS_JPILOT );
792 addrindex_write_attr( fp, ATTAG_JPILOT_NAME, jpf->name );
793 addrindex_write_attr( fp, ATTAG_JPILOT_FILE, jpf->path );
798 sprintf( name, "%s%d", ATTAG_JPILOT_CUSTOM, ind );
799 addrindex_write_attr( fp, name, node->data );
801 node = g_list_next( node );
803 fputs( " />\n", fp );
807 // Just read/write name-value pairs
808 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
809 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
810 GList *list = addrindex_read_attributes( file );
811 ds->rawDataSource = list;
815 static void addrindex_write_jpilot( FILE *fp, AddressDataSource *ds, gint lvl ) {
816 GList *list = ds->rawDataSource;
818 addrindex_write_attributes( fp, TAG_DS_JPILOT, list, lvl );
824 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
825 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
826 SyldapServer *server;
829 server = syldap_create();
830 attr = xml_get_current_tag_attr( file );
832 gchar *name = ((XMLAttr *)attr->data)->name;
833 gchar *value = ((XMLAttr *)attr->data)->value;
834 gint ivalue = atoi( value );
835 if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
836 syldap_set_name( server, value );
838 else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
839 syldap_set_host( server, value );
841 else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
842 syldap_set_port( server, ivalue );
844 else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
845 syldap_set_base_dn( server, value );
847 else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
848 syldap_set_bind_dn( server, value );
850 else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
851 syldap_set_bind_password( server, value );
853 else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
854 syldap_set_search_criteria( server, value );
856 else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
857 syldap_set_max_entries( server, ivalue );
859 else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
860 syldap_set_timeout( server, ivalue );
862 attr = g_list_next( attr );
865 ds->rawDataSource = server;
869 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
870 SyldapServer *server = ds->rawDataSource;
874 addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP );
875 addrindex_write_attr( fp, ATTAG_LDAP_NAME, server->name );
876 addrindex_write_attr( fp, ATTAG_LDAP_HOST, server->hostName );
878 sprintf( value, "%d", server->port );
879 addrindex_write_attr( fp, ATTAG_LDAP_PORT, value );
881 addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, server->baseDN );
882 addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, server->bindDN );
883 addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, server->bindPass );
884 addrindex_write_attr( fp, ATTAG_LDAP_CRITERIA, server->searchCriteria );
886 sprintf( value, "%d", server->maxEntries );
887 addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value );
888 sprintf( value, "%d", server->timeOut );
889 addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value );
895 // Just read/write name-value pairs
896 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
897 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
898 GList *list = addrindex_read_attributes( file );
903 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
904 GList *list = ds->rawDataSource;
906 addrindex_write_attributes( fp, TAG_DS_LDAP, list, lvl );
911 /* **********************************************************************
912 * Address index I/O functions.
913 * ***********************************************************************
915 static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
920 AddressInterface *iface = NULL, *dsIFace = NULL;
921 AddressDataSource *ds;
924 prev_level = file->level;
925 xml_parse_next_tag( file );
926 if( file->level < prev_level ) return;
928 xtag = xml_get_current_tag( file );
929 // printf( "tag : %s\n", xtag->tag );
931 iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
933 addrIndex->lastType = iface->type;
934 if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
935 // printf( "found : %s\n", iface->name );
938 dsIFace = addrindex_tag_get_datasource( addrIndex, addrIndex->lastType, xtag->tag );
940 // Add data source to list
941 // printf( "\tdata source: %s\n", dsIFace->name );
943 if( addrIndex->lastType == ADDR_IF_BOOK ) {
944 ds = addrindex_parse_book( file );
945 if( ds->rawDataSource ) {
946 addrbook_set_path( ds->rawDataSource, addrIndex->filePath );
947 // addrbook_print_book( ds->rawDataSource, stdout );
950 else if( addrIndex->lastType == ADDR_IF_VCARD ) {
951 ds = addrindex_parse_vcard( file );
952 // if( ds->rawDataSource ) {
953 // vcard_print_file( ds->rawDataSource, stdout );
956 else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
957 ds = addrindex_parse_jpilot( file );
959 if( ds->rawDataSource ) {
960 jpilot_print_file( ds->rawDataSource, stdout );
961 // addrindex_print_attributes( ds->rawDataSource, stdout );
965 else if( addrIndex->lastType == ADDR_IF_LDAP ) {
966 ds = addrindex_parse_ldap( file );
968 if( ds->rawDataSource ) {
969 syldap_print_data( ds->rawDataSource, stdout );
970 // addrindex_print_attributes( ds->rawDataSource, stdout );
975 ds->type = addrIndex->lastType;
976 ds->interface = dsIFace;
977 dsIFace->listSource = g_list_append( dsIFace->listSource, ds );
979 // printf( "=============================\n\n" );
983 element = xml_get_element( file );
984 attr = xml_get_current_tag_attr( file );
985 if( _interfaceLast_ && ! _interfaceLast_->legacyFlag ) {
986 show_attribs( attr );
987 printf( "\ttag value : %s :\n", element );
990 addrindex_read_index( addrIndex, file );
994 static gint addrindex_read_file( AddressIndex *addrIndex ) {
995 XMLFile *file = NULL;
996 gchar *fileSpec = NULL;
997 g_return_if_fail( addrIndex != NULL );
999 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1000 addrIndex->retVal = MGU_NO_FILE;
1001 file = xml_open_file( fileSpec );
1004 if( file == NULL ) {
1005 // fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName );
1006 return addrIndex->retVal;
1009 addrIndex->retVal = MGU_BAD_FORMAT;
1010 if( xml_get_dtd( file ) == 0 ) {
1011 if( xml_parse_next_tag( file ) == 0 ) {
1012 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1013 addrindex_read_index( addrIndex, file );
1014 addrIndex->retVal = MGU_SUCCESS;
1018 xml_close_file( file );
1020 return addrIndex->retVal;
1023 static void addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1024 GList *nodeIF, *nodeDS;
1026 gint lvlItem = 1 + lvlList;
1028 nodeIF = addrIndex->interfaceList;
1030 AddressInterface *iface = nodeIF->data;
1031 if( ! iface->legacyFlag ) {
1032 nodeDS = iface->listSource;
1033 addrindex_write_elem_s( fp, lvlList, iface->listTag );
1036 AddressDataSource *ds = nodeDS->data;
1038 if( iface->type == ADDR_IF_BOOK ) {
1039 addrindex_write_book( fp, ds, lvlItem );
1041 if( iface->type == ADDR_IF_VCARD ) {
1042 addrindex_write_vcard( fp, ds, lvlItem );
1044 if( iface->type == ADDR_IF_JPILOT ) {
1045 addrindex_write_jpilot( fp, ds, lvlItem );
1047 if( iface->type == ADDR_IF_LDAP ) {
1048 addrindex_write_ldap( fp, ds, lvlItem );
1051 nodeDS = g_list_next( nodeDS );
1053 addrindex_write_elem_e( fp, lvlList, iface->listTag );
1055 nodeIF = g_list_next( nodeIF );
1060 * Write data to specified file.
1061 * Enter: addrIndex Address index object.
1062 * newFile New file name.
1063 * return: Status code, from addrIndex->retVal.
1064 * Note: File will be created in directory specified by addrIndex.
1066 gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1069 #ifndef DEV_STANDALONE
1073 g_return_if_fail( addrIndex != NULL );
1075 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1076 addrIndex->retVal = MGU_OPEN_FILE;
1077 #ifdef DEV_STANDALONE
1078 fp = fopen( fileSpec, "w" );
1081 fputs( "<?xml version=\"1.0\" ?>\n", fp );
1083 pfile = prefs_write_open( fileSpec );
1087 fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
1088 conv_get_current_charset_str() );
1090 addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
1093 addrindex_write_index( addrIndex, fp );
1094 addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
1096 addrIndex->retVal = MGU_SUCCESS;
1097 #ifdef DEV_STANDALONE
1100 if( prefs_write_close( pfile ) < 0 ) {
1101 addrIndex->retVal = MGU_ERROR_WRITE;
1107 return addrIndex->retVal;
1111 * Save address index data to original file.
1112 * return: Status code, from addrIndex->retVal.
1114 gint addrindex_save_data( AddressIndex *addrIndex ) {
1115 g_return_if_fail( addrIndex != NULL );
1117 addrIndex->retVal = MGU_NO_FILE;
1118 if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1119 if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1121 addrindex_write_to( addrIndex, addrIndex->fileName );
1122 if( addrIndex->retVal == MGU_SUCCESS ) {
1123 addrIndex->dirtyFlag = FALSE;
1125 return addrIndex->retVal;
1129 * Save all address book files which may have changed.
1130 * Return: Status code, set if there was a problem saving data.
1132 gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1133 gint retVal = MGU_SUCCESS;
1134 GList *nodeIf, *nodeDS;
1135 nodeIf = addrIndex->interfaceList;
1137 AddressInterface *iface = nodeIf->data;
1138 if( iface->type == ADDR_IF_BOOK ) {
1139 nodeDS = iface->listSource;
1141 AddressDataSource *ds = nodeDS->data;
1142 AddressBookFile *abf = ds->rawDataSource;
1143 if( abf->dirtyFlag ) {
1144 if( abf->readFlag ) {
1145 addrbook_save_data( abf );
1146 if( abf->retVal != MGU_SUCCESS ) {
1147 retVal = abf->retVal;
1151 nodeDS = g_list_next( nodeDS );
1155 nodeIf = g_list_next( nodeIf );
1161 /* **********************************************************************
1162 * Address book conversion to new format.
1163 * ***********************************************************************
1166 #define ELTAG_IF_OLD_FOLDER "folder"
1167 #define ELTAG_IF_OLD_GROUP "group"
1168 #define ELTAG_IF_OLD_ITEM "item"
1169 #define ELTAG_IF_OLD_NAME "name"
1170 #define ELTAG_IF_OLD_ADDRESS "address"
1171 #define ELTAG_IF_OLD_REMARKS "remarks"
1172 #define ATTAG_IF_OLD_NAME "name"
1174 #define TEMPNODE_ROOT 0
1175 #define TEMPNODE_FOLDER 1
1176 #define TEMPNODE_GROUP 2
1177 #define TEMPNODE_ADDRESS 3
1179 typedef struct _AddressCvt_Node AddressCvtNode;
1180 struct _AddressCvt_Node {
1189 * Parse current address item.
1191 static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1196 nn = g_new0( AddressCvtNode, 1 );
1197 nn->type = TEMPNODE_ADDRESS;
1200 level = file->level;
1203 xml_parse_next_tag(file);
1204 if (file->level < level) return nn;
1206 element = xml_get_element( file );
1207 if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1208 nn->name = g_strdup( element );
1210 if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1211 nn->address = g_strdup( element );
1213 if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1214 nn->remarks = g_strdup( element );
1216 xml_parse_next_tag(file);
1221 * Create a temporary node below specified node.
1223 static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1225 nn = g_new0( AddressCvtNode, 1 );
1227 nn->name = g_strdup( name );
1228 nn->remarks = g_strdup( rem );
1229 node->list = g_list_append( node->list, nn );
1234 * Process current temporary node.
1236 static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1239 AddressCvtNode *newNode = NULL;
1244 prev_level = file->level;
1245 xml_parse_next_tag( file );
1246 if (file->level < prev_level) return;
1250 if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1251 attr = xml_get_current_tag_attr(file);
1253 name = ((XMLAttr *)attr->data)->name;
1254 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1255 value = ((XMLAttr *)attr->data)->value;
1258 newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1259 addrindex_add_obj( file, newNode );
1262 else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1263 attr = xml_get_current_tag_attr(file);
1265 name = ((XMLAttr *)attr->data)->name;
1266 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1267 value = ((XMLAttr *)attr->data)->value;
1270 newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1271 addrindex_add_obj( file, newNode );
1273 else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
1274 newNode = addrindex_parse_item( file );
1275 node->list = g_list_append( node->list, newNode );
1278 // printf( "invalid: !!! \n" );
1279 attr = xml_get_current_tag_attr( file );
1285 * Consume all nodes below current tag.
1287 static void addrindex_consume_tree( XMLFile *file ) {
1294 prev_level = file->level;
1295 xml_parse_next_tag( file );
1296 if (file->level < prev_level) return;
1298 xtag = xml_get_current_tag( file );
1299 // printf( "tag : %s\n", xtag->tag );
1300 element = xml_get_element( file );
1301 attr = xml_get_current_tag_attr( file );
1302 // show_attribs( attr );
1303 // printf( "\ttag value : %s :\n", element );
1304 addrindex_consume_tree( file );
1309 * Print temporary tree.
1311 static void addrindex_print_node( AddressCvtNode *node, FILE *stream ) {
1313 fprintf( stream, "Node:\ttype :%d:\n", node->type );
1314 fprintf( stream, "\tname :%s:\n", node->name );
1315 fprintf( stream, "\taddr :%s:\n", node->address );
1316 fprintf( stream, "\trems :%s:\n", node->remarks );
1318 fprintf( stream, "\t--list----\n" );
1322 AddressCvtNode *lNode = list->data;
1323 list = g_list_next( list );
1324 addrindex_print_node( lNode, stream );
1326 fprintf( stream, "\t==list-%d==\n", node->type );
1330 * Free up temporary tree.
1332 static void addrindex_free_node( AddressCvtNode *node ) {
1333 GList *list = node->list;
1335 AddressCvtNode *lNode = list->data;
1336 list = g_list_next( list );
1337 addrindex_free_node( lNode );
1339 node->type = TEMPNODE_ROOT;
1340 g_free( node->name );
1341 g_free( node->address );
1342 g_free( node->remarks );
1343 g_list_free( node->list );
1348 * Process address book for specified node.
1350 static void addrindex_process_node(
1351 AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
1352 ItemGroup *parentGrp, ItemFolder *folderGrp )
1355 ItemFolder *itemFolder;
1356 ItemGroup *itemGParent = parentGrp;
1357 ItemFolder *itemGFolder = folderGrp;
1358 AddressCache *cache = abf->addressCache;
1360 if( node->type == TEMPNODE_ROOT ) {
1361 itemFolder = parent;
1363 else if( node->type == TEMPNODE_FOLDER ) {
1364 itemFolder = addritem_create_item_folder();
1365 addritem_folder_set_name( itemFolder, node->name );
1366 addrcache_id_folder( cache, itemFolder );
1367 addrcache_folder_add_folder( cache, parent, itemFolder );
1370 else if( node->type == TEMPNODE_GROUP ) {
1371 ItemGroup *itemGroup;
1374 // Create a folder for group
1375 fName = g_strdup_printf( "Cvt - %s", node->name );
1376 itemGFolder = addritem_create_item_folder();
1377 addritem_folder_set_name( itemGFolder, fName );
1378 addrcache_id_folder( cache, itemGFolder );
1379 addrcache_folder_add_folder( cache, parent, itemGFolder );
1382 // Add group into folder
1383 itemGroup = addritem_create_item_group();
1384 addritem_group_set_name( itemGroup, node->name );
1385 addrcache_id_group( cache, itemGroup );
1386 addrcache_folder_add_group( cache, itemGFolder, itemGroup );
1387 itemGParent = itemGroup;
1389 else if( node->type == TEMPNODE_ADDRESS ) {
1390 ItemPerson *itemPerson;
1391 ItemEMail *itemEMail;
1393 // Create person and email objects
1394 itemPerson = addritem_create_item_person();
1395 addritem_person_set_common_name( itemPerson, node->name );
1396 addrcache_id_person( cache, itemPerson );
1397 itemEMail = addritem_create_item_email();
1398 addritem_email_set_address( itemEMail, node->address );
1399 addritem_email_set_remarks( itemEMail, node->remarks );
1400 addrcache_id_email( cache, itemEMail );
1401 addrcache_person_add_email( cache, itemPerson, itemEMail );
1403 // Add person into appropriate folder
1405 addrcache_folder_add_person( cache, itemGFolder, itemPerson );
1408 addrcache_folder_add_person( cache, parent, itemPerson );
1411 // Add email address only into group
1413 addrcache_group_add_email( cache, parentGrp, itemEMail );
1419 AddressCvtNode *lNode = list->data;
1420 list = g_list_next( list );
1421 addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
1426 * Process address book to specified file number.
1428 static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
1429 gboolean retVal = FALSE;
1430 AddressBookFile *abf = NULL;
1431 AddressCvtNode *rootNode = NULL;
1432 gchar *newFile = NULL;
1433 GList *fileList = NULL;
1437 rootNode = g_new0( AddressCvtNode, 1 );
1438 rootNode->type = TEMPNODE_ROOT;
1439 rootNode->name = g_strdup( "root" );
1440 rootNode->list = NULL;
1441 addrindex_add_obj( file, rootNode );
1442 // addrindex_print_node( rootNode, stdout );
1444 // Create new address book
1445 abf = addrbook_create_book();
1446 addrbook_set_name( abf, displayName );
1447 addrbook_set_path( abf, addrIndex->filePath );
1449 // Determine next available file number
1450 fileList = addrbook_get_bookfile_list( abf );
1452 fileNum = 1 + abf->maxValue;
1454 g_list_free( fileList );
1457 newFile = addrbook_gen_new_file_name( fileNum );
1459 addrbook_set_file( abf, newFile );
1462 addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
1464 // addrbook_dump_book( abf, stdout );
1465 addrbook_save_data( abf );
1466 addrIndex->retVal = abf->retVal;
1467 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1469 addrbook_free_book( abf );
1471 addrindex_free_node( rootNode );
1474 // Create entries in address index
1476 abf = addrbook_create_book();
1477 addrbook_set_name( abf, displayName );
1478 addrbook_set_path( abf, addrIndex->filePath );
1479 addrbook_set_file( abf, newFile );
1480 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1487 * Process tree converting data.
1489 static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
1497 prev_level = file->level;
1498 xml_parse_next_tag( file );
1499 if (file->level < prev_level) return;
1501 xtag = xml_get_current_tag( file );
1502 // printf( "tag : %d : %s\n", prev_level, xtag->tag );
1503 if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
1504 if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
1505 addrIndex->needsConversion = FALSE;
1506 addrIndex->wasConverted = TRUE;
1511 if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
1512 if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
1513 addrIndex->needsConversion = FALSE;
1514 addrIndex->wasConverted = TRUE;
1519 element = xml_get_element( file );
1520 attr = xml_get_current_tag_attr( file );
1521 // show_attribs( attr );
1522 // printf( "\ttag value : %s :\n", element );
1523 addrindex_consume_tree( file );
1527 static gint addrindex_convert_data( AddressIndex *addrIndex ) {
1528 XMLFile *file = NULL;
1531 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1532 addrIndex->retVal = MGU_NO_FILE;
1533 file = xml_open_file( fileSpec );
1536 if( file == NULL ) {
1537 // fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName );
1538 return addrIndex->retVal;
1541 addrIndex->retVal = MGU_BAD_FORMAT;
1542 if( xml_get_dtd( file ) == 0 ) {
1543 if( xml_parse_next_tag( file ) == 0 ) {
1544 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1545 addrindex_convert_tree( addrIndex, file );
1549 xml_close_file( file );
1550 return addrIndex->retVal;
1554 * Create a new address book file.
1556 static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
1557 gboolean retVal = FALSE;
1558 AddressBookFile *abf = NULL;
1559 gchar *newFile = NULL;
1560 GList *fileList = NULL;
1563 // Create new address book
1564 abf = addrbook_create_book();
1565 addrbook_set_name( abf, displayName );
1566 addrbook_set_path( abf, addrIndex->filePath );
1568 // Determine next available file number
1569 fileList = addrbook_get_bookfile_list( abf );
1571 fileNum = 1 + abf->maxValue;
1573 g_list_free( fileList );
1576 newFile = addrbook_gen_new_file_name( fileNum );
1578 addrbook_set_file( abf, newFile );
1581 addrbook_save_data( abf );
1582 addrIndex->retVal = abf->retVal;
1583 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1584 addrbook_free_book( abf );
1587 // Create entries in address index
1589 abf = addrbook_create_book();
1590 addrbook_set_name( abf, displayName );
1591 addrbook_set_path( abf, addrIndex->filePath );
1592 addrbook_set_file( abf, newFile );
1593 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1600 * Read data for address index performing a conversion if necesary.
1601 * Enter: addrIndex Address index object.
1602 * return: Status code, from addrIndex->retVal.
1603 * Note: New address book files will be created in directory specified by
1604 * addrIndex. Three files will be created, for the following:
1605 * "Common addresses"
1606 * "Personal addresses"
1607 * "Gathered addresses" - a new address book.
1609 gint addrindex_read_data( AddressIndex *addrIndex ) {
1610 g_return_if_fail( addrIndex != NULL );
1612 addrIndex->conversionError = FALSE;
1613 addrindex_read_file( addrIndex );
1614 if( addrIndex->retVal == MGU_SUCCESS ) {
1615 if( addrIndex->needsConversion ) {
1616 if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
1617 addrIndex->conversionError = TRUE;
1620 addrIndex->conversionError = TRUE;
1623 addrIndex->dirtyFlag = TRUE;
1625 return addrIndex->retVal;
1629 * Create new address books for a new address index.
1630 * Enter: addrIndex Address index object.
1631 * return: Status code, from addrIndex->retVal.
1632 * Note: New address book files will be created in directory specified by
1633 * addrIndex. Three files will be created, for the following:
1634 * "Common addresses"
1635 * "Personal addresses"
1636 * "Gathered addresses" - a new address book.
1638 gint addrindex_create_new_books( AddressIndex *addrIndex ) {
1641 g_return_if_fail( addrIndex != NULL );
1643 flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
1645 flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
1646 addrIndex->dirtyFlag = TRUE;
1648 return addrIndex->retVal;
1651 /* **********************************************************************
1652 * New interface stuff.
1653 * ***********************************************************************
1657 * Return modified flag for specified data source.
1659 gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
1660 gboolean retVal = FALSE;
1661 AddressInterface *iface;
1663 if( ds == NULL ) return retVal;
1664 iface = ds->interface;
1665 if( iface == NULL ) return retVal;
1666 if( iface->getModifyFlag ) {
1667 retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
1673 * Return accessed flag for specified data source.
1675 gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
1676 gboolean retVal = FALSE;
1677 AddressInterface *iface;
1679 if( ds == NULL ) return retVal;
1680 iface = ds->interface;
1681 if( iface == NULL ) return retVal;
1682 if( iface->getAccessFlag ) {
1683 retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
1689 * Return data read flag for specified data source.
1691 gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
1692 gboolean retVal = TRUE;
1693 AddressInterface *iface;
1695 if( ds == NULL ) return retVal;
1696 iface = ds->interface;
1697 if( iface == NULL ) return retVal;
1698 if( iface->getReadFlag ) {
1699 retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
1705 * Return status code for specified data source.
1707 gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
1708 gint retVal = MGU_SUCCESS;
1709 AddressInterface *iface;
1711 if( ds == NULL ) return retVal;
1712 iface = ds->interface;
1713 if( iface == NULL ) return retVal;
1714 if( iface->getStatusCode ) {
1715 retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
1721 * Return data read flag for specified data source.
1723 gint addrindex_ds_read_data( AddressDataSource *ds ) {
1724 gint retVal = MGU_SUCCESS;
1725 AddressInterface *iface;
1727 if( ds == NULL ) return retVal;
1728 iface = ds->interface;
1729 if( iface == NULL ) return retVal;
1730 if( iface->getReadData ) {
1731 retVal = ( iface->getReadData ) ( ds->rawDataSource );
1737 * Return data read flag for specified data source.
1739 ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
1740 ItemFolder *retVal = NULL;
1741 AddressInterface *iface;
1743 if( ds == NULL ) return retVal;
1744 iface = ds->interface;
1745 if( iface == NULL ) return retVal;
1746 if( iface->getRootFolder ) {
1747 retVal = ( iface->getRootFolder ) ( ds->rawDataSource );
1753 * Return list of folders for specified data source.
1755 GList *addrindex_ds_get_list_folder( AddressDataSource *ds ) {
1756 GList *retVal = FALSE;
1757 AddressInterface *iface;
1759 if( ds == NULL ) return retVal;
1760 iface = ds->interface;
1761 if( iface == NULL ) return retVal;
1762 if( iface->getListFolder ) {
1763 retVal = ( iface->getListFolder ) ( ds->rawDataSource );
1769 * Return list of persons in root folder for specified data source.
1771 GList *addrindex_ds_get_list_person( AddressDataSource *ds ) {
1772 GList *retVal = FALSE;
1773 AddressInterface *iface;
1775 if( ds == NULL ) return retVal;
1776 iface = ds->interface;
1777 if( iface == NULL ) return retVal;
1778 if( iface->getListPerson ) {
1779 retVal = ( iface->getListPerson ) ( ds->rawDataSource );
1785 * Return name for specified data source.
1787 gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
1788 gchar *retVal = FALSE;
1789 AddressInterface *iface;
1791 if( ds == NULL ) return retVal;
1792 iface = ds->interface;
1793 if( iface == NULL ) return retVal;
1794 if( iface->getName ) {
1795 retVal = ( iface->getName ) ( ds->rawDataSource );
1801 * Set the access flag inside the data source.
1803 void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
1804 AddressInterface *iface;
1806 if( ds == NULL ) return;
1807 iface = ds->interface;
1808 if( iface == NULL ) return;
1809 if( iface->setAccessFlag ) {
1810 ( iface->setAccessFlag ) ( ds->rawDataSource, value );
1815 * Return read only flag for specified data source.
1817 gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
1818 AddressInterface *iface;
1819 if( ds == NULL ) return TRUE;
1820 iface = ds->interface;
1821 if( iface == NULL ) return TRUE;
1822 return iface->readOnly;
1826 * Return list of all persons for specified data source.
1828 GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
1829 GList *retVal = NULL;
1830 AddressInterface *iface;
1832 if( ds == NULL ) return retVal;
1833 iface = ds->interface;
1834 if( iface == NULL ) return retVal;
1835 if( iface->getAllPersons ) {
1836 retVal = ( iface->getAllPersons ) ( ds->rawDataSource );