2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
21 * General functions for accessing address index file.
31 #include <glib/gi18n.h>
35 #include "addrcache.h"
37 #include "addressbook.h"
38 #include "addrindex.h"
40 #include "addrquery.h"
41 #include "addr_compl.h"
43 #include "alertpanel.h"
45 #ifndef DEV_STANDALONE
46 #include "prefs_gtk.h"
57 #include "ldapserver.h"
59 #include "ldapquery.h"
60 #include "ldapupdate.h"
64 #define TAG_ADDRESS_INDEX "addressbook"
66 #define TAG_IF_ADDRESS_BOOK "book_list"
67 #define TAG_IF_VCARD "vcard_list"
68 #define TAG_IF_JPILOT "jpilot_list"
69 #define TAG_IF_LDAP "ldap_list"
71 #define TAG_DS_ADDRESS_BOOK "book"
72 #define TAG_DS_VCARD "vcard"
73 #define TAG_DS_JPILOT "jpilot"
74 #define TAG_DS_LDAP "server"
76 /* XML Attribute names */
77 #define ATTAG_BOOK_NAME "name"
78 #define ATTAG_BOOK_FILE "file"
80 #define ATTAG_VCARD_NAME "name"
81 #define ATTAG_VCARD_FILE "file"
83 #define ATTAG_JPILOT_NAME "name"
84 #define ATTAG_JPILOT_FILE "file"
85 #define ATTAG_JPILOT_CUSTOM_1 "custom-1"
86 #define ATTAG_JPILOT_CUSTOM_2 "custom-2"
87 #define ATTAG_JPILOT_CUSTOM_3 "custom-3"
88 #define ATTAG_JPILOT_CUSTOM_4 "custom-4"
89 #define ATTAG_JPILOT_CUSTOM "custom-"
91 #define ATTAG_LDAP_NAME "name"
92 #define ATTAG_LDAP_HOST "host"
93 #define ATTAG_LDAP_PORT "port"
94 #define ATTAG_LDAP_BASE_DN "base-dn"
95 #define ATTAG_LDAP_BIND_DN "bind-dn"
96 #define ATTAG_LDAP_BIND_PASS "bind-pass"
97 #define ATTAG_LDAP_CRITERIA "criteria"
98 #define ATTAG_LDAP_MAX_ENTRY "max-entry"
99 #define ATTAG_LDAP_TIMEOUT "timeout"
100 #define ATTAG_LDAP_MAX_AGE "max-age"
101 #define ATTAG_LDAP_DYN_SEARCH "dyn-search"
102 #define ATTAG_LDAP_MATCH_OPT "match-opt"
103 #define ATTAG_LDAP_ENABLE_TLS "enable-tls"
104 #define ATTAG_LDAP_ENABLE_SSL "enable-ssl"
106 #define ELTAG_LDAP_ATTR_SRCH "attribute"
107 #define ATTAG_LDAP_ATTR_NAME "name"
109 /* Attribute values */
110 #define ATVAL_BOOLEAN_YES "yes"
111 #define ATVAL_BOOLEAN_NO "no"
112 #define ATVAL_LDAP_MATCH_BEGIN "begin-with"
113 #define ATVAL_LDAP_MATCH_CONTAINS "contains"
116 #define ATTAG_LDAP_DEFAULT "default"
118 #define DISP_NEW_COMMON _("Common addresses")
119 #define DISP_NEW_PERSONAL _("Personal addresses")
121 /* Old address book */
122 #define TAG_IF_OLD_COMMON "common_address"
123 #define TAG_IF_OLD_PERSONAL "personal_address"
125 #define DISP_OLD_COMMON _("Common address")
126 #define DISP_OLD_PERSONAL _("Personal address")
131 static AddressIndex *_addressIndex_ = NULL;
134 * Define attribute name-value pair.
136 typedef struct _AddressIfAttr AddressIfAttrib;
137 struct _AddressIfAttr {
142 static AddressDataSource *addrindex_create_datasource ( AddressIfType ifType );
144 static GList *addrindex_ds_get_all_persons ( AddressDataSource *ds );
145 static GList *addrindex_ds_get_all_groups ( AddressDataSource *ds );
146 static AddressDataSource *addrindex_get_datasource ( AddressIndex *addrIndex,
147 const gchar *cacheID );
148 static AddressInterface *addrindex_get_interface ( AddressIndex *addrIndex,
149 AddressIfType ifType );
150 static gint addrindex_write_to ( AddressIndex *addrIndex,
151 const gchar *newFile );
154 * Define DOM fragment.
156 typedef struct _AddressIfFrag AddressIfFragment;
157 struct _AddressIfFrag {
164 * Build interface with default values.
166 * \param type Interface type.
167 * \param name Interface name.
168 * \param tagIf XML tag name for interface in address index file.
169 * \param tagDS XML tag name for datasource in address index file.
170 * \return Address interface object.
172 static AddressInterface *addrindex_create_interface(
173 gint type, gchar *name, gchar *tagIf, gchar *tagDS )
175 AddressInterface *iface = g_new0( AddressInterface, 1 );
177 ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
178 ADDRITEM_ID(iface) = NULL;
179 ADDRITEM_NAME(iface) = g_strdup( name );
180 ADDRITEM_PARENT(iface) = NULL;
181 ADDRITEM_SUBTYPE(iface) = type;
183 iface->name = g_strdup( name );
184 iface->listTag = g_strdup( tagIf );
185 iface->itemTag = g_strdup( tagDS );
186 iface->legacyFlag = FALSE;
187 iface->haveLibrary = TRUE;
188 iface->useInterface = TRUE;
189 iface->readOnly = TRUE;
191 /* Set callbacks to NULL values - override for each interface */
192 iface->getAccessFlag = NULL;
193 iface->getModifyFlag = NULL;
194 iface->getReadFlag = NULL;
195 iface->getStatusCode = NULL;
196 iface->getReadData = NULL;
197 iface->getRootFolder = NULL;
198 iface->getListFolder = NULL;
199 iface->getListPerson = NULL;
200 iface->getAllPersons = NULL;
201 iface->getAllGroups = NULL;
202 iface->getName = NULL;
203 iface->listSource = NULL;
206 iface->externalQuery = FALSE;
207 iface->searchOrder = 0; /* Ignored */
208 iface->startSearch = NULL;
209 iface->stopSearch = NULL;
215 * Build table of of all address book interfaces.
216 * \param addrIndex Address index object.
218 static void addrindex_build_if_list( AddressIndex *addrIndex ) {
219 AddressInterface *iface;
221 /* Create intrinsic XML address book interface */
222 iface = addrindex_create_interface(
223 ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK,
224 TAG_DS_ADDRESS_BOOK );
225 iface->readOnly = FALSE;
226 iface->getModifyFlag = ( void * ) addrbook_get_modified;
227 iface->getAccessFlag = ( void * ) addrbook_get_accessed;
228 iface->getReadFlag = ( void * ) addrbook_get_read_flag;
229 iface->getStatusCode = ( void * ) addrbook_get_status;
230 iface->getReadData = ( void * ) addrbook_read_data;
231 iface->getRootFolder = ( void * ) addrbook_get_root_folder;
232 iface->getListFolder = ( void * ) addrbook_get_list_folder;
233 iface->getListPerson = ( void * ) addrbook_get_list_person;
234 iface->getAllPersons = ( void * ) addrbook_get_all_persons;
235 iface->getAllGroups = ( void * ) addrbook_get_all_groups;
236 iface->getName = ( void * ) addrbook_get_name;
237 iface->setAccessFlag = ( void * ) addrbook_set_accessed;
238 iface->searchOrder = 0;
240 /* Add to list of interfaces in address book */
241 addrIndex->interfaceList =
242 g_list_append( addrIndex->interfaceList, iface );
243 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
245 /* Create vCard interface */
246 iface = addrindex_create_interface(
247 ADDR_IF_VCARD, "vCard", TAG_IF_VCARD, TAG_DS_VCARD );
248 iface->getModifyFlag = ( void * ) vcard_get_modified;
249 iface->getAccessFlag = ( void * ) vcard_get_accessed;
250 iface->getReadFlag = ( void * ) vcard_get_read_flag;
251 iface->getStatusCode = ( void * ) vcard_get_status;
252 iface->getReadData = ( void * ) vcard_read_data;
253 iface->getRootFolder = ( void * ) vcard_get_root_folder;
254 iface->getListFolder = ( void * ) vcard_get_list_folder;
255 iface->getListPerson = ( void * ) vcard_get_list_person;
256 iface->getAllPersons = ( void * ) vcard_get_all_persons;
257 iface->getName = ( void * ) vcard_get_name;
258 iface->setAccessFlag = ( void * ) vcard_set_accessed;
259 iface->searchOrder = 0;
260 addrIndex->interfaceList =
261 g_list_append( addrIndex->interfaceList, iface );
262 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
264 /* Create JPilot interface */
265 iface = addrindex_create_interface(
266 ADDR_IF_JPILOT, "J-Pilot", TAG_IF_JPILOT,
269 iface->haveLibrary = jpilot_test_pilot_lib();
270 iface->useInterface = iface->haveLibrary;
271 iface->getModifyFlag = ( void * ) jpilot_get_modified;
272 iface->getAccessFlag = ( void * ) jpilot_get_accessed;
273 iface->getReadFlag = ( void * ) jpilot_get_read_flag;
274 iface->getStatusCode = ( void * ) jpilot_get_status;
275 iface->getReadData = ( void * ) jpilot_read_data;
276 iface->getRootFolder = ( void * ) jpilot_get_root_folder;
277 iface->getListFolder = ( void * ) jpilot_get_list_folder;
278 iface->getListPerson = ( void * ) jpilot_get_list_person;
279 iface->getAllPersons = ( void * ) jpilot_get_all_persons;
280 iface->getName = ( void * ) jpilot_get_name;
281 iface->setAccessFlag = ( void * ) jpilot_set_accessed;
282 iface->searchOrder = 0;
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 /* Create LDAP interface */
292 iface = addrindex_create_interface(
293 ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
295 iface->readOnly = FALSE;
296 /* iface->haveLibrary = ldapsvr_test_ldap_lib(); */
297 iface->haveLibrary = ldaputil_test_ldap_lib();
298 iface->useInterface = iface->haveLibrary;
299 iface->getModifyFlag = ( void * ) ldapsvr_get_modified;
300 iface->getAccessFlag = ( void * ) ldapsvr_get_accessed;
301 iface->getReadFlag = ( void * ) ldapsvr_get_read_flag;
302 iface->getStatusCode = ( void * ) ldapsvr_get_status;
303 iface->getReadData = ( void * ) ldapsvr_read_data;
304 iface->getRootFolder = ( void * ) ldapsvr_get_root_folder;
305 iface->getListFolder = ( void * ) ldapsvr_get_list_folder;
306 iface->getListPerson = ( void * ) ldapsvr_get_list_person;
307 iface->getName = ( void * ) ldapsvr_get_name;
308 iface->setAccessFlag = ( void * ) ldapsvr_set_accessed;
309 iface->externalQuery = TRUE;
310 iface->searchOrder = 1;
312 iface->useInterface = FALSE;
313 iface->haveLibrary = FALSE;
315 addrIndex->interfaceList =
316 g_list_append( addrIndex->interfaceList, iface );
317 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
319 /* Two old legacy data sources (pre 0.7.0) */
320 iface = addrindex_create_interface(
321 ADDR_IF_COMMON, "Old Address - common",
322 TAG_IF_OLD_COMMON, NULL );
323 iface->legacyFlag = TRUE;
324 addrIndex->interfaceList =
325 g_list_append( addrIndex->interfaceList, iface );
326 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
328 iface = addrindex_create_interface(
329 ADDR_IF_COMMON, "Old Address - personal",
330 TAG_IF_OLD_PERSONAL, NULL );
331 iface->legacyFlag = TRUE;
332 addrIndex->interfaceList =
333 g_list_append( addrIndex->interfaceList, iface );
334 ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
340 * \param fragment Fragment to free.
342 static void addrindex_free_fragment( AddressIfFragment *fragment ) {
346 node = fragment->children;
348 AddressIfFragment *child = node->data;
349 addrindex_free_fragment( child );
351 node = g_list_next( node );
353 g_list_free( fragment->children );
355 /* Free attributes */
356 node = fragment->attributes;
358 AddressIfAttrib *nv = node->data;
363 node = g_list_next( node );
365 g_list_free( fragment->attributes );
367 g_free( fragment->name );
368 fragment->name = NULL;
369 fragment->attributes = NULL;
370 fragment->children = NULL;
376 * Create a new data source.
377 * \param ifType Interface type to create.
378 * \return Initialized data source.
380 AddressDataSource *addrindex_create_datasource( AddressIfType ifType ) {
381 AddressDataSource *ds = g_new0( AddressDataSource, 1 );
383 ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
384 ADDRITEM_ID(ds) = NULL;
385 ADDRITEM_NAME(ds) = NULL;
386 ADDRITEM_PARENT(ds) = NULL;
387 ADDRITEM_SUBTYPE(ds) = 0;
389 ds->rawDataSource = NULL;
390 ds->interface = NULL;
395 * Free up data source.
396 * \param ds Data source to free.
398 void addrindex_free_datasource( AddressDataSource *ds ) {
399 AddressInterface *iface;
401 g_return_if_fail( ds != NULL );
403 iface = ds->interface;
404 if( ds->rawDataSource != NULL ) {
405 if( iface != NULL ) {
406 if( iface->useInterface ) {
407 if( iface->type == ADDR_IF_BOOK ) {
408 AddressBookFile *abf = ds->rawDataSource;
409 addrbook_free_book( abf );
411 else if( iface->type == ADDR_IF_VCARD ) {
412 VCardFile *vcf = ds->rawDataSource;
416 else if( iface->type == ADDR_IF_JPILOT ) {
417 JPilotFile *jpf = ds->rawDataSource;
422 else if( iface->type == ADDR_IF_LDAP ) {
423 LdapServer *server = ds->rawDataSource;
424 ldapsvr_free( server );
431 AddressIfFragment *fragment = ds->rawDataSource;
432 addrindex_free_fragment( fragment );
437 ADDRITEM_TYPE(ds) = ITEMTYPE_NONE;
438 ADDRITEM_ID(ds) = NULL;
439 ADDRITEM_NAME(ds) = NULL;
440 ADDRITEM_PARENT(ds) = NULL;
441 ADDRITEM_SUBTYPE(ds) = 0;
442 ds->type = ADDR_IF_NONE;
443 ds->interface = NULL;
444 ds->rawDataSource = NULL;
450 * Free up all data sources for specified interface.
451 * \param iface Address interface to process.
453 static void addrindex_free_all_datasources( AddressInterface *iface ) {
454 GList *node = iface->listSource;
456 AddressDataSource *ds = node->data;
457 addrindex_free_datasource( ds );
459 node = g_list_next( node );
464 * Free up specified interface.
465 * \param iface Interface to process.
467 static void addrindex_free_interface( AddressInterface *iface ) {
468 /* Free up data sources */
469 addrindex_free_all_datasources( iface );
470 g_list_free( iface->listSource );
472 /* Free internal storage */
473 g_free( ADDRITEM_ID(iface) );
474 g_free( ADDRITEM_NAME(iface) );
475 g_free( iface->name );
476 g_free( iface->listTag );
477 g_free( iface->itemTag );
479 /* Clear all pointers */
480 ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
481 ADDRITEM_ID(iface) = NULL;
482 ADDRITEM_NAME(iface) = NULL;
483 ADDRITEM_PARENT(iface) = NULL;
484 ADDRITEM_SUBTYPE(iface) = 0;
485 iface->type = ADDR_IF_NONE;
487 iface->listTag = NULL;
488 iface->itemTag = NULL;
489 iface->legacyFlag = FALSE;
490 iface->useInterface = FALSE;
491 iface->haveLibrary = FALSE;
492 iface->listSource = NULL;
495 iface->searchOrder = 0;
496 iface->startSearch = NULL;
497 iface->stopSearch = NULL;
503 * Return cache ID for specified data source.
505 * \param addrIndex Address index.
506 * \param ds Data source.
507 * \return ID or NULL if not found. This should be <code>g_free()</code>
510 gchar *addrindex_get_cache_id( AddressIndex *addrIndex, AddressDataSource *ds ) {
511 gchar *cacheID = NULL;
512 AddrBookBase *adbase;
515 g_return_val_if_fail( addrIndex != NULL, NULL );
516 g_return_val_if_fail( ds != NULL, NULL );
518 adbase = ( AddrBookBase * ) ds->rawDataSource;
520 cache = adbase->addressCache;
522 cacheID = g_strdup( cache->cacheID );
530 * Return reference to data source for specified cacheID.
531 * \param addrIndex Address index.
533 * \return Data source, or NULL if not found.
535 static AddressDataSource *addrindex_get_datasource(
536 AddressIndex *addrIndex, const gchar *cacheID )
538 g_return_val_if_fail( addrIndex != NULL, NULL );
539 g_return_val_if_fail( cacheID != NULL, NULL );
540 return ( AddressDataSource * ) g_hash_table_lookup( addrIndex->hashCache, cacheID );
544 * Return reference to address cache for specified cacheID.
545 * \param addrIndex Address index.
547 * \return Address cache, or NULL if not found.
549 AddressCache *addrindex_get_cache( AddressIndex *addrIndex, const gchar *cacheID ) {
550 AddressDataSource *ds;
551 AddrBookBase *adbase;
554 g_return_val_if_fail( addrIndex != NULL, NULL );
555 g_return_val_if_fail( cacheID != NULL, NULL );
558 ds = addrindex_get_datasource( addrIndex, cacheID );
560 adbase = ( AddrBookBase * ) ds->rawDataSource;
561 cache = adbase->addressCache;
567 * Add data source into hash table.
568 * \param addrIndex Address index.
569 * \param ds Data source.
571 static void addrindex_hash_add_cache(
572 AddressIndex *addrIndex, AddressDataSource *ds )
576 cacheID = addrindex_get_cache_id( addrIndex, ds );
578 g_hash_table_insert( addrIndex->hashCache, cacheID, ds );
583 * Free hash table callback function.
585 static gboolean addrindex_free_cache_cb( gpointer key, gpointer value, gpointer data ) {
593 * Free hash table of address cache items.
595 static void addrindex_free_cache_hash( GHashTable *table ) {
596 g_hash_table_foreach_remove( table, addrindex_free_cache_cb, NULL );
597 g_hash_table_destroy( table );
601 * Remove data source from internal hashtable.
602 * \param addrIndex Address index.
603 * \param ds Data source to remove.
605 static void addrindex_hash_remove_cache(
606 AddressIndex *addrIndex, AddressDataSource *ds )
610 cacheID = addrindex_get_cache_id( addrIndex, ds );
612 g_hash_table_remove( addrIndex->hashCache, cacheID );
619 * Create a new address index. This is created as a singleton object.
620 * \return Initialized address index object.
622 AddressIndex *addrindex_create_index( void ) {
625 if( _addressIndex_ == NULL ) {
626 index = g_new0( AddressIndex, 1 );
627 ADDRITEM_TYPE(index) = ITEMTYPE_INDEX;
628 ADDRITEM_ID(index) = NULL;
629 ADDRITEM_NAME(index) = g_strdup( "Address Index" );
630 ADDRITEM_PARENT(index) = NULL;
631 ADDRITEM_SUBTYPE(index) = 0;
632 index->filePath = NULL;
633 index->fileName = NULL;
634 index->retVal = MGU_SUCCESS;
635 index->needsConversion = FALSE;
636 index->wasConverted = FALSE;
637 index->conversionError = FALSE;
638 index->interfaceList = NULL;
639 index->lastType = ADDR_IF_NONE;
640 index->dirtyFlag = FALSE;
641 index->hashCache = g_hash_table_new( g_str_hash, g_str_equal );
642 index->loadedFlag = FALSE;
643 index->searchOrder = NULL;
644 addrindex_build_if_list( index );
645 _addressIndex_ = index;
647 return _addressIndex_;
651 * Property - Specify file path to address index file.
652 * \param addrIndex Address index.
653 * \param value Path to index file.
655 void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
656 g_return_if_fail( addrIndex != NULL );
657 addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
661 * Property - Specify file name to address index file.
662 * \param addrIndex Address index.
663 * \param value File name.
665 void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
666 g_return_if_fail( addrIndex != NULL );
667 addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
671 * Return list of address interfaces.
672 * \param addrIndex Address index.
673 * \return List of address interfaces.
675 GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
676 g_return_val_if_fail( addrIndex != NULL, NULL );
677 return addrIndex->interfaceList;
681 * Perform any other initialization of address index.
683 void addrindex_initialize( void ) {
685 addrcompl_initialize();
689 * Perform any other teardown of address index.
691 void addrindex_teardown( void ) {
692 addrcompl_teardown();
697 * Free up address index.
698 * \param addrIndex Address index.
700 void addrindex_free_index( AddressIndex *addrIndex ) {
703 g_return_if_fail( addrIndex != NULL );
706 g_list_free( addrIndex->searchOrder );
707 addrIndex->searchOrder = NULL;
709 /* Free internal storage */
710 g_free( ADDRITEM_ID(addrIndex) );
711 g_free( ADDRITEM_NAME(addrIndex) );
712 g_free( addrIndex->filePath );
713 g_free( addrIndex->fileName );
716 ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
717 ADDRITEM_ID(addrIndex) = NULL;
718 ADDRITEM_NAME(addrIndex) = NULL;
719 ADDRITEM_PARENT(addrIndex) = NULL;
720 ADDRITEM_SUBTYPE(addrIndex) = 0;
721 addrIndex->filePath = NULL;
722 addrIndex->fileName = NULL;
723 addrIndex->retVal = MGU_SUCCESS;
724 addrIndex->needsConversion = FALSE;
725 addrIndex->wasConverted = FALSE;
726 addrIndex->conversionError = FALSE;
727 addrIndex->lastType = ADDR_IF_NONE;
728 addrIndex->dirtyFlag = FALSE;
730 /* Free up interfaces */
731 node = addrIndex->interfaceList;
733 AddressInterface *iface = node->data;
734 addrindex_free_interface( iface );
735 node = g_list_next( node );
737 g_list_free( addrIndex->interfaceList );
738 addrIndex->interfaceList = NULL;
740 /* Free up hash cache */
741 addrindex_free_cache_hash( addrIndex->hashCache );
742 addrIndex->hashCache = NULL;
744 addrIndex->loadedFlag = FALSE;
748 _addressIndex_ = NULL;
752 * Print address index.
753 * \param addrIndex Address index.
754 * \parem stream Stream to print.
756 void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
757 g_return_if_fail( addrIndex != NULL );
758 fprintf( stream, "AddressIndex:\n" );
759 fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
760 fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
761 fprintf( stream, "\t status: %d\n", addrIndex->retVal );
762 fprintf( stream, "\tconverted: '%s'\n",
763 addrIndex->wasConverted ? "yes" : "no" );
764 fprintf( stream, "\tcvt error: '%s'\n",
765 addrIndex->conversionError ? "yes" : "no" );
766 fprintf( stream, "\t---\n" );
770 * Retrieve reference to address interface for specified interface type.
771 * \param addrIndex Address index.
772 * \param ifType Interface type.
773 * \return Address interface, or NULL if not found.
775 static AddressInterface *addrindex_get_interface(
776 AddressIndex *addrIndex, AddressIfType ifType )
778 AddressInterface *retVal = NULL;
781 g_return_val_if_fail( addrIndex != NULL, NULL );
783 node = addrIndex->interfaceList;
785 AddressInterface *iface = node->data;
786 node = g_list_next( node );
787 if( iface->type == ifType ) {
796 * Add raw data source to index. The raw data object (an AddressBookFile or
797 * VCardFile object, for example) should be supplied as the raw dataSource
800 * \param addrIndex Address index.
801 * \param ifType Interface type to add.
802 * \param dataSource Actual raw data source to add.
803 * \return Data source added, or NULL if invalid interface type.
805 AddressDataSource *addrindex_index_add_datasource(
806 AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource )
808 AddressInterface *iface;
809 AddressDataSource *ds = NULL;
811 g_return_val_if_fail( addrIndex != NULL, NULL );
812 g_return_val_if_fail( dataSource != NULL, NULL );
814 iface = addrindex_get_interface( addrIndex, ifType );
816 ds = addrindex_create_datasource( ifType );
817 ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
819 ds->rawDataSource = dataSource;
820 ds->interface = iface;
821 iface->listSource = g_list_append( iface->listSource, ds );
822 addrIndex->dirtyFlag = TRUE;
824 addrindex_hash_add_cache( addrIndex, ds );
830 * Remove specified data source from index.
831 * \param addrIndex Address index.
832 * \param dataSource Data source to add.
833 * \return Reference to data source if removed, or NULL if data source was not
834 * found in index. Note the this object must still be freed.
836 AddressDataSource *addrindex_index_remove_datasource(
837 AddressIndex *addrIndex, AddressDataSource *dataSource )
839 AddressDataSource *retVal = FALSE;
840 AddressInterface *iface;
842 g_return_val_if_fail( addrIndex != NULL, NULL );
843 g_return_val_if_fail( dataSource != NULL, NULL );
845 iface = addrindex_get_interface( addrIndex, dataSource->type );
847 iface->listSource = g_list_remove( iface->listSource, dataSource );
848 addrIndex->dirtyFlag = TRUE;
849 dataSource->interface = NULL;
851 /* Remove cache from hash table */
852 addrindex_hash_remove_cache( addrIndex, dataSource );
860 * Retrieve a reference to address interface for specified interface type and
861 * XML interface tag name.
862 * \param addrIndex Address index.
863 * \param tag XML interface tag name to match.
864 * \param ifType Interface type to match.
865 * \return Reference to address index, or NULL if not found in index.
867 static AddressInterface *addrindex_tag_get_interface(
868 AddressIndex *addrIndex, gchar *tag, AddressIfType ifType )
870 AddressInterface *retVal = NULL;
871 GList *node = addrIndex->interfaceList;
874 AddressInterface *iface = node->data;
875 node = g_list_next( node );
877 if( strcmp( iface->listTag, tag ) == 0 ) {
883 if( iface->type == ifType ) {
893 * Retrieve a reference to address interface for specified interface type and
894 * XML datasource tag name.
895 * \param addrIndex Address index.
896 * \param ifType Interface type to match.
897 * \param tag XML datasource tag name to match.
898 * \return Reference to address index, or NULL if not found in index.
900 static AddressInterface *addrindex_tag_get_datasource(
901 AddressIndex *addrIndex, AddressIfType ifType, gchar *tag )
903 AddressInterface *retVal = NULL;
904 GList *node = addrIndex->interfaceList;
907 AddressInterface *iface = node->data;
908 node = g_list_next( node );
909 if( iface->type == ifType && iface->itemTag ) {
910 if( strcmp( iface->itemTag, tag ) == 0 ) {
919 /* **********************************************************************
920 * Interface XML parsing functions.
921 * ***********************************************************************
925 * Write start of XML element to file.
927 * \param lvl Indentation level.
928 * \param name Element name.
930 static int addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name ) {
932 for( i = 0; i < lvl; i++ )
933 if (fputs( " ", fp ) == EOF)
935 if (fputs( "<", fp ) == EOF)
937 if (fputs( name, fp ) == EOF)
943 * Write end of XML element to file.
945 * \param lvl Indentation level.
946 * \param name Element name.
948 static int addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name ) {
950 for( i = 0; i < lvl; i++ )
951 if (fputs( " ", fp ) == EOF)
953 if (fputs( "</", fp ) == EOF)
955 if (fputs( name, fp ) == EOF)
957 if (fputs( ">\n", fp ) == EOF)
963 * Write XML attribute to file.
965 * \param name Attribute name.
966 * \param value Attribute value.
968 static int addrindex_write_attr( FILE *fp, const gchar *name, const gchar *value ) {
969 if (fputs( " ", fp ) == EOF)
971 if (fputs( name, fp ) == EOF)
973 if (fputs( "=\"", fp ) == EOF)
975 if (xml_file_put_escape_str( fp, value ) < 0)
977 if (fputs( "\"", fp ) == EOF)
983 * Return DOM fragment for current XML tag from file.
984 * \param file XML file being processed.
985 * \return Fragment representing DOM fragment for configuration element.
987 static AddressIfFragment *addrindex_read_fragment( XMLFile *file ) {
988 AddressIfFragment *fragment;
989 AddressIfFragment *child;
999 /* g_print( "addrindex_read_fragment\n" ); */
1001 prevLevel = file->level;
1003 /* Get current tag name */
1004 xtag = xml_get_current_tag( file );
1006 /* Create new fragment */
1007 fragment = g_new0( AddressIfFragment, 1 );
1008 fragment->name = g_strdup( xtag->tag );
1009 fragment->children = NULL;
1010 fragment->attributes = NULL;
1012 /* Read attributes */
1014 attr = xml_get_current_tag_attr( file );
1016 name = ((XMLAttr *)attr->data)->name;
1017 value = ((XMLAttr *)attr->data)->value;
1018 nv = g_new0( AddressIfAttrib, 1 );
1019 nv->name = g_strdup( name );
1020 nv->value = g_strdup( value );
1021 list = g_list_append( list, nv );
1022 attr = g_list_next( attr );
1024 fragment->attributes = list;
1026 /* Now read the children */
1028 rc = xml_parse_next_tag( file );
1033 if( file->level < prevLevel ) {
1034 /* We must be above level we start at */
1037 child = addrindex_read_fragment( file );
1038 fragment->children = g_list_append( fragment->children, child );
1045 * Write DOM fragment to file.
1046 * \param fp File to write.
1047 * \param fragment DOM fragment for configuration element.
1048 * \param lvl Indent level.
1050 static int addrindex_write_fragment(
1051 FILE *fp, const AddressIfFragment *fragment, const gint lvl )
1056 if (addrindex_write_elem_s( fp, lvl, fragment->name ) < 0)
1058 node = fragment->attributes;
1060 AddressIfAttrib *nv = node->data;
1061 if (addrindex_write_attr( fp, nv->name, nv->value ) < 0)
1063 node = g_list_next( node );
1065 if( fragment->children ) {
1066 if (fputs(" >\n", fp) == EOF)
1069 /* Output children */
1070 node = fragment->children;
1072 AddressIfFragment *child = node->data;
1073 if (addrindex_write_fragment( fp, child, 1+lvl ) < 0)
1075 node = g_list_next( node );
1078 /* Output closing tag */
1079 if (addrindex_write_elem_e( fp, lvl, fragment->name ) < 0)
1083 if (fputs(" />\n", fp) == EOF)
1092 * Read/parse address index file, creating a data source for a regular
1093 * intrinsic XML addressbook.
1094 * \param file Address index file.
1095 * \return Data source.
1097 static AddressDataSource *addrindex_parse_book( XMLFile *file ) {
1098 AddressDataSource *ds;
1099 AddressBookFile *abf;
1102 ds = addrindex_create_datasource( ADDR_IF_BOOK );
1103 abf = addrbook_create_book();
1104 attr = xml_get_current_tag_attr( file );
1106 gchar *name = ((XMLAttr *)attr->data)->name;
1107 gchar *value = ((XMLAttr *)attr->data)->value;
1108 if( strcmp( name, ATTAG_BOOK_NAME ) == 0 ) {
1109 addrbook_set_name( abf, value );
1111 else if( strcmp( name, ATTAG_BOOK_FILE ) == 0) {
1112 addrbook_set_file( abf, value );
1114 attr = g_list_next( attr );
1116 ds->rawDataSource = abf;
1120 static int addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
1121 AddressBookFile *abf = ds->rawDataSource;
1123 if (addrindex_write_elem_s( fp, lvl, TAG_DS_ADDRESS_BOOK ) < 0)
1125 if (addrindex_write_attr( fp, ATTAG_BOOK_NAME, addrbook_get_name( abf ) ) < 0)
1127 if (addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName ) < 0)
1129 if (fputs( " />\n", fp ) == EOF)
1135 static AddressDataSource *addrindex_parse_vcard( XMLFile *file ) {
1136 AddressDataSource *ds;
1140 ds = addrindex_create_datasource( ADDR_IF_VCARD );
1141 vcf = vcard_create();
1142 attr = xml_get_current_tag_attr( file );
1144 gchar *name = ((XMLAttr *)attr->data)->name;
1145 gchar *value = ((XMLAttr *)attr->data)->value;
1146 if( strcmp( name, ATTAG_VCARD_NAME ) == 0 ) {
1147 vcard_set_name( vcf, value );
1149 else if( strcmp( name, ATTAG_VCARD_FILE ) == 0) {
1150 vcard_set_file( vcf, value );
1152 attr = g_list_next( attr );
1154 ds->rawDataSource = vcf;
1158 static int addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
1159 VCardFile *vcf = ds->rawDataSource;
1161 if (addrindex_write_elem_s( fp, lvl, TAG_DS_VCARD ) < 0)
1163 if (addrindex_write_attr( fp, ATTAG_VCARD_NAME, vcard_get_name( vcf ) ) < 0)
1165 if (addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path ) < 0)
1167 if (fputs( " />\n", fp ) == EOF)
1174 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
1175 AddressDataSource *ds;
1179 ds = addrindex_create_datasource( ADDR_IF_JPILOT );
1180 jpf = jpilot_create();
1181 attr = xml_get_current_tag_attr( file );
1183 gchar *name = ((XMLAttr *)attr->data)->name;
1184 gchar *value = ((XMLAttr *)attr->data)->value;
1185 if( strcmp( name, ATTAG_JPILOT_NAME ) == 0 ) {
1186 jpilot_set_name( jpf, value );
1188 else if( strcmp( name, ATTAG_JPILOT_FILE ) == 0 ) {
1189 jpilot_set_file( jpf, value );
1191 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_1 ) == 0 ) {
1192 jpilot_add_custom_label( jpf, value );
1194 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_2 ) == 0 ) {
1195 jpilot_add_custom_label( jpf, value );
1197 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_3 ) == 0 ) {
1198 jpilot_add_custom_label( jpf, value );
1200 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_4 ) == 0 ) {
1201 jpilot_add_custom_label( jpf, value );
1203 attr = g_list_next( attr );
1205 ds->rawDataSource = jpf;
1209 static int addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
1210 JPilotFile *jpf = ds->rawDataSource;
1214 GList *customLbl = jpilot_get_custom_labels( jpf );
1215 if (addrindex_write_elem_s( fp, lvl, TAG_DS_JPILOT ) < 0)
1217 if (addrindex_write_attr( fp, ATTAG_JPILOT_NAME, jpilot_get_name( jpf ) ) < 0)
1219 if (addrindex_write_attr( fp, ATTAG_JPILOT_FILE, jpf->path ) < 0)
1225 g_snprintf( name, sizeof(name), "%s%d",
1226 ATTAG_JPILOT_CUSTOM, ind );
1227 if (addrindex_write_attr( fp, name, node->data ) < 0)
1230 node = g_list_next( node );
1232 if (fputs( " />\n", fp ) == EOF)
1240 * Just read/write DOM fragments (preserve data found in file).
1242 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
1243 AddressDataSource *ds;
1245 ds = addrindex_create_datasource( ADDR_IF_JPILOT );
1246 ds->rawDataSource = addrindex_read_fragment( file );
1250 static int addrindex_write_jpilot( FILE *fp, AddressDataSource *ds, gint lvl ) {
1251 AddressIfFragment *fragment = ds->rawDataSource;
1253 if (addrindex_write_fragment( fp, fragment, lvl ) < 0)
1262 * Parse LDAP criteria attribute data from XML file.
1263 * \param file Index file.
1264 * \param ctl LDAP control object to populate.
1266 static void addrindex_parse_ldap_attrlist( XMLFile *file, LdapControl *ctl ) {
1275 if( file == NULL ) {
1280 prevLevel = file->level;
1281 xtagPrev = xml_get_current_tag( file );
1283 rc = xml_parse_next_tag( file );
1285 /* Terminate prematurely */
1286 mgu_free_dlist( list );
1290 if( file->level < prevLevel ) {
1291 /* We must be above level we start at */
1295 /* Get a tag (element) */
1296 xtag = xml_get_current_tag( file );
1297 if( strcmp( xtag->tag, ELTAG_LDAP_ATTR_SRCH ) == 0 ) {
1298 /* LDAP criteria attribute */
1299 attr = xml_get_current_tag_attr( file );
1301 gchar *name = ((XMLAttr *)attr->data)->name;
1302 gchar *value = ((XMLAttr *)attr->data)->value;
1303 if( strcmp( name, ATTAG_LDAP_ATTR_NAME ) == 0 ) {
1304 if( value && strlen( value ) > 0 ) {
1305 list = g_list_append(
1306 list, g_strdup( value ) );
1309 attr = g_list_next( attr );
1313 if( xtag != xtagPrev ) {
1314 /* Found a new tag */
1321 /* Build list of search attributes */
1322 ldapctl_criteria_list_clear( ctl );
1325 ldapctl_criteria_list_add( ctl, node->data );
1326 g_free( node->data );
1328 node = g_list_next( node );
1330 g_list_free( list );
1335 void ldapsvr_set_control( LdapServer *server, LdapControl *ctl );
1337 * Parse LDAP control data from XML file.
1338 * \param file Index file.
1339 * \return Initialized data soruce object.
1341 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
1342 AddressDataSource *ds;
1346 gchar *serverName = NULL;
1347 gchar *criteria = NULL;
1348 gboolean bDynSearch;
1349 gboolean bTLS, bSSL;
1352 /* g_print( "addrindex_parse_ldap\n" ); */
1353 /* Set up some defaults */
1357 iMatch = LDAPCTL_MATCH_BEGINWITH;
1359 ds = addrindex_create_datasource( ADDR_IF_LDAP );
1360 ctl = ldapctl_create();
1361 attr = xml_get_current_tag_attr( file );
1363 gchar *name = ((XMLAttr *)attr->data)->name;
1364 gchar *value = ((XMLAttr *)attr->data)->value;
1365 gint ivalue = atoi( value );
1367 if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
1368 g_free( serverName );
1369 serverName = g_strdup( value );
1371 else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
1372 ldapctl_set_host( ctl, value );
1374 else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
1375 ldapctl_set_port( ctl, ivalue );
1377 else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
1378 ldapctl_set_base_dn( ctl, value );
1380 else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
1381 ldapctl_set_bind_dn( ctl, value );
1383 else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
1384 ldapctl_set_bind_password( ctl, value );
1386 else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
1388 criteria = g_strdup( value );
1389 g_print("criteria %s\n", criteria);
1391 else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
1392 ldapctl_set_max_entries( ctl, ivalue );
1394 else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
1395 ldapctl_set_timeout( ctl, ivalue );
1397 else if( strcmp( name, ATTAG_LDAP_MAX_AGE ) == 0 ) {
1398 ldapctl_set_max_query_age( ctl, ivalue );
1400 else if( strcmp( name, ATTAG_LDAP_DYN_SEARCH ) == 0 ) {
1402 if( strcmp( value, ATVAL_BOOLEAN_YES ) == 0 ) {
1406 else if( strcmp( name, ATTAG_LDAP_MATCH_OPT ) == 0 ) {
1407 iMatch = LDAPCTL_MATCH_BEGINWITH;
1408 if( strcmp( value, ATVAL_LDAP_MATCH_CONTAINS ) == 0 ) {
1409 iMatch = LDAPCTL_MATCH_CONTAINS;
1412 else if( strcmp( name, ATTAG_LDAP_ENABLE_TLS ) == 0 ) {
1414 if( strcmp( value, ATVAL_BOOLEAN_YES ) == 0 ) {
1418 else if( strcmp( name, ATTAG_LDAP_ENABLE_SSL ) == 0 ) {
1420 if( strcmp( value, ATVAL_BOOLEAN_YES ) == 0 ) {
1424 attr = g_list_next( attr );
1427 server = ldapsvr_create_noctl();
1428 ldapsvr_set_name( server, serverName );
1429 ldapsvr_set_search_flag( server, bDynSearch );
1430 ldapctl_set_matching_option( ctl, iMatch );
1432 ldapctl_set_tls( ctl, bTLS );
1433 ldapctl_set_ssl( ctl, bSSL );
1435 g_free( serverName );
1436 ldapsvr_set_control( server, ctl );
1437 ds->rawDataSource = server;
1439 addrindex_parse_ldap_attrlist( file, ctl );
1441 * If criteria have been specified and no attributes were listed, then
1442 * convert old style criteria into an attribute list. Any criteria will
1443 * be dropped when saving data.
1446 if( ! ldapctl_get_criteria_list( ctl ) ) {
1447 ldapctl_parse_ldap_search( ctl, criteria );
1451 /* ldapsvr_print_data( server, stdout ); */
1456 static int addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1457 LdapServer *server = ds->rawDataSource;
1458 LdapControl *ctl = NULL;
1463 ctl = server->control;
1465 if( ctl == NULL ) return 0;
1467 /* Output start element with attributes */
1468 if (addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP ) < 0)
1470 if (addrindex_write_attr( fp, ATTAG_LDAP_NAME, ldapsvr_get_name( server ) ) < 0)
1472 if (addrindex_write_attr( fp, ATTAG_LDAP_HOST, ctl->hostName ) < 0)
1475 sprintf( value, "%d", ctl->port );
1476 if (addrindex_write_attr( fp, ATTAG_LDAP_PORT, value ) < 0)
1479 if (addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, ctl->baseDN ) < 0)
1481 if (addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, ctl->bindDN ) < 0)
1483 if (addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, ctl->bindPass ) < 0)
1486 sprintf( value, "%d", ctl->maxEntries );
1487 if (addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value ) < 0)
1489 sprintf( value, "%d", ctl->timeOut );
1490 if (addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value ) < 0)
1492 sprintf( value, "%d", ctl->maxQueryAge );
1493 if (addrindex_write_attr( fp, ATTAG_LDAP_MAX_AGE, value ) < 0)
1496 if (addrindex_write_attr( fp, ATTAG_LDAP_DYN_SEARCH,
1497 server->searchFlag ?
1498 ATVAL_BOOLEAN_YES : ATVAL_BOOLEAN_NO ) < 0)
1501 if (addrindex_write_attr( fp, ATTAG_LDAP_MATCH_OPT,
1502 ( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) ?
1503 ATVAL_LDAP_MATCH_CONTAINS : ATVAL_LDAP_MATCH_BEGIN ) < 0)
1506 if (addrindex_write_attr( fp, ATTAG_LDAP_ENABLE_TLS,
1508 ATVAL_BOOLEAN_YES : ATVAL_BOOLEAN_NO ) < 0)
1510 if (addrindex_write_attr( fp, ATTAG_LDAP_ENABLE_SSL,
1512 ATVAL_BOOLEAN_YES : ATVAL_BOOLEAN_NO ) < 0)
1515 if (fputs(" >\n", fp) == EOF)
1518 /* Output attributes */
1519 node = ldapctl_get_criteria_list( ctl );
1521 if (addrindex_write_elem_s( fp, 1+lvl, ELTAG_LDAP_ATTR_SRCH ) < 0)
1523 if (addrindex_write_attr( fp, ATTAG_LDAP_ATTR_NAME, node->data ) < 0)
1525 if (fputs(" />\n", fp) == EOF)
1527 node = g_list_next( node );
1530 /* End of element */
1531 if (addrindex_write_elem_e( fp, lvl, TAG_DS_LDAP ) < 0)
1539 * Just read/write DOM fragments (preserve data found in file).
1541 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
1542 AddressDataSource *ds;
1544 ds = addrindex_create_datasource( ADDR_IF_LDAP );
1545 ds->rawDataSource = addrindex_read_fragment( file );
1549 static int addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1550 AddressIfFragment *fragment = ds->rawDataSource;
1552 if (addrindex_write_fragment( fp, fragment, lvl ) < 0)
1559 /* **********************************************************************
1560 * Address index I/O functions.
1561 * ***********************************************************************
1564 * Read address index file, creating appropriate data sources for each address
1567 * \param addrIndex Address index.
1568 * \param file Address index file.
1570 static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
1573 AddressInterface *iface = NULL, *dsIFace = NULL;
1574 AddressDataSource *ds;
1577 addrIndex->loadedFlag = FALSE;
1579 prev_level = file->level;
1580 rc = xml_parse_next_tag( file );
1581 if( file->level == 0 ) return;
1583 xtag = xml_get_current_tag( file );
1585 iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
1587 addrIndex->lastType = iface->type;
1588 if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
1591 dsIFace = addrindex_tag_get_datasource(
1592 addrIndex, addrIndex->lastType, xtag->tag );
1594 /* Add data source to list */
1596 if( addrIndex->lastType == ADDR_IF_BOOK ) {
1597 ds = addrindex_parse_book( file );
1598 if( ds->rawDataSource ) {
1599 addrbook_set_path( ds->rawDataSource,
1600 addrIndex->filePath );
1603 else if( addrIndex->lastType == ADDR_IF_VCARD ) {
1604 ds = addrindex_parse_vcard( file );
1606 else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
1607 ds = addrindex_parse_jpilot( file );
1609 else if( addrIndex->lastType == ADDR_IF_LDAP ) {
1610 ds = addrindex_parse_ldap( file );
1613 ds->interface = dsIFace;
1614 addrindex_hash_add_cache( addrIndex, ds );
1615 dsIFace->listSource =
1616 g_list_append( dsIFace->listSource, ds );
1624 * Search order sorting comparison function for building search order list.
1626 static gint addrindex_search_order_compare( gconstpointer ptrA, gconstpointer ptrB ) {
1627 AddressInterface *ifaceA = ( AddressInterface * ) ptrA;
1628 AddressInterface *ifaceB = ( AddressInterface * ) ptrB;
1630 return ifaceA->searchOrder - ifaceB->searchOrder;
1634 * Build list of data sources to process.
1635 * \param addrIndex Address index object.
1637 static void addrindex_build_search_order( AddressIndex *addrIndex ) {
1640 /* Clear existing list */
1641 g_list_free( addrIndex->searchOrder );
1642 addrIndex->searchOrder = NULL;
1644 /* Build new list */
1645 nodeIf = addrIndex->interfaceList;
1647 AddressInterface *iface = nodeIf->data;
1648 if( iface->useInterface ) {
1649 if( iface->searchOrder > 0 ) {
1650 /* Add to search order list */
1651 addrIndex->searchOrder = g_list_insert_sorted(
1652 addrIndex->searchOrder, iface,
1653 addrindex_search_order_compare );
1656 nodeIf = g_list_next( nodeIf );
1660 static gint addrindex_read_file( AddressIndex *addrIndex ) {
1661 XMLFile *file = NULL;
1662 gchar *fileSpec = NULL;
1664 g_return_val_if_fail( addrIndex != NULL, -1 );
1666 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1667 addrIndex->retVal = MGU_NO_FILE;
1668 file = xml_open_file( fileSpec );
1671 if( file == NULL ) {
1673 g_print( " file '%s' does not exist.\n", addrIndex->fileName );
1675 return addrIndex->retVal;
1678 addrIndex->retVal = MGU_BAD_FORMAT;
1679 if( xml_get_dtd( file ) == 0 ) {
1680 if( xml_parse_next_tag( file ) == 0 ) {
1681 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1682 addrindex_read_index( addrIndex, file );
1683 addrIndex->retVal = MGU_SUCCESS;
1687 xml_close_file( file );
1689 addrindex_build_search_order( addrIndex );
1691 return addrIndex->retVal;
1694 static int addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1695 GList *nodeIF, *nodeDS;
1697 gint lvlItem = 1 + lvlList;
1699 nodeIF = addrIndex->interfaceList;
1701 AddressInterface *iface = nodeIF->data;
1702 if( ! iface->legacyFlag ) {
1703 nodeDS = iface->listSource;
1704 if (addrindex_write_elem_s( fp, lvlList, iface->listTag ) < 0)
1706 if (fputs( ">\n", fp ) == EOF)
1709 AddressDataSource *ds = nodeDS->data;
1711 if( iface->type == ADDR_IF_BOOK ) {
1712 if (addrindex_write_book( fp, ds, lvlItem ) < 0)
1715 if( iface->type == ADDR_IF_VCARD ) {
1716 if (addrindex_write_vcard( fp, ds, lvlItem ) < 0)
1719 if( iface->type == ADDR_IF_JPILOT ) {
1720 if (addrindex_write_jpilot( fp, ds, lvlItem ) < 0)
1723 if( iface->type == ADDR_IF_LDAP ) {
1724 if (addrindex_write_ldap( fp, ds, lvlItem ) < 0)
1728 nodeDS = g_list_next( nodeDS );
1730 if (addrindex_write_elem_e( fp, lvlList, iface->listTag ) < 0)
1733 nodeIF = g_list_next( nodeIF );
1739 * Write data to specified file.
1740 * Enter: addrIndex Address index object.
1741 * newFile New file name.
1742 * return: Status code, from addrIndex->retVal.
1743 * Note: File will be created in directory specified by addrIndex.
1745 static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1748 #ifndef DEV_STANDALONE
1752 g_return_val_if_fail( addrIndex != NULL, -1 );
1754 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1755 addrIndex->retVal = MGU_OPEN_FILE;
1756 #ifdef DEV_STANDALONE
1757 fp = g_fopen( fileSpec, "wb" );
1760 fputs( "<?xml version=\"1.0\" ?>\n", fp );
1762 pfile = prefs_write_open( fileSpec );
1766 if (fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n", CS_INTERNAL ) < 0)
1769 if (addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX ) < 0)
1771 if (fputs( ">\n", fp ) == EOF)
1774 if (addrindex_write_index( addrIndex, fp ) < 0)
1776 if (addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX ) < 0)
1779 addrIndex->retVal = MGU_SUCCESS;
1780 #ifdef DEV_STANDALONE
1783 if( prefs_file_close( pfile ) < 0 ) {
1784 addrIndex->retVal = MGU_ERROR_WRITE;
1790 return addrIndex->retVal;
1792 g_warning("error writing AB index\n");
1793 addrIndex->retVal = MGU_ERROR_WRITE;
1795 prefs_file_close_revert( pfile );
1796 return addrIndex->retVal;
1800 * Save address index data to original file.
1801 * return: Status code, from addrIndex->retVal.
1803 gint addrindex_save_data( AddressIndex *addrIndex ) {
1809 g_return_val_if_fail( addrIndex != NULL, -1 );
1812 nodeIf = addrIndex->interfaceList;
1813 /* save LDAP interfaces */
1815 AddressInterface *iface = nodeIf->data;
1816 if( iface->type == ADDR_IF_LDAP ) {
1817 nodeDS = iface->listSource;
1819 AddressDataSource *ds = nodeDS->data;
1820 LdapServer *abf = ds->rawDataSource;
1821 if( ldapsvr_get_read_flag( abf ) ) {
1822 if( ldapsvr_get_modified( abf ) ) {
1823 ldapsvr_update_book( abf, NULL );
1824 if( abf->retVal != LDAPRC_SUCCESS ) {
1825 alertpanel( _("Address(es) update"),
1826 _("Update failed. Changes not written to Directory."),
1827 GTK_STOCK_CLOSE, NULL, NULL );
1830 abf->retVal = MGU_SUCCESS;
1831 ldapsvr_set_modified( abf, FALSE );
1835 nodeDS = g_list_next( nodeDS );
1839 nodeIf = g_list_next( nodeIf );
1842 addrIndex->retVal = MGU_NO_FILE;
1843 if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1844 if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1846 addrindex_write_to( addrIndex, addrIndex->fileName );
1847 if( addrIndex->retVal == MGU_SUCCESS ) {
1848 addrIndex->dirtyFlag = FALSE;
1850 return addrIndex->retVal;
1854 * Save all address book files which may have changed.
1855 * Return: Status code, set if there was a problem saving data.
1857 gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1858 gint retVal = MGU_SUCCESS;
1859 GList *nodeIf, *nodeDS;
1861 nodeIf = addrIndex->interfaceList;
1863 AddressInterface *iface = nodeIf->data;
1864 if( iface->type == ADDR_IF_BOOK ) {
1865 nodeDS = iface->listSource;
1867 AddressDataSource *ds = nodeDS->data;
1868 AddressBookFile *abf = ds->rawDataSource;
1869 if( addrbook_get_dirty( abf ) ) {
1870 if( addrbook_get_read_flag( abf ) ) {
1871 addrbook_save_data( abf );
1872 if( abf->retVal != MGU_SUCCESS ) {
1873 retVal = abf->retVal;
1877 nodeDS = g_list_next( nodeDS );
1881 nodeIf = g_list_next( nodeIf );
1887 /* **********************************************************************
1888 * Address book conversion to new format.
1889 * ***********************************************************************
1892 #define ELTAG_IF_OLD_FOLDER "folder"
1893 #define ELTAG_IF_OLD_GROUP "group"
1894 #define ELTAG_IF_OLD_ITEM "item"
1895 #define ELTAG_IF_OLD_NAME "name"
1896 #define ELTAG_IF_OLD_ADDRESS "address"
1897 #define ELTAG_IF_OLD_REMARKS "remarks"
1898 #define ATTAG_IF_OLD_NAME "name"
1900 #define TEMPNODE_ROOT 0
1901 #define TEMPNODE_FOLDER 1
1902 #define TEMPNODE_GROUP 2
1903 #define TEMPNODE_ADDRESS 3
1905 typedef struct _AddressCvt_Node AddressCvtNode;
1906 struct _AddressCvt_Node {
1915 * Parse current address item.
1917 static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1922 nn = g_new0( AddressCvtNode, 1 );
1923 nn->type = TEMPNODE_ADDRESS;
1926 level = file->level;
1929 xml_parse_next_tag(file);
1930 if (file->level < level) return nn;
1932 element = xml_get_element( file );
1933 if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1934 nn->name = g_strdup( element );
1936 if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1937 nn->address = g_strdup( element );
1939 if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1940 nn->remarks = g_strdup( element );
1943 xml_parse_next_tag(file);
1948 * Create a temporary node below specified node.
1950 static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1952 nn = g_new0( AddressCvtNode, 1 );
1954 nn->name = g_strdup( name );
1955 nn->remarks = g_strdup( rem );
1956 node->list = g_list_append( node->list, nn );
1961 * Process current temporary node.
1963 static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1966 AddressCvtNode *newNode = NULL;
1971 prev_level = file->level;
1972 xml_parse_next_tag( file );
1973 if (file->level < prev_level) return;
1977 if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1978 attr = xml_get_current_tag_attr(file);
1980 name = ((XMLAttr *)attr->data)->name;
1981 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1982 value = ((XMLAttr *)attr->data)->value;
1985 newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1986 addrindex_add_obj( file, newNode );
1989 else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1990 attr = xml_get_current_tag_attr(file);
1992 name = ((XMLAttr *)attr->data)->name;
1993 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1994 value = ((XMLAttr *)attr->data)->value;
1997 newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1998 addrindex_add_obj( file, newNode );
2000 else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
2001 newNode = addrindex_parse_item( file );
2002 node->list = g_list_append( node->list, newNode );
2005 /* g_print( "invalid: !!! \n" ); */
2006 attr = xml_get_current_tag_attr( file );
2012 * Consume all nodes below current tag.
2014 static void addrindex_consume_tree( XMLFile *file ) {
2021 prev_level = file->level;
2022 xml_parse_next_tag( file );
2023 if (file->level < prev_level) return;
2025 xtag = xml_get_current_tag( file );
2026 /* g_print( "tag : %s\n", xtag->tag ); */
2027 element = xml_get_element( file );
2028 attr = xml_get_current_tag_attr( file );
2029 /* show_attribs( attr ); */
2030 /* g_print( "\ttag value : %s :\n", element ); */
2031 addrindex_consume_tree( file );
2036 * Print temporary tree.
2038 static void addrindex_print_node( AddressCvtNode *node, FILE *stream ) {
2041 fprintf( stream, "Node:\ttype :%d:\n", node->type );
2042 fprintf( stream, "\tname :%s:\n", node->name );
2043 fprintf( stream, "\taddr :%s:\n", node->address );
2044 fprintf( stream, "\trems :%s:\n", node->remarks );
2046 fprintf( stream, "\t--list----\n" );
2050 AddressCvtNode *lNode = list->data;
2051 list = g_list_next( list );
2052 addrindex_print_node( lNode, stream );
2054 fprintf( stream, "\t==list-%d==\n", node->type );
2058 * Free up temporary tree.
2060 static void addrindex_free_node( AddressCvtNode *node ) {
2061 GList *list = node->list;
2064 AddressCvtNode *lNode = list->data;
2065 list = g_list_next( list );
2066 addrindex_free_node( lNode );
2068 node->type = TEMPNODE_ROOT;
2069 g_free( node->name );
2070 g_free( node->address );
2071 g_free( node->remarks );
2072 g_list_free( node->list );
2077 * Process address book for specified node.
2079 static void addrindex_process_node(
2080 AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
2081 ItemGroup *parentGrp, ItemFolder *folderGrp )
2084 ItemFolder *itemFolder = NULL;
2085 ItemGroup *itemGParent = parentGrp;
2086 ItemFolder *itemGFolder = folderGrp;
2087 AddressCache *cache = abf->addressCache;
2089 if( node->type == TEMPNODE_ROOT ) {
2090 itemFolder = parent;
2092 else if( node->type == TEMPNODE_FOLDER ) {
2093 itemFolder = addritem_create_item_folder();
2094 addritem_folder_set_name( itemFolder, node->name );
2095 addrcache_id_folder( cache, itemFolder );
2096 addrcache_folder_add_folder( cache, parent, itemFolder );
2099 else if( node->type == TEMPNODE_GROUP ) {
2100 ItemGroup *itemGroup;
2103 /* Create a folder for group */
2104 fName = g_strdup_printf( "Cvt - %s", node->name );
2105 itemGFolder = addritem_create_item_folder();
2106 addritem_folder_set_name( itemGFolder, fName );
2107 addrcache_id_folder( cache, itemGFolder );
2108 addrcache_folder_add_folder( cache, parent, itemGFolder );
2111 /* Add group into folder */
2112 itemGroup = addritem_create_item_group();
2113 addritem_group_set_name( itemGroup, node->name );
2114 addrcache_id_group( cache, itemGroup );
2115 addrcache_folder_add_group( cache, itemGFolder, itemGroup );
2116 itemGParent = itemGroup;
2118 else if( node->type == TEMPNODE_ADDRESS ) {
2119 ItemPerson *itemPerson;
2120 ItemEMail *itemEMail;
2122 /* Create person and email objects */
2123 itemPerson = addritem_create_item_person();
2124 addritem_person_set_common_name( itemPerson, node->name );
2125 addrcache_id_person( cache, itemPerson );
2126 itemEMail = addritem_create_item_email();
2127 addritem_email_set_address( itemEMail, node->address );
2128 addritem_email_set_remarks( itemEMail, node->remarks );
2129 addrcache_id_email( cache, itemEMail );
2130 addrcache_person_add_email( cache, itemPerson, itemEMail );
2132 /* Add person into appropriate folder */
2134 addrcache_folder_add_person( cache, itemGFolder, itemPerson );
2137 addrcache_folder_add_person( cache, parent, itemPerson );
2140 /* Add email address only into group */
2142 addrcache_group_add_email( cache, parentGrp, itemEMail );
2148 AddressCvtNode *lNode = list->data;
2149 list = g_list_next( list );
2150 addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
2155 * Process address book to specified file number.
2157 static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
2158 gboolean retVal = FALSE;
2159 AddressBookFile *abf = NULL;
2160 AddressCvtNode *rootNode = NULL;
2161 gchar *newFile = NULL;
2162 GList *fileList = NULL;
2165 /* Setup root node */
2166 rootNode = g_new0( AddressCvtNode, 1 );
2167 rootNode->type = TEMPNODE_ROOT;
2168 rootNode->name = g_strdup( "root" );
2169 rootNode->list = NULL;
2170 addrindex_add_obj( file, rootNode );
2171 /* addrindex_print_node( rootNode, stdout ); */
2173 /* Create new address book */
2174 abf = addrbook_create_book();
2175 addrbook_set_name( abf, displayName );
2176 addrbook_set_path( abf, addrIndex->filePath );
2178 /* Determine next available file number */
2179 fileList = addrbook_get_bookfile_list( abf );
2181 fileNum = 1 + abf->maxValue;
2183 g_list_free( fileList );
2186 newFile = addrbook_gen_new_file_name( fileNum );
2188 addrbook_set_file( abf, newFile );
2191 addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
2193 /* addrbook_dump_book( abf, stdout ); */
2194 addrbook_save_data( abf );
2195 addrIndex->retVal = abf->retVal;
2196 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2198 addrbook_free_book( abf );
2200 addrindex_free_node( rootNode );
2203 /* Create entries in address index */
2205 abf = addrbook_create_book();
2206 addrbook_set_name( abf, displayName );
2207 addrbook_set_path( abf, addrIndex->filePath );
2208 addrbook_set_file( abf, newFile );
2209 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2216 * Process tree converting data.
2218 static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
2226 prev_level = file->level;
2227 xml_parse_next_tag( file );
2228 if (file->level < prev_level) return;
2230 xtag = xml_get_current_tag( file );
2231 /* g_print( "tag : %d : %s\n", prev_level, xtag->tag ); */
2232 if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
2233 if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
2234 addrIndex->needsConversion = FALSE;
2235 addrIndex->wasConverted = TRUE;
2240 if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
2241 if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
2242 addrIndex->needsConversion = FALSE;
2243 addrIndex->wasConverted = TRUE;
2248 element = xml_get_element( file );
2249 attr = xml_get_current_tag_attr( file );
2250 /* show_attribs( attr ); */
2251 /* g_print( "\ttag value : %s :\n", element ); */
2252 addrindex_consume_tree( file );
2256 static gint addrindex_convert_data( AddressIndex *addrIndex ) {
2257 XMLFile *file = NULL;
2260 fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
2261 addrIndex->retVal = MGU_NO_FILE;
2262 file = xml_open_file( fileSpec );
2265 if( file == NULL ) {
2266 /* g_print( " file '%s' does not exist.\n", addrIndex->fileName ); */
2267 return addrIndex->retVal;
2270 addrIndex->retVal = MGU_BAD_FORMAT;
2271 if( xml_get_dtd( file ) == 0 ) {
2272 if( xml_parse_next_tag( file ) == 0 ) {
2273 if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
2274 addrindex_convert_tree( addrIndex, file );
2278 xml_close_file( file );
2279 return addrIndex->retVal;
2283 * Create a new address book file.
2285 static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
2286 gboolean retVal = FALSE;
2287 AddressBookFile *abf = NULL;
2288 gchar *newFile = NULL;
2289 GList *fileList = NULL;
2292 /* Create new address book */
2293 abf = addrbook_create_book();
2294 addrbook_set_name( abf, displayName );
2295 addrbook_set_path( abf, addrIndex->filePath );
2297 /* Determine next available file number */
2298 fileList = addrbook_get_bookfile_list( abf );
2300 fileNum = 1 + abf->maxValue;
2302 g_list_free( fileList );
2305 newFile = addrbook_gen_new_file_name( fileNum );
2307 addrbook_set_file( abf, newFile );
2310 addrbook_save_data( abf );
2311 addrIndex->retVal = abf->retVal;
2312 if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2313 addrbook_free_book( abf );
2316 /* Create entries in address index */
2318 abf = addrbook_create_book();
2319 addrbook_set_name( abf, displayName );
2320 addrbook_set_path( abf, addrIndex->filePath );
2321 addrbook_set_file( abf, newFile );
2322 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2329 * Read data for address index performing a conversion if necesary.
2330 * Enter: addrIndex Address index object.
2331 * return: Status code, from addrIndex->retVal.
2332 * Note: New address book files will be created in directory specified by
2333 * addrIndex. Three files will be created, for the following:
2334 * "Common addresses"
2335 * "Personal addresses"
2336 * "Gathered addresses" - a new address book.
2338 gint addrindex_read_data( AddressIndex *addrIndex ) {
2339 g_return_val_if_fail( addrIndex != NULL, -1 );
2341 addrIndex->conversionError = FALSE;
2342 addrindex_read_file( addrIndex );
2343 if( addrIndex->retVal == MGU_SUCCESS ) {
2344 if( addrIndex->needsConversion ) {
2345 if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
2346 addrIndex->conversionError = TRUE;
2349 addrIndex->conversionError = TRUE;
2352 addrIndex->dirtyFlag = TRUE;
2354 return addrIndex->retVal;
2358 * Create new address books for a new address index.
2359 * Enter: addrIndex Address index object.
2360 * return: Status code, from addrIndex->retVal.
2361 * Note: New address book files will be created in directory specified by
2362 * addrIndex. Three files will be created, for the following:
2363 * "Common addresses"
2364 * "Personal addresses"
2365 * "Gathered addresses" - a new address book.
2367 gint addrindex_create_new_books( AddressIndex *addrIndex ) {
2370 g_return_val_if_fail( addrIndex != NULL, -1 );
2372 flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
2374 flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
2375 addrIndex->dirtyFlag = TRUE;
2377 return addrIndex->retVal;
2380 /* **********************************************************************
2381 * New interface stuff.
2382 * ***********************************************************************
2386 * Return modified flag for specified data source.
2388 gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
2389 gboolean retVal = FALSE;
2390 AddressInterface *iface;
2392 if( ds == NULL ) return retVal;
2393 iface = ds->interface;
2394 if( iface == NULL ) return retVal;
2395 if( iface->getModifyFlag ) {
2396 retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
2402 * Return accessed flag for specified data source.
2404 gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
2405 gboolean retVal = FALSE;
2406 AddressInterface *iface;
2408 if( ds == NULL ) return retVal;
2409 iface = ds->interface;
2410 if( iface == NULL ) return retVal;
2411 if( iface->getAccessFlag ) {
2412 retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
2418 * Return data read flag for specified data source.
2420 gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
2421 gboolean retVal = TRUE;
2422 AddressInterface *iface;
2424 if( ds == NULL ) return retVal;
2425 iface = ds->interface;
2426 if( iface == NULL ) return retVal;
2427 if( iface->getReadFlag ) {
2428 retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
2434 * Return status code for specified data source.
2436 gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
2437 gint retVal = MGU_SUCCESS;
2438 AddressInterface *iface;
2440 if( ds == NULL ) return retVal;
2441 iface = ds->interface;
2442 if( iface == NULL ) return retVal;
2443 if( iface->getStatusCode ) {
2444 retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
2450 * Return data read flag for specified data source.
2452 gint addrindex_ds_read_data( AddressDataSource *ds ) {
2453 gint retVal = MGU_SUCCESS;
2454 AddressInterface *iface;
2456 if( ds == NULL ) return retVal;
2457 iface = ds->interface;
2458 if( iface == NULL ) return retVal;
2459 if( iface->getReadData ) {
2461 gchar *name = ( iface->getName ) ( ds->rawDataSource );
2462 g_print( "addrindex_ds_read_data...reading:::%s:::\n", name );
2464 retVal = ( iface->getReadData ) ( ds->rawDataSource );
2470 * Return data read flag for specified data source.
2472 ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
2473 ItemFolder *retVal = NULL;
2474 AddressInterface *iface;
2476 if( ds == NULL ) return retVal;
2477 iface = ds->interface;
2478 if( iface == NULL ) return retVal;
2479 if( iface->getRootFolder ) {
2480 retVal = ( iface->getRootFolder ) ( ds->rawDataSource );
2486 * Return name for specified data source.
2488 gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
2489 gchar *retVal = FALSE;
2490 AddressInterface *iface;
2492 if( ds == NULL ) return retVal;
2493 iface = ds->interface;
2494 if( iface == NULL ) return retVal;
2495 if( iface->getName ) {
2496 retVal = ( iface->getName ) ( ds->rawDataSource );
2502 * Set the access flag inside the data source.
2504 void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
2505 AddressInterface *iface;
2507 if( ds == NULL ) return;
2508 iface = ds->interface;
2509 if( iface == NULL ) return;
2510 if( iface->setAccessFlag ) {
2511 ( iface->setAccessFlag ) ( ds->rawDataSource, value );
2516 * Return read only flag for specified data source.
2518 gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
2519 AddressInterface *iface;
2520 if( ds == NULL ) return TRUE;
2521 iface = ds->interface;
2522 if( iface == NULL ) return TRUE;
2523 return iface->readOnly;
2527 * Return list of all persons for specified data source.
2529 static GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
2530 GList *retVal = NULL;
2531 AddressInterface *iface;
2533 if( ds == NULL ) return retVal;
2534 iface = ds->interface;
2535 if( iface == NULL ) return retVal;
2536 if( iface->getAllPersons ) {
2537 retVal = ( iface->getAllPersons ) ( ds->rawDataSource );
2543 * Return list of all groups for specified data source.
2545 static GList *addrindex_ds_get_all_groups( AddressDataSource *ds ) {
2546 GList *retVal = NULL;
2547 AddressInterface *iface;
2549 if( ds == NULL ) return retVal;
2550 iface = ds->interface;
2551 if( iface == NULL ) return retVal;
2552 if( iface->getAllGroups ) {
2553 retVal = ( iface->getAllGroups ) ( ds->rawDataSource );
2558 /* **********************************************************************
2559 * Address search stuff.
2560 * ***********************************************************************
2564 * Setup or register the dynamic search that will be performed. The search
2565 * is registered with the query manager.
2567 * \param searchTerm Search term. A private copy will be made.
2568 * \param callBackEntry Callback function that should be called when
2569 * each entry is received.
2570 * \param callBackEnd Callback function that should be called when
2571 * search has finished running.
2572 * \return ID allocated to query that will be executed.
2574 gint addrindex_setup_search(
2575 const gchar *searchTerm, void *callBackEnd, void *callBackEntry )
2580 /* Set up a dynamic address query */
2581 req = qrymgr_add_request( searchTerm, callBackEnd, callBackEntry );
2582 queryID = req->queryID;
2583 qryreq_set_search_type( req, ADDRSEARCH_DYNAMIC );
2585 /* g_print( "***> query ID ::%d::\n", queryID ); */
2592 * Function prototypes (not in header file or circular reference errors are
2595 LdapQuery *ldapsvr_new_dynamic_search(
2596 LdapServer *server, QueryRequest *req );
2597 LdapQuery *ldapsvr_new_explicit_search(
2598 LdapServer *server, QueryRequest *req, ItemFolder *folder );
2599 void ldapsvr_execute_query( LdapServer *server, LdapQuery *qry );
2604 * Execute the previously registered dynamic search.
2606 * \param req Address query object to execute.
2607 * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2610 static gboolean addrindex_start_dynamic( QueryRequest *req ) {
2611 AddressInterface *iface;
2612 AddressDataSource *ds;
2617 /* g_print( "addrindex_start_dynamic::%d::\n", req->queryID ); */
2618 nodeIf = _addressIndex_->searchOrder;
2620 iface = nodeIf->data;
2621 nodeIf = g_list_next( nodeIf );
2623 if( ! iface->useInterface ) {
2626 if( ! iface->externalQuery ) {
2631 nodeDS = iface->listSource;
2634 nodeDS = g_list_next( nodeDS );
2636 if( type == ADDR_IF_LDAP ) {
2640 server = ds->rawDataSource;
2641 if( ! server->searchFlag ) {
2644 if( ldapsvr_reuse_previous( server, req ) ) {
2648 /* Start a new dynamic search */
2649 qry = ldapsvr_new_dynamic_search( server, req );
2651 ldapsvr_execute_query( server, qry );
2661 * Stop the previously registered search.
2663 * \param queryID ID of search query to stop.
2665 void addrindex_stop_search( const gint queryID ){
2667 AddrQueryObject *aqo;
2670 /* g_print( "addrindex_stop_search/queryID=%d\n", queryID ); */
2671 /* If query ID does not match, search has not been setup */
2672 req = qrymgr_find_request( queryID );
2677 /* Stop all queries that were associated with request */
2678 node = req->queryList;
2682 if( aqo->queryType == ADDRQUERY_LDAP ) {
2683 LdapQuery *qry = ( LdapQuery * ) aqo;
2684 ldapqry_set_stop_flag( qry, TRUE );
2688 node = g_list_next( node );
2691 /* Delete query request */
2692 qrymgr_delete_request( queryID );
2696 * Setup or register the explicit search that will be performed. The search is
2697 * registered with the query manager.
2699 * \param ds Data source to search.
2700 * \param searchTerm Search term to locate.
2701 * \param folder Folder to receive search results; may be NULL.
2702 * \param callbackEnd Function to call when search has terminated.
2703 * \param callbackEntry Function to called for each entry processed.
2704 * \return ID allocated to query that will be executed.
2706 gint addrindex_setup_explicit_search(
2707 AddressDataSource *ds, const gchar *searchTerm, ItemFolder *folder,
2708 void *callBackEnd, void *callBackEntry )
2715 /* Name the query */
2716 name = g_strdup_printf( "Search '%s'", searchTerm );
2718 /* Set up query request */
2719 if (!strcmp(searchTerm, "*"))
2720 mySearch = g_strdup("*@");
2722 mySearch = g_strdup(searchTerm);
2724 req = qrymgr_add_request( mySearch, callBackEnd, callBackEntry );
2728 qryreq_set_search_type( req, ADDRSEARCH_EXPLICIT );
2729 queryID = req->queryID;
2731 if( ds->type == ADDR_IF_LDAP ) {
2735 server = ds->rawDataSource;
2736 ldapsvr_new_explicit_search( server, req, folder );
2740 qrymgr_delete_request( queryID );
2749 * Execute the previously registered explicit search.
2751 * \param req Address query request object to execute.
2752 * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2755 static gboolean addrindex_start_explicit( QueryRequest *req ) {
2757 AddrQueryObject *aqo;
2761 /* Note: there should only be one query in the list. */
2762 aqo = req->queryList->data;
2764 if( aqo->queryType == ADDRQUERY_LDAP ) {
2768 qry = ( LdapQuery * ) aqo;
2769 server = qry->server;
2771 /* Start the search */
2773 ldapsvr_execute_query( server, qry );
2780 * Start the previously registered search.
2782 * \param queryID ID of search query to be executed.
2783 * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2786 gboolean addrindex_start_search( const gint queryID ) {
2789 AddrSearchType searchType;
2792 /* g_print( "addrindex_start_search/queryID=%d\n", queryID ); */
2793 req = qrymgr_find_request( queryID );
2798 searchType = req->searchType;
2799 if( searchType == ADDRSEARCH_DYNAMIC ) {
2800 retVal = addrindex_start_dynamic( req );
2802 else if( searchType == ADDRSEARCH_EXPLICIT ) {
2803 retVal = addrindex_start_explicit( req );
2810 * Remove results (folder and data) for specified data source and folder.
2811 * \param ds Data source to process.
2812 * \param folder Results folder to remove.
2814 void addrindex_remove_results( AddressDataSource *ds, ItemFolder *folder ) {
2815 AddrBookBase *adbase;
2816 AddressCache *cache;
2819 /* g_print( "addrindex_remove_results/start\n" ); */
2821 /* Test for folder */
2822 if( folder->folderType != ADDRFOLDER_QUERY_RESULTS ) return;
2823 /* g_print( "folder name ::%s::\n", ADDRITEM_NAME(folder) ); */
2824 adbase = ( AddrBookBase * ) ds->rawDataSource;
2825 if( adbase == NULL ) return;
2826 cache = adbase->addressCache;
2828 /* Hide folder to prevent re-display */
2829 addritem_folder_set_hidden( folder, TRUE );
2831 if( ds->type == ADDR_IF_LDAP ) {
2836 qry = ( LdapQuery * ) folder->folderData;
2837 queryID = ADDRQUERY_ID(qry);
2838 /* g_print( "calling ldapquery_remove_results...queryID=%d\n", queryID ); */
2839 delFlag = ldapquery_remove_results( qry );
2841 ldapqry_free( qry );
2843 /* g_print( "calling ldapquery_remove_results...done\n" ); */
2846 g_print( "delFlag IS-TRUE\n" );
2849 g_print( "delFlag IS-FALSE\n" );
2854 /* g_print( "addrindex_remove_results/end\n" ); */
2856 /* Delete query request */
2858 qrymgr_delete_request( queryID );
2862 /* **********************************************************************
2863 * Address completion stuff.
2864 * ***********************************************************************
2867 static void addrindex_load_completion_load_persons(
2868 gint (*callBackFunc) ( const gchar *, const gchar *,
2869 const gchar *, const gchar *, GList * ),
2870 AddressDataSource *ds)
2872 GList *listP, *nodeP;
2876 /* Read address book */
2877 if( addrindex_ds_get_modify_flag( ds ) ) {
2878 addrindex_ds_read_data( ds );
2881 if( ! addrindex_ds_get_read_flag( ds ) ) {
2882 addrindex_ds_read_data( ds );
2885 /* Get all groups */
2886 listP = addrindex_ds_get_all_groups( ds );
2889 ItemGroup *group = nodeP->data;
2890 GList *emails = NULL;
2891 for (nodeM = group->listEMail; nodeM; nodeM = g_list_next(nodeM)) {
2892 ItemEMail *email = nodeM->data;
2894 emails = g_list_append(emails, email);
2896 callBackFunc( ((AddrItemObject *)group)->name, NULL,
2897 NULL, NULL, emails );
2898 nodeP = g_list_next( nodeP );
2901 /* Free up the list */
2902 g_list_free( listP );
2903 /* Get all persons */
2904 listP = addrindex_ds_get_all_persons( ds );
2907 ItemPerson *person = nodeP->data;
2908 nodeM = person->listEMail;
2910 /* Figure out name to use */
2911 sName = ADDRITEM_NAME(person);
2912 if( sName == NULL || *sName == '\0' ) {
2913 sName = person->nickName;
2916 /* Process each E-Mail address */
2918 ItemEMail *email = nodeM->data;
2920 callBackFunc( sName, email->address, person->nickName,
2921 ADDRITEM_NAME(email), NULL );
2923 nodeM = g_list_next( nodeM );
2925 nodeP = g_list_next( nodeP );
2928 /* Free up the list */
2929 g_list_free( listP );
2933 * This function is used by the address completion function to load
2934 * addresses for all non-external address book interfaces.
2936 * \param callBackFunc Function to be called when an address is
2938 * \param folderpath Addressbook's Book/folder path to restrict to (if NULL or ""
2939 * or "Any", assume the whole addressbook
2940 * \return <i>TRUE</i> if data loaded, <i>FALSE</i> if address index not loaded.
2943 gboolean addrindex_load_completion(
2944 gint (*callBackFunc) ( const gchar *, const gchar *,
2945 const gchar *, const gchar *, GList * ),
2948 GList *nodeIf, *nodeDS;
2950 if( folderpath != NULL ) {
2951 AddressDataSource *book;
2954 /* split the folder path we've received, we'll try to match this path, subpath by
2955 subpath against the book/folder structure in order and restrict loading of
2956 addresses to that subpart (if matches). book/folder path must exist and
2957 folderpath must not be empty or NULL */
2959 if( ! addressbook_peek_folder_exists( folderpath, &book, &folder ) ) {
2960 g_warning("addrindex_load_completion: folder path '%s' doesn't exist\n", folderpath);
2964 if( folder != NULL ) {
2971 debug_print("addrindex_load_completion: folder %p '%s'\n", folder, folder->obj.name);
2973 /* Load email addresses */
2974 items = addritem_folder_get_person_list( folder );
2975 for( ; items != NULL; items = g_list_next( items ) ) {
2976 person = items->data;
2977 nodeM = person->listEMail;
2979 /* Figure out name to use */
2980 sName = ADDRITEM_NAME(person);
2981 if( sName == NULL || *sName == '\0' ) {
2982 sName = person->nickName;
2985 /* Process each E-Mail address */
2987 ItemEMail *email = nodeM->data;
2989 callBackFunc( sName, email->address, person->nickName,
2990 ADDRITEM_NAME(email), NULL );
2992 nodeM = g_list_next( nodeM );
2995 /* Free up the list */
2996 mgu_clear_list( items );
2997 g_list_free( items );
3003 if( book != NULL ) {
3005 AddressBookFile *abf = book->rawDataSource;
3007 debug_print("addrindex_load_completion: book %p '%s'\n", book, abf?abf->fileName:"(null)");
3009 addrindex_load_completion_load_persons( callBackFunc, book );
3014 g_warning("addrindex_load_completion: book/folder path is valid but got no pointer\n");
3021 nodeIf = addrindex_get_interface_list( _addressIndex_ );
3023 AddressInterface *iface = nodeIf->data;
3025 nodeIf = g_list_next( nodeIf );
3027 if( ! iface->useInterface || iface->externalQuery )
3030 nodeDS = iface->listSource;
3032 addrindex_load_completion_load_persons( callBackFunc, nodeDS->data );
3033 nodeDS = g_list_next( nodeDS );
3042 * This function can be used to collect information about
3043 * addressbook entries that contain a specific attribute.
3045 * \param attr Name of attribute to look for
3046 * \param callBackFunc Function to be called when a matching attribute was found
3047 * \return <i>TRUE</i>
3049 gboolean addrindex_load_person_attribute(
3051 gint (*callBackFunc) ( ItemPerson *, const gchar * ) )
3053 AddressDataSource *ds;
3054 GList *nodeIf, *nodeDS;
3055 GList *listP, *nodeP;
3058 nodeIf = addrindex_get_interface_list( _addressIndex_ );
3061 AddressInterface *iface = nodeIf->data;
3063 nodeIf = g_list_next( nodeIf );
3065 if( ! iface->useInterface || iface->externalQuery )
3068 nodeDS = iface->listSource;
3072 /* Read address book */
3073 if( addrindex_ds_get_modify_flag( ds ) ) {
3074 addrindex_ds_read_data( ds );
3077 if( ! addrindex_ds_get_read_flag( ds ) ) {
3078 addrindex_ds_read_data( ds );
3081 /* Check addressbook name */
3082 cur_bname = addrindex_ds_get_name( ds );
3084 /* Get all persons */
3085 listP = addrindex_ds_get_all_persons( ds );
3088 ItemPerson *person = nodeP->data;
3090 /* Return all ItemPerson's if attr is NULL */
3091 if( attr == NULL ) {
3092 callBackFunc(person, cur_bname);
3095 /* Return ItemPerson's with specific attribute */
3097 nodeA = person->listAttrib;
3098 /* Process each User Attribute */
3100 UserAttribute *attrib = nodeA->data;
3102 !strcmp( attrib->name,attr ) ) {
3103 callBackFunc(person, cur_bname);
3105 nodeA = g_list_next( nodeA );
3108 nodeP = g_list_next( nodeP );
3110 /* Free up the list */
3111 g_list_free( listP );
3113 nodeDS = g_list_next( nodeDS );
3120 * This function can be used to collect information about
3121 * addressbook entries
3123 * \param callBackFunc Function to be called for each ItemPerson
3124 * \return <i>TRUE</i>
3126 gboolean addrindex_load_person_ds( gint (*callBackFunc)
3127 ( ItemPerson *, AddressDataSource * ) )
3129 AddressDataSource *ds;
3130 GList *nodeIf, *nodeDS;
3131 GList *listP, *nodeP;
3133 nodeIf = addrindex_get_interface_list( _addressIndex_ );
3135 AddressInterface *iface = nodeIf->data;
3137 nodeIf = g_list_next( nodeIf );
3139 if( ! iface->useInterface || iface->externalQuery )
3142 nodeDS = iface->listSource;
3146 /* Read address book */
3147 if( addrindex_ds_get_modify_flag( ds ) ) {
3148 addrindex_ds_read_data( ds );
3151 if( ! addrindex_ds_get_read_flag( ds ) ) {
3152 addrindex_ds_read_data( ds );
3155 /* Get all persons */
3156 listP = addrindex_ds_get_all_persons( ds );
3159 ItemPerson *person = nodeP->data;
3161 callBackFunc(person, ds);
3162 nodeP = g_list_next( nodeP );
3164 /* Free up the list */
3165 g_list_free( listP );
3167 nodeDS = g_list_next( nodeDS );
3173 gchar *addrindex_get_picture_file(const gchar *emailaddr)
3175 AddressDataSource *ds;
3176 GList *nodeIf, *nodeDS;
3177 GList *listP, *nodeP;
3178 gboolean found = FALSE;
3179 gchar *filename = NULL;
3180 gchar *raw_addr = NULL;
3185 Xstrdup_a(raw_addr, emailaddr, return NULL);
3186 extract_address(raw_addr);
3188 nodeIf = addrindex_get_interface_list( _addressIndex_ );
3190 AddressInterface *iface = nodeIf->data;
3192 nodeIf = g_list_next( nodeIf );
3194 if( ! iface->useInterface || iface->externalQuery )
3197 nodeDS = iface->listSource;
3198 while( nodeDS && !found) {
3201 /* Read address book */
3202 if( addrindex_ds_get_modify_flag( ds ) ) {
3203 addrindex_ds_read_data( ds );
3206 if( ! addrindex_ds_get_read_flag( ds ) ) {
3207 addrindex_ds_read_data( ds );
3210 /* Get all persons */
3211 listP = addrindex_ds_get_all_persons( ds );
3215 ItemPerson *person = nodeP->data;
3216 nodeM = person->listEMail;
3218 ItemEMail *email = nodeM->data;
3219 if (email->address && !strcasecmp(raw_addr, email->address)) {
3221 filename = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S,
3222 ADDRBOOK_DIR, G_DIR_SEPARATOR_S,
3223 person->picture, ".png", NULL );
3226 nodeM = nodeM->next;
3228 nodeP = g_list_next( nodeP );
3230 /* Free up the list */
3231 g_list_free( listP );
3233 nodeDS = g_list_next( nodeDS );