* src/common/plugin.[ch]
[claws.git] / src / vcard.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2002 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 #include "adbookbase.h"
35
36 #define GNOMECARD_DIR     ".gnome"
37 #define GNOMECARD_FILE    "GnomeCard"
38 #define GNOMECARD_SECTION "[file]"
39 #define GNOMECARD_PARAM   "open"
40
41 #define VCARD_TEST_LINES  200
42
43 /*
44 * Create new cardfile object.
45 */
46 VCardFile *vcard_create() {
47         VCardFile *cardFile;
48         cardFile = g_new0( VCardFile, 1 );
49         cardFile->type = ADBOOKTYPE_VCARD;
50         cardFile->addressCache = addrcache_create();
51         cardFile->retVal = MGU_SUCCESS;
52
53         cardFile->file = NULL;
54         cardFile->path = NULL;
55         cardFile->bufptr = cardFile->buffer;
56
57         /* We want to use an address completion index */
58         addrcache_use_index( cardFile->addressCache, TRUE );
59
60         return cardFile;
61 }
62
63 /*
64 * Properties...
65 */
66 void vcard_set_name( VCardFile* cardFile, const gchar *value ) {
67         g_return_if_fail( cardFile != NULL );
68         addrcache_set_name( cardFile->addressCache, value );
69 }
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 );
75 }
76 void vcard_set_accessed( VCardFile *cardFile, const gboolean value ) {
77         g_return_if_fail( cardFile != NULL );
78         cardFile->addressCache->accessFlag = value;
79 }
80
81 /*
82 * Test whether file was modified since last access.
83 * Return: TRUE if file was modified.
84 */
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;
90 }
91 gboolean vcard_get_accessed( VCardFile *cardFile ) {
92         g_return_val_if_fail( cardFile != NULL, FALSE );
93         return cardFile->addressCache->accessFlag;
94 }
95
96 /*
97 * Test whether file was read.
98 * Return: TRUE if file was read.
99 */
100 gboolean vcard_get_read_flag( VCardFile *cardFile ) {
101         g_return_val_if_fail( cardFile != NULL, FALSE );
102         return cardFile->addressCache->dataRead;
103 }
104 void vcard_set_read_flag( VCardFile *cardFile, const gboolean value ) {
105         g_return_if_fail( cardFile != NULL );
106         cardFile->addressCache->dataRead = value;
107 }
108
109 /*
110 * Return status code from last file operation.
111 * Return: Status code.
112 */
113 gint vcard_get_status( VCardFile *cardFile ) {
114         g_return_val_if_fail( cardFile != NULL, -1 );
115         return cardFile->retVal;
116 }
117
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 );
121 }
122 gchar *vcard_get_name( VCardFile *cardFile ) {
123         g_return_val_if_fail( cardFile != NULL, NULL );
124         return addrcache_get_name( cardFile->addressCache );
125 }
126
127 /*
128 * Refresh internal variables to force a file read.
129 */
130 void vcard_force_refresh( VCardFile *cardFile ) {
131         addrcache_refresh( cardFile->addressCache );
132 }
133
134 /*
135 * Create new cardfile object for specified file.
136 */
137 VCardFile *vcard_create_path( const gchar *path ) {
138         VCardFile *cardFile;
139         cardFile = vcard_create();
140         vcard_set_file(cardFile, path);
141         return cardFile;
142 }
143
144 /*
145 * Free up cardfile object by releasing internal memory.
146 */
147 void vcard_free( VCardFile *cardFile ) {
148         g_return_if_fail( cardFile != NULL );
149
150         /* Close file */
151         if( cardFile->file ) fclose( cardFile->file );
152
153         /* Clear cache */
154         addrcache_free( cardFile->addressCache );
155
156         /* Free internal stuff */
157         g_free( cardFile->path );
158
159         /* Clear pointers */
160         cardFile->file = NULL;
161         cardFile->path = NULL;
162         cardFile->bufptr = NULL;
163
164         cardFile->type = ADBOOKTYPE_NONE;
165         cardFile->addressCache = NULL;
166         cardFile->retVal = MGU_SUCCESS;
167
168         /* Now release file object */
169         g_free( cardFile );
170 }
171
172 /*
173 * Display object to specified stream.
174 */
175 void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
176         g_return_if_fail( cardFile != NULL );
177
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 );
183 }
184
185 /*
186 * Open file for read.
187 * return: TRUE if file opened successfully.
188 */
189 static gint vcard_open_file( VCardFile* cardFile ) {
190         g_return_val_if_fail( cardFile != NULL, -1 );
191
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;
200                 }
201         }
202         else {
203                 /* fprintf( stderr, "file not specified\n" ); */
204                 cardFile->retVal = MGU_NO_FILE;
205                 return cardFile->retVal;
206         }
207
208         /* Setup a buffer area */
209         cardFile->buffer[0] = '\0';
210         cardFile->bufptr = cardFile->buffer;
211         cardFile->retVal = MGU_SUCCESS;
212         return cardFile->retVal;
213 }
214
215 /*
216 * Close file.
217 */
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;
222 }
223
224 /*
225 * Read line of text from file.
226 * Return: ptr to buffer where line starts.
227 */
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 )
231                         return NULL;
232                 g_strstrip( cardFile->buffer );
233                 cardFile->bufptr = cardFile->buffer;
234         }
235         return cardFile->bufptr;
236 }
237
238 /*
239 * Read line of text from file.
240 * Return: ptr to buffer where line starts.
241 */
242 static gchar *vcard_get_line( VCardFile *cardFile ) {
243         gchar buf[ VCARDBUFSIZE ];
244         gchar *start, *end;
245         gint len;
246
247         if (vcard_read_line( cardFile ) == NULL ) {
248                 buf[0] = '\0';
249                 return NULL;
250         }
251
252         /* Copy into private buffer */
253         start = cardFile->bufptr;
254         len = strlen( start );
255         end = start + len;
256         strncpy( buf, start, len );
257         buf[ len ] = '\0';
258         g_strstrip(buf);
259         cardFile->bufptr = end + 1;
260
261         /* Return a copy of buffer */   
262         return g_strdup( buf );
263 }
264
265 /*
266 * Free linked lists of character strings.
267 */
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 );
273 }
274
275 /*
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.
279 */
280 static gchar *vcard_read_qp( VCardFile *cardFile, char *tagvalue ) {
281         GSList *listQP = NULL;
282         gint len = 0;
283         gchar *line = tagvalue;
284         while( line ) {
285                 listQP = g_slist_append( listQP, line );
286                 len = strlen( line ) - 1;
287                 if( line[ len ] != '=' ) break;
288                 line[ len ] = '\0';
289                 line = vcard_get_line( cardFile );
290         }
291
292         /* Coalesce linked list into one long buffer. */
293         line = mgu_list_coalesce( listQP );
294
295         /* Clean up */
296         mgu_free_list( listQP );
297         listQP = NULL;
298         return line;
299 }
300
301 /*
302 * Parse tag name from line buffer.
303 * Return: Buffer containing the tag name, or NULL if no delimiter char found.
304 */
305 static gchar *vcard_get_tagname( char* line, gchar dlm ) {
306         gint len = 0;
307         gchar *tag = NULL;
308         gchar *lptr = line;
309
310         while( *lptr++ ) {
311                 if( *lptr == dlm ) {
312                         len = lptr - line;
313                         tag = g_strndup( line, len+1 );
314                         tag[ len ] = '\0';
315                         g_strdown( tag );
316                         return tag;
317                 }
318         }
319         return tag;
320 }
321
322 /*
323 * Parse tag value from line buffer.
324 * Return: Buffer containing the tag value. Empty string is returned if
325 * no delimiter char found.
326 */
327 static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
328         gchar *value = NULL;
329         gchar *start = NULL;
330         gchar *lptr;
331         gint len = 0;
332
333         for( lptr = line; *lptr; lptr++ ) {
334                 if( *lptr == dlm ) {
335                         if( ! start )
336                                 start = lptr + 1;
337                 }
338         }
339         if( start ) {
340                 len = lptr - start;
341                 value = g_strndup( start, len+1 );
342         }
343         else {
344                 /* Ensure that we get an empty string */
345                 value = g_strndup( "", 1 );
346         }
347         value[ len ] = '\0';
348         return value;
349 }
350
351 /*
352 * Build an address list entry and append to list of address items.
353 */
354 static void vcard_build_items(
355         VCardFile *cardFile, GSList *listName, GSList *listAddr,
356         GSList *listRem, GSList *listID )
357 {
358         GSList *nodeName = listName;
359         GSList *nodeID = listID;
360         gchar *str;
361         while( nodeName ) {
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;
368                         if( *str != '\0' ) {
369                                 ItemEMail *email = addritem_create_item_email();
370                                 addritem_email_set_address( email, str );
371                                 str = nodeRemarks->data;
372                                 if( nodeRemarks ) {
373                                         if( str ) {
374                                                 if( g_strcasecmp( str, "internet" ) != 0 ) {
375                                                         if( *str != '\0' )
376                                                                 addritem_email_set_remarks( email, str );
377                                                 }
378                                         }
379                                 }
380                                 addrcache_id_email( cardFile->addressCache, email );
381                                 addrcache_person_add_email( cardFile->addressCache, person, email );
382                         }
383                         nodeAddress = g_slist_next( nodeAddress );
384                         nodeRemarks = g_slist_next( nodeRemarks );
385                 }
386                 if( person->listEMail ) {
387                         addrcache_id_person( cardFile->addressCache, person );
388                         addrcache_add_person( cardFile->addressCache, person );
389                         if( nodeID ) {
390                                 str = nodeID->data;
391                                 addritem_person_set_external_id( person, str );
392                         }
393                 }
394                 else {
395                         addritem_free_item_person( person );
396                 }
397                 nodeName = g_slist_next( nodeName );
398                 nodeID = g_slist_next( nodeID );
399         }
400 }
401
402 /* Unescape characters in quoted-printable string. */
403 static void vcard_unescape_qp( gchar *value ) {
404         gchar *ptr, *src, *dest;
405         gint d, v = 0;
406         gchar ch;
407         gboolean gotch;
408         ptr = value;
409         while( *ptr ) {
410                 gotch = FALSE;
411                 if( *ptr == '=' ) {
412                         v = 0;
413                         ch = *(ptr + 1);
414                         if( ch ) {
415                                 if( ch > '0' && ch < '8' ) v = ch - '0';
416                         }
417                         d = -1;
418                         ch = *(ptr + 2);
419                         if( ch ) {
420                                 if( ch > '\x60' ) ch -= '\x20';
421                                 if( ch > '0' && ch < ' ' ) d = ch - '0';
422                                 d = ch - '0';
423                                 if( d > 9 ) d -= 7;
424                                 if( d > -1 && d < 16 ) {
425                                         v = ( 16 * v ) + d;
426                                         gotch = TRUE;
427                                 }
428                         }
429                 }
430                 if( gotch ) {
431                         /* Replace = with char and move down in buffer */
432                         *ptr = v;
433                         src = ptr + 3;
434                         dest = ptr + 1;
435                         while( *src ) {
436                                 *dest++ = *src++;
437                         }
438                         *dest = '\0';
439                 }
440                 ptr++;
441         }
442 }
443
444 /*
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.
453 */
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; */
458
459         for( ;; ) {
460                 gchar *line =  vcard_get_line( cardFile );
461                 if( line == NULL ) break;
462
463                 /* fprintf( stdout, "%s\n", line ); */
464
465                 /* Parse line */
466                 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
467                 if( tagtemp == NULL ) {
468                         g_free( line );
469                         continue;
470                 }
471
472                 /* fprintf( stdout, "\ttemp:  %s\n", tagtemp ); */
473                 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
474                 if( tagvalue == NULL ) {
475                         g_free( tagtemp );
476                         g_free( line );
477                         continue;
478                 }
479
480                 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
481                 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
482                 if( tagname == NULL ) {
483                         tagname = tagtemp;
484                         tagtemp = NULL;
485                 }
486
487                 /* fprintf( stdout, "\tname:  %s\n", tagname ); */
488                 /* fprintf( stdout, "\ttype:  %s\n", tagtype ); */
489                 /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
490
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 ); */
496                 }
497
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;
503                 }
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 ) );
507                 }
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 ) );
512                 }
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 ) );
516                 }
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;
525                 }
526
527                 g_free( tagname );
528                 g_free( tagtype );
529                 g_free( tagvalue );
530                 g_free( tagtemp );
531                 g_free( line );
532                 line = NULL;
533         }
534
535         /* Free lists */
536         vcard_free_lists( listName, listAddress, listRemarks, listID );
537         listName = listAddress = listRemarks = listID = NULL;
538 }
539
540 /* ============================================================================================ */
541 /*
542 * Read file into list. Main entry point
543 * Return: TRUE if file read successfully.
544 */
545 /* ============================================================================================ */
546 gint vcard_read_data( VCardFile *cardFile ) {
547         g_return_val_if_fail( cardFile != NULL, -1 );
548
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 );
558
559                         /* Mark cache */
560                         addrcache_mark_file( cardFile->addressCache, cardFile->path );
561                         cardFile->addressCache->modified = FALSE;
562                         cardFile->addressCache->dataRead = TRUE;
563
564                         /* Build address completion index */
565                         addrcache_build_index( cardFile->addressCache );
566                 }
567         }
568         return cardFile->retVal;
569 }
570
571 /*
572 * Return link list of persons.
573 */
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 );
577 }
578
579 /*
580 * Return link list of folders. This is always NULL since there are
581 * no folders in GnomeCard.
582 * Return: NULL.
583 */
584 GList *vcard_get_list_folder( VCardFile *cardFile ) {
585         g_return_val_if_fail( cardFile != NULL, NULL );
586         return NULL;
587 }
588
589 /*
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.
594 */
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 );
598 }
599
600 /*
601 * Validate that all parameters specified.
602 * Return: TRUE if data is good.
603 */
604 gboolean vcard_validate( const VCardFile *cardFile ) {
605         gboolean retVal;
606         gchar *name;
607
608         g_return_val_if_fail( cardFile != NULL, FALSE );
609
610         retVal = TRUE;
611         if( cardFile->path ) {
612                 if( strlen( cardFile->path ) < 1 ) retVal = FALSE;
613         }
614         else {
615                 retVal = FALSE;
616         }
617         name = addrcache_get_name( cardFile->addressCache );
618         if( name ) {
619                 if( strlen( name ) < 1 ) retVal = FALSE;
620         }
621         else {
622                 retVal = FALSE;
623         }
624         return retVal;
625 }
626
627 #define WORK_BUFLEN 1024
628
629 /*
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.
633 */
634 gchar *vcard_find_gnomecard( void ) {
635         gchar *homedir;
636         gchar buf[ WORK_BUFLEN ];
637         gchar str[ WORK_BUFLEN ];
638         gchar *fileSpec;
639         gint len, lenlbl, i;
640         FILE *fp;
641
642         homedir = g_get_home_dir();
643         if( ! homedir ) return NULL;
644
645         strcpy( str, homedir );
646         len = strlen( str );
647         if( len > 0 ) {
648                 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
649                         str[ len ] = G_DIR_SEPARATOR;
650                         str[ ++len ] = '\0';
651                 }
652         }
653         strcat( str, GNOMECARD_DIR );
654         strcat( str, G_DIR_SEPARATOR_S );
655         strcat( str, GNOMECARD_FILE );
656
657         fileSpec = NULL;
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 ) ) {
663                                 break;
664                         }
665                 }
666
667                 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
668                         g_strchomp( buf );
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 );
675                                         }
676                                 }
677                         }
678                 }
679                 fclose( fp );
680         }
681
682         if( fileSpec == NULL ) {
683                 /* Use the home directory */
684                 str[ len ] = '\0';
685                 fileSpec = g_strdup( str );
686         }
687
688         return fileSpec;
689 }
690
691 /*
692 * Attempt to read file, testing for valid vCard format.
693 * Return: TRUE if file appears to be valid format.
694 */
695 gint vcard_test_read_file( const gchar *fileSpec ) {
696         gboolean haveStart;
697         gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line;
698         VCardFile *cardFile;
699         gint retVal, lines;
700
701         if( ! fileSpec ) return MGU_NO_FILE;
702
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;
708                 haveStart = FALSE;
709                 lines = VCARD_TEST_LINES;
710                 while( lines > 0 ) {
711                         lines--;
712                         if( ( line =  vcard_get_line( cardFile ) ) == NULL ) break;
713
714                         /* Parse line */
715                         tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
716                         if( tagtemp == NULL ) {
717                                 g_free( line );
718                                 continue;
719                         }
720
721                         tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
722                         if( tagvalue == NULL ) {
723                                 g_free( tagtemp );
724                                 g_free( line );
725                                 continue;
726                         }
727
728                         tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
729                         tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
730                         if( tagname == NULL ) {
731                                 tagname = tagtemp;
732                                 tagtemp = NULL;
733                         }
734
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 );
739                         }
740                         if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
741                                 g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
742                                 haveStart = TRUE;
743                         }
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;
748                         }
749
750                         g_free( tagname );
751                         g_free( tagtype );
752                         g_free( tagvalue );
753                         g_free( tagtemp );
754                         g_free( line );
755                 }
756                 vcard_close_file( cardFile );
757         }
758         retVal = cardFile->retVal;
759         vcard_free( cardFile );
760         cardFile = NULL;
761         return retVal;
762 }
763
764 /*
765 * End of Source.
766 */
767