2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2003 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"
39 #include "addrquery.h"
41 #ifndef DEV_STANDALONE
42 #include "prefs_gtk.h"
53 #include "ldapserver.h"
55 #include "ldapquery.h"
58 #define TAG_ADDRESS_INDEX "addressbook"
60 #define TAG_IF_ADDRESS_BOOK "book_list"
61 #define TAG_IF_VCARD "vcard_list"
62 #define TAG_IF_JPILOT "jpilot_list"
63 #define TAG_IF_LDAP "ldap_list"
65 #define TAG_DS_ADDRESS_BOOK "book"
66 #define TAG_DS_VCARD "vcard"
67 #define TAG_DS_JPILOT "jpilot"
68 #define TAG_DS_LDAP "server"
70 /* XML Attribute names */
71 #define ATTAG_BOOK_NAME "name"
72 #define ATTAG_BOOK_FILE "file"
74 #define ATTAG_VCARD_NAME "name"
75 #define ATTAG_VCARD_FILE "file"
77 #define ATTAG_JPILOT_NAME "name"
78 #define ATTAG_JPILOT_FILE "file"
79 #define ATTAG_JPILOT_CUSTOM_1 "custom-1"
80 #define ATTAG_JPILOT_CUSTOM_2 "custom-2"
81 #define ATTAG_JPILOT_CUSTOM_3 "custom-3"
82 #define ATTAG_JPILOT_CUSTOM_4 "custom-4"
83 #define ATTAG_JPILOT_CUSTOM "custom-"
85 #define ATTAG_LDAP_NAME "name"
86 #define ATTAG_LDAP_HOST "host"
87 #define ATTAG_LDAP_PORT "port"
88 #define ATTAG_LDAP_BASE_DN "base-dn"
89 #define ATTAG_LDAP_BIND_DN "bind-dn"
90 #define ATTAG_LDAP_BIND_PASS "bind-pass"
91 #define ATTAG_LDAP_CRITERIA "criteria"
92 #define ATTAG_LDAP_MAX_ENTRY "max-entry"
93 #define ATTAG_LDAP_TIMEOUT "timeout"
94 #define ATTAG_LDAP_MAX_AGE "max-age"
95 #define ATTAG_LDAP_DYN_SEARCH "dyn-search"
97 #define ELTAG_LDAP_ATTR_SRCH "attribute"
98 #define ATTAG_LDAP_ATTR_NAME "name"
101 #define ATTAG_LDAP_DEFAULT "default"
105 N_("Personal address")
108 #define DISP_NEW_COMMON _("Common address")
109 #define DISP_NEW_PERSONAL _("Personal address")
111 /* Old address book */
112 #define TAG_IF_OLD_COMMON "common_address"
113 #define TAG_IF_OLD_PERSONAL "personal_address"
115 #define DISP_OLD_COMMON _("Common address")
116 #define DISP_OLD_PERSONAL _("Personal address")
119 * Define attribute name-value pair.
121 typedef struct _AddressIfAttr AddressIfAttrib;
122 struct _AddressIfAttr {
128 * Define DOM fragment.
130 typedef struct _AddressIfFrag AddressIfFragment;
131 struct _AddressIfFrag {
138 * Build interface with default values.
140 * \param type Interface type.
141 * \param name Interface name.
142 * \param tagIf XML tag name for interface in address index file.
143 * \param tagDS XML tag name for datasource in address index file.
144 * \return Address interface object.
146 static AddressInterface *addrindex_create_interface(
147 gint type, gchar *name, gchar *tagIf, gchar *tagDS )
149 AddressInterface *iface = g_new0( AddressInterface, 1 );
151 ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
152 ADDRITEM_ID(iface) = NULL;
153 ADDRITEM_NAME(iface) = g_strdup( name );
154 ADDRITEM_PARENT(iface) = NULL;
155 ADDRITEM_SUBTYPE(iface) = type;
157 iface->name = g_strdup( name );
158 iface->listTag = g_strdup( tagIf );
159 iface->itemTag = g_strdup( tagDS );
160 iface->legacyFlag = FALSE;
161 iface->haveLibrary = TRUE;
162 iface->useInterface = TRUE;
163 iface->readOnly = TRUE;
165 /* Set callbacks to NULL values - override for each interface */
166 iface->getAccessFlag = NULL;
167 iface->getModifyFlag = NULL;
168 iface->getReadFlag = NULL;
169 iface->getStatusCode = NULL;
170 iface->getReadData = NULL;
171 iface->getRootFolder = NULL;
172 iface->getListFolder = NULL;
173 iface->getListPerson = NULL;
174 iface->getAllPersons = NULL;
175 iface->getAllGroups = NULL;
176 iface->getName = NULL;
177 iface->listSource = NULL;
180 iface->externalQuery = FALSE;
181 iface->searchOrder = 0; /* Ignored */
182 iface->startSearch = NULL;
183 iface->stopSearch = NULL;
189 * Build table of of all address book interfaces.
190 * \param addrIndex Address index object.
192 static void addrindex_build_if_list( AddressIndex *addrIndex ) {
193 AddressInterface *iface;
195 /* Create intrinsic XML address book interface */
196 iface = addrindex_create_interface(
197 ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK,
198 TAG_DS_ADDRESS_BOOK );
199 iface->readOnly = FALSE;
200 iface->getModifyFlag = ( void * ) addrbook_get_modified;
201 iface->getAccessFlag = ( void * ) addrbook_get_accessed;
202 iface->getReadFlag = ( void * ) addrbook_get_read_flag;
203 iface->getStatusCode = ( void * ) addrbook_get_status;
204 iface->getReadData = ( void * ) addrbook_read_data;
205 iface->getRootFolder = ( void * ) addrbook_get_root_folder;
206 iface->getListFolder = ( void * ) addrbook_get_list_folder;
207 iface->getListPerson = ( void * ) addrbook_get_list_person;
208 iface->getAllPersons = ( void * ) addrbook_get_all_persons;
209 iface->getName = ( void * ) addrbook_get_name;
210 iface->setAccessFlag = ( void * ) addrbook_set_accessed;
211 iface->searchOrder = 2;
213 /* Add to list of interfaces in address book */
214 addrIndex->interfaceList =
215 g_list_append( addrIndex->interfaceList, iface );
216 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
218 /* Create vCard interface */
219 iface = addrindex_create_interface(
220 ADDR_IF_VCARD, "vCard", TAG_IF_VCARD, TAG_DS_VCARD );
221 iface->getModifyFlag = ( void * ) vcard_get_modified;
222 iface->getAccessFlag = ( void * ) vcard_get_accessed;
223 iface->getReadFlag = ( void * ) vcard_get_read_flag;
224 iface->getStatusCode = ( void * ) vcard_get_status;
225 iface->getReadData = ( void * ) vcard_read_data;
226 iface->getRootFolder = ( void * ) vcard_get_root_folder;
227 iface->getListFolder = ( void * ) vcard_get_list_folder;
228 iface->getListPerson = ( void * ) vcard_get_list_person;
229 iface->getAllPersons = ( void * ) vcard_get_all_persons;
230 iface->getName = ( void * ) vcard_get_name;
231 iface->setAccessFlag = ( void * ) vcard_set_accessed;
232 iface->searchOrder = 3;
233 addrIndex->interfaceList =
234 g_list_append( addrIndex->interfaceList, iface );
235 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
237 /* Create JPilot interface */
238 iface = addrindex_create_interface(
239 ADDR_IF_JPILOT, "J-Pilot", TAG_IF_JPILOT,
242 iface->haveLibrary = jpilot_test_pilot_lib();
243 iface->useInterface = iface->haveLibrary;
244 iface->getModifyFlag = ( void * ) jpilot_get_modified;
245 iface->getAccessFlag = ( void * ) jpilot_get_accessed;
246 iface->getReadFlag = ( void * ) jpilot_get_read_flag;
247 iface->getStatusCode = ( void * ) jpilot_get_status;
248 iface->getReadData = ( void * ) jpilot_read_data;
249 iface->getRootFolder = ( void * ) jpilot_get_root_folder;
250 iface->getListFolder = ( void * ) jpilot_get_list_folder;
251 iface->getListPerson = ( void * ) jpilot_get_list_person;
252 iface->getAllPersons = ( void * ) jpilot_get_all_persons;
253 iface->getName = ( void * ) jpilot_get_name;
254 iface->setAccessFlag = ( void * ) jpilot_set_accessed;
255 iface->searchOrder = 3;
257 iface->useInterface = FALSE;
258 iface->haveLibrary = FALSE;
260 addrIndex->interfaceList =
261 g_list_append( addrIndex->interfaceList, iface );
262 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
264 /* Create LDAP interface */
265 iface = addrindex_create_interface(
266 ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
268 /* iface->haveLibrary = ldapsvr_test_ldap_lib(); */
269 iface->haveLibrary = ldaputil_test_ldap_lib();
270 iface->useInterface = iface->haveLibrary;
271 /* iface->getModifyFlag = ( void * ) ldapsvr_get_modified; */
272 iface->getAccessFlag = ( void * ) ldapsvr_get_accessed;
273 /* iface->getReadFlag = ( void * ) ldapsvr_get_read_flag; */
274 iface->getStatusCode = ( void * ) ldapsvr_get_status;
275 /* iface->getReadData = ( void * ) ldapsvr_read_data; */
276 iface->getRootFolder = ( void * ) ldapsvr_get_root_folder;
277 iface->getListFolder = ( void * ) ldapsvr_get_list_folder;
278 iface->getListPerson = ( void * ) ldapsvr_get_list_person;
279 iface->getName = ( void * ) ldapsvr_get_name;
280 iface->setAccessFlag = ( void * ) ldapsvr_set_accessed;
281 iface->externalQuery = TRUE;
282 iface->searchOrder = 1;
284 iface->useInterface = FALSE;
285 iface->haveLibrary = FALSE;
287 addrIndex->interfaceList =
288 g_list_append( addrIndex->interfaceList, iface );
289 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
291 /* Two old legacy data sources (pre 0.7.0) */
292 iface = addrindex_create_interface(
293 ADDR_IF_COMMON, "Old Address - common",
294 TAG_IF_OLD_COMMON, NULL );
295 iface->legacyFlag = TRUE;
296 addrIndex->interfaceList =
297 g_list_append( addrIndex->interfaceList, iface );
298 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
300 iface = addrindex_create_interface(
301 ADDR_IF_COMMON, "Old Address - personal",
302 TAG_IF_OLD_PERSONAL, NULL );
303 iface->legacyFlag = TRUE;
304 addrIndex->interfaceList =
305 g_list_append( addrIndex->interfaceList, iface );
306 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
312 * \param fragment Fragment to free.
314 static addrindex_free_fragment( AddressIfFragment *fragment ) {
318 node = fragment->children;
320 AddressIfFragment *child = node->data;
321 addrindex_free_fragment( child );
323 node = g_list_next( node );
325 g_list_free( fragment->children );
327 /* Free attributes */
328 node = fragment->attributes;
330 AddressIfAttrib *nv = node->data;
335 node = g_list_next( node );
337 g_list_free( fragment->attributes );
339 g_free( fragment->name );
340 fragment->name = NULL;
341 fragment->attributes = NULL;
342 fragment->children = NULL;
348 * Create a new data source.
349 * \param ifType Interface type to create.
350 * \return Initialized data source.
352 AddressDataSource *addrindex_create_datasource( AddressIfType ifType ) {
353 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
355 ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
356 ADDRITEM_ID(ds) = NULL;
357 ADDRITEM_NAME(ds) = NULL;
358 ADDRITEM_PARENT(ds) = NULL;
359 ADDRITEM_SUBTYPE(ds) = 0;
361 ds->rawDataSource = NULL;
362 ds->interface = NULL;
367 * Free up data source.
368 * \param ds Data source to free.
370 void addrindex_free_datasource( AddressDataSource *ds ) {
371 AddressInterface *iface;
374 g_return_if_fail( ds != NULL );
376 iface = ds->interface;
377 if( ds->rawDataSource != NULL ) {
378 if( iface != NULL ) {
379 if( iface->useInterface ) {
380 if( iface->type == ADDR_IF_BOOK ) {
381 AddressBookFile *abf = ds->rawDataSource;
382 addrbook_free_book( abf );
384 else if( iface->type == ADDR_IF_VCARD ) {
385 VCardFile *vcf = ds->rawDataSource;
389 else if( iface->type == ADDR_IF_JPILOT ) {
390 JPilotFile *jpf = ds->rawDataSource;
395 else if( iface->type == ADDR_IF_LDAP ) {
396 LdapServer *server = ds->rawDataSource;
397 cache = server->addressCache;
398 addrcache_use_index( cache, FALSE );
399 ldapsvr_free( server );
406 AddressIfFragment *fragment = ds->rawDataSource;
407 addrindex_free_fragment( fragment );
412 ADDRITEM_TYPE(ds) = ITEMTYPE_NONE;
413 ADDRITEM_ID(ds) = NULL;
414 ADDRITEM_NAME(ds) = NULL;
415 ADDRITEM_PARENT(ds) = NULL;
416 ADDRITEM_SUBTYPE(ds) = 0;
417 ds->type = ADDR_IF_NONE;
418 ds->interface = NULL;
419 ds->rawDataSource = NULL;
425 * Free up all data sources for specified interface.
426 * \param iface Address interface to process.
428 static void addrindex_free_all_datasources( AddressInterface *iface ) {
429 GList *node = iface->listSource;
431 AddressDataSource *ds = node->data;
432 addrindex_free_datasource( ds );
434 node = g_list_next( node );
439 * Free up specified interface.
440 * \param iface Interface to process.
442 static void addrindex_free_interface( AddressInterface *iface ) {
443 /* Free up data sources */
444 addrindex_free_all_datasources( iface );
445 g_list_free( iface->listSource );
447 /* Free internal storage */
448 g_free( ADDRITEM_ID(iface) );
449 g_free( ADDRITEM_NAME(iface) );
450 g_free( iface->name );
451 g_free( iface->listTag );
452 g_free( iface->itemTag );
454 /* Clear all pointers */
455 ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
456 ADDRITEM_ID(iface) = NULL;
457 ADDRITEM_NAME(iface) = NULL;
458 ADDRITEM_PARENT(iface) = NULL;
459 ADDRITEM_SUBTYPE(iface) = 0;
460 iface->type = ADDR_IF_NONE;
462 iface->listTag = NULL;
463 iface->itemTag = NULL;
464 iface->legacyFlag = FALSE;
465 iface->useInterface = FALSE;
466 iface->haveLibrary = FALSE;
467 iface->listSource = NULL;
470 iface->searchOrder = 0;
471 iface->startSearch = NULL;
472 iface->stopSearch = NULL;
478 * Return cache ID for specified data source.
480 * \param addrIndex Address index.
481 * \param ds Data source.
482 * \return ID or NULL if not found. This should be <code>g_free()</code>
485 gchar *addrindex_get_cache_id( AddressIndex *addrIndex, AddressDataSource *ds ) {
486 gchar *cacheID = NULL;
487 AddrBookBase *adbase;
490 g_return_val_if_fail( addrIndex != NULL, NULL );
491 g_return_val_if_fail( ds != NULL, NULL );
493 adbase = ( AddrBookBase * ) ds->rawDataSource;
495 cache = adbase->addressCache;
497 cacheID = g_strdup( cache->cacheID );
505 * Return reference to data source for specified cacheID.
506 * \param addrIndex Address index.
508 * \return Data source, or NULL if not found.
510 AddressDataSource *addrindex_get_datasource(
511 AddressIndex *addrIndex, const gchar *cacheID )
513 g_return_val_if_fail( addrIndex != NULL, NULL );
514 g_return_val_if_fail( cacheID != NULL, NULL );
515 return ( AddressDataSource * ) g_hash_table_lookup( addrIndex->hashCache, cacheID );
519 * Return reference to address cache for specified cacheID.
520 * \param addrIndex Address index.
522 * \return Address cache, or NULL if not found.
524 AddressCache *addrindex_get_cache( AddressIndex *addrIndex, const gchar *cacheID ) {
525 AddressDataSource *ds;
526 AddrBookBase *adbase;
529 g_return_val_if_fail( addrIndex != NULL, NULL );
530 g_return_val_if_fail( cacheID != NULL, NULL );
533 ds = addrindex_get_datasource( addrIndex, cacheID );
535 adbase = ( AddrBookBase * ) ds->rawDataSource;
536 cache = adbase->addressCache;
542 * Add data source into hash table.
543 * \param addrIndex Address index.
544 * \param ds Data source.
546 static void addrindex_hash_add_cache(
547 AddressIndex *addrIndex, AddressDataSource *ds )
551 cacheID = addrindex_get_cache_id( addrIndex, ds );
553 g_hash_table_insert( addrIndex->hashCache, cacheID, ds );
558 * Free hash table callback function.
560 static gboolean addrindex_free_cache_cb( gpointer key, gpointer value, gpointer data ) {
568 * Free hash table of address cache items.
570 static void addrindex_free_cache_hash( GHashTable *table ) {
571 g_hash_table_freeze( table );
572 g_hash_table_foreach_remove( table, addrindex_free_cache_cb, NULL );
573 g_hash_table_thaw( table );
574 g_hash_table_destroy( table );
578 * Remove data source from internal hashtable.
579 * \param addrIndex Address index.
580 * \param ds Data source to remove.
582 static void addrindex_hash_remove_cache(
583 AddressIndex *addrIndex, AddressDataSource *ds )
587 cacheID = addrindex_get_cache_id( addrIndex, ds );
589 g_hash_table_remove( addrIndex->hashCache, cacheID );
596 * Create a new address index.
597 * \return Initialized address index object.
599 AddressIndex *addrindex_create_index( void ) {
600 AddressIndex *addrIndex = g_new0( AddressIndex, 1 );
602 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_INDEX;
603 ADDRITEM_ID(addrIndex) = NULL;
604 ADDRITEM_NAME(addrIndex) = g_strdup( "Address Index" );
605 ADDRITEM_PARENT(addrIndex) = NULL;
606 ADDRITEM_SUBTYPE(addrIndex) = 0;
607 addrIndex->filePath = NULL;
608 addrIndex->fileName = NULL;
609 addrIndex->retVal = MGU_SUCCESS;
610 addrIndex->needsConversion = FALSE;
611 addrIndex->wasConverted = FALSE;
612 addrIndex->conversionError = FALSE;
613 addrIndex->interfaceList = NULL;
614 addrIndex->lastType = ADDR_IF_NONE;
615 addrIndex->dirtyFlag = FALSE;
616 addrIndex->hashCache = g_hash_table_new( g_str_hash, g_str_equal );
617 addrIndex->loadedFlag = FALSE;
618 addrIndex->searchOrder = NULL;
619 addrindex_build_if_list( addrIndex );
624 * Property - Specify file path to address index file.
625 * \param addrIndex Address index.
626 * \param value Path to index file.
628 void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
629 g_return_if_fail( addrIndex != NULL );
630 addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
634 * Property - Specify file name to address index file.
635 * \param addrIndex Address index.
636 * \param value File name.
638 void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
639 g_return_if_fail( addrIndex != NULL );
640 addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
644 * Property - Specify file path to be used.
645 * \param addrIndex Address index.
646 * \param value Path to JPilot file.
648 void addrindex_set_dirty( AddressIndex *addrIndex, const gboolean value ) {
649 g_return_if_fail( addrIndex != NULL );
650 addrIndex->dirtyFlag = value;
654 * Property - get loaded flag. Note that this flag is set after reading data
655 * from the address books.
656 * \param addrIndex Address index.
657 * \return <i>TRUE</i> if address index data was loaded.
659 gboolean addrindex_get_loaded( AddressIndex *addrIndex ) {
660 g_return_val_if_fail( addrIndex != NULL, FALSE );
661 return addrIndex->loadedFlag;
665 * Return list of address interfaces.
666 * \param addrIndex Address index.
667 * \return List of address interfaces.
669 GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
670 g_return_val_if_fail( addrIndex != NULL, NULL );
671 return addrIndex->interfaceList;
675 * Perform any other initialization of address index.
676 * \param addrIndex Address index.
678 void addrindex_initialize( AddressIndex *addrIndex ) {
679 addrcompl_initialize();
683 * Perform any other teardown of address index.
684 * \param addrIndex Address index.
686 void addrindex_teardown( AddressIndex *addrIndex ) {
687 addrcompl_teardown();
691 * Free up address index.
692 * \param addrIndex Address index.
694 void addrindex_free_index( AddressIndex *addrIndex ) {
697 g_return_if_fail( addrIndex != NULL );
700 g_list_free( addrIndex->searchOrder );
701 addrIndex->searchOrder = NULL;
703 /* Free internal storage */
704 g_free( ADDRITEM_ID(addrIndex) );
705 g_free( ADDRITEM_NAME(addrIndex) );
706 g_free( addrIndex->filePath );
707 g_free( addrIndex->fileName );
710 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
711 ADDRITEM_ID(addrIndex) = NULL;
712 ADDRITEM_NAME(addrIndex) = NULL;
713 ADDRITEM_PARENT(addrIndex) = NULL;
714 ADDRITEM_SUBTYPE(addrIndex) = 0;
715 addrIndex->filePath = NULL;
716 addrIndex->fileName = NULL;
717 addrIndex->retVal = MGU_SUCCESS;
718 addrIndex->needsConversion = FALSE;
719 addrIndex->wasConverted = FALSE;
720 addrIndex->conversionError = FALSE;
721 addrIndex->lastType = ADDR_IF_NONE;
722 addrIndex->dirtyFlag = FALSE;
724 /* Free up interfaces */
725 node = addrIndex->interfaceList;
727 AddressInterface *iface = node->data;
728 addrindex_free_interface( iface );
729 node = g_list_next( node );
731 g_list_free( addrIndex->interfaceList );
732 addrIndex->interfaceList = NULL;
734 /* Free up hash cache */
735 addrindex_free_cache_hash( addrIndex->hashCache );
736 addrIndex->hashCache = NULL;
738 addrIndex->loadedFlag = FALSE;
744 * Print address index.
745 * \param addrIndex Address index.
746 * \parem stream Stream to print.
748 void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
749 g_return_if_fail( addrIndex != NULL );
750 fprintf( stream, "AddressIndex:\n" );
751 fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
752 fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
753 fprintf( stream, "\t status: %d\n", addrIndex->retVal );
754 fprintf( stream, "\tconverted: '%s'\n",
755 addrIndex->wasConverted ? "yes" : "no" );
756 fprintf( stream, "\tcvt error: '%s'\n",
757 addrIndex->conversionError ? "yes" : "no" );
758 fprintf( stream, "\t---\n" );
762 * Retrieve reference to address interface for specified interface type.
763 * \param addrIndex Address index.
764 * \param ifType Interface type.
765 * \return Address interface, or NULL if not found.
767 AddressInterface *addrindex_get_interface(
768 AddressIndex *addrIndex, AddressIfType ifType )
770 AddressInterface *retVal = NULL;
773 g_return_val_if_fail( addrIndex != NULL, NULL );
775 node = addrIndex->interfaceList;
777 AddressInterface *iface = node->data;
778 node = g_list_next( node );
779 if( iface->type == ifType ) {
788 * Add raw data source to index. The raw data object (an AddressBookFile or
789 * VCardFile object, for example) should be supplied as the raw dataSource
792 * \param addrIndex Address index.
793 * \param ifType Interface type to add.
794 * \param dataSource Actual raw data source to add.
795 * \return Data source added, or NULL if invalid interface type.
797 AddressDataSource *addrindex_index_add_datasource(
798 AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource )
800 AddressInterface *iface;
801 AddressDataSource *ds = NULL;
803 g_return_val_if_fail( addrIndex != NULL, NULL );
804 g_return_val_if_fail( dataSource != NULL, NULL );
806 iface = addrindex_get_interface( addrIndex, ifType );
808 ds = addrindex_create_datasource( ifType );
809 ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
811 ds->rawDataSource = dataSource;
812 ds->interface = iface;
813 iface->listSource = g_list_append( iface->listSource, ds );
814 addrIndex->dirtyFlag = TRUE;
816 addrindex_hash_add_cache( addrIndex, ds );
822 * Remove specified data source from index.
823 * \param addrIndex Address index.
824 * \param dataSource Data source to add.
825 * \return Reference to data source if removed, or NULL if data source was not
826 * found in index. Note the this object must still be freed.
828 AddressDataSource *addrindex_index_remove_datasource(
829 AddressIndex *addrIndex, AddressDataSource *dataSource )
831 AddressDataSource *retVal = FALSE;
832 AddressInterface *iface;
834 g_return_val_if_fail( addrIndex != NULL, NULL );
835 g_return_val_if_fail( dataSource != NULL, NULL );
837 iface = addrindex_get_interface( addrIndex, dataSource->type );
839 iface->listSource = g_list_remove( iface->listSource, dataSource );
840 addrIndex->dirtyFlag = TRUE;
841 dataSource->interface = NULL;
843 /* Remove cache from hash table */
844 addrindex_hash_remove_cache( addrIndex, dataSource );
852 * Retrieve a reference to address interface for specified interface type and
853 * XML interface tag name.
854 * \param addrIndex Address index.
855 * \param tag XML interface tag name to match.
856 * \param ifType Interface type to match.
857 * \return Reference to address index, or NULL if not found in index.
859 static AddressInterface *addrindex_tag_get_interface(
860 AddressIndex *addrIndex, gchar *tag, AddressIfType ifType )
862 AddressInterface *retVal = NULL;
863 GList *node = addrIndex->interfaceList;
866 AddressInterface *iface = node->data;
867 node = g_list_next( node );
869 if( strcmp( iface->listTag, tag ) == 0 ) {
875 if( iface->type == ifType ) {
885 * Retrieve a reference to address interface for specified interface type and
886 * XML datasource tag name.
887 * \param addrIndex Address index.
888 * \param ifType Interface type to match.
889 * \param tag XML datasource tag name to match.
890 * \return Reference to address index, or NULL if not found in index.
892 static AddressInterface *addrindex_tag_get_datasource(
893 AddressIndex *addrIndex, AddressIfType ifType, gchar *tag )
895 AddressInterface *retVal = NULL;
896 GList *node = addrIndex->interfaceList;
899 AddressInterface *iface = node->data;
900 node = g_list_next( node );
901 if( iface->type == ifType && iface->itemTag ) {
902 if( strcmp( iface->itemTag, tag ) == 0 ) {
911 /* **********************************************************************
912 * Interface XML parsing functions.
913 * ***********************************************************************
917 * Write start of XML element to file.
919 * \param lvl Indentation level.
920 * \param name Element name.
922 static void addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name ) {
924 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
930 * Write end of XML element to file.
932 * \param lvl Indentation level.
933 * \param name Element name.
935 static void addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name ) {
937 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
944 * Write XML attribute to file.
946 * \param name Attribute name.
947 * \param value Attribute value.
949 static void addrindex_write_attr( FILE *fp, const gchar *name, const gchar *value ) {
953 xml_file_put_escape_str( fp, value );
958 * Return DOM fragment for current XML tag from file.
959 * \param file XML file being processed.
960 * \return Fragment representing DOM fragment for configuration element.
962 static AddressIfFragment *addrindex_read_fragment( XMLFile *file ) {
963 AddressIfFragment *fragment;
964 AddressIfFragment *child;
974 prevLevel = file->level;
976 /* Get current tag name */
977 xtag = xml_get_current_tag( file );
979 /* Create new fragment */
980 fragment = g_new0( AddressIfFragment, 1 );
981 fragment->name = g_strdup( xtag->tag );
982 fragment->children = NULL;
983 fragment->attributes = NULL;
985 /* Read attributes */
987 attr = xml_get_current_tag_attr( file );
989 name = ((XMLAttr *)attr->data)->name;
990 value = ((XMLAttr *)attr->data)->value;
991 nv = g_new0( AddressIfAttrib, 1 );
992 nv->name = g_strdup( name );
993 nv->value = g_strdup( value );
994 list = g_list_append( list, nv );
995 attr = g_list_next( attr );
997 fragment->attributes = list;
999 /* Now read the children */
1001 rc = xml_parse_next_tag( file );
1006 if( file->level < prevLevel ) {
1007 /* We must be above level we start at */
1010 child = addrindex_read_fragment( file );
1011 fragment->children = g_list_append( fragment->children, child );
1018 * Write DOM fragment to file.
1019 * \param fp File to write.
1020 * \param fragment DOM fragment for configuration element.
1021 * \param lvl Indent level.
1023 static void addrindex_write_fragment(
1024 FILE *fp, const AddressIfFragment *fragment, const gint lvl )
1029 addrindex_write_elem_s( fp, lvl, fragment->name );
1030 node = fragment->attributes;
1032 AddressIfAttrib *nv = node->data;
1033 addrindex_write_attr( fp, nv->name, nv->value );
1034 node = g_list_next( node );
1036 if( fragment->children ) {
1039 /* Output children */
1040 node = fragment->children;
1042 AddressIfFragment *child = node->data;
1043 addrindex_write_fragment( fp, child, 1+lvl );
1044 node = g_list_next( node );
1047 /* Output closing tag */
1048 addrindex_write_elem_e( fp, lvl, fragment->name );
1057 static void addrindex_print_fragment_r(
1058 const AddressIfFragment *fragment, FILE *stream, gint lvl )
1063 for( i = 0; i < lvl; i++ )
1064 fprintf( stream, " " );
1065 fprintf( stream, "Element:%s:\n", fragment->name );
1066 node = fragment->attributes;
1068 AddressIfAttrib *nv = node->data;
1069 for( i = 0; i < lvl; i++ )
1070 fprintf( stream, " " );
1071 fprintf( stream, " %s : %s\n", nv->name, nv->value );
1072 node = g_list_next( node );
1074 node = fragment->children;
1076 AddressIfFragment *child = node->data;
1077 addrindex_print_fragment_r( child, stream, 1+lvl );
1078 node = g_list_next( node );
1082 static void addrindex_print_fragment( const AddressIfFragment *fragment, FILE *stream ) {
1083 addrindex_print_fragment_r( fragment, stream, 0 );
1088 * Read/parse address index file, creating a data source for a regular
1089 * intrinsic XML addressbook.
1090 * \param file Address index file.
1091 * \return Data source.
1093 static AddressDataSource *addrindex_parse_book( XMLFile *file ) {
1094 AddressDataSource *ds;
1095 AddressBookFile *abf;
1098 ds = addrindex_create_datasource( ADDR_IF_BOOK );
1099 abf = addrbook_create_book();
1100 attr = xml_get_current_tag_attr( file );
1102 gchar *name = ((XMLAttr *)attr->data)->name;
1103 gchar *value = ((XMLAttr *)attr->data)->value;
1104 if( strcmp( name, ATTAG_BOOK_NAME ) == 0 ) {
1105 addrbook_set_name( abf, value );
1107 else if( strcmp( name, ATTAG_BOOK_FILE ) == 0) {
1108 addrbook_set_file( abf, value );
1110 attr = g_list_next( attr );
1112 ds->rawDataSource = abf;
1116 static void addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
1117 AddressBookFile *abf = ds->rawDataSource;
1119 addrindex_write_elem_s( fp, lvl, TAG_DS_ADDRESS_BOOK );
1120 addrindex_write_attr( fp, ATTAG_BOOK_NAME, addrbook_get_name( abf ) );
1121 addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName );
1122 fputs( " />\n", fp );
1126 static AddressDataSource *addrindex_parse_vcard( XMLFile *file ) {
1127 AddressDataSource *ds;
1131 ds = addrindex_create_datasource( ADDR_IF_VCARD );
1132 vcf = vcard_create();
1133 attr = xml_get_current_tag_attr( file );
1135 gchar *name = ((XMLAttr *)attr->data)->name;
1136 gchar *value = ((XMLAttr *)attr->data)->value;
1137 if( strcmp( name, ATTAG_VCARD_NAME ) == 0 ) {
1138 vcard_set_name( vcf, value );
1140 else if( strcmp( name, ATTAG_VCARD_FILE ) == 0) {
1141 vcard_set_file( vcf, value );
1143 attr = g_list_next( attr );
1145 ds->rawDataSource = vcf;
1149 static void addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
1150 VCardFile *vcf = ds->rawDataSource;
1152 addrindex_write_elem_s( fp, lvl, TAG_DS_VCARD );
1153 addrindex_write_attr( fp, ATTAG_VCARD_NAME, vcard_get_name( vcf ) );
1154 addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path );
1155 fputs( " />\n", fp );
1160 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
1161 AddressDataSource *ds;
1165 ds = addrindex_create_datasource( ADDR_IF_JPILOT );
1166 jpf = jpilot_create();
1167 attr = xml_get_current_tag_attr( file );
1169 gchar *name = ((XMLAttr *)attr->data)->name;
1170 gchar *value = ((XMLAttr *)attr->data)->value;
1171 if( strcmp( name, ATTAG_JPILOT_NAME ) == 0 ) {
1172 jpilot_set_name( jpf, value );
1174 else if( strcmp( name, ATTAG_JPILOT_FILE ) == 0 ) {
1175 jpilot_set_file( jpf, value );
1177 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_1 ) == 0 ) {
1178 jpilot_add_custom_label( jpf, value );
1180 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_2 ) == 0 ) {
1181 jpilot_add_custom_label( jpf, value );
1183 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_3 ) == 0 ) {
1184 jpilot_add_custom_label( jpf, value );
1186 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_4 ) == 0 ) {
1187 jpilot_add_custom_label( jpf, value );
1189 attr = g_list_next( attr );
1191 ds->rawDataSource = jpf;
1195 static void addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
1196 JPilotFile *jpf = ds->rawDataSource;
1200 GList *customLbl = jpilot_get_custom_labels( jpf );
1201 addrindex_write_elem_s( fp, lvl, TAG_DS_JPILOT );
1202 addrindex_write_attr( fp, ATTAG_JPILOT_NAME, jpilot_get_name( jpf ) );
1203 addrindex_write_attr( fp, ATTAG_JPILOT_FILE, jpf->path );
1208 g_snprintf( name, sizeof(name), "%s%d",
1209 ATTAG_JPILOT_CUSTOM, ind );
1210 addrindex_write_attr( fp, name, node->data );
1212 node = g_list_next( node );
1214 fputs( " />\n", fp );
1220 * Just read/write DOM fragments (preserve data found in file).
1222 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
1223 AddressDataSource *ds;
1225 ds = addrindex_create_datasource( ADDR_IF_JPILOT );
1226 ds->rawDataSource = addrindex_read_fragment( file );
1230 static void addrindex_write_jpilot( FILE *fp, AddressDataSource *ds, gint lvl ) {
1231 AddressIfFragment *fragment = ds->rawDataSource;
1233 addrindex_write_fragment( fp, fragment, lvl );
1240 * Parse LDAP criteria attribute data from XML file.
1241 * \param file Index file.
1242 * \param ctl LDAP control object to populate.
1244 static void addrindex_parse_ldap_attrlist( XMLFile *file, LdapControl *ctl ) {
1253 if( file == NULL ) {
1258 prevLevel = file->level;
1259 xtagPrev = xml_get_current_tag( file );
1261 rc = xml_parse_next_tag( file );
1263 /* Terminate prematurely */
1264 mgu_free_dlist( list );
1268 if( file->level < prevLevel ) {
1269 /* We must be above level we start at */
1273 /* Get a tag (element) */
1274 xtag = xml_get_current_tag( file );
1275 if( strcmp( xtag->tag, ELTAG_LDAP_ATTR_SRCH ) == 0 ) {
1276 /* LDAP criteria attribute */
1277 attr = xml_get_current_tag_attr( file );
1279 gchar *name = ((XMLAttr *)attr->data)->name;
1280 gchar *value = ((XMLAttr *)attr->data)->value;
1281 if( strcmp( name, ATTAG_LDAP_ATTR_NAME ) == 0 ) {
1282 if( value && strlen( value ) > 0 ) {
1283 list = g_list_append(
1284 list, g_strdup( value ) );
1287 attr = g_list_next( attr );
1291 if( xtag != xtagPrev ) {
1292 /* Found a new tag */
1299 /* Build list of search attributes */
1300 ldapctl_criteria_list_clear( ctl );
1303 ldapctl_criteria_list_add( ctl, node->data );
1304 g_free( node->data );
1306 node = g_list_next( node );
1308 g_list_free( list );
1313 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
1314 AddressDataSource *ds;
1318 gchar *serverName = NULL;
1319 gchar *criteria = NULL;
1320 gboolean bSearch = FALSE;
1321 gboolean cvtFlag = TRUE;
1323 ds = addrindex_create_datasource( ADDR_IF_LDAP );
1324 ctl = ldapctl_create();
1325 attr = xml_get_current_tag_attr( file );
1327 gchar *name = ((XMLAttr *)attr->data)->name;
1328 gchar *value = ((XMLAttr *)attr->data)->value;
1329 gint ivalue = atoi( value );
1331 if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
1332 if( serverName ) g_free( serverName );
1333 serverName = g_strdup( value );
1335 else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
1336 ldapctl_set_host( ctl, value );
1338 else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
1339 ldapctl_set_port( ctl, ivalue );
1341 else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
1342 ldapctl_set_base_dn( ctl, value );
1344 else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
1345 ldapctl_set_bind_dn( ctl, value );
1347 else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
1348 ldapctl_set_bind_password( ctl, value );
1350 else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
1351 if( criteria ) g_free( criteria );
1352 criteria = g_strdup( value );
1354 else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
1355 ldapctl_set_max_entries( ctl, ivalue );
1357 else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
1358 ldapctl_set_timeout( ctl, ivalue );
1360 else if( strcmp( name, ATTAG_LDAP_MAX_AGE ) == 0 ) {
1361 ldapctl_set_max_query_age( ctl, ivalue );
1363 else if( strcmp( name, ATTAG_LDAP_DYN_SEARCH ) == 0 ) {
1366 if( strcmp( value, "yes" ) == 0 ) {
1370 attr = g_list_next( attr );
1373 server = ldapsvr_create();
1374 ldapsvr_set_name( server, serverName );
1375 ldapsvr_set_search_flag( server, bSearch );
1376 g_free( serverName );
1377 ldapsvr_set_control( server, ctl );
1378 ds->rawDataSource = server;
1380 addrindex_parse_ldap_attrlist( file, ctl );
1382 * If criteria have been specified and no attributes were listed, then
1383 * convert old style criteria into an attribute list. Any criteria will
1384 * be dropped when saving data.
1387 if( ! ldapctl_get_criteria_list( ctl ) ) {
1388 ldapctl_parse_ldap_search( ctl, criteria );
1393 * If no search flag was found, then we are converting from old format
1394 * server data to new format.
1397 ldapsvr_set_search_flag( server, TRUE );
1399 /* ldapsvr_print_data( server, stdout ); */
1404 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1405 LdapServer *server = ds->rawDataSource;
1406 LdapControl *ctl = NULL;
1411 ctl = server->control;
1413 if( ctl == NULL ) return;
1415 /* Output start element with attributes */
1416 addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP );
1417 addrindex_write_attr( fp, ATTAG_LDAP_NAME, ldapsvr_get_name( server ) );
1418 addrindex_write_attr( fp, ATTAG_LDAP_HOST, ctl->hostName );
1420 sprintf( value, "%d", ctl->port );
1421 addrindex_write_attr( fp, ATTAG_LDAP_PORT, value );
1423 addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, ctl->baseDN );
1424 addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, ctl->bindDN );
1425 addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, ctl->bindPass );
1427 sprintf( value, "%d", ctl->maxEntries );
1428 addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value );
1429 sprintf( value, "%d", ctl->timeOut );
1430 addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value );
1431 sprintf( value, "%d", ctl->maxQueryAge );
1432 addrindex_write_attr( fp, ATTAG_LDAP_MAX_AGE, value );
1434 addrindex_write_attr( fp, ATTAG_LDAP_DYN_SEARCH,
1435 server->searchFlag ? "yes" : "no" );
1439 /* Output attributes */
1440 node = ldapctl_get_criteria_list( ctl );
1442 addrindex_write_elem_s( fp, 1+lvl, ELTAG_LDAP_ATTR_SRCH );
1443 addrindex_write_attr( fp, ATTAG_LDAP_ATTR_NAME, node->data );
1445 node = g_list_next( node );
1448 /* End of element */
1449 addrindex_write_elem_e( fp, lvl, TAG_DS_LDAP );
1455 * Just read/write DOM fragments (preserve data found in file).
1457 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
1458 AddressDataSource *ds;
1460 ds = addrindex_create_datasource( ADDR_IF_LDAP );
1461 ds->rawDataSource = addrindex_read_fragment( file );
1465 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1466 AddressIfFragment *fragment = ds->rawDataSource;
1468 addrindex_write_fragment( fp, fragment, lvl );
1473 /* **********************************************************************
1474 * Address index I/O functions.
1475 * ***********************************************************************
1478 * Read address index file, creating appropriate data sources for each address
1481 * \param addrIndex Address index.
1482 * \param file Address index file.
1484 static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
1487 AddressInterface *iface = NULL, *dsIFace = NULL;
1488 AddressDataSource *ds;
1491 addrIndex->loadedFlag = FALSE;
1493 prev_level = file->level;
1494 rc = xml_parse_next_tag( file );
1495 if( file->level == 0 ) return;
1497 xtag = xml_get_current_tag( file );
1499 iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
1501 addrIndex->lastType = iface->type;
1502 if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
1505 dsIFace = addrindex_tag_get_datasource(
1506 addrIndex, addrIndex->lastType, xtag->tag );
1508 /* Add data source to list */
1510 if( addrIndex->lastType == ADDR_IF_BOOK ) {
1511 ds = addrindex_parse_book( file );
1512 if( ds->rawDataSource ) {
1513 addrbook_set_path( ds->rawDataSource,
1514 addrIndex->filePath );
1517 else if( addrIndex->lastType == ADDR_IF_VCARD ) {
1518 ds = addrindex_parse_vcard( file );
1520 else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
1521 ds = addrindex_parse_jpilot( file );
1523 else if( addrIndex->lastType == ADDR_IF_LDAP ) {
1524 ds = addrindex_parse_ldap( file );
1527 ds->interface = dsIFace;
1528 addrindex_hash_add_cache( addrIndex, ds );
1529 dsIFace->listSource =
1530 g_list_append( dsIFace->listSource, ds );
1538 * Search order sorting comparison function for building search order list.
1540 static gint addrindex_search_order_compare( gconstpointer ptrA, gconstpointer ptrB ) {
1541 AddressInterface *ifaceA = ( AddressInterface * ) ptrA;
1542 AddressInterface *ifaceB = ( AddressInterface * ) ptrB;
1544 return ifaceA->searchOrder - ifaceB->searchOrder;
1548 * Build list of data sources to process.
1549 * \param addrIndex Address index object.
1551 static void addrindex_build_search_order( AddressIndex *addrIndex ) {
1552 AddressInterface *iface;
1555 /* Clear existing list */
1556 g_list_free( addrIndex->searchOrder );
1557 addrIndex->searchOrder = NULL;
1559 /* Build new list */
1560 nodeIf = addrIndex->interfaceList;
1562 AddressInterface *iface = nodeIf->data;
1563 if( iface->searchOrder > 0 ) {
1564 /* Add to search order list */
1565 addrIndex->searchOrder = g_list_insert_sorted(
1566 addrIndex->searchOrder, iface,
1567 addrindex_search_order_compare );
1569 nodeIf = g_list_next( nodeIf );
1572 nodeIf = addrIndex->searchOrder;
1574 AddressInterface *iface = nodeIf->data;
1575 nodeIf = g_list_next( nodeIf );
1580 static gint addrindex_read_file( AddressIndex *addrIndex ) {
1581 XMLFile *file = NULL;
1582 gchar *fileSpec = NULL;
1584 g_return_val_if_fail( addrIndex != NULL, -1 );
1586 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1587 addrIndex->retVal = MGU_NO_FILE;
1588 file = xml_open_file( fileSpec );
1591 if( file == NULL ) {
1593 fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName );
1595 return addrIndex->retVal;
1598 addrIndex->retVal = MGU_BAD_FORMAT;
1599 if( xml_get_dtd( file ) == 0 ) {
1600 if( xml_parse_next_tag( file ) == 0 ) {
1601 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1602 addrindex_read_index( addrIndex, file );
1603 addrIndex->retVal = MGU_SUCCESS;
1607 xml_close_file( file );
1609 addrindex_build_search_order( addrIndex );
1611 return addrIndex->retVal;
1614 static void addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1615 GList *nodeIF, *nodeDS;
1617 gint lvlItem = 1 + lvlList;
1619 nodeIF = addrIndex->interfaceList;
1621 AddressInterface *iface = nodeIF->data;
1622 if( ! iface->legacyFlag ) {
1623 nodeDS = iface->listSource;
1624 addrindex_write_elem_s( fp, lvlList, iface->listTag );
1627 AddressDataSource *ds = nodeDS->data;
1629 if( iface->type == ADDR_IF_BOOK ) {
1630 addrindex_write_book( fp, ds, lvlItem );
1632 if( iface->type == ADDR_IF_VCARD ) {
1633 addrindex_write_vcard( fp, ds, lvlItem );
1635 if( iface->type == ADDR_IF_JPILOT ) {
1636 addrindex_write_jpilot( fp, ds, lvlItem );
1638 if( iface->type == ADDR_IF_LDAP ) {
1639 addrindex_write_ldap( fp, ds, lvlItem );
1642 nodeDS = g_list_next( nodeDS );
1644 addrindex_write_elem_e( fp, lvlList, iface->listTag );
1646 nodeIF = g_list_next( nodeIF );
1651 * Write data to specified file.
1652 * Enter: addrIndex Address index object.
1653 * newFile New file name.
1654 * return: Status code, from addrIndex->retVal.
1655 * Note: File will be created in directory specified by addrIndex.
1657 gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1660 #ifndef DEV_STANDALONE
1664 g_return_val_if_fail( addrIndex != NULL, -1 );
1666 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1667 addrIndex->retVal = MGU_OPEN_FILE;
1668 #ifdef DEV_STANDALONE
1669 fp = fopen( fileSpec, "wb" );
1672 fputs( "<?xml version=\"1.0\" ?>\n", fp );
1674 pfile = prefs_write_open( fileSpec );
1678 fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
1679 conv_get_current_charset_str() );
1681 addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
1684 addrindex_write_index( addrIndex, fp );
1685 addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
1687 addrIndex->retVal = MGU_SUCCESS;
1688 #ifdef DEV_STANDALONE
1691 if( prefs_file_close( pfile ) < 0 ) {
1692 addrIndex->retVal = MGU_ERROR_WRITE;
1698 return addrIndex->retVal;
1702 * Save address index data to original file.
1703 * return: Status code, from addrIndex->retVal.
1705 gint addrindex_save_data( AddressIndex *addrIndex ) {
1706 g_return_val_if_fail( addrIndex != NULL, -1 );
1708 addrIndex->retVal = MGU_NO_FILE;
1709 if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1710 if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1712 addrindex_write_to( addrIndex, addrIndex->fileName );
1713 if( addrIndex->retVal == MGU_SUCCESS ) {
1714 addrIndex->dirtyFlag = FALSE;
1716 return addrIndex->retVal;
1720 * Save all address book files which may have changed.
1721 * Return: Status code, set if there was a problem saving data.
1723 gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1724 gint retVal = MGU_SUCCESS;
1725 GList *nodeIf, *nodeDS;
1727 nodeIf = addrIndex->interfaceList;
1729 AddressInterface *iface = nodeIf->data;
1730 if( iface->type == ADDR_IF_BOOK ) {
1731 nodeDS = iface->listSource;
1733 AddressDataSource *ds = nodeDS->data;
1734 AddressBookFile *abf = ds->rawDataSource;
1735 if( addrbook_get_dirty( abf ) ) {
1736 if( addrbook_get_read_flag( abf ) ) {
1737 addrbook_save_data( abf );
1738 if( abf->retVal != MGU_SUCCESS ) {
1739 retVal = abf->retVal;
1743 nodeDS = g_list_next( nodeDS );
1747 nodeIf = g_list_next( nodeIf );
1753 /* **********************************************************************
1754 * Address book conversion to new format.
1755 * ***********************************************************************
1758 #define ELTAG_IF_OLD_FOLDER "folder"
1759 #define ELTAG_IF_OLD_GROUP "group"
1760 #define ELTAG_IF_OLD_ITEM "item"
1761 #define ELTAG_IF_OLD_NAME "name"
1762 #define ELTAG_IF_OLD_ADDRESS "address"
1763 #define ELTAG_IF_OLD_REMARKS "remarks"
1764 #define ATTAG_IF_OLD_NAME "name"
1766 #define TEMPNODE_ROOT 0
1767 #define TEMPNODE_FOLDER 1
1768 #define TEMPNODE_GROUP 2
1769 #define TEMPNODE_ADDRESS 3
1771 typedef struct _AddressCvt_Node AddressCvtNode;
1772 struct _AddressCvt_Node {
1781 * Parse current address item.
1783 static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1788 nn = g_new0( AddressCvtNode, 1 );
1789 nn->type = TEMPNODE_ADDRESS;
1792 level = file->level;
1795 xml_parse_next_tag(file);
1796 if (file->level < level) return nn;
1798 element = xml_get_element( file );
1799 if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1800 nn->name = g_strdup( element );
1802 if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1803 nn->address = g_strdup( element );
1805 if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1806 nn->remarks = g_strdup( element );
1808 xml_parse_next_tag(file);
1813 * Create a temporary node below specified node.
1815 static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1817 nn = g_new0( AddressCvtNode, 1 );
1819 nn->name = g_strdup( name );
1820 nn->remarks = g_strdup( rem );
1821 node->list = g_list_append( node->list, nn );
1826 * Process current temporary node.
1828 static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1831 AddressCvtNode *newNode = NULL;
1836 prev_level = file->level;
1837 xml_parse_next_tag( file );
1838 if (file->level < prev_level) return;
1842 if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1843 attr = xml_get_current_tag_attr(file);
1845 name = ((XMLAttr *)attr->data)->name;
1846 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1847 value = ((XMLAttr *)attr->data)->value;
1850 newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1851 addrindex_add_obj( file, newNode );
1854 else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1855 attr = xml_get_current_tag_attr(file);
1857 name = ((XMLAttr *)attr->data)->name;
1858 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1859 value = ((XMLAttr *)attr->data)->value;
1862 newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1863 addrindex_add_obj( file, newNode );
1865 else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
1866 newNode = addrindex_parse_item( file );
1867 node->list = g_list_append( node->list, newNode );
1870 /* printf( "invalid: !!! \n" ); */
1871 attr = xml_get_current_tag_attr( file );
1877 * Consume all nodes below current tag.
1879 static void addrindex_consume_tree( XMLFile *file ) {
1886 prev_level = file->level;
1887 xml_parse_next_tag( file );
1888 if (file->level < prev_level) return;
1890 xtag = xml_get_current_tag( file );
1891 /* printf( "tag : %s\n", xtag->tag ); */
1892 element = xml_get_element( file );
1893 attr = xml_get_current_tag_attr( file );
1894 /* show_attribs( attr ); */
1895 /* printf( "\ttag value : %s :\n", element ); */
1896 addrindex_consume_tree( file );
1901 * Print temporary tree.
1903 static void addrindex_print_node( AddressCvtNode *node, FILE *stream ) {
1906 fprintf( stream, "Node:\ttype :%d:\n", node->type );
1907 fprintf( stream, "\tname :%s:\n", node->name );
1908 fprintf( stream, "\taddr :%s:\n", node->address );
1909 fprintf( stream, "\trems :%s:\n", node->remarks );
1911 fprintf( stream, "\t--list----\n" );
1915 AddressCvtNode *lNode = list->data;
1916 list = g_list_next( list );
1917 addrindex_print_node( lNode, stream );
1919 fprintf( stream, "\t==list-%d==\n", node->type );
1923 * Free up temporary tree.
1925 static void addrindex_free_node( AddressCvtNode *node ) {
1926 GList *list = node->list;
1929 AddressCvtNode *lNode = list->data;
1930 list = g_list_next( list );
1931 addrindex_free_node( lNode );
1933 node->type = TEMPNODE_ROOT;
1934 g_free( node->name );
1935 g_free( node->address );
1936 g_free( node->remarks );
1937 g_list_free( node->list );
1942 * Process address book for specified node.
1944 static void addrindex_process_node(
1945 AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
1946 ItemGroup *parentGrp, ItemFolder *folderGrp )
1949 ItemFolder *itemFolder = NULL;
1950 ItemGroup *itemGParent = parentGrp;
1951 ItemFolder *itemGFolder = folderGrp;
1952 AddressCache *cache = abf->addressCache;
1954 if( node->type == TEMPNODE_ROOT ) {
1955 itemFolder = parent;
1957 else if( node->type == TEMPNODE_FOLDER ) {
1958 itemFolder = addritem_create_item_folder();
1959 addritem_folder_set_name( itemFolder, node->name );
1960 addrcache_id_folder( cache, itemFolder );
1961 addrcache_folder_add_folder( cache, parent, itemFolder );
1964 else if( node->type == TEMPNODE_GROUP ) {
1965 ItemGroup *itemGroup;
1968 /* Create a folder for group */
1969 fName = g_strdup_printf( "Cvt - %s", node->name );
1970 itemGFolder = addritem_create_item_folder();
1971 addritem_folder_set_name( itemGFolder, fName );
1972 addrcache_id_folder( cache, itemGFolder );
1973 addrcache_folder_add_folder( cache, parent, itemGFolder );
1976 /* Add group into folder */
1977 itemGroup = addritem_create_item_group();
1978 addritem_group_set_name( itemGroup, node->name );
1979 addrcache_id_group( cache, itemGroup );
1980 addrcache_folder_add_group( cache, itemGFolder, itemGroup );
1981 itemGParent = itemGroup;
1983 else if( node->type == TEMPNODE_ADDRESS ) {
1984 ItemPerson *itemPerson;
1985 ItemEMail *itemEMail;
1987 /* Create person and email objects */
1988 itemPerson = addritem_create_item_person();
1989 addritem_person_set_common_name( itemPerson, node->name );
1990 addrcache_id_person( cache, itemPerson );
1991 itemEMail = addritem_create_item_email();
1992 addritem_email_set_address( itemEMail, node->address );
1993 addritem_email_set_remarks( itemEMail, node->remarks );
1994 addrcache_id_email( cache, itemEMail );
1995 addrcache_person_add_email( cache, itemPerson, itemEMail );
1997 /* Add person into appropriate folder */
1999 addrcache_folder_add_person( cache, itemGFolder, itemPerson );
2002 addrcache_folder_add_person( cache, parent, itemPerson );
2005 /* Add email address only into group */
2007 addrcache_group_add_email( cache, parentGrp, itemEMail );
2013 AddressCvtNode *lNode = list->data;
2014 list = g_list_next( list );
2015 addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
2020 * Process address book to specified file number.
2022 static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
2023 gboolean retVal = FALSE;
2024 AddressBookFile *abf = NULL;
2025 AddressCvtNode *rootNode = NULL;
2026 gchar *newFile = NULL;
2027 GList *fileList = NULL;
2030 /* Setup root node */
2031 rootNode = g_new0( AddressCvtNode, 1 );
2032 rootNode->type = TEMPNODE_ROOT;
2033 rootNode->name = g_strdup( "root" );
2034 rootNode->list = NULL;
2035 addrindex_add_obj( file, rootNode );
2036 /* addrindex_print_node( rootNode, stdout ); */
2038 /* Create new address book */
2039 abf = addrbook_create_book();
2040 addrbook_set_name( abf, displayName );
2041 addrbook_set_path( abf, addrIndex->filePath );
2043 /* Determine next available file number */
2044 fileList = addrbook_get_bookfile_list( abf );
2046 fileNum = 1 + abf->maxValue;
2048 g_list_free( fileList );
2051 newFile = addrbook_gen_new_file_name( fileNum );
2053 addrbook_set_file( abf, newFile );
2056 addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
2058 /* addrbook_dump_book( abf, stdout ); */
2059 addrbook_save_data( abf );
2060 addrIndex->retVal = abf->retVal;
2061 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2063 addrbook_free_book( abf );
2065 addrindex_free_node( rootNode );
2068 /* Create entries in address index */
2070 abf = addrbook_create_book();
2071 addrbook_set_name( abf, displayName );
2072 addrbook_set_path( abf, addrIndex->filePath );
2073 addrbook_set_file( abf, newFile );
2074 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2081 * Process tree converting data.
2083 static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
2091 prev_level = file->level;
2092 xml_parse_next_tag( file );
2093 if (file->level < prev_level) return;
2095 xtag = xml_get_current_tag( file );
2096 /* printf( "tag : %d : %s\n", prev_level, xtag->tag ); */
2097 if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
2098 if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
2099 addrIndex->needsConversion = FALSE;
2100 addrIndex->wasConverted = TRUE;
2105 if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
2106 if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
2107 addrIndex->needsConversion = FALSE;
2108 addrIndex->wasConverted = TRUE;
2113 element = xml_get_element( file );
2114 attr = xml_get_current_tag_attr( file );
2115 /* show_attribs( attr ); */
2116 /* printf( "\ttag value : %s :\n", element ); */
2117 addrindex_consume_tree( file );
2121 static gint addrindex_convert_data( AddressIndex *addrIndex ) {
2122 XMLFile *file = NULL;
2125 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
2126 addrIndex->retVal = MGU_NO_FILE;
2127 file = xml_open_file( fileSpec );
2130 if( file == NULL ) {
2131 /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
2132 return addrIndex->retVal;
2135 addrIndex->retVal = MGU_BAD_FORMAT;
2136 if( xml_get_dtd( file ) == 0 ) {
2137 if( xml_parse_next_tag( file ) == 0 ) {
2138 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
2139 addrindex_convert_tree( addrIndex, file );
2143 xml_close_file( file );
2144 return addrIndex->retVal;
2148 * Create a new address book file.
2150 static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
2151 gboolean retVal = FALSE;
2152 AddressBookFile *abf = NULL;
2153 gchar *newFile = NULL;
2154 GList *fileList = NULL;
2157 /* Create new address book */
2158 abf = addrbook_create_book();
2159 addrbook_set_name( abf, displayName );
2160 addrbook_set_path( abf, addrIndex->filePath );
2162 /* Determine next available file number */
2163 fileList = addrbook_get_bookfile_list( abf );
2165 fileNum = 1 + abf->maxValue;
2167 g_list_free( fileList );
2170 newFile = addrbook_gen_new_file_name( fileNum );
2172 addrbook_set_file( abf, newFile );
2175 addrbook_save_data( abf );
2176 addrIndex->retVal = abf->retVal;
2177 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2178 addrbook_free_book( abf );
2181 /* Create entries in address index */
2183 abf = addrbook_create_book();
2184 addrbook_set_name( abf, displayName );
2185 addrbook_set_path( abf, addrIndex->filePath );
2186 addrbook_set_file( abf, newFile );
2187 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2194 * Read data for address index performing a conversion if necesary.
2195 * Enter: addrIndex Address index object.
2196 * return: Status code, from addrIndex->retVal.
2197 * Note: New address book files will be created in directory specified by
2198 * addrIndex. Three files will be created, for the following:
2199 * "Common addresses"
2200 * "Personal addresses"
2201 * "Gathered addresses" - a new address book.
2203 gint addrindex_read_data( AddressIndex *addrIndex ) {
2204 g_return_val_if_fail( addrIndex != NULL, -1 );
2206 addrIndex->conversionError = FALSE;
2207 addrindex_read_file( addrIndex );
2208 if( addrIndex->retVal == MGU_SUCCESS ) {
2209 if( addrIndex->needsConversion ) {
2210 if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
2211 addrIndex->conversionError = TRUE;
2214 addrIndex->conversionError = TRUE;
2217 addrIndex->dirtyFlag = TRUE;
2219 return addrIndex->retVal;
2223 * Create new address books for a new address index.
2224 * Enter: addrIndex Address index object.
2225 * return: Status code, from addrIndex->retVal.
2226 * Note: New address book files will be created in directory specified by
2227 * addrIndex. Three files will be created, for the following:
2228 * "Common addresses"
2229 * "Personal addresses"
2230 * "Gathered addresses" - a new address book.
2232 gint addrindex_create_new_books( AddressIndex *addrIndex ) {
2235 g_return_val_if_fail( addrIndex != NULL, -1 );
2237 flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
2239 flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
2240 addrIndex->dirtyFlag = TRUE;
2242 return addrIndex->retVal;
2245 /* **********************************************************************
2246 * New interface stuff.
2247 * ***********************************************************************
2251 * Return modified flag for specified data source.
2253 gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
2254 gboolean retVal = FALSE;
2255 AddressInterface *iface;
2257 if( ds == NULL ) return retVal;
2258 iface = ds->interface;
2259 if( iface == NULL ) return retVal;
2260 if( iface->getModifyFlag ) {
2261 retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
2267 * Return accessed flag for specified data source.
2269 gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
2270 gboolean retVal = FALSE;
2271 AddressInterface *iface;
2273 if( ds == NULL ) return retVal;
2274 iface = ds->interface;
2275 if( iface == NULL ) return retVal;
2276 if( iface->getAccessFlag ) {
2277 retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
2283 * Return data read flag for specified data source.
2285 gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
2286 gboolean retVal = TRUE;
2287 AddressInterface *iface;
2289 if( ds == NULL ) return retVal;
2290 iface = ds->interface;
2291 if( iface == NULL ) return retVal;
2292 if( iface->getReadFlag ) {
2293 retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
2299 * Return status code for specified data source.
2301 gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
2302 gint retVal = MGU_SUCCESS;
2303 AddressInterface *iface;
2305 if( ds == NULL ) return retVal;
2306 iface = ds->interface;
2307 if( iface == NULL ) return retVal;
2308 if( iface->getStatusCode ) {
2309 retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
2315 * Return data read flag for specified data source.
2317 gint addrindex_ds_read_data( AddressDataSource *ds ) {
2318 gint retVal = MGU_SUCCESS;
2319 AddressInterface *iface;
2321 if( ds == NULL ) return retVal;
2322 iface = ds->interface;
2323 if( iface == NULL ) return retVal;
2324 if( iface->getReadData ) {
2325 retVal = ( iface->getReadData ) ( ds->rawDataSource );
2331 * Return data read flag for specified data source.
2333 ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
2334 ItemFolder *retVal = NULL;
2335 AddressInterface *iface;
2337 if( ds == NULL ) return retVal;
2338 iface = ds->interface;
2339 if( iface == NULL ) return retVal;
2340 if( iface->getRootFolder ) {
2341 retVal = ( iface->getRootFolder ) ( ds->rawDataSource );
2347 * Return list of folders for specified data source.
2349 GList *addrindex_ds_get_list_folder( AddressDataSource *ds ) {
2350 GList *retVal = FALSE;
2351 AddressInterface *iface;
2353 if( ds == NULL ) return retVal;
2354 iface = ds->interface;
2355 if( iface == NULL ) return retVal;
2356 if( iface->getListFolder ) {
2357 retVal = ( iface->getListFolder ) ( ds->rawDataSource );
2363 * Return list of persons in root folder for specified data source.
2365 GList *addrindex_ds_get_list_person( AddressDataSource *ds ) {
2366 GList *retVal = FALSE;
2367 AddressInterface *iface;
2369 if( ds == NULL ) return retVal;
2370 iface = ds->interface;
2371 if( iface == NULL ) return retVal;
2372 if( iface->getListPerson ) {
2373 retVal = ( iface->getListPerson ) ( ds->rawDataSource );
2379 * Return name for specified data source.
2381 gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
2382 gchar *retVal = FALSE;
2383 AddressInterface *iface;
2385 if( ds == NULL ) return retVal;
2386 iface = ds->interface;
2387 if( iface == NULL ) return retVal;
2388 if( iface->getName ) {
2389 retVal = ( iface->getName ) ( ds->rawDataSource );
2395 * Set the access flag inside the data source.
2397 void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
2398 AddressInterface *iface;
2400 if( ds == NULL ) return;
2401 iface = ds->interface;
2402 if( iface == NULL ) return;
2403 if( iface->setAccessFlag ) {
2404 ( iface->setAccessFlag ) ( ds->rawDataSource, value );
2409 * Return read only flag for specified data source.
2411 gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
2412 AddressInterface *iface;
2413 if( ds == NULL ) return TRUE;
2414 iface = ds->interface;
2415 if( iface == NULL ) return TRUE;
2416 return iface->readOnly;
2420 * Return list of all persons for specified data source.
2422 GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
2423 GList *retVal = NULL;
2424 AddressInterface *iface;
2426 if( ds == NULL ) return retVal;
2427 iface = ds->interface;
2428 if( iface == NULL ) return retVal;
2429 if( iface->getAllPersons ) {
2430 retVal = ( iface->getAllPersons ) ( ds->rawDataSource );
2436 * Return list of all groups for specified data source.
2438 GList *addrindex_ds_get_all_groups( AddressDataSource *ds ) {
2439 GList *retVal = NULL;
2440 AddressInterface *iface;
2442 if( ds == NULL ) return retVal;
2443 iface = ds->interface;
2444 if( iface == NULL ) return retVal;
2445 if( iface->getAllGroups ) {
2446 retVal = ( iface->getAllGroups ) ( ds->rawDataSource );
2451 /* **********************************************************************
2452 * Address search stuff.
2453 * ***********************************************************************
2457 * Current query ID. This is incremented for each query created.
2459 static gint _currentQueryID_ = 0;
2462 * Variables for the search that is being performed.
2464 static gchar *_searchTerm_ = NULL;
2465 static gpointer _searchTarget_ = NULL;
2466 static AddrSearchCallbackFunc *_searchCallback_ = NULL;
2469 * Setup or register the search that will be performed.
2470 * \param addrIndex Address index object.
2471 * \param searchTerm Search term. A private copy will be made.
2472 * \param target Target object that will receive data.
2473 * \param callBack Callback function.
2474 * \return ID allocated to query that will be executed.
2476 gint addrindex_setup_search(
2477 AddressIndex *addrIndex, const gchar *searchTerm,
2478 const gpointer target, AddrSearchCallbackFunc callBack )
2482 /* printf( "search term ::%s::\n", searchTerm ); */
2483 g_free( _searchTerm_ );
2484 _searchTerm_ = g_strdup( searchTerm );
2486 queryID = ++_currentQueryID_;
2487 _searchTarget_ = target;
2488 _searchCallback_ = callBack;
2489 /* printf( "query ID ::%d::\n", queryID ); */
2494 * Perform the search for specified address cache.
2495 * \param cache Cache to be searched.
2496 * \param queryID ID of search query to be executed.
2498 static void addrindex_search_cache( AddressCache *cache, const gint queryID ) {
2499 AddrCacheIndex *index;
2502 index = cache->searchIndex;
2503 if( index == NULL ) return;
2504 if( index->invalid ) {
2505 addrcache_build_index( cache );
2509 printf( "query ::%d:: searching index for ::%s::\n", queryID, _searchTerm_ );
2511 listEMail = addrcindex_search( index, _searchTerm_ );
2512 ( _searchCallback_ ) ( queryID, listEMail, _searchTarget_ );
2513 g_list_free( listEMail );
2515 /* printf( "searching index done\n" ); */
2520 * LDAP callback entry point for each address entry found.
2521 * \param qry LDAP query.
2522 * \param listEMail List of Item EMail objects found.
2524 static void addrindex_ldap_entry_cb( LdapQuery *qry, GList *listEMail ) {
2528 printf( "\naddrindex::addrindex_ldap_entry_cb ::%s::\n", qry->queryName );
2532 ItemEMail *email = node->data;
2533 /* printf( "\temail ::%s::\n", email->address ); */
2534 node = g_list_next( node );
2536 if( _searchCallback_ ) {
2537 ( _searchCallback_ ) ( qry->queryID, listEMail, _searchTarget_ );
2539 g_list_free( listEMail );
2543 * LDAP callback entry point for completion of search.
2544 * \param qry LDAP query.
2546 static void addrindex_ldap_end_cb( LdapQuery *qry ) {
2547 /* printf( "\naddrindex::addrindex_ldap_end_cb ::%s::\n", qry->queryName ); */
2551 * Return results of previous query.
2553 * \return List of ItemEMail objects.
2555 static void addrindex_ldap_use_previous( const ItemFolder *folder, const gint queryID )
2562 if( _searchCallback_ ) {
2563 node = folder->listPerson;
2565 AddrItemObject *aio = node->data;
2566 if( aio && aio->type == ITEMTYPE_PERSON ) {
2567 ItemPerson *person = node->data;
2568 nodeEM = person->listEMail;
2570 ItemEMail *email = nodeEM->data;
2571 nodeEM = g_list_next( nodeEM );
2572 listEMail = g_list_append( listEMail, email );
2575 node = g_list_next( node );
2577 ( _searchCallback_ ) ( queryID, listEMail, _searchTarget_ );
2578 g_list_free( listEMail );
2582 LdapQuery *ldapsvr_locate_query( LdapServer *server, const gchar *searchTerm );
2585 * Construct an LDAP query and initiate an LDAP search.
2586 * \param server LDAP server object.
2587 * \param queryID ID of search query to be executed.
2589 static void addrindex_search_ldap( LdapServer *server, const gint queryID ) {
2593 if( ! server->searchFlag ) return;
2594 printf( "Searching ::%s::\n", ldapsvr_get_name( server ) );
2596 /* Retire any aged queries */
2597 ldapsvr_retire_query( server );
2599 /* Test whether any queries for the same term exist */
2600 qry = ldapsvr_locate_query( server, _searchTerm_ );
2602 ItemFolder *folder = qry->folder;
2604 /* Touch query to ensure it hangs around for a bit longer */
2605 ldapqry_touch( qry );
2607 addrindex_ldap_use_previous( folder, queryID );
2612 /* Construct a query */
2613 qry = ldapqry_create();
2614 ldapqry_set_query_id( qry, queryID );
2615 ldapqry_set_search_value( qry, _searchTerm_ );
2616 ldapqry_set_query_type( qry, LDAPQUERY_DYNAMIC );
2617 ldapqry_set_callback_entry( qry, addrindex_ldap_entry_cb );
2618 ldapqry_set_callback_end( qry, addrindex_ldap_end_cb );
2620 /* Name the query */
2621 name = g_strdup_printf( "Search for '%s'", _searchTerm_ );
2622 ldapqry_set_name( qry, name );
2625 ldapsvr_add_query( server, qry );
2626 /* printf( "addrindex_search_ldap::executing dynamic search...\n" ); */
2627 ldapsvr_execute_query( server, qry );
2631 * Construct an LDAP query and initiate an LDAP search.
2632 * \param server LDAP server object to search.
2633 * \param searchTerm Search term to locate.
2634 * \param callbackEnd Function to call when search has terminated.
2637 void addrindex_search_ldap_noid(
2638 LdapServer *server, const gchar *searchTerm, void * callbackEnd )
2643 /* Construct a query */
2644 qry = ldapqry_create();
2645 ldapqry_set_search_value( qry, searchTerm );
2646 ldapqry_set_query_type( qry, LDAPQUERY_STATIC );
2647 ldapqry_set_callback_end( qry, callbackEnd );
2649 /* Name the query */
2650 name = g_strdup_printf( "Static Search for '%s'", searchTerm );
2651 ldapqry_set_name( qry, name );
2654 ldapsvr_add_query( server, qry );
2655 /* printf( "addrindex_search_ldap_noid::executing static search...\n" ); */
2656 ldapsvr_execute_query( server, qry );
2661 * Perform the previously registered search.
2662 * \param addrIndex Address index object.
2663 * \param queryID ID of search query to be executed.
2664 * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2667 gboolean addrindex_start_search( AddressIndex *addrIndex, const gint queryID ) {
2668 AddressInterface *iface;
2669 AddressDataSource *ds;
2670 AddressCache *cache;
2675 /* printf( "addrindex_start_search::%d::\n", queryID ); */
2676 nodeIf = addrIndex->searchOrder;
2678 iface = nodeIf->data;
2679 nodeIf = g_list_next( nodeIf );
2681 if( ! iface->useInterface ) {
2686 nodeDS = iface->listSource;
2689 nodeDS = g_list_next( nodeDS );
2692 if( type == ADDR_IF_BOOK ) {
2693 AddressBookFile *abf = ds->rawDataSource;
2694 cache = abf->addressCache;
2696 else if( type == ADDR_IF_VCARD ) {
2697 VCardFile *vcf = ds->rawDataSource;
2698 cache = vcf->addressCache;
2701 else if( type == ADDR_IF_JPILOT ) {
2702 JPilotFile *jpf = ds->rawDataSource;
2703 cache = jpf->addressCache;
2707 else if( type == ADDR_IF_LDAP ) {
2708 LdapServer *server = ds->rawDataSource;
2709 addrindex_search_ldap( server, queryID );
2713 addrindex_search_cache( cache, queryID );
2721 * Stop the previously registered search.
2722 * \param addrIndex Address index object.
2723 * \param queryID ID of search query to stop.
2725 void addrindex_stop_search( AddressIndex *addrIndex, const gint queryID ){
2727 AddressInterface *iface;
2728 AddressDataSource *ds;
2733 /* If query ID does not match, search has not been setup */
2734 /* if( queryID != _queryID_ ) return; */
2736 /* printf( "addrindex_stop_search::%d::\n", queryID ); */
2737 nodeIf = addrIndex->searchOrder;
2739 iface = nodeIf->data;
2740 nodeIf = g_list_next( nodeIf );
2742 if( ! iface->useInterface ) {
2747 nodeDS = iface->listSource;
2750 nodeDS = g_list_next( nodeDS );
2751 if( type == ADDR_IF_LDAP ) {
2752 LdapServer *server = ds->rawDataSource;
2753 ldapsvr_stop_all_query( server );
2761 * Read all address books that do not support dynamic queries.
2762 * \param addrIndex Address index object.
2764 void addrindex_read_all( AddressIndex *addrIndex ) {
2765 AddressInterface *iface;
2766 AddressDataSource *ds;
2770 nodeIf = addrIndex->searchOrder;
2772 iface = nodeIf->data;
2773 nodeIf = g_list_next( nodeIf );
2775 if( ! iface->useInterface ) {
2778 if( iface->externalQuery ) {
2781 nodeDS = iface->listSource;
2784 nodeDS = g_list_next( nodeDS );
2786 /* Read address book */
2787 if( addrindex_ds_get_modify_flag( ds ) ) {
2788 addrindex_ds_read_data( ds );
2792 if( ! addrindex_ds_get_read_flag( ds ) ) {
2793 addrindex_ds_read_data( ds );
2798 addrIndex->loadedFlag = TRUE;
2802 * Perform a simple search of all non-query type data sources for specified
2803 * search term. If several entries are found, only the first item is
2804 * returned. Interfaces that require a time-consuming "external query" are
2805 * ignored for this search.
2807 * \param addrIndex Address index object.
2808 * \param searchTerm Search term to find. Typically an email address.
2809 * \return List of references to zero or mail E-Mail object that was found in
2810 * the address books, or <i>NULL</i> if nothing found. This list
2811 * *SHOULD* be freed when done.
2813 GList *addrindex_quick_search_list(
2814 AddressIndex *addrIndex, const gchar *searchTerm )
2816 GList *listRet = NULL;
2818 AddressInterface *iface;
2819 AddressDataSource *ds;
2820 AddressCache *cache;
2821 AddrCacheIndex *index;
2828 nodeIf = addrIndex->searchOrder;
2830 iface = nodeIf->data;
2831 nodeIf = g_list_next( nodeIf );
2833 if( ! iface->useInterface ) {
2834 /* Ignore interfaces that don't have a library */
2837 if( iface->externalQuery ) {
2838 /* Ignore interfaces that require a "query" */
2843 nodeDS = iface->listSource;
2846 nodeDS = g_list_next( nodeDS );
2849 if( type == ADDR_IF_BOOK ) {
2850 AddressBookFile *abf = ds->rawDataSource;
2851 cache = abf->addressCache;
2853 else if( type == ADDR_IF_VCARD ) {
2854 VCardFile *vcf = ds->rawDataSource;
2855 cache = vcf->addressCache;
2858 else if( type == ADDR_IF_JPILOT ) {
2859 JPilotFile *jpf = ds->rawDataSource;
2860 cache = jpf->addressCache;
2864 index = cache->searchIndex;
2865 if( index == NULL ) {
2868 if( index->invalid ) {
2869 addrcache_build_index( cache );
2871 listEMail = addrcindex_search( index, searchTerm );
2874 email = listEMail->data;
2875 listRet = g_list_append( listRet, email );
2876 nodeEM = g_list_next( nodeEM );
2878 g_list_free( listEMail );
2886 * Perform a simple search of all non-query type data sources for specified
2887 * search term. If several entries are found, only the first item is
2888 * returned. Interfaces that require a time-consuming "external query" are
2889 * ignored for this search.
2891 * \param addrIndex Address index object.
2892 * \param searchTerm Search term to find. Typically an email address.
2893 * \return Reference to a single E-Mail object that was found in the address
2894 * book, or <i>NULL</i> if nothing found. This should *NOT* be freed
2897 ItemEMail *addrindex_quick_search_single(
2898 AddressIndex *addrIndex, const gchar *searchTerm )
2900 ItemEMail *email = NULL;
2901 AddressInterface *iface;
2902 AddressDataSource *ds;
2903 AddressCache *cache;
2904 AddrCacheIndex *index;
2910 /* printf( "addrindex_quick_search::%s::\n", searchTerm ); */
2911 nodeIf = addrIndex->searchOrder;
2913 iface = nodeIf->data;
2914 nodeIf = g_list_next( nodeIf );
2916 if( ! iface->useInterface ) {
2919 if( iface->externalQuery ) {
2924 nodeDS = iface->listSource;
2927 nodeDS = g_list_next( nodeDS );
2930 if( type == ADDR_IF_BOOK ) {
2931 AddressBookFile *abf = ds->rawDataSource;
2932 cache = abf->addressCache;
2934 else if( type == ADDR_IF_VCARD ) {
2935 VCardFile *vcf = ds->rawDataSource;
2936 cache = vcf->addressCache;
2939 else if( type == ADDR_IF_JPILOT ) {
2940 JPilotFile *jpf = ds->rawDataSource;
2941 cache = jpf->addressCache;
2945 index = cache->searchIndex;
2946 if( index == NULL ) {
2949 if( index->invalid ) {
2950 addrcache_build_index( cache );
2953 listEMail = addrcindex_search( index, searchTerm );
2955 email = listEMail->data;
2957 g_list_free( listEMail );