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"
94 N_("Personal address")
97 #define DISP_NEW_COMMON _("Common address")
98 #define DISP_NEW_PERSONAL _("Personal address")
100 /* Old address book */
101 #define TAG_IF_OLD_COMMON "common_address"
102 #define TAG_IF_OLD_PERSONAL "personal_address"
104 #define DISP_OLD_COMMON _("Common address")
105 #define DISP_OLD_PERSONAL _("Personal address")
107 typedef struct _AddressIfAttr AddressIfAttrib;
108 struct _AddressIfAttr {
114 * Build interface with default values.
116 static AddressInterface *addrindex_create_interface( gint type, gchar *name, gchar *tagIf, gchar *tagDS ) {
117 AddressInterface *iface = g_new0( AddressInterface, 1 );
118 ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
119 ADDRITEM_ID(iface) = NULL;
120 ADDRITEM_NAME(iface) = g_strdup( name );
121 ADDRITEM_PARENT(iface) = NULL;
122 ADDRITEM_SUBTYPE(iface) = type;
124 iface->name = g_strdup( name );
125 iface->listTag = g_strdup( tagIf );
126 iface->itemTag = g_strdup( tagDS );
127 iface->legacyFlag = FALSE;
128 iface->haveLibrary = TRUE;
129 iface->useInterface = TRUE;
130 iface->readOnly = TRUE;
131 iface->getAccessFlag = NULL;
132 iface->getModifyFlag = NULL;
133 iface->getReadFlag = NULL;
134 iface->getStatusCode = NULL;
135 iface->getReadData = NULL;
136 iface->getRootFolder = NULL;
137 iface->getListFolder = NULL;
138 iface->getListPerson = NULL;
139 iface->getAllPersons = NULL;
140 iface->getAllGroups = NULL;
141 iface->getName = NULL;
142 iface->listSource = NULL;
147 * Build table of interfaces.
149 static void addrindex_build_if_list( AddressIndex *addrIndex ) {
150 AddressInterface *iface;
152 iface = addrindex_create_interface( ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK, TAG_DS_ADDRESS_BOOK );
153 iface->readOnly = FALSE;
154 iface->getModifyFlag = ( void * ) addrbook_get_modified;
155 iface->getAccessFlag = ( void * ) addrbook_get_accessed;
156 iface->getReadFlag = ( void * ) addrbook_get_read_flag;
157 iface->getStatusCode = ( void * ) addrbook_get_status;
158 iface->getReadData = ( void * ) addrbook_read_data;
159 iface->getRootFolder = ( void * ) addrbook_get_root_folder;
160 iface->getListFolder = ( void * ) addrbook_get_list_folder;
161 iface->getListPerson = ( void * ) addrbook_get_list_person;
162 iface->getAllPersons = ( void * ) addrbook_get_all_persons;
163 iface->getName = ( void * ) addrbook_get_name;
164 iface->setAccessFlag = ( void * ) addrbook_set_accessed;
165 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
166 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
168 iface = addrindex_create_interface( ADDR_IF_VCARD, "vCard", TAG_IF_VCARD, TAG_DS_VCARD );
169 iface->getModifyFlag = ( void * ) vcard_get_modified;
170 iface->getAccessFlag = ( void * ) vcard_get_accessed;
171 iface->getReadFlag = ( void * ) vcard_get_read_flag;
172 iface->getStatusCode = ( void * ) vcard_get_status;
173 iface->getReadData = ( void * ) vcard_read_data;
174 iface->getRootFolder = ( void * ) vcard_get_root_folder;
175 iface->getListFolder = ( void * ) vcard_get_list_folder;
176 iface->getListPerson = ( void * ) vcard_get_list_person;
177 iface->getAllPersons = ( void * ) vcard_get_all_persons;
178 iface->getName = ( void * ) vcard_get_name;
179 iface->setAccessFlag = ( void * ) vcard_set_accessed;
180 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
181 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
183 iface = addrindex_create_interface( ADDR_IF_JPILOT, "J-Pilot", TAG_IF_JPILOT, TAG_DS_JPILOT );
185 iface->haveLibrary = jpilot_test_pilot_lib();
186 iface->useInterface = iface->haveLibrary;
187 iface->getModifyFlag = ( void * ) jpilot_get_modified;
188 iface->getAccessFlag = ( void * ) jpilot_get_accessed;
189 iface->getReadFlag = ( void * ) jpilot_get_read_flag;
190 iface->getStatusCode = ( void * ) jpilot_get_status;
191 iface->getReadData = ( void * ) jpilot_read_data;
192 iface->getRootFolder = ( void * ) jpilot_get_root_folder;
193 iface->getListFolder = ( void * ) jpilot_get_list_folder;
194 iface->getListPerson = ( void * ) jpilot_get_list_person;
195 iface->getAllPersons = ( void * ) jpilot_get_all_persons;
196 iface->getName = ( void * ) jpilot_get_name;
197 iface->setAccessFlag = ( void * ) jpilot_set_accessed;
199 iface->useInterface = FALSE;
200 iface->haveLibrary = FALSE;
202 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
203 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
205 iface = addrindex_create_interface( ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
207 iface->haveLibrary = syldap_test_ldap_lib();
208 iface->useInterface = iface->haveLibrary;
209 iface->getAccessFlag = ( void * ) syldap_get_accessed;
210 /* iface->getModifyFlag = ( void * ) syldap_get_modified; */
211 /* iface->getReadFlag = ( void * ) syldap_get_read_flag; */
212 iface->getStatusCode = ( void * ) syldap_get_status;
213 iface->getReadData = ( void * ) syldap_read_data;
214 iface->getRootFolder = ( void * ) syldap_get_root_folder;
215 iface->getListFolder = ( void * ) syldap_get_list_folder;
216 iface->getListPerson = ( void * ) syldap_get_list_person;
217 iface->getName = ( void * ) syldap_get_name;
218 iface->setAccessFlag = ( void * ) syldap_set_accessed;
220 iface->useInterface = FALSE;
221 iface->haveLibrary = FALSE;
223 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
224 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
226 /* Two old legacy data sources */
227 iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - common", TAG_IF_OLD_COMMON, NULL );
228 iface->legacyFlag = TRUE;
229 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
230 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
232 iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - personal", TAG_IF_OLD_PERSONAL, NULL );
233 iface->legacyFlag = TRUE;
234 addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
235 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
240 * Free name-value pairs.
242 static void addrindex_free_attributes( GList *list ) {
245 AddressIfAttrib *nv = node->data;
246 g_free( nv->name ); nv->name = NULL;
247 g_free( nv->value ); nv->value = NULL;
250 node = g_list_next( node );
256 * Free up data source.
258 void addrindex_free_datasource( AddressIndex *addrIndex, AddressDataSource *ds ) {
259 AddressInterface *iface = NULL;
260 g_return_if_fail( addrIndex != NULL );
261 g_return_if_fail( ds != NULL );
263 if( ds->interface == NULL ) {
264 iface = addrindex_get_interface( addrIndex, ds->type );
266 if( iface == NULL ) return;
268 if( iface->useInterface ) {
269 if( iface->type == ADDR_IF_BOOK ) {
270 AddressBookFile *abf = ds->rawDataSource;
272 addrbook_free_book( abf );
275 else if( iface->type == ADDR_IF_VCARD ) {
276 VCardFile *vcf = ds->rawDataSource;
282 else if( iface->type == ADDR_IF_JPILOT ) {
283 JPilotFile *jpf = ds->rawDataSource;
290 else if( iface->type == ADDR_IF_LDAP ) {
291 SyldapServer *server = ds->rawDataSource;
293 syldap_free( server );
299 GList *list = ds->rawDataSource;
300 addrindex_free_attributes( list );
303 g_free( ADDRITEM_ID(addrIndex) );
304 g_free( ADDRITEM_NAME(addrIndex) );
306 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
307 ADDRITEM_ID(addrIndex) = NULL;
308 ADDRITEM_NAME(addrIndex) = NULL;
309 ADDRITEM_PARENT(addrIndex) = NULL;
310 ADDRITEM_SUBTYPE(addrIndex) = 0;
311 ds->type = ADDR_IF_NONE;
312 ds->rawDataSource = NULL;
313 ds->interface = NULL;
315 ds->type = ADDR_IF_NONE;
316 ds->rawDataSource = NULL;
317 ds->interface = NULL;
321 static void addrindex_free_all_datasources( AddressInterface *iface ) {
322 GList *node = iface->listSource;
324 AddressDataSource *ds = node->data;
325 if( iface->useInterface ) {
326 if( iface->type == ADDR_IF_BOOK ) {
327 AddressBookFile *abf = ds->rawDataSource;
329 addrbook_free_book( abf );
332 else if( iface->type == ADDR_IF_VCARD ) {
333 VCardFile *vcf = ds->rawDataSource;
339 else if( iface->type == ADDR_IF_JPILOT ) {
340 JPilotFile *jpf = ds->rawDataSource;
347 else if( iface->type == ADDR_IF_LDAP ) {
348 SyldapServer *server = ds->rawDataSource;
350 syldap_free( server );
356 GList *list = ds->rawDataSource;
357 addrindex_free_attributes( list );
360 ds->type = ADDR_IF_NONE;
361 ds->rawDataSource = NULL;
362 ds->interface = NULL;
365 node = g_list_next( node );
369 static void addrindex_free_interface( AddressInterface *iface ) {
370 addrindex_free_all_datasources( iface );
372 g_free( ADDRITEM_ID(iface) );
373 g_free( ADDRITEM_NAME(iface) );
374 g_free( iface->name );
375 g_free( iface->listTag );
376 g_free( iface->itemTag );
378 ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
379 ADDRITEM_ID(iface) = NULL;
380 ADDRITEM_NAME(iface) = NULL;
381 ADDRITEM_PARENT(iface) = NULL;
382 ADDRITEM_SUBTYPE(iface) = 0;
383 iface->type = ADDR_IF_NONE;
385 iface->listTag = NULL;
386 iface->itemTag = NULL;
387 iface->legacyFlag = FALSE;
388 iface->useInterface = FALSE;
389 iface->haveLibrary = FALSE;
391 g_list_free( iface->listSource );
392 iface->listSource = NULL;
398 AddressIndex *addrindex_create_index() {
399 AddressIndex *addrIndex = g_new0( AddressIndex, 1 );
401 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_INDEX;
402 ADDRITEM_ID(addrIndex) = NULL;
403 ADDRITEM_NAME(addrIndex) = g_strdup( "Address Index" );
404 ADDRITEM_PARENT(addrIndex) = NULL;
405 ADDRITEM_SUBTYPE(addrIndex) = 0;
406 addrIndex->filePath = NULL;
407 addrIndex->fileName = NULL;
408 addrIndex->retVal = MGU_SUCCESS;
409 addrIndex->needsConversion = FALSE;
410 addrIndex->wasConverted = FALSE;
411 addrIndex->conversionError = FALSE;
412 addrIndex->interfaceList = NULL;
413 addrIndex->lastType = ADDR_IF_NONE;
414 addrIndex->dirtyFlag = FALSE;
415 addrindex_build_if_list( addrIndex );
420 * Specify file to be used.
422 void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
423 g_return_if_fail( addrIndex != NULL );
424 addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
426 void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
427 g_return_if_fail( addrIndex != NULL );
428 addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
430 void addrindex_set_dirty( AddressIndex *addrIndex, const gboolean value ) {
431 g_return_if_fail( addrIndex != NULL );
432 addrIndex->dirtyFlag = value;
436 * Return list of interfaces.
438 GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
439 g_return_val_if_fail( addrIndex != NULL, NULL );
440 return addrIndex->interfaceList;
446 void addrindex_free_index( AddressIndex *addrIndex ) {
449 g_return_if_fail( addrIndex != NULL );
451 g_free( ADDRITEM_ID(addrIndex) );
452 g_free( ADDRITEM_NAME(addrIndex) );
453 g_free( addrIndex->filePath );
454 g_free( addrIndex->fileName );
455 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
456 ADDRITEM_ID(addrIndex) = NULL;
457 ADDRITEM_NAME(addrIndex) = NULL;
458 ADDRITEM_PARENT(addrIndex) = NULL;
459 ADDRITEM_SUBTYPE(addrIndex) = 0;
460 addrIndex->filePath = NULL;
461 addrIndex->fileName = NULL;
462 addrIndex->retVal = MGU_SUCCESS;
463 addrIndex->needsConversion = FALSE;
464 addrIndex->wasConverted = FALSE;
465 addrIndex->conversionError = FALSE;
466 addrIndex->lastType = ADDR_IF_NONE;
467 addrIndex->dirtyFlag = FALSE;
468 node = addrIndex->interfaceList;
470 AddressInterface *iface = node->data;
471 addrindex_free_interface( iface );
472 node = g_list_next( node );
474 g_list_free( addrIndex->interfaceList );
475 addrIndex->interfaceList = NULL;
480 * Print address index.
482 void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
483 g_return_if_fail( addrIndex != NULL );
484 fprintf( stream, "AddressIndex:\n" );
485 fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
486 fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
487 fprintf( stream, "\t status: %d : '%s'\n", addrIndex->retVal, mgu_error2string( addrIndex->retVal ) );
488 fprintf( stream, "\tconverted: '%s'\n", addrIndex->wasConverted ? "yes" : "no" );
489 fprintf( stream, "\tcvt error: '%s'\n", addrIndex->conversionError ? "yes" : "no" );
490 fprintf( stream, "\t---\n" );
494 * Retrieve specified interface from index.
496 AddressInterface *addrindex_get_interface( AddressIndex *addrIndex, AddressIfType ifType ) {
497 AddressInterface *retVal = NULL;
500 g_return_val_if_fail( addrIndex != NULL, NULL );
502 node = addrIndex->interfaceList;
504 AddressInterface *iface = node->data;
505 node = g_list_next( node );
506 if( iface->type == ifType ) {
514 AddressDataSource *addrindex_create_datasource() {
515 AddressDataSource *ds = NULL;
516 ds = g_new0( AddressDataSource, 1 );
517 ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
518 ADDRITEM_ID(ds) = NULL;
519 ADDRITEM_NAME(ds) = NULL;
520 ADDRITEM_PARENT(ds) = NULL;
521 ADDRITEM_SUBTYPE(ds) = 0;
522 ds->type = ADDR_IF_NONE;
523 ds->rawDataSource = NULL;
524 ds->interface = NULL;
529 * Add data source to index.
530 * Enter: addrIndex Address index object.
531 * ifType Interface type to add.
532 * dataSource Actual data source to add.
533 * Return: TRUE if data source was added.
534 * Note: The raw data object (for example, AddressBookFile or VCardFile object) should be
535 * supplied as the dataSource argument.
537 AddressDataSource *addrindex_index_add_datasource( AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource ) {
538 AddressInterface *iface;
539 AddressDataSource *ds = NULL;
541 g_return_val_if_fail( addrIndex != NULL, NULL );
542 g_return_val_if_fail( dataSource != NULL, NULL );
544 iface = addrindex_get_interface( addrIndex, ifType );
546 ds = addrindex_create_datasource();
547 ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
549 ds->rawDataSource = dataSource;
550 ds->interface = iface;
551 iface->listSource = g_list_append( iface->listSource, ds );
552 addrIndex->dirtyFlag = TRUE;
558 * Remove data source from index.
559 * Enter: addrIndex Address index object.
560 * dataSource Data source to remove.
561 * Return: Data source if removed, or NULL if data source was not found in
562 * index. Note the this object must still be freed.
564 AddressDataSource *addrindex_index_remove_datasource( AddressIndex *addrIndex, AddressDataSource *dataSource ) {
565 AddressDataSource *retVal = FALSE;
566 AddressInterface *iface;
568 g_return_val_if_fail( addrIndex != NULL, NULL );
569 g_return_val_if_fail( dataSource != NULL, NULL );
571 iface = addrindex_get_interface( addrIndex, dataSource->type );
573 iface->listSource = g_list_remove( iface->listSource, dataSource );
574 addrIndex->dirtyFlag = TRUE;
575 dataSource->interface = NULL;
581 static AddressInterface *addrindex_tag_get_interface( AddressIndex *addrIndex, gchar *tag, AddressIfType ifType ) {
582 AddressInterface *retVal = NULL;
583 GList *node = addrIndex->interfaceList;
586 AddressInterface *iface = node->data;
587 node = g_list_next( node );
589 if( strcmp( iface->listTag, tag ) == 0 ) {
595 if( iface->type == ifType ) {
604 static AddressInterface *addrindex_tag_get_datasource( AddressIndex *addrIndex, AddressIfType ifType, gchar *tag ) {
605 AddressInterface *retVal = NULL;
606 GList *node = addrIndex->interfaceList;
609 AddressInterface *iface = node->data;
610 node = g_list_next( node );
611 if( iface->type == ifType && iface->itemTag ) {
612 if( strcmp( iface->itemTag, tag ) == 0 ) {
621 /* **********************************************************************
622 * Interface XML parsing functions.
623 * ***********************************************************************
625 static void show_attribs( GList *attr ) {
627 gchar *name = ((XMLAttr *)attr->data)->name;
628 gchar *value = ((XMLAttr *)attr->data)->value;
629 printf( "\tattr value : %s :%s:\n", name, value );
630 attr = g_list_next( attr );
635 static void addrindex_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
637 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
642 static void addrindex_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
644 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
650 static void addrindex_write_attr( FILE *fp, gchar *name, gchar *value ) {
654 xml_file_put_escape_str( fp, value );
659 * Return list of name-value pairs.
661 static GList *addrindex_read_attributes( XMLFile *file ) {
668 attr = xml_get_current_tag_attr( file );
670 name = ((XMLAttr *)attr->data)->name;
671 value = ((XMLAttr *)attr->data)->value;
672 nv = g_new0( AddressIfAttrib, 1 );
673 nv->name = g_strdup( name );
674 nv->value = g_strdup( value );
675 list = g_list_append( list, nv );
676 attr = g_list_next( attr );
682 * Output name-value pairs.
684 static void addrindex_write_attributes( FILE *fp, gchar *tag, GList *list, gint lvl ) {
688 addrindex_write_elem_s( fp, lvl, tag );
692 addrindex_write_attr( fp, nv->name, nv->value );
693 node = g_list_next( node );
699 static void addrindex_print_attributes( GList *list, FILE *stream ) {
702 AddressIfAttrib *nv = node->data;
703 fprintf( stream, "%s : %s\n", nv->name, nv->value );
704 node = g_list_next( node );
708 static AddressDataSource *addrindex_parse_book( XMLFile *file ) {
709 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
710 AddressBookFile *abf;
713 abf = addrbook_create_book();
714 attr = xml_get_current_tag_attr( file );
716 gchar *name = ((XMLAttr *)attr->data)->name;
717 gchar *value = ((XMLAttr *)attr->data)->value;
718 if( strcmp( name, ATTAG_BOOK_NAME ) == 0 ) {
719 addrbook_set_name( abf, value );
721 else if( strcmp( name, ATTAG_BOOK_FILE ) == 0) {
722 addrbook_set_file( abf, value );
724 attr = g_list_next( attr );
726 ds->rawDataSource = abf;
730 static void addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
731 AddressBookFile *abf = ds->rawDataSource;
733 addrindex_write_elem_s( fp, lvl, TAG_DS_ADDRESS_BOOK );
734 addrindex_write_attr( fp, ATTAG_BOOK_NAME, abf->name );
735 addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName );
736 fputs( " />\n", fp );
740 static AddressDataSource *addrindex_parse_vcard( XMLFile *file ) {
741 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
745 vcf = vcard_create();
746 attr = xml_get_current_tag_attr( file );
748 gchar *name = ((XMLAttr *)attr->data)->name;
749 gchar *value = ((XMLAttr *)attr->data)->value;
750 if( strcmp( name, ATTAG_VCARD_NAME ) == 0 ) {
751 vcard_set_name( vcf, value );
753 else if( strcmp( name, ATTAG_VCARD_FILE ) == 0) {
754 vcard_set_file( vcf, value );
756 attr = g_list_next( attr );
758 ds->rawDataSource = vcf;
762 static void addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
763 VCardFile *vcf = ds->rawDataSource;
765 addrindex_write_elem_s( fp, lvl, TAG_DS_VCARD );
766 addrindex_write_attr( fp, ATTAG_VCARD_NAME, vcf->name );
767 addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path );
768 fputs( " />\n", fp );
773 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
774 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
778 jpf = jpilot_create();
779 attr = xml_get_current_tag_attr( file );
781 gchar *name = ((XMLAttr *)attr->data)->name;
782 gchar *value = ((XMLAttr *)attr->data)->value;
783 if( strcmp( name, ATTAG_JPILOT_NAME ) == 0 ) {
784 jpilot_set_name( jpf, value );
786 else if( strcmp( name, ATTAG_JPILOT_FILE ) == 0 ) {
787 jpilot_set_file( jpf, value );
789 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_1 ) == 0 ) {
790 jpilot_add_custom_label( jpf, value );
792 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_2 ) == 0 ) {
793 jpilot_add_custom_label( jpf, value );
795 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_3 ) == 0 ) {
796 jpilot_add_custom_label( jpf, value );
798 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_4 ) == 0 ) {
799 jpilot_add_custom_label( jpf, value );
801 attr = g_list_next( attr );
803 ds->rawDataSource = jpf;
807 static void addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
808 JPilotFile *jpf = ds->rawDataSource;
812 GList *customLbl = jpilot_get_custom_labels( jpf );
813 addrindex_write_elem_s( fp, lvl, TAG_DS_JPILOT );
814 addrindex_write_attr( fp, ATTAG_JPILOT_NAME, jpf->name );
815 addrindex_write_attr( fp, ATTAG_JPILOT_FILE, jpf->path );
820 sprintf( name, "%s%d", ATTAG_JPILOT_CUSTOM, ind );
821 addrindex_write_attr( fp, name, node->data );
823 node = g_list_next( node );
825 fputs( " />\n", fp );
829 /* Just read/write name-value pairs */
830 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
831 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
832 GList *list = addrindex_read_attributes( file );
833 ds->rawDataSource = list;
837 static void addrindex_write_jpilot( FILE *fp, AddressDataSource *ds, gint lvl ) {
838 GList *list = ds->rawDataSource;
840 addrindex_write_attributes( fp, TAG_DS_JPILOT, list, lvl );
846 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
847 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
848 SyldapServer *server;
851 server = syldap_create();
852 attr = xml_get_current_tag_attr( file );
854 gchar *name = ((XMLAttr *)attr->data)->name;
855 gchar *value = ((XMLAttr *)attr->data)->value;
856 gint ivalue = atoi( value );
857 if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
858 syldap_set_name( server, value );
860 else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
861 syldap_set_host( server, value );
863 else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
864 syldap_set_port( server, ivalue );
866 else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
867 syldap_set_base_dn( server, value );
869 else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
870 syldap_set_bind_dn( server, value );
872 else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
873 syldap_set_bind_password( server, value );
875 else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
876 syldap_set_search_criteria( server, value );
878 else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
879 syldap_set_max_entries( server, ivalue );
881 else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
882 syldap_set_timeout( server, ivalue );
884 attr = g_list_next( attr );
887 ds->rawDataSource = server;
891 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
892 SyldapServer *server = ds->rawDataSource;
896 addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP );
897 addrindex_write_attr( fp, ATTAG_LDAP_NAME, server->name );
898 addrindex_write_attr( fp, ATTAG_LDAP_HOST, server->hostName );
900 sprintf( value, "%d", server->port );
901 addrindex_write_attr( fp, ATTAG_LDAP_PORT, value );
903 addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, server->baseDN );
904 addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, server->bindDN );
905 addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, server->bindPass );
906 addrindex_write_attr( fp, ATTAG_LDAP_CRITERIA, server->searchCriteria );
908 sprintf( value, "%d", server->maxEntries );
909 addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value );
910 sprintf( value, "%d", server->timeOut );
911 addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value );
917 /* Just read/write name-value pairs */
918 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
919 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
920 GList *list = addrindex_read_attributes( file );
921 ds->rawDataSource = list;
925 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
926 GList *list = ds->rawDataSource;
928 addrindex_write_attributes( fp, TAG_DS_LDAP, list, lvl );
933 /* **********************************************************************
934 * Address index I/O functions.
935 * ***********************************************************************
937 static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
939 /* gchar *element; */
942 AddressInterface *iface = NULL, *dsIFace = NULL;
943 AddressDataSource *ds;
946 prev_level = file->level;
947 xml_parse_next_tag( file );
948 if( file->level < prev_level ) return;
950 xtag = xml_get_current_tag( file );
951 /* printf( "tag : %s\n", xtag->tag ); */
953 iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
955 addrIndex->lastType = iface->type;
956 if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
957 /* printf( "found : %s\n", iface->name ); */
960 dsIFace = addrindex_tag_get_datasource( addrIndex, addrIndex->lastType, xtag->tag );
962 /* Add data source to list */
963 /* printf( "\tdata source: %s\n", dsIFace->name ); */
965 if( addrIndex->lastType == ADDR_IF_BOOK ) {
966 ds = addrindex_parse_book( file );
967 if( ds->rawDataSource ) {
968 addrbook_set_path( ds->rawDataSource, addrIndex->filePath );
969 /* addrbook_print_book( ds->rawDataSource, stdout ); */
972 else if( addrIndex->lastType == ADDR_IF_VCARD ) {
973 ds = addrindex_parse_vcard( file );
974 /* if( ds->rawDataSource ) { */
975 /* vcard_print_file( ds->rawDataSource, stdout ); */
978 else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
979 ds = addrindex_parse_jpilot( file );
981 if( ds->rawDataSource ) {
982 jpilot_print_file( ds->rawDataSource, stdout );
983 // addrindex_print_attributes( ds->rawDataSource, stdout );
987 else if( addrIndex->lastType == ADDR_IF_LDAP ) {
988 ds = addrindex_parse_ldap( file );
990 if( ds->rawDataSource ) {
991 syldap_print_data( ds->rawDataSource, stdout );
992 // addrindex_print_attributes( ds->rawDataSource, stdout );
997 ds->type = addrIndex->lastType;
998 ds->interface = dsIFace;
999 dsIFace->listSource = g_list_append( dsIFace->listSource, ds );
1001 /* printf( "=============================\n\n" ); */
1005 element = xml_get_element( file );
1006 attr = xml_get_current_tag_attr( file );
1007 if( _interfaceLast_ && ! _interfaceLast_->legacyFlag ) {
1008 show_attribs( attr );
1009 printf( "\ttag value : %s :\n", element );
1012 addrindex_read_index( addrIndex, file );
1016 static gint addrindex_read_file( AddressIndex *addrIndex ) {
1017 XMLFile *file = NULL;
1018 gchar *fileSpec = NULL;
1020 g_return_val_if_fail( addrIndex != NULL, -1 );
1022 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1023 addrIndex->retVal = MGU_NO_FILE;
1024 file = xml_open_file( fileSpec );
1027 if( file == NULL ) {
1028 /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
1029 return addrIndex->retVal;
1032 addrIndex->retVal = MGU_BAD_FORMAT;
1033 if( xml_get_dtd( file ) == 0 ) {
1034 if( xml_parse_next_tag( file ) == 0 ) {
1035 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1036 addrindex_read_index( addrIndex, file );
1037 addrIndex->retVal = MGU_SUCCESS;
1041 xml_close_file( file );
1043 return addrIndex->retVal;
1046 static void addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1047 GList *nodeIF, *nodeDS;
1049 gint lvlItem = 1 + lvlList;
1051 nodeIF = addrIndex->interfaceList;
1053 AddressInterface *iface = nodeIF->data;
1054 if( ! iface->legacyFlag ) {
1055 nodeDS = iface->listSource;
1056 addrindex_write_elem_s( fp, lvlList, iface->listTag );
1059 AddressDataSource *ds = nodeDS->data;
1061 if( iface->type == ADDR_IF_BOOK ) {
1062 addrindex_write_book( fp, ds, lvlItem );
1064 if( iface->type == ADDR_IF_VCARD ) {
1065 addrindex_write_vcard( fp, ds, lvlItem );
1067 if( iface->type == ADDR_IF_JPILOT ) {
1068 addrindex_write_jpilot( fp, ds, lvlItem );
1070 if( iface->type == ADDR_IF_LDAP ) {
1071 addrindex_write_ldap( fp, ds, lvlItem );
1074 nodeDS = g_list_next( nodeDS );
1076 addrindex_write_elem_e( fp, lvlList, iface->listTag );
1078 nodeIF = g_list_next( nodeIF );
1083 * Write data to specified file.
1084 * Enter: addrIndex Address index object.
1085 * newFile New file name.
1086 * return: Status code, from addrIndex->retVal.
1087 * Note: File will be created in directory specified by addrIndex.
1089 gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1092 #ifndef DEV_STANDALONE
1096 g_return_val_if_fail( addrIndex != NULL, -1 );
1098 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1099 addrIndex->retVal = MGU_OPEN_FILE;
1100 #ifdef DEV_STANDALONE
1101 fp = fopen( fileSpec, "w" );
1104 fputs( "<?xml version=\"1.0\" ?>\n", fp );
1106 pfile = prefs_write_open( fileSpec );
1110 fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
1111 conv_get_current_charset_str() );
1113 addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
1116 addrindex_write_index( addrIndex, fp );
1117 addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
1119 addrIndex->retVal = MGU_SUCCESS;
1120 #ifdef DEV_STANDALONE
1123 if( prefs_write_close( pfile ) < 0 ) {
1124 addrIndex->retVal = MGU_ERROR_WRITE;
1130 return addrIndex->retVal;
1134 * Save address index data to original file.
1135 * return: Status code, from addrIndex->retVal.
1137 gint addrindex_save_data( AddressIndex *addrIndex ) {
1138 g_return_val_if_fail( addrIndex != NULL, -1 );
1140 addrIndex->retVal = MGU_NO_FILE;
1141 if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1142 if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1144 addrindex_write_to( addrIndex, addrIndex->fileName );
1145 if( addrIndex->retVal == MGU_SUCCESS ) {
1146 addrIndex->dirtyFlag = FALSE;
1148 return addrIndex->retVal;
1152 * Save all address book files which may have changed.
1153 * Return: Status code, set if there was a problem saving data.
1155 gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1156 gint retVal = MGU_SUCCESS;
1157 GList *nodeIf, *nodeDS;
1159 nodeIf = addrIndex->interfaceList;
1161 AddressInterface *iface = nodeIf->data;
1162 if( iface->type == ADDR_IF_BOOK ) {
1163 nodeDS = iface->listSource;
1165 AddressDataSource *ds = nodeDS->data;
1166 AddressBookFile *abf = ds->rawDataSource;
1167 if( abf->dirtyFlag ) {
1168 if( abf->readFlag ) {
1169 addrbook_save_data( abf );
1170 if( abf->retVal != MGU_SUCCESS ) {
1171 retVal = abf->retVal;
1175 nodeDS = g_list_next( nodeDS );
1179 nodeIf = g_list_next( nodeIf );
1185 /* **********************************************************************
1186 * Address book conversion to new format.
1187 * ***********************************************************************
1190 #define ELTAG_IF_OLD_FOLDER "folder"
1191 #define ELTAG_IF_OLD_GROUP "group"
1192 #define ELTAG_IF_OLD_ITEM "item"
1193 #define ELTAG_IF_OLD_NAME "name"
1194 #define ELTAG_IF_OLD_ADDRESS "address"
1195 #define ELTAG_IF_OLD_REMARKS "remarks"
1196 #define ATTAG_IF_OLD_NAME "name"
1198 #define TEMPNODE_ROOT 0
1199 #define TEMPNODE_FOLDER 1
1200 #define TEMPNODE_GROUP 2
1201 #define TEMPNODE_ADDRESS 3
1203 typedef struct _AddressCvt_Node AddressCvtNode;
1204 struct _AddressCvt_Node {
1213 * Parse current address item.
1215 static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1220 nn = g_new0( AddressCvtNode, 1 );
1221 nn->type = TEMPNODE_ADDRESS;
1224 level = file->level;
1227 xml_parse_next_tag(file);
1228 if (file->level < level) return nn;
1230 element = xml_get_element( file );
1231 if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1232 nn->name = g_strdup( element );
1234 if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1235 nn->address = g_strdup( element );
1237 if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1238 nn->remarks = g_strdup( element );
1240 xml_parse_next_tag(file);
1245 * Create a temporary node below specified node.
1247 static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1249 nn = g_new0( AddressCvtNode, 1 );
1251 nn->name = g_strdup( name );
1252 nn->remarks = g_strdup( rem );
1253 node->list = g_list_append( node->list, nn );
1258 * Process current temporary node.
1260 static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1263 AddressCvtNode *newNode = NULL;
1268 prev_level = file->level;
1269 xml_parse_next_tag( file );
1270 if (file->level < prev_level) return;
1274 if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1275 attr = xml_get_current_tag_attr(file);
1277 name = ((XMLAttr *)attr->data)->name;
1278 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1279 value = ((XMLAttr *)attr->data)->value;
1282 newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1283 addrindex_add_obj( file, newNode );
1286 else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1287 attr = xml_get_current_tag_attr(file);
1289 name = ((XMLAttr *)attr->data)->name;
1290 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1291 value = ((XMLAttr *)attr->data)->value;
1294 newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1295 addrindex_add_obj( file, newNode );
1297 else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
1298 newNode = addrindex_parse_item( file );
1299 node->list = g_list_append( node->list, newNode );
1302 /* printf( "invalid: !!! \n" ); */
1303 attr = xml_get_current_tag_attr( file );
1309 * Consume all nodes below current tag.
1311 static void addrindex_consume_tree( XMLFile *file ) {
1318 prev_level = file->level;
1319 xml_parse_next_tag( file );
1320 if (file->level < prev_level) return;
1322 xtag = xml_get_current_tag( file );
1323 /* printf( "tag : %s\n", xtag->tag ); */
1324 element = xml_get_element( file );
1325 attr = xml_get_current_tag_attr( file );
1326 /* show_attribs( attr ); */
1327 /* printf( "\ttag value : %s :\n", element ); */
1328 addrindex_consume_tree( file );
1333 * Print temporary tree.
1335 static void addrindex_print_node( AddressCvtNode *node, FILE *stream ) {
1338 fprintf( stream, "Node:\ttype :%d:\n", node->type );
1339 fprintf( stream, "\tname :%s:\n", node->name );
1340 fprintf( stream, "\taddr :%s:\n", node->address );
1341 fprintf( stream, "\trems :%s:\n", node->remarks );
1343 fprintf( stream, "\t--list----\n" );
1347 AddressCvtNode *lNode = list->data;
1348 list = g_list_next( list );
1349 addrindex_print_node( lNode, stream );
1351 fprintf( stream, "\t==list-%d==\n", node->type );
1355 * Free up temporary tree.
1357 static void addrindex_free_node( AddressCvtNode *node ) {
1358 GList *list = node->list;
1361 AddressCvtNode *lNode = list->data;
1362 list = g_list_next( list );
1363 addrindex_free_node( lNode );
1365 node->type = TEMPNODE_ROOT;
1366 g_free( node->name );
1367 g_free( node->address );
1368 g_free( node->remarks );
1369 g_list_free( node->list );
1374 * Process address book for specified node.
1376 static void addrindex_process_node(
1377 AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
1378 ItemGroup *parentGrp, ItemFolder *folderGrp )
1381 ItemFolder *itemFolder = NULL;
1382 ItemGroup *itemGParent = parentGrp;
1383 ItemFolder *itemGFolder = folderGrp;
1384 AddressCache *cache = abf->addressCache;
1386 if( node->type == TEMPNODE_ROOT ) {
1387 itemFolder = parent;
1389 else if( node->type == TEMPNODE_FOLDER ) {
1390 itemFolder = addritem_create_item_folder();
1391 addritem_folder_set_name( itemFolder, node->name );
1392 addrcache_id_folder( cache, itemFolder );
1393 addrcache_folder_add_folder( cache, parent, itemFolder );
1396 else if( node->type == TEMPNODE_GROUP ) {
1397 ItemGroup *itemGroup;
1400 /* Create a folder for group */
1401 fName = g_strdup_printf( "Cvt - %s", node->name );
1402 itemGFolder = addritem_create_item_folder();
1403 addritem_folder_set_name( itemGFolder, fName );
1404 addrcache_id_folder( cache, itemGFolder );
1405 addrcache_folder_add_folder( cache, parent, itemGFolder );
1408 /* Add group into folder */
1409 itemGroup = addritem_create_item_group();
1410 addritem_group_set_name( itemGroup, node->name );
1411 addrcache_id_group( cache, itemGroup );
1412 addrcache_folder_add_group( cache, itemGFolder, itemGroup );
1413 itemGParent = itemGroup;
1415 else if( node->type == TEMPNODE_ADDRESS ) {
1416 ItemPerson *itemPerson;
1417 ItemEMail *itemEMail;
1419 /* Create person and email objects */
1420 itemPerson = addritem_create_item_person();
1421 addritem_person_set_common_name( itemPerson, node->name );
1422 addrcache_id_person( cache, itemPerson );
1423 itemEMail = addritem_create_item_email();
1424 addritem_email_set_address( itemEMail, node->address );
1425 addritem_email_set_remarks( itemEMail, node->remarks );
1426 addrcache_id_email( cache, itemEMail );
1427 addrcache_person_add_email( cache, itemPerson, itemEMail );
1429 /* Add person into appropriate folder */
1431 addrcache_folder_add_person( cache, itemGFolder, itemPerson );
1434 addrcache_folder_add_person( cache, parent, itemPerson );
1437 /* Add email address only into group */
1439 addrcache_group_add_email( cache, parentGrp, itemEMail );
1445 AddressCvtNode *lNode = list->data;
1446 list = g_list_next( list );
1447 addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
1452 * Process address book to specified file number.
1454 static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
1455 gboolean retVal = FALSE;
1456 AddressBookFile *abf = NULL;
1457 AddressCvtNode *rootNode = NULL;
1458 gchar *newFile = NULL;
1459 GList *fileList = NULL;
1462 /* Setup root node */
1463 rootNode = g_new0( AddressCvtNode, 1 );
1464 rootNode->type = TEMPNODE_ROOT;
1465 rootNode->name = g_strdup( "root" );
1466 rootNode->list = NULL;
1467 addrindex_add_obj( file, rootNode );
1468 /* addrindex_print_node( rootNode, stdout ); */
1470 /* Create new address book */
1471 abf = addrbook_create_book();
1472 addrbook_set_name( abf, displayName );
1473 addrbook_set_path( abf, addrIndex->filePath );
1475 /* Determine next available file number */
1476 fileList = addrbook_get_bookfile_list( abf );
1478 fileNum = 1 + abf->maxValue;
1480 g_list_free( fileList );
1483 newFile = addrbook_gen_new_file_name( fileNum );
1485 addrbook_set_file( abf, newFile );
1488 addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
1490 /* addrbook_dump_book( abf, stdout ); */
1491 addrbook_save_data( abf );
1492 addrIndex->retVal = abf->retVal;
1493 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1495 addrbook_free_book( abf );
1497 addrindex_free_node( rootNode );
1500 /* Create entries in address index */
1502 abf = addrbook_create_book();
1503 addrbook_set_name( abf, displayName );
1504 addrbook_set_path( abf, addrIndex->filePath );
1505 addrbook_set_file( abf, newFile );
1506 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1513 * Process tree converting data.
1515 static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
1523 prev_level = file->level;
1524 xml_parse_next_tag( file );
1525 if (file->level < prev_level) return;
1527 xtag = xml_get_current_tag( file );
1528 /* printf( "tag : %d : %s\n", prev_level, xtag->tag ); */
1529 if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
1530 if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
1531 addrIndex->needsConversion = FALSE;
1532 addrIndex->wasConverted = TRUE;
1537 if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
1538 if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
1539 addrIndex->needsConversion = FALSE;
1540 addrIndex->wasConverted = TRUE;
1545 element = xml_get_element( file );
1546 attr = xml_get_current_tag_attr( file );
1547 /* show_attribs( attr ); */
1548 /* printf( "\ttag value : %s :\n", element ); */
1549 addrindex_consume_tree( file );
1553 static gint addrindex_convert_data( AddressIndex *addrIndex ) {
1554 XMLFile *file = NULL;
1557 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1558 addrIndex->retVal = MGU_NO_FILE;
1559 file = xml_open_file( fileSpec );
1562 if( file == NULL ) {
1563 /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
1564 return addrIndex->retVal;
1567 addrIndex->retVal = MGU_BAD_FORMAT;
1568 if( xml_get_dtd( file ) == 0 ) {
1569 if( xml_parse_next_tag( file ) == 0 ) {
1570 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1571 addrindex_convert_tree( addrIndex, file );
1575 xml_close_file( file );
1576 return addrIndex->retVal;
1580 * Create a new address book file.
1582 static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
1583 gboolean retVal = FALSE;
1584 AddressBookFile *abf = NULL;
1585 gchar *newFile = NULL;
1586 GList *fileList = NULL;
1589 /* Create new address book */
1590 abf = addrbook_create_book();
1591 addrbook_set_name( abf, displayName );
1592 addrbook_set_path( abf, addrIndex->filePath );
1594 /* Determine next available file number */
1595 fileList = addrbook_get_bookfile_list( abf );
1597 fileNum = 1 + abf->maxValue;
1599 g_list_free( fileList );
1602 newFile = addrbook_gen_new_file_name( fileNum );
1604 addrbook_set_file( abf, newFile );
1607 addrbook_save_data( abf );
1608 addrIndex->retVal = abf->retVal;
1609 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1610 addrbook_free_book( abf );
1613 /* Create entries in address index */
1615 abf = addrbook_create_book();
1616 addrbook_set_name( abf, displayName );
1617 addrbook_set_path( abf, addrIndex->filePath );
1618 addrbook_set_file( abf, newFile );
1619 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1626 * Read data for address index performing a conversion if necesary.
1627 * Enter: addrIndex Address index object.
1628 * return: Status code, from addrIndex->retVal.
1629 * Note: New address book files will be created in directory specified by
1630 * addrIndex. Three files will be created, for the following:
1631 * "Common addresses"
1632 * "Personal addresses"
1633 * "Gathered addresses" - a new address book.
1635 gint addrindex_read_data( AddressIndex *addrIndex ) {
1636 g_return_val_if_fail( addrIndex != NULL, -1 );
1638 addrIndex->conversionError = FALSE;
1639 addrindex_read_file( addrIndex );
1640 if( addrIndex->retVal == MGU_SUCCESS ) {
1641 if( addrIndex->needsConversion ) {
1642 if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
1643 addrIndex->conversionError = TRUE;
1646 addrIndex->conversionError = TRUE;
1649 addrIndex->dirtyFlag = TRUE;
1651 return addrIndex->retVal;
1655 * Create new address books for a new address index.
1656 * Enter: addrIndex Address index object.
1657 * return: Status code, from addrIndex->retVal.
1658 * Note: New address book files will be created in directory specified by
1659 * addrIndex. Three files will be created, for the following:
1660 * "Common addresses"
1661 * "Personal addresses"
1662 * "Gathered addresses" - a new address book.
1664 gint addrindex_create_new_books( AddressIndex *addrIndex ) {
1667 g_return_val_if_fail( addrIndex != NULL, -1 );
1669 flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
1671 flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
1672 addrIndex->dirtyFlag = TRUE;
1674 return addrIndex->retVal;
1677 /* **********************************************************************
1678 * New interface stuff.
1679 * ***********************************************************************
1683 * Return modified flag for specified data source.
1685 gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
1686 gboolean retVal = FALSE;
1687 AddressInterface *iface;
1689 if( ds == NULL ) return retVal;
1690 iface = ds->interface;
1691 if( iface == NULL ) return retVal;
1692 if( iface->getModifyFlag ) {
1693 retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
1699 * Return accessed flag for specified data source.
1701 gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
1702 gboolean retVal = FALSE;
1703 AddressInterface *iface;
1705 if( ds == NULL ) return retVal;
1706 iface = ds->interface;
1707 if( iface == NULL ) return retVal;
1708 if( iface->getAccessFlag ) {
1709 retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
1715 * Return data read flag for specified data source.
1717 gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
1718 gboolean retVal = TRUE;
1719 AddressInterface *iface;
1721 if( ds == NULL ) return retVal;
1722 iface = ds->interface;
1723 if( iface == NULL ) return retVal;
1724 if( iface->getReadFlag ) {
1725 retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
1731 * Return status code for specified data source.
1733 gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
1734 gint retVal = MGU_SUCCESS;
1735 AddressInterface *iface;
1737 if( ds == NULL ) return retVal;
1738 iface = ds->interface;
1739 if( iface == NULL ) return retVal;
1740 if( iface->getStatusCode ) {
1741 retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
1747 * Return data read flag for specified data source.
1749 gint addrindex_ds_read_data( AddressDataSource *ds ) {
1750 gint retVal = MGU_SUCCESS;
1751 AddressInterface *iface;
1753 if( ds == NULL ) return retVal;
1754 iface = ds->interface;
1755 if( iface == NULL ) return retVal;
1756 if( iface->getReadData ) {
1757 retVal = ( iface->getReadData ) ( ds->rawDataSource );
1763 * Return data read flag for specified data source.
1765 ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
1766 ItemFolder *retVal = NULL;
1767 AddressInterface *iface;
1769 if( ds == NULL ) return retVal;
1770 iface = ds->interface;
1771 if( iface == NULL ) return retVal;
1772 if( iface->getRootFolder ) {
1773 retVal = ( iface->getRootFolder ) ( ds->rawDataSource );
1779 * Return list of folders for specified data source.
1781 GList *addrindex_ds_get_list_folder( AddressDataSource *ds ) {
1782 GList *retVal = FALSE;
1783 AddressInterface *iface;
1785 if( ds == NULL ) return retVal;
1786 iface = ds->interface;
1787 if( iface == NULL ) return retVal;
1788 if( iface->getListFolder ) {
1789 retVal = ( iface->getListFolder ) ( ds->rawDataSource );
1795 * Return list of persons in root folder for specified data source.
1797 GList *addrindex_ds_get_list_person( AddressDataSource *ds ) {
1798 GList *retVal = FALSE;
1799 AddressInterface *iface;
1801 if( ds == NULL ) return retVal;
1802 iface = ds->interface;
1803 if( iface == NULL ) return retVal;
1804 if( iface->getListPerson ) {
1805 retVal = ( iface->getListPerson ) ( ds->rawDataSource );
1811 * Return name for specified data source.
1813 gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
1814 gchar *retVal = FALSE;
1815 AddressInterface *iface;
1817 if( ds == NULL ) return retVal;
1818 iface = ds->interface;
1819 if( iface == NULL ) return retVal;
1820 if( iface->getName ) {
1821 retVal = ( iface->getName ) ( ds->rawDataSource );
1827 * Set the access flag inside the data source.
1829 void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
1830 AddressInterface *iface;
1832 if( ds == NULL ) return;
1833 iface = ds->interface;
1834 if( iface == NULL ) return;
1835 if( iface->setAccessFlag ) {
1836 ( iface->setAccessFlag ) ( ds->rawDataSource, value );
1841 * Return read only flag for specified data source.
1843 gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
1844 AddressInterface *iface;
1845 if( ds == NULL ) return TRUE;
1846 iface = ds->interface;
1847 if( iface == NULL ) return TRUE;
1848 return iface->readOnly;
1852 * Return list of all persons for specified data source.
1854 GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
1855 GList *retVal = NULL;
1856 AddressInterface *iface;
1858 if( ds == NULL ) return retVal;
1859 iface = ds->interface;
1860 if( iface == NULL ) return retVal;
1861 if( iface->getAllPersons ) {
1862 retVal = ( iface->getAllPersons ) ( ds->rawDataSource );
1868 * Return list of all groups for specified data source.
1870 GList *addrindex_ds_get_all_groups( AddressDataSource *ds ) {
1871 GList *retVal = NULL;
1872 AddressInterface *iface;
1874 if( ds == NULL ) return retVal;
1875 iface = ds->interface;
1876 if( iface == NULL ) return retVal;
1877 if( iface->getAllGroups ) {
1878 retVal = ( iface->getAllGroups ) ( ds->rawDataSource );