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