2005-03-30 [colin] 1.9.6cvs14
[claws.git] / src / addrindex.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2004 Match Grun
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
21  * General functions for accessing address index file.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32
33 #include "mgutils.h"
34 #include "addritem.h"
35 #include "addrcache.h"
36 #include "addrbook.h"
37 #include "addrindex.h"
38 #include "xml.h"
39 #include "addrquery.h"
40 #include "addr_compl.h"
41
42 #ifndef DEV_STANDALONE
43 #include "prefs_gtk.h"
44 #include "codeconv.h"
45 #endif
46
47 #include "vcard.h"
48
49 #ifdef USE_JPILOT
50 #include "jpilot.h"
51 #endif
52
53 #ifdef USE_LDAP
54 #include "ldapserver.h"
55 #include "ldapctrl.h"
56 #include "ldapquery.h"
57 #endif
58
59 #define TAG_ADDRESS_INDEX    "addressbook"
60
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"
65
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"
70
71 /* XML Attribute names */
72 #define ATTAG_BOOK_NAME       "name"
73 #define ATTAG_BOOK_FILE       "file"
74
75 #define ATTAG_VCARD_NAME      "name"
76 #define ATTAG_VCARD_FILE      "file"
77
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-"
85
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"
97 #define ATTAG_LDAP_MATCH_OPT  "match-opt"
98 #define ATTAG_LDAP_ENABLE_TLS "enable-tls"
99
100 #define ELTAG_LDAP_ATTR_SRCH  "attribute"
101 #define ATTAG_LDAP_ATTR_NAME  "name"
102
103 /* Attribute values */
104 #define ATVAL_BOOLEAN_YES         "yes"
105 #define ATVAL_BOOLEAN_NO          "no"
106 #define ATVAL_LDAP_MATCH_BEGIN    "begin-with"
107 #define ATVAL_LDAP_MATCH_CONTAINS "contains"
108
109 /* New attributes */
110 #define ATTAG_LDAP_DEFAULT    "default"
111
112 #if 0
113 N_("Common address")
114 N_("Personal address")
115 #endif
116
117 #define DISP_NEW_COMMON       _("Common address")
118 #define DISP_NEW_PERSONAL     _("Personal address")
119
120 /* Old address book */
121 #define TAG_IF_OLD_COMMON     "common_address"
122 #define TAG_IF_OLD_PERSONAL   "personal_address"
123
124 #define DISP_OLD_COMMON       _("Common address")
125 #define DISP_OLD_PERSONAL     _("Personal address")
126
127 /**
128  * Singleton object.
129  */
130 static AddressIndex *_addressIndex_ = NULL;
131
132 /*
133  * Define attribute name-value pair.
134  */
135 typedef struct _AddressIfAttr AddressIfAttrib;
136 struct _AddressIfAttr {
137         gchar *name;
138         gchar *value;
139 };
140
141 /*
142  * Define DOM fragment.
143  */
144 typedef struct _AddressIfFrag AddressIfFragment;
145 struct _AddressIfFrag {
146         gchar *name;
147         GList *children;
148         GList *attributes;
149 };
150
151 /**
152  * Build interface with default values.
153  *
154  * \param type Interface type.
155  * \param name Interface name.
156  * \param tagIf XML tag name for interface in address index file.
157  * \param tagDS XML tag name for datasource in address index file.
158  * \return Address interface object.
159 */
160 static AddressInterface *addrindex_create_interface(
161                 gint type, gchar *name, gchar *tagIf, gchar *tagDS )
162 {
163         AddressInterface *iface = g_new0( AddressInterface, 1 );
164
165         ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
166         ADDRITEM_ID(iface) = NULL;
167         ADDRITEM_NAME(iface) = g_strdup( name );
168         ADDRITEM_PARENT(iface) = NULL;
169         ADDRITEM_SUBTYPE(iface) = type;
170         iface->type = type;
171         iface->name = g_strdup( name );
172         iface->listTag = g_strdup( tagIf );
173         iface->itemTag = g_strdup( tagDS );
174         iface->legacyFlag = FALSE;
175         iface->haveLibrary = TRUE;
176         iface->useInterface = TRUE;
177         iface->readOnly      = TRUE;
178
179         /* Set callbacks to NULL values - override for each interface */
180         iface->getAccessFlag = NULL;
181         iface->getModifyFlag = NULL;
182         iface->getReadFlag   = NULL;
183         iface->getStatusCode = NULL;
184         iface->getReadData   = NULL;
185         iface->getRootFolder = NULL;
186         iface->getListFolder = NULL;
187         iface->getListPerson = NULL;
188         iface->getAllPersons = NULL;
189         iface->getAllGroups  = NULL;
190         iface->getName       = NULL;
191         iface->listSource = NULL;
192
193         /* Search stuff */
194         iface->externalQuery = FALSE;
195         iface->searchOrder = 0;         /* Ignored */
196         iface->startSearch = NULL;
197         iface->stopSearch = NULL;
198
199         return iface;
200 }
201
202 /**
203  * Build table of of all address book interfaces.
204  * \param addrIndex Address index object.
205  */
206 static void addrindex_build_if_list( AddressIndex *addrIndex ) {
207         AddressInterface *iface;
208
209         /* Create intrinsic XML address book interface */
210         iface = addrindex_create_interface(
211                         ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK,
212                         TAG_DS_ADDRESS_BOOK );
213         iface->readOnly      = FALSE;
214         iface->getModifyFlag = ( void * ) addrbook_get_modified;
215         iface->getAccessFlag = ( void * ) addrbook_get_accessed;
216         iface->getReadFlag   = ( void * ) addrbook_get_read_flag;
217         iface->getStatusCode = ( void * ) addrbook_get_status;
218         iface->getReadData   = ( void * ) addrbook_read_data;
219         iface->getRootFolder = ( void * ) addrbook_get_root_folder;
220         iface->getListFolder = ( void * ) addrbook_get_list_folder;
221         iface->getListPerson = ( void * ) addrbook_get_list_person;
222         iface->getAllPersons = ( void * ) addrbook_get_all_persons;
223         iface->getName       = ( void * ) addrbook_get_name;
224         iface->setAccessFlag = ( void * ) addrbook_set_accessed;
225         iface->searchOrder   = 0;
226
227         /* Add to list of interfaces in address book */ 
228         addrIndex->interfaceList =
229                 g_list_append( addrIndex->interfaceList, iface );
230         ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
231
232         /* Create vCard interface */
233         iface = addrindex_create_interface(
234                         ADDR_IF_VCARD, "vCard", TAG_IF_VCARD, TAG_DS_VCARD );
235         iface->getModifyFlag = ( void * ) vcard_get_modified;
236         iface->getAccessFlag = ( void * ) vcard_get_accessed;
237         iface->getReadFlag   = ( void * ) vcard_get_read_flag;
238         iface->getStatusCode = ( void * ) vcard_get_status;
239         iface->getReadData   = ( void * ) vcard_read_data;
240         iface->getRootFolder = ( void * ) vcard_get_root_folder;
241         iface->getListFolder = ( void * ) vcard_get_list_folder;
242         iface->getListPerson = ( void * ) vcard_get_list_person;
243         iface->getAllPersons = ( void * ) vcard_get_all_persons;
244         iface->getName       = ( void * ) vcard_get_name;
245         iface->setAccessFlag = ( void * ) vcard_set_accessed;
246         iface->searchOrder   = 0;
247         addrIndex->interfaceList =
248                 g_list_append( addrIndex->interfaceList, iface );
249         ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
250
251         /* Create JPilot interface */
252         iface = addrindex_create_interface(
253                         ADDR_IF_JPILOT, "J-Pilot", TAG_IF_JPILOT,
254                         TAG_DS_JPILOT );
255 #ifdef USE_JPILOT
256         iface->haveLibrary = jpilot_test_pilot_lib();
257         iface->useInterface = iface->haveLibrary;
258         iface->getModifyFlag = ( void * ) jpilot_get_modified;
259         iface->getAccessFlag = ( void * ) jpilot_get_accessed;
260         iface->getReadFlag   = ( void * ) jpilot_get_read_flag;
261         iface->getStatusCode = ( void * ) jpilot_get_status;
262         iface->getReadData   = ( void * ) jpilot_read_data;
263         iface->getRootFolder = ( void * ) jpilot_get_root_folder;
264         iface->getListFolder = ( void * ) jpilot_get_list_folder;
265         iface->getListPerson = ( void * ) jpilot_get_list_person;
266         iface->getAllPersons = ( void * ) jpilot_get_all_persons;
267         iface->getName       = ( void * ) jpilot_get_name;
268         iface->setAccessFlag = ( void * ) jpilot_set_accessed;
269         iface->searchOrder   = 0;
270 #else
271         iface->useInterface = FALSE;
272         iface->haveLibrary = FALSE;
273 #endif
274         addrIndex->interfaceList =
275                 g_list_append( addrIndex->interfaceList, iface );
276         ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
277
278         /* Create LDAP interface */
279         iface = addrindex_create_interface(
280                         ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
281 #ifdef USE_LDAP
282         /* iface->haveLibrary = ldapsvr_test_ldap_lib(); */
283         iface->haveLibrary = ldaputil_test_ldap_lib();
284         iface->useInterface = iface->haveLibrary;
285         /* iface->getModifyFlag = ( void * ) ldapsvr_get_modified; */
286         iface->getAccessFlag = ( void * ) ldapsvr_get_accessed;
287         /* iface->getReadFlag   = ( void * ) ldapsvr_get_read_flag; */
288         iface->getStatusCode = ( void * ) ldapsvr_get_status;
289         /* iface->getReadData   = ( void * ) ldapsvr_read_data; */
290         iface->getRootFolder = ( void * ) ldapsvr_get_root_folder;
291         iface->getListFolder = ( void * ) ldapsvr_get_list_folder;
292         iface->getListPerson = ( void * ) ldapsvr_get_list_person;
293         iface->getName       = ( void * ) ldapsvr_get_name;
294         iface->setAccessFlag = ( void * ) ldapsvr_set_accessed;
295         iface->externalQuery = TRUE;
296         iface->searchOrder   = 1;
297 #else
298         iface->useInterface = FALSE;
299         iface->haveLibrary = FALSE;
300 #endif
301         addrIndex->interfaceList =
302                 g_list_append( addrIndex->interfaceList, iface );
303         ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
304
305         /* Two old legacy data sources (pre 0.7.0) */
306         iface = addrindex_create_interface(
307                         ADDR_IF_COMMON, "Old Address - common",
308                         TAG_IF_OLD_COMMON, NULL );
309         iface->legacyFlag = TRUE;
310         addrIndex->interfaceList =
311                 g_list_append( addrIndex->interfaceList, iface );
312         ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
313
314         iface = addrindex_create_interface(
315                         ADDR_IF_COMMON, "Old Address - personal",
316                         TAG_IF_OLD_PERSONAL, NULL );
317         iface->legacyFlag = TRUE;
318         addrIndex->interfaceList =
319                 g_list_append( addrIndex->interfaceList, iface );
320         ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
321
322 }
323
324 /**
325  * Free DOM fragment.
326  * \param fragment Fragment to free.
327  */
328 static void addrindex_free_fragment( AddressIfFragment *fragment ) {
329         GList *node;
330
331         /* Free children */
332         node = fragment->children;
333         while( node ) {
334                 AddressIfFragment *child = node->data;
335                 addrindex_free_fragment( child );
336                 node->data = NULL;
337                 node = g_list_next( node );
338         }
339         g_list_free( fragment->children );
340
341         /* Free attributes */
342         node = fragment->attributes;
343         while( node ) {
344                 AddressIfAttrib *nv = node->data;
345                 g_free( nv->name );
346                 g_free( nv->value );
347                 g_free( nv );
348                 node->data = NULL;
349                 node = g_list_next( node );
350         }
351         g_list_free( fragment->attributes );
352
353         g_free( fragment->name );
354         fragment->name = NULL;
355         fragment->attributes = NULL;
356         fragment->children = NULL;
357
358         g_free( fragment );
359 }
360
361 /**
362  * Create a new data source.
363  * \param ifType Interface type to create.
364  * \return Initialized data source.
365  */
366 AddressDataSource *addrindex_create_datasource( AddressIfType ifType ) {
367         AddressDataSource *ds = g_new0( AddressDataSource, 1 );
368
369         ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
370         ADDRITEM_ID(ds) = NULL;
371         ADDRITEM_NAME(ds) = NULL;
372         ADDRITEM_PARENT(ds) = NULL;
373         ADDRITEM_SUBTYPE(ds) = 0;
374         ds->type = ifType;
375         ds->rawDataSource = NULL;
376         ds->interface = NULL;
377         return ds;
378 }
379
380 /**
381  * Free up data source.
382  * \param ds Data source to free.
383  */
384 void addrindex_free_datasource( AddressDataSource *ds ) {
385         AddressInterface *iface;
386
387         g_return_if_fail( ds != NULL );
388
389         iface = ds->interface;
390         if( ds->rawDataSource != NULL ) {
391                 if( iface != NULL ) {
392                         if( iface->useInterface ) {
393                                 if( iface->type == ADDR_IF_BOOK ) {
394                                         AddressBookFile *abf = ds->rawDataSource;
395                                         addrbook_free_book( abf );
396                                 }
397                                 else if( iface->type == ADDR_IF_VCARD ) {
398                                         VCardFile *vcf = ds->rawDataSource;
399                                         vcard_free( vcf );
400                                 }
401 #ifdef USE_JPILOT
402                                 else if( iface->type == ADDR_IF_JPILOT ) {
403                                         JPilotFile *jpf = ds->rawDataSource;
404                                         jpilot_free( jpf );
405                                 }
406 #endif
407 #ifdef USE_LDAP
408                                 else if( iface->type == ADDR_IF_LDAP ) {
409                                         LdapServer *server = ds->rawDataSource;
410                                         ldapsvr_free( server );
411                                 }
412 #endif
413                                 else {
414                                 }
415                         }
416                         else {
417                                 AddressIfFragment *fragment = ds->rawDataSource;
418                                 addrindex_free_fragment( fragment );
419                         }
420                 }
421         }
422
423         ADDRITEM_TYPE(ds) = ITEMTYPE_NONE;
424         ADDRITEM_ID(ds) = NULL;
425         ADDRITEM_NAME(ds) = NULL;
426         ADDRITEM_PARENT(ds) = NULL;
427         ADDRITEM_SUBTYPE(ds) = 0;
428         ds->type = ADDR_IF_NONE;
429         ds->interface = NULL;
430         ds->rawDataSource = NULL;
431
432         g_free( ds );
433 }
434
435 /**
436  * Free up all data sources for specified interface.
437  * \param iface Address interface to process.
438  */
439 static void addrindex_free_all_datasources( AddressInterface *iface ) {
440         GList *node = iface->listSource;
441         while( node ) {
442                 AddressDataSource *ds = node->data;
443                 addrindex_free_datasource( ds );
444                 node->data = NULL;
445                 node = g_list_next( node );
446         }
447 }
448
449 /**
450  * Free up specified interface.
451  * \param iface Interface to process.
452  */
453 static void addrindex_free_interface( AddressInterface *iface ) {
454         /* Free up data sources */
455         addrindex_free_all_datasources( iface );
456         g_list_free( iface->listSource );
457
458         /* Free internal storage */
459         g_free( ADDRITEM_ID(iface) );
460         g_free( ADDRITEM_NAME(iface) );
461         g_free( iface->name );
462         g_free( iface->listTag );
463         g_free( iface->itemTag );
464
465         /* Clear all pointers */
466         ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
467         ADDRITEM_ID(iface) = NULL;
468         ADDRITEM_NAME(iface) = NULL;
469         ADDRITEM_PARENT(iface) = NULL;
470         ADDRITEM_SUBTYPE(iface) = 0;
471         iface->type = ADDR_IF_NONE;
472         iface->name = NULL;
473         iface->listTag = NULL;
474         iface->itemTag = NULL;
475         iface->legacyFlag = FALSE;
476         iface->useInterface = FALSE;
477         iface->haveLibrary = FALSE;
478         iface->listSource = NULL;
479
480         /* Search stuff */
481         iface->searchOrder = 0;
482         iface->startSearch = NULL;
483         iface->stopSearch = NULL;
484
485         g_free( iface );
486 }
487
488 /**
489  * Return cache ID for specified data source.
490  *
491  * \param  addrIndex Address index.
492  * \param  ds        Data source.
493  * \return ID or NULL if not found. This should be <code>g_free()</code>
494  *         when done.
495  */
496 gchar *addrindex_get_cache_id( AddressIndex *addrIndex, AddressDataSource *ds ) {
497         gchar *cacheID = NULL;
498         AddrBookBase *adbase;
499         AddressCache *cache;
500
501         g_return_val_if_fail( addrIndex != NULL, NULL );
502         g_return_val_if_fail( ds != NULL, NULL );
503
504         adbase = ( AddrBookBase * ) ds->rawDataSource;
505         if( adbase ) {
506                 cache = adbase->addressCache;
507                 if( cache ) {
508                         cacheID = g_strdup( cache->cacheID );
509                 }
510         }
511
512         return cacheID;
513 }
514
515 /**
516  * Return reference to data source for specified cacheID.
517  * \param addrIndex Address index.
518  * \param cacheID   ID.
519  * \return Data source, or NULL if not found.
520  */
521 AddressDataSource *addrindex_get_datasource(
522                 AddressIndex *addrIndex, const gchar *cacheID )
523 {
524         g_return_val_if_fail( addrIndex != NULL, NULL );
525         g_return_val_if_fail( cacheID != NULL, NULL );
526         return ( AddressDataSource * ) g_hash_table_lookup( addrIndex->hashCache, cacheID );
527 }
528
529 /**
530  * Return reference to address cache for specified cacheID.
531  * \param addrIndex Address index.
532  * \param cacheID   ID.
533  * \return Address cache, or NULL if not found.
534  */
535 AddressCache *addrindex_get_cache( AddressIndex *addrIndex, const gchar *cacheID ) {
536         AddressDataSource *ds;
537         AddrBookBase *adbase;
538         AddressCache *cache;
539
540         g_return_val_if_fail( addrIndex != NULL, NULL );
541         g_return_val_if_fail( cacheID != NULL, NULL );
542
543         cache = NULL;
544         ds = addrindex_get_datasource( addrIndex, cacheID );
545         if( ds ) {
546                 adbase = ( AddrBookBase * ) ds->rawDataSource;
547                 cache = adbase->addressCache;
548         }
549         return cache;
550 }
551
552 /**
553  * Add data source into hash table.
554  * \param addrIndex Address index.
555  * \param ds        Data source.
556  */
557 static void addrindex_hash_add_cache(
558                 AddressIndex *addrIndex, AddressDataSource *ds )
559 {
560         gchar *cacheID;
561
562         cacheID = addrindex_get_cache_id( addrIndex, ds );
563         if( cacheID ) {
564                 g_hash_table_insert( addrIndex->hashCache, cacheID, ds );
565         }
566 }
567
568 /*
569  * Free hash table callback function.
570  */
571 static gboolean addrindex_free_cache_cb( gpointer key, gpointer value, gpointer data ) {
572         g_free( key );
573         key = NULL;
574         value = NULL;
575         return TRUE;
576 }
577
578 /*
579  * Free hash table of address cache items.
580  */
581 static void addrindex_free_cache_hash( GHashTable *table ) {
582         g_hash_table_freeze( table );
583         g_hash_table_foreach_remove( table, addrindex_free_cache_cb, NULL );
584         g_hash_table_thaw( table );
585         g_hash_table_destroy( table );
586 }
587
588 /**
589  * Remove data source from internal hashtable.
590  * \param addrIndex Address index.
591  * \param ds        Data source to remove.
592  */
593 static void addrindex_hash_remove_cache(
594                 AddressIndex *addrIndex, AddressDataSource *ds )
595 {
596         gchar *cacheID;
597
598         cacheID = addrindex_get_cache_id( addrIndex, ds );
599         if( cacheID ) {
600                 g_hash_table_remove( addrIndex->hashCache, cacheID );
601                 g_free( cacheID );
602                 cacheID = NULL;
603         }
604 }
605
606 /**
607  * Create a new address index. This is created as a singleton object.
608  * \return Initialized address index object.
609  */
610 AddressIndex *addrindex_create_index( void ) {
611         AddressIndex *index;
612
613         if( _addressIndex_ == NULL ) {
614                 index = g_new0( AddressIndex, 1 );
615                 ADDRITEM_TYPE(index) = ITEMTYPE_INDEX;
616                 ADDRITEM_ID(index) = NULL;
617                 ADDRITEM_NAME(index) = g_strdup( "Address Index" );
618                 ADDRITEM_PARENT(index) = NULL;
619                 ADDRITEM_SUBTYPE(index) = 0;
620                 index->filePath = NULL;
621                 index->fileName = NULL;
622                 index->retVal = MGU_SUCCESS;
623                 index->needsConversion = FALSE;
624                 index->wasConverted = FALSE;
625                 index->conversionError = FALSE;
626                 index->interfaceList = NULL;
627                 index->lastType = ADDR_IF_NONE;
628                 index->dirtyFlag = FALSE;
629                 index->hashCache = g_hash_table_new( g_str_hash, g_str_equal );
630                 index->loadedFlag = FALSE;
631                 index->searchOrder = NULL;
632                 addrindex_build_if_list( index );
633                 _addressIndex_ = index;
634         }
635         return _addressIndex_;
636 }
637
638 /**
639  * Return reference to address index.
640  * \return Address index object.
641  */
642 AddressIndex *addrindex_get_object( void ) {
643         return _addressIndex_;
644 }
645
646 /**
647  * Property - Specify file path to address index file.
648  * \param addrIndex Address index.
649  * \param value Path to index file.
650  */
651 void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
652         g_return_if_fail( addrIndex != NULL );
653         addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
654 }
655
656 /**
657  * Property - Specify file name to address index file.
658  * \param addrIndex Address index.
659  * \param value File name.
660  */
661 void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
662         g_return_if_fail( addrIndex != NULL );
663         addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
664 }
665
666 /**
667  * Property - Specify file path to be used.
668  * \param addrIndex Address index.
669  * \param value Path to JPilot file.
670  */
671 void addrindex_set_dirty( AddressIndex *addrIndex, const gboolean value ) {
672         g_return_if_fail( addrIndex != NULL );
673         addrIndex->dirtyFlag = value;
674 }
675
676 /**
677  * Property - get loaded flag. Note that this flag is set after reading data
678  * from the address books.
679  * \param addrIndex Address index.
680  * \return <i>TRUE</i> if address index data was loaded.
681  */
682 gboolean addrindex_get_loaded( AddressIndex *addrIndex ) {
683         g_return_val_if_fail( addrIndex != NULL, FALSE );
684         return addrIndex->loadedFlag;
685 }
686
687 /**
688  * Return list of address interfaces.
689  * \param addrIndex Address index.
690  * \return List of address interfaces.
691  */
692 GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
693         g_return_val_if_fail( addrIndex != NULL, NULL );
694         return addrIndex->interfaceList;
695 }
696
697 /**
698  * Perform any other initialization of address index.
699  */
700 void addrindex_initialize( void ) {
701         qrymgr_initialize();
702         addrcompl_initialize();
703 }
704
705 /**
706  * Perform any other teardown of address index.
707  */
708 void addrindex_teardown( void ) {
709         addrcompl_teardown();
710         qrymgr_teardown();
711 }
712
713 /**
714  * Free up address index.
715  * \param addrIndex Address index.
716  */
717 void addrindex_free_index( AddressIndex *addrIndex ) {
718         GList *node;
719
720         g_return_if_fail( addrIndex != NULL );
721
722         /* Search stuff */
723         g_list_free( addrIndex->searchOrder );
724         addrIndex->searchOrder = NULL;
725
726         /* Free internal storage */
727         g_free( ADDRITEM_ID(addrIndex) );
728         g_free( ADDRITEM_NAME(addrIndex) );
729         g_free( addrIndex->filePath );
730         g_free( addrIndex->fileName );
731
732         /* Clear pointers */    
733         ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
734         ADDRITEM_ID(addrIndex) = NULL;
735         ADDRITEM_NAME(addrIndex) = NULL;
736         ADDRITEM_PARENT(addrIndex) = NULL;
737         ADDRITEM_SUBTYPE(addrIndex) = 0;
738         addrIndex->filePath = NULL;
739         addrIndex->fileName = NULL;
740         addrIndex->retVal = MGU_SUCCESS;
741         addrIndex->needsConversion = FALSE;
742         addrIndex->wasConverted = FALSE;
743         addrIndex->conversionError = FALSE;
744         addrIndex->lastType = ADDR_IF_NONE;
745         addrIndex->dirtyFlag = FALSE;
746
747         /* Free up interfaces */        
748         node = addrIndex->interfaceList;
749         while( node ) {
750                 AddressInterface *iface = node->data;
751                 addrindex_free_interface( iface );
752                 node = g_list_next( node );
753         }
754         g_list_free( addrIndex->interfaceList );
755         addrIndex->interfaceList = NULL;
756
757         /* Free up hash cache */
758         addrindex_free_cache_hash( addrIndex->hashCache );
759         addrIndex->hashCache = NULL;
760
761         addrIndex->loadedFlag = FALSE;
762
763         g_free( addrIndex );
764         addrIndex = NULL;
765         _addressIndex_ = NULL;
766 }
767
768 /**
769  * Print address index.
770  * \param addrIndex Address index.
771  * \parem stream    Stream to print.
772 */
773 void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
774         g_return_if_fail( addrIndex != NULL );
775         fprintf( stream, "AddressIndex:\n" );
776         fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
777         fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
778         fprintf( stream, "\t   status: %d\n", addrIndex->retVal );
779         fprintf( stream, "\tconverted: '%s'\n",
780                         addrIndex->wasConverted ? "yes" : "no" );
781         fprintf( stream, "\tcvt error: '%s'\n",
782                         addrIndex->conversionError ? "yes" : "no" );
783         fprintf( stream, "\t---\n" );
784 }
785
786 /**
787  * Retrieve reference to address interface for specified interface type.
788  * \param  addrIndex Address index.
789  * \param  ifType Interface type.
790  * \return Address interface, or NULL if not found.
791  */
792 AddressInterface *addrindex_get_interface(
793         AddressIndex *addrIndex, AddressIfType ifType )
794 {
795         AddressInterface *retVal = NULL;
796         GList *node;
797
798         g_return_val_if_fail( addrIndex != NULL, NULL );
799
800         node = addrIndex->interfaceList;
801         while( node ) {
802                 AddressInterface *iface = node->data;
803                 node = g_list_next( node );
804                 if( iface->type == ifType ) {
805                         retVal = iface;
806                         break;
807                 }
808         }
809         return retVal;
810 }
811
812 /**
813  * Add raw data source to index. The raw data object (an AddressBookFile or
814  * VCardFile object, for example) should be supplied as the raw dataSource
815  * argument.
816  *
817  * \param  addrIndex Address index.
818  * \param ifType     Interface type to add.
819  * \param dataSource Actual raw data source to add. 
820  * \return Data source added, or NULL if invalid interface type.
821  */
822 AddressDataSource *addrindex_index_add_datasource(
823         AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource )
824 {
825         AddressInterface *iface;
826         AddressDataSource *ds = NULL;
827
828         g_return_val_if_fail( addrIndex != NULL, NULL );
829         g_return_val_if_fail( dataSource != NULL, NULL );
830
831         iface = addrindex_get_interface( addrIndex, ifType );
832         if( iface ) {
833                 ds = addrindex_create_datasource( ifType );
834                 ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
835                 ds->type = ifType;
836                 ds->rawDataSource = dataSource;
837                 ds->interface = iface;
838                 iface->listSource = g_list_append( iface->listSource, ds );
839                 addrIndex->dirtyFlag = TRUE;
840
841                 addrindex_hash_add_cache( addrIndex, ds );
842         }
843         return ds;
844 }
845
846 /**
847  * Remove specified data source from index.
848  * \param  addrIndex Address index.
849  * \param  dataSource Data source to add. 
850  * \return Reference to data source if removed, or NULL if data source was not
851  *         found in index. Note the this object must still be freed.
852  */
853 AddressDataSource *addrindex_index_remove_datasource(
854         AddressIndex *addrIndex, AddressDataSource *dataSource )
855 {
856         AddressDataSource *retVal = FALSE;
857         AddressInterface *iface;
858
859         g_return_val_if_fail( addrIndex != NULL, NULL );
860         g_return_val_if_fail( dataSource != NULL, NULL );
861
862         iface = addrindex_get_interface( addrIndex, dataSource->type );
863         if( iface ) {
864                 iface->listSource = g_list_remove( iface->listSource, dataSource );
865                 addrIndex->dirtyFlag = TRUE;
866                 dataSource->interface = NULL;
867
868                 /* Remove cache from hash table */
869                 addrindex_hash_remove_cache( addrIndex, dataSource );
870
871                 retVal = dataSource;
872         }
873         return retVal;
874 }
875
876 /**
877  * Retrieve a reference to address interface for specified interface type and
878  * XML interface tag name.
879  * \param  addrIndex Address index.
880  * \param  tag       XML interface tag name to match.
881  * \param  ifType    Interface type to match.
882  * \return Reference to address index, or NULL if not found in index.
883  */
884 static AddressInterface *addrindex_tag_get_interface(
885         AddressIndex *addrIndex, gchar *tag, AddressIfType ifType )
886 {
887         AddressInterface *retVal = NULL;
888         GList *node = addrIndex->interfaceList;
889
890         while( node ) {
891                 AddressInterface *iface = node->data;
892                 node = g_list_next( node );
893                 if( tag ) {
894                         if( strcmp( iface->listTag, tag ) == 0 ) {
895                                 retVal = iface;
896                                 break;
897                         }
898                 }
899                 else {
900                         if( iface->type == ifType ) {
901                                 retVal = iface;
902                                 break;
903                         }
904                 }
905         }
906         return retVal;
907 }
908
909 /**
910  * Retrieve a reference to address interface for specified interface type and
911  * XML datasource tag name.
912  * \param  addrIndex Address index.
913  * \param  ifType    Interface type to match.
914  * \param  tag       XML datasource tag name to match.
915  * \return Reference to address index, or NULL if not found in index.
916  */
917 static AddressInterface *addrindex_tag_get_datasource(
918         AddressIndex *addrIndex, AddressIfType ifType, gchar *tag )
919 {
920         AddressInterface *retVal = NULL;
921         GList *node = addrIndex->interfaceList;
922
923         while( node ) {
924                 AddressInterface *iface = node->data;
925                 node = g_list_next( node );
926                 if( iface->type == ifType && iface->itemTag ) {
927                         if( strcmp( iface->itemTag, tag ) == 0 ) {
928                                 retVal = iface;
929                                 break;
930                         }
931                 }
932         }
933         return retVal;
934 }
935
936 /* **********************************************************************
937 * Interface XML parsing functions.
938 * ***********************************************************************
939 */
940
941 /**
942  * Write start of XML element to file.
943  * \param fp   File.
944  * \param lvl  Indentation level.
945  * \param name Element name.
946  */
947 static void addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name ) {
948         gint i;
949         for( i = 0; i < lvl; i++ ) fputs( "  ", fp );
950         fputs( "<", fp );
951         fputs( name, fp );
952 }
953
954 /**
955  * Write end of XML element to file.
956  * \param fp   File.
957  * \param lvl  Indentation level.
958  * \param name Element name.
959  */
960 static void addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name ) {
961         gint i;
962         for( i = 0; i < lvl; i++ ) fputs( "  ", fp );
963         fputs( "</", fp );
964         fputs( name, fp );
965         fputs( ">\n", fp );
966 }
967
968 /**
969  * Write XML attribute to file.
970  * \param fp    File.
971  * \param name  Attribute name.
972  * \param value Attribute value.
973  */
974 static void addrindex_write_attr( FILE *fp, const gchar *name, const gchar *value ) {
975         fputs( " ", fp );
976         fputs( name, fp );
977         fputs( "=\"", fp );
978         xml_file_put_escape_str( fp, value );
979         fputs( "\"", fp );
980 }
981
982 /**
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.
986  */
987 static AddressIfFragment *addrindex_read_fragment( XMLFile *file ) {
988         AddressIfFragment *fragment;
989         AddressIfFragment *child;
990         AddressIfAttrib *nv;
991         XMLTag *xtag;
992         GList *list;
993         GList *attr;
994         gchar *name;
995         gchar *value;
996         guint prevLevel;
997         gint rc;
998
999         /* printf( "addrindex_read_fragment\n" ); */
1000
1001         prevLevel = file->level;
1002
1003         /* Get current tag name */
1004         xtag = xml_get_current_tag( file );
1005
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;
1011
1012         /* Read attributes */
1013         list = NULL;
1014         attr = xml_get_current_tag_attr( file );
1015         while( attr ) {
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 );
1023         }
1024         fragment->attributes = list;
1025
1026         /* Now read the children */
1027         while( TRUE ) {
1028                 rc = xml_parse_next_tag( file );
1029                 if( rc != 0 ) {
1030                         /* End of file? */
1031                         break;
1032                 }
1033                 if( file->level < prevLevel ) {
1034                         /* We must be above level we start at */
1035                         break;
1036                 }
1037                 child = addrindex_read_fragment( file );
1038                 fragment->children = g_list_append( fragment->children, child );
1039         }
1040
1041         return fragment;
1042 }
1043
1044 /**
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.
1049  */
1050 static void addrindex_write_fragment(
1051                 FILE *fp, const AddressIfFragment *fragment, const gint lvl )
1052 {
1053         GList *node;
1054
1055         if( fragment ) {
1056                 addrindex_write_elem_s( fp, lvl, fragment->name );
1057                 node = fragment->attributes;
1058                 while( node ) {
1059                         AddressIfAttrib *nv = node->data;
1060                         addrindex_write_attr( fp, nv->name, nv->value );
1061                         node = g_list_next( node );
1062                 }
1063                 if( fragment->children ) {
1064                         fputs(" >\n", fp);
1065
1066                         /* Output children */
1067                         node = fragment->children;
1068                         while( node ) {
1069                                 AddressIfFragment *child = node->data;
1070                                 addrindex_write_fragment( fp, child, 1+lvl );
1071                                 node = g_list_next( node );
1072                         }
1073
1074                         /* Output closing tag */
1075                         addrindex_write_elem_e( fp, lvl, fragment->name );
1076                 }
1077                 else {
1078                         fputs(" />\n", fp);
1079                 }
1080         }
1081 }
1082
1083 #if 0
1084 static void addrindex_print_fragment_r(
1085                 const AddressIfFragment *fragment, FILE *stream, gint lvl )
1086 {
1087         GList *node;
1088         gint i;
1089
1090         for( i = 0; i < lvl; i++ )
1091                 fprintf( stream, "  " );
1092         fprintf( stream, "Element:%s:\n", fragment->name );
1093         node = fragment->attributes;
1094         while( node ) {
1095                 AddressIfAttrib *nv = node->data;
1096                 for( i = 0; i < lvl; i++ )
1097                         fprintf( stream, "  " );
1098                 fprintf( stream, "    %s : %s\n", nv->name, nv->value );
1099                 node = g_list_next( node );
1100         }
1101         node = fragment->children;
1102         while( node ) {
1103                 AddressIfFragment *child = node->data;
1104                 addrindex_print_fragment_r( child, stream, 1+lvl );
1105                 node = g_list_next( node );
1106         }
1107 }
1108
1109 static void addrindex_print_fragment( const AddressIfFragment *fragment, FILE *stream ) {
1110         addrindex_print_fragment_r( fragment, stream, 0 );
1111 }
1112 #endif
1113
1114 /**
1115  * Read/parse address index file, creating a data source for a regular
1116  * intrinsic XML addressbook.
1117  * \param  file Address index file.
1118  * \return Data source.
1119  */
1120 static AddressDataSource *addrindex_parse_book( XMLFile *file ) {
1121         AddressDataSource *ds;
1122         AddressBookFile *abf;
1123         GList *attr;
1124
1125         ds = addrindex_create_datasource( ADDR_IF_BOOK );
1126         abf = addrbook_create_book();
1127         attr = xml_get_current_tag_attr( file );
1128         while( attr ) {
1129                 gchar *name = ((XMLAttr *)attr->data)->name;
1130                 gchar *value = ((XMLAttr *)attr->data)->value;
1131                 if( strcmp( name, ATTAG_BOOK_NAME ) == 0 ) {
1132                         addrbook_set_name( abf, value );
1133                 }
1134                 else if( strcmp( name, ATTAG_BOOK_FILE ) == 0) {
1135                         addrbook_set_file( abf, value );
1136                 }
1137                 attr = g_list_next( attr );
1138         }
1139         ds->rawDataSource = abf;
1140         return ds;
1141 }
1142
1143 static void addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
1144         AddressBookFile *abf = ds->rawDataSource;
1145         if( abf ) {
1146                 addrindex_write_elem_s( fp, lvl, TAG_DS_ADDRESS_BOOK );
1147                 addrindex_write_attr( fp, ATTAG_BOOK_NAME, addrbook_get_name( abf ) );
1148                 addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName );
1149                 fputs( " />\n", fp );
1150         }
1151 }
1152
1153 static AddressDataSource *addrindex_parse_vcard( XMLFile *file ) {
1154         AddressDataSource *ds;
1155         VCardFile *vcf;
1156         GList *attr;
1157
1158         ds = addrindex_create_datasource( ADDR_IF_VCARD );
1159         vcf = vcard_create();
1160         attr = xml_get_current_tag_attr( file );
1161         while( attr ) {
1162                 gchar *name = ((XMLAttr *)attr->data)->name;
1163                 gchar *value = ((XMLAttr *)attr->data)->value;
1164                 if( strcmp( name, ATTAG_VCARD_NAME ) == 0 ) {
1165                         vcard_set_name( vcf, value );
1166                 }
1167                 else if( strcmp( name, ATTAG_VCARD_FILE ) == 0) {
1168                         vcard_set_file( vcf, value );
1169                 }
1170                 attr = g_list_next( attr );
1171         }
1172         ds->rawDataSource = vcf;
1173         return ds;
1174 }
1175
1176 static void addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
1177         VCardFile *vcf = ds->rawDataSource;
1178         if( vcf ) {
1179                 addrindex_write_elem_s( fp, lvl, TAG_DS_VCARD );
1180                 addrindex_write_attr( fp, ATTAG_VCARD_NAME, vcard_get_name( vcf ) );
1181                 addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path );
1182                 fputs( " />\n", fp );
1183         }
1184 }
1185
1186 #ifdef USE_JPILOT
1187 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
1188         AddressDataSource *ds;
1189         JPilotFile *jpf;
1190         GList *attr;
1191
1192         ds = addrindex_create_datasource( ADDR_IF_JPILOT );
1193         jpf = jpilot_create();
1194         attr = xml_get_current_tag_attr( file );
1195         while( attr ) {
1196                 gchar *name = ((XMLAttr *)attr->data)->name;
1197                 gchar *value = ((XMLAttr *)attr->data)->value;
1198                 if( strcmp( name, ATTAG_JPILOT_NAME ) == 0 ) {
1199                         jpilot_set_name( jpf, value );
1200                 }
1201                 else if( strcmp( name, ATTAG_JPILOT_FILE ) == 0 ) {
1202                         jpilot_set_file( jpf, value );
1203                 }
1204                 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_1 ) == 0 ) {
1205                         jpilot_add_custom_label( jpf, value );
1206                 }
1207                 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_2 ) == 0 ) {
1208                         jpilot_add_custom_label( jpf, value );
1209                 }
1210                 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_3 ) == 0 ) {
1211                         jpilot_add_custom_label( jpf, value );
1212                 }
1213                 else if( strcmp( name, ATTAG_JPILOT_CUSTOM_4 ) == 0 ) {
1214                         jpilot_add_custom_label( jpf, value );
1215                 }
1216                 attr = g_list_next( attr );
1217         }
1218         ds->rawDataSource = jpf;
1219         return ds;
1220 }
1221
1222 static void addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
1223         JPilotFile *jpf = ds->rawDataSource;
1224         if( jpf ) {
1225                 gint ind;
1226                 GList *node;
1227                 GList *customLbl = jpilot_get_custom_labels( jpf );
1228                 addrindex_write_elem_s( fp, lvl, TAG_DS_JPILOT );
1229                 addrindex_write_attr( fp, ATTAG_JPILOT_NAME, jpilot_get_name( jpf ) );
1230                 addrindex_write_attr( fp, ATTAG_JPILOT_FILE, jpf->path );
1231                 node = customLbl;
1232                 ind = 1;
1233                 while( node ) {
1234                         gchar name[256];
1235                         g_snprintf( name, sizeof(name), "%s%d",
1236                                     ATTAG_JPILOT_CUSTOM, ind );
1237                         addrindex_write_attr( fp, name, node->data );
1238                         ind++;
1239                         node = g_list_next( node );
1240                 }
1241                 fputs( " />\n", fp );
1242         }
1243 }
1244
1245 #else
1246 /*
1247  * Just read/write DOM fragments (preserve data found in file).
1248  */
1249 static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
1250         AddressDataSource *ds;
1251
1252         ds = addrindex_create_datasource( ADDR_IF_JPILOT );
1253         ds->rawDataSource = addrindex_read_fragment( file );
1254         return ds;
1255 }
1256
1257 static void addrindex_write_jpilot( FILE *fp, AddressDataSource *ds, gint lvl ) {
1258         AddressIfFragment *fragment = ds->rawDataSource;
1259         if( fragment ) {
1260                 addrindex_write_fragment( fp, fragment, lvl );
1261         }
1262 }
1263 #endif
1264
1265 #ifdef USE_LDAP
1266 /**
1267  * Parse LDAP criteria attribute data from XML file.
1268  * \param file Index file.
1269  * \param ctl  LDAP control object to populate.
1270  */
1271 static void addrindex_parse_ldap_attrlist( XMLFile *file, LdapControl *ctl ) {
1272         guint prevLevel;
1273         XMLTag *xtag;
1274         XMLTag *xtagPrev;
1275         gint rc;
1276         GList *attr;
1277         GList *list;
1278         GList *node;
1279
1280         if( file == NULL ) {
1281                 return;
1282         }
1283
1284         list = NULL;
1285         prevLevel = file->level;
1286         xtagPrev = xml_get_current_tag( file );
1287         while( TRUE ) {
1288                 rc = xml_parse_next_tag( file );
1289                 if( rc != 0 ) {
1290                         /* Terminate prematurely */
1291                         mgu_free_dlist( list );
1292                         list = NULL;
1293                         return;
1294                 }
1295                 if( file->level < prevLevel ) {
1296                         /* We must be above level we start at */
1297                         break;
1298                 }
1299
1300                 /* Get a tag (element) */
1301                 xtag = xml_get_current_tag( file );
1302                 if( strcmp( xtag->tag, ELTAG_LDAP_ATTR_SRCH ) == 0 ) {
1303                         /* LDAP criteria attribute */
1304                         attr = xml_get_current_tag_attr( file );
1305                         while( attr ) {
1306                                 gchar *name = ((XMLAttr *)attr->data)->name;
1307                                 gchar *value = ((XMLAttr *)attr->data)->value;
1308                                 if( strcmp( name, ATTAG_LDAP_ATTR_NAME ) == 0 ) {
1309                                         if( value && strlen( value ) > 0 ) {
1310                                                 list = g_list_append(
1311                                                         list, g_strdup( value ) );
1312                                         }
1313                                 }
1314                                 attr = g_list_next( attr );
1315                         }
1316                 }
1317                 else {
1318                         if( xtag != xtagPrev ) {
1319                                 /* Found a new tag */
1320                                 break;
1321                         }
1322                 }
1323                 xtag = xtagPrev;
1324         }
1325
1326         /* Build list of search attributes */
1327         ldapctl_criteria_list_clear( ctl );
1328         node = list;
1329         while( node ) {
1330                 ldapctl_criteria_list_add( ctl, node->data );
1331                 g_free( node->data );
1332                 node->data = NULL;
1333                 node = g_list_next( node );
1334         }
1335         g_list_free( list );
1336         list = NULL;
1337
1338 }
1339
1340 /**
1341  * Parse LDAP control data from XML file.
1342  * \param  file Index file.
1343  * \return Initialized data soruce object.
1344  */
1345 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
1346         AddressDataSource *ds;
1347         LdapServer *server;
1348         LdapControl *ctl;
1349         GList *attr;
1350         gchar *serverName = NULL;
1351         gchar *criteria = NULL;
1352         gboolean bDynSearch;
1353         gboolean bTLS;
1354         gint iMatch;
1355
1356         /* printf( "addrindex_parse_ldap\n" ); */
1357         /* Set up some defaults */
1358         bDynSearch = FALSE;
1359         bTLS = FALSE;
1360         iMatch = LDAPCTL_MATCH_BEGINWITH;
1361
1362         ds = addrindex_create_datasource( ADDR_IF_LDAP );
1363         ctl = ldapctl_create();
1364         attr = xml_get_current_tag_attr( file );
1365         while( attr ) {
1366                 gchar *name = ((XMLAttr *)attr->data)->name;
1367                 gchar *value = ((XMLAttr *)attr->data)->value;
1368                 gint ivalue = atoi( value );
1369
1370                 if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
1371                         if( serverName ) g_free( serverName );
1372                         serverName = g_strdup( value );
1373                 }
1374                 else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
1375                         ldapctl_set_host( ctl, value );
1376                 }
1377                 else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
1378                         ldapctl_set_port( ctl, ivalue );
1379                 }
1380                 else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
1381                         ldapctl_set_base_dn( ctl, value );
1382                 }
1383                 else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
1384                         ldapctl_set_bind_dn( ctl, value );
1385                 }
1386                 else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
1387                         ldapctl_set_bind_password( ctl, value );
1388                 }
1389                 else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
1390                         if( criteria ) g_free( criteria );
1391                         criteria = g_strdup( value );
1392                 }
1393                 else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
1394                         ldapctl_set_max_entries( ctl, ivalue );
1395                 }
1396                 else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
1397                         ldapctl_set_timeout( ctl, ivalue );
1398                 }
1399                 else if( strcmp( name, ATTAG_LDAP_MAX_AGE ) == 0 ) {
1400                         ldapctl_set_max_query_age( ctl, ivalue );
1401                 }
1402                 else if( strcmp( name, ATTAG_LDAP_DYN_SEARCH ) == 0 ) {
1403                         bDynSearch = FALSE;
1404                         if( strcmp( value, ATVAL_BOOLEAN_YES ) == 0 ) {
1405                                 bDynSearch = TRUE;
1406                         }
1407                 }
1408                 else if( strcmp( name, ATTAG_LDAP_MATCH_OPT ) == 0 ) {
1409                         iMatch = LDAPCTL_MATCH_BEGINWITH;
1410                         if( strcmp( value, ATVAL_LDAP_MATCH_CONTAINS ) == 0 ) {
1411                                 iMatch = LDAPCTL_MATCH_CONTAINS;
1412                         }
1413                 }
1414                 else if( strcmp( name, ATTAG_LDAP_ENABLE_TLS ) == 0 ) {
1415                         bTLS = FALSE;
1416                         if( strcmp( value, ATVAL_BOOLEAN_YES ) == 0 ) {
1417                                 bTLS = TRUE;
1418                         }
1419                 }
1420                 attr = g_list_next( attr );
1421         }
1422
1423         server = ldapsvr_create_noctl();
1424         ldapsvr_set_name( server, serverName );
1425         ldapsvr_set_search_flag( server, bDynSearch );
1426         ldapctl_set_matching_option( ctl, iMatch );
1427         ldapctl_set_tls( ctl, bTLS );
1428         g_free( serverName );
1429         ldapsvr_set_control( server, ctl );
1430         ds->rawDataSource = server;
1431
1432         addrindex_parse_ldap_attrlist( file, ctl );
1433         /*
1434          * If criteria have been specified and no attributes were listed, then
1435          * convert old style criteria into an attribute list. Any criteria will
1436          * be dropped when saving data.
1437          */
1438         if( criteria ) {
1439                 if( ! ldapctl_get_criteria_list( ctl ) ) {
1440                         ldapctl_parse_ldap_search( ctl, criteria );
1441                 }
1442                 g_free( criteria );
1443         }
1444         /* ldapsvr_print_data( server, stdout ); */
1445
1446         return ds;
1447 }
1448
1449 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1450         LdapServer *server = ds->rawDataSource;
1451         LdapControl *ctl = NULL;
1452         GList *node;
1453         gchar value[256];
1454
1455         if( server ) {
1456                 ctl = server->control;
1457         }
1458         if( ctl == NULL ) return;
1459
1460         /* Output start element with attributes */
1461         addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP );
1462         addrindex_write_attr( fp, ATTAG_LDAP_NAME, ldapsvr_get_name( server ) );
1463         addrindex_write_attr( fp, ATTAG_LDAP_HOST, ctl->hostName );
1464
1465         sprintf( value, "%d", ctl->port );      
1466         addrindex_write_attr( fp, ATTAG_LDAP_PORT, value );
1467
1468         addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, ctl->baseDN );
1469         addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, ctl->bindDN );
1470         addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, ctl->bindPass );
1471
1472         sprintf( value, "%d", ctl->maxEntries );
1473         addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value );
1474         sprintf( value, "%d", ctl->timeOut );
1475         addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value );
1476         sprintf( value, "%d", ctl->maxQueryAge );
1477         addrindex_write_attr( fp, ATTAG_LDAP_MAX_AGE, value );
1478
1479         addrindex_write_attr( fp, ATTAG_LDAP_DYN_SEARCH,
1480                         server->searchFlag ?
1481                         ATVAL_BOOLEAN_YES : ATVAL_BOOLEAN_NO );
1482
1483         addrindex_write_attr( fp, ATTAG_LDAP_MATCH_OPT,
1484                 ( ctl->matchingOption == LDAPCTL_MATCH_CONTAINS ) ?
1485                 ATVAL_LDAP_MATCH_CONTAINS : ATVAL_LDAP_MATCH_BEGIN );
1486
1487         addrindex_write_attr( fp, ATTAG_LDAP_ENABLE_TLS,
1488                         ctl->enableTLS ?
1489                         ATVAL_BOOLEAN_YES : ATVAL_BOOLEAN_NO );
1490
1491         fputs(" >\n", fp);
1492
1493         /* Output attributes */
1494         node = ldapctl_get_criteria_list( ctl );
1495         while( node ) {
1496                 addrindex_write_elem_s( fp, 1+lvl, ELTAG_LDAP_ATTR_SRCH );
1497                 addrindex_write_attr( fp, ATTAG_LDAP_ATTR_NAME, node->data );
1498                 fputs(" />\n", fp);
1499                 node = g_list_next( node );
1500         }
1501
1502         /* End of element */    
1503         addrindex_write_elem_e( fp, lvl, TAG_DS_LDAP );
1504 }
1505
1506 #else
1507 /*
1508  * Just read/write DOM fragments (preserve data found in file).
1509  */
1510 static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
1511         AddressDataSource *ds;
1512
1513         ds = addrindex_create_datasource( ADDR_IF_LDAP );
1514         ds->rawDataSource = addrindex_read_fragment( file );
1515         return ds;
1516 }
1517
1518 static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
1519         AddressIfFragment *fragment = ds->rawDataSource;
1520         if( fragment ) {
1521                 addrindex_write_fragment( fp, fragment, lvl );
1522         }
1523 }
1524 #endif
1525
1526 /* **********************************************************************
1527 * Address index I/O functions.
1528 * ***********************************************************************
1529 */
1530 /**
1531  * Read address index file, creating appropriate data sources for each address
1532  * index file entry.
1533  *
1534  * \param  addrIndex Address index.
1535  * \param  file Address index file.
1536  */
1537 static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
1538         guint prev_level;
1539         XMLTag *xtag;
1540         AddressInterface *iface = NULL, *dsIFace = NULL;
1541         AddressDataSource *ds;
1542         gint rc;
1543
1544         addrIndex->loadedFlag = FALSE;
1545         for (;;) {
1546                 prev_level = file->level;
1547                 rc = xml_parse_next_tag( file );
1548                 if( file->level == 0 ) return;
1549
1550                 xtag = xml_get_current_tag( file );
1551
1552                 iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
1553                 if( iface ) {
1554                         addrIndex->lastType = iface->type;
1555                         if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
1556                 }
1557                 else {
1558                         dsIFace = addrindex_tag_get_datasource(
1559                                         addrIndex, addrIndex->lastType, xtag->tag );
1560                         if( dsIFace ) {
1561                                 /* Add data source to list */
1562                                 ds = NULL;
1563                                 if( addrIndex->lastType == ADDR_IF_BOOK ) {
1564                                         ds = addrindex_parse_book( file );
1565                                         if( ds->rawDataSource ) {
1566                                                 addrbook_set_path( ds->rawDataSource,
1567                                                         addrIndex->filePath );
1568                                         }
1569                                 }
1570                                 else if( addrIndex->lastType == ADDR_IF_VCARD ) {
1571                                         ds = addrindex_parse_vcard( file );
1572                                 }
1573                                 else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
1574                                         ds = addrindex_parse_jpilot( file );
1575                                 }
1576                                 else if( addrIndex->lastType == ADDR_IF_LDAP ) {
1577                                         ds = addrindex_parse_ldap( file );
1578                                 }
1579                                 if( ds ) {
1580                                         ds->interface = dsIFace;
1581                                         addrindex_hash_add_cache( addrIndex, ds );
1582                                         dsIFace->listSource =
1583                                                 g_list_append( dsIFace->listSource, ds );
1584                                 }
1585                         }
1586                 }
1587         }
1588 }
1589
1590 /*
1591  * Search order sorting comparison function for building search order list.
1592  */
1593 static gint addrindex_search_order_compare( gconstpointer ptrA, gconstpointer ptrB ) {
1594         AddressInterface *ifaceA = ( AddressInterface * ) ptrA;
1595         AddressInterface *ifaceB = ( AddressInterface * ) ptrB;
1596
1597         return ifaceA->searchOrder - ifaceB->searchOrder;
1598 }
1599
1600 /**
1601  * Build list of data sources to process.
1602  * \param addrIndex Address index object.
1603  */
1604 static void addrindex_build_search_order( AddressIndex *addrIndex ) {
1605         GList *nodeIf;
1606
1607         /* Clear existing list */
1608         g_list_free( addrIndex->searchOrder );
1609         addrIndex->searchOrder = NULL;
1610
1611         /* Build new list */
1612         nodeIf = addrIndex->interfaceList;
1613         while( nodeIf ) {
1614                 AddressInterface *iface = nodeIf->data;
1615                 if( iface->useInterface ) {
1616                         if( iface->searchOrder > 0 ) {
1617                                 /* Add to search order list */
1618                                 addrIndex->searchOrder = g_list_insert_sorted(
1619                                         addrIndex->searchOrder, iface,
1620                                         addrindex_search_order_compare );
1621                         }
1622                 }
1623                 nodeIf = g_list_next( nodeIf );
1624         }
1625 }
1626
1627 static gint addrindex_read_file( AddressIndex *addrIndex ) {
1628         XMLFile *file = NULL;
1629         gchar *fileSpec = NULL;
1630
1631         g_return_val_if_fail( addrIndex != NULL, -1 );
1632
1633         fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1634         addrIndex->retVal = MGU_NO_FILE;
1635         file = xml_open_file( fileSpec );
1636         g_free( fileSpec );
1637
1638         if( file == NULL ) {
1639                 /*
1640                 fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName );
1641                 */
1642                 return addrIndex->retVal;
1643         }
1644
1645         addrIndex->retVal = MGU_BAD_FORMAT;
1646         if( xml_get_dtd( file ) == 0 ) {
1647                 if( xml_parse_next_tag( file ) == 0 ) {
1648                         if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1649                                 addrindex_read_index( addrIndex, file );
1650                                 addrIndex->retVal = MGU_SUCCESS;
1651                         }
1652                 }
1653         }
1654         xml_close_file( file );
1655
1656         addrindex_build_search_order( addrIndex );
1657
1658         return addrIndex->retVal;
1659 }
1660
1661 static void addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1662         GList *nodeIF, *nodeDS;
1663         gint lvlList = 1;
1664         gint lvlItem = 1 + lvlList;
1665
1666         nodeIF = addrIndex->interfaceList;
1667         while( nodeIF ) {
1668                 AddressInterface *iface = nodeIF->data;
1669                 if( ! iface->legacyFlag ) {
1670                         nodeDS = iface->listSource;
1671                         addrindex_write_elem_s( fp, lvlList, iface->listTag );
1672                         fputs( ">\n", fp );
1673                         while( nodeDS ) {
1674                                 AddressDataSource *ds = nodeDS->data;
1675                                 if( ds ) {
1676                                         if( iface->type == ADDR_IF_BOOK ) {
1677                                                 addrindex_write_book( fp, ds, lvlItem );
1678                                         }
1679                                         if( iface->type == ADDR_IF_VCARD ) {
1680                                                 addrindex_write_vcard( fp, ds, lvlItem );
1681                                         }
1682                                         if( iface->type == ADDR_IF_JPILOT ) {
1683                                                 addrindex_write_jpilot( fp, ds, lvlItem );
1684                                         }
1685                                         if( iface->type == ADDR_IF_LDAP ) {
1686                                                 addrindex_write_ldap( fp, ds, lvlItem );
1687                                         }
1688                                 }
1689                                 nodeDS = g_list_next( nodeDS );
1690                         }
1691                         addrindex_write_elem_e( fp, lvlList, iface->listTag );
1692                 }
1693                 nodeIF = g_list_next( nodeIF );
1694         }
1695 }
1696
1697 /*
1698 * Write data to specified file.
1699 * Enter: addrIndex Address index object.
1700 *        newFile   New file name.
1701 * return: Status code, from addrIndex->retVal.
1702 * Note: File will be created in directory specified by addrIndex.
1703 */
1704 gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1705         FILE *fp;
1706         gchar *fileSpec;
1707 #ifndef DEV_STANDALONE
1708         PrefFile *pfile;
1709 #endif
1710
1711         g_return_val_if_fail( addrIndex != NULL, -1 );
1712
1713         fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1714         addrIndex->retVal = MGU_OPEN_FILE;
1715 #ifdef DEV_STANDALONE
1716         fp = fopen( fileSpec, "wb" );
1717         g_free( fileSpec );
1718         if( fp ) {
1719                 fputs( "<?xml version=\"1.0\" ?>\n", fp );
1720 #else
1721         pfile = prefs_write_open( fileSpec );
1722         g_free( fileSpec );
1723         if( pfile ) {
1724                 fp = pfile->fp;
1725                 fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n", CS_INTERNAL );
1726 #endif
1727                 addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
1728                 fputs( ">\n", fp );
1729
1730                 addrindex_write_index( addrIndex, fp );
1731                 addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
1732
1733                 addrIndex->retVal = MGU_SUCCESS;
1734 #ifdef DEV_STANDALONE
1735                 fclose( fp );
1736 #else
1737                 if( prefs_file_close( pfile ) < 0 ) {
1738                         addrIndex->retVal = MGU_ERROR_WRITE;
1739                 }
1740 #endif
1741         }
1742
1743         fileSpec = NULL;
1744         return addrIndex->retVal;
1745 }
1746
1747 /*
1748 * Save address index data to original file.
1749 * return: Status code, from addrIndex->retVal.
1750 */
1751 gint addrindex_save_data( AddressIndex *addrIndex ) {
1752         g_return_val_if_fail( addrIndex != NULL, -1 );
1753
1754         addrIndex->retVal = MGU_NO_FILE;
1755         if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1756         if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1757
1758         addrindex_write_to( addrIndex, addrIndex->fileName );
1759         if( addrIndex->retVal == MGU_SUCCESS ) {
1760                 addrIndex->dirtyFlag = FALSE;
1761         }
1762         return addrIndex->retVal;
1763 }
1764
1765 /*
1766 * Save all address book files which may have changed.
1767 * Return: Status code, set if there was a problem saving data.
1768 */
1769 gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1770         gint retVal = MGU_SUCCESS;
1771         GList *nodeIf, *nodeDS;
1772
1773         nodeIf = addrIndex->interfaceList;
1774         while( nodeIf ) {
1775                 AddressInterface *iface = nodeIf->data;
1776                 if( iface->type == ADDR_IF_BOOK ) {
1777                         nodeDS = iface->listSource;
1778                         while( nodeDS ) {
1779                                 AddressDataSource *ds = nodeDS->data;
1780                                 AddressBookFile *abf = ds->rawDataSource;
1781                                 if( addrbook_get_dirty( abf ) ) {
1782                                         if( addrbook_get_read_flag( abf ) ) {
1783                                                 addrbook_save_data( abf );
1784                                                 if( abf->retVal != MGU_SUCCESS ) {
1785                                                         retVal = abf->retVal;
1786                                                 }
1787                                         }
1788                                 }
1789                                 nodeDS = g_list_next( nodeDS );
1790                         }
1791                         break;
1792                 }
1793                 nodeIf = g_list_next( nodeIf );
1794         }
1795         return retVal;
1796 }
1797
1798
1799 /* **********************************************************************
1800 * Address book conversion to new format.
1801 * ***********************************************************************
1802 */
1803
1804 #define ELTAG_IF_OLD_FOLDER   "folder"
1805 #define ELTAG_IF_OLD_GROUP    "group"
1806 #define ELTAG_IF_OLD_ITEM     "item"
1807 #define ELTAG_IF_OLD_NAME     "name"
1808 #define ELTAG_IF_OLD_ADDRESS  "address"
1809 #define ELTAG_IF_OLD_REMARKS  "remarks"
1810 #define ATTAG_IF_OLD_NAME     "name"
1811
1812 #define TEMPNODE_ROOT         0
1813 #define TEMPNODE_FOLDER       1
1814 #define TEMPNODE_GROUP        2
1815 #define TEMPNODE_ADDRESS      3
1816
1817 typedef struct _AddressCvt_Node AddressCvtNode;
1818 struct _AddressCvt_Node {
1819         gint  type;
1820         gchar *name;
1821         gchar *address;
1822         gchar *remarks;
1823         GList *list;
1824 };
1825
1826 /*
1827 * Parse current address item.
1828 */
1829 static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1830         gchar *element;
1831         guint level;
1832         AddressCvtNode *nn;
1833
1834         nn = g_new0( AddressCvtNode, 1 );
1835         nn->type = TEMPNODE_ADDRESS;
1836         nn->list = NULL;
1837
1838         level = file->level;
1839
1840         for (;;) {
1841                 xml_parse_next_tag(file);
1842                 if (file->level < level) return nn;
1843
1844                 element = xml_get_element( file );
1845                 if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1846                         nn->name = g_strdup( element );
1847                 }
1848                 if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1849                         nn->address = g_strdup( element );
1850                 }
1851                 if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1852                         nn->remarks = g_strdup( element );
1853                 }
1854                 xml_parse_next_tag(file);
1855         }
1856 }
1857
1858 /*
1859 * Create a temporary node below specified node.
1860 */
1861 static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1862         AddressCvtNode *nn;
1863         nn = g_new0( AddressCvtNode, 1 );
1864         nn->type = type;
1865         nn->name = g_strdup( name );
1866         nn->remarks = g_strdup( rem );
1867         node->list = g_list_append( node->list, nn );
1868         return nn;
1869 }
1870
1871 /*
1872 * Process current temporary node.
1873 */
1874 static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1875         GList *attr;
1876         guint prev_level;
1877         AddressCvtNode *newNode = NULL;
1878         gchar *name;
1879         gchar *value;
1880
1881         for (;;) {
1882                 prev_level = file->level;
1883                 xml_parse_next_tag( file );
1884                 if (file->level < prev_level) return;
1885                 name = NULL;
1886                 value = NULL;
1887
1888                 if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1889                         attr = xml_get_current_tag_attr(file);
1890                         if (attr) {
1891                                 name = ((XMLAttr *)attr->data)->name;
1892                                 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1893                                         value = ((XMLAttr *)attr->data)->value;
1894                                 }
1895                         }
1896                         newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1897                         addrindex_add_obj( file, newNode );
1898
1899                 }
1900                 else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1901                         attr = xml_get_current_tag_attr(file);
1902                         if (attr) {
1903                                 name = ((XMLAttr *)attr->data)->name;
1904                                 if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1905                                         value = ((XMLAttr *)attr->data)->value;
1906                                 }
1907                         }
1908                         newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1909                         addrindex_add_obj( file, newNode );
1910                 }
1911                 else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
1912                         newNode = addrindex_parse_item( file );
1913                         node->list = g_list_append( node->list, newNode );
1914                 }
1915                 else {
1916                         /* printf( "invalid: !!! \n" ); */
1917                         attr = xml_get_current_tag_attr( file );
1918                 }
1919         }
1920 }
1921
1922 /*
1923 * Consume all nodes below current tag.
1924 */
1925 static void addrindex_consume_tree( XMLFile *file ) {
1926         guint prev_level;
1927         gchar *element;
1928         GList *attr;
1929         XMLTag *xtag;
1930
1931         for (;;) {
1932                 prev_level = file->level;
1933                 xml_parse_next_tag( file );
1934                 if (file->level < prev_level) return;
1935
1936                 xtag = xml_get_current_tag( file );
1937                 /* printf( "tag : %s\n", xtag->tag ); */
1938                 element = xml_get_element( file );
1939                 attr = xml_get_current_tag_attr( file );
1940                 /* show_attribs( attr ); */
1941                 /* printf( "\ttag  value : %s :\n", element ); */
1942                 addrindex_consume_tree( file );
1943         }
1944 }
1945
1946 /*
1947 * Print temporary tree.
1948 */
1949 static void addrindex_print_node( AddressCvtNode *node, FILE *stream  ) {
1950         GList *list;
1951
1952         fprintf( stream, "Node:\ttype :%d:\n", node->type );
1953         fprintf( stream, "\tname :%s:\n", node->name );
1954         fprintf( stream, "\taddr :%s:\n", node->address );
1955         fprintf( stream, "\trems :%s:\n", node->remarks );
1956         if( node->list ) {
1957                 fprintf( stream, "\t--list----\n" );
1958         }
1959         list = node->list;
1960         while( list ) {
1961                 AddressCvtNode *lNode = list->data;
1962                 list = g_list_next( list );
1963                 addrindex_print_node( lNode, stream );
1964         }
1965         fprintf( stream, "\t==list-%d==\n", node->type );
1966 }
1967
1968 /*
1969 * Free up temporary tree.
1970 */
1971 static void addrindex_free_node( AddressCvtNode *node ) {
1972         GList *list = node->list;
1973
1974         while( list ) {
1975                 AddressCvtNode *lNode = list->data;
1976                 list = g_list_next( list );
1977                 addrindex_free_node( lNode );
1978         }
1979         node->type = TEMPNODE_ROOT;
1980         g_free( node->name );
1981         g_free( node->address );
1982         g_free( node->remarks );
1983         g_list_free( node->list );
1984         g_free( node );
1985 }
1986
1987 /*
1988 * Process address book for specified node.
1989 */
1990 static void addrindex_process_node(
1991                 AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
1992                 ItemGroup *parentGrp, ItemFolder *folderGrp )
1993 {
1994         GList *list;
1995         ItemFolder *itemFolder = NULL;
1996         ItemGroup *itemGParent = parentGrp;
1997         ItemFolder *itemGFolder = folderGrp;
1998         AddressCache *cache = abf->addressCache;
1999
2000         if( node->type == TEMPNODE_ROOT ) {
2001                 itemFolder = parent;
2002         }
2003         else if( node->type == TEMPNODE_FOLDER ) {
2004                 itemFolder = addritem_create_item_folder();
2005                 addritem_folder_set_name( itemFolder, node->name );
2006                 addrcache_id_folder( cache, itemFolder );
2007                 addrcache_folder_add_folder( cache, parent, itemFolder );
2008                 itemGFolder = NULL;
2009         }
2010         else if( node->type == TEMPNODE_GROUP ) {
2011                 ItemGroup *itemGroup;
2012                 gchar *fName;
2013
2014                 /* Create a folder for group */
2015                 fName = g_strdup_printf( "Cvt - %s", node->name );
2016                 itemGFolder = addritem_create_item_folder();
2017                 addritem_folder_set_name( itemGFolder, fName );
2018                 addrcache_id_folder( cache, itemGFolder );
2019                 addrcache_folder_add_folder( cache, parent, itemGFolder );
2020                 g_free( fName );
2021
2022                 /* Add group into folder */
2023                 itemGroup = addritem_create_item_group();
2024                 addritem_group_set_name( itemGroup, node->name );
2025                 addrcache_id_group( cache, itemGroup );
2026                 addrcache_folder_add_group( cache, itemGFolder, itemGroup );
2027                 itemGParent = itemGroup;
2028         }
2029         else if( node->type == TEMPNODE_ADDRESS ) {
2030                 ItemPerson *itemPerson;
2031                 ItemEMail *itemEMail;
2032
2033                 /* Create person and email objects */
2034                 itemPerson = addritem_create_item_person();
2035                 addritem_person_set_common_name( itemPerson, node->name );
2036                 addrcache_id_person( cache, itemPerson );
2037                 itemEMail = addritem_create_item_email();
2038                 addritem_email_set_address( itemEMail, node->address );
2039                 addritem_email_set_remarks( itemEMail, node->remarks );
2040                 addrcache_id_email( cache, itemEMail );
2041                 addrcache_person_add_email( cache, itemPerson, itemEMail );
2042
2043                 /* Add person into appropriate folder */
2044                 if( itemGFolder ) {
2045                         addrcache_folder_add_person( cache, itemGFolder, itemPerson );
2046                 }
2047                 else {
2048                         addrcache_folder_add_person( cache, parent, itemPerson );
2049                 }
2050
2051                 /* Add email address only into group */
2052                 if( parentGrp ) {
2053                         addrcache_group_add_email( cache, parentGrp, itemEMail );
2054                 }
2055         }
2056
2057         list = node->list;
2058         while( list ) {
2059                 AddressCvtNode *lNode = list->data;
2060                 list = g_list_next( list );
2061                 addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
2062         }
2063 }
2064
2065 /*
2066 * Process address book to specified file number.
2067 */
2068 static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
2069         gboolean retVal = FALSE;
2070         AddressBookFile *abf = NULL;
2071         AddressCvtNode *rootNode = NULL;
2072         gchar *newFile = NULL;
2073         GList *fileList = NULL;
2074         gint fileNum  = 0;
2075
2076         /* Setup root node */
2077         rootNode = g_new0( AddressCvtNode, 1 );
2078         rootNode->type = TEMPNODE_ROOT;
2079         rootNode->name = g_strdup( "root" );
2080         rootNode->list = NULL;
2081         addrindex_add_obj( file, rootNode );
2082         /* addrindex_print_node( rootNode, stdout ); */
2083
2084         /* Create new address book */
2085         abf = addrbook_create_book();
2086         addrbook_set_name( abf, displayName );
2087         addrbook_set_path( abf, addrIndex->filePath );
2088
2089         /* Determine next available file number */
2090         fileList = addrbook_get_bookfile_list( abf );
2091         if( fileList ) {
2092                 fileNum = 1 + abf->maxValue;
2093         }
2094         g_list_free( fileList );
2095         fileList = NULL;
2096
2097         newFile = addrbook_gen_new_file_name( fileNum );
2098         if( newFile ) {
2099                 addrbook_set_file( abf, newFile );
2100         }
2101
2102         addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
2103
2104         /* addrbook_dump_book( abf, stdout ); */
2105         addrbook_save_data( abf );
2106         addrIndex->retVal = abf->retVal;
2107         if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2108
2109         addrbook_free_book( abf );
2110         abf = NULL;
2111         addrindex_free_node( rootNode );
2112         rootNode = NULL;
2113
2114         /* Create entries in address index */
2115         if( retVal ) {
2116                 abf = addrbook_create_book();
2117                 addrbook_set_name( abf, displayName );
2118                 addrbook_set_path( abf, addrIndex->filePath );
2119                 addrbook_set_file( abf, newFile );
2120                 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2121         }
2122
2123         return retVal;
2124 }
2125
2126 /*
2127 * Process tree converting data.
2128 */
2129 static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
2130         guint prev_level;
2131         gchar *element;
2132         GList *attr;
2133         XMLTag *xtag;
2134
2135         /* Process file */
2136         for (;;) {
2137                 prev_level = file->level;
2138                 xml_parse_next_tag( file );
2139                 if (file->level < prev_level) return;
2140
2141                 xtag = xml_get_current_tag( file );
2142                 /* printf( "tag : %d : %s\n", prev_level, xtag->tag ); */
2143                 if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
2144                         if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
2145                                 addrIndex->needsConversion = FALSE;
2146                                 addrIndex->wasConverted = TRUE;
2147                                 continue;
2148                         }
2149                         return;
2150                 }
2151                 if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
2152                         if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
2153                                 addrIndex->needsConversion = FALSE;
2154                                 addrIndex->wasConverted = TRUE;
2155                                 continue;
2156                         }
2157                         return;
2158                 }
2159                 element = xml_get_element( file );
2160                 attr = xml_get_current_tag_attr( file );
2161                 /* show_attribs( attr ); */
2162                 /* printf( "\ttag  value : %s :\n", element ); */
2163                 addrindex_consume_tree( file );
2164         }
2165 }
2166
2167 static gint addrindex_convert_data( AddressIndex *addrIndex ) {
2168         XMLFile *file = NULL;
2169         gchar *fileSpec;
2170
2171         fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
2172         addrIndex->retVal = MGU_NO_FILE;
2173         file = xml_open_file( fileSpec );
2174         g_free( fileSpec );
2175
2176         if( file == NULL ) {
2177                 /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
2178                 return addrIndex->retVal;
2179         }
2180
2181         addrIndex->retVal = MGU_BAD_FORMAT;
2182         if( xml_get_dtd( file ) == 0 ) {
2183                 if( xml_parse_next_tag( file ) == 0 ) {
2184                         if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
2185                                 addrindex_convert_tree( addrIndex, file );
2186                         }
2187                 }
2188         }
2189         xml_close_file( file );
2190         return addrIndex->retVal;
2191 }
2192
2193 /*
2194 * Create a new address book file.
2195 */
2196 static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
2197         gboolean retVal = FALSE;
2198         AddressBookFile *abf = NULL;
2199         gchar *newFile = NULL;
2200         GList *fileList = NULL;
2201         gint fileNum = 0;
2202
2203         /* Create new address book */
2204         abf = addrbook_create_book();
2205         addrbook_set_name( abf, displayName );
2206         addrbook_set_path( abf, addrIndex->filePath );
2207
2208         /* Determine next available file number */
2209         fileList = addrbook_get_bookfile_list( abf );
2210         if( fileList ) {
2211                 fileNum = 1 + abf->maxValue;
2212         }
2213         g_list_free( fileList );
2214         fileList = NULL;
2215
2216         newFile = addrbook_gen_new_file_name( fileNum );
2217         if( newFile ) {
2218                 addrbook_set_file( abf, newFile );
2219         }
2220
2221         addrbook_save_data( abf );
2222         addrIndex->retVal = abf->retVal;
2223         if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
2224         addrbook_free_book( abf );
2225         abf = NULL;
2226
2227         /* Create entries in address index */
2228         if( retVal ) {
2229                 abf = addrbook_create_book();
2230                 addrbook_set_name( abf, displayName );
2231                 addrbook_set_path( abf, addrIndex->filePath );
2232                 addrbook_set_file( abf, newFile );
2233                 addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
2234         }
2235
2236         return retVal;
2237 }
2238
2239 /*
2240 * Read data for address index performing a conversion if necesary.
2241 * Enter: addrIndex Address index object.
2242 * return: Status code, from addrIndex->retVal.
2243 * Note: New address book files will be created in directory specified by
2244 * addrIndex. Three files will be created, for the following:
2245 *       "Common addresses"
2246 *       "Personal addresses"
2247 *       "Gathered addresses" - a new address book.
2248 */
2249 gint addrindex_read_data( AddressIndex *addrIndex ) {
2250         g_return_val_if_fail( addrIndex != NULL, -1 );
2251
2252         addrIndex->conversionError = FALSE;
2253         addrindex_read_file( addrIndex );
2254         if( addrIndex->retVal == MGU_SUCCESS ) {
2255                 if( addrIndex->needsConversion ) {
2256                         if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
2257                                 addrIndex->conversionError = TRUE;
2258                         }
2259                         else {
2260                                 addrIndex->conversionError = TRUE;
2261                         }
2262                 }
2263                 addrIndex->dirtyFlag = TRUE;
2264         }
2265         return addrIndex->retVal;
2266 }
2267
2268 /*
2269 * Create new address books for a new address index.
2270 * Enter: addrIndex Address index object.
2271 * return: Status code, from addrIndex->retVal.
2272 * Note: New address book files will be created in directory specified by
2273 * addrIndex. Three files will be created, for the following:
2274 *       "Common addresses"
2275 *       "Personal addresses"
2276 *       "Gathered addresses" - a new address book.
2277 */
2278 gint addrindex_create_new_books( AddressIndex *addrIndex ) {
2279         gboolean flg;
2280
2281         g_return_val_if_fail( addrIndex != NULL, -1 );
2282
2283         flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
2284         if( flg ) {
2285                 flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
2286                 addrIndex->dirtyFlag = TRUE;
2287         }
2288         return addrIndex->retVal;
2289 }
2290
2291 /* **********************************************************************
2292 * New interface stuff.
2293 * ***********************************************************************
2294 */
2295
2296 /*
2297  * Return modified flag for specified data source.
2298  */
2299 gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
2300         gboolean retVal = FALSE;
2301         AddressInterface *iface;
2302
2303         if( ds == NULL ) return retVal;
2304         iface = ds->interface;
2305         if( iface == NULL ) return retVal;
2306         if( iface->getModifyFlag ) {
2307                 retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
2308         }
2309         return retVal;
2310 }
2311
2312 /*
2313  * Return accessed flag for specified data source.
2314  */
2315 gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
2316         gboolean retVal = FALSE;
2317         AddressInterface *iface;
2318
2319         if( ds == NULL ) return retVal;
2320         iface = ds->interface;
2321         if( iface == NULL ) return retVal;
2322         if( iface->getAccessFlag ) {
2323                 retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
2324         }
2325         return retVal;
2326 }
2327
2328 /*
2329  * Return data read flag for specified data source.
2330  */
2331 gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
2332         gboolean retVal = TRUE;
2333         AddressInterface *iface;
2334
2335         if( ds == NULL ) return retVal;
2336         iface = ds->interface;
2337         if( iface == NULL ) return retVal;
2338         if( iface->getReadFlag ) {
2339                 retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
2340         }
2341         return retVal;
2342 }
2343
2344 /*
2345  * Return status code for specified data source.
2346  */
2347 gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
2348         gint retVal = MGU_SUCCESS;
2349         AddressInterface *iface;
2350
2351         if( ds == NULL ) return retVal;
2352         iface = ds->interface;
2353         if( iface == NULL ) return retVal;
2354         if( iface->getStatusCode ) {
2355                 retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
2356         }
2357         return retVal;
2358 }
2359
2360 /*
2361  * Return data read flag for specified data source.
2362  */
2363 gint addrindex_ds_read_data( AddressDataSource *ds ) {
2364         gint retVal = MGU_SUCCESS;
2365         AddressInterface *iface;
2366
2367         if( ds == NULL ) return retVal;
2368         iface = ds->interface;
2369         if( iface == NULL ) return retVal;
2370         if( iface->getReadData ) {
2371                 /*
2372                 gchar *name = ( iface->getName ) ( ds->rawDataSource );
2373                 printf( "addrindex_ds_read_data...reading:::%s:::\n", name );
2374                 */
2375                 retVal = ( iface->getReadData ) ( ds->rawDataSource );
2376         }
2377         return retVal;
2378 }
2379
2380 /*
2381  * Return data read flag for specified data source.
2382  */
2383 ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
2384         ItemFolder *retVal = NULL;
2385         AddressInterface *iface;
2386
2387         if( ds == NULL ) return retVal;
2388         iface = ds->interface;
2389         if( iface == NULL ) return retVal;
2390         if( iface->getRootFolder ) {
2391                 retVal = ( iface->getRootFolder ) ( ds->rawDataSource );
2392         }
2393         return retVal;
2394 }
2395
2396 /*
2397  * Return list of folders for specified data source.
2398  */
2399 GList *addrindex_ds_get_list_folder( AddressDataSource *ds ) {
2400         GList *retVal = FALSE;
2401         AddressInterface *iface;
2402
2403         if( ds == NULL ) return retVal;
2404         iface = ds->interface;
2405         if( iface == NULL ) return retVal;
2406         if( iface->getListFolder ) {
2407                 retVal = ( iface->getListFolder ) ( ds->rawDataSource );
2408         }
2409         return retVal;
2410 }
2411
2412 /*
2413  * Return list of persons in root folder for specified data source.
2414  */
2415 GList *addrindex_ds_get_list_person( AddressDataSource *ds ) {
2416         GList *retVal = FALSE;
2417         AddressInterface *iface;
2418
2419         if( ds == NULL ) return retVal;
2420         iface = ds->interface;
2421         if( iface == NULL ) return retVal;
2422         if( iface->getListPerson ) {
2423                 retVal = ( iface->getListPerson ) ( ds->rawDataSource );
2424         }
2425         return retVal;
2426 }
2427
2428 /*
2429  * Return name for specified data source.
2430  */
2431 gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
2432         gchar *retVal = FALSE;
2433         AddressInterface *iface;
2434
2435         if( ds == NULL ) return retVal;
2436         iface = ds->interface;
2437         if( iface == NULL ) return retVal;
2438         if( iface->getName ) {
2439                 retVal = ( iface->getName ) ( ds->rawDataSource );
2440         }
2441         return retVal;
2442 }
2443
2444 /*
2445  * Set the access flag inside the data source.
2446  */
2447 void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
2448         AddressInterface *iface;
2449
2450         if( ds == NULL ) return;
2451         iface = ds->interface;
2452         if( iface == NULL ) return;
2453         if( iface->setAccessFlag ) {
2454                 ( iface->setAccessFlag ) ( ds->rawDataSource, value );
2455         }
2456 }
2457
2458 /*
2459  * Return read only flag for specified data source.
2460  */
2461 gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
2462         AddressInterface *iface;
2463         if( ds == NULL ) return TRUE;
2464         iface = ds->interface;
2465         if( iface == NULL ) return TRUE;
2466         return iface->readOnly;
2467 }
2468
2469 /*
2470  * Return list of all persons for specified data source.
2471  */
2472 GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
2473         GList *retVal = NULL;
2474         AddressInterface *iface;
2475
2476         if( ds == NULL ) return retVal;
2477         iface = ds->interface;
2478         if( iface == NULL ) return retVal;
2479         if( iface->getAllPersons ) {
2480                 retVal = ( iface->getAllPersons ) ( ds->rawDataSource );
2481         }
2482         return retVal;
2483 }
2484
2485 /*
2486  * Return list of all groups for specified data source.
2487  */
2488 GList *addrindex_ds_get_all_groups( AddressDataSource *ds ) {
2489         GList *retVal = NULL;
2490         AddressInterface *iface;
2491
2492         if( ds == NULL ) return retVal;
2493         iface = ds->interface;
2494         if( iface == NULL ) return retVal;
2495         if( iface->getAllGroups ) {
2496                 retVal = ( iface->getAllGroups ) ( ds->rawDataSource );
2497         }
2498         return retVal;
2499 }
2500
2501 /* **********************************************************************
2502 * Address search stuff.
2503 * ***********************************************************************
2504 */
2505
2506 /**
2507  * Setup or register the dynamic search that will be performed. The search
2508  * is registered with the query manager.
2509  *
2510  * \param searchTerm    Search term. A private copy will be made.
2511  * \param callBackEntry Callback function that should be called when
2512  *                      each entry is received.
2513  * \param callBackEnd   Callback function that should be called when
2514  *                      search has finished running.
2515  * \return ID allocated to query that will be executed.
2516  */
2517 gint addrindex_setup_search(
2518         const gchar *searchTerm, void *callBackEnd, void *callBackEntry )
2519 {
2520         QueryRequest *req;
2521         gint queryID;
2522
2523         /* Set up a dynamic address query */
2524         req = qrymgr_add_request( searchTerm, callBackEnd, callBackEntry );
2525         queryID = req->queryID;
2526         qryreq_set_search_type( req, ADDRSEARCH_DYNAMIC );
2527
2528         /* printf( "***> query ID ::%d::\n", queryID ); */
2529         return queryID;
2530 }
2531
2532 #ifdef USE_LDAP
2533
2534 /*
2535  * Function prototypes (not in header file or circular reference errors are
2536  * encountered!)
2537  */
2538 LdapQuery *ldapsvr_new_dynamic_search( 
2539                 LdapServer *server, QueryRequest *req );
2540 LdapQuery *ldapsvr_new_explicit_search(
2541                 LdapServer *server, QueryRequest *req, ItemFolder *folder );
2542
2543 #endif
2544
2545 /**
2546  * Execute the previously registered dynamic search.
2547  *
2548  * \param  req Address query object to execute.
2549  * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2550  *         failed.
2551  */
2552 static gboolean addrindex_start_dynamic( QueryRequest *req ) {
2553         AddressInterface *iface;
2554         AddressDataSource *ds;
2555         GList *nodeIf;
2556         GList *nodeDS;
2557         gint type;
2558
2559         /* printf( "addrindex_start_dynamic::%d::\n", req->queryID ); */
2560         nodeIf = _addressIndex_->searchOrder;
2561         while( nodeIf ) {
2562                 iface = nodeIf->data;
2563                 nodeIf = g_list_next( nodeIf );
2564
2565                 if( ! iface->useInterface ) {
2566                         continue;
2567                 }
2568                 if( ! iface->externalQuery ) {
2569                         continue;
2570                 }
2571
2572                 type = iface->type;
2573                 nodeDS = iface->listSource;
2574                 while( nodeDS ) {
2575                         ds = nodeDS->data;
2576                         nodeDS = g_list_next( nodeDS );
2577 #ifdef USE_LDAP
2578                         if( type == ADDR_IF_LDAP ) {
2579                                 LdapServer *server;
2580                                 LdapQuery *qry;
2581
2582                                 server = ds->rawDataSource;
2583                                 if( ! server->searchFlag ) {
2584                                         continue;
2585                                 }
2586                                 if( ldapsvr_reuse_previous( server, req ) ) {
2587                                         continue;
2588                                 }
2589
2590                                 /* Start a new dynamic search */
2591                                 qry = ldapsvr_new_dynamic_search( server, req );
2592                                 if( qry ) {
2593                                         ldapsvr_execute_query( server, qry );
2594                                 }
2595                         }
2596 #endif
2597                 }
2598         }
2599         return TRUE;
2600 }
2601
2602 /**
2603  * Stop the previously registered search.
2604  *
2605  * \param queryID ID of search query to stop.
2606  */
2607 void addrindex_stop_search( const gint queryID ){
2608         QueryRequest *req;
2609         AddrQueryObject *aqo;
2610         GList *node;
2611
2612         /* printf( "addrindex_stop_search/queryID=%d\n", queryID ); */
2613         /* If query ID does not match, search has not been setup */
2614         req = qrymgr_find_request( queryID );
2615         if( req == NULL ) {
2616                 return;
2617         }
2618
2619         /* Stop all queries that were associated with request */
2620         node = req->queryList;
2621         while( node ) {
2622                 aqo = node->data;
2623 #ifdef USE_LDAP
2624                 if( aqo->queryType == ADDRQUERY_LDAP ) {
2625                         LdapQuery *qry = ( LdapQuery * ) aqo;
2626                         ldapqry_set_stop_flag( qry, TRUE );
2627                 }
2628 #endif
2629                 node->data = NULL;
2630                 node = g_list_next( node );
2631         }
2632
2633         /* Delete query request */
2634         qrymgr_delete_request( queryID );
2635 }
2636
2637 /**
2638  * Setup or register the explicit search that will be performed. The search is
2639  * registered with the query manager.
2640  *
2641  * \param  ds            Data source to search.
2642  * \param  searchTerm    Search term to locate.
2643  * \param  folder        Folder to receive search results; may be NULL.
2644  * \param  callbackEnd   Function to call when search has terminated.
2645  * \param  callbackEntry Function to called for each entry processed.
2646  * \return ID allocated to query that will be executed.
2647  */
2648 gint addrindex_setup_explicit_search(
2649         AddressDataSource *ds, const gchar *searchTerm, ItemFolder *folder,
2650         void *callBackEnd, void *callBackEntry )
2651 {
2652         QueryRequest *req;
2653         gint queryID;
2654         gchar *name;
2655
2656         /* Name the query */
2657         name = g_strdup_printf( "Search '%s'", searchTerm );
2658
2659         /* Set up query request */
2660         req = qrymgr_add_request( searchTerm, callBackEnd, callBackEntry );
2661         qryreq_set_search_type( req, ADDRSEARCH_EXPLICIT );
2662         queryID = req->queryID;
2663
2664         if( ds->type == ADDR_IF_LDAP ) {
2665 #ifdef USE_LDAP
2666                 LdapServer *server;
2667
2668                 server = ds->rawDataSource;
2669                 ldapsvr_new_explicit_search( server, req, folder );
2670 #endif
2671         }
2672         else {
2673                 qrymgr_delete_request( queryID );
2674                 queryID = 0;
2675         }
2676         g_free( name );
2677
2678         return queryID;
2679 }
2680
2681 /**
2682  * Execute the previously registered explicit search.
2683  *
2684  * \param  req Address query request object to execute.
2685  * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2686  *         failed.
2687  */
2688 static gboolean addrindex_start_explicit( QueryRequest *req ) {
2689         gboolean retVal;
2690         AddrQueryObject *aqo;
2691
2692         retVal = FALSE;
2693
2694         /* Note: there should only be one query in the list. */
2695         aqo = req->queryList->data;
2696 #ifdef USE_LDAP
2697         if( aqo->queryType == ADDRQUERY_LDAP ) {
2698                 LdapServer *server;
2699                 LdapQuery *qry;
2700
2701                 qry = ( LdapQuery * ) aqo;
2702                 server = qry->server;
2703
2704                 /* Start the search */
2705                 retVal = TRUE;
2706                 ldapsvr_execute_query( server, qry );
2707         }
2708 #endif
2709         return retVal;
2710 }
2711
2712 /**
2713  * Start the previously registered search.
2714  *
2715  * \param  queryID    ID of search query to be executed.
2716  * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
2717  *         failed.
2718  */
2719 gboolean addrindex_start_search( const gint queryID ) {
2720         gboolean retVal;
2721         QueryRequest *req;
2722         AddrSearchType searchType;
2723
2724         retVal = FALSE;
2725         /* printf( "addrindex_start_search/queryID=%d\n", queryID ); */
2726         req = qrymgr_find_request( queryID );
2727         if( req == NULL ) {
2728                 return retVal;
2729         }
2730
2731         searchType = req->searchType;
2732         if( searchType == ADDRSEARCH_DYNAMIC ) {
2733                 retVal = addrindex_start_dynamic( req );
2734         }
2735         else if( searchType == ADDRSEARCH_EXPLICIT ) {
2736                 retVal = addrindex_start_explicit( req );
2737         }
2738
2739         return retVal;
2740 }
2741
2742 /**
2743  * Remove results (folder and data) for specified data source and folder.
2744  * \param ds     Data source to process.
2745  * \param folder Results folder to remove.
2746  */
2747 void addrindex_remove_results( AddressDataSource *ds, ItemFolder *folder ) {
2748         AddrBookBase *adbase;
2749         AddressCache *cache;
2750         gint queryID = 0;
2751
2752         /* printf( "addrindex_remove_results/start\n" ); */
2753
2754         /* Test for folder */
2755         if( folder->folderType != ADDRFOLDER_QUERY_RESULTS ) return;
2756         /* printf( "folder name ::%s::\n", ADDRITEM_NAME(folder) ); */
2757         adbase = ( AddrBookBase * ) ds->rawDataSource;
2758         if( adbase == NULL ) return;
2759         cache = adbase->addressCache;
2760
2761         /* Hide folder to prevent re-display */
2762         addritem_folder_set_hidden( folder, TRUE );
2763
2764         if( ds->type == ADDR_IF_LDAP ) {
2765 #ifdef USE_LDAP
2766                 LdapQuery *qry;
2767                 gboolean  delFlag;
2768
2769                 qry = ( LdapQuery * ) folder->folderData;
2770                 queryID = ADDRQUERY_ID(qry);
2771                 /* printf( "calling ldapquery_remove_results...queryID=%d\n", queryID ); */
2772                 delFlag = ldapquery_remove_results( qry );
2773                 if (delFlag) {
2774                         ldapqry_free( qry );
2775                 }
2776                 /* printf( "calling ldapquery_remove_results...done\n" ); */
2777                 /*
2778                 if( delFlag ) {
2779                         printf( "delFlag IS-TRUE\n" );
2780                 }
2781                 else {
2782                         printf( "delFlag IS-FALSE\n" );
2783                         addressbook_clear_idler( queryID );
2784                 }
2785                 */
2786 #endif
2787         }
2788         /* printf( "addrindex_remove_results/end\n" ); */
2789
2790         /* Delete query request */
2791         if( queryID > 0 ) {
2792                 qrymgr_delete_request( queryID );
2793         }
2794 }
2795
2796 /* **********************************************************************
2797 * Address completion stuff.
2798 * ***********************************************************************
2799 */
2800
2801 /**
2802  * This function is used by the address completion function to load
2803  * addresses for all non-external address book interfaces.
2804  *
2805  * \param callBackFunc Function to be called when an address is
2806  *                     to be loaded.
2807  * \return <i>TRUE</i> if data loaded, <i>FALSE</i> if address index not loaded.
2808  */
2809 gboolean addrindex_load_completion(
2810                 gint (*callBackFunc) ( const gchar *, const gchar *, 
2811                                        const gchar *, const gchar * ) )
2812 {
2813         AddressDataSource *ds;
2814         GList *nodeIf, *nodeDS;
2815         GList *listP, *nodeP;
2816         GList *nodeM;
2817         gchar *sName;
2818
2819         nodeIf = addrindex_get_interface_list( _addressIndex_ );
2820         while( nodeIf ) {
2821                 AddressInterface *iface = nodeIf->data;
2822
2823                 nodeIf = g_list_next( nodeIf );
2824                 if( ! iface->useInterface ) {
2825                         continue;
2826                 }
2827                 if( iface->externalQuery ) {
2828                         continue;
2829                 }
2830                 nodeDS = iface->listSource;
2831                 while( nodeDS ) {
2832                         ds = nodeDS->data;
2833
2834                         /* Read address book */
2835                         if( addrindex_ds_get_modify_flag( ds ) ) {
2836                                 addrindex_ds_read_data( ds );
2837                         }
2838
2839                         if( ! addrindex_ds_get_read_flag( ds ) ) {
2840                                 addrindex_ds_read_data( ds );
2841                         }
2842
2843                         /* Get all persons */
2844                         listP = addrindex_ds_get_all_persons( ds );
2845                         nodeP = listP;
2846                         while( nodeP ) {
2847                                 ItemPerson *person = nodeP->data;
2848                                 nodeM = person->listEMail;
2849
2850                                 /* Figure out name to use */
2851                                 sName = ADDRITEM_NAME(person);
2852                                 if( sName == NULL || *sName == '\0' ) {
2853                                         sName = person->nickName;
2854                                 }
2855
2856                                 /* Process each E-Mail address */
2857                                 while( nodeM ) {
2858                                         ItemEMail *email = nodeM->data;
2859                                         
2860                                         callBackFunc( sName, email->address, person->nickName, 
2861                                                       ADDRITEM_NAME(email) );
2862                                         
2863                                         nodeM = g_list_next( nodeM );
2864                                 }
2865                                 nodeP = g_list_next( nodeP );
2866                         }
2867                         /* Free up the list */
2868                         g_list_free( listP );
2869
2870                         nodeDS = g_list_next( nodeDS );
2871                 }
2872         }
2873
2874         return TRUE;
2875 }
2876
2877 /**
2878  * This function can be used to collect information about
2879  * addressbook entries that contain a specific attribute.
2880  *
2881  * \param attr         Name of attribute to look for
2882  * \param callBackFunc Function to be called when a matching attribute was found
2883  * \return <i>TRUE</i>
2884  */
2885 gboolean addrindex_load_person_attribute(
2886                 const gchar *attr,
2887                 gint (*callBackFunc) ( ItemPerson *, const gchar * ) )
2888 {
2889         AddressDataSource *ds;
2890         GList *nodeIf, *nodeDS;
2891         GList *listP, *nodeP;
2892         GList *nodeA;
2893
2894         nodeIf = addrindex_get_interface_list( _addressIndex_ );
2895         while( nodeIf ) {
2896                 gchar *cur_bname;
2897                 AddressInterface *iface = nodeIf->data;
2898
2899                 nodeIf = g_list_next( nodeIf );
2900                 if( ! iface->useInterface ) {
2901                         continue;
2902                 }
2903                 if( iface->externalQuery ) {
2904                         continue;
2905                 }
2906                 nodeDS = iface->listSource;
2907                 while( nodeDS ) {
2908                         ds = nodeDS->data;
2909
2910                         /* Read address book */
2911                         if( addrindex_ds_get_modify_flag( ds ) ) {
2912                                 addrindex_ds_read_data( ds );
2913                         }
2914
2915                         if( ! addrindex_ds_get_read_flag( ds ) ) {
2916                                 addrindex_ds_read_data( ds );
2917                         }
2918
2919                         /* Check addressbook name */
2920                         cur_bname = addrindex_ds_get_name( ds );
2921
2922                         /* Get all persons */
2923                         listP = addrindex_ds_get_all_persons( ds );
2924                         nodeP = listP;
2925                         while( nodeP ) {
2926                                 ItemPerson *person = nodeP->data;
2927
2928                                 /* Return all ItemPerson's if attr is NULL */
2929                                 if( attr == NULL ) {
2930                                         callBackFunc(person, cur_bname);
2931                                 }
2932
2933                                 /* Return ItemPerson's with specific attribute */
2934                                 else {
2935                                         nodeA = person->listAttrib;
2936                                         /* Process each User Attribute */
2937                                         while( nodeA ) {
2938                                                 UserAttribute *attrib = nodeA->data;
2939                                                 if( attrib->name && 
2940                                                     !strcmp( attrib->name,attr ) ) {
2941                                                         callBackFunc(person, cur_bname);
2942                                                 }
2943                                                 nodeA = g_list_next( nodeA );
2944                                         }
2945                                 }
2946                                 nodeP = g_list_next( nodeP );
2947                         }
2948                         /* Free up the list */
2949                         g_list_free( listP );
2950
2951                         nodeDS = g_list_next( nodeDS );
2952                 }
2953         }
2954         return TRUE;
2955 }
2956
2957 /*
2958  * End of Source.
2959  */