2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Match Grun
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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.
33 #include "addrcache.h"
35 #define GNOMECARD_DIR ".gnome"
36 #define GNOMECARD_FILE "GnomeCard"
37 #define GNOMECARD_SECTION "[file]"
38 #define GNOMECARD_PARAM "open"
40 #define VCARD_TEST_LINES 200
43 * Create new cardfile object.
45 VCardFile *vcard_create() {
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;
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 );
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 );
72 void vcard_set_accessed( VCardFile *cardFile, const gboolean value ) {
73 g_return_if_fail( cardFile != NULL );
74 cardFile->accessFlag = value;
78 * Test whether file was modified since last access.
79 * Return: TRUE if file was modified.
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 );
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 );
91 * Test whether file was read.
92 * Return: TRUE if file was read.
94 gboolean vcard_get_read_flag( VCardFile *vcardFile ) {
95 g_return_val_if_fail( vcardFile != NULL, FALSE );
96 return vcardFile->addressCache->dataRead;
100 * Return status code from last file operation.
101 * Return: Status code.
103 gint vcard_get_status( VCardFile *cardFile ) {
104 g_return_val_if_fail( cardFile != NULL, -1 );
105 return cardFile->retVal;
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 );
112 gchar *vcard_get_name( VCardFile *cardFile ) {
113 g_return_val_if_fail( cardFile != NULL, NULL );
114 return cardFile->name;
118 * Refresh internal variables to force a file read.
120 void vcard_force_refresh( VCardFile *cardFile ) {
121 addrcache_refresh( cardFile->addressCache );
125 * Create new cardfile object for specified file.
127 VCardFile *vcard_create_path( const gchar *path ) {
129 cardFile = vcard_create();
130 vcard_set_file(cardFile, path);
135 * Free up cardfile object by releasing internal memory.
137 void vcard_free( VCardFile *cardFile ) {
138 g_return_if_fail( cardFile != NULL );
141 if( cardFile->file ) fclose( cardFile->file );
143 /* Free internal stuff */
144 g_free( cardFile->name );
145 g_free( cardFile->path );
148 addrcache_clear( cardFile->addressCache );
149 addrcache_free( cardFile->addressCache );
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;
159 /* Now release file object */
165 * Display object to specified stream.
167 void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
168 g_return_if_fail( cardFile != NULL );
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 );
179 * Open file for read.
180 * return: TRUE if file opened successfully.
182 static gint vcard_open_file( VCardFile* cardFile ) {
183 g_return_val_if_fail( cardFile != NULL, -1 );
185 /* fprintf( stdout, "Opening file\n" ); */
186 cardFile->addressCache->dataRead = FALSE;
187 if( cardFile->path ) {
188 cardFile->file = fopen( cardFile->path, "r" );
189 if( ! cardFile->file ) {
190 /* fprintf( stderr, "can't open %s\n", cardFile->path ); */
191 cardFile->retVal = MGU_OPEN_FILE;
192 return cardFile->retVal;
196 /* fprintf( stderr, "file not specified\n" ); */
197 cardFile->retVal = MGU_NO_FILE;
198 return cardFile->retVal;
201 /* Setup a buffer area */
202 cardFile->buffer[0] = '\0';
203 cardFile->bufptr = cardFile->buffer;
204 cardFile->retVal = MGU_SUCCESS;
205 return cardFile->retVal;
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;
218 * Read line of text from file.
219 * Return: ptr to buffer where line starts.
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 )
225 g_strstrip( cardFile->buffer );
226 cardFile->bufptr = cardFile->buffer;
228 return cardFile->bufptr;
232 * Read line of text from file.
233 * Return: ptr to buffer where line starts.
235 static gchar *vcard_get_line( VCardFile *cardFile ) {
236 gchar buf[ VCARDBUFSIZE ];
240 if (vcard_read_line( cardFile ) == NULL ) {
245 /* Copy into private buffer */
246 start = cardFile->bufptr;
247 len = strlen( start );
249 strncpy( buf, start, len );
252 cardFile->bufptr = end + 1;
254 /* Return a copy of buffer */
255 return g_strdup( buf );
259 * Free linked lists of character strings.
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 );
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.
273 static gchar *vcard_read_qp( VCardFile *cardFile, gchar *tagvalue ) {
274 GSList *listQP = NULL;
276 gchar *line = tagvalue;
279 listQP = g_slist_append( listQP, line );
280 len = strlen( line ) - 1;
282 if( line[ len ] != '=' ) break;
285 line = vcard_get_line( cardFile );
288 /* Coalesce linked list into one long buffer. */
289 line = mgu_list_coalesce( listQP );
292 mgu_free_list( listQP );
298 * Parse tag name from line buffer.
299 * Return: Buffer containing the tag name, or NULL if no delimiter char found.
301 static gchar *vcard_get_tagname( gchar* line, gchar dlm ) {
309 tag = g_strndup( line, len+1 );
319 * Parse tag value from line buffer.
320 * Return: Buffer containing the tag value. Empty string is returned if
321 * no delimiter char found.
323 static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
329 for( lptr = line; *lptr; lptr++ ) {
337 value = g_strndup( start, len+1 );
340 /* Ensure that we get an empty string */
341 value = g_strndup( "", 1 );
348 * Dump linked lists of character strings (for debug).
350 static void vcard_dump_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList *listID, FILE *stream ) {
351 fprintf( stream, "dump name\n" );
352 fprintf( stream, "------------\n" );
353 mgu_print_list( listName, stdout );
354 fprintf( stream, "dump address\n" );
355 fprintf( stream, "------------\n" );
356 mgu_print_list( listAddr, stdout );
357 fprintf( stream, "dump remarks\n" );
358 fprintf( stdout, "------------\n" );
359 mgu_print_list( listRem, stdout );
360 fprintf( stream, "dump id\n" );
361 fprintf( stdout, "------------\n" );
362 mgu_print_list( listID, stdout );
366 * Build an address list entry and append to list of address items.
368 static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *listAddr, GSList *listRem,
371 GSList *nodeName = listName;
372 GSList *nodeID = listID;
375 GSList *nodeAddress = listAddr;
376 GSList *nodeRemarks = listRem;
377 ItemPerson *person = addritem_create_item_person();
378 addritem_person_set_common_name( person, nodeName->data );
379 while( nodeAddress ) {
380 str = nodeAddress->data;
382 ItemEMail *email = addritem_create_item_email();
383 addritem_email_set_address( email, str );
384 str = nodeRemarks->data;
387 if( g_strcasecmp( str, "internet" ) != 0 ) {
388 if( *str != '\0' ) addritem_email_set_remarks( email, str );
392 addrcache_id_email( cardFile->addressCache, email );
393 addrcache_person_add_email( cardFile->addressCache, person, email );
395 nodeAddress = g_slist_next( nodeAddress );
396 nodeRemarks = g_slist_next( nodeRemarks );
398 if( person->listEMail ) {
399 addrcache_id_person( cardFile->addressCache, person );
400 addrcache_add_person( cardFile->addressCache, person );
403 addritem_free_item_person( person );
407 addritem_person_set_external_id( person, str );
409 nodeName = g_slist_next( nodeName );
410 nodeID = g_slist_next( nodeID );
414 /* Unescape characters in quoted-printable string. */
415 static void vcard_unescape_qp( gchar *value ) {
416 gchar *ptr, *src, *dest;
427 if( ch > '0' && ch < '8' ) v = ch - '0';
432 if( ch > '\x60' ) ch -= '\x20';
433 if( ch > '0' && ch < ' ' ) d = ch - '0';
436 if( d > -1 && d < 16 ) {
443 /* Replace = with char and move down in buffer */
457 * Read file data into root folder.
458 * Note that one vCard can have multiple E-Mail addresses (MAIL tags);
459 * these are broken out into separate address items. An address item
460 * is generated for the person identified by FN tag and each EMAIL tag.
461 * If a sub-type is included in the EMAIL entry, this will be used as
462 * the Remarks member. Also note that it is possible for one vCard
463 * entry to have multiple FN tags; this might not make sense. However,
464 * it will generate duplicate address entries for each person listed.
466 static void vcard_read_file( VCardFile *cardFile ) {
467 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL;
468 GSList *listName = NULL, *listAddress = NULL, *listRemarks = NULL, *listID = NULL;
469 /* GSList *listQP = NULL; */
474 line = vcard_get_line( cardFile );
475 if( line == NULL ) break;
477 /* fprintf( stdout, "%s\n", line ); */
480 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
481 if( tagtemp == NULL ) {
486 /* fprintf( stdout, "\ttemp: %s\n", tagtemp ); */
488 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
489 if( tagvalue == NULL ) {
495 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
496 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
497 if( tagname == NULL ) {
502 /* fprintf( stdout, "\tname: %s\n", tagname ); */
503 /* fprintf( stdout, "\ttype: %s\n", tagtype ); */
504 /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
506 if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
507 /* Quoted-Printable: could span multiple lines */
508 tagvalue = vcard_read_qp( cardFile, tagvalue );
509 vcard_unescape_qp( tagvalue );
510 /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
513 if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
514 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
515 /* fprintf( stdout, "start card\n" ); */
516 vcard_free_lists( listName, listAddress, listRemarks, listID );
517 listName = listAddress = listRemarks = listID = NULL;
519 if( g_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
520 /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
521 listName = g_slist_append( listName, g_strdup( tagvalue ) );
523 if( g_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) {
524 /* fprintf( stdout, "- address: %s\n", tagvalue ); */
525 listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
526 listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
528 if( g_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) {
529 /* fprintf( stdout, "- id: %s\n", tagvalue ); */
530 listID = g_slist_append( listID, g_strdup( tagvalue ) );
532 if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
533 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
534 /* vCard is complete */
535 /* fprintf( stdout, "end card\n--\n" ); */
536 /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
537 vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
538 vcard_free_lists( listName, listAddress, listRemarks, listID );
539 listName = listAddress = listRemarks = listID = NULL;
550 vcard_free_lists( listName, listAddress, listRemarks, listID );
551 listName = listAddress = listRemarks = listID = NULL;
554 /* ============================================================================================ */
556 * Read file into list. Main entry point
557 * Return: TRUE if file read successfully.
559 /* ============================================================================================ */
560 gint vcard_read_data( VCardFile *cardFile ) {
561 g_return_val_if_fail( cardFile != NULL, -1 );
563 cardFile->retVal = MGU_SUCCESS;
564 cardFile->accessFlag = FALSE;
565 if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
566 addrcache_clear( cardFile->addressCache );
567 vcard_open_file( cardFile );
568 if( cardFile->retVal == MGU_SUCCESS ) {
569 /* Read data into the list */
570 vcard_read_file( cardFile );
571 vcard_close_file( cardFile );
574 addrcache_mark_file( cardFile->addressCache, cardFile->path );
575 cardFile->addressCache->modified = FALSE;
576 cardFile->addressCache->dataRead = TRUE;
579 return cardFile->retVal;
583 * Return link list of persons.
585 GList *vcard_get_list_person( VCardFile *cardFile ) {
586 g_return_val_if_fail( cardFile != NULL, NULL );
587 return addrcache_get_list_person( cardFile->addressCache );
591 * Return link list of folders. This is always NULL since there are
592 * no folders in GnomeCard.
595 GList *vcard_get_list_folder( VCardFile *cardFile ) {
596 g_return_val_if_fail( cardFile != NULL, NULL );
601 * Return link list of all persons. Note that the list contains references
602 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
603 * this will destroy the addressbook data!
604 * Return: List of items, or NULL if none.
606 GList *vcard_get_all_persons( VCardFile *cardFile ) {
607 g_return_val_if_fail( cardFile != NULL, NULL );
608 return addrcache_get_all_persons( cardFile->addressCache );
612 * Validate that all parameters specified.
613 * Return: TRUE if data is good.
615 gboolean vcard_validate( const VCardFile *cardFile ) {
618 g_return_val_if_fail( cardFile != NULL, FALSE );
621 if( cardFile->path ) {
622 if( strlen( cardFile->path ) < 1 ) retVal = FALSE;
627 if( cardFile->name ) {
628 if( strlen( cardFile->name ) < 1 ) retVal = FALSE;
636 #define WORK_BUFLEN 1024
639 * Attempt to find a valid GnomeCard file.
640 * Return: Filename, or home directory if not found. Filename should
641 * be g_free() when done.
643 gchar *vcard_find_gnomecard( void ) {
645 gchar buf[ WORK_BUFLEN ];
646 gchar str[ WORK_BUFLEN ];
651 homedir = g_get_home_dir();
652 if( ! homedir ) return NULL;
654 strcpy( str, homedir );
657 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
658 str[ len ] = G_DIR_SEPARATOR;
662 strcat( str, GNOMECARD_DIR );
663 strcat( str, G_DIR_SEPARATOR_S );
664 strcat( str, GNOMECARD_FILE );
667 if( ( fp = fopen( str, "r" ) ) != NULL ) {
668 /* Read configuration file */
669 lenlbl = strlen( GNOMECARD_SECTION );
670 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
671 if( 0 == g_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
676 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
678 if( buf[0] == '[' ) break;
679 for( i = 0; i < lenlbl; i++ ) {
680 if( buf[i] == '=' ) {
681 if( 0 == g_strncasecmp( buf, GNOMECARD_PARAM, i ) ) {
682 fileSpec = g_strdup( buf + i + 1 );
683 g_strstrip( fileSpec );
691 if( fileSpec == NULL ) {
692 /* Use the home directory */
694 fileSpec = g_strdup( str );
701 * Attempt to read file, testing for valid vCard format.
702 * Return: TRUE if file appears to be valid format.
704 gint vcard_test_read_file( const gchar *fileSpec ) {
706 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line;
710 if( ! fileSpec ) return MGU_NO_FILE;
712 cardFile = vcard_create_path( fileSpec );
713 cardFile->retVal = MGU_SUCCESS;
714 vcard_open_file( cardFile );
715 if( cardFile->retVal == MGU_SUCCESS ) {
716 cardFile->retVal = MGU_BAD_FORMAT;
718 lines = VCARD_TEST_LINES;
721 if( ( line = vcard_get_line( cardFile ) ) == NULL ) break;
724 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
725 if( tagtemp == NULL ) {
730 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
731 if( tagvalue == NULL ) {
737 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
738 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
739 if( tagname == NULL ) {
744 if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
745 /* Quoted-Printable: could span multiple lines */
746 tagvalue = vcard_read_qp( cardFile, tagvalue );
747 vcard_unescape_qp( tagvalue );
749 if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
750 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
753 if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
754 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
755 /* vCard is complete */
756 if( haveStart ) cardFile->retVal = MGU_SUCCESS;
765 vcard_close_file( cardFile );
767 retVal = cardFile->retVal;
768 vcard_free( cardFile );