2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2002 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"
34 #include "adbookbase.h"
36 #define GNOMECARD_DIR ".gnome"
37 #define GNOMECARD_FILE "GnomeCard"
38 #define GNOMECARD_SECTION "[file]"
39 #define GNOMECARD_PARAM "open"
41 #define VCARD_TEST_LINES 200
44 * Create new cardfile object.
46 VCardFile *vcard_create() {
48 cardFile = g_new0( VCardFile, 1 );
49 cardFile->type = ADBOOKTYPE_VCARD;
50 cardFile->addressCache = addrcache_create();
51 cardFile->retVal = MGU_SUCCESS;
53 cardFile->file = NULL;
54 cardFile->path = NULL;
55 cardFile->bufptr = cardFile->buffer;
62 void vcard_set_name( VCardFile* cardFile, const gchar *value ) {
63 g_return_if_fail( cardFile != NULL );
64 addrcache_set_name( cardFile->addressCache, value );
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->addressCache->accessFlag = value;
78 * Test whether file was modified since last access.
79 * Return: TRUE if file was modified.
81 gboolean vcard_get_modified( VCardFile *cardFile ) {
82 g_return_val_if_fail( cardFile != NULL, FALSE );
83 cardFile->addressCache->modified =
84 addrcache_check_file( cardFile->addressCache, cardFile->path );
85 return cardFile->addressCache->modified;
87 gboolean vcard_get_accessed( VCardFile *cardFile ) {
88 g_return_val_if_fail( cardFile != NULL, FALSE );
89 return cardFile->addressCache->accessFlag;
93 * Test whether file was read.
94 * Return: TRUE if file was read.
96 gboolean vcard_get_read_flag( VCardFile *cardFile ) {
97 g_return_val_if_fail( cardFile != NULL, FALSE );
98 return cardFile->addressCache->dataRead;
100 void vcard_set_read_flag( VCardFile *cardFile, const gboolean value ) {
101 g_return_if_fail( cardFile != NULL );
102 cardFile->addressCache->dataRead = value;
106 * Return status code from last file operation.
107 * Return: Status code.
109 gint vcard_get_status( VCardFile *cardFile ) {
110 g_return_val_if_fail( cardFile != NULL, -1 );
111 return cardFile->retVal;
114 ItemFolder *vcard_get_root_folder( VCardFile *cardFile ) {
115 g_return_val_if_fail( cardFile != NULL, NULL );
116 return addrcache_get_root_folder( cardFile->addressCache );
118 gchar *vcard_get_name( VCardFile *cardFile ) {
119 g_return_val_if_fail( cardFile != NULL, NULL );
120 return addrcache_get_name( cardFile->addressCache );
124 * Refresh internal variables to force a file read.
126 void vcard_force_refresh( VCardFile *cardFile ) {
127 addrcache_refresh( cardFile->addressCache );
131 * Create new cardfile object for specified file.
133 VCardFile *vcard_create_path( const gchar *path ) {
135 cardFile = vcard_create();
136 vcard_set_file(cardFile, path);
141 * Free up cardfile object by releasing internal memory.
143 void vcard_free( VCardFile *cardFile ) {
144 g_return_if_fail( cardFile != NULL );
147 if( cardFile->file ) fclose( cardFile->file );
150 addrcache_clear( cardFile->addressCache );
151 addrcache_free( cardFile->addressCache );
153 /* Free internal stuff */
154 g_free( cardFile->path );
157 cardFile->file = NULL;
158 cardFile->path = NULL;
159 cardFile->bufptr = NULL;
161 cardFile->type = ADBOOKTYPE_NONE;
162 cardFile->addressCache = NULL;
163 cardFile->retVal = MGU_SUCCESS;
165 /* Now release file object */
170 * Display object to specified stream.
172 void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
173 g_return_if_fail( cardFile != NULL );
175 fprintf( stream, "VCardFile:\n" );
176 fprintf( stream, "file spec: '%s'\n", cardFile->path );
177 fprintf( stream, " ret val: %d\n", cardFile->retVal );
178 addrcache_print( cardFile->addressCache, stream );
179 addritem_print_item_folder( cardFile->addressCache->rootFolder, stream );
183 * Open file for read.
184 * return: TRUE if file opened successfully.
186 static gint vcard_open_file( VCardFile* cardFile ) {
187 g_return_val_if_fail( cardFile != NULL, -1 );
189 /* fprintf( stdout, "Opening file\n" ); */
190 cardFile->addressCache->dataRead = FALSE;
191 if( cardFile->path ) {
192 cardFile->file = fopen( cardFile->path, "rb" );
193 if( ! cardFile->file ) {
194 /* fprintf( stderr, "can't open %s\n", cardFile->path ); */
195 cardFile->retVal = MGU_OPEN_FILE;
196 return cardFile->retVal;
200 /* fprintf( stderr, "file not specified\n" ); */
201 cardFile->retVal = MGU_NO_FILE;
202 return cardFile->retVal;
205 /* Setup a buffer area */
206 cardFile->buffer[0] = '\0';
207 cardFile->bufptr = cardFile->buffer;
208 cardFile->retVal = MGU_SUCCESS;
209 return cardFile->retVal;
215 static void vcard_close_file( VCardFile *cardFile ) {
216 g_return_if_fail( cardFile != NULL );
217 if( cardFile->file ) fclose( cardFile->file );
218 cardFile->file = NULL;
222 * Read line of text from file.
223 * Return: ptr to buffer where line starts.
225 static gchar *vcard_read_line( VCardFile *cardFile ) {
226 while( *cardFile->bufptr == '\n' || *cardFile->bufptr == '\0' ) {
227 if( fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
229 g_strstrip( cardFile->buffer );
230 cardFile->bufptr = cardFile->buffer;
232 return cardFile->bufptr;
236 * Read line of text from file.
237 * Return: ptr to buffer where line starts.
239 static gchar *vcard_get_line( VCardFile *cardFile ) {
240 gchar buf[ VCARDBUFSIZE ];
244 if (vcard_read_line( cardFile ) == NULL ) {
249 /* Copy into private buffer */
250 start = cardFile->bufptr;
251 len = strlen( start );
253 strncpy( buf, start, len );
256 cardFile->bufptr = end + 1;
258 /* Return a copy of buffer */
259 return g_strdup( buf );
263 * Free linked lists of character strings.
265 static void vcard_free_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList* listID ) {
266 mgu_free_list( listName );
267 mgu_free_list( listAddr );
268 mgu_free_list( listRem );
269 mgu_free_list( listID );
273 * Read quoted-printable text, which may span several lines into one long string.
274 * Param: cardFile - object.
275 * Param: tagvalue - will be placed into the linked list.
277 static gchar *vcard_read_qp( VCardFile *cardFile, char *tagvalue ) {
278 GSList *listQP = NULL;
280 gchar *line = tagvalue;
282 listQP = g_slist_append( listQP, line );
283 len = strlen( line ) - 1;
285 if( line[ len ] != '=' ) break;
288 line = vcard_get_line( cardFile );
291 /* Coalesce linked list into one long buffer. */
292 line = mgu_list_coalesce( listQP );
295 mgu_free_list( listQP );
301 * Parse tag name from line buffer.
302 * Return: Buffer containing the tag name, or NULL if no delimiter char found.
304 static gchar *vcard_get_tagname( char* line, gchar dlm ) {
312 tag = g_strndup( line, len+1 );
322 * Parse tag value from line buffer.
323 * Return: Buffer containing the tag value. Empty string is returned if
324 * no delimiter char found.
326 static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
332 for( lptr = line; *lptr; lptr++ ) {
340 value = g_strndup( start, len+1 );
343 /* Ensure that we get an empty string */
344 value = g_strndup( "", 1 );
351 * Build an address list entry and append to list of address items.
353 static void vcard_build_items(
354 VCardFile *cardFile, GSList *listName, GSList *listAddr,
355 GSList *listRem, GSList *listID )
357 GSList *nodeName = listName;
358 GSList *nodeID = listID;
361 GSList *nodeAddress = listAddr;
362 GSList *nodeRemarks = listRem;
363 ItemPerson *person = addritem_create_item_person();
364 addritem_person_set_common_name( person, nodeName->data );
365 while( nodeAddress ) {
366 str = nodeAddress->data;
368 ItemEMail *email = addritem_create_item_email();
369 addritem_email_set_address( email, str );
370 str = nodeRemarks->data;
373 if( g_strcasecmp( str, "internet" ) != 0 ) {
375 addritem_email_set_remarks( email, str );
379 addrcache_id_email( cardFile->addressCache, email );
380 addrcache_person_add_email( cardFile->addressCache, person, email );
382 nodeAddress = g_slist_next( nodeAddress );
383 nodeRemarks = g_slist_next( nodeRemarks );
385 if( person->listEMail ) {
386 addrcache_id_person( cardFile->addressCache, person );
387 addrcache_add_person( cardFile->addressCache, person );
390 addritem_person_set_external_id( person, str );
394 addritem_free_item_person( person );
396 nodeName = g_slist_next( nodeName );
397 nodeID = g_slist_next( nodeID );
401 /* Unescape characters in quoted-printable string. */
402 static void vcard_unescape_qp( gchar *value ) {
403 gchar *ptr, *src, *dest;
414 if( ch > '0' && ch < '8' ) v = ch - '0';
419 if( ch > '\x60' ) ch -= '\x20';
420 if( ch > '0' && ch < ' ' ) d = ch - '0';
423 if( d > -1 && d < 16 ) {
430 /* Replace = with char and move down in buffer */
444 * Read file data into root folder.
445 * Note that one vCard can have multiple E-Mail addresses (MAIL tags);
446 * these are broken out into separate address items. An address item
447 * is generated for the person identified by FN tag and each EMAIL tag.
448 * If a sub-type is included in the EMAIL entry, this will be used as
449 * the Remarks member. Also note that it is possible for one vCard
450 * entry to have multiple FN tags; this might not make sense. However,
451 * it will generate duplicate address entries for each person listed.
453 static void vcard_read_file( VCardFile *cardFile ) {
454 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL;
455 GSList *listName = NULL, *listAddress = NULL, *listRemarks = NULL, *listID = NULL;
456 /* GSList *listQP = NULL; */
459 gchar *line = vcard_get_line( cardFile );
460 if( line == NULL ) break;
462 /* fprintf( stdout, "%s\n", line ); */
465 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
466 if( tagtemp == NULL ) {
471 /* fprintf( stdout, "\ttemp: %s\n", tagtemp ); */
472 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
473 if( tagvalue == NULL ) {
479 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
480 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
481 if( tagname == NULL ) {
486 /* fprintf( stdout, "\tname: %s\n", tagname ); */
487 /* fprintf( stdout, "\ttype: %s\n", tagtype ); */
488 /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
490 if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
491 /* Quoted-Printable: could span multiple lines */
492 tagvalue = vcard_read_qp( cardFile, tagvalue );
493 vcard_unescape_qp( tagvalue );
494 /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
497 if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
498 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
499 /* fprintf( stdout, "start card\n" ); */
500 vcard_free_lists( listName, listAddress, listRemarks, listID );
501 listName = listAddress = listRemarks = listID = NULL;
503 if( g_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
504 /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
505 listName = g_slist_append( listName, g_strdup( tagvalue ) );
507 if( g_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) {
508 /* fprintf( stdout, "- address: %s\n", tagvalue ); */
509 listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
510 listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
512 if( g_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) {
513 /* fprintf( stdout, "- id: %s\n", tagvalue ); */
514 listID = g_slist_append( listID, g_strdup( tagvalue ) );
516 if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
517 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
518 /* vCard is complete */
519 /* fprintf( stdout, "end card\n--\n" ); */
520 /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
521 vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
522 vcard_free_lists( listName, listAddress, listRemarks, listID );
523 listName = listAddress = listRemarks = listID = NULL;
535 vcard_free_lists( listName, listAddress, listRemarks, listID );
536 listName = listAddress = listRemarks = listID = NULL;
539 /* ============================================================================================ */
541 * Read file into list. Main entry point
542 * Return: TRUE if file read successfully.
544 /* ============================================================================================ */
545 gint vcard_read_data( VCardFile *cardFile ) {
546 g_return_val_if_fail( cardFile != NULL, -1 );
548 cardFile->retVal = MGU_SUCCESS;
549 cardFile->addressCache->accessFlag = FALSE;
550 if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
551 addrcache_clear( cardFile->addressCache );
552 vcard_open_file( cardFile );
553 if( cardFile->retVal == MGU_SUCCESS ) {
554 /* Read data into the list */
555 vcard_read_file( cardFile );
556 vcard_close_file( cardFile );
559 addrcache_mark_file( cardFile->addressCache, cardFile->path );
560 cardFile->addressCache->modified = FALSE;
561 cardFile->addressCache->dataRead = TRUE;
564 return cardFile->retVal;
568 * Return link list of persons.
570 GList *vcard_get_list_person( VCardFile *cardFile ) {
571 g_return_val_if_fail( cardFile != NULL, NULL );
572 return addrcache_get_list_person( cardFile->addressCache );
576 * Return link list of folders. This is always NULL since there are
577 * no folders in GnomeCard.
580 GList *vcard_get_list_folder( VCardFile *cardFile ) {
581 g_return_val_if_fail( cardFile != NULL, NULL );
586 * Return link list of all persons. Note that the list contains references
587 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
588 * this will destroy the addressbook data!
589 * Return: List of items, or NULL if none.
591 GList *vcard_get_all_persons( VCardFile *cardFile ) {
592 g_return_val_if_fail( cardFile != NULL, NULL );
593 return addrcache_get_all_persons( cardFile->addressCache );
597 * Validate that all parameters specified.
598 * Return: TRUE if data is good.
600 gboolean vcard_validate( const VCardFile *cardFile ) {
604 g_return_val_if_fail( cardFile != NULL, FALSE );
607 if( cardFile->path ) {
608 if( strlen( cardFile->path ) < 1 ) retVal = FALSE;
613 name = addrcache_get_name( cardFile->addressCache );
615 if( strlen( name ) < 1 ) retVal = FALSE;
623 #define WORK_BUFLEN 1024
626 * Attempt to find a valid GnomeCard file.
627 * Return: Filename, or home directory if not found. Filename should
628 * be g_free() when done.
630 gchar *vcard_find_gnomecard( void ) {
632 gchar buf[ WORK_BUFLEN ];
633 gchar str[ WORK_BUFLEN ];
638 homedir = g_get_home_dir();
639 if( ! homedir ) return NULL;
641 strcpy( str, homedir );
644 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
645 str[ len ] = G_DIR_SEPARATOR;
649 strcat( str, GNOMECARD_DIR );
650 strcat( str, G_DIR_SEPARATOR_S );
651 strcat( str, GNOMECARD_FILE );
654 if( ( fp = fopen( str, "rb" ) ) != NULL ) {
655 /* Read configuration file */
656 lenlbl = strlen( GNOMECARD_SECTION );
657 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
658 if( 0 == g_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
663 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
665 if( buf[0] == '[' ) break;
666 for( i = 0; i < lenlbl; i++ ) {
667 if( buf[i] == '=' ) {
668 if( 0 == g_strncasecmp( buf, GNOMECARD_PARAM, i ) ) {
669 fileSpec = g_strdup( buf + i + 1 );
670 g_strstrip( fileSpec );
678 if( fileSpec == NULL ) {
679 /* Use the home directory */
681 fileSpec = g_strdup( str );
688 * Attempt to read file, testing for valid vCard format.
689 * Return: TRUE if file appears to be valid format.
691 gint vcard_test_read_file( const gchar *fileSpec ) {
693 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line;
697 if( ! fileSpec ) return MGU_NO_FILE;
699 cardFile = vcard_create_path( fileSpec );
700 cardFile->retVal = MGU_SUCCESS;
701 vcard_open_file( cardFile );
702 if( cardFile->retVal == MGU_SUCCESS ) {
703 cardFile->retVal = MGU_BAD_FORMAT;
705 lines = VCARD_TEST_LINES;
708 if( ( line = vcard_get_line( cardFile ) ) == NULL ) break;
711 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
712 if( tagtemp == NULL ) {
717 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
718 if( tagvalue == NULL ) {
724 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
725 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
726 if( tagname == NULL ) {
731 if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
732 /* Quoted-Printable: could span multiple lines */
733 tagvalue = vcard_read_qp( cardFile, tagvalue );
734 vcard_unescape_qp( tagvalue );
736 if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
737 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
740 if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
741 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
742 /* vCard is complete */
743 if( haveStart ) cardFile->retVal = MGU_SUCCESS;
752 vcard_close_file( cardFile );
754 retVal = cardFile->retVal;
755 vcard_free( cardFile );