root / src / addrindex.c @ 41
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", CS_INTERNAL );
|
| 1118 | #endif
|
| 1119 | addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX );
|
| 1120 | fputs( ">\n", fp );
|
| 1121 | |
| 1122 | addrindex_write_index( addrIndex, fp ); |
| 1123 | addrindex_write_elem_e( fp, 0, TAG_ADDRESS_INDEX );
|
| 1124 | |
| 1125 | addrIndex->retVal = MGU_SUCCESS; |
| 1126 | #ifdef DEV_STANDALONE
|
| 1127 | fclose( fp ); |
| 1128 | #else
|
| 1129 | if( prefs_file_close( pfile ) < 0 ) { |
| 1130 | addrIndex->retVal = MGU_ERROR_WRITE; |
| 1131 | } |
| 1132 | #endif
|
| 1133 | } |
| 1134 | |
| 1135 | fileSpec = NULL;
|
| 1136 | return addrIndex->retVal;
|
| 1137 | } |
| 1138 | |
| 1139 | /*
|
| 1140 | * Save address index data to original file. |
| 1141 | * return: Status code, from addrIndex->retVal. |
| 1142 | */ |
| 1143 | gint addrindex_save_data( AddressIndex *addrIndex ) {
|
| 1144 | g_return_val_if_fail( addrIndex != NULL, -1 ); |
| 1145 | |
| 1146 | addrIndex->retVal = MGU_NO_FILE; |
| 1147 | if( addrIndex->fileName == NULL || *addrIndex->fileName == '\0' ) return addrIndex->retVal; |
| 1148 | if( addrIndex->filePath == NULL || *addrIndex->filePath == '\0' ) return addrIndex->retVal; |
| 1149 | |
| 1150 | addrindex_write_to( addrIndex, addrIndex->fileName ); |
| 1151 | if( addrIndex->retVal == MGU_SUCCESS ) {
|
| 1152 | addrIndex->dirtyFlag = FALSE; |
| 1153 | } |
| 1154 | return addrIndex->retVal;
|
| 1155 | } |
| 1156 | |
| 1157 | /*
|
| 1158 | * Save all address book files which may have changed. |
| 1159 | * Return: Status code, set if there was a problem saving data. |
| 1160 | */ |
| 1161 | gint addrindex_save_all_books( AddressIndex *addrIndex ) {
|
| 1162 | gint retVal = MGU_SUCCESS; |
| 1163 | GList *nodeIf, *nodeDS; |
| 1164 | |
| 1165 | nodeIf = addrIndex->interfaceList; |
| 1166 | while( nodeIf ) {
|
| 1167 | AddressInterface *iface = nodeIf->data; |
| 1168 | if( iface->type == ADDR_IF_BOOK ) {
|
| 1169 | nodeDS = iface->listSource; |
| 1170 | while( nodeDS ) {
|
| 1171 | AddressDataSource *ds = nodeDS->data; |
| 1172 | AddressBookFile *abf = ds->rawDataSource; |
| 1173 | if( abf->dirtyFlag ) {
|
| 1174 | if( abf->readFlag ) {
|
| 1175 | addrbook_save_data( abf ); |
| 1176 | if( abf->retVal != MGU_SUCCESS ) {
|
| 1177 | retVal = abf->retVal; |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | nodeDS = g_list_next( nodeDS ); |
| 1182 | } |
| 1183 | break;
|
| 1184 | } |
| 1185 | nodeIf = g_list_next( nodeIf ); |
| 1186 | } |
| 1187 | return retVal;
|
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | /* **********************************************************************
|
| 1192 | * Address book conversion to new format. |
| 1193 | * *********************************************************************** |
| 1194 | */ |
| 1195 | |
| 1196 | #define ELTAG_IF_OLD_FOLDER "folder" |
| 1197 | #define ELTAG_IF_OLD_GROUP "group" |
| 1198 | #define ELTAG_IF_OLD_ITEM "item" |
| 1199 | #define ELTAG_IF_OLD_NAME "name" |
| 1200 | #define ELTAG_IF_OLD_ADDRESS "address" |
| 1201 | #define ELTAG_IF_OLD_REMARKS "remarks" |
| 1202 | #define ATTAG_IF_OLD_NAME "name" |
| 1203 | |
| 1204 | #define TEMPNODE_ROOT 0 |
| 1205 | #define TEMPNODE_FOLDER 1 |
| 1206 | #define TEMPNODE_GROUP 2 |
| 1207 | #define TEMPNODE_ADDRESS 3 |
| 1208 | |
| 1209 | typedef struct _AddressCvt_Node AddressCvtNode; |
| 1210 | struct _AddressCvt_Node {
|
| 1211 | gint type; |
| 1212 | gchar *name; |
| 1213 | gchar *address; |
| 1214 | gchar *remarks; |
| 1215 | GList *list; |
| 1216 | }; |
| 1217 | |
| 1218 | /*
|
| 1219 | * Parse current address item. |
| 1220 | */ |
| 1221 | static AddressCvtNode *addrindex_parse_item( XMLFile *file ) {
|
| 1222 | gchar *element; |
| 1223 | guint level; |
| 1224 | AddressCvtNode *nn; |
| 1225 | |
| 1226 | nn = g_new0( AddressCvtNode, 1 );
|
| 1227 | nn->type = TEMPNODE_ADDRESS; |
| 1228 | nn->list = NULL;
|
| 1229 | |
| 1230 | level = file->level; |
| 1231 | |
| 1232 | for (;;) {
|
| 1233 | xml_parse_next_tag(file); |
| 1234 | if (file->level < level) return nn; |
| 1235 | |
| 1236 | element = xml_get_element( file ); |
| 1237 | if( xml_compare_tag( file, ELTAG_IF_OLD_NAME ) ) {
|
| 1238 | nn->name = g_strdup( element ); |
| 1239 | } |
| 1240 | if( xml_compare_tag( file, ELTAG_IF_OLD_ADDRESS ) ) {
|
| 1241 | nn->address = g_strdup( element ); |
| 1242 | } |
| 1243 | if( xml_compare_tag( file, ELTAG_IF_OLD_REMARKS ) ) {
|
| 1244 | nn->remarks = g_strdup( element ); |
| 1245 | } |
| 1246 | xml_parse_next_tag(file); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /*
|
| 1251 | * Create a temporary node below specified node. |
| 1252 | */ |
| 1253 | static AddressCvtNode *addrindex_add_object( AddressCvtNode *node, gint type, gchar *name, gchar *addr, char *rem ) { |
| 1254 | AddressCvtNode *nn; |
| 1255 | nn = g_new0( AddressCvtNode, 1 );
|
| 1256 | nn->type = type; |
| 1257 | nn->name = g_strdup( name ); |
| 1258 | nn->remarks = g_strdup( rem ); |
| 1259 | node->list = g_list_append( node->list, nn ); |
| 1260 | return nn;
|
| 1261 | } |
| 1262 | |
| 1263 | /*
|
| 1264 | * Process current temporary node. |
| 1265 | */ |
| 1266 | static void addrindex_add_obj( XMLFile *file, AddressCvtNode *node ) { |
| 1267 | GList *attr; |
| 1268 | guint prev_level; |
| 1269 | AddressCvtNode *newNode = NULL;
|
| 1270 | gchar *name; |
| 1271 | gchar *value; |
| 1272 | |
| 1273 | for (;;) {
|
| 1274 | prev_level = file->level; |
| 1275 | xml_parse_next_tag( file ); |
| 1276 | if (file->level < prev_level) return; |
| 1277 | name = NULL;
|
| 1278 | value = NULL;
|
| 1279 | |
| 1280 | if( xml_compare_tag( file, ELTAG_IF_OLD_GROUP ) ) {
|
| 1281 | attr = xml_get_current_tag_attr(file); |
| 1282 | if (attr) {
|
| 1283 | name = ((XMLAttr *)attr->data)->name; |
| 1284 | if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) { |
| 1285 | value = ((XMLAttr *)attr->data)->value; |
| 1286 | } |
| 1287 | } |
| 1288 | newNode = addrindex_add_object( node, TEMPNODE_GROUP, value, "", "" ); |
| 1289 | addrindex_add_obj( file, newNode ); |
| 1290 | |
| 1291 | } |
| 1292 | else if( xml_compare_tag( file, ELTAG_IF_OLD_FOLDER ) ) { |
| 1293 | attr = xml_get_current_tag_attr(file); |
| 1294 | if (attr) {
|
| 1295 | name = ((XMLAttr *)attr->data)->name; |
| 1296 | if( strcmp( name, ATTAG_IF_OLD_NAME ) == 0 ) { |
| 1297 | value = ((XMLAttr *)attr->data)->value; |
| 1298 | } |
| 1299 | } |
| 1300 | newNode = addrindex_add_object( node, TEMPNODE_FOLDER, value, "", "" ); |
| 1301 | addrindex_add_obj( file, newNode ); |
| 1302 | } |
| 1303 | else if( xml_compare_tag( file, ELTAG_IF_OLD_ITEM ) ) { |
| 1304 | newNode = addrindex_parse_item( file ); |
| 1305 | node->list = g_list_append( node->list, newNode ); |
| 1306 | } |
| 1307 | else {
|
| 1308 | /* printf( "invalid: !!! \n" ); */
|
| 1309 | attr = xml_get_current_tag_attr( file ); |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /*
|
| 1315 | * Consume all nodes below current tag. |
| 1316 | */ |
| 1317 | static void addrindex_consume_tree( XMLFile *file ) { |
| 1318 | guint prev_level; |
| 1319 | gchar *element; |
| 1320 | GList *attr; |
| 1321 | XMLTag *xtag; |
| 1322 | |
| 1323 | for (;;) {
|
| 1324 | prev_level = file->level; |
| 1325 | xml_parse_next_tag( file ); |
| 1326 | if (file->level < prev_level) return; |
| 1327 | |
| 1328 | xtag = xml_get_current_tag( file ); |
| 1329 | /* printf( "tag : %s\n", xtag->tag ); */
|
| 1330 | element = xml_get_element( file ); |
| 1331 | attr = xml_get_current_tag_attr( file ); |
| 1332 | /* show_attribs( attr ); */
|
| 1333 | /* printf( "\ttag value : %s :\n", element ); */
|
| 1334 | addrindex_consume_tree( file ); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /*
|
| 1339 | * Print temporary tree. |
| 1340 | */ |
| 1341 | static void addrindex_print_node( AddressCvtNode *node, FILE *stream ) { |
| 1342 | GList *list; |
| 1343 | |
| 1344 | fprintf( stream, "Node:\ttype :%d:\n", node->type );
|
| 1345 | fprintf( stream, "\tname :%s:\n", node->name );
|
| 1346 | fprintf( stream, "\taddr :%s:\n", node->address );
|
| 1347 | fprintf( stream, "\trems :%s:\n", node->remarks );
|
| 1348 | if( node->list ) {
|
| 1349 | fprintf( stream, "\t--list----\n" );
|
| 1350 | } |
| 1351 | list = node->list; |
| 1352 | while( list ) {
|
| 1353 | AddressCvtNode *lNode = list->data; |
| 1354 | list = g_list_next( list ); |
| 1355 | addrindex_print_node( lNode, stream ); |
| 1356 | } |
| 1357 | fprintf( stream, "\t==list-%d==\n", node->type );
|
| 1358 | } |
| 1359 | |
| 1360 | /*
|
| 1361 | * Free up temporary tree. |
| 1362 | */ |
| 1363 | static void addrindex_free_node( AddressCvtNode *node ) { |
| 1364 | GList *list = node->list; |
| 1365 | |
| 1366 | while( list ) {
|
| 1367 | AddressCvtNode *lNode = list->data; |
| 1368 | list = g_list_next( list ); |
| 1369 | addrindex_free_node( lNode ); |
| 1370 | } |
| 1371 | node->type = TEMPNODE_ROOT; |
| 1372 | g_free( node->name ); |
| 1373 | g_free( node->address ); |
| 1374 | g_free( node->remarks ); |
| 1375 | g_list_free( node->list ); |
| 1376 | g_free( node ); |
| 1377 | } |
| 1378 | |
| 1379 | /*
|
| 1380 | * Process address book for specified node. |
| 1381 | */ |
| 1382 | static void addrindex_process_node( |
| 1383 | AddressBookFile *abf, AddressCvtNode *node, ItemFolder *parent, |
| 1384 | ItemGroup *parentGrp, ItemFolder *folderGrp ) |
| 1385 | {
|
| 1386 | GList *list; |
| 1387 | ItemFolder *itemFolder = NULL;
|
| 1388 | ItemGroup *itemGParent = parentGrp; |
| 1389 | ItemFolder *itemGFolder = folderGrp; |
| 1390 | AddressCache *cache = abf->addressCache; |
| 1391 | |
| 1392 | if( node->type == TEMPNODE_ROOT ) {
|
| 1393 | itemFolder = parent; |
| 1394 | } |
| 1395 | else if( node->type == TEMPNODE_FOLDER ) { |
| 1396 | itemFolder = addritem_create_item_folder(); |
| 1397 | addritem_folder_set_name( itemFolder, node->name ); |
| 1398 | addrcache_id_folder( cache, itemFolder ); |
| 1399 | addrcache_folder_add_folder( cache, parent, itemFolder ); |
| 1400 | itemGFolder = NULL;
|
| 1401 | } |
| 1402 | else if( node->type == TEMPNODE_GROUP ) { |
| 1403 | ItemGroup *itemGroup; |
| 1404 | gchar *fName; |
| 1405 | |
| 1406 | /* Create a folder for group */
|
| 1407 | fName = g_strdup_printf( "Cvt - %s", node->name );
|
| 1408 | itemGFolder = addritem_create_item_folder(); |
| 1409 | addritem_folder_set_name( itemGFolder, fName ); |
| 1410 | addrcache_id_folder( cache, itemGFolder ); |
| 1411 | addrcache_folder_add_folder( cache, parent, itemGFolder ); |
| 1412 | g_free( fName ); |
| 1413 | |
| 1414 | /* Add group into folder */
|
| 1415 | itemGroup = addritem_create_item_group(); |
| 1416 | addritem_group_set_name( itemGroup, node->name ); |
| 1417 | addrcache_id_group( cache, itemGroup ); |
| 1418 | addrcache_folder_add_group( cache, itemGFolder, itemGroup ); |
| 1419 | itemGParent = itemGroup; |
| 1420 | } |
| 1421 | else if( node->type == TEMPNODE_ADDRESS ) { |
| 1422 | ItemPerson *itemPerson; |
| 1423 | ItemEMail *itemEMail; |
| 1424 | |
| 1425 | /* Create person and email objects */
|
| 1426 | itemPerson = addritem_create_item_person(); |
| 1427 | addritem_person_set_common_name( itemPerson, node->name ); |
| 1428 | addrcache_id_person( cache, itemPerson ); |
| 1429 | itemEMail = addritem_create_item_email(); |
| 1430 | addritem_email_set_address( itemEMail, node->address ); |
| 1431 | addritem_email_set_remarks( itemEMail, node->remarks ); |
| 1432 | addrcache_id_email( cache, itemEMail ); |
| 1433 | addrcache_person_add_email( cache, itemPerson, itemEMail ); |
| 1434 | |
| 1435 | /* Add person into appropriate folder */
|
| 1436 | if( itemGFolder ) {
|
| 1437 | addrcache_folder_add_person( cache, itemGFolder, itemPerson ); |
| 1438 | } |
| 1439 | else {
|
| 1440 | addrcache_folder_add_person( cache, parent, itemPerson ); |
| 1441 | } |
| 1442 | |
| 1443 | /* Add email address only into group */
|
| 1444 | if( parentGrp ) {
|
| 1445 | addrcache_group_add_email( cache, parentGrp, itemEMail ); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | list = node->list; |
| 1450 | while( list ) {
|
| 1451 | AddressCvtNode *lNode = list->data; |
| 1452 | list = g_list_next( list ); |
| 1453 | addrindex_process_node( abf, lNode, itemFolder, itemGParent, itemGFolder ); |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /*
|
| 1458 | * Process address book to specified file number. |
| 1459 | */ |
| 1460 | static gboolean addrindex_process_book( AddressIndex *addrIndex, XMLFile *file, gchar *displayName ) {
|
| 1461 | gboolean retVal = FALSE; |
| 1462 | AddressBookFile *abf = NULL;
|
| 1463 | AddressCvtNode *rootNode = NULL;
|
| 1464 | gchar *newFile = NULL;
|
| 1465 | GList *fileList = NULL;
|
| 1466 | gint fileNum = 0;
|
| 1467 | |
| 1468 | /* Setup root node */
|
| 1469 | rootNode = g_new0( AddressCvtNode, 1 );
|
| 1470 | rootNode->type = TEMPNODE_ROOT; |
| 1471 | rootNode->name = g_strdup( "root" );
|
| 1472 | rootNode->list = NULL;
|
| 1473 | addrindex_add_obj( file, rootNode ); |
| 1474 | /* addrindex_print_node( rootNode, stdout ); */
|
| 1475 | |
| 1476 | /* Create new address book */
|
| 1477 | abf = addrbook_create_book(); |
| 1478 | addrbook_set_name( abf, displayName ); |
| 1479 | addrbook_set_path( abf, addrIndex->filePath ); |
| 1480 | |
| 1481 | /* Determine next available file number */
|
| 1482 | fileList = addrbook_get_bookfile_list( abf ); |
| 1483 | if( fileList ) {
|
| 1484 | fileNum = 1 + abf->maxValue;
|
| 1485 | } |
| 1486 | g_list_free( fileList ); |
| 1487 | fileList = NULL;
|
| 1488 | |
| 1489 | newFile = addrbook_gen_new_file_name( fileNum ); |
| 1490 | if( newFile ) {
|
| 1491 | addrbook_set_file( abf, newFile ); |
| 1492 | } |
| 1493 | |
| 1494 | addrindex_process_node( abf, rootNode, abf->addressCache->rootFolder, NULL, NULL ); |
| 1495 | |
| 1496 | /* addrbook_dump_book( abf, stdout ); */
|
| 1497 | addrbook_save_data( abf ); |
| 1498 | addrIndex->retVal = abf->retVal; |
| 1499 | if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
|
| 1500 | |
| 1501 | addrbook_free_book( abf ); |
| 1502 | abf = NULL;
|
| 1503 | addrindex_free_node( rootNode ); |
| 1504 | rootNode = NULL;
|
| 1505 | |
| 1506 | /* Create entries in address index */
|
| 1507 | if( retVal ) {
|
| 1508 | abf = addrbook_create_book(); |
| 1509 | addrbook_set_name( abf, displayName ); |
| 1510 | addrbook_set_path( abf, addrIndex->filePath ); |
| 1511 | addrbook_set_file( abf, newFile ); |
| 1512 | addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf ); |
| 1513 | } |
| 1514 | |
| 1515 | return retVal;
|
| 1516 | } |
| 1517 | |
| 1518 | /*
|
| 1519 | * Process tree converting data. |
| 1520 | */ |
| 1521 | static void addrindex_convert_tree( AddressIndex *addrIndex, XMLFile *file ) { |
| 1522 | guint prev_level; |
| 1523 | gchar *element; |
| 1524 | GList *attr; |
| 1525 | XMLTag *xtag; |
| 1526 | |
| 1527 | /* Process file */
|
| 1528 | for (;;) {
|
| 1529 | prev_level = file->level; |
| 1530 | xml_parse_next_tag( file ); |
| 1531 | if (file->level < prev_level) return; |
| 1532 | |
| 1533 | xtag = xml_get_current_tag( file ); |
| 1534 | /* printf( "tag : %d : %s\n", prev_level, xtag->tag ); */
|
| 1535 | if( strcmp( xtag->tag, TAG_IF_OLD_COMMON ) == 0 ) { |
| 1536 | if( addrindex_process_book( addrIndex, file, DISP_OLD_COMMON ) ) {
|
| 1537 | addrIndex->needsConversion = FALSE; |
| 1538 | addrIndex->wasConverted = TRUE; |
| 1539 | continue;
|
| 1540 | } |
| 1541 | return;
|
| 1542 | } |
| 1543 | if( strcmp( xtag->tag, TAG_IF_OLD_PERSONAL ) == 0 ) { |
| 1544 | if( addrindex_process_book( addrIndex, file, DISP_OLD_PERSONAL ) ) {
|
| 1545 | addrIndex->needsConversion = FALSE; |
| 1546 | addrIndex->wasConverted = TRUE; |
| 1547 | continue;
|
| 1548 | } |
| 1549 | return;
|
| 1550 | } |
| 1551 | element = xml_get_element( file ); |
| 1552 | attr = xml_get_current_tag_attr( file ); |
| 1553 | /* show_attribs( attr ); */
|
| 1554 | /* printf( "\ttag value : %s :\n", element ); */
|
| 1555 | addrindex_consume_tree( file ); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | static gint addrindex_convert_data( AddressIndex *addrIndex ) {
|
| 1560 | XMLFile *file = NULL;
|
| 1561 | gchar *fileSpec; |
| 1562 | |
| 1563 | fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, addrIndex->fileName, NULL );
|
| 1564 | addrIndex->retVal = MGU_NO_FILE; |
| 1565 | file = xml_open_file( fileSpec ); |
| 1566 | g_free( fileSpec ); |
| 1567 | |
| 1568 | if( file == NULL ) { |
| 1569 | /* fprintf( stdout, " file '%s' does not exist.\n", addrIndex->fileName ); */
|
| 1570 | return addrIndex->retVal;
|
| 1571 | } |
| 1572 | |
| 1573 | addrIndex->retVal = MGU_BAD_FORMAT; |
| 1574 | if( xml_get_dtd( file ) == 0 ) { |
| 1575 | if( xml_parse_next_tag( file ) == 0 ) { |
| 1576 | if( xml_compare_tag( file, TAG_ADDRESS_INDEX ) ) {
|
| 1577 | addrindex_convert_tree( addrIndex, file ); |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | xml_close_file( file ); |
| 1582 | return addrIndex->retVal;
|
| 1583 | } |
| 1584 | |
| 1585 | /*
|
| 1586 | * Create a new address book file. |
| 1587 | */ |
| 1588 | static gboolean addrindex_create_new_book( AddressIndex *addrIndex, gchar *displayName ) {
|
| 1589 | gboolean retVal = FALSE; |
| 1590 | AddressBookFile *abf = NULL;
|
| 1591 | gchar *newFile = NULL;
|
| 1592 | GList *fileList = NULL;
|
| 1593 | gint fileNum = 0;
|
| 1594 | |
| 1595 | /* Create new address book */
|
| 1596 | abf = addrbook_create_book(); |
| 1597 | addrbook_set_name( abf, displayName ); |
| 1598 | addrbook_set_path( abf, addrIndex->filePath ); |
| 1599 | |
| 1600 | /* Determine next available file number */
|
| 1601 | fileList = addrbook_get_bookfile_list( abf ); |
| 1602 | if( fileList ) {
|
| 1603 | fileNum = 1 + abf->maxValue;
|
| 1604 | } |
| 1605 | g_list_free( fileList ); |
| 1606 | fileList = NULL;
|
| 1607 | |
| 1608 | newFile = addrbook_gen_new_file_name( fileNum ); |
| 1609 | if( newFile ) {
|
| 1610 | addrbook_set_file( abf, newFile ); |
| 1611 | } |
| 1612 | |
| 1613 | addrbook_save_data( abf ); |
| 1614 | addrIndex->retVal = abf->retVal; |
| 1615 | if( abf->retVal == MGU_SUCCESS ) retVal = TRUE;
|
| 1616 | addrbook_free_book( abf ); |
| 1617 | abf = NULL;
|
| 1618 | |
| 1619 | /* Create entries in address index */
|
| 1620 | if( retVal ) {
|
| 1621 | abf = addrbook_create_book(); |
| 1622 | addrbook_set_name( abf, displayName ); |
| 1623 | addrbook_set_path( abf, addrIndex->filePath ); |
| 1624 | addrbook_set_file( abf, newFile ); |
| 1625 | addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf ); |
| 1626 | } |
| 1627 | |
| 1628 | return retVal;
|
| 1629 | } |
| 1630 | |
| 1631 | /*
|
| 1632 | * Read data for address index performing a conversion if necesary. |
| 1633 | * Enter: addrIndex Address index object. |
| 1634 | * return: Status code, from addrIndex->retVal. |
| 1635 | * Note: New address book files will be created in directory specified by |
| 1636 | * addrIndex. Three files will be created, for the following: |
| 1637 | * "Common addresses" |
| 1638 | * "Personal addresses" |
| 1639 | * "Gathered addresses" - a new address book. |
| 1640 | */ |
| 1641 | gint addrindex_read_data( AddressIndex *addrIndex ) {
|
| 1642 | g_return_val_if_fail( addrIndex != NULL, -1 ); |
| 1643 | |
| 1644 | addrIndex->conversionError = FALSE; |
| 1645 | addrindex_read_file( addrIndex ); |
| 1646 | if( addrIndex->retVal == MGU_SUCCESS ) {
|
| 1647 | if( addrIndex->needsConversion ) {
|
| 1648 | if( addrindex_convert_data( addrIndex ) == MGU_SUCCESS ) {
|
| 1649 | addrIndex->conversionError = TRUE; |
| 1650 | } |
| 1651 | else {
|
| 1652 | addrIndex->conversionError = TRUE; |
| 1653 | } |
| 1654 | } |
| 1655 | addrIndex->dirtyFlag = TRUE; |
| 1656 | } |
| 1657 | return addrIndex->retVal;
|
| 1658 | } |
| 1659 | |
| 1660 | /*
|
| 1661 | * Create new address books for a new address index. |
| 1662 | * Enter: addrIndex Address index object. |
| 1663 | * return: Status code, from addrIndex->retVal. |
| 1664 | * Note: New address book files will be created in directory specified by |
| 1665 | * addrIndex. Three files will be created, for the following: |
| 1666 | * "Common addresses" |
| 1667 | * "Personal addresses" |
| 1668 | * "Gathered addresses" - a new address book. |
| 1669 | */ |
| 1670 | gint addrindex_create_new_books( AddressIndex *addrIndex ) {
|
| 1671 | gboolean flg; |
| 1672 | |
| 1673 | g_return_val_if_fail( addrIndex != NULL, -1 ); |
| 1674 | |
| 1675 | flg = addrindex_create_new_book( addrIndex, DISP_NEW_COMMON ); |
| 1676 | if( flg ) {
|
| 1677 | flg = addrindex_create_new_book( addrIndex, DISP_NEW_PERSONAL ); |
| 1678 | addrIndex->dirtyFlag = TRUE; |
| 1679 | } |
| 1680 | return addrIndex->retVal;
|
| 1681 | } |
| 1682 | |
| 1683 | /* **********************************************************************
|
| 1684 | * New interface stuff. |
| 1685 | * *********************************************************************** |
| 1686 | */ |
| 1687 | |
| 1688 | /*
|
| 1689 | * Return modified flag for specified data source. |
| 1690 | */ |
| 1691 | gboolean addrindex_ds_get_modify_flag( AddressDataSource *ds ) {
|
| 1692 | gboolean retVal = FALSE; |
| 1693 | AddressInterface *iface; |
| 1694 | |
| 1695 | if( ds == NULL ) return retVal; |
| 1696 | iface = ds->interface; |
| 1697 | if( iface == NULL ) return retVal; |
| 1698 | if( iface->getModifyFlag ) {
|
| 1699 | retVal = ( iface->getModifyFlag ) ( ds->rawDataSource ); |
| 1700 | } |
| 1701 | return retVal;
|
| 1702 | } |
| 1703 | |
| 1704 | /*
|
| 1705 | * Return accessed flag for specified data source. |
| 1706 | */ |
| 1707 | gboolean addrindex_ds_get_access_flag( AddressDataSource *ds ) {
|
| 1708 | gboolean retVal = FALSE; |
| 1709 | AddressInterface *iface; |
| 1710 | |
| 1711 | if( ds == NULL ) return retVal; |
| 1712 | iface = ds->interface; |
| 1713 | if( iface == NULL ) return retVal; |
| 1714 | if( iface->getAccessFlag ) {
|
| 1715 | retVal = ( iface->getAccessFlag ) ( ds->rawDataSource ); |
| 1716 | } |
| 1717 | return retVal;
|
| 1718 | } |
| 1719 | |
| 1720 | /*
|
| 1721 | * Return data read flag for specified data source. |
| 1722 | */ |
| 1723 | gboolean addrindex_ds_get_read_flag( AddressDataSource *ds ) {
|
| 1724 | gboolean retVal = TRUE; |
| 1725 | AddressInterface *iface; |
| 1726 | |
| 1727 | if( ds == NULL ) return retVal; |
| 1728 | iface = ds->interface; |
| 1729 | if( iface == NULL ) return retVal; |
| 1730 | if( iface->getReadFlag ) {
|
| 1731 | retVal = ( iface->getReadFlag ) ( ds->rawDataSource ); |
| 1732 | } |
| 1733 | return retVal;
|
| 1734 | } |
| 1735 | |
| 1736 | /*
|
| 1737 | * Return status code for specified data source. |
| 1738 | */ |
| 1739 | gint addrindex_ds_get_status_code( AddressDataSource *ds ) {
|
| 1740 | gint retVal = MGU_SUCCESS; |
| 1741 | AddressInterface *iface; |
| 1742 | |
| 1743 | if( ds == NULL ) return retVal; |
| 1744 | iface = ds->interface; |
| 1745 | if( iface == NULL ) return retVal; |
| 1746 | if( iface->getStatusCode ) {
|
| 1747 | retVal = ( iface->getStatusCode ) ( ds->rawDataSource ); |
| 1748 | } |
| 1749 | return retVal;
|
| 1750 | } |
| 1751 | |
| 1752 | /*
|
| 1753 | * Return data read flag for specified data source. |
| 1754 | */ |
| 1755 | gint addrindex_ds_read_data( AddressDataSource *ds ) {
|
| 1756 | gint retVal = MGU_SUCCESS; |
| 1757 | AddressInterface *iface; |
| 1758 | |
| 1759 | if( ds == NULL ) return retVal; |
| 1760 | iface = ds->interface; |
| 1761 | if( iface == NULL ) return retVal; |
| 1762 | if( iface->getReadData ) {
|
| 1763 | retVal = ( iface->getReadData ) ( ds->rawDataSource ); |
| 1764 | } |
| 1765 | return retVal;
|
| 1766 | } |
| 1767 | |
| 1768 | /*
|
| 1769 | * Return data read flag for specified data source. |
| 1770 | */ |
| 1771 | ItemFolder *addrindex_ds_get_root_folder( AddressDataSource *ds ) {
|
| 1772 | ItemFolder *retVal = NULL;
|
| 1773 | AddressInterface *iface; |
| 1774 | |
| 1775 | if( ds == NULL ) return retVal; |
| 1776 | iface = ds->interface; |
| 1777 | if( iface == NULL ) return retVal; |
| 1778 | if( iface->getRootFolder ) {
|
| 1779 | retVal = ( iface->getRootFolder ) ( ds->rawDataSource ); |
| 1780 | } |
| 1781 | return retVal;
|
| 1782 | } |
| 1783 | |
| 1784 | /*
|
| 1785 | * Return list of folders for specified data source. |
| 1786 | */ |
| 1787 | GList *addrindex_ds_get_list_folder( AddressDataSource *ds ) {
|
| 1788 | GList *retVal = FALSE; |
| 1789 | AddressInterface *iface; |
| 1790 | |
| 1791 | if( ds == NULL ) return retVal; |
| 1792 | iface = ds->interface; |
| 1793 | if( iface == NULL ) return retVal; |
| 1794 | if( iface->getListFolder ) {
|
| 1795 | retVal = ( iface->getListFolder ) ( ds->rawDataSource ); |
| 1796 | } |
| 1797 | return retVal;
|
| 1798 | } |
| 1799 | |
| 1800 | /*
|
| 1801 | * Return list of persons in root folder for specified data source. |
| 1802 | */ |
| 1803 | GList *addrindex_ds_get_list_person( AddressDataSource *ds ) {
|
| 1804 | GList *retVal = FALSE; |
| 1805 | AddressInterface *iface; |
| 1806 | |
| 1807 | if( ds == NULL ) return retVal; |
| 1808 | iface = ds->interface; |
| 1809 | if( iface == NULL ) return retVal; |
| 1810 | if( iface->getListPerson ) {
|
| 1811 | retVal = ( iface->getListPerson ) ( ds->rawDataSource ); |
| 1812 | } |
| 1813 | return retVal;
|
| 1814 | } |
| 1815 | |
| 1816 | /*
|
| 1817 | * Return name for specified data source. |
| 1818 | */ |
| 1819 | gchar *addrindex_ds_get_name( AddressDataSource *ds ) {
|
| 1820 | gchar *retVal = FALSE; |
| 1821 | AddressInterface *iface; |
| 1822 | |
| 1823 | if( ds == NULL ) return retVal; |
| 1824 | iface = ds->interface; |
| 1825 | if( iface == NULL ) return retVal; |
| 1826 | if( iface->getName ) {
|
| 1827 | retVal = ( iface->getName ) ( ds->rawDataSource ); |
| 1828 | } |
| 1829 | return retVal;
|
| 1830 | } |
| 1831 | |
| 1832 | /*
|
| 1833 | * Set the access flag inside the data source. |
| 1834 | */ |
| 1835 | void addrindex_ds_set_access_flag( AddressDataSource *ds, gboolean *value ) {
|
| 1836 | AddressInterface *iface; |
| 1837 | |
| 1838 | if( ds == NULL ) return; |
| 1839 | iface = ds->interface; |
| 1840 | if( iface == NULL ) return; |
| 1841 | if( iface->setAccessFlag ) {
|
| 1842 | ( iface->setAccessFlag ) ( ds->rawDataSource, value ); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | /*
|
| 1847 | * Return read only flag for specified data source. |
| 1848 | */ |
| 1849 | gboolean addrindex_ds_get_readonly( AddressDataSource *ds ) {
|
| 1850 | AddressInterface *iface; |
| 1851 | if( ds == NULL ) return TRUE; |
| 1852 | iface = ds->interface; |
| 1853 | if( iface == NULL ) return TRUE; |
| 1854 | return iface->readOnly;
|
| 1855 | } |
| 1856 | |
| 1857 | /*
|
| 1858 | * Return list of all persons for specified data source. |
| 1859 | */ |
| 1860 | GList *addrindex_ds_get_all_persons( AddressDataSource *ds ) {
|
| 1861 | GList *retVal = NULL;
|
| 1862 | AddressInterface *iface; |
| 1863 | |
| 1864 | if( ds == NULL ) return retVal; |
| 1865 | iface = ds->interface; |
| 1866 | if( iface == NULL ) return retVal; |
| 1867 | if( iface->getAllPersons ) {
|
| 1868 | retVal = ( iface->getAllPersons ) ( ds->rawDataSource ); |
| 1869 | } |
| 1870 | return retVal;
|
| 1871 | } |
| 1872 | |
| 1873 | /*
|
| 1874 | * Return list of all groups for specified data source. |
| 1875 | */ |
| 1876 | GList *addrindex_ds_get_all_groups( AddressDataSource *ds ) {
|
| 1877 | GList *retVal = NULL;
|
| 1878 | AddressInterface *iface; |
| 1879 | |
| 1880 | if( ds == NULL ) return retVal; |
| 1881 | iface = ds->interface; |
| 1882 | if( iface == NULL ) return retVal; |
| 1883 | if( iface->getAllGroups ) {
|
| 1884 | retVal = ( iface->getAllGroups ) ( ds->rawDataSource ); |
| 1885 | } |
| 1886 | return retVal;
|
| 1887 | } |
| 1888 | |
| 1889 | /*
|
| 1890 | * End of Source. |
| 1891 | */ |