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