root / src / vcard.c @ 1
History | View | Annotate | Download (20.3 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 | * Functions necessary to access vCard files. vCard files are used |
| 22 | * by GnomeCard for addressbook, and Netscape for sending business |
| 23 | * card information. Refer to RFC2426 for more information. |
| 24 | */ |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "mgutils.h" |
| 31 | #include "vcard.h" |
| 32 | #include "addritem.h" |
| 33 | #include "addrcache.h" |
| 34 | |
| 35 | #define GNOMECARD_DIR ".gnome" |
| 36 | #define GNOMECARD_FILE "GnomeCard" |
| 37 | #define GNOMECARD_SECTION "[file]" |
| 38 | #define GNOMECARD_PARAM "open" |
| 39 | |
| 40 | #define VCARD_TEST_LINES 200 |
| 41 | |
| 42 | /*
|
| 43 | * Create new cardfile object. |
| 44 | */ |
| 45 | VCardFile *vcard_create() {
|
| 46 | VCardFile *cardFile; |
| 47 | cardFile = g_new0( VCardFile, 1 );
|
| 48 | cardFile->name = NULL;
|
| 49 | cardFile->path = NULL;
|
| 50 | cardFile->file = NULL;
|
| 51 | cardFile->bufptr = cardFile->buffer; |
| 52 | cardFile->addressCache = addrcache_create(); |
| 53 | cardFile->retVal = MGU_SUCCESS; |
| 54 | cardFile->accessFlag = FALSE; |
| 55 | return cardFile;
|
| 56 | } |
| 57 | |
| 58 | /*
|
| 59 | * Properties... |
| 60 | */ |
| 61 | void vcard_set_name( VCardFile* cardFile, const gchar *value ) { |
| 62 | g_return_if_fail( cardFile != NULL );
|
| 63 | cardFile->name = mgu_replace_string( cardFile->name, value ); |
| 64 | g_strstrip( cardFile->name ); |
| 65 | } |
| 66 | void vcard_set_file( VCardFile* cardFile, const gchar *value ) { |
| 67 | g_return_if_fail( cardFile != NULL );
|
| 68 | addrcache_refresh( cardFile->addressCache ); |
| 69 | cardFile->path = mgu_replace_string( cardFile->path, value ); |
| 70 | g_strstrip( cardFile->path ); |
| 71 | } |
| 72 | void vcard_set_accessed( VCardFile *cardFile, const gboolean value ) { |
| 73 | g_return_if_fail( cardFile != NULL );
|
| 74 | cardFile->accessFlag = value; |
| 75 | } |
| 76 | |
| 77 | /*
|
| 78 | * Test whether file was modified since last access. |
| 79 | * Return: TRUE if file was modified. |
| 80 | */ |
| 81 | gboolean vcard_get_modified( VCardFile *vcardFile ) {
|
| 82 | g_return_val_if_fail( vcardFile != NULL, FALSE );
|
| 83 | return addrcache_check_file( vcardFile->addressCache, vcardFile->path );
|
| 84 | } |
| 85 | gboolean vcard_get_accessed( VCardFile *vcardFile ) {
|
| 86 | g_return_val_if_fail( vcardFile != NULL, FALSE );
|
| 87 | return addrcache_check_file( vcardFile->addressCache, vcardFile->path );
|
| 88 | } |
| 89 | |
| 90 | /*
|
| 91 | * Test whether file was read. |
| 92 | * Return: TRUE if file was read. |
| 93 | */ |
| 94 | gboolean vcard_get_read_flag( VCardFile *vcardFile ) {
|
| 95 | g_return_val_if_fail( vcardFile != NULL, FALSE );
|
| 96 | return vcardFile->addressCache->dataRead;
|
| 97 | } |
| 98 | |
| 99 | /*
|
| 100 | * Return status code from last file operation. |
| 101 | * Return: Status code. |
| 102 | */ |
| 103 | gint vcard_get_status( VCardFile *cardFile ) {
|
| 104 | g_return_val_if_fail( cardFile != NULL, -1 ); |
| 105 | return cardFile->retVal;
|
| 106 | } |
| 107 | |
| 108 | ItemFolder *vcard_get_root_folder( VCardFile *cardFile ) {
|
| 109 | g_return_val_if_fail( cardFile != NULL, NULL ); |
| 110 | return addrcache_get_root_folder( cardFile->addressCache );
|
| 111 | } |
| 112 | gchar *vcard_get_name( VCardFile *cardFile ) {
|
| 113 | g_return_val_if_fail( cardFile != NULL, NULL ); |
| 114 | return cardFile->name;
|
| 115 | } |
| 116 | |
| 117 | /*
|
| 118 | * Refresh internal variables to force a file read. |
| 119 | */ |
| 120 | void vcard_force_refresh( VCardFile *cardFile ) {
|
| 121 | addrcache_refresh( cardFile->addressCache ); |
| 122 | } |
| 123 | |
| 124 | /*
|
| 125 | * Create new cardfile object for specified file. |
| 126 | */ |
| 127 | VCardFile *vcard_create_path( const gchar *path ) {
|
| 128 | VCardFile *cardFile; |
| 129 | cardFile = vcard_create(); |
| 130 | vcard_set_file(cardFile, path); |
| 131 | return cardFile;
|
| 132 | } |
| 133 | |
| 134 | /*
|
| 135 | * Free up cardfile object by releasing internal memory. |
| 136 | */ |
| 137 | void vcard_free( VCardFile *cardFile ) {
|
| 138 | g_return_if_fail( cardFile != NULL );
|
| 139 | |
| 140 | /* Close file */
|
| 141 | if( cardFile->file ) fclose( cardFile->file );
|
| 142 | |
| 143 | /* Free internal stuff */
|
| 144 | g_free( cardFile->name ); |
| 145 | g_free( cardFile->path ); |
| 146 | |
| 147 | /* Clear cache */
|
| 148 | addrcache_clear( cardFile->addressCache ); |
| 149 | addrcache_free( cardFile->addressCache ); |
| 150 | |
| 151 | /* Clear pointers */
|
| 152 | cardFile->file = NULL;
|
| 153 | cardFile->name = NULL;
|
| 154 | cardFile->path = NULL;
|
| 155 | cardFile->addressCache = NULL;
|
| 156 | cardFile->retVal = MGU_SUCCESS; |
| 157 | cardFile->accessFlag = FALSE; |
| 158 | |
| 159 | /* Now release file object */
|
| 160 | g_free( cardFile ); |
| 161 | |
| 162 | } |
| 163 | |
| 164 | /*
|
| 165 | * Display object to specified stream. |
| 166 | */ |
| 167 | void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
|
| 168 | g_return_if_fail( cardFile != NULL );
|
| 169 | |
| 170 | fprintf( stream, "VCardFile:\n" );
|
| 171 | fprintf( stream, " name: '%s'\n", cardFile->name );
|
| 172 | fprintf( stream, "file spec: '%s'\n", cardFile->path );
|
| 173 | fprintf( stream, " ret val: %d\n", cardFile->retVal );
|
| 174 | addrcache_print( cardFile->addressCache, stream ); |
| 175 | addritem_print_item_folder( cardFile->addressCache->rootFolder, stream ); |
| 176 | } |
| 177 | |
| 178 | /*
|
| 179 | * Open file for read. |
| 180 | * return: TRUE if file opened successfully. |
| 181 | */ |
| 182 | static gint vcard_open_file( VCardFile* cardFile ) {
|
| 183 | g_return_val_if_fail( cardFile != NULL, -1 ); |
| 184 | |
| 185 | /* fprintf( stdout, "Opening file\n" ); */
|
| 186 | cardFile->addressCache->dataRead = FALSE; |
| 187 | if( cardFile->path ) {
|
| 188 | cardFile->file = fopen( cardFile->path, "rb" );
|
| 189 | if( ! cardFile->file ) {
|
| 190 | /* fprintf( stderr, "can't open %s\n", cardFile->path ); */
|
| 191 | cardFile->retVal = MGU_OPEN_FILE; |
| 192 | return cardFile->retVal;
|
| 193 | } |
| 194 | } |
| 195 | else {
|
| 196 | /* fprintf( stderr, "file not specified\n" ); */
|
| 197 | cardFile->retVal = MGU_NO_FILE; |
| 198 | return cardFile->retVal;
|
| 199 | } |
| 200 | |
| 201 | /* Setup a buffer area */
|
| 202 | cardFile->buffer[0] = '\0'; |
| 203 | cardFile->bufptr = cardFile->buffer; |
| 204 | cardFile->retVal = MGU_SUCCESS; |
| 205 | return cardFile->retVal;
|
| 206 | } |
| 207 | |
| 208 | /*
|
| 209 | * Close file. |
| 210 | */ |
| 211 | static void vcard_close_file( VCardFile *cardFile ) { |
| 212 | g_return_if_fail( cardFile != NULL );
|
| 213 | if( cardFile->file ) fclose( cardFile->file );
|
| 214 | cardFile->file = NULL;
|
| 215 | } |
| 216 | |
| 217 | /*
|
| 218 | * Read line of text from file. |
| 219 | * Return: ptr to buffer where line starts. |
| 220 | */ |
| 221 | static gchar *vcard_read_line( VCardFile *cardFile ) {
|
| 222 | while( *cardFile->bufptr == '\n' || *cardFile->bufptr == '\0' ) { |
| 223 | if( fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL ) |
| 224 | return NULL; |
| 225 | g_strstrip( cardFile->buffer ); |
| 226 | cardFile->bufptr = cardFile->buffer; |
| 227 | } |
| 228 | return cardFile->bufptr;
|
| 229 | } |
| 230 | |
| 231 | /*
|
| 232 | * Read line of text from file. |
| 233 | * Return: ptr to buffer where line starts. |
| 234 | */ |
| 235 | static gchar *vcard_get_line( VCardFile *cardFile ) {
|
| 236 | gchar buf[ VCARDBUFSIZE ]; |
| 237 | gchar *start, *end; |
| 238 | gint len; |
| 239 | |
| 240 | if (vcard_read_line( cardFile ) == NULL ) { |
| 241 | buf[0] = '\0'; |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | /* Copy into private buffer */
|
| 246 | start = cardFile->bufptr; |
| 247 | len = strlen( start ); |
| 248 | end = start + len; |
| 249 | strncpy( buf, start, len ); |
| 250 | buf[ len ] = '\0';
|
| 251 | g_strstrip(buf); |
| 252 | cardFile->bufptr = end + 1;
|
| 253 | |
| 254 | /* Return a copy of buffer */
|
| 255 | return g_strdup( buf );
|
| 256 | } |
| 257 | |
| 258 | /*
|
| 259 | * Free linked lists of character strings. |
| 260 | */ |
| 261 | static void vcard_free_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList* listID ) { |
| 262 | mgu_free_list( listName ); |
| 263 | mgu_free_list( listAddr ); |
| 264 | mgu_free_list( listRem ); |
| 265 | mgu_free_list( listID ); |
| 266 | } |
| 267 | |
| 268 | /*
|
| 269 | * Read quoted-printable text, which may span several lines into one long string. |
| 270 | * Param: cardFile - object. |
| 271 | * Param: tagvalue - will be placed into the linked list. |
| 272 | */ |
| 273 | static gchar *vcard_read_qp( VCardFile *cardFile, gchar *tagvalue ) {
|
| 274 | GSList *listQP = NULL;
|
| 275 | gint len = 0;
|
| 276 | gchar *line = tagvalue; |
| 277 | |
| 278 | while( line ) {
|
| 279 | listQP = g_slist_append( listQP, line ); |
| 280 | len = strlen( line ) - 1;
|
| 281 | if( len > 0 ) { |
| 282 | if( line[ len ] != '=' ) break; |
| 283 | line[ len ] = '\0';
|
| 284 | } |
| 285 | line = vcard_get_line( cardFile ); |
| 286 | } |
| 287 | |
| 288 | /* Coalesce linked list into one long buffer. */
|
| 289 | line = mgu_list_coalesce( listQP ); |
| 290 | |
| 291 | /* Clean up */
|
| 292 | mgu_free_list( listQP ); |
| 293 | listQP = NULL;
|
| 294 | return line;
|
| 295 | } |
| 296 | |
| 297 | /*
|
| 298 | * Parse tag name from line buffer. |
| 299 | * Return: Buffer containing the tag name, or NULL if no delimiter char found. |
| 300 | */ |
| 301 | static gchar *vcard_get_tagname( gchar* line, gchar dlm ) {
|
| 302 | gint len = 0;
|
| 303 | gchar *tag = NULL;
|
| 304 | gchar *lptr = line; |
| 305 | |
| 306 | while( *lptr++ ) {
|
| 307 | if( *lptr == dlm ) {
|
| 308 | len = lptr - line; |
| 309 | tag = g_strndup( line, len+1 );
|
| 310 | tag[ len ] = '\0';
|
| 311 | g_strdown( tag ); |
| 312 | return tag;
|
| 313 | } |
| 314 | } |
| 315 | return tag;
|
| 316 | } |
| 317 | |
| 318 | /*
|
| 319 | * Parse tag value from line buffer. |
| 320 | * Return: Buffer containing the tag value. Empty string is returned if |
| 321 | * no delimiter char found. |
| 322 | */ |
| 323 | static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
|
| 324 | gchar *value = NULL;
|
| 325 | gchar *start = NULL;
|
| 326 | gchar *lptr; |
| 327 | gint len = 0;
|
| 328 | |
| 329 | for( lptr = line; *lptr; lptr++ ) {
|
| 330 | if( *lptr == dlm ) {
|
| 331 | if( ! start )
|
| 332 | start = lptr + 1;
|
| 333 | } |
| 334 | } |
| 335 | if( start ) {
|
| 336 | len = lptr - start; |
| 337 | value = g_strndup( start, len+1 );
|
| 338 | } |
| 339 | else {
|
| 340 | /* Ensure that we get an empty string */
|
| 341 | value = g_strndup( "", 1 ); |
| 342 | } |
| 343 | value[ len ] = '\0';
|
| 344 | return value;
|
| 345 | } |
| 346 | |
| 347 | #if 0
|
| 348 | /* |
| 349 | * Dump linked lists of character strings (for debug). |
| 350 | */ |
| 351 | static void vcard_dump_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList *listID, FILE *stream ) {
|
| 352 | fprintf( stream, "dump name\n" ); |
| 353 | fprintf( stream, "------------\n" ); |
| 354 | mgu_print_list( listName, stdout ); |
| 355 | fprintf( stream, "dump address\n" ); |
| 356 | fprintf( stream, "------------\n" ); |
| 357 | mgu_print_list( listAddr, stdout ); |
| 358 | fprintf( stream, "dump remarks\n" ); |
| 359 | fprintf( stdout, "------------\n" ); |
| 360 | mgu_print_list( listRem, stdout ); |
| 361 | fprintf( stream, "dump id\n" ); |
| 362 | fprintf( stdout, "------------\n" ); |
| 363 | mgu_print_list( listID, stdout ); |
| 364 | } |
| 365 | #endif |
| 366 | |
| 367 | /*
|
| 368 | * Build an address list entry and append to list of address items. |
| 369 | */ |
| 370 | static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *listAddr, GSList *listRem, |
| 371 | GSList *listID ) |
| 372 | {
|
| 373 | GSList *nodeName = listName; |
| 374 | GSList *nodeID = listID; |
| 375 | gchar *str; |
| 376 | while( nodeName ) {
|
| 377 | GSList *nodeAddress = listAddr; |
| 378 | GSList *nodeRemarks = listRem; |
| 379 | ItemPerson *person = addritem_create_item_person(); |
| 380 | addritem_person_set_common_name( person, nodeName->data ); |
| 381 | while( nodeAddress ) {
|
| 382 | str = nodeAddress->data; |
| 383 | if( *str != '\0' ) { |
| 384 | ItemEMail *email = addritem_create_item_email(); |
| 385 | addritem_email_set_address( email, str ); |
| 386 | str = nodeRemarks->data; |
| 387 | if( nodeRemarks ) {
|
| 388 | if( str ) {
|
| 389 | if( g_strcasecmp( str, "internet" ) != 0 ) { |
| 390 | if( *str != '\0' ) addritem_email_set_remarks( email, str ); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | addrcache_id_email( cardFile->addressCache, email ); |
| 395 | addrcache_person_add_email( cardFile->addressCache, person, email ); |
| 396 | } |
| 397 | nodeAddress = g_slist_next( nodeAddress ); |
| 398 | nodeRemarks = g_slist_next( nodeRemarks ); |
| 399 | } |
| 400 | if( person->listEMail ) {
|
| 401 | addrcache_id_person( cardFile->addressCache, person ); |
| 402 | addrcache_add_person( cardFile->addressCache, person ); |
| 403 | } |
| 404 | else {
|
| 405 | addritem_free_item_person( person ); |
| 406 | } |
| 407 | if( nodeID ) {
|
| 408 | str = nodeID->data; |
| 409 | addritem_person_set_external_id( person, str ); |
| 410 | } |
| 411 | nodeName = g_slist_next( nodeName ); |
| 412 | nodeID = g_slist_next( nodeID ); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* Unescape characters in quoted-printable string. */
|
| 417 | static void vcard_unescape_qp( gchar *value ) { |
| 418 | gchar *ptr, *src, *dest; |
| 419 | gint d, v = 0;
|
| 420 | gchar ch; |
| 421 | gboolean gotch; |
| 422 | ptr = value; |
| 423 | while( *ptr ) {
|
| 424 | gotch = FALSE; |
| 425 | if( *ptr == '=' ) { |
| 426 | v = 0;
|
| 427 | ch = *(ptr + 1);
|
| 428 | if( ch ) {
|
| 429 | if( ch > '0' && ch < '8' ) v = ch - '0'; |
| 430 | } |
| 431 | d = -1;
|
| 432 | ch = *(ptr + 2);
|
| 433 | if( ch ) {
|
| 434 | if( ch > '\x60' ) ch -= '\x20'; |
| 435 | if( ch > '0' && ch < ' ' ) d = ch - '0'; |
| 436 | d = ch - '0';
|
| 437 | if( d > 9 ) d -= 7; |
| 438 | if( d > -1 && d < 16 ) { |
| 439 | v = ( 16 * v ) + d;
|
| 440 | gotch = TRUE; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | if( gotch ) {
|
| 445 | /* Replace = with char and move down in buffer */
|
| 446 | *ptr = v; |
| 447 | src = ptr + 3;
|
| 448 | dest = ptr + 1;
|
| 449 | while( *src ) {
|
| 450 | *dest++ = *src++; |
| 451 | } |
| 452 | *dest = '\0';
|
| 453 | } |
| 454 | ptr++; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /*
|
| 459 | * Read file data into root folder. |
| 460 | * Note that one vCard can have multiple E-Mail addresses (MAIL tags); |
| 461 | * these are broken out into separate address items. An address item |
| 462 | * is generated for the person identified by FN tag and each EMAIL tag. |
| 463 | * If a sub-type is included in the EMAIL entry, this will be used as |
| 464 | * the Remarks member. Also note that it is possible for one vCard |
| 465 | * entry to have multiple FN tags; this might not make sense. However, |
| 466 | * it will generate duplicate address entries for each person listed. |
| 467 | */ |
| 468 | static void vcard_read_file( VCardFile *cardFile ) { |
| 469 | gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL; |
| 470 | GSList *listName = NULL, *listAddress = NULL, *listRemarks = NULL, *listID = NULL; |
| 471 | /* GSList *listQP = NULL; */
|
| 472 | |
| 473 | for( ;; ) {
|
| 474 | gchar *line; |
| 475 | |
| 476 | line = vcard_get_line( cardFile ); |
| 477 | if( line == NULL ) break; |
| 478 | |
| 479 | /* fprintf( stdout, "%s\n", line ); */
|
| 480 | |
| 481 | /* Parse line */
|
| 482 | tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG ); |
| 483 | if( tagtemp == NULL ) { |
| 484 | g_free( line ); |
| 485 | continue;
|
| 486 | } |
| 487 | |
| 488 | /* fprintf( stdout, "\ttemp: %s\n", tagtemp ); */
|
| 489 | |
| 490 | tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG ); |
| 491 | if( tagvalue == NULL ) { |
| 492 | g_free( tagtemp ); |
| 493 | g_free( line ); |
| 494 | continue;
|
| 495 | } |
| 496 | |
| 497 | tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE ); |
| 498 | tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE ); |
| 499 | if( tagname == NULL ) { |
| 500 | tagname = tagtemp; |
| 501 | tagtemp = NULL;
|
| 502 | } |
| 503 | |
| 504 | /* fprintf( stdout, "\tname: %s\n", tagname ); */
|
| 505 | /* fprintf( stdout, "\ttype: %s\n", tagtype ); */
|
| 506 | /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
|
| 507 | |
| 508 | if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) { |
| 509 | /* Quoted-Printable: could span multiple lines */
|
| 510 | tagvalue = vcard_read_qp( cardFile, tagvalue ); |
| 511 | vcard_unescape_qp( tagvalue ); |
| 512 | /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
|
| 513 | } |
| 514 | |
| 515 | if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 && |
| 516 | g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
|
| 517 | /* fprintf( stdout, "start card\n" ); */
|
| 518 | vcard_free_lists( listName, listAddress, listRemarks, listID ); |
| 519 | listName = listAddress = listRemarks = listID = NULL;
|
| 520 | } |
| 521 | if( g_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) { |
| 522 | /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
|
| 523 | listName = g_slist_append( listName, g_strdup( tagvalue ) ); |
| 524 | } |
| 525 | if( g_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) { |
| 526 | /* fprintf( stdout, "- address: %s\n", tagvalue ); */
|
| 527 | listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) ); |
| 528 | listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) ); |
| 529 | } |
| 530 | if( g_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) { |
| 531 | /* fprintf( stdout, "- id: %s\n", tagvalue ); */
|
| 532 | listID = g_slist_append( listID, g_strdup( tagvalue ) ); |
| 533 | } |
| 534 | if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 && |
| 535 | g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
|
| 536 | /* vCard is complete */
|
| 537 | /* fprintf( stdout, "end card\n--\n" ); */
|
| 538 | /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
|
| 539 | vcard_build_items( cardFile, listName, listAddress, listRemarks, listID ); |
| 540 | vcard_free_lists( listName, listAddress, listRemarks, listID ); |
| 541 | listName = listAddress = listRemarks = listID = NULL;
|
| 542 | } |
| 543 | |
| 544 | g_free( tagname ); |
| 545 | g_free( tagtype ); |
| 546 | g_free( tagvalue ); |
| 547 | g_free( tagtemp ); |
| 548 | g_free( line ); |
| 549 | } |
| 550 | |
| 551 | /* Free lists */
|
| 552 | vcard_free_lists( listName, listAddress, listRemarks, listID ); |
| 553 | listName = listAddress = listRemarks = listID = NULL;
|
| 554 | } |
| 555 | |
| 556 | /* ============================================================================================ */
|
| 557 | /*
|
| 558 | * Read file into list. Main entry point |
| 559 | * Return: TRUE if file read successfully. |
| 560 | */ |
| 561 | /* ============================================================================================ */
|
| 562 | gint vcard_read_data( VCardFile *cardFile ) {
|
| 563 | g_return_val_if_fail( cardFile != NULL, -1 ); |
| 564 | |
| 565 | cardFile->retVal = MGU_SUCCESS; |
| 566 | cardFile->accessFlag = FALSE; |
| 567 | if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
|
| 568 | addrcache_clear( cardFile->addressCache ); |
| 569 | vcard_open_file( cardFile ); |
| 570 | if( cardFile->retVal == MGU_SUCCESS ) {
|
| 571 | /* Read data into the list */
|
| 572 | vcard_read_file( cardFile ); |
| 573 | vcard_close_file( cardFile ); |
| 574 | |
| 575 | /* Mark cache */
|
| 576 | addrcache_mark_file( cardFile->addressCache, cardFile->path ); |
| 577 | cardFile->addressCache->modified = FALSE; |
| 578 | cardFile->addressCache->dataRead = TRUE; |
| 579 | } |
| 580 | } |
| 581 | return cardFile->retVal;
|
| 582 | } |
| 583 | |
| 584 | /*
|
| 585 | * Return link list of persons. |
| 586 | */ |
| 587 | GList *vcard_get_list_person( VCardFile *cardFile ) {
|
| 588 | g_return_val_if_fail( cardFile != NULL, NULL ); |
| 589 | return addrcache_get_list_person( cardFile->addressCache );
|
| 590 | } |
| 591 | |
| 592 | /*
|
| 593 | * Return link list of folders. This is always NULL since there are |
| 594 | * no folders in GnomeCard. |
| 595 | * Return: NULL. |
| 596 | */ |
| 597 | GList *vcard_get_list_folder( VCardFile *cardFile ) {
|
| 598 | g_return_val_if_fail( cardFile != NULL, NULL ); |
| 599 | return NULL; |
| 600 | } |
| 601 | |
| 602 | /*
|
| 603 | * Return link list of all persons. Note that the list contains references |
| 604 | * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions... |
| 605 | * this will destroy the addressbook data! |
| 606 | * Return: List of items, or NULL if none. |
| 607 | */ |
| 608 | GList *vcard_get_all_persons( VCardFile *cardFile ) {
|
| 609 | g_return_val_if_fail( cardFile != NULL, NULL ); |
| 610 | return addrcache_get_all_persons( cardFile->addressCache );
|
| 611 | } |
| 612 | |
| 613 | /*
|
| 614 | * Validate that all parameters specified. |
| 615 | * Return: TRUE if data is good. |
| 616 | */ |
| 617 | gboolean vcard_validate( const VCardFile *cardFile ) {
|
| 618 | gboolean retVal; |
| 619 | |
| 620 | g_return_val_if_fail( cardFile != NULL, FALSE );
|
| 621 | |
| 622 | retVal = TRUE; |
| 623 | if( cardFile->path ) {
|
| 624 | if( strlen( cardFile->path ) < 1 ) retVal = FALSE; |
| 625 | } |
| 626 | else {
|
| 627 | retVal = FALSE; |
| 628 | } |
| 629 | if( cardFile->name ) {
|
| 630 | if( strlen( cardFile->name ) < 1 ) retVal = FALSE; |
| 631 | } |
| 632 | else {
|
| 633 | retVal = FALSE; |
| 634 | } |
| 635 | return retVal;
|
| 636 | } |
| 637 | |
| 638 | #define WORK_BUFLEN 1024 |
| 639 | |
| 640 | /*
|
| 641 | * Attempt to find a valid GnomeCard file. |
| 642 | * Return: Filename, or home directory if not found. Filename should |
| 643 | * be g_free() when done. |
| 644 | */ |
| 645 | gchar *vcard_find_gnomecard( void ) {
|
| 646 | const gchar *homedir;
|
| 647 | gchar buf[ WORK_BUFLEN ]; |
| 648 | gchar str[ WORK_BUFLEN ]; |
| 649 | gchar *fileSpec; |
| 650 | gint len, lenlbl, i; |
| 651 | FILE *fp; |
| 652 | |
| 653 | homedir = g_get_home_dir(); |
| 654 | if( ! homedir ) return NULL; |
| 655 | |
| 656 | strcpy( str, homedir ); |
| 657 | len = strlen( str ); |
| 658 | if( len > 0 ) { |
| 659 | if( str[ len-1 ] != G_DIR_SEPARATOR ) { |
| 660 | str[ len ] = G_DIR_SEPARATOR; |
| 661 | str[ ++len ] = '\0';
|
| 662 | } |
| 663 | } |
| 664 | strcat( str, GNOMECARD_DIR ); |
| 665 | strcat( str, G_DIR_SEPARATOR_S ); |
| 666 | strcat( str, GNOMECARD_FILE ); |
| 667 | |
| 668 | fileSpec = NULL;
|
| 669 | if( ( fp = fopen( str, "rb" ) ) != NULL ) { |
| 670 | /* Read configuration file */
|
| 671 | lenlbl = strlen( GNOMECARD_SECTION ); |
| 672 | while( fgets( buf, sizeof( buf ), fp ) != NULL ) { |
| 673 | if( 0 == g_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) { |
| 674 | break;
|
| 675 | } |
| 676 | } |
| 677 | |
| 678 | while( fgets( buf, sizeof( buf ), fp ) != NULL ) { |
| 679 | g_strchomp( buf ); |
| 680 | if( buf[0] == '[' ) break; |
| 681 | for( i = 0; i < lenlbl; i++ ) { |
| 682 | if( buf[i] == '=' ) { |
| 683 | if( 0 == g_strncasecmp( buf, GNOMECARD_PARAM, i ) ) { |
| 684 | fileSpec = g_strdup( buf + i + 1 );
|
| 685 | g_strstrip( fileSpec ); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | fclose( fp ); |
| 691 | } |
| 692 | |
| 693 | if( fileSpec == NULL ) { |
| 694 | /* Use the home directory */
|
| 695 | str[ len ] = '\0';
|
| 696 | fileSpec = g_strdup( str ); |
| 697 | } |
| 698 | |
| 699 | return fileSpec;
|
| 700 | } |
| 701 | |
| 702 | /*
|
| 703 | * Attempt to read file, testing for valid vCard format. |
| 704 | * Return: TRUE if file appears to be valid format. |
| 705 | */ |
| 706 | gint vcard_test_read_file( const gchar *fileSpec ) {
|
| 707 | gboolean haveStart; |
| 708 | gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line; |
| 709 | VCardFile *cardFile; |
| 710 | gint retVal, lines; |
| 711 | |
| 712 | if( ! fileSpec ) return MGU_NO_FILE; |
| 713 | |
| 714 | cardFile = vcard_create_path( fileSpec ); |
| 715 | cardFile->retVal = MGU_SUCCESS; |
| 716 | vcard_open_file( cardFile ); |
| 717 | if( cardFile->retVal == MGU_SUCCESS ) {
|
| 718 | cardFile->retVal = MGU_BAD_FORMAT; |
| 719 | haveStart = FALSE; |
| 720 | lines = VCARD_TEST_LINES; |
| 721 | while( lines > 0 ) { |
| 722 | lines--; |
| 723 | if( ( line = vcard_get_line( cardFile ) ) == NULL ) break; |
| 724 | |
| 725 | /* Parse line */
|
| 726 | tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG ); |
| 727 | if( tagtemp == NULL ) { |
| 728 | g_free( line ); |
| 729 | continue;
|
| 730 | } |
| 731 | |
| 732 | tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG ); |
| 733 | if( tagvalue == NULL ) { |
| 734 | g_free( tagtemp ); |
| 735 | g_free( line ); |
| 736 | continue;
|
| 737 | } |
| 738 | |
| 739 | tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE ); |
| 740 | tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE ); |
| 741 | if( tagname == NULL ) { |
| 742 | tagname = tagtemp; |
| 743 | tagtemp = NULL;
|
| 744 | } |
| 745 | |
| 746 | if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) { |
| 747 | /* Quoted-Printable: could span multiple lines */
|
| 748 | tagvalue = vcard_read_qp( cardFile, tagvalue ); |
| 749 | vcard_unescape_qp( tagvalue ); |
| 750 | } |
| 751 | if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 && |
| 752 | g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
|
| 753 | haveStart = TRUE; |
| 754 | } |
| 755 | if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 && |
| 756 | g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
|
| 757 | /* vCard is complete */
|
| 758 | if( haveStart ) cardFile->retVal = MGU_SUCCESS;
|
| 759 | } |
| 760 | |
| 761 | g_free( tagname ); |
| 762 | g_free( tagtype ); |
| 763 | g_free( tagvalue ); |
| 764 | g_free( tagtemp ); |
| 765 | g_free( line ); |
| 766 | } |
| 767 | vcard_close_file( cardFile ); |
| 768 | } |
| 769 | retVal = cardFile->retVal; |
| 770 | vcard_free( cardFile ); |
| 771 | cardFile = NULL;
|
| 772 | return retVal;
|
| 773 | } |
| 774 | |
| 775 | /*
|
| 776 | * End of Source. |
| 777 | */ |