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