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;
57 /* We want to use an address completion index */
58 addrcache_use_index( cardFile->addressCache, TRUE );
66 void vcard_set_name( VCardFile* cardFile, const gchar *value ) {
67 g_return_if_fail( cardFile != NULL );
68 addrcache_set_name( cardFile->addressCache, value );
70 void vcard_set_file( VCardFile* cardFile, const gchar *value ) {
71 g_return_if_fail( cardFile != NULL );
72 addrcache_refresh( cardFile->addressCache );
73 cardFile->path = mgu_replace_string( cardFile->path, value );
74 g_strstrip( cardFile->path );
76 void vcard_set_accessed( VCardFile *cardFile, const gboolean value ) {
77 g_return_if_fail( cardFile != NULL );
78 cardFile->addressCache->accessFlag = value;
82 * Test whether file was modified since last access.
83 * Return: TRUE if file was modified.
85 gboolean vcard_get_modified( VCardFile *cardFile ) {
86 g_return_val_if_fail( cardFile != NULL, FALSE );
87 cardFile->addressCache->modified =
88 addrcache_check_file( cardFile->addressCache, cardFile->path );
89 return cardFile->addressCache->modified;
91 gboolean vcard_get_accessed( VCardFile *cardFile ) {
92 g_return_val_if_fail( cardFile != NULL, FALSE );
93 return cardFile->addressCache->accessFlag;
97 * Test whether file was read.
98 * Return: TRUE if file was read.
100 gboolean vcard_get_read_flag( VCardFile *cardFile ) {
101 g_return_val_if_fail( cardFile != NULL, FALSE );
102 return cardFile->addressCache->dataRead;
104 void vcard_set_read_flag( VCardFile *cardFile, const gboolean value ) {
105 g_return_if_fail( cardFile != NULL );
106 cardFile->addressCache->dataRead = value;
110 * Return status code from last file operation.
111 * Return: Status code.
113 gint vcard_get_status( VCardFile *cardFile ) {
114 g_return_val_if_fail( cardFile != NULL, -1 );
115 return cardFile->retVal;
118 ItemFolder *vcard_get_root_folder( VCardFile *cardFile ) {
119 g_return_val_if_fail( cardFile != NULL, NULL );
120 return addrcache_get_root_folder( cardFile->addressCache );
122 gchar *vcard_get_name( VCardFile *cardFile ) {
123 g_return_val_if_fail( cardFile != NULL, NULL );
124 return addrcache_get_name( cardFile->addressCache );
128 * Refresh internal variables to force a file read.
130 void vcard_force_refresh( VCardFile *cardFile ) {
131 addrcache_refresh( cardFile->addressCache );
135 * Create new cardfile object for specified file.
137 VCardFile *vcard_create_path( const gchar *path ) {
139 cardFile = vcard_create();
140 vcard_set_file(cardFile, path);
145 * Free up cardfile object by releasing internal memory.
147 void vcard_free( VCardFile *cardFile ) {
148 g_return_if_fail( cardFile != NULL );
151 if( cardFile->file ) fclose( cardFile->file );
154 addrcache_free( cardFile->addressCache );
156 /* Free internal stuff */
157 g_free( cardFile->path );
160 cardFile->file = NULL;
161 cardFile->path = NULL;
162 cardFile->bufptr = NULL;
164 cardFile->type = ADBOOKTYPE_NONE;
165 cardFile->addressCache = NULL;
166 cardFile->retVal = MGU_SUCCESS;
168 /* Now release file object */
173 * Display object to specified stream.
175 void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
176 g_return_if_fail( cardFile != NULL );
178 fprintf( stream, "VCardFile:\n" );
179 fprintf( stream, "file spec: '%s'\n", cardFile->path );
180 fprintf( stream, " ret val: %d\n", cardFile->retVal );
181 addrcache_print( cardFile->addressCache, stream );
182 addritem_print_item_folder( cardFile->addressCache->rootFolder, stream );
186 * Open file for read.
187 * return: TRUE if file opened successfully.
189 static gint vcard_open_file( VCardFile* cardFile ) {
190 g_return_val_if_fail( cardFile != NULL, -1 );
192 /* fprintf( stdout, "Opening file\n" ); */
193 cardFile->addressCache->dataRead = FALSE;
194 if( cardFile->path ) {
195 cardFile->file = fopen( cardFile->path, "rb" );
196 if( ! cardFile->file ) {
197 /* fprintf( stderr, "can't open %s\n", cardFile->path ); */
198 cardFile->retVal = MGU_OPEN_FILE;
199 return cardFile->retVal;
203 /* fprintf( stderr, "file not specified\n" ); */
204 cardFile->retVal = MGU_NO_FILE;
205 return cardFile->retVal;
208 /* Setup a buffer area */
209 cardFile->buffer[0] = '\0';
210 cardFile->bufptr = cardFile->buffer;
211 cardFile->retVal = MGU_SUCCESS;
212 return cardFile->retVal;
218 static void vcard_close_file( VCardFile *cardFile ) {
219 g_return_if_fail( cardFile != NULL );
220 if( cardFile->file ) fclose( cardFile->file );
221 cardFile->file = NULL;
225 * Read line of text from file.
226 * Return: ptr to buffer where line starts.
228 static gchar *vcard_read_line( VCardFile *cardFile ) {
229 while( *cardFile->bufptr == '\n' || *cardFile->bufptr == '\0' ) {
230 if( fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
232 g_strstrip( cardFile->buffer );
233 cardFile->bufptr = cardFile->buffer;
235 return cardFile->bufptr;
239 * Read line of text from file.
240 * Return: ptr to buffer where line starts.
242 static gchar *vcard_get_line( VCardFile *cardFile ) {
243 gchar buf[ VCARDBUFSIZE ];
247 if (vcard_read_line( cardFile ) == NULL ) {
252 /* Copy into private buffer */
253 start = cardFile->bufptr;
254 len = strlen( start );
256 strncpy( buf, start, len );
259 cardFile->bufptr = end + 1;
261 /* Return a copy of buffer */
262 return g_strdup( buf );
266 * Free linked lists of character strings.
268 static void vcard_free_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList* listID ) {
269 mgu_free_list( listName );
270 mgu_free_list( listAddr );
271 mgu_free_list( listRem );
272 mgu_free_list( listID );
276 * Read quoted-printable text, which may span several lines into one long string.
277 * Param: cardFile - object.
278 * Param: tagvalue - will be placed into the linked list.
280 static gchar *vcard_read_qp( VCardFile *cardFile, char *tagvalue ) {
281 GSList *listQP = NULL;
283 gchar *line = tagvalue;
285 listQP = g_slist_append( listQP, line );
286 len = strlen( line ) - 1;
287 if( line[ len ] != '=' ) break;
289 line = vcard_get_line( cardFile );
292 /* Coalesce linked list into one long buffer. */
293 line = mgu_list_coalesce( listQP );
296 mgu_free_list( listQP );
302 * Parse tag name from line buffer.
303 * Return: Buffer containing the tag name, or NULL if no delimiter char found.
305 static gchar *vcard_get_tagname( char* line, gchar dlm ) {
313 tag = g_strndup( line, len+1 );
323 * Parse tag value from line buffer.
324 * Return: Buffer containing the tag value. Empty string is returned if
325 * no delimiter char found.
327 static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
333 for( lptr = line; *lptr; lptr++ ) {
341 value = g_strndup( start, len+1 );
344 /* Ensure that we get an empty string */
345 value = g_strndup( "", 1 );
352 * Build an address list entry and append to list of address items.
354 static void vcard_build_items(
355 VCardFile *cardFile, GSList *listName, GSList *listAddr,
356 GSList *listRem, GSList *listID )
358 GSList *nodeName = listName;
359 GSList *nodeID = listID;
362 GSList *nodeAddress = listAddr;
363 GSList *nodeRemarks = listRem;
364 ItemPerson *person = addritem_create_item_person();
365 addritem_person_set_common_name( person, nodeName->data );
366 while( nodeAddress ) {
367 str = nodeAddress->data;
369 ItemEMail *email = addritem_create_item_email();
370 addritem_email_set_address( email, str );
371 str = nodeRemarks->data;
374 if( g_strcasecmp( str, "internet" ) != 0 ) {
376 addritem_email_set_remarks( email, str );
380 addrcache_id_email( cardFile->addressCache, email );
381 addrcache_person_add_email( cardFile->addressCache, person, email );
383 nodeAddress = g_slist_next( nodeAddress );
384 nodeRemarks = g_slist_next( nodeRemarks );
386 if( person->listEMail ) {
387 addrcache_id_person( cardFile->addressCache, person );
388 addrcache_add_person( cardFile->addressCache, person );
391 addritem_person_set_external_id( person, str );
395 addritem_free_item_person( person );
397 nodeName = g_slist_next( nodeName );
398 nodeID = g_slist_next( nodeID );
402 /* Unescape characters in quoted-printable string. */
403 static void vcard_unescape_qp( gchar *value ) {
404 gchar *ptr, *src, *dest;
415 if( ch > '0' && ch < '8' ) v = ch - '0';
420 if( ch > '\x60' ) ch -= '\x20';
421 if( ch > '0' && ch < ' ' ) d = ch - '0';
424 if( d > -1 && d < 16 ) {
431 /* Replace = with char and move down in buffer */
445 * Read file data into root folder.
446 * Note that one vCard can have multiple E-Mail addresses (MAIL tags);
447 * these are broken out into separate address items. An address item
448 * is generated for the person identified by FN tag and each EMAIL tag.
449 * If a sub-type is included in the EMAIL entry, this will be used as
450 * the Remarks member. Also note that it is possible for one vCard
451 * entry to have multiple FN tags; this might not make sense. However,
452 * it will generate duplicate address entries for each person listed.
454 static void vcard_read_file( VCardFile *cardFile ) {
455 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL;
456 GSList *listName = NULL, *listAddress = NULL, *listRemarks = NULL, *listID = NULL;
457 /* GSList *listQP = NULL; */
460 gchar *line = vcard_get_line( cardFile );
461 if( line == NULL ) break;
463 /* fprintf( stdout, "%s\n", line ); */
466 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
467 if( tagtemp == NULL ) {
472 /* fprintf( stdout, "\ttemp: %s\n", tagtemp ); */
473 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
474 if( tagvalue == NULL ) {
480 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
481 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
482 if( tagname == NULL ) {
487 /* fprintf( stdout, "\tname: %s\n", tagname ); */
488 /* fprintf( stdout, "\ttype: %s\n", tagtype ); */
489 /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
491 if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
492 /* Quoted-Printable: could span multiple lines */
493 tagvalue = vcard_read_qp( cardFile, tagvalue );
494 vcard_unescape_qp( tagvalue );
495 /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
498 if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
499 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
500 /* fprintf( stdout, "start card\n" ); */
501 vcard_free_lists( listName, listAddress, listRemarks, listID );
502 listName = listAddress = listRemarks = listID = NULL;
504 if( g_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
505 /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
506 listName = g_slist_append( listName, g_strdup( tagvalue ) );
508 if( g_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) {
509 /* fprintf( stdout, "- address: %s\n", tagvalue ); */
510 listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
511 listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
513 if( g_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) {
514 /* fprintf( stdout, "- id: %s\n", tagvalue ); */
515 listID = g_slist_append( listID, g_strdup( tagvalue ) );
517 if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
518 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
519 /* vCard is complete */
520 /* fprintf( stdout, "end card\n--\n" ); */
521 /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
522 vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
523 vcard_free_lists( listName, listAddress, listRemarks, listID );
524 listName = listAddress = listRemarks = listID = NULL;
536 vcard_free_lists( listName, listAddress, listRemarks, listID );
537 listName = listAddress = listRemarks = listID = NULL;
540 /* ============================================================================================ */
542 * Read file into list. Main entry point
543 * Return: TRUE if file read successfully.
545 /* ============================================================================================ */
546 gint vcard_read_data( VCardFile *cardFile ) {
547 g_return_val_if_fail( cardFile != NULL, -1 );
549 cardFile->retVal = MGU_SUCCESS;
550 cardFile->addressCache->accessFlag = FALSE;
551 if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
552 addrcache_clear( cardFile->addressCache );
553 vcard_open_file( cardFile );
554 if( cardFile->retVal == MGU_SUCCESS ) {
555 /* Read data into the list */
556 vcard_read_file( cardFile );
557 vcard_close_file( cardFile );
560 addrcache_mark_file( cardFile->addressCache, cardFile->path );
561 cardFile->addressCache->modified = FALSE;
562 cardFile->addressCache->dataRead = TRUE;
564 /* Build address completion index */
565 addrcache_build_index( cardFile->addressCache );
568 return cardFile->retVal;
572 * Return link list of persons.
574 GList *vcard_get_list_person( VCardFile *cardFile ) {
575 g_return_val_if_fail( cardFile != NULL, NULL );
576 return addrcache_get_list_person( cardFile->addressCache );
580 * Return link list of folders. This is always NULL since there are
581 * no folders in GnomeCard.
584 GList *vcard_get_list_folder( VCardFile *cardFile ) {
585 g_return_val_if_fail( cardFile != NULL, NULL );
590 * Return link list of all persons. Note that the list contains references
591 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
592 * this will destroy the addressbook data!
593 * Return: List of items, or NULL if none.
595 GList *vcard_get_all_persons( VCardFile *cardFile ) {
596 g_return_val_if_fail( cardFile != NULL, NULL );
597 return addrcache_get_all_persons( cardFile->addressCache );
601 * Validate that all parameters specified.
602 * Return: TRUE if data is good.
604 gboolean vcard_validate( const VCardFile *cardFile ) {
608 g_return_val_if_fail( cardFile != NULL, FALSE );
611 if( cardFile->path ) {
612 if( strlen( cardFile->path ) < 1 ) retVal = FALSE;
617 name = addrcache_get_name( cardFile->addressCache );
619 if( strlen( name ) < 1 ) retVal = FALSE;
627 #define WORK_BUFLEN 1024
630 * Attempt to find a valid GnomeCard file.
631 * Return: Filename, or home directory if not found. Filename should
632 * be g_free() when done.
634 gchar *vcard_find_gnomecard( void ) {
636 gchar buf[ WORK_BUFLEN ];
637 gchar str[ WORK_BUFLEN ];
642 homedir = g_get_home_dir();
643 if( ! homedir ) return NULL;
645 strcpy( str, homedir );
648 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
649 str[ len ] = G_DIR_SEPARATOR;
653 strcat( str, GNOMECARD_DIR );
654 strcat( str, G_DIR_SEPARATOR_S );
655 strcat( str, GNOMECARD_FILE );
658 if( ( fp = fopen( str, "rb" ) ) != NULL ) {
659 /* Read configuration file */
660 lenlbl = strlen( GNOMECARD_SECTION );
661 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
662 if( 0 == g_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
667 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
669 if( buf[0] == '[' ) break;
670 for( i = 0; i < lenlbl; i++ ) {
671 if( buf[i] == '=' ) {
672 if( 0 == g_strncasecmp( buf, GNOMECARD_PARAM, i ) ) {
673 fileSpec = g_strdup( buf + i + 1 );
674 g_strstrip( fileSpec );
682 if( fileSpec == NULL ) {
683 /* Use the home directory */
685 fileSpec = g_strdup( str );
692 * Attempt to read file, testing for valid vCard format.
693 * Return: TRUE if file appears to be valid format.
695 gint vcard_test_read_file( const gchar *fileSpec ) {
697 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line;
701 if( ! fileSpec ) return MGU_NO_FILE;
703 cardFile = vcard_create_path( fileSpec );
704 cardFile->retVal = MGU_SUCCESS;
705 vcard_open_file( cardFile );
706 if( cardFile->retVal == MGU_SUCCESS ) {
707 cardFile->retVal = MGU_BAD_FORMAT;
709 lines = VCARD_TEST_LINES;
712 if( ( line = vcard_get_line( cardFile ) ) == NULL ) break;
715 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
716 if( tagtemp == NULL ) {
721 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
722 if( tagvalue == NULL ) {
728 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
729 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
730 if( tagname == NULL ) {
735 if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
736 /* Quoted-Printable: could span multiple lines */
737 tagvalue = vcard_read_qp( cardFile, tagvalue );
738 vcard_unescape_qp( tagvalue );
740 if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
741 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
744 if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
745 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
746 /* vCard is complete */
747 if( haveStart ) cardFile->retVal = MGU_SUCCESS;
756 vcard_close_file( cardFile );
758 retVal = cardFile->retVal;
759 vcard_free( cardFile );