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"
40 #include "addr_compl.h"
42 #ifndef DEV_STANDALONE
43 #include "prefs_gtk.h"
54 #include "ldapserver.h"
56 #include "ldapquery.h"
59 #define TAG_ADDRESS_INDEX "addressbook"
61 #define TAG_IF_ADDRESS_BOOK "book_list"
62 #define TAG_IF_VCARD "vcard_list"
63 #define TAG_IF_JPILOT "jpilot_list"
64 #define TAG_IF_LDAP "ldap_list"
66 #define TAG_DS_ADDRESS_BOOK "book"
67 #define TAG_DS_VCARD "vcard"
68 #define TAG_DS_JPILOT "jpilot"
69 #define TAG_DS_LDAP "server"
71 /* XML Attribute names */
72 #define ATTAG_BOOK_NAME "name"
73 #define ATTAG_BOOK_FILE "file"
75 #define ATTAG_VCARD_NAME "name"
76 #define ATTAG_VCARD_FILE "file"
78 #define ATTAG_JPILOT_NAME "name"
79 #define ATTAG_JPILOT_FILE "file"
80 #define ATTAG_JPILOT_CUSTOM_1 "custom-1"
81 #define ATTAG_JPILOT_CUSTOM_2 "custom-2"
82 #define ATTAG_JPILOT_CUSTOM_3 "custom-3"
83 #define ATTAG_JPILOT_CUSTOM_4 "custom-4"
84 #define ATTAG_JPILOT_CUSTOM "custom-"
86 #define ATTAG_LDAP_NAME "name"
87 #define ATTAG_LDAP_HOST "host"
88 #define ATTAG_LDAP_PORT "port"
89 #define ATTAG_LDAP_BASE_DN "base-dn"
90 #define ATTAG_LDAP_BIND_DN "bind-dn"
91 #define ATTAG_LDAP_BIND_PASS "bind-pass"
92 #define ATTAG_LDAP_CRITERIA "criteria"
93 #define ATTAG_LDAP_MAX_ENTRY "max-entry"
94 #define ATTAG_LDAP_TIMEOUT "timeout"
95 #define ATTAG_LDAP_MAX_AGE "max-age"
96 #define ATTAG_LDAP_DYN_SEARCH "dyn-search"
98 #define ELTAG_LDAP_ATTR_SRCH "attribute"
99 #define ATTAG_LDAP_ATTR_NAME "name"
102 #define ATTAG_LDAP_DEFAULT "default"
106 N_("Personal address")
109 #define DISP_NEW_COMMON _("Common address")
110 #define DISP_NEW_PERSONAL _("Personal address")
112 /* Old address book */
113 #define TAG_IF_OLD_COMMON "common_address"
114 #define TAG_IF_OLD_PERSONAL "personal_address"
116 #define DISP_OLD_COMMON _("Common address")
117 #define DISP_OLD_PERSONAL _("Personal address")
120 * Define attribute name-value pair.
122 typedef struct _AddressIfAttr AddressIfAttrib;
123 struct _AddressIfAttr {
129 * Define DOM fragment.
131 typedef struct _AddressIfFrag AddressIfFragment;
132 struct _AddressIfFrag {
139 * Build interface with default values.
141 * \param type Interface type.
142 * \param name Interface name.
143 * \param tagIf XML tag name for interface in address index file.
144 * \param tagDS XML tag name for datasource in address index file.
145 * \return Address interface object.
147 static AddressInterface *addrindex_create_interface(
148 gint type, gchar *name, gchar *tagIf, gchar *tagDS )
150 AddressInterface *iface = g_new0( AddressInterface, 1 );
152 ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
153 ADDRITEM_ID(iface) = NULL;
154 ADDRITEM_NAME(iface) = g_strdup( name );
155 ADDRITEM_PARENT(iface) = NULL;
156 ADDRITEM_SUBTYPE(iface) = type;
158 iface->name = g_strdup( name );
159 iface->listTag = g_strdup( tagIf );
160 iface->itemTag = g_strdup( tagDS );
161 iface->legacyFlag = FALSE;
162 iface->haveLibrary = TRUE;
163 iface->useInterface = TRUE;
164 iface->readOnly = TRUE;
166 /* Set callbacks to NULL values - override for each interface */
167 iface->getAccessFlag = NULL;
168 iface->getModifyFlag = NULL;
169 iface->getReadFlag = NULL;
170 iface->getStatusCode = NULL;
171 iface->getReadData = NULL;
172 iface->getRootFolder = NULL;
173 iface->getListFolder = NULL;
174 iface->getListPerson = NULL;
175 iface->getAllPersons = NULL;
176 iface->getAllGroups = NULL;
177 iface->getName = NULL;
178 iface->listSource = NULL;
181 iface->externalQuery = FALSE;
182 iface->searchOrder = 0; /* Ignored */
183 iface->startSearch = NULL;
184 iface->stopSearch = NULL;
190 * Build table of of all address book interfaces.
191 * \param addrIndex Address index object.
193 static void addrindex_build_if_list( AddressIndex *addrIndex ) {
194 AddressInterface *iface;
196 /* Create intrinsic XML address book interface */
197 iface = addrindex_create_interface(
198 ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK,
199 TAG_DS_ADDRESS_BOOK );
200 iface->readOnly = FALSE;
201 iface->getModifyFlag = ( void * ) addrbook_get_modified;
202 iface->getAccessFlag = ( void * ) addrbook_get_accessed;
203 iface->getReadFlag = ( void * ) addrbook_get_read_flag;
204 iface->getStatusCode = ( void * ) addrbook_get_status;
205 iface->getReadData = ( void * ) addrbook_read_data;
206 iface->getRootFolder = ( void * ) addrbook_get_root_folder;
207 iface->getListFolder = ( void * ) addrbook_get_list_folder;
208 iface->getListPerson = ( void * ) addrbook_get_list_person;
209 iface->getAllPersons = ( void * ) addrbook_get_all_persons;
210 iface->getName = ( void * ) addrbook_get_name;
211 iface->setAccessFlag = ( void * ) addrbook_set_accessed;
212 iface->searchOrder = 0;
214 /* Add to list of interfaces in address book */
215 addrIndex->interfaceList =
216 g_list_append( addrIndex->interfaceList, iface );
217 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
219 /* Create vCard interface */
220 iface = addrindex_create_interface(
221 ADDR_IF_VCARD, "vCard", TAG_IF_VCARD, TAG_DS_VCARD );
222 iface->getModifyFlag = ( void * ) vcard_get_modified;
223 iface->getAccessFlag = ( void * ) vcard_get_accessed;
224 iface->getReadFlag = ( void * ) vcard_get_read_flag;
225 iface->getStatusCode = ( void * ) vcard_get_status;
226 iface->getReadData = ( void * ) vcard_read_data;
227 iface->getRootFolder = ( void * ) vcard_get_root_folder;
228 iface->getListFolder = ( void * ) vcard_get_list_folder;
229 iface->getListPerson = ( void * ) vcard_get_list_person;
230 iface->getAllPersons = ( void * ) vcard_get_all_persons;
231 iface->getName = ( void * ) vcard_get_name;
232 iface->setAccessFlag = ( void * ) vcard_set_accessed;
233 iface->searchOrder = 0;
234 addrIndex->interfaceList =
235 g_list_append( addrIndex->interfaceList, iface );
236 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
238 /* Create JPilot interface */
239 iface = addrindex_create_interface(
240 ADDR_IF_JPILOT, "J-Pilot", TAG_IF_JPILOT,
243 iface->haveLibrary = jpilot_test_pilot_lib();
244 iface->useInterface = iface->haveLibrary;
245 iface->getModifyFlag = ( void * ) jpilot_get_modified;
246 iface->getAccessFlag = ( void * ) jpilot_get_accessed;
247 iface->getReadFlag = ( void * ) jpilot_get_read_flag;
248 iface->getStatusCode = ( void * ) jpilot_get_status;
249 iface->getReadData = ( void * ) jpilot_read_data;
250 iface->getRootFolder = ( void * ) jpilot_get_root_folder;
251 iface->getListFolder = ( void * ) jpilot_get_list_folder;
252 iface->getListPerson = ( void * ) jpilot_get_list_person;
253 iface->getAllPersons = ( void * ) jpilot_get_all_persons;
254 iface->getName = ( void * ) jpilot_get_name;
255 iface->setAccessFlag = ( void * ) jpilot_set_accessed;
256 iface->searchOrder = 0;
258 iface->useInterface = FALSE;
259 iface->haveLibrary = FALSE;
261 addrIndex->interfaceList =
262 g_list_append( addrIndex->interfaceList, iface );
263 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
265 /* Create LDAP interface */
266 iface = addrindex_create_interface(
267 ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
269 /* iface->haveLibrary = ldapsvr_test_ldap_lib(); */
270 iface->haveLibrary = ldaputil_test_ldap_lib();
271 iface->useInterface = iface->haveLibrary;
272 /* iface->getModifyFlag = ( void * ) ldapsvr_get_modified; */
273 iface->getAccessFlag = ( void * ) ldapsvr_get_accessed;
274 /* iface->getReadFlag = ( void * ) ldapsvr_get_read_flag; */
275 iface->getStatusCode = ( void * ) ldapsvr_get_status;
276 /* iface->getReadData = ( void * ) ldapsvr_read_data; */
277 iface->getRootFolder = ( void * ) ldapsvr_get_root_folder;
278 iface->getListFolder = ( void * ) ldapsvr_get_list_folder;
279 iface->getListPerson = ( void * ) ldapsvr_get_list_person;
280 iface->getName = ( void * ) ldapsvr_get_name;
281 iface->setAccessFlag = ( void * ) ldapsvr_set_accessed;
282 iface->externalQuery = TRUE;
283 iface->searchOrder = 1;
285 iface->useInterface = FALSE;
286 iface->haveLibrary = FALSE;
288 addrIndex->interfaceList =
289 g_list_append( addrIndex->interfaceList, iface );
290 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
292 /* Two old legacy data sources (pre 0.7.0) */
293 iface = addrindex_create_interface(
294 ADDR_IF_COMMON, "Old Address - common",
295 TAG_IF_OLD_COMMON, NULL );
296 iface->legacyFlag = TRUE;
297 addrIndex->interfaceList =
298 g_list_append( addrIndex->interfaceList, iface );
299 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
301 iface = addrindex_create_interface(
302 ADDR_IF_COMMON, "Old Address - personal",
303 TAG_IF_OLD_PERSONAL, NULL );
304 iface->legacyFlag = TRUE;
305 addrIndex->interfaceList =
306 g_list_append( addrIndex->interfaceList, iface );
307 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
313 * \param fragment Fragment to free.
315 static void addrindex_free_fragment( AddressIfFragment *fragment ) {
319 node = fragment->children;
321 AddressIfFragment *child = node->data;
322 addrindex_free_fragment( child );
324 node = g_list_next( node );
326 g_list_free( fragment->children );
328 /* Free attributes */
329 node = fragment->attributes;
331 AddressIfAttrib *nv = node->data;
336 node = g_list_next( node );
338 g_list_free( fragment->attributes );
340 g_free( fragment->name );
341 fragment->name = NULL;
342 fragment->attributes = NULL;
343 fragment->children = NULL;
349 * Create a new data source.
350 * \param ifType Interface type to create.
351 * \return Initialized data source.
353 AddressDataSource *addrindex_create_datasource( AddressIfType ifType ) {
354 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
356 ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
357 ADDRITEM_ID(ds) = NULL;
358 ADDRITEM_NAME(ds) = NULL;
359 ADDRITEM_PARENT(ds) = NULL;
360 ADDRITEM_SUBTYPE(ds) = 0;
362 ds->rawDataSource = NULL;
363 ds->interface = NULL;
368 * Free up data source.
369 * \param ds Data source to free.
371 void addrindex_free_datasource( AddressDataSource *ds ) {
372 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 ldapsvr_free( server );
404 AddressIfFragment *fragment = ds->rawDataSource;
405 addrindex_free_fragment( fragment );
410 ADDRITEM_TYPE(ds) = ITEMTYPE_NONE;
411 ADDRITEM_ID(ds) = NULL;
412 ADDRITEM_NAME(ds) = NULL;
413 ADDRITEM_PARENT(ds) = NULL;
414 ADDRITEM_SUBTYPE(ds) = 0;
415 ds->type = ADDR_IF_NONE;
416 ds->interface = NULL;
417 ds->rawDataSource = NULL;
423 * Free up all data sources for specified interface.
424 * \param iface Address interface to process.
426 static void addrindex_free_all_datasources( AddressInterface *iface ) {
427 GList *node = iface->listSource;
429 AddressDataSource *ds = node->data;
430 addrindex_free_datasource( ds );
432 node = g_list_next( node );
437 * Free up specified interface.
438 * \param iface Interface to process.
440 static void addrindex_free_interface( AddressInterface *iface ) {
441 /* Free up data sources */
442 addrindex_free_all_datasources( iface );
443 g_list_free( iface->listSource );
445 /* Free internal storage */
446 g_free( ADDRITEM_ID(iface) );
447 g_free( ADDRITEM_NAME(iface) );
448 g_free( iface->name );
449 g_free( iface->listTag );
450 g_free( iface->itemTag );
452 /* Clear all pointers */
453 ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
454 ADDRITEM_ID(iface) = NULL;
455 ADDRITEM_NAME(iface) = NULL;
456 ADDRITEM_PARENT(iface) = NULL;
457 ADDRITEM_SUBTYPE(iface) = 0;
458 iface->type = ADDR_IF_NONE;
460 iface->listTag = NULL;
461 iface->itemTag = NULL;
462 iface->legacyFlag = FALSE;
463 iface->useInterface = FALSE;
464 iface->haveLibrary = FALSE;
465 iface->listSource = NULL;
468 iface->searchOrder = 0;
469 iface->startSearch = NULL;
470 iface->stopSearch = NULL;
476 * Return cache ID for specified data source.
478 * \param addrIndex Address index.
479 * \param ds Data source.
480 * \return ID or NULL if not found. This should be <code>g_free()</code>
483 gchar *addrindex_get_cache_id( AddressIndex *addrIndex, AddressDataSource *ds ) {
484 gchar *cacheID = NULL;
485 AddrBookBase *adbase;
488 g_return_val_if_fail( addrIndex != NULL, NULL );
489 g_return_val_if_fail( ds != NULL, NULL );
491 adbase = ( AddrBookBase * ) ds->rawDataSource;
493 cache = adbase->addressCache;
495 cacheID = g_strdup( cache->cacheID );
503 * Return reference to data source for specified cacheID.
504 * \param addrIndex Address index.
506 * \return Data source, or NULL if not found.
508 AddressDataSource *addrindex_get_datasource(
509 AddressIndex *addrIndex, const gchar *cacheID )
511 g_return_val_if_fail( addrIndex != NULL, NULL );
512 g_return_val_if_fail( cacheID != NULL, NULL );
513 return ( AddressDataSource * ) g_hash_table_lookup( addrIndex->hashCache, cacheID );
517 * Return reference to address cache for specified cacheID.
518 * \param addrIndex Address index.
520 * \return Address cache, or NULL if not found.
522 AddressCache *addrindex_get_cache( AddressIndex *addrIndex, const gchar *cacheID ) {
523 AddressDataSource *ds;
524 AddrBookBase *adbase;
527 g_return_val_if_fail( addrIndex != NULL, NULL );
528 g_return_val_if_fail( cacheID != NULL, NULL );
531 ds = addrindex_get_datasource( addrIndex, cacheID );
533 adbase = ( AddrBookBase * ) ds->rawDataSource;
534 cache = adbase->addressCache;
540 * Add data source into hash table.
541 * \param addrIndex Address index.
542 * \param ds Data source.
544 static void addrindex_hash_add_cache(
545 AddressIndex *addrIndex, AddressDataSource *ds )
549 cacheID = addrindex_get_cache_id( addrIndex, ds );
551 g_hash_table_insert( addrIndex->hashCache, cacheID, ds );
556 * Free hash table callback function.
558 static gboolean addrindex_free_cache_cb( gpointer key, gpointer value, gpointer data ) {
566 * Free hash table of address cache items.
568 static void addrindex_free_cache_hash( GHashTable *table ) {
569 g_hash_table_freeze( table );
570 g_hash_table_foreach_remove( table, addrindex_free_cache_cb, NULL );
571 g_hash_table_thaw( table );
572 g_hash_table_destroy( table );
576 * Remove data source from internal hashtable.
577 * \param addrIndex Address index.
578 * \param ds Data source to remove.
580 static void addrindex_hash_remove_cache(
581 AddressIndex *addrIndex, AddressDataSource *ds )
585 cacheID = addrindex_get_cache_id( addrIndex, ds );
587 g_hash_table_remove( addrIndex->hashCache, cacheID );
594 * Create a new address index.
595 * \return Initialized address index object.
597 AddressIndex *addrindex_create_index( void ) {
598 AddressIndex *addrIndex = g_new0( AddressIndex, 1 );
600 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_INDEX;
601 ADDRITEM_ID(addrIndex) = NULL;
602 ADDRITEM_NAME(addrIndex) = g_strdup( "Address Index" );
603 ADDRITEM_PARENT(addrIndex) = NULL;
604 ADDRITEM_SUBTYPE(addrIndex) = 0;
605 addrIndex->filePath = NULL;
606 addrIndex->fileName = NULL;
607 addrIndex->retVal = MGU_SUCCESS;
608 addrIndex->needsConversion = FALSE;
609 addrIndex->wasConverted = FALSE;
610 addrIndex->conversionError = FALSE;
611 addrIndex->interfaceList = NULL;
612 addrIndex->lastType = ADDR_IF_NONE;
613 addrIndex->dirtyFlag = FALSE;
614 addrIndex->hashCache = g_hash_table_new( g_str_hash, g_str_equal );
615 addrIndex->loadedFlag = FALSE;
616 addrIndex->searchOrder = NULL;
617 addrindex_build_if_list( addrIndex );
622 * Property - Specify file path to address index file.
623 * \param addrIndex Address index.
624 * \param value Path to index file.
626 void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
627 g_return_if_fail( addrIndex != NULL );
628 addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
632 * Property - Specify file name to address index file.
633 * \param addrIndex Address index.
634 * \param value File name.
636 void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
637 g_return_if_fail( addrIndex != NULL );
638 addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
642 * Property - Specify file path to be used.
643 * \param addrIndex Address index.
644 * \param value Path to JPilot file.
646 void addrindex_set_dirty( AddressIndex *addrIndex, const gboolean value ) {
647 g_return_if_fail( addrIndex != NULL );
648 addrIndex->dirtyFlag = value;
652 * Property - get loaded flag. Note that this flag is set after reading data
653 * from the address books.
654 * \param addrIndex Address index.
655 * \return <i>TRUE</i> if address index data was loaded.
657 gboolean addrindex_get_loaded( AddressIndex *addrIndex ) {
658 g_return_val_if_fail( addrIndex != NULL, FALSE );
659 return addrIndex->loadedFlag;
663 * Return list of address interfaces.
664 * \param addrIndex Address index.
665 * \return List of address interfaces.
667 GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
668 g_return_val_if_fail( addrIndex != NULL, NULL );
669 return addrIndex->interfaceList;
673 * Perform any other initialization of address index.
674 * \param addrIndex Address index.
676 void addrindex_initialize( AddressIndex *addrIndex ) {
677 addrcompl_initialize( addrIndex );
681 * Perform any other teardown of address index.
682 * \param addrIndex Address index.
684 void addrindex_teardown( AddressIndex *addrIndex ) {
685 addrcompl_teardown();
689 * Free up address index.
690 * \param addrIndex Address index.
692 void addrindex_free_index( AddressIndex *addrIndex ) {
695 g_return_if_fail( addrIndex != NULL );
698 g_list_free( addrIndex->searchOrder );
699 addrIndex->searchOrder = NULL;
701 /* Free internal storage */
702 g_free( ADDRITEM_ID(addrIndex) );
703 g_free( ADDRITEM_NAME(addrIndex) );
704 g_free( addrIndex->filePath );
705 g_free( addrIndex->fileName );
708 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
709 ADDRITEM_ID(addrIndex) = NULL;
710 ADDRITEM_NAME(addrIndex) = NULL;
711 ADDRITEM_PARENT(addrIndex) = NULL;
712 ADDRITEM_SUBTYPE(addrIndex) = 0;
713 addrIndex->filePath = NULL;
714 addrIndex->fileName = NULL;
715 addrIndex->retVal = MGU_SUCCESS;
716 addrIndex->needsConversion = FALSE;
717 addrIndex->wasConverted = FALSE;
718 addrIndex->conversionError = FALSE;
719 addrIndex->lastType = ADDR_IF_NONE;
720 addrIndex->dirtyFlag = FALSE;
722 /* Free up interfaces */
723 node = addrIndex->interfaceList;
725 AddressInterface *iface = node->data;
726 addrindex_free_interface( iface );
727 node = g_list_next( node );
729 g_list_free( addrIndex->interfaceList );
730 addrIndex->interfaceList = NULL;
732 /* Free up hash cache */
733 addrindex_free_cache_hash( addrIndex->hashCache );
734 addrIndex->hashCache = NULL;
736 addrIndex->loadedFlag = FALSE;
742 * Print address index.
743 * \param addrIndex Address index.
744 * \parem stream Stream to print.
746 void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
747 g_return_if_fail( addrIndex != NULL );
748 fprintf( stream, "AddressIndex:\n" );
749 fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
750 fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
751 fprintf( stream, "\t status: %d\n", addrIndex->retVal );
752 fprintf( stream, "\tconverted: '%s'\n",
753 addrIndex->wasConverted ? "yes" : "no" );
754 fprintf( stream, "\tcvt error: '%s'\n",
755 addrIndex->conversionError ? "yes" : "no" );
756 fprintf( stream, "\t---\n" );
760 * Retrieve reference to address interface for specified interface type.
761 * \param addrIndex Address index.
762 * \param ifType Interface type.
763 * \return Address interface, or NULL if not found.
765 AddressInterface *addrindex_get_interface(
766 AddressIndex *addrIndex, AddressIfType ifType )
768 AddressInterface *retVal = NULL;
771 g_return_val_if_fail( addrIndex != NULL, NULL );
773 node = addrIndex->interfaceList;
775 AddressInterface *iface = node->data;
776 node = g_list_next( node );
777 if( iface->type == ifType ) {
786 * Add raw data source to index. The raw data object (an AddressBookFile or
787 * VCardFile object, for example) should be supplied as the raw dataSource
790 * \param addrIndex Address index.
791 * \param ifType Interface type to add.
792 * \param dataSource Actual raw data source to add.
793 * \return Data source added, or NULL if invalid interface type.
795 AddressDataSource *addrindex_index_add_datasource(
796 AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource )
798 AddressInterface *iface;
799 AddressDataSource *ds = NULL;
801 g_return_val_if_fail( addrIndex != NULL, NULL );
802 g_return_val_if_fail( dataSource != NULL, NULL );
804 iface = addrindex_get_interface( addrIndex, ifType );
806 ds = addrindex_create_datasource( ifType );
807 ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
809 ds->rawDataSource = dataSource;
810 ds->interface = iface;
811 iface->listSource = g_list_append( iface->listSource, ds );
812 addrIndex->dirtyFlag = TRUE;
814 addrindex_hash_add_cache( addrIndex, ds );
820 * Remove specified data source from index.
821 * \param addrIndex Address index.
822 * \param dataSource Data source to add.
823 * \return Reference to data source if removed, or NULL if data source was not
824 * found in index. Note the this object must still be freed.
826 AddressDataSource *addrindex_index_remove_datasource(
827 AddressIndex *addrIndex, AddressDataSource *dataSource )
829 AddressDataSource *retVal = FALSE;
830 AddressInterface *iface;
832 g_return_val_if_fail( addrIndex != NULL, NULL );
833 g_return_val_if_fail( dataSource != NULL, NULL );
835 iface = addrindex_get_interface( addrIndex, dataSource->type );
837 iface->listSource = g_list_remove( iface->listSource, dataSource );
838 addrIndex->dirtyFlag = TRUE;
839 dataSource->interface = NULL;
841 /* Remove cache from hash table */
842 addrindex_hash_remove_cache( addrIndex, dataSource );
850 * Retrieve a reference to address interface for specified interface type and
851 * XML interface tag name.
852 * \param addrIndex Address index.
853 * \param tag XML interface tag name to match.
854 * \param ifType Interface type to match.
855 * \return Reference to address index, or NULL if not found in index.
857 static AddressInterface *addrindex_tag_get_interface(
858 AddressIndex *addrIndex, gchar *tag, AddressIfType ifType )
860 AddressInterface *retVal = NULL;
861 GList *node = addrIndex->interfaceList;
864 AddressInterface *iface = node->data;
865 node = g_list_next( node );
867 if( strcmp( iface->listTag, tag ) == 0 ) {
873 if( iface->type == ifType ) {
883 * Retrieve a reference to address interface for specified interface type and
884 * XML datasource tag name.
885 * \param addrIndex Address index.
886 * \param ifType Interface type to match.
887 * \param tag XML datasource tag name to match.
888 * \return Reference to address index, or NULL if not found in index.
890 static AddressInterface *addrindex_tag_get_datasource(
891 AddressIndex *addrIndex, AddressIfType ifType, gchar *tag )
893 AddressInterface *retVal = NULL;
894 GList *node = addrIndex->interfaceList;
897 AddressInterface *iface = node->data;
898 node = g_list_next( node );
899 if( iface->type == ifType && iface->itemTag ) {
900 if( strcmp( iface->itemTag, tag ) == 0 ) {
909 /* **********************************************************************
910 * Interface XML parsing functions.
911 * ***********************************************************************
915 * Write start of XML element to file.
917 * \param lvl Indentation level.
918 * \param name Element name.
920 static void addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name ) {
922 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
928 * Write end of XML element to file.
930 * \param lvl Indentation level.
931 * \param name Element name.
933 static void addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name ) {
935 for( i = 0; i < lvl; i++ ) fputs( " ", fp );
942 * Write XML attribute to file.
944 * \param name Attribute name.
945 * \param value Attribute value.
947 static void addrindex_write_attr( FILE *fp, const gchar *name, const gchar *value ) {
951 xml_file_put_escape_str( fp, value );
956 * Return DOM fragment for current XML tag from file.
957 * \param file XML file being processed.
958 * \return Fragment representing DOM fragment for configuration element.
960 static AddressIfFragment *addrindex_read_fragment( XMLFile *file ) {
961 AddressIfFragment *fragment;
962 AddressIfFragment *child;
972 /* printf( "addrindex_read_fragment\n" ); */
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 /* printf( "addrindex_parse_ldap\n" ); */
1325 ds = addrindex_create_datasource( ADDR_IF_LDAP );
1326 ctl = ldapctl_create();
1327 attr = xml_get_current_tag_attr( file );
1329 gchar *name = ((XMLAttr *)attr->data)->name;
1330 gchar *value = ((XMLAttr *)attr->data)->value;
1331 gint ivalue = atoi( value );
1333 if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
1334 if( serverName ) g_free( serverName );
1335 serverName = g_strdup( value );
1337 else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
1338 ldapctl_set_host( ctl, value );
1340 else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
1341 ldapctl_set_port( ctl, ivalue );
1343 else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
1344 ldapctl_set_base_dn( ctl, value );
1346 else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
1347 ldapctl_set_bind_dn( ctl, value );
1349 else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
1350 ldapctl_set_bind_password( ctl, value );
1352 else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
1353 if( criteria ) g_free( criteria );
1354 criteria = g_strdup( value );
1356 else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
1357 ldapctl_set_max_entries( ctl, ivalue );
1359 else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
1360 ldapctl_set_timeout( ctl, ivalue );
1362 else if( strcmp( name, ATTAG_LDAP_MAX_AGE ) == 0 ) {
1363 ldapctl_set_max_query_age( ctl, ivalue );
1365 else if( strcmp( name, ATTAG_LDAP_DYN_SEARCH ) == 0 ) {
1368 if( strcmp( value, "yes" ) == 0 ) {
1372 attr = g_list_next( attr );
1375 server = ldapsvr_create_noctl();
1376 ldapsvr_set_name( server, serverName );
1377 ldapsvr_set_search_flag( server, bSearch );
1378 g_free( serverName );
1379 ldapsvr_set_control( server, ctl );
1380 ds->rawDataSource = server;
1382 addrindex_parse_ldap_attrlist( file, ctl );
1384 * If criteria have been specified and no attributes were listed, then
1385 * convert old style criteria into an attribute list. Any criteria will
1386 * be dropped when saving data.
1389 if( ! ldapctl_get_criteria_list( ctl ) ) {
1390 ldapctl_parse_ldap_search( ctl, criteria );
1395 * If no search flag was found, then we are converting from old format
1396 * server data to new format.
1399 ldapsvr_set_search_flag( server, TRUE );
1401 /* ldapsvr_print_data( server, stdout ); */
1406 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1407 LdapServer *server = ds->rawDataSource;
1408 LdapControl *ctl = NULL;
1413 ctl = server->control;
1415 if( ctl == NULL ) return;
1417 /* Output start element with attributes */
1418 addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP );
1419 addrindex_write_attr( fp, ATTAG_LDAP_NAME, ldapsvr_get_name( server ) );
1420 addrindex_write_attr( fp, ATTAG_LDAP_HOST, ctl->hostName );
1422 sprintf( value, "%d", ctl->port );
1423 addrindex_write_attr( fp, ATTAG_LDAP_PORT, value );
1425 addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, ctl->baseDN );
1426 addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, ctl->bindDN );
1427 addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, ctl->bindPass );
1429 sprintf( value, "%d", ctl->maxEntries );
1430 addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value );
1431 sprintf( value, "%d", ctl->timeOut );
1432 addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value );
1433 sprintf( value, "%d", ctl->maxQueryAge );
1434 addrindex_write_attr( fp, ATTAG_LDAP_MAX_AGE, value );
1436 addrindex_write_attr( fp, ATTAG_LDAP_DYN_SEARCH,
1437 server->searchFlag ? "yes" : "no" );
1441 /* Output attributes */
1442 node = ldapctl_get_criteria_list( ctl );
1444 addrindex_write_elem_s( fp, 1+lvl, ELTAG_LDAP_ATTR_SRCH );
1445 addrindex_write_attr( fp, ATTAG_LDAP_ATTR_NAME, node->data );
1447 node = g_list_next( node );
1450 /* End of element */
1451 addrindex_write_elem_e( fp, lvl, TAG_DS_LDAP );
1457 * Just read/write DOM fragments (preserve data found in file).
1459 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
1460 AddressDataSource *ds;
1462 ds = addrindex_create_datasource( ADDR_IF_LDAP );
1463 ds->rawDataSource = addrindex_read_fragment( file );
1467 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1468 AddressIfFragment *fragment = ds->rawDataSource;
1470 addrindex_write_fragment( fp, fragment, lvl );
1475 /* **********************************************************************
1476 * Address index I/O functions.
1477 * ***********************************************************************
1480 * Read address index file, creating appropriate data sources for each address
1483 * \param addrIndex Address index.
1484 * \param file Address index file.
1486 static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
1489 AddressInterface *iface = NULL, *dsIFace = NULL;
1490 AddressDataSource *ds;
1493 addrIndex->loadedFlag = FALSE;
1495 prev_level = file->level;
1496 rc = xml_parse_next_tag( file );
1497 if( file->level == 0 ) return;
1499 xtag = xml_get_current_tag( file );
1501 iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
1503 addrIndex->lastType = iface->type;
1504 if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
1507 dsIFace = addrindex_tag_get_datasource(
1508 addrIndex, addrIndex->lastType, xtag->tag );
1510 /* Add data source to list */
1512 if( addrIndex->lastType == ADDR_IF_BOOK ) {
1513 ds = addrindex_parse_book( file );
1514 if( ds->rawDataSource ) {
1515 addrbook_set_path( ds->rawDataSource,
1516 addrIndex->filePath );
1519 else if( addrIndex->lastType == ADDR_IF_VCARD ) {
1520 ds = addrindex_parse_vcard( file );
1522 else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
1523 ds = addrindex_parse_jpilot( file );
1525 else if( addrIndex->lastType == ADDR_IF_LDAP ) {
1526 ds = addrindex_parse_ldap( file );
1529 ds->interface = dsIFace;
1530 addrindex_hash_add_cache( addrIndex, ds );
1531 dsIFace->listSource =
1532 g_list_append( dsIFace->listSource, ds );
1540 * Search order sorting comparison function for building search order list.
1542 static gint addrindex_search_order_compare( gconstpointer ptrA, gconstpointer ptrB ) {
1543 AddressInterface *ifaceA = ( AddressInterface * ) ptrA;
1544 AddressInterface *ifaceB = ( AddressInterface * ) ptrB;
1546 return ifaceA->searchOrder - ifaceB->searchOrder;
1550 * Build list of data sources to process.
1551 * \param addrIndex Address index object.
1553 static void addrindex_build_search_order( AddressIndex *addrIndex ) {
1556 /* Clear existing list */
1557 g_list_free( addrIndex->searchOrder );
1558 addrIndex->searchOrder = NULL;
1560 /* Build new list */
1561 nodeIf = addrIndex->interfaceList;
1563 AddressInterface *iface = nodeIf->data;
1564 if( iface->useInterface ) {
1565 if( iface->searchOrder > 0 ) {
1566 /* Add to search order list */
1567 addrIndex->searchOrder = g_list_insert_sorted(
1568 addrIndex->searchOrder, iface,
1569 addrindex_search_order_compare );
1572 nodeIf = g_list_next( nodeIf );
1576 static gint addrindex_read_file( AddressIndex *addrIndex ) {
1577 XMLFile *file = NULL;
1578 gchar *fileSpec = NULL;
1580 g_return_val_if_fail( addrIndex != NULL, -1 );
1582 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1583 addrIndex->retVal = MGU_NO_FILE;
1584 file = xml_open_file( fileSpec );
1587 if( file == NULL ) {
1589 fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName );
1591 return addrIndex->retVal;
1594 addrIndex->retVal = MGU_BAD_FORMAT;
1595 if( xml_get_dtd( file ) == 0 ) {
1596 if( xml_parse_next_tag( file ) == 0 ) {
1597 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1598 addrindex_read_index( addrIndex, file );
1599 addrIndex->retVal = MGU_SUCCESS;
1603 xml_close_file( file );
1605 addrindex_build_search_order( addrIndex );
1607 return addrIndex->retVal;
1610 static void addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1611 GList *nodeIF, *nodeDS;
1613 gint lvlItem = 1 + lvlList;
1615 nodeIF = addrIndex->interfaceList;
1617 AddressInterface *iface = nodeIF->data;
1618 if( ! iface->legacyFlag ) {
1619 nodeDS = iface->listSource;
1620 addrindex_write_elem_s( fp, lvlList, iface->listTag );
1623 AddressDataSource *ds = nodeDS->data;
1625 if( iface->type == ADDR_IF_BOOK ) {
1626 addrindex_write_book( fp, ds, lvlItem );
1628 if( iface->type == ADDR_IF_VCARD ) {
1629 addrindex_write_vcard( fp, ds, lvlItem );
1631 if( iface->type == ADDR_IF_JPILOT ) {
1632 addrindex_write_jpilot( fp, ds, lvlItem );
1634 if( iface->type == ADDR_IF_LDAP ) {
1635 addrindex_write_ldap( fp, ds, lvlItem );
1638 nodeDS = g_list_next( nodeDS );
1640 addrindex_write_elem_e( fp, lvlList, iface->listTag );
1642 nodeIF = g_list_next( nodeIF );
1647 * Write data to specified file.
1648 * Enter: addrIndex Address index object.
1649 * newFile New file name.
1650 * return: Status code, from addrIndex->retVal.
1651 * Note: File will be created in directory specified by addrIndex.
1653 gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1656 #ifndef DEV_STANDALONE
1660 g_return_val_if_fail( addrIndex != NULL, -1 );
1662 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1663 addrIndex->retVal = MGU_OPEN_FILE;
1664 #ifdef DEV_STANDALONE
1665 fp = fopen( fileSpec, "wb" );
1668 fputs( "<?xml version=\"1.0\" ?>\n", fp );
1670 pfile = prefs_write_open( fileSpec );
1674 fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
1675 conv_get_current_charset_str() );
1677 addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
1680 addrindex_write_index( addrIndex, fp );
1681 addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
1683 addrIndex->retVal = MGU_SUCCESS;
1684 #ifdef DEV_STANDALONE
1687 if( prefs_file_close( pfile ) < 0 ) {
1688 addrIndex->retVal = MGU_ERROR_WRITE;
1694 return addrIndex->retVal;
1698 * Save address index data to original file.
1699 * return: Status code, from addrIndex->retVal.
1701 gint addrindex_save_data( AddressIndex *addrIndex ) {
1702 g_return_val_if_fail( addrIndex != NULL, -1 );
1704 addrIndex->retVal = MGU_NO_FILE;
1705 if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1706 if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1708 addrindex_write_to( addrIndex, addrIndex->fileName );
1709 if( addrIndex->retVal == MGU_SUCCESS ) {
1710 addrIndex->dirtyFlag = FALSE;
1712 return addrIndex->retVal;
1716 * Save all address book files which may have changed.
1717 * Return: Status code, set if there was a problem saving data.
1719 gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1720 gint retVal = MGU_SUCCESS;
1721 GList *nodeIf, *nodeDS;
1723 nodeIf = addrIndex->interfaceList;
1725 AddressInterface *iface = nodeIf->data;
1726 if( iface->type == ADDR_IF_BOOK ) {
1727 nodeDS = iface->listSource;
1729 AddressDataSource *ds = nodeDS->data;
1730 AddressBookFile *abf = ds->rawDataSource;
1731 if( addrbook_get_dirty( abf ) ) {
1732 if( addrbook_get_read_flag( abf ) ) {
1733 addrbook_save_data( abf );
1734 if( abf->retVal != MGU_SUCCESS ) {
1735 retVal = abf->retVal;
1739 nodeDS = g_list_next( nodeDS );
1743 nodeIf = g_list_next( nodeIf );
1749 /* **********************************************************************
1750 * Address book conversion to new format.
1751 * ***********************************************************************
1754 #define ELTAG_IF_OLD_FOLDER "folder"
1755 #define ELTAG_IF_OLD_GROUP "group"
1756 #define ELTAG_IF_OLD_ITEM "item"
1757 #define ELTAG_IF_OLD_NAME "name"
1758 #define ELTAG_IF_OLD_ADDRESS "address"
1759 #define ELTAG_IF_OLD_REMARKS "remarks"
1760 #define ATTAG_IF_OLD_NAME "name"
1762 #define TEMPNODE_ROOT 0
1763 #define TEMPNODE_FOLDER 1
1764 #define TEMPNODE_GROUP 2
1765 #define TEMPNODE_ADDRESS 3
1767 typedef struct _AddressCvt_Node AddressCvtNode;
1768 struct _AddressCvt_Node {
1777 * Parse current address item.
1779 static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1784 nn = g_new0( AddressCvtNode, 1 );
1785 nn->type = TEMPNODE_ADDRESS;
1788 level = file->level;
1791 xml_parse_next_tag(file);
1792 if (file->level < level) return nn;
1794 element = xml_get_element( file );
1795 if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1796 nn->name = g_strdup( element );
1798 if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1799 nn->address = g_strdup( element );
1801 if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1802 nn->remarks = g_strdup( element );
1804 xml_parse_next_tag(file);
1809 * Create a temporary node below specified node.
1811 static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1813 nn = g_new0( AddressCvtNode, 1 );
1815 nn->name = g_strdup( name );
1816 nn->remarks = g_strdup( rem );
1817 node->list = g_list_append( node->list, nn );
1822 * Process current temporary node.
1824 static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1827 AddressCvtNode *newNode = NULL;
1832 prev_level = file->level;
1833 xml_parse_next_tag( file );
1834 if (file->level < prev_level) return;
1838 if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1839 attr = xml_get_current_tag_attr(file);
1841 name = ((XMLAttr *)attr->data)->name;
1842 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1843 value = ((XMLAttr *)attr->data)->value;
1846 newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1847 addrindex_add_obj( file, newNode );
1850 else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1851 attr = xml_get_current_tag_attr(file);
1853 name = ((XMLAttr *)attr->data)->name;
1854 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1855 value = ((XMLAttr *)attr->data)->value;
1858 newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1859 addrindex_add_obj( file, newNode );
1861 else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
1862 newNode = addrindex_parse_item( file );
1863 node->list = g_list_append( node->list, newNode );
1866 /* printf( "invalid: !!! \n" ); */
1867 attr = xml_get_current_tag_attr( file );
1873 * Consume all nodes below current tag.
1875 static void addrindex_consume_tree( XMLFile *file ) {
1882 prev_level = file->level;
1883 xml_parse_next_tag( file );
1884 if (file->level < prev_level) return;
1886 xtag = xml_get_current_tag( file );
1887 /* printf( "tag : %s\n", xtag->tag ); */
1888 element = xml_get_element( file );
1889 attr = xml_get_current_tag_attr( file );
1890 /* show_attribs( attr ); */
1891 /* printf( "\ttag value : %s :\n", element ); */
1892 addrindex_consume_tree( file );
1897 * Print temporary tree.
1899 static void addrindex_print_node( AddressCvtNode *node, FILE *stream ) {
1902 fprintf( stream, "Node:\ttype :%d:\n", node->type );
1903 fprintf( stream, "\tname :%s:\n", node->name );
1904 fprintf( stream, "\taddr :%s:\n", node->address );
1905 fprintf( stream, "\trems :%s:\n", node->remarks );
1907 fprintf( stream, "\t--list----\n" );
1911 AddressCvtNode *lNode = list->data;
1912 list = g_list_next( list );
1913 addrindex_print_node( lNode, stream );
1915 fprintf( stream, "\t==list-%d==\n", node->type );
1919 * Free up temporary tree.
1921 static void addrindex_free_node( AddressCvtNode *node ) {
1922 GList *list = node->list;
1925 AddressCvtNode *lNode = list->data;
1926 list = g_list_next( list );
1927 addrindex_free_node( lNode );
1929 node->type = TEMPNODE_ROOT;
1930 g_free( node->name );
1931 g_free( node->address );
1932 g_free( node->remarks );
1933 g_list_free( node->list );
1938 * Process address book for specified node.
1940 static void addrindex_process_node(
1941 AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
1942 ItemGroup *parentGrp, ItemFolder *folderGrp )
1945 ItemFolder *itemFolder = NULL;
1946 ItemGroup *itemGParent = parentGrp;
1947 ItemFolder *itemGFolder = folderGrp;
1948 AddressCache *cache = abf->addressCache;
1950 if( node->type == TEMPNODE_ROOT ) {
1951 itemFolder = parent;
1953 else if( node->type == TEMPNODE_FOLDER ) {
1954 itemFolder = addritem_create_item_folder();
1955 addritem_folder_set_name( itemFolder, node->name );
1956 addrcache_id_folder( cache, itemFolder );
1957 addrcache_folder_add_folder( cache, parent, itemFolder );
1960 else if( node->type == TEMPNODE_GROUP ) {
1961 ItemGroup *itemGroup;
1964 /* Create a folder for group */
1965 fName = g_strdup_printf( "Cvt - %s", node->name );
1966 itemGFolder = addritem_create_item_folder();
1967 addritem_folder_set_name( itemGFolder, fName );
1968 addrcache_id_folder( cache, itemGFolder );
1969 addrcache_folder_add_folder( cache, parent, itemGFolder );
1972 /* Add group into folder */
1973 itemGroup = addritem_create_item_group();
1974 addritem_group_set_name( itemGroup, node->name );
1975 addrcache_id_group( cache, itemGroup );
1976 addrcache_folder_add_group( cache, itemGFolder, itemGroup );
1977 itemGParent = itemGroup;
1979 else if( node->type == TEMPNODE_ADDRESS ) {
1980 ItemPerson *itemPerson;
1981 ItemEMail *itemEMail;
1983 /* Create person and email objects */
1984 itemPerson = addritem_create_item_person();
1985 addritem_person_set_common_name( itemPerson, node->name );
1986 addrcache_id_person( cache, itemPerson );
1987 itemEMail = addritem_create_item_email();
1988 addritem_email_set_address( itemEMail, node->address );
1989 addritem_email_set_remarks( itemEMail, node->remarks );
1990 addrcache_id_email( cache, itemEMail );
1991 addrcache_person_add_email( cache, itemPerson, itemEMail );
1993 /* Add person into appropriate folder */
1995 addrcache_folder_add_person( cache, itemGFolder, itemPerson );
1998 addrcache_folder_add_person( cache, parent, itemPerson );
2001 /* Add email address only into group */
2003 addrcache_group_add_email( cache, parentGrp, itemEMail );
2009 AddressCvtNode *lNode = list->data;
2010 list = g_list_next( list );
2011 addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
2016 * Process address book to specified file number.
2018 static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
2019 gboolean retVal = FALSE;
2020 AddressBookFile *abf = NULL;
2021 AddressCvtNode *rootNode = NULL;
2022 gchar *newFile = NULL;
2023 GList *fileList = NULL;
2026 /* Setup root node */
2027 rootNode = g_new0( AddressCvtNode, 1 );
2028 rootNode->type = TEMPNODE_ROOT;
2029 rootNode->name = g_strdup( "root" );
2030 rootNode->list = NULL;
2031 addrindex_add_obj( file, rootNode );
2032 /* addrindex_print_node( rootNode, stdout ); */
2034 /* Create new address book */
2035 abf = addrbook_create_book();
2036 addrbook_set_name( abf, displayName );
2037 addrbook_set_path( abf, addrIndex->filePath );
2039 /* Determine next available file number */
2040 fileList = addrbook_get_bookfile_list( abf );
2042 fileNum = 1 + abf->maxValue;
2044 g_list_free( fileList );
2047 newFile = addrbook_gen_new_file_name( fileNum );
2049 addrbook_set_file( abf, newFile );
2052 addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
2054 /* addrbook_dump_book( abf, stdout ); */
2055 addrbook_save_data( abf );
2056 addrIndex->retVal = abf->retVal;
2057 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2059 addrbook_free_book( abf );
2061 addrindex_free_node( rootNode );
2064 /* Create entries in address index */
2066 abf = addrbook_create_book();
2067 addrbook_set_name( abf, displayName );
2068 addrbook_set_path( abf, addrIndex->filePath );
2069 addrbook_set_file( abf, newFile );
2070 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2077 * Process tree converting data.
2079 static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
2087 prev_level = file->level;
2088 xml_parse_next_tag( file );
2089 if (file->level < prev_level) return;
2091 xtag = xml_get_current_tag( file );
2092 /* printf( "tag : %d : %s\n", prev_level, xtag->tag ); */
2093 if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
2094 if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
2095 addrIndex->needsConversion = FALSE;
2096 addrIndex->wasConverted = TRUE;
2101 if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
2102 if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
2103 addrIndex->needsConversion = FALSE;
2104 addrIndex->wasConverted = TRUE;
2109 element = xml_get_element( file );
2110 attr = xml_get_current_tag_attr( file );
2111 /* show_attribs( attr ); */
2112 /* printf( "\ttag value : %s :\n", element ); */
2113 addrindex_consume_tree( file );
2117 static gint addrindex_convert_data( AddressIndex *addrIndex ) {
2118 XMLFile *file = NULL;
2121 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
2122 addrIndex->retVal = MGU_NO_FILE;
2123 file = xml_open_file( fileSpec );
2126 if( file == NULL ) {
2127 /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
2128 return addrIndex->retVal;
2131 addrIndex->retVal = MGU_BAD_FORMAT;
2132 if( xml_get_dtd( file ) == 0 ) {
2133 if( xml_parse_next_tag( file ) == 0 ) {
2134 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
2135 addrindex_convert_tree( addrIndex, file );
2139 xml_close_file( file );
2140 return addrIndex->retVal;
2144 * Create a new address book file.
2146 static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
2147 gboolean retVal = FALSE;
2148 AddressBookFile *abf = NULL;
2149 gchar *newFile = NULL;
2150 GList *fileList = NULL;
2153 /* Create new address book */
2154 abf = addrbook_create_book();
2155 addrbook_set_name( abf, displayName );
2156 addrbook_set_path( abf, addrIndex->filePath );
2158 /* Determine next available file number */
2159 fileList = addrbook_get_bookfile_list( abf );
2161 fileNum = 1 + abf->maxValue;
2163 g_list_free( fileList );
2166 newFile = addrbook_gen_new_file_name( fileNum );
2168 addrbook_set_file( abf, newFile );
2171 addrbook_save_data( abf );
2172 addrIndex->retVal = abf->retVal;
2173 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2174 addrbook_free_book( abf );
2177 /* Create entries in address index */
2179 abf = addrbook_create_book();
2180 addrbook_set_name( abf, displayName );
2181 addrbook_set_path( abf, addrIndex->filePath );
2182 addrbook_set_file( abf, newFile );
2183 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2190 * Read data for address index performing a conversion if necesary.
2191 * Enter: addrIndex Address index object.
2192 * return: Status code, from addrIndex->retVal.
2193 * Note: New address book files will be created in directory specified by
2194 * addrIndex. Three files will be created, for the following:
2195 * "Common addresses"
2196 * "Personal addresses"
2197 * "Gathered addresses" - a new address book.
2199 gint addrindex_read_data( AddressIndex *addrIndex ) {
2200 g_return_val_if_fail( addrIndex != NULL, -1 );
2202 addrIndex->conversionError = FALSE;
2203 addrindex_read_file( addrIndex );
2204 if( addrIndex->retVal == MGU_SUCCESS ) {
2205 if( addrIndex->needsConversion ) {
2206 if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
2207 addrIndex->conversionError = TRUE;
2210 addrIndex->conversionError = TRUE;
2213 addrIndex->dirtyFlag = TRUE;
2215 return addrIndex->retVal;
2219 * Create new address books for a new address index.
2220 * Enter: addrIndex Address index object.
2221 * return: Status code, from addrIndex->retVal.
2222 * Note: New address book files will be created in directory specified by
2223 * addrIndex. Three files will be created, for the following:
2224 * "Common addresses"
2225 * "Personal addresses"
2226 * "Gathered addresses" - a new address book.
2228 gint addrindex_create_new_books( AddressIndex *addrIndex ) {
2231 g_return_val_if_fail( addrIndex != NULL, -1 );
2233 flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
2235 flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
2236 addrIndex->dirtyFlag = TRUE;
2238 return addrIndex->retVal;
2241 /* **********************************************************************
2242 * New interface stuff.
2243 * ***********************************************************************
2247 * Return modified flag for specified data source.
2249 gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
2250 gboolean retVal = FALSE;
2251 AddressInterface *iface;
2253 if( ds == NULL ) return retVal;
2254 iface = ds->interface;
2255 if( iface == NULL ) return retVal;
2256 if( iface->getModifyFlag ) {
2257 retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
2263 * Return accessed flag for specified data source.
2265 gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
2266 gboolean retVal = FALSE;
2267 AddressInterface *iface;
2269 if( ds == NULL ) return retVal;
2270 iface = ds->interface;
2271 if( iface == NULL ) return retVal;
2272 if( iface->getAccessFlag ) {
2273 retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
2279 * Return data read flag for specified data source.
2281 gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
2282 gboolean retVal = TRUE;
2283 AddressInterface *iface;
2285 if( ds == NULL ) return retVal;
2286 iface = ds->interface;
2287 if( iface == NULL ) return retVal;
2288 if( iface->getReadFlag ) {
2289 retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
2295 * Return status code for specified data source.
2297 gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
2298 gint retVal = MGU_SUCCESS;
2299 AddressInterface *iface;
2301 if( ds == NULL ) return retVal;
2302 iface = ds->interface;
2303 if( iface == NULL ) return retVal;
2304 if( iface->getStatusCode ) {
2305 retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
2311 * Return data read flag for specified data source.
2313 gint addrindex_ds_read_data( AddressDataSource *ds ) {
2314 gint retVal = MGU_SUCCESS;
2315 AddressInterface *iface;
2317 if( ds == NULL ) return retVal;
2318 iface = ds->interface;
2319 if( iface == NULL ) return retVal;
2320 if( iface->getReadData ) {
2322 gchar *name = ( iface->getName ) ( ds->rawDataSource );
2323 printf( "addrindex_ds_read_data...reading:::%s:::\n", name );
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 ); */
2495 * LDAP callback entry point for each address entry found.
2496 * \param qry LDAP query.
2497 * \param listEMail List of Item EMail objects found.
2499 static void addrindex_ldap_entry_cb( LdapQuery *qry, GList *listEMail ) {
2503 printf( "\naddrindex::addrindex_ldap_entry_cb ::%s::\n", qry->queryName );
2506 ItemEMail *email = node->data;
2507 printf( "\temail ::%s::\n", email->address );
2508 node = g_list_next( node );
2511 if( _searchCallback_ ) {
2512 ( _searchCallback_ ) ( qry->queryID, listEMail, _searchTarget_ );
2514 g_list_free( listEMail );
2518 * LDAP callback entry point for completion of search.
2519 * \param qry LDAP query.
2521 static void addrindex_ldap_end_cb( LdapQuery *qry ) {
2522 /* printf( "\naddrindex::addrindex_ldap_end_cb ::%s::\n", qry->queryName ); */
2526 * Return results of previous query.
2528 * \return List of ItemEMail objects.
2530 static void addrindex_ldap_use_previous( const ItemFolder *folder, const gint queryID )
2537 if( _searchCallback_ ) {
2538 node = folder->listPerson;
2540 AddrItemObject *aio = node->data;
2541 if( aio && aio->type == ITEMTYPE_PERSON ) {
2542 ItemPerson *person = node->data;
2543 nodeEM = person->listEMail;
2545 ItemEMail *email = nodeEM->data;
2546 nodeEM = g_list_next( nodeEM );
2547 listEMail = g_list_append( listEMail, email );
2550 node = g_list_next( node );
2552 ( _searchCallback_ ) ( queryID, listEMail, _searchTarget_ );
2553 g_list_free( listEMail );
2557 LdapQuery *ldapsvr_locate_query( LdapServer *server, const gchar *searchTerm );
2560 * Construct an LDAP query and initiate an LDAP search.
2561 * \param server LDAP server object.
2562 * \param queryID ID of search query to be executed.
2564 static void addrindex_search_ldap( LdapServer *server, const gint queryID ) {
2568 if( ! server->searchFlag ) return;
2570 /* Retire any aged queries */
2571 ldapsvr_retire_query( server );
2573 /* Test whether any queries for the same term exist */
2574 qry = ldapsvr_locate_query( server, _searchTerm_ );
2576 ItemFolder *folder = qry->folder;
2578 /* Touch query to ensure it hangs around for a bit longer */
2579 ldapqry_touch( qry );
2581 addrindex_ldap_use_previous( folder, queryID );
2586 /* Construct a query */
2587 qry = ldapqry_create();
2588 ldapqry_set_query_id( qry, queryID );
2589 ldapqry_set_search_value( qry, _searchTerm_ );
2590 ldapqry_set_query_type( qry, LDAPQUERY_DYNAMIC );
2591 ldapqry_set_callback_entry( qry, addrindex_ldap_entry_cb );
2592 ldapqry_set_callback_end( qry, addrindex_ldap_end_cb );
2594 /* Name the query */
2595 name = g_strdup_printf( "Search for '%s'", _searchTerm_ );
2596 ldapqry_set_name( qry, name );
2599 ldapsvr_add_query( server, qry );
2600 ldapsvr_execute_query( server, qry );
2604 * Construct an LDAP query and initiate an LDAP search.
2605 * \param server LDAP server object to search.
2606 * \param searchTerm Search term to locate.
2607 * \param callbackEnd Function to call when search has terminated.
2610 void addrindex_search_ldap_noid(
2611 LdapServer *server, const gchar *searchTerm, void * callbackEnd )
2616 /* Construct a query */
2617 qry = ldapqry_create();
2618 ldapqry_set_search_value( qry, searchTerm );
2619 ldapqry_set_query_type( qry, LDAPQUERY_STATIC );
2620 ldapqry_set_callback_end( qry, callbackEnd );
2622 /* Name the query */
2623 name = g_strdup_printf( "Static Search for '%s'", searchTerm );
2624 ldapqry_set_name( qry, name );
2627 ldapsvr_add_query( server, qry );
2628 /* printf( "addrindex_search_ldap_noid::executing static search...\n" ); */
2629 ldapsvr_execute_query( server, qry );
2634 * Perform the previously registered search.
2635 * \param addrIndex Address index object.
2636 * \param queryID ID of search query to be executed.
2637 * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2640 gboolean addrindex_start_search( AddressIndex *addrIndex, const gint queryID ) {
2641 AddressInterface *iface;
2642 AddressDataSource *ds;
2647 /* printf( "addrindex_start_search::%d::\n", queryID ); */
2648 nodeIf = addrIndex->searchOrder;
2650 iface = nodeIf->data;
2651 nodeIf = g_list_next( nodeIf );
2653 if( ! iface->useInterface ) {
2657 if( ! iface->externalQuery ) {
2662 nodeDS = iface->listSource;
2665 nodeDS = g_list_next( nodeDS );
2667 if( type == ADDR_IF_LDAP ) {
2668 LdapServer *server = ds->rawDataSource;
2669 addrindex_search_ldap( server, queryID );
2678 * Stop the previously registered search.
2679 * \param addrIndex Address index object.
2680 * \param queryID ID of search query to stop.
2682 void addrindex_stop_search( AddressIndex *addrIndex, const gint queryID ){
2684 AddressInterface *iface;
2685 AddressDataSource *ds;
2690 /* If query ID does not match, search has not been setup */
2691 /* if( queryID != _queryID_ ) return; */
2693 /* printf( "addrindex_stop_search::%d::\n", queryID ); */
2694 nodeIf = addrIndex->searchOrder;
2696 iface = nodeIf->data;
2697 nodeIf = g_list_next( nodeIf );
2699 if( ! iface->useInterface ) {
2704 nodeDS = iface->listSource;
2707 nodeDS = g_list_next( nodeDS );
2708 if( type == ADDR_IF_LDAP ) {
2709 LdapServer *server = ds->rawDataSource;
2710 ldapsvr_stop_all_query( server );
2718 * Read all address books that do not support dynamic queries.
2719 * \param addrIndex Address index object.
2721 void addrindex_read_all( AddressIndex *addrIndex ) {
2722 AddressInterface *iface;
2723 AddressDataSource *ds;
2727 nodeIf = addrIndex->searchOrder;
2729 iface = nodeIf->data;
2730 nodeIf = g_list_next( nodeIf );
2732 if( ! iface->useInterface ) {
2735 if( iface->externalQuery ) {
2738 nodeDS = iface->listSource;
2741 nodeDS = g_list_next( nodeDS );
2743 /* Read address book */
2744 if( addrindex_ds_get_modify_flag( ds ) ) {
2745 addrindex_ds_read_data( ds );
2749 if( ! addrindex_ds_get_read_flag( ds ) ) {
2750 addrindex_ds_read_data( ds );
2755 addrIndex->loadedFlag = TRUE;
2759 * This function is used by the address completion function to load
2760 * addresses for all non-external address book interfaces.
2762 * \param addrIndex Address index object.
2763 * \param callBackFunc Function to be called when an address is
2765 * \return <i>TRUE</i> if data loaded, <i>FALSE</i> if address index not loaded.
2767 gboolean addrindex_load_completion(
2768 AddressIndex *addrIndex,
2769 gint (*callBackFunc) ( const gchar *, const gchar *, const gchar * ) )
2771 AddressDataSource *ds;
2772 GList *nodeIf, *nodeDS;
2773 GList *listP, *nodeP;
2775 gchar *sName, *sAddress, *sAlias, *sFriendly;
2777 if( addrIndex == NULL ) return FALSE;
2779 nodeIf = addrindex_get_interface_list( addrIndex );
2781 AddressInterface *iface = nodeIf->data;
2783 nodeIf = g_list_next( nodeIf );
2784 if( ! iface->useInterface ) {
2787 if( iface->externalQuery ) {
2790 nodeDS = iface->listSource;
2794 /* Read address book */
2795 if( addrindex_ds_get_modify_flag( ds ) ) {
2796 addrindex_ds_read_data( ds );
2799 if( ! addrindex_ds_get_read_flag( ds ) ) {
2800 addrindex_ds_read_data( ds );
2803 /* Get all persons */
2804 listP = addrindex_ds_get_all_persons( ds );
2807 ItemPerson *person = nodeP->data;
2808 nodeM = person->listEMail;
2810 /* Figure out name to use */
2811 sName = person->nickName;
2812 if( sName == NULL || *sName == '\0' ) {
2813 sName = ADDRITEM_NAME(person);
2816 /* Process each E-Mail address */
2818 ItemEMail *email = nodeM->data;
2821 sAddress = email->address;
2822 if( sAddress || *sAddress != '\0' ) {
2823 sAlias = ADDRITEM_NAME(email);
2824 if( sAlias && *sAlias != '\0' ) {
2827 ( callBackFunc ) ( sFriendly, sAddress, sName );
2830 nodeM = g_list_next( nodeM );
2832 nodeP = g_list_next( nodeP );
2834 /* Free up the list */
2835 g_list_free( listP );
2837 nodeDS = g_list_next( nodeDS );