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