Statistics
| Revision:

root / src / addrindex.c @ 1

History | View | Annotate | Download (51.5 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 2001 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 <stdlib.h>
32

    
33
#include "intl.h"
34
#include "mgutils.h"
35
#include "addritem.h"
36
#include "addrcache.h"
37
#include "addrbook.h"
38
#include "addrindex.h"
39
#include "xml.h"
40

    
41
#ifndef DEV_STANDALONE
42
#include "prefs.h"
43
#include "codeconv.h"
44
#endif
45

    
46
#include "vcard.h"
47

    
48
#ifdef USE_JPILOT
49
#include "jpilot.h"
50
#endif
51

    
52
#ifdef USE_LDAP
53
#include "syldap.h"
54
#endif
55

    
56
#define TAG_ADDRESS_INDEX    "addressbook"
57

    
58
#define TAG_IF_ADDRESS_BOOK  "book_list"
59
#define TAG_IF_VCARD         "vcard_list"
60
#define TAG_IF_JPILOT        "jpilot_list"
61
#define TAG_IF_LDAP          "ldap_list"
62

    
63
#define TAG_DS_ADDRESS_BOOK  "book"
64
#define TAG_DS_VCARD         "vcard"
65
#define TAG_DS_JPILOT        "jpilot"
66
#define TAG_DS_LDAP          "server"
67

    
68
/* XML Attribute names */
69
#define ATTAG_BOOK_NAME       "name"
70
#define ATTAG_BOOK_FILE       "file"
71

    
72
#define ATTAG_VCARD_NAME      "name"
73
#define ATTAG_VCARD_FILE      "file"
74

    
75
#define ATTAG_JPILOT_NAME     "name"
76
#define ATTAG_JPILOT_FILE     "file"
77
#define ATTAG_JPILOT_CUSTOM_1 "custom-1"
78
#define ATTAG_JPILOT_CUSTOM_2 "custom-2"
79
#define ATTAG_JPILOT_CUSTOM_3 "custom-3"
80
#define ATTAG_JPILOT_CUSTOM_4 "custom-4"
81
#define ATTAG_JPILOT_CUSTOM   "custom-"
82

    
83
#define ATTAG_LDAP_NAME       "name"
84
#define ATTAG_LDAP_HOST       "host"
85
#define ATTAG_LDAP_PORT       "port"
86
#define ATTAG_LDAP_BASE_DN    "base-dn"
87
#define ATTAG_LDAP_BIND_DN    "bind-dn"
88
#define ATTAG_LDAP_BIND_PASS  "bind-pass"
89
#define ATTAG_LDAP_CRITERIA   "criteria"
90
#define ATTAG_LDAP_MAX_ENTRY  "max-entry"
91
#define ATTAG_LDAP_TIMEOUT    "timeout"
92

    
93
#if 0
94
N_("Common address")
95
N_("Personal address")
96
#endif
97

    
98
#define DISP_NEW_COMMON       _("Common address")
99
#define DISP_NEW_PERSONAL     _("Personal address")
100

    
101
/* Old address book */
102
#define TAG_IF_OLD_COMMON     "common_address"
103
#define TAG_IF_OLD_PERSONAL   "personal_address"
104

    
105
#define DISP_OLD_COMMON       _("Common address")
106
#define DISP_OLD_PERSONAL     _("Personal address")
107

    
108
typedef struct _AddressIfAttr AddressIfAttrib;
109
struct _AddressIfAttr {
110
        gchar *name;
111
        gchar *value;
112
};
113

    
114
/*
115
* Build interface with default values.
116
*/
117
static AddressInterface *addrindex_create_interface( gint type, gchar *name, gchar *tagIf, gchar *tagDS ) {
118
        AddressInterface *iface = g_new0( AddressInterface, 1 );
119
        ADDRITEM_TYPE(iface) = ITEMTYPE_INTERFACE;
120
        ADDRITEM_ID(iface) = NULL;
121
        ADDRITEM_NAME(iface) = g_strdup( name );
122
        ADDRITEM_PARENT(iface) = NULL;
123
        ADDRITEM_SUBTYPE(iface) = type;
124
        iface->type = type;
125
        iface->name = g_strdup( name );
126
        iface->listTag = g_strdup( tagIf );
127
        iface->itemTag = g_strdup( tagDS );
128
        iface->legacyFlag = FALSE;
129
        iface->haveLibrary = TRUE;
130
        iface->useInterface = TRUE;
131
        iface->readOnly      = TRUE;
132
        iface->getAccessFlag = NULL;
133
        iface->getModifyFlag = NULL;
134
        iface->getReadFlag   = NULL;
135
        iface->getStatusCode = NULL;
136
        iface->getReadData   = NULL;
137
        iface->getRootFolder = NULL;
138
        iface->getListFolder = NULL;
139
        iface->getListPerson = NULL;
140
        iface->getAllPersons = NULL;
141
        iface->getAllGroups  = NULL;
142
        iface->getName       = NULL;
143
        iface->listSource = NULL;
144
        return iface;
145
}
146

    
147
/*
148
* Build table of interfaces.
149
*/
150
static void addrindex_build_if_list( AddressIndex *addrIndex ) {
151
        AddressInterface *iface;
152

    
153
        iface = addrindex_create_interface( ADDR_IF_BOOK, "Address Book", TAG_IF_ADDRESS_BOOK, TAG_DS_ADDRESS_BOOK );
154
        iface->readOnly      = FALSE;
155
        iface->getModifyFlag = ( void * ) addrbook_get_modified;
156
        iface->getAccessFlag = ( void * ) addrbook_get_accessed;
157
        iface->getReadFlag   = ( void * ) addrbook_get_read_flag;
158
        iface->getStatusCode = ( void * ) addrbook_get_status;
159
        iface->getReadData   = ( void * ) addrbook_read_data;
160
        iface->getRootFolder = ( void * ) addrbook_get_root_folder;
161
        iface->getListFolder = ( void * ) addrbook_get_list_folder;
162
        iface->getListPerson = ( void * ) addrbook_get_list_person;
163
        iface->getAllPersons = ( void * ) addrbook_get_all_persons;
164
        iface->getName       = ( void * ) addrbook_get_name;
165
        iface->setAccessFlag = ( void * ) addrbook_set_accessed;
166
        addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
167
        ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
168

    
169
        iface = addrindex_create_interface( ADDR_IF_VCARD, "vCard", TAG_IF_VCARD, TAG_DS_VCARD );
170
        iface->getModifyFlag = ( void * ) vcard_get_modified;
171
        iface->getAccessFlag = ( void * ) vcard_get_accessed;
172
        iface->getReadFlag   = ( void * ) vcard_get_read_flag;
173
        iface->getStatusCode = ( void * ) vcard_get_status;
174
        iface->getReadData   = ( void * ) vcard_read_data;
175
        iface->getRootFolder = ( void * ) vcard_get_root_folder;
176
        iface->getListFolder = ( void * ) vcard_get_list_folder;
177
        iface->getListPerson = ( void * ) vcard_get_list_person;
178
        iface->getAllPersons = ( void * ) vcard_get_all_persons;
179
        iface->getName       = ( void * ) vcard_get_name;
180
        iface->setAccessFlag = ( void * ) vcard_set_accessed;
181
        addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
182
        ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
183

    
184
        iface = addrindex_create_interface( ADDR_IF_JPILOT, "JPilot", TAG_IF_JPILOT, TAG_DS_JPILOT );
185
#ifdef USE_JPILOT
186
        /* iface->haveLibrary = jpilot_test_pilot_lib(); */
187
        iface->haveLibrary = TRUE;
188
        iface->useInterface = iface->haveLibrary;
189
        iface->getModifyFlag = ( void * ) jpilot_get_modified;
190
        iface->getAccessFlag = ( void * ) jpilot_get_accessed;
191
        iface->getReadFlag   = ( void * ) jpilot_get_read_flag;
192
        iface->getStatusCode = ( void * ) jpilot_get_status;
193
        iface->getReadData   = ( void * ) jpilot_read_data;
194
        iface->getRootFolder = ( void * ) jpilot_get_root_folder;
195
        iface->getListFolder = ( void * ) jpilot_get_list_folder;
196
        iface->getListPerson = ( void * ) jpilot_get_list_person;
197
        iface->getAllPersons = ( void * ) jpilot_get_all_persons;
198
        iface->getName       = ( void * ) jpilot_get_name;
199
        iface->setAccessFlag = ( void * ) jpilot_set_accessed;
200
#else
201
        iface->useInterface = FALSE;
202
        iface->haveLibrary = FALSE;
203
#endif
204
        addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
205
        ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
206

    
207
        iface = addrindex_create_interface( ADDR_IF_LDAP, "LDAP", TAG_IF_LDAP, TAG_DS_LDAP );
208
#ifdef USE_LDAP
209
        /* iface->haveLibrary = syldap_test_ldap_lib(); */
210
        iface->haveLibrary = TRUE;
211
        iface->useInterface = iface->haveLibrary;
212
        iface->getAccessFlag = ( void * ) syldap_get_accessed;
213
        /* iface->getModifyFlag = ( void * ) syldap_get_modified; */
214
        /* iface->getReadFlag   = ( void * ) syldap_get_read_flag; */
215
        iface->getStatusCode = ( void * ) syldap_get_status;
216
        iface->getReadData   = ( void * ) syldap_read_data;
217
        iface->getRootFolder = ( void * ) syldap_get_root_folder;
218
        iface->getListFolder = ( void * ) syldap_get_list_folder;
219
        iface->getListPerson = ( void * ) syldap_get_list_person;
220
        iface->getName       = ( void * ) syldap_get_name;
221
        iface->setAccessFlag = ( void * ) syldap_set_accessed;
222
#else
223
        iface->useInterface = FALSE;
224
        iface->haveLibrary = FALSE;
225
#endif
226
        addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
227
        ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
228

    
229
        /* Two old legacy data sources */
230
        iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - common", TAG_IF_OLD_COMMON, NULL );
231
        iface->legacyFlag = TRUE;
232
        addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
233
        ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
234

    
235
        iface = addrindex_create_interface( ADDR_IF_COMMON, "Old Address - personal", TAG_IF_OLD_PERSONAL, NULL );
236
        iface->legacyFlag = TRUE;
237
        addrIndex->interfaceList = g_list_append( addrIndex->interfaceList, iface );
238
        ADDRITEM_PARENT(iface) = ADDRITEM_OBJECT(addrIndex);
239

    
240
}
241

    
242
/*
243
* Free name-value pairs.
244
*/
245
static void addrindex_free_attributes( GList *list ) {
246
        GList *node = list;
247
        while( node ) {
248
                AddressIfAttrib *nv = node->data;
249
                g_free( nv->name ); nv->name = NULL;
250
                g_free( nv->value ); nv->value = NULL;
251
                g_free( nv );
252
                node->data = NULL;
253
                node = g_list_next( node );
254
        }
255
        g_list_free( list );
256
}
257

    
258
/*
259
* Free up data source.
260
*/
261
void addrindex_free_datasource( AddressIndex *addrIndex, AddressDataSource *ds ) {
262
        AddressInterface *iface = NULL;
263
        g_return_if_fail( addrIndex != NULL );
264
        g_return_if_fail( ds != NULL );
265

    
266
        if( ds->interface == NULL ) {
267
                iface = addrindex_get_interface( addrIndex, ds->type );
268
        }
269
        if( iface == NULL ) return;
270

    
271
        if( iface->useInterface ) {
272
                if( iface->type == ADDR_IF_BOOK ) {
273
                        AddressBookFile *abf = ds->rawDataSource;
274
                        if( abf ) {
275
                                addrbook_free_book( abf );
276
                        }
277
                }
278
                else if( iface->type == ADDR_IF_VCARD ) {
279
                        VCardFile *vcf = ds->rawDataSource;
280
                        if( vcf ) {
281
                                vcard_free( vcf );
282
                        }
283
                }
284
#ifdef USE_JPILOT
285
                else if( iface->type == ADDR_IF_JPILOT ) {
286
                        JPilotFile *jpf = ds->rawDataSource;
287
                        if( jpf ) {
288
                                jpilot_free( jpf );
289
                        }
290
                }
291
#endif
292
#ifdef USE_LDAP
293
                else if( iface->type == ADDR_IF_LDAP ) {
294
                        SyldapServer *server = ds->rawDataSource;
295
                        if( server ) {
296
                                syldap_free( server );
297
                        }
298
                }
299
#endif
300
        }
301
        else {
302
                GList *list = ds->rawDataSource;
303
                addrindex_free_attributes( list );
304
        }
305

    
306
        g_free( ADDRITEM_ID(addrIndex) );
307
        g_free( ADDRITEM_NAME(addrIndex) );
308

    
309
        ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
310
        ADDRITEM_ID(addrIndex) = NULL;
311
        ADDRITEM_NAME(addrIndex) = NULL;
312
        ADDRITEM_PARENT(addrIndex) = NULL;
313
        ADDRITEM_SUBTYPE(addrIndex) = 0;
314
        ds->type = ADDR_IF_NONE;
315
        ds->rawDataSource = NULL;
316
        ds->interface = NULL;
317

    
318
        ds->type = ADDR_IF_NONE;
319
        ds->rawDataSource = NULL;
320
        ds->interface = NULL;
321
        g_free( ds );
322
}
323

    
324
static void addrindex_free_all_datasources( AddressInterface *iface ) {
325
        GList *node = iface->listSource;
326
        while( node ) {
327
                AddressDataSource *ds = node->data;
328
                if( iface->useInterface ) {
329
                        if( iface->type == ADDR_IF_BOOK ) {
330
                                AddressBookFile *abf = ds->rawDataSource;
331
                                if( abf ) {
332
                                        addrbook_free_book( abf );
333
                                }
334
                        }
335
                        else if( iface->type == ADDR_IF_VCARD ) {
336
                                VCardFile *vcf = ds->rawDataSource;
337
                                if( vcf ) {
338
                                        vcard_free( vcf );
339
                                }
340
                        }
341
#ifdef USE_JPILOT
342
                        else if( iface->type == ADDR_IF_JPILOT ) {
343
                                JPilotFile *jpf = ds->rawDataSource;
344
                                if( jpf ) {
345
                                        jpilot_free( jpf );
346
                                }
347
                        }
348
#endif
349
#ifdef USE_LDAP
350
                        else if( iface->type == ADDR_IF_LDAP ) {
351
                                SyldapServer *server = ds->rawDataSource;
352
                                if( server ) {
353
                                        syldap_free( server );
354
                                }
355
                        }
356
#endif
357
                }
358
                else {
359
                        GList *list = ds->rawDataSource;
360
                        addrindex_free_attributes( list );
361
                }
362

    
363
                ds->type = ADDR_IF_NONE;
364
                ds->rawDataSource = NULL;
365
                ds->interface = NULL;
366
                g_free( ds );
367
                node->data = NULL;
368
                node = g_list_next( node );
369
        }
370
}
371

    
372
static void addrindex_free_interface( AddressInterface *iface ) {
373
        addrindex_free_all_datasources( iface );
374

    
375
        g_free( ADDRITEM_ID(iface) );
376
        g_free( ADDRITEM_NAME(iface) );
377
        g_free( iface->name );
378
        g_free( iface->listTag );
379
        g_free( iface->itemTag );
380

    
381
        ADDRITEM_TYPE(iface) = ITEMTYPE_NONE;
382
        ADDRITEM_ID(iface) = NULL;
383
        ADDRITEM_NAME(iface) = NULL;
384
        ADDRITEM_PARENT(iface) = NULL;
385
        ADDRITEM_SUBTYPE(iface) = 0;
386
        iface->type = ADDR_IF_NONE;
387
        iface->name = NULL;
388
        iface->listTag = NULL;
389
        iface->itemTag = NULL;
390
        iface->legacyFlag = FALSE;
391
        iface->useInterface = FALSE;
392
        iface->haveLibrary = FALSE;
393

    
394
        g_list_free( iface->listSource );
395
        iface->listSource = NULL;
396
}
397

    
398
/*
399
* Create new object.
400
*/
401
AddressIndex *addrindex_create_index() {
402
        AddressIndex *addrIndex = g_new0( AddressIndex, 1 );
403

    
404
        ADDRITEM_TYPE(addrIndex) = ITEMTYPE_INDEX;
405
        ADDRITEM_ID(addrIndex) = NULL;
406
        ADDRITEM_NAME(addrIndex) = g_strdup( "Address Index" );
407
        ADDRITEM_PARENT(addrIndex) = NULL;
408
        ADDRITEM_SUBTYPE(addrIndex) = 0;
409
        addrIndex->filePath = NULL;
410
        addrIndex->fileName = NULL;
411
        addrIndex->retVal = MGU_SUCCESS;
412
        addrIndex->needsConversion = FALSE;
413
        addrIndex->wasConverted = FALSE;
414
        addrIndex->conversionError = FALSE;
415
        addrIndex->interfaceList = NULL;
416
        addrIndex->lastType = ADDR_IF_NONE;
417
        addrIndex->dirtyFlag = FALSE;
418
        addrindex_build_if_list( addrIndex );
419
        return addrIndex;
420
}
421

    
422
/*
423
* Specify file to be used.
424
*/
425
void addrindex_set_file_path( AddressIndex *addrIndex, const gchar *value ) {
426
        g_return_if_fail( addrIndex != NULL );
427
        addrIndex->filePath = mgu_replace_string( addrIndex->filePath, value );
428
}
429
void addrindex_set_file_name( AddressIndex *addrIndex, const gchar *value ) {
430
        g_return_if_fail( addrIndex != NULL );
431
        addrIndex->fileName = mgu_replace_string( addrIndex->fileName, value );
432
}
433
void addrindex_set_dirty( AddressIndex *addrIndex, const gboolean value ) {
434
        g_return_if_fail( addrIndex != NULL );
435
        addrIndex->dirtyFlag = value;
436
}
437

    
438
/*
439
* Return list of interfaces.
440
*/
441
GList *addrindex_get_interface_list( AddressIndex *addrIndex ) {
442
        g_return_val_if_fail( addrIndex != NULL, NULL );
443
        return addrIndex->interfaceList;
444
}
445

    
446
/*
447
* Free up object.
448
*/
449
void addrindex_free_index( AddressIndex *addrIndex ) {
450
        GList *node;
451

    
452
        g_return_if_fail( addrIndex != NULL );
453

    
454
        g_free( ADDRITEM_ID(addrIndex) );
455
        g_free( ADDRITEM_NAME(addrIndex) );
456
        g_free( addrIndex->filePath );
457
        g_free( addrIndex->fileName );
458
        ADDRITEM_TYPE(addrIndex) = ITEMTYPE_NONE;
459
        ADDRITEM_ID(addrIndex) = NULL;
460
        ADDRITEM_NAME(addrIndex) = NULL;
461
        ADDRITEM_PARENT(addrIndex) = NULL;
462
        ADDRITEM_SUBTYPE(addrIndex) = 0;
463
        addrIndex->filePath = NULL;
464
        addrIndex->fileName = NULL;
465
        addrIndex->retVal = MGU_SUCCESS;
466
        addrIndex->needsConversion = FALSE;
467
        addrIndex->wasConverted = FALSE;
468
        addrIndex->conversionError = FALSE;
469
        addrIndex->lastType = ADDR_IF_NONE;
470
        addrIndex->dirtyFlag = FALSE;
471
        node = addrIndex->interfaceList;
472
        while( node ) {
473
                AddressInterface *iface = node->data;
474
                addrindex_free_interface( iface );
475
                node = g_list_next( node );
476
        }
477
        g_list_free( addrIndex->interfaceList );
478
        addrIndex->interfaceList = NULL;
479
        g_free( addrIndex );
480
}
481

    
482
/*
483
* Print address index.
484
*/
485
void addrindex_print_index( AddressIndex *addrIndex, FILE *stream ) {
486
        g_return_if_fail( addrIndex != NULL );
487
        fprintf( stream, "AddressIndex:\n" );
488
        fprintf( stream, "\tfile path: '%s'\n", addrIndex->filePath );
489
        fprintf( stream, "\tfile name: '%s'\n", addrIndex->fileName );
490
        fprintf( stream, "\t   status: %d : '%s'\n", addrIndex->retVal, mgu_error2string( addrIndex->retVal ) );
491
        fprintf( stream, "\tconverted: '%s'\n", addrIndex->wasConverted ? "yes" : "no" );
492
        fprintf( stream, "\tcvt error: '%s'\n", addrIndex->conversionError ? "yes" : "no" );
493
        fprintf( stream, "\t---\n" );
494
}
495

    
496
/*
497
* Retrieve specified interface from index.
498
*/
499
AddressInterface *addrindex_get_interface( AddressIndex *addrIndex, AddressIfType ifType ) {
500
        AddressInterface *retVal = NULL;
501
        GList *node;
502

    
503
        g_return_val_if_fail( addrIndex != NULL, NULL );
504

    
505
        node = addrIndex->interfaceList;
506
        while( node ) {
507
                AddressInterface *iface = node->data;
508
                node = g_list_next( node );
509
                if( iface->type == ifType ) {
510
                        retVal = iface;
511
                        break;
512
                }
513
        }
514
        return retVal;
515
}
516

    
517
AddressDataSource *addrindex_create_datasource() {
518
        AddressDataSource *ds = NULL;
519
        ds = g_new0( AddressDataSource, 1 );
520
        ADDRITEM_TYPE(ds) = ITEMTYPE_DATASOURCE;
521
        ADDRITEM_ID(ds) = NULL;
522
        ADDRITEM_NAME(ds) = NULL;
523
        ADDRITEM_PARENT(ds) = NULL;
524
        ADDRITEM_SUBTYPE(ds) = 0;
525
        ds->type = ADDR_IF_NONE;
526
        ds->rawDataSource = NULL;
527
        ds->interface = NULL;
528
        return ds;
529
}
530

    
531
/*
532
* Add data source to index.
533
* Enter: addrIndex  Address index object.
534
*        ifType     Interface type to add.
535
*        dataSource Actual data source to add.
536
* Return: TRUE if data source was added.
537
* Note: The raw data object (for example, AddressBookFile or VCardFile object) should be
538
* supplied as the dataSource argument.
539
*/
540
AddressDataSource *addrindex_index_add_datasource( AddressIndex *addrIndex, AddressIfType ifType, gpointer dataSource ) {
541
        AddressInterface *iface;
542
        AddressDataSource *ds = NULL;
543

    
544
        g_return_val_if_fail( addrIndex != NULL, NULL );
545
        g_return_val_if_fail( dataSource != NULL, NULL );
546

    
547
        iface = addrindex_get_interface( addrIndex, ifType );
548
        if( iface ) {
549
                ds = addrindex_create_datasource();
550
                ADDRITEM_PARENT(ds) = ADDRITEM_OBJECT(iface);
551
                ds->type = ifType;
552
                ds->rawDataSource = dataSource;
553
                ds->interface = iface;
554
                iface->listSource = g_list_append( iface->listSource, ds );
555
                addrIndex->dirtyFlag = TRUE;
556
        }
557
        return ds;
558
}
559

    
560
/*
561
* Remove data source from index.
562
* Enter: addrIndex  Address index object.
563
*        dataSource Data source to remove.
564
* Return: Data source if removed, or NULL if data source was not found in
565
* index. Note the this object must still be freed.
566
*/
567
AddressDataSource *addrindex_index_remove_datasource( AddressIndex *addrIndex, AddressDataSource *dataSource ) {
568
        AddressDataSource *retVal = FALSE;
569
        AddressInterface *iface;
570

    
571
        g_return_val_if_fail( addrIndex != NULL, NULL );
572
        g_return_val_if_fail( dataSource != NULL, NULL );
573

    
574
        iface = addrindex_get_interface( addrIndex, dataSource->type );
575
        if( iface ) {
576
                iface->listSource = g_list_remove( iface->listSource, dataSource );
577
                addrIndex->dirtyFlag = TRUE;
578
                dataSource->interface = NULL;
579
                retVal = dataSource;
580
        }
581
        return retVal;
582
}
583

    
584
static AddressInterface *addrindex_tag_get_interface( AddressIndex *addrIndex, gchar *tag, AddressIfType ifType ) {
585
        AddressInterface *retVal = NULL;
586
        GList *node = addrIndex->interfaceList;
587

    
588
        while( node ) {
589
                AddressInterface *iface = node->data;
590
                node = g_list_next( node );
591
                if( tag ) {
592
                        if( strcmp( iface->listTag, tag ) == 0 ) {
593
                                retVal = iface;
594
                                break;
595
                        }
596
                }
597
                else {
598
                        if( iface->type == ifType ) {
599
                                retVal = iface;
600
                                break;
601
                        }
602
                }
603
        }
604
        return retVal;
605
}
606

    
607
static AddressInterface *addrindex_tag_get_datasource( AddressIndex *addrIndex, AddressIfType ifType, gchar *tag ) {
608
        AddressInterface *retVal = NULL;
609
        GList *node = addrIndex->interfaceList;
610

    
611
        while( node ) {
612
                AddressInterface *iface = node->data;
613
                node = g_list_next( node );
614
                if( iface->type == ifType && iface->itemTag ) {
615
                        if( strcmp( iface->itemTag, tag ) == 0 ) {
616
                                retVal = iface;
617
                                break;
618
                        }
619
                }
620
        }
621
        return retVal;
622
}
623

    
624
/* **********************************************************************
625
* Interface XML parsing functions.
626
* ***********************************************************************
627
*/
628
#if 0
629
static void show_attribs( GList *attr ) {
630
        while( attr ) {
631
                gchar *name = ((XMLAttr *)attr->data)->name;
632
                gchar *value = ((XMLAttr *)attr->data)->value;
633
                printf( "\tattr value : %s :%s:\n", name, value );
634
                attr = g_list_next( attr );
635
        }
636
        printf( "\t---\n" );
637
}
638
#endif
639

    
640
static void addrindex_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
641
        gint i;
642
        for( i = 0; i < lvl; i++ ) fputs( "  ", fp );
643
        fputs( "<", fp );
644
        fputs( name, fp );
645
}
646

    
647
static void addrindex_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
648
        gint i;
649
        for( i = 0; i < lvl; i++ ) fputs( "  ", fp );
650
        fputs( "</", fp );
651
        fputs( name, fp );
652
        fputs( ">\n", fp );
653
}
654

    
655
static void addrindex_write_attr( FILE *fp, gchar *name, gchar *value ) {
656
        fputs( " ", fp );
657
        fputs( name, fp );
658
        fputs( "=\"", fp );
659
        xml_file_put_escape_str( fp, value );
660
        fputs( "\"", fp );
661
}
662

    
663
/*
664
* Return list of name-value pairs.
665
*/
666
static GList *addrindex_read_attributes( XMLFile *file ) {
667
        GList *list = NULL;
668
        AddressIfAttrib *nv;
669
        GList *attr;
670
        gchar *name;
671
        gchar *value;
672

    
673
        attr = xml_get_current_tag_attr( file );
674
        while( attr ) {
675
                name = ((XMLAttr *)attr->data)->name;
676
                value = ((XMLAttr *)attr->data)->value;
677
                nv = g_new0( AddressIfAttrib, 1 );
678
                nv->name = g_strdup( name );
679
                nv->value = g_strdup( value );
680
                list = g_list_append( list, nv );
681
                attr = g_list_next( attr );
682
        }
683
        return list;
684
}
685

    
686
/*
687
* Output name-value pairs.
688
*/
689
static void addrindex_write_attributes( FILE *fp, gchar *tag, GList *list, gint lvl ) {
690
        GList *node;
691
        AddressIfAttrib *nv;
692
        if( list ) {
693
                addrindex_write_elem_s( fp, lvl, tag );
694
                node = list;
695
                while( node ) {
696
                        nv = node->data;
697
                        addrindex_write_attr( fp, nv->name, nv->value );
698
                        node = g_list_next( node );
699
                }
700
                fputs(" />\n", fp);
701
        }
702
}
703

    
704
#if 0
705
static void addrindex_print_attributes( GList *list, FILE *stream ) {
706
        GList *node = list;
707
        while( node ) {
708
                AddressIfAttrib *nv = node->data;
709
                fprintf( stream, "%s : %s\n", nv->name, nv->value );
710
                node = g_list_next( node );
711
        }
712
}
713
#endif
714

    
715
static AddressDataSource *addrindex_parse_book( XMLFile *file ) {
716
        AddressDataSource *ds = g_new0( AddressDataSource, 1 );
717
        AddressBookFile *abf;
718
        GList *attr;
719

    
720
        abf = addrbook_create_book();
721
        attr = xml_get_current_tag_attr( file );
722
        while( attr ) {
723
                gchar *name = ((XMLAttr *)attr->data)->name;
724
                gchar *value = ((XMLAttr *)attr->data)->value;
725
                if( strcmp( name, ATTAG_BOOK_NAME ) == 0 ) {
726
                        addrbook_set_name( abf, value );
727
                }
728
                else if( strcmp( name, ATTAG_BOOK_FILE ) == 0) {
729
                        addrbook_set_file( abf, value );
730
                }
731
                attr = g_list_next( attr );
732
        }
733
        ds->rawDataSource = abf;
734
        return ds;
735
}
736

    
737
static void addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
738
        AddressBookFile *abf = ds->rawDataSource;
739
        if( abf ) {
740
                addrindex_write_elem_s( fp, lvl, TAG_DS_ADDRESS_BOOK );
741
                addrindex_write_attr( fp, ATTAG_BOOK_NAME, abf->name );
742
                addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName );
743
                fputs( " />\n", fp );
744
        }
745
}
746

    
747
static AddressDataSource *addrindex_parse_vcard( XMLFile *file ) {
748
        AddressDataSource *ds = g_new0( AddressDataSource, 1 );
749
        VCardFile *vcf;
750
        GList *attr;
751

    
752
        vcf = vcard_create();
753
        attr = xml_get_current_tag_attr( file );
754
        while( attr ) {
755
                gchar *name = ((XMLAttr *)attr->data)->name;
756
                gchar *value = ((XMLAttr *)attr->data)->value;
757
                if( strcmp( name, ATTAG_VCARD_NAME ) == 0 ) {
758
                        vcard_set_name( vcf, value );
759
                }
760
                else if( strcmp( name, ATTAG_VCARD_FILE ) == 0) {
761
                        vcard_set_file( vcf, value );
762
                }
763
                attr = g_list_next( attr );
764
        }
765
        ds->rawDataSource = vcf;
766
        return ds;
767
}
768

    
769
static void addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
770
             VCardFile *vcf = ds->rawDataSource;
771
        if( vcf ) {
772
                addrindex_write_elem_s( fp, lvl, TAG_DS_VCARD );
773
                addrindex_write_attr( fp, ATTAG_VCARD_NAME, vcf->name );
774
                addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path );
775
                fputs( " />\n", fp );
776
        }
777
}
778

    
779
#ifdef USE_JPILOT
780
static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
781
        AddressDataSource *ds = g_new0( AddressDataSource, 1 );
782
        JPilotFile *jpf;
783
        GList *attr;
784

    
785
        jpf = jpilot_create();
786
        attr = xml_get_current_tag_attr( file );
787
        while( attr ) {
788
                gchar *name = ((XMLAttr *)attr->data)->name;
789
                gchar *value = ((XMLAttr *)attr->data)->value;
790
                if( strcmp( name, ATTAG_JPILOT_NAME ) == 0 ) {
791
                        jpilot_set_name( jpf, value );
792
                }
793
                else if( strcmp( name, ATTAG_JPILOT_FILE ) == 0 ) {
794
                        jpilot_set_file( jpf, value );
795
                }
796
                else if( strcmp( name, ATTAG_JPILOT_CUSTOM_1 ) == 0 ) {
797
                        jpilot_add_custom_label( jpf, value );
798
                }
799
                else if( strcmp( name, ATTAG_JPILOT_CUSTOM_2 ) == 0 ) {
800
                        jpilot_add_custom_label( jpf, value );
801
                }
802
                else if( strcmp( name, ATTAG_JPILOT_CUSTOM_3 ) == 0 ) {
803
                        jpilot_add_custom_label( jpf, value );
804
                }
805
                else if( strcmp( name, ATTAG_JPILOT_CUSTOM_4 ) == 0 ) {
806
                        jpilot_add_custom_label( jpf, value );
807
                }
808
                attr = g_list_next( attr );
809
        }
810
        ds->rawDataSource = jpf;
811
        return ds;
812
}
813

    
814
static void addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
815
        JPilotFile *jpf = ds->rawDataSource;
816
        if( jpf ) {
817
                gint ind;
818
                GList *node;
819
                GList *customLbl = jpilot_get_custom_labels( jpf );
820
                addrindex_write_elem_s( fp, lvl, TAG_DS_JPILOT );
821
                addrindex_write_attr( fp, ATTAG_JPILOT_NAME, jpf->name );
822
                addrindex_write_attr( fp, ATTAG_JPILOT_FILE, jpf->path );
823
                node = customLbl;
824
                ind = 1;
825
                while( node ) {
826
                        gchar name[256];
827
                        sprintf( name, "%s%d", ATTAG_JPILOT_CUSTOM, ind );
828
                        addrindex_write_attr( fp, name, node->data );
829
                        ind++;
830
                        node = g_list_next( node );
831
                }
832
                fputs( " />\n", fp );
833
        }
834
}
835
#else
836
/* Just read/write name-value pairs */
837
static AddressDataSource *addrindex_parse_jpilot( XMLFile *file ) {
838
        AddressDataSource *ds = g_new0( AddressDataSource, 1 );
839
        GList *list = addrindex_read_attributes( file );
840
        ds->rawDataSource = list;
841
        return ds;
842
}
843

    
844
static void addrindex_write_jpilot( FILE *fp, AddressDataSource *ds, gint lvl ) {
845
        GList *list = ds->rawDataSource;
846
        if( list ) {
847
                addrindex_write_attributes( fp, TAG_DS_JPILOT, list, lvl );
848
        }
849
}
850
#endif
851

    
852
#ifdef USE_LDAP
853
static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
854
        AddressDataSource *ds = g_new0( AddressDataSource, 1 );
855
        SyldapServer *server;
856
        GList *attr;
857

    
858
        server = syldap_create();
859
        attr = xml_get_current_tag_attr( file );
860
        while( attr ) {
861
                gchar *name = ((XMLAttr *)attr->data)->name;
862
                gchar *value = ((XMLAttr *)attr->data)->value;
863
                gint ivalue = atoi( value );
864
                if( strcmp( name, ATTAG_LDAP_NAME ) == 0 ) {
865
                        syldap_set_name( server, value );
866
                }
867
                else if( strcmp( name, ATTAG_LDAP_HOST ) == 0 ) {
868
                        syldap_set_host( server, value );
869
                }
870
                else if( strcmp( name, ATTAG_LDAP_PORT ) == 0 ) {
871
                        syldap_set_port( server, ivalue );
872
                }
873
                else if( strcmp( name, ATTAG_LDAP_BASE_DN ) == 0 ) {
874
                        syldap_set_base_dn( server, value );
875
                }
876
                else if( strcmp( name, ATTAG_LDAP_BIND_DN ) == 0 ) {
877
                        syldap_set_bind_dn( server, value );
878
                }
879
                else if( strcmp( name, ATTAG_LDAP_BIND_PASS ) == 0 ) {
880
                        syldap_set_bind_password( server, value );
881
                }
882
                else if( strcmp( name, ATTAG_LDAP_CRITERIA ) == 0 ) {
883
                        syldap_set_search_criteria( server, value );
884
                }
885
                else if( strcmp( name, ATTAG_LDAP_MAX_ENTRY ) == 0 ) {
886
                        syldap_set_max_entries( server, ivalue );
887
                }
888
                else if( strcmp( name, ATTAG_LDAP_TIMEOUT ) == 0 ) {
889
                        syldap_set_timeout( server, ivalue );
890
                }
891
                attr = g_list_next( attr );
892
        }
893

    
894
        ds->rawDataSource = server;
895
        return ds;
896
}
897

    
898
static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
899
        SyldapServer *server = ds->rawDataSource;
900
        if( server ) {
901
                gchar value[256];
902

    
903
                addrindex_write_elem_s( fp, lvl, TAG_DS_LDAP );
904
                addrindex_write_attr( fp, ATTAG_LDAP_NAME, server->name );
905
                addrindex_write_attr( fp, ATTAG_LDAP_HOST, server->hostName );
906

    
907
                sprintf( value, "%d", server->port );        
908
                addrindex_write_attr( fp, ATTAG_LDAP_PORT, value );
909

    
910
                addrindex_write_attr( fp, ATTAG_LDAP_BASE_DN, server->baseDN );
911
                addrindex_write_attr( fp, ATTAG_LDAP_BIND_DN, server->bindDN );
912
                addrindex_write_attr( fp, ATTAG_LDAP_BIND_PASS, server->bindPass );
913
                addrindex_write_attr( fp, ATTAG_LDAP_CRITERIA, server->searchCriteria );
914

    
915
                sprintf( value, "%d", server->maxEntries );
916
                addrindex_write_attr( fp, ATTAG_LDAP_MAX_ENTRY, value );
917
                sprintf( value, "%d", server->timeOut );
918
                addrindex_write_attr( fp, ATTAG_LDAP_TIMEOUT, value );
919

    
920
                fputs(" />\n", fp);
921
        }
922
}
923
#else
924
/* Just read/write name-value pairs */
925
static AddressDataSource *addrindex_parse_ldap( XMLFile *file ) {
926
        AddressDataSource *ds = g_new0( AddressDataSource, 1 );
927
        GList *list = addrindex_read_attributes( file );
928
        ds->rawDataSource = list;
929
        return ds;
930
}
931

    
932
static void addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
933
        GList *list = ds->rawDataSource;
934
        if( list ) {
935
                addrindex_write_attributes( fp, TAG_DS_LDAP, list, lvl );
936
        }
937
}
938
#endif
939

    
940
/* **********************************************************************
941
* Address index I/O functions.
942
* ***********************************************************************
943
*/
944
static void addrindex_read_index( AddressIndex *addrIndex, XMLFile *file ) {
945
        guint prev_level;
946
        /* gchar *element; */
947
        /* GList *attr; */
948
        XMLTag *xtag;
949
        AddressInterface *iface = NULL, *dsIFace = NULL;
950
        AddressDataSource *ds;
951

    
952
        for (;;) {
953
                prev_level = file->level;
954
                xml_parse_next_tag( file );
955
                if( file->level < prev_level ) return;
956

    
957
                xtag = xml_get_current_tag( file );
958
                /* printf( "tag : %s\n", xtag->tag ); */
959

    
960
                iface = addrindex_tag_get_interface( addrIndex, xtag->tag, ADDR_IF_NONE );
961
                if( iface ) {
962
                        addrIndex->lastType = iface->type;
963
                        if( iface->legacyFlag ) addrIndex->needsConversion = TRUE;
964
                        /* printf( "found : %s\n", iface->name ); */
965
                }
966
                else {
967
                        dsIFace = addrindex_tag_get_datasource( addrIndex, addrIndex->lastType, xtag->tag );
968
                        if( dsIFace ) {
969
                                /* Add data source to list */
970
                                /* printf( "\tdata source: %s\n", dsIFace->name ); */
971
                                ds = NULL;
972
                                if( addrIndex->lastType == ADDR_IF_BOOK ) {
973
                                        ds = addrindex_parse_book( file );
974
                                        if( ds->rawDataSource ) {
975
                                                addrbook_set_path( ds->rawDataSource, addrIndex->filePath );
976
                                                /* addrbook_print_book( ds->rawDataSource, stdout ); */
977
                                        }
978
                                }
979
                                else if( addrIndex->lastType == ADDR_IF_VCARD ) {
980
                                        ds = addrindex_parse_vcard( file );
981
                                        /* if( ds->rawDataSource ) { */
982
                                        /*        vcard_print_file( ds->rawDataSource, stdout ); */
983
                                        /* } */
984
                                }
985
                                else if( addrIndex->lastType == ADDR_IF_JPILOT ) {
986
                                        ds = addrindex_parse_jpilot( file );
987
                                        /*
988
                                        if( ds->rawDataSource ) {
989
                                                jpilot_print_file( ds->rawDataSource, stdout );
990
                                                // addrindex_print_attributes( ds->rawDataSource, stdout );
991
                                        }
992
                                        */
993
                                }
994
                                else if( addrIndex->lastType == ADDR_IF_LDAP ) {
995
                                        ds = addrindex_parse_ldap( file );
996
                                        /*
997
                                        if( ds->rawDataSource ) {
998
                                                syldap_print_data( ds->rawDataSource, stdout );
999
                                                // addrindex_print_attributes( ds->rawDataSource, stdout );
1000
                                        }
1001
                                        */
1002
                                }
1003
                                if( ds ) {
1004
                                        ds->type = addrIndex->lastType;
1005
                                        ds->interface = dsIFace;
1006
                                        dsIFace->listSource = g_list_append( dsIFace->listSource, ds );
1007
                                }
1008
                                /* printf( "=============================\n\n" ); */
1009
                        }
1010
                }
1011
                /*
1012
                element = xml_get_element( file );
1013
                attr = xml_get_current_tag_attr( file );
1014
                if( _interfaceLast_ && ! _interfaceLast_->legacyFlag ) {
1015
                        show_attribs( attr );
1016
                        printf( "\ttag  value : %s :\n", element );
1017
                }
1018
                */
1019
                addrindex_read_index( addrIndex, file );
1020
        }
1021
}
1022

    
1023
static gint addrindex_read_file( AddressIndex *addrIndex ) {
1024
        XMLFile *file = NULL;
1025
        gchar *fileSpec = NULL;
1026

    
1027
        g_return_val_if_fail( addrIndex != NULL, -1 );
1028

    
1029
        fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1030
        addrIndex->retVal = MGU_NO_FILE;
1031
        file = xml_open_file( fileSpec );
1032
        g_free( fileSpec );
1033

    
1034
        if( file == NULL ) {
1035
                /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
1036
                return addrIndex->retVal;
1037
        }
1038

    
1039
        addrIndex->retVal = MGU_BAD_FORMAT;
1040
        if( xml_get_dtd( file ) == 0 ) {
1041
                if( xml_parse_next_tag( file ) == 0 ) {
1042
                        if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1043
                                addrindex_read_index( addrIndex, file );
1044
                                addrIndex->retVal = MGU_SUCCESS;
1045
                        }
1046
                }
1047
        }
1048
        xml_close_file( file );
1049

    
1050
        return addrIndex->retVal;
1051
}
1052

    
1053
static void addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
1054
        GList *nodeIF, *nodeDS;
1055
        gint lvlList = 1;
1056
        gint lvlItem = 1 + lvlList;
1057

    
1058
        nodeIF = addrIndex->interfaceList;
1059
        while( nodeIF ) {
1060
                AddressInterface *iface = nodeIF->data;
1061
                if( ! iface->legacyFlag ) {
1062
                        nodeDS = iface->listSource;
1063
                        addrindex_write_elem_s( fp, lvlList, iface->listTag );
1064
                        fputs( ">\n", fp );
1065
                        while( nodeDS ) {
1066
                                AddressDataSource *ds = nodeDS->data;
1067
                                if( ds ) {
1068
                                        if( iface->type == ADDR_IF_BOOK ) {
1069
                                                addrindex_write_book( fp, ds, lvlItem );
1070
                                        }
1071
                                        if( iface->type == ADDR_IF_VCARD ) {
1072
                                                addrindex_write_vcard( fp, ds, lvlItem );
1073
                                        }
1074
                                        if( iface->type == ADDR_IF_JPILOT ) {
1075
                                                addrindex_write_jpilot( fp, ds, lvlItem );
1076
                                        }
1077
                                        if( iface->type == ADDR_IF_LDAP ) {
1078
                                                addrindex_write_ldap( fp, ds, lvlItem );
1079
                                        }
1080
                                }
1081
                                nodeDS = g_list_next( nodeDS );
1082
                        }
1083
                        addrindex_write_elem_e( fp, lvlList, iface->listTag );
1084
                }
1085
                nodeIF = g_list_next( nodeIF );
1086
        }
1087
}
1088

    
1089
/*
1090
* Write data to specified file.
1091
* Enter: addrIndex Address index object.
1092
*        newFile   New file name.
1093
* return: Status code, from addrIndex->retVal.
1094
* Note: File will be created in directory specified by addrIndex.
1095
*/
1096
gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile ) {
1097
        FILE *fp;
1098
        gchar *fileSpec;
1099
#ifndef DEV_STANDALONE
1100
        PrefFile *pfile;
1101
#endif
1102

    
1103
        g_return_val_if_fail( addrIndex != NULL, -1 );
1104

    
1105
        fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
1106
        addrIndex->retVal = MGU_OPEN_FILE;
1107
#ifdef DEV_STANDALONE
1108
        fp = fopen( fileSpec, "wb" );
1109
        g_free( fileSpec );
1110
        if( fp ) {
1111
                fputs( "<?xml version=\"1.0\" ?>\n", fp );
1112
#else
1113
        pfile = prefs_file_open( fileSpec );
1114
        g_free( fileSpec );
1115
        if( pfile ) {
1116
                fp = pfile->fp;
1117
                fprintf( fp, "<?xml version=\"1.0\" encoding=\"%s\" ?>\n",
1118
                                conv_get_internal_charset_str() );
1119
#endif
1120
                addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
1121
                fputs( ">\n", fp );
1122

    
1123
                addrindex_write_index( addrIndex, fp );
1124
                addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
1125

    
1126
                addrIndex->retVal = MGU_SUCCESS;
1127
#ifdef DEV_STANDALONE
1128
                fclose( fp );
1129
#else
1130
                if( prefs_file_close( pfile ) < 0 ) {
1131
                        addrIndex->retVal = MGU_ERROR_WRITE;
1132
                }
1133
#endif
1134
        }
1135

    
1136
        fileSpec = NULL;
1137
        return addrIndex->retVal;
1138
}
1139

    
1140
/*
1141
* Save address index data to original file.
1142
* return: Status code, from addrIndex->retVal.
1143
*/
1144
gint addrindex_save_data( AddressIndex *addrIndex ) {
1145
        g_return_val_if_fail( addrIndex != NULL, -1 );
1146

    
1147
        addrIndex->retVal = MGU_NO_FILE;
1148
        if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal;
1149
        if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal;
1150

    
1151
        addrindex_write_to( addrIndex, addrIndex->fileName );
1152
        if( addrIndex->retVal == MGU_SUCCESS ) {
1153
                addrIndex->dirtyFlag = FALSE;
1154
        }
1155
        return addrIndex->retVal;
1156
}
1157

    
1158
/*
1159
* Save all address book files which may have changed.
1160
* Return: Status code, set if there was a problem saving data.
1161
*/
1162
gint addrindex_save_all_books( AddressIndex *addrIndex ) {
1163
        gint retVal = MGU_SUCCESS;
1164
        GList *nodeIf, *nodeDS;
1165

    
1166
        nodeIf = addrIndex->interfaceList;
1167
        while( nodeIf ) {
1168
                AddressInterface *iface = nodeIf->data;
1169
                if( iface->type == ADDR_IF_BOOK ) {
1170
                        nodeDS = iface->listSource;
1171
                        while( nodeDS ) {
1172
                                AddressDataSource *ds = nodeDS->data;
1173
                                AddressBookFile *abf = ds->rawDataSource;
1174
                                if( abf->dirtyFlag ) {
1175
                                        if( abf->readFlag ) {
1176
                                                addrbook_save_data( abf );
1177
                                                if( abf->retVal != MGU_SUCCESS ) {
1178
                                                        retVal = abf->retVal;
1179
                                                }
1180
                                        }
1181
                                }
1182
                                nodeDS = g_list_next( nodeDS );
1183
                        }
1184
                        break;
1185
                }
1186
                nodeIf = g_list_next( nodeIf );
1187
        }
1188
        return retVal;
1189
}
1190

    
1191

    
1192
/* **********************************************************************
1193
* Address book conversion to new format.
1194
* ***********************************************************************
1195
*/
1196

    
1197
#define ELTAG_IF_OLD_FOLDER   "folder"
1198
#define ELTAG_IF_OLD_GROUP    "group"
1199
#define ELTAG_IF_OLD_ITEM     "item"
1200
#define ELTAG_IF_OLD_NAME     "name"
1201
#define ELTAG_IF_OLD_ADDRESS  "address"
1202
#define ELTAG_IF_OLD_REMARKS  "remarks"
1203
#define ATTAG_IF_OLD_NAME     "name"
1204

    
1205
#define TEMPNODE_ROOT         0
1206
#define TEMPNODE_FOLDER       1
1207
#define TEMPNODE_GROUP        2
1208
#define TEMPNODE_ADDRESS      3
1209

    
1210
typedef struct _AddressCvt_Node AddressCvtNode;
1211
struct _AddressCvt_Node {
1212
        gint  type;
1213
        gchar *name;
1214
        gchar *address;
1215
        gchar *remarks;
1216
        GList *list;
1217
};
1218

    
1219
/*
1220
* Parse current address item.
1221
*/
1222
static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
1223
        gchar *element;
1224
        guint level;
1225
        AddressCvtNode *nn;
1226

    
1227
        nn = g_new0( AddressCvtNode, 1 );
1228
        nn->type = TEMPNODE_ADDRESS;
1229
        nn->list = NULL;
1230

    
1231
        level = file->level;
1232

    
1233
        for (;;) {
1234
                xml_parse_next_tag(file);
1235
                if (file->level < level) return nn;
1236

    
1237
                element = xml_get_element( file );
1238
                if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
1239
                        nn->name = g_strdup( element );
1240
                }
1241
                if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
1242
                        nn->address = g_strdup( element );
1243
                }
1244
                if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
1245
                        nn->remarks = g_strdup( element );
1246
                }
1247
                xml_parse_next_tag(file);
1248
        }
1249
}
1250

    
1251
/*
1252
* Create a temporary node below specified node.
1253
*/
1254
static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) {
1255
        AddressCvtNode *nn;
1256
        nn = g_new0( AddressCvtNode, 1 );
1257
        nn->type = type;
1258
        nn->name = g_strdup( name );
1259
        nn->remarks = g_strdup( rem );
1260
        node->list = g_list_append( node->list, nn );
1261
        return nn;
1262
}
1263

    
1264
/*
1265
* Process current temporary node.
1266
*/
1267
static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) {
1268
        GList *attr;
1269
        guint prev_level;
1270
        AddressCvtNode *newNode = NULL;
1271
        gchar *name;
1272
        gchar *value;
1273

    
1274
        for (;;) {
1275
                prev_level = file->level;
1276
                xml_parse_next_tag( file );
1277
                if (file->level < prev_level) return;
1278
                name = NULL;
1279
                value = NULL;
1280

    
1281
                if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
1282
                        attr = xml_get_current_tag_attr(file);
1283
                        if (attr) {
1284
                                name = ((XMLAttr *)attr->data)->name;
1285
                                if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1286
                                        value = ((XMLAttr *)attr->data)->value;
1287
                                }
1288
                        }
1289
                        newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" );
1290
                        addrindex_add_obj( file, newNode );
1291

    
1292
                }
1293
                else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) {
1294
                        attr = xml_get_current_tag_attr(file);
1295
                        if (attr) {
1296
                                name = ((XMLAttr *)attr->data)->name;
1297
                                if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) {
1298
                                        value = ((XMLAttr *)attr->data)->value;
1299
                                }
1300
                        }
1301
                        newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" );
1302
                        addrindex_add_obj( file, newNode );
1303
                }
1304
                else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) {
1305
                        newNode = addrindex_parse_item( file );
1306
                        node->list = g_list_append( node->list, newNode );
1307
                }
1308
                else {
1309
                        /* printf( "invalid: !!! \n" ); */
1310
                        attr = xml_get_current_tag_attr( file );
1311
                }
1312
        }
1313
}
1314

    
1315
/*
1316
* Consume all nodes below current tag.
1317
*/
1318
static void addrindex_consume_tree( XMLFile *file ) {
1319
        guint prev_level;
1320
        gchar *element;
1321
        GList *attr;
1322
        XMLTag *xtag;
1323

    
1324
        for (;;) {
1325
                prev_level = file->level;
1326
                xml_parse_next_tag( file );
1327
                if (file->level < prev_level) return;
1328

    
1329
                xtag = xml_get_current_tag( file );
1330
                /* printf( "tag : %s\n", xtag->tag ); */
1331
                element = xml_get_element( file );
1332
                attr = xml_get_current_tag_attr( file );
1333
                /* show_attribs( attr ); */
1334
                /* printf( "\ttag  value : %s :\n", element ); */
1335
                addrindex_consume_tree( file );
1336
        }
1337
}
1338

    
1339
/*
1340
* Print temporary tree.
1341
*/
1342
static void addrindex_print_node( AddressCvtNode *node, FILE *stream  ) {
1343
        GList *list;
1344

    
1345
        fprintf( stream, "Node:\ttype :%d:\n", node->type );
1346
        fprintf( stream, "\tname :%s:\n", node->name );
1347
        fprintf( stream, "\taddr :%s:\n", node->address );
1348
        fprintf( stream, "\trems :%s:\n", node->remarks );
1349
        if( node->list ) {
1350
                fprintf( stream, "\t--list----\n" );
1351
        }
1352
        list = node->list;
1353
        while( list ) {
1354
                AddressCvtNode *lNode = list->data;
1355
                list = g_list_next( list );
1356
                addrindex_print_node( lNode, stream );
1357
        }
1358
        fprintf( stream, "\t==list-%d==\n", node->type );
1359
}
1360

    
1361
/*
1362
* Free up temporary tree.
1363
*/
1364
static void addrindex_free_node( AddressCvtNode *node ) {
1365
        GList *list = node->list;
1366

    
1367
        while( list ) {
1368
                AddressCvtNode *lNode = list->data;
1369
                list = g_list_next( list );
1370
                addrindex_free_node( lNode );
1371
        }
1372
        node->type = TEMPNODE_ROOT;
1373
        g_free( node->name );
1374
        g_free( node->address );
1375
        g_free( node->remarks );
1376
        g_list_free( node->list );
1377
        g_free( node );
1378
}
1379

    
1380
/*
1381
* Process address book for specified node.
1382
*/
1383
static void addrindex_process_node(
1384
                AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent,
1385
                ItemGroup *parentGrp, ItemFolder *folderGrp )
1386
{
1387
        GList *list;
1388
        ItemFolder *itemFolder = NULL;
1389
        ItemGroup *itemGParent = parentGrp;
1390
        ItemFolder *itemGFolder = folderGrp;
1391
        AddressCache *cache = abf->addressCache;
1392

    
1393
        if( node->type == TEMPNODE_ROOT ) {
1394
                itemFolder = parent;
1395
        }
1396
        else if( node->type == TEMPNODE_FOLDER ) {
1397
                itemFolder = addritem_create_item_folder();
1398
                addritem_folder_set_name( itemFolder, node->name );
1399
                addrcache_id_folder( cache, itemFolder );
1400
                addrcache_folder_add_folder( cache, parent, itemFolder );
1401
                itemGFolder = NULL;
1402
        }
1403
        else if( node->type == TEMPNODE_GROUP ) {
1404
                ItemGroup *itemGroup;
1405
                gchar *fName;
1406

    
1407
                /* Create a folder for group */
1408
                fName = g_strdup_printf( "Cvt - %s", node->name );
1409
                itemGFolder = addritem_create_item_folder();
1410
                addritem_folder_set_name( itemGFolder, fName );
1411
                addrcache_id_folder( cache, itemGFolder );
1412
                addrcache_folder_add_folder( cache, parent, itemGFolder );
1413
                g_free( fName );
1414

    
1415
                /* Add group into folder */
1416
                itemGroup = addritem_create_item_group();
1417
                addritem_group_set_name( itemGroup, node->name );
1418
                addrcache_id_group( cache, itemGroup );
1419
                addrcache_folder_add_group( cache, itemGFolder, itemGroup );
1420
                itemGParent = itemGroup;
1421
        }
1422
        else if( node->type == TEMPNODE_ADDRESS ) {
1423
                ItemPerson *itemPerson;
1424
                ItemEMail *itemEMail;
1425

    
1426
                /* Create person and email objects */
1427
                itemPerson = addritem_create_item_person();
1428
                addritem_person_set_common_name( itemPerson, node->name );
1429
                addrcache_id_person( cache, itemPerson );
1430
                itemEMail = addritem_create_item_email();
1431
                addritem_email_set_address( itemEMail, node->address );
1432
                addritem_email_set_remarks( itemEMail, node->remarks );
1433
                addrcache_id_email( cache, itemEMail );
1434
                addrcache_person_add_email( cache, itemPerson, itemEMail );
1435

    
1436
                /* Add person into appropriate folder */
1437
                if( itemGFolder ) {
1438
                        addrcache_folder_add_person( cache, itemGFolder, itemPerson );
1439
                }
1440
                else {
1441
                        addrcache_folder_add_person( cache, parent, itemPerson );
1442
                }
1443

    
1444
                /* Add email address only into group */
1445
                if( parentGrp ) {
1446
                        addrcache_group_add_email( cache, parentGrp, itemEMail );
1447
                }
1448
        }
1449

    
1450
        list = node->list;
1451
        while( list ) {
1452
                AddressCvtNode *lNode = list->data;
1453
                list = g_list_next( list );
1454
                addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder );
1455
        }
1456
}
1457

    
1458
/*
1459
* Process address book to specified file number.
1460
*/
1461
static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
1462
        gboolean retVal = FALSE;
1463
        AddressBookFile *abf = NULL;
1464
        AddressCvtNode *rootNode = NULL;
1465
        gchar *newFile = NULL;
1466
        GList *fileList = NULL;
1467
        gint fileNum  = 0;
1468

    
1469
        /* Setup root node */
1470
        rootNode = g_new0( AddressCvtNode, 1 );
1471
        rootNode->type = TEMPNODE_ROOT;
1472
        rootNode->name = g_strdup( "root" );
1473
        rootNode->list = NULL;
1474
        addrindex_add_obj( file, rootNode );
1475
        /* addrindex_print_node( rootNode, stdout ); */
1476

    
1477
        /* Create new address book */
1478
        abf = addrbook_create_book();
1479
        addrbook_set_name( abf, displayName );
1480
        addrbook_set_path( abf, addrIndex->filePath );
1481

    
1482
        /* Determine next available file number */
1483
        fileList = addrbook_get_bookfile_list( abf );
1484
        if( fileList ) {
1485
                fileNum = 1 + abf->maxValue;
1486
        }
1487
        g_list_free( fileList );
1488
        fileList = NULL;
1489

    
1490
        newFile = addrbook_gen_new_file_name( fileNum );
1491
        if( newFile ) {
1492
                addrbook_set_file( abf, newFile );
1493
        }
1494

    
1495
        addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL );
1496

    
1497
        /* addrbook_dump_book( abf, stdout ); */
1498
        addrbook_save_data( abf );
1499
        addrIndex->retVal = abf->retVal;
1500
        if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1501

    
1502
        addrbook_free_book( abf );
1503
        abf = NULL;
1504
        addrindex_free_node( rootNode );
1505
        rootNode = NULL;
1506

    
1507
        /* Create entries in address index */
1508
        if( retVal ) {
1509
                abf = addrbook_create_book();
1510
                addrbook_set_name( abf, displayName );
1511
                addrbook_set_path( abf, addrIndex->filePath );
1512
                addrbook_set_file( abf, newFile );
1513
                addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1514
        }
1515

    
1516
        return retVal;
1517
}
1518

    
1519
/*
1520
* Process tree converting data.
1521
*/
1522
static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) {
1523
        guint prev_level;
1524
        gchar *element;
1525
        GList *attr;
1526
        XMLTag *xtag;
1527

    
1528
        /* Process file */
1529
        for (;;) {
1530
                prev_level = file->level;
1531
                xml_parse_next_tag( file );
1532
                if (file->level < prev_level) return;
1533

    
1534
                xtag = xml_get_current_tag( file );
1535
                /* printf( "tag : %d : %s\n", prev_level, xtag->tag ); */
1536
                if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) {
1537
                        if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
1538
                                addrIndex->needsConversion = FALSE;
1539
                                addrIndex->wasConverted = TRUE;
1540
                                continue;
1541
                        }
1542
                        return;
1543
                }
1544
                if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) {
1545
                        if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
1546
                                addrIndex->needsConversion = FALSE;
1547
                                addrIndex->wasConverted = TRUE;
1548
                                continue;
1549
                        }
1550
                        return;
1551
                }
1552
                element = xml_get_element( file );
1553
                attr = xml_get_current_tag_attr( file );
1554
                /* show_attribs( attr ); */
1555
                /* printf( "\ttag  value : %s :\n", element ); */
1556
                addrindex_consume_tree( file );
1557
        }
1558
}
1559

    
1560
static gint addrindex_convert_data( AddressIndex *addrIndex ) {
1561
        XMLFile *file = NULL;
1562
        gchar *fileSpec;
1563

    
1564
        fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
1565
        addrIndex->retVal = MGU_NO_FILE;
1566
        file = xml_open_file( fileSpec );
1567
        g_free( fileSpec );
1568

    
1569
        if( file == NULL ) {
1570
                /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
1571
                return addrIndex->retVal;
1572
        }
1573

    
1574
        addrIndex->retVal = MGU_BAD_FORMAT;
1575
        if( xml_get_dtd( file ) == 0 ) {
1576
                if( xml_parse_next_tag( file ) == 0 ) {
1577
                        if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
1578
                                addrindex_convert_tree( addrIndex, file );
1579
                        }
1580
                }
1581
        }
1582
        xml_close_file( file );
1583
        return addrIndex->retVal;
1584
}
1585

    
1586
/*
1587
* Create a new address book file.
1588
*/
1589
static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
1590
        gboolean retVal = FALSE;
1591
        AddressBookFile *abf = NULL;
1592
        gchar *newFile = NULL;
1593
        GList *fileList = NULL;
1594
        gint fileNum = 0;
1595

    
1596
        /* Create new address book */
1597
        abf = addrbook_create_book();
1598
        addrbook_set_name( abf, displayName );
1599
        addrbook_set_path( abf, addrIndex->filePath );
1600

    
1601
        /* Determine next available file number */
1602
        fileList = addrbook_get_bookfile_list( abf );
1603
        if( fileList ) {
1604
                fileNum = 1 + abf->maxValue;
1605
        }
1606
        g_list_free( fileList );
1607
        fileList = NULL;
1608

    
1609
        newFile = addrbook_gen_new_file_name( fileNum );
1610
        if( newFile ) {
1611
                addrbook_set_file( abf, newFile );
1612
        }
1613

    
1614
        addrbook_save_data( abf );
1615
        addrIndex->retVal = abf->retVal;
1616
        if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
1617
        addrbook_free_book( abf );
1618
        abf = NULL;
1619

    
1620
        /* Create entries in address index */
1621
        if( retVal ) {
1622
                abf = addrbook_create_book();
1623
                addrbook_set_name( abf, displayName );
1624
                addrbook_set_path( abf, addrIndex->filePath );
1625
                addrbook_set_file( abf, newFile );
1626
                addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
1627
        }
1628

    
1629
        return retVal;
1630
}
1631

    
1632
/*
1633
* Read data for address index performing a conversion if necesary.
1634
* Enter: addrIndex Address index object.
1635
* return: Status code, from addrIndex->retVal.
1636
* Note: New address book files will be created in directory specified by
1637
* addrIndex. Three files will be created, for the following:
1638
*        "Common addresses"
1639
*        "Personal addresses"
1640
*        "Gathered addresses" - a new address book.
1641
*/
1642
gint addrindex_read_data( AddressIndex *addrIndex ) {
1643
        g_return_val_if_fail( addrIndex != NULL, -1 );
1644

    
1645
        addrIndex->conversionError = FALSE;
1646
        addrindex_read_file( addrIndex );
1647
        if( addrIndex->retVal == MGU_SUCCESS ) {
1648
                if( addrIndex->needsConversion ) {
1649
                        if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
1650
                                addrIndex->conversionError = TRUE;
1651
                        }
1652
                        else {
1653
                                addrIndex->conversionError = TRUE;
1654
                        }
1655
                }
1656
                addrIndex->dirtyFlag = TRUE;
1657
        }
1658
        return addrIndex->retVal;
1659
}
1660

    
1661
/*
1662
* Create new address books for a new address index.
1663
* Enter: addrIndex Address index object.
1664
* return: Status code, from addrIndex->retVal.
1665
* Note: New address book files will be created in directory specified by
1666
* addrIndex. Three files will be created, for the following:
1667
*        "Common addresses"
1668
*        "Personal addresses"
1669
*        "Gathered addresses" - a new address book.
1670
*/
1671
gint addrindex_create_new_books( AddressIndex *addrIndex ) {
1672
        gboolean flg;
1673

    
1674
        g_return_val_if_fail( addrIndex != NULL, -1 );
1675

    
1676
        flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON );
1677
        if( flg ) {
1678
                flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL );
1679
                addrIndex->dirtyFlag = TRUE;
1680
        }
1681
        return addrIndex->retVal;
1682
}
1683

    
1684
/* **********************************************************************
1685
* New interface stuff.
1686
* ***********************************************************************
1687
*/
1688

    
1689
/*
1690
 * Return modified flag for specified data source.
1691
 */
1692
gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
1693
        gboolean retVal = FALSE;
1694
        AddressInterface *iface;
1695

    
1696
        if( ds == NULL ) return retVal;
1697
        iface = ds->interface;
1698
        if( iface == NULL ) return retVal;
1699
        if( iface->getModifyFlag ) {
1700
                retVal = ( iface->getModifyFlag ) ( ds->rawDataSource );
1701
        }
1702
        return retVal;
1703
}
1704

    
1705
/*
1706
 * Return accessed flag for specified data source.
1707
 */
1708
gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
1709
        gboolean retVal = FALSE;
1710
        AddressInterface *iface;
1711

    
1712
        if( ds == NULL ) return retVal;
1713
        iface = ds->interface;
1714
        if( iface == NULL ) return retVal;
1715
        if( iface->getAccessFlag ) {
1716
                retVal = ( iface->getAccessFlag ) ( ds->rawDataSource );
1717
        }
1718
        return retVal;
1719
}
1720

    
1721
/*
1722
 * Return data read flag for specified data source.
1723
 */
1724
gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
1725
        gboolean retVal = TRUE;
1726
        AddressInterface *iface;
1727

    
1728
        if( ds == NULL ) return retVal;
1729
        iface = ds->interface;
1730
        if( iface == NULL ) return retVal;
1731
        if( iface->getReadFlag ) {
1732
                retVal = ( iface->getReadFlag ) ( ds->rawDataSource );
1733
        }
1734
        return retVal;
1735
}
1736

    
1737
/*
1738
 * Return status code for specified data source.
1739
 */
1740
gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
1741
        gint retVal = MGU_SUCCESS;
1742
        AddressInterface *iface;
1743

    
1744
        if( ds == NULL ) return retVal;
1745
        iface = ds->interface;
1746
        if( iface == NULL ) return retVal;
1747
        if( iface->getStatusCode ) {
1748
                retVal = ( iface->getStatusCode ) ( ds->rawDataSource );
1749
        }
1750
        return retVal;
1751
}
1752

    
1753
/*
1754
 * Return data read flag for specified data source.
1755
 */
1756
gint addrindex_ds_read_data( AddressDataSource *ds ) {
1757
        gint retVal = MGU_SUCCESS;
1758
        AddressInterface *iface;
1759

    
1760
        if( ds == NULL ) return retVal;
1761
        iface = ds->interface;
1762
        if( iface == NULL ) return retVal;
1763
        if( iface->getReadData ) {
1764
                retVal = ( iface->getReadData ) ( ds->rawDataSource );
1765
        }
1766
        return retVal;
1767
}
1768

    
1769
/*
1770
 * Return data read flag for specified data source.
1771
 */
1772
ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
1773
        ItemFolder *retVal = NULL;
1774
        AddressInterface *iface;
1775

    
1776
        if( ds == NULL ) return retVal;
1777
        iface = ds->interface;
1778
        if( iface == NULL ) return retVal;
1779
        if( iface->getRootFolder ) {
1780
                retVal = ( iface->getRootFolder ) ( ds->rawDataSource );
1781
        }
1782
        return retVal;
1783
}
1784

    
1785
/*
1786
 * Return list of folders for specified data source.
1787
 */
1788
GList *addrindex_ds_get_list_folder( AddressDataSource *ds ) {
1789
        GList *retVal = FALSE;
1790
        AddressInterface *iface;
1791

    
1792
        if( ds == NULL ) return retVal;
1793
        iface = ds->interface;
1794
        if( iface == NULL ) return retVal;
1795
        if( iface->getListFolder ) {
1796
                retVal = ( iface->getListFolder ) ( ds->rawDataSource );
1797
        }
1798
        return retVal;
1799
}
1800

    
1801
/*
1802
 * Return list of persons in root folder for specified data source.
1803
 */
1804
GList *addrindex_ds_get_list_person( AddressDataSource *ds ) {
1805
        GList *retVal = FALSE;
1806
        AddressInterface *iface;
1807

    
1808
        if( ds == NULL ) return retVal;
1809
        iface = ds->interface;
1810
        if( iface == NULL ) return retVal;
1811
        if( iface->getListPerson ) {
1812
                retVal = ( iface->getListPerson ) ( ds->rawDataSource );
1813
        }
1814
        return retVal;
1815
}
1816

    
1817
/*
1818
 * Return name for specified data source.
1819
 */
1820
gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
1821
        gchar *retVal = FALSE;
1822
        AddressInterface *iface;
1823

    
1824
        if( ds == NULL ) return retVal;
1825
        iface = ds->interface;
1826
        if( iface == NULL ) return retVal;
1827
        if( iface->getName ) {
1828
                retVal = ( iface->getName ) ( ds->rawDataSource );
1829
        }
1830
        return retVal;
1831
}
1832

    
1833
/*
1834
 * Set the access flag inside the data source.
1835
 */
1836
void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
1837
        AddressInterface *iface;
1838

    
1839
        if( ds == NULL ) return;
1840
        iface = ds->interface;
1841
        if( iface == NULL ) return;
1842
        if( iface->setAccessFlag ) {
1843
                ( iface->setAccessFlag ) ( ds->rawDataSource, value );
1844
        }
1845
}
1846

    
1847
/*
1848
 * Return read only flag for specified data source.
1849
 */
1850
gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
1851
        AddressInterface *iface;
1852
        if( ds == NULL ) return TRUE;
1853
        iface = ds->interface;
1854
        if( iface == NULL ) return TRUE;
1855
        return iface->readOnly;
1856
}
1857

    
1858
/*
1859
 * Return list of all persons for specified data source.
1860
 */
1861
GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
1862
        GList *retVal = NULL;
1863
        AddressInterface *iface;
1864

    
1865
        if( ds == NULL ) return retVal;
1866
        iface = ds->interface;
1867
        if( iface == NULL ) return retVal;
1868
        if( iface->getAllPersons ) {
1869
                retVal = ( iface->getAllPersons ) ( ds->rawDataSource );
1870
        }
1871
        return retVal;
1872
}
1873

    
1874
/*
1875
 * Return list of all groups for specified data source.
1876
 */
1877
GList *addrindex_ds_get_all_groups( AddressDataSource *ds ) {
1878
        GList *retVal = NULL;
1879
        AddressInterface *iface;
1880

    
1881
        if( ds == NULL ) return retVal;
1882
        iface = ds->interface;
1883
        if( iface == NULL ) return retVal;
1884
        if( iface->getAllGroups ) {
1885
                retVal = ( iface->getAllGroups ) ( ds->rawDataSource );
1886
        }
1887
        return retVal;
1888
}
1889

    
1890
/*
1891
* End of Source.
1892
*/