2007-06-17 [wwp] 2.9.2cvs68
[claws.git] / src / vcard.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 * Open file for read.
172 * return: TRUE if file opened successfully.
173 */
174 static gint vcard_open_file( VCardFile* cardFile ) {
175         g_return_val_if_fail( cardFile != NULL, -1 );
176
177         /* fprintf( stdout, "Opening file\n" ); */
178         cardFile->addressCache->dataRead = FALSE;
179         if( cardFile->path ) {
180                 cardFile->file = g_fopen( cardFile->path, "rb" );
181                 if( ! cardFile->file ) {
182                         /* fprintf( stderr, "can't open %s\n", cardFile->path ); */
183                         cardFile->retVal = MGU_OPEN_FILE;
184                         return cardFile->retVal;
185                 }
186         }
187         else {
188                 /* fprintf( stderr, "file not specified\n" ); */
189                 cardFile->retVal = MGU_NO_FILE;
190                 return cardFile->retVal;
191         }
192
193         /* Setup a buffer area */
194         cardFile->buffer[0] = '\0';
195         cardFile->bufptr = cardFile->buffer;
196         cardFile->retVal = MGU_SUCCESS;
197         return cardFile->retVal;
198 }
199
200 /*
201 * Close file.
202 */
203 static void vcard_close_file( VCardFile *cardFile ) {
204         g_return_if_fail( cardFile != NULL );
205         if( cardFile->file ) fclose( cardFile->file );
206         cardFile->file = NULL;
207 }
208
209 /*
210 * Read line of text from file.
211 * Return: ptr to buffer where line starts.
212 */
213 static gchar *vcard_read_line( VCardFile *cardFile ) {
214         while( *cardFile->bufptr == '\n' || *cardFile->bufptr == '\0' ) {
215                 if( fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
216                         return NULL;
217                 g_strstrip( cardFile->buffer );
218                 cardFile->bufptr = cardFile->buffer;
219         }
220         return cardFile->bufptr;
221 }
222
223 /*
224 * Read line of text from file.
225 * Return: ptr to buffer where line starts.
226 */
227 static gchar *vcard_get_line( VCardFile *cardFile ) {
228         gchar buf[ VCARDBUFSIZE ];
229         gchar *start, *end;
230         gint len;
231
232         if (vcard_read_line( cardFile ) == NULL ) {
233                 buf[0] = '\0';
234                 return NULL;
235         }
236
237         /* Copy into private buffer */
238         start = cardFile->bufptr;
239         len = strlen( start );
240         end = start + len;
241         strncpy( buf, start, len );
242         buf[ len ] = '\0';
243         g_strstrip(buf);
244         cardFile->bufptr = end + 1;
245
246         /* Return a copy of buffer */   
247         return g_strdup( buf );
248 }
249
250 /*
251 * Free linked lists of character strings.
252 */
253 static void vcard_free_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList* listID ) {
254         mgu_free_list( listName );
255         mgu_free_list( listAddr );
256         mgu_free_list( listRem );
257         mgu_free_list( listID );
258 }
259
260 /*
261 * Read quoted-printable text, which may span several lines into one long string.
262 * Param: cardFile - object.
263 * Param: tagvalue - will be placed into the linked list.
264 */
265 static gchar *vcard_read_qp( VCardFile *cardFile, char *tagvalue ) {
266         GSList *listQP = NULL;
267         gint len = 0;
268         gchar *line = tagvalue;
269         while( line ) {
270                 listQP = g_slist_append( listQP, line );
271                 len = strlen( line ) - 1;
272                 if( line[ len ] != '=' ) break;
273                 line[ len ] = '\0';
274                 line = vcard_get_line( cardFile );
275         }
276
277         /* Coalesce linked list into one long buffer. */
278         line = mgu_list_coalesce( listQP );
279
280         /* Clean up */
281         mgu_free_list( listQP );
282         listQP = NULL;
283         return line;
284 }
285
286 /*
287 * Parse tag name from line buffer.
288 * Return: Buffer containing the tag name, or NULL if no delimiter char found.
289 */
290 static gchar *vcard_get_tagname( char* line, gchar dlm ) {
291         gint len = 0;
292         gchar *tag = NULL;
293         gchar *lptr = line;
294
295         while( *lptr++ ) {
296                 if( *lptr == dlm ) {
297                         len = lptr - line;
298                         tag = g_strndup( line, len+1 );
299                         tag[ len ] = '\0';
300                         g_strdown( tag );
301                         return tag;
302                 }
303         }
304         return tag;
305 }
306
307 /*
308 * Parse tag value from line buffer.
309 * Return: Buffer containing the tag value. Empty string is returned if
310 * no delimiter char found.
311 */
312 static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
313         gchar *value = NULL;
314         gchar *start = NULL;
315         gchar *lptr;
316         gint len = 0;
317
318         for( lptr = line; *lptr; lptr++ ) {
319                 if( *lptr == dlm ) {
320                         if( ! start )
321                                 start = lptr + 1;
322                 }
323         }
324         if( start ) {
325                 len = lptr - start;
326                 value = g_strndup( start, len+1 );
327         }
328         else {
329                 /* Ensure that we get an empty string */
330                 value = g_strndup( "", 1 );
331         }
332         value[ len ] = '\0';
333         return value;
334 }
335
336 /*
337 * Build an address list entry and append to list of address items.
338 */
339 static void vcard_build_items(
340         VCardFile *cardFile, GSList *listName, GSList *listAddr,
341         GSList *listRem, GSList *listID )
342 {
343         GSList *nodeName = listName;
344         GSList *nodeID = listID;
345         gchar *str;
346         while( nodeName ) {
347                 GSList *nodeAddress = listAddr;
348                 GSList *nodeRemarks = listRem;
349                 ItemPerson *person = addritem_create_item_person();
350                 addritem_person_set_common_name( person, nodeName->data );
351                 while( nodeAddress ) {
352                         str = nodeAddress->data;
353                         if( *str != '\0' ) {
354                                 ItemEMail *email = addritem_create_item_email();
355                                 addritem_email_set_address( email, str );
356                                 str = nodeRemarks->data;
357                                 if( nodeRemarks ) {
358                                         if( str ) {
359                                                 if( g_utf8_collate( str, "internet" ) != 0 ) {
360                                                         if( *str != '\0' )
361                                                                 addritem_email_set_remarks( email, str );
362                                                 }
363                                         }
364                                 }
365                                 addrcache_id_email( cardFile->addressCache, email );
366                                 addrcache_person_add_email( cardFile->addressCache, person, email );
367                         }
368                         nodeAddress = g_slist_next( nodeAddress );
369                         nodeRemarks = g_slist_next( nodeRemarks );
370                 }
371                 if( person->listEMail ) {
372                         addrcache_id_person( cardFile->addressCache, person );
373                         addrcache_add_person( cardFile->addressCache, person );
374                         if( nodeID ) {
375                                 str = nodeID->data;
376                                 addritem_person_set_external_id( person, str );
377                         }
378                 }
379                 else {
380                         addritem_free_item_person( person );
381                 }
382                 nodeName = g_slist_next( nodeName );
383                 nodeID = g_slist_next( nodeID );
384         }
385 }
386
387 /* Unescape characters in quoted-printable string. */
388 static void vcard_unescape_qp( gchar *value ) {
389         gchar *ptr, *src, *dest;
390         gint d, v = 0;
391         gchar ch;
392         gboolean gotch;
393         ptr = value;
394         while( *ptr ) {
395                 gotch = FALSE;
396                 if( *ptr == '=' ) {
397                         v = 0;
398                         ch = *(ptr + 1);
399                         if( ch ) {
400                                 if( ch > '0' && ch < '8' ) v = ch - '0';
401                         }
402                         d = -1;
403                         ch = *(ptr + 2);
404                         if( ch ) {
405                                 if( ch > '\x60' ) ch -= '\x20';
406                                 if( ch > '0' && ch < ' ' ) d = ch - '0';
407                                 d = ch - '0';
408                                 if( d > 9 ) d -= 7;
409                                 if( d > -1 && d < 16 ) {
410                                         v = ( 16 * v ) + d;
411                                         gotch = TRUE;
412                                 }
413                         }
414                 }
415                 if( gotch ) {
416                         /* Replace = with char and move down in buffer */
417                         *ptr = v;
418                         src = ptr + 3;
419                         dest = ptr + 1;
420                         while( *src ) {
421                                 *dest++ = *src++;
422                         }
423                         *dest = '\0';
424                 }
425                 ptr++;
426         }
427 }
428
429 /*
430 * Read file data into root folder.
431 * Note that one vCard can have multiple E-Mail addresses (MAIL tags);
432 * these are broken out into separate address items. An address item
433 * is generated for the person identified by FN tag and each EMAIL tag.
434 * If a sub-type is included in the EMAIL entry, this will be used as
435 * the Remarks member. Also note that it is possible for one vCard
436 * entry to have multiple FN tags; this might not make sense. However,
437 * it will generate duplicate address entries for each person listed.
438 */
439 static void vcard_read_file( VCardFile *cardFile ) {
440         gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL;
441         GSList *listName = NULL, *listAddress = NULL, *listRemarks = NULL, *listID = NULL;
442         /* GSList *listQP = NULL; */
443
444         for( ;; ) {
445                 gchar *line =  vcard_get_line( cardFile );
446                 if( line == NULL ) break;
447
448                 /* fprintf( stdout, "%s\n", line ); */
449
450                 /* Parse line */
451                 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
452                 if( tagtemp == NULL ) {
453                         g_free( line );
454                         continue;
455                 }
456
457                 /* fprintf( stdout, "\ttemp:  %s\n", tagtemp ); */
458                 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
459                 if( tagvalue == NULL ) {
460                         g_free( tagtemp );
461                         g_free( line );
462                         continue;
463                 }
464
465                 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
466                 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
467                 if( tagname == NULL ) {
468                         tagname = tagtemp;
469                         tagtemp = NULL;
470                 }
471
472                 /* fprintf( stdout, "\tname:  %s\n", tagname ); */
473                 /* fprintf( stdout, "\ttype:  %s\n", tagtype ); */
474                 /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
475
476                 if( g_utf8_collate( tagtype, VCARD_TYPE_QP ) == 0 ) {
477                         /* Quoted-Printable: could span multiple lines */
478                         tagvalue = vcard_read_qp( cardFile, tagvalue );
479                         vcard_unescape_qp( tagvalue );
480                         /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
481                 }
482
483                 if( g_utf8_collate( tagname, VCARD_TAG_START ) == 0 &&
484                         g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
485                         /* fprintf( stdout, "start card\n" ); */
486                         vcard_free_lists( listName, listAddress, listRemarks, listID );
487                         listName = listAddress = listRemarks = listID = NULL;
488                 }
489                 if( g_utf8_collate( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
490                         /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
491                         listName = g_slist_append( listName, g_strdup( tagvalue ) );
492                 }
493                 if( g_utf8_collate( tagname, VCARD_TAG_EMAIL ) == 0 ) {
494                         /* fprintf( stdout, "- address: %s\n", tagvalue ); */
495                         listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
496                         listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
497                 }
498                 if( g_utf8_collate( tagname, VCARD_TAG_UID ) == 0 ) {
499                         /* fprintf( stdout, "- id: %s\n", tagvalue ); */
500                         listID = g_slist_append( listID, g_strdup( tagvalue ) );
501                 }
502                 if( g_utf8_collate( tagname, VCARD_TAG_END ) == 0 &&
503                         g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
504                         /* vCard is complete */
505                         /* fprintf( stdout, "end card\n--\n" ); */
506                         /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
507                         vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
508                         vcard_free_lists( listName, listAddress, listRemarks, listID );
509                         listName = listAddress = listRemarks = listID = NULL;
510                 }
511
512                 g_free( tagname );
513                 g_free( tagtype );
514                 g_free( tagvalue );
515                 g_free( tagtemp );
516                 g_free( line );
517                 line = NULL;
518         }
519
520         /* Free lists */
521         vcard_free_lists( listName, listAddress, listRemarks, listID );
522         listName = listAddress = listRemarks = listID = NULL;
523 }
524
525 /* ============================================================================================ */
526 /*
527 * Read file into list. Main entry point
528 * Return: TRUE if file read successfully.
529 */
530 /* ============================================================================================ */
531 gint vcard_read_data( VCardFile *cardFile ) {
532         g_return_val_if_fail( cardFile != NULL, -1 );
533
534         cardFile->retVal = MGU_SUCCESS;
535         cardFile->addressCache->accessFlag = FALSE;
536         if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
537                 addrcache_clear( cardFile->addressCache );
538                 vcard_open_file( cardFile );
539                 if( cardFile->retVal == MGU_SUCCESS ) {
540                         /* Read data into the list */
541                         vcard_read_file( cardFile );
542                         vcard_close_file( cardFile );
543
544                         /* Mark cache */
545                         addrcache_mark_file( cardFile->addressCache, cardFile->path );
546                         cardFile->addressCache->modified = FALSE;
547                         cardFile->addressCache->dataRead = TRUE;
548                 }
549         }
550         return cardFile->retVal;
551 }
552
553 /*
554 * Return link list of persons.
555 */
556 GList *vcard_get_list_person( VCardFile *cardFile ) {
557         g_return_val_if_fail( cardFile != NULL, NULL );
558         return addrcache_get_list_person( cardFile->addressCache );
559 }
560
561 /*
562 * Return link list of folders. This is always NULL since there are
563 * no folders in GnomeCard.
564 * Return: NULL.
565 */
566 GList *vcard_get_list_folder( VCardFile *cardFile ) {
567         g_return_val_if_fail( cardFile != NULL, NULL );
568         return NULL;
569 }
570
571 /*
572 * Return link list of all persons. Note that the list contains references
573 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
574 * this will destroy the addressbook data!
575 * Return: List of items, or NULL if none.
576 */
577 GList *vcard_get_all_persons( VCardFile *cardFile ) {
578         g_return_val_if_fail( cardFile != NULL, NULL );
579         return addrcache_get_all_persons( cardFile->addressCache );
580 }
581
582 /*
583 * Validate that all parameters specified.
584 * Return: TRUE if data is good.
585 */
586 gboolean vcard_validate( const VCardFile *cardFile ) {
587         gboolean retVal;
588         gchar *name;
589
590         g_return_val_if_fail( cardFile != NULL, FALSE );
591
592         retVal = TRUE;
593         if( cardFile->path ) {
594                 if( strlen( cardFile->path ) < 1 ) retVal = FALSE;
595         }
596         else {
597                 retVal = FALSE;
598         }
599         name = addrcache_get_name( cardFile->addressCache );
600         if( name ) {
601                 if( strlen( name ) < 1 ) retVal = FALSE;
602         }
603         else {
604                 retVal = FALSE;
605         }
606         return retVal;
607 }
608
609 #define WORK_BUFLEN 1024
610
611 /*
612 * Attempt to find a valid GnomeCard file.
613 * Return: Filename, or home directory if not found. Filename should
614 *       be g_free() when done.
615 */
616 gchar *vcard_find_gnomecard( void ) {
617         const gchar *homedir;
618         gchar buf[ WORK_BUFLEN ];
619         gchar str[ WORK_BUFLEN ];
620         gchar *fileSpec;
621         gint len, lenlbl, i;
622         FILE *fp;
623
624         homedir = get_home_dir();
625         if( ! homedir ) return NULL;
626
627         strcpy( str, homedir );
628         len = strlen( str );
629         if( len > 0 ) {
630                 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
631                         str[ len ] = G_DIR_SEPARATOR;
632                         str[ ++len ] = '\0';
633                 }
634         }
635         strcat( str, GNOMECARD_DIR );
636         strcat( str, G_DIR_SEPARATOR_S );
637         strcat( str, GNOMECARD_FILE );
638
639         fileSpec = NULL;
640         if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
641                 /* Read configuration file */
642                 lenlbl = strlen( GNOMECARD_SECTION );
643                 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
644                         if( 0 == g_ascii_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
645                                 break;
646                         }
647                 }
648
649                 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
650                         g_strchomp( buf );
651                         if( buf[0] == '[' ) break;
652                         for( i = 0; i < lenlbl; i++ ) {
653                                 if( buf[i] == '=' ) {
654                                         if( 0 == g_ascii_strncasecmp( buf, GNOMECARD_PARAM, i ) ) {
655                                                 fileSpec = g_strdup( buf + i + 1 );
656                                                 g_strstrip( fileSpec );
657                                         }
658                                 }
659                         }
660                 }
661                 fclose( fp );
662         }
663
664         if( fileSpec == NULL ) {
665                 /* Use the home directory */
666                 str[ len ] = '\0';
667                 fileSpec = g_strdup( str );
668         }
669
670         return fileSpec;
671 }
672
673 /*
674 * Attempt to read file, testing for valid vCard format.
675 * Return: TRUE if file appears to be valid format.
676 */
677 gint vcard_test_read_file( const gchar *fileSpec ) {
678         gboolean haveStart;
679         gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line;
680         VCardFile *cardFile;
681         gint retVal, lines;
682
683         if( ! fileSpec ) return MGU_NO_FILE;
684
685         cardFile = vcard_create_path( fileSpec );
686         cardFile->retVal = MGU_SUCCESS;
687         vcard_open_file( cardFile );
688         if( cardFile->retVal == MGU_SUCCESS ) {
689                 cardFile->retVal = MGU_BAD_FORMAT;
690                 haveStart = FALSE;
691                 lines = VCARD_TEST_LINES;
692                 while( lines > 0 ) {
693                         lines--;
694                         if( ( line =  vcard_get_line( cardFile ) ) == NULL ) break;
695
696                         /* Parse line */
697                         tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
698                         if( tagtemp == NULL ) {
699                                 g_free( line );
700                                 continue;
701                         }
702
703                         tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
704                         if( tagvalue == NULL ) {
705                                 g_free( tagtemp );
706                                 g_free( line );
707                                 continue;
708                         }
709
710                         tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
711                         tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
712                         if( tagname == NULL ) {
713                                 tagname = tagtemp;
714                                 tagtemp = NULL;
715                         }
716
717                         if( g_utf8_collate( tagtype, VCARD_TYPE_QP ) == 0 ) {
718                                 /* Quoted-Printable: could span multiple lines */
719                                 tagvalue = vcard_read_qp( cardFile, tagvalue );
720                                 vcard_unescape_qp( tagvalue );
721                         }
722                         if( g_utf8_collate( tagname, VCARD_TAG_START ) == 0 &&
723                                 g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
724                                 haveStart = TRUE;
725                         }
726                         if( g_utf8_collate( tagname, VCARD_TAG_END ) == 0 &&
727                                 g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
728                                 /* vCard is complete */
729                                 if( haveStart ) cardFile->retVal = MGU_SUCCESS;
730                         }
731
732                         g_free( tagname );
733                         g_free( tagtype );
734                         g_free( tagvalue );
735                         g_free( tagtemp );
736                         g_free( line );
737                 }
738                 vcard_close_file( cardFile );
739         }
740         retVal = cardFile->retVal;
741         vcard_free( cardFile );
742         cardFile = NULL;
743         return retVal;
744 }
745
746 /*
747 * End of Source.
748 */
749