summaryrefslogtreecommitdiff
path: root/src/vcard.c
diff options
context:
space:
mode:
authorSimeon Simeonov2018-02-26 11:23:00 +0100
committerSimeon Simeonov2018-02-26 11:23:00 +0100
commit0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch)
treeaf916a30553c78ce9d4f2d8658656a175c5b6921 /src/vcard.c
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'src/vcard.c')
-rw-r--r--src/vcard.c777
1 files changed, 777 insertions, 0 deletions
diff --git a/src/vcard.c b/src/vcard.c
new file mode 100644
index 0000000..dd7219a
--- /dev/null
+++ b/src/vcard.c
@@ -0,0 +1,777 @@
1/*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 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 "utils.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*/
46VCardFile *vcard_create() {
47 VCardFile *cardFile;
48 cardFile = g_new0( VCardFile, 1 );
49 cardFile->name = NULL;
50 cardFile->path = NULL;
51 cardFile->file = NULL;
52 cardFile->bufptr = cardFile->buffer;
53 cardFile->addressCache = addrcache_create();
54 cardFile->retVal = MGU_SUCCESS;
55 cardFile->accessFlag = FALSE;
56 return cardFile;
57}
58
59/*
60* Properties...
61*/
62void vcard_set_name( VCardFile* cardFile, const gchar *value ) {
63 g_return_if_fail( cardFile != NULL );
64 cardFile->name = mgu_replace_string( cardFile->name, value );
65 g_strstrip( cardFile->name );
66}
67void 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}
73void 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*/
82gboolean vcard_get_modified( VCardFile *vcardFile ) {
83 g_return_val_if_fail( vcardFile != NULL, FALSE );
84 return addrcache_check_file( vcardFile->addressCache, vcardFile->path );
85}
86gboolean 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*/
95gboolean 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*/
104gint vcard_get_status( VCardFile *cardFile ) {
105 g_return_val_if_fail( cardFile != NULL, -1 );
106 return cardFile->retVal;
107}
108
109ItemFolder *vcard_get_root_folder( VCardFile *cardFile ) {
110 g_return_val_if_fail( cardFile != NULL, NULL );
111 return addrcache_get_root_folder( cardFile->addressCache );
112}
113gchar *vcard_get_name( VCardFile *cardFile ) {
114 g_return_val_if_fail( cardFile != NULL, NULL );
115 return cardFile->name;
116}
117
118/*
119* Refresh internal variables to force a file read.
120*/
121void vcard_force_refresh( VCardFile *cardFile ) {
122 addrcache_refresh( cardFile->addressCache );
123}
124
125/*
126* Create new cardfile object for specified file.
127*/
128VCardFile *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*/
138void vcard_free( VCardFile *cardFile ) {
139 g_return_if_fail( cardFile != NULL );
140
141 /* Close file */
142 if( cardFile->file ) fclose( cardFile->file );
143
144 /* Free internal stuff */
145 g_free( cardFile->name );
146 g_free( cardFile->path );
147
148 /* Clear cache */
149 addrcache_clear( cardFile->addressCache );
150 addrcache_free( cardFile->addressCache );
151
152 /* Clear pointers */
153 cardFile->file = NULL;
154 cardFile->name = NULL;
155 cardFile->path = NULL;
156 cardFile->addressCache = NULL;
157 cardFile->retVal = MGU_SUCCESS;
158 cardFile->accessFlag = FALSE;
159
160 /* Now release file object */
161 g_free( cardFile );
162
163}
164
165/*
166* Display object to specified stream.
167*/
168void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
169 g_return_if_fail( cardFile != NULL );
170
171 fprintf( stream, "VCardFile:\n" );
172 fprintf( stream, " name: '%s'\n", cardFile->name );
173 fprintf( stream, "file spec: '%s'\n", cardFile->path );
174 fprintf( stream, " ret val: %d\n", cardFile->retVal );
175 addrcache_print( cardFile->addressCache, stream );
176 addritem_print_item_folder( cardFile->addressCache->rootFolder, stream );
177}
178
179/*
180* Open file for read.
181* return: TRUE if file opened successfully.
182*/
183static gint vcard_open_file( VCardFile* cardFile ) {
184 g_return_val_if_fail( cardFile != NULL, -1 );
185
186 /* fprintf( stdout, "Opening file\n" ); */
187 cardFile->addressCache->dataRead = FALSE;
188 if( cardFile->path ) {
189 cardFile->file = g_fopen( cardFile->path, "rb" );
190 if( ! cardFile->file ) {
191 /* fprintf( stderr, "can't open %s\n", cardFile->path ); */
192 cardFile->retVal = MGU_OPEN_FILE;
193 return cardFile->retVal;
194 }
195 }
196 else {
197 /* fprintf( stderr, "file not specified\n" ); */
198 cardFile->retVal = MGU_NO_FILE;
199 return cardFile->retVal;
200 }
201
202 /* Setup a buffer area */
203 cardFile->buffer[0] = '\0';
204 cardFile->bufptr = cardFile->buffer;
205 cardFile->retVal = MGU_SUCCESS;
206 return cardFile->retVal;
207}
208
209/*
210* Close file.
211*/
212static void vcard_close_file( VCardFile *cardFile ) {
213 g_return_if_fail( cardFile != NULL );
214 if( cardFile->file ) fclose( cardFile->file );
215 cardFile->file = NULL;
216}
217
218/*
219* Read line of text from file.
220* Return: ptr to buffer where line starts.
221*/
222static gchar *vcard_read_line( VCardFile *cardFile ) {
223 while( *cardFile->bufptr == '\n' || *cardFile->bufptr == '\0' ) {
224 if( fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
225 return NULL;
226 g_strstrip( cardFile->buffer );
227 cardFile->bufptr = cardFile->buffer;
228 }
229 return cardFile->bufptr;
230}
231
232/*
233* Read line of text from file.
234* Return: ptr to buffer where line starts.
235*/
236static gchar *vcard_get_line( VCardFile *cardFile ) {
237 gchar buf[ VCARDBUFSIZE ];
238 gchar *start, *end;
239 gint len;
240
241 if (vcard_read_line( cardFile ) == NULL ) {
242 buf[0] = '\0';
243 return NULL;
244 }
245
246 /* Copy into private buffer */
247 start = cardFile->bufptr;
248 len = strlen( start );
249 end = start + len;
250 strncpy( buf, start, len );
251 buf[ len ] = '\0';
252 g_strstrip(buf);
253 cardFile->bufptr = end + 1;
254
255 /* Return a copy of buffer */
256 return g_strdup( buf );
257}
258
259/*
260* Free linked lists of character strings.
261*/
262static void vcard_free_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList* listID ) {
263 mgu_free_list( listName );
264 mgu_free_list( listAddr );
265 mgu_free_list( listRem );
266 mgu_free_list( listID );
267}
268
269/*
270* Read quoted-printable text, which may span several lines into one long string.
271* Param: cardFile - object.
272* Param: tagvalue - will be placed into the linked list.
273*/
274static gchar *vcard_read_qp( VCardFile *cardFile, gchar *tagvalue ) {
275 GSList *listQP = NULL;
276 gint len = 0;
277 gchar *line = tagvalue;
278
279 while( line ) {
280 listQP = g_slist_append( listQP, line );
281 len = strlen( line ) - 1;
282 if( len > 0 ) {
283 if( line[ len ] != '=' ) break;
284 line[ len ] = '\0';
285 }
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*/
302static gchar *vcard_get_tagname( gchar* 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*/
324static 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#if 0
349/*
350* Dump linked lists of character strings (for debug).
351*/
352static void vcard_dump_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList *listID, FILE *stream ) {
353 fprintf( stream, "dump name\n" );
354 fprintf( stream, "------------\n" );
355 mgu_print_list( listName, stdout );
356 fprintf( stream, "dump address\n" );
357 fprintf( stream, "------------\n" );
358 mgu_print_list( listAddr, stdout );
359 fprintf( stream, "dump remarks\n" );
360 fprintf( stdout, "------------\n" );
361 mgu_print_list( listRem, stdout );
362 fprintf( stream, "dump id\n" );
363 fprintf( stdout, "------------\n" );
364 mgu_print_list( listID, stdout );
365}
366#endif
367
368/*
369* Build an address list entry and append to list of address items.
370*/
371static 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 if( nodeRemarks ) {
388 str = nodeRemarks->data;
389 if( str ) {
390 if( g_ascii_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. */
418static 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 d = ch - '0';
437 if( d > 9 ) d -= 7;
438 if( d > -1 && d < 16 ) {
439 v = ( 16 * v ) + d;
440 gotch = TRUE;
441 }
442 }
443 }
444 if( gotch ) {
445 /* Replace = with char and move down in buffer */
446 *ptr = v;
447 src = ptr + 3;
448 dest = ptr + 1;
449 while( *src ) {
450 *dest++ = *src++;
451 }
452 *dest = '\0';
453 }
454 ptr++;
455 }
456}
457
458/*
459* Read file data into root folder.
460* Note that one vCard can have multiple E-Mail addresses (MAIL tags);
461* these are broken out into separate address items. An address item
462* is generated for the person identified by FN tag and each EMAIL tag.
463* If a sub-type is included in the EMAIL entry, this will be used as
464* the Remarks member. Also note that it is possible for one vCard
465* entry to have multiple FN tags; this might not make sense. However,
466* it will generate duplicate address entries for each person listed.
467*/
468static void vcard_read_file( VCardFile *cardFile ) {
469 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL;
470 GSList *listName = NULL, *listAddress = NULL, *listRemarks = NULL, *listID = NULL;
471 /* GSList *listQP = NULL; */
472
473 for( ;; ) {
474 gchar *line;
475
476 line = vcard_get_line( cardFile );
477 if( line == NULL ) break;
478
479 /* fprintf( stdout, "%s\n", line ); */
480
481 /* Parse line */
482 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
483 if( tagtemp == NULL ) {
484 g_free( line );
485 continue;
486 }
487
488 /* fprintf( stdout, "\ttemp: %s\n", tagtemp ); */
489
490 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
491 if( tagvalue == NULL ) {
492 g_free( tagtemp );
493 g_free( line );
494 continue;
495 }
496
497 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
498 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
499 if( tagname == NULL ) {
500 tagname = tagtemp;
501 tagtemp = NULL;
502 }
503
504 /* fprintf( stdout, "\tname: %s\n", tagname ); */
505 /* fprintf( stdout, "\ttype: %s\n", tagtype ); */
506 /* fprintf( stdout, "\tvalue: %s\n", tagvalue ); */
507
508 if( g_ascii_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
509 /* Quoted-Printable: could span multiple lines */
510 tagvalue = vcard_read_qp( cardFile, tagvalue );
511 vcard_unescape_qp( tagvalue );
512 /* fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
513 }
514
515 if( g_ascii_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
516 g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
517 /* fprintf( stdout, "start card\n" ); */
518 vcard_free_lists( listName, listAddress, listRemarks, listID );
519 listName = listAddress = listRemarks = listID = NULL;
520 }
521 if( g_ascii_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
522 /* fprintf( stdout, "- full name: %s\n", tagvalue ); */
523 listName = g_slist_append( listName, g_strdup( tagvalue ) );
524 }
525 if( g_ascii_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) {
526 /* fprintf( stdout, "- address: %s\n", tagvalue ); */
527 listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
528 listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
529 }
530 if( g_ascii_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) {
531 /* fprintf( stdout, "- id: %s\n", tagvalue ); */
532 listID = g_slist_append( listID, g_strdup( tagvalue ) );
533 }
534 if( g_ascii_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
535 g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
536 /* vCard is complete */
537 /* fprintf( stdout, "end card\n--\n" ); */
538 /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
539 vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
540 vcard_free_lists( listName, listAddress, listRemarks, listID );
541 listName = listAddress = listRemarks = listID = NULL;
542 }
543
544 g_free( tagname );
545 g_free( tagtype );
546 g_free( tagvalue );
547 g_free( tagtemp );
548 g_free( line );
549 }
550
551 /* Free lists */
552 vcard_free_lists( listName, listAddress, listRemarks, listID );
553 listName = listAddress = listRemarks = listID = NULL;
554}
555
556/* ============================================================================================ */
557/*
558* Read file into list. Main entry point
559* Return: TRUE if file read successfully.
560*/
561/* ============================================================================================ */
562gint vcard_read_data( VCardFile *cardFile ) {
563 g_return_val_if_fail( cardFile != NULL, -1 );
564
565 cardFile->retVal = MGU_SUCCESS;
566 cardFile->accessFlag = FALSE;
567 if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
568 addrcache_clear( cardFile->addressCache );
569 vcard_open_file( cardFile );
570 if( cardFile->retVal == MGU_SUCCESS ) {
571 /* Read data into the list */
572 vcard_read_file( cardFile );
573 vcard_close_file( cardFile );
574
575 /* Mark cache */
576 addrcache_mark_file( cardFile->addressCache, cardFile->path );
577 cardFile->addressCache->modified = FALSE;
578 cardFile->addressCache->dataRead = TRUE;
579 }
580 }
581 return cardFile->retVal;
582}
583
584/*
585* Return link list of persons.
586*/
587GList *vcard_get_list_person( VCardFile *cardFile ) {
588 g_return_val_if_fail( cardFile != NULL, NULL );
589 return addrcache_get_list_person( cardFile->addressCache );
590}
591
592/*
593* Return link list of folders. This is always NULL since there are
594* no folders in GnomeCard.
595* Return: NULL.
596*/
597GList *vcard_get_list_folder( VCardFile *cardFile ) {
598 g_return_val_if_fail( cardFile != NULL, NULL );
599 return NULL;
600}
601
602/*
603* Return link list of all persons. Note that the list contains references
604* to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
605* this will destroy the addressbook data!
606* Return: List of items, or NULL if none.
607*/
608GList *vcard_get_all_persons( VCardFile *cardFile ) {
609 g_return_val_if_fail( cardFile != NULL, NULL );
610 return addrcache_get_all_persons( cardFile->addressCache );
611}
612
613/*
614* Validate that all parameters specified.
615* Return: TRUE if data is good.
616*/
617gboolean vcard_validate( const VCardFile *cardFile ) {
618 gboolean retVal;
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 if( cardFile->name ) {
630 if( strlen( cardFile->name ) < 1 ) retVal = FALSE;
631 }
632 else {
633 retVal = FALSE;
634 }
635 return retVal;
636}
637
638#define WORK_BUFLEN 1024
639
640/*
641* Attempt to find a valid GnomeCard file.
642* Return: Filename, or home directory if not found. Filename should
643* be g_free() when done.
644*/
645gchar *vcard_find_gnomecard( void ) {
646 const gchar *homedir;
647 gchar buf[ WORK_BUFLEN ];
648 gchar str[ WORK_BUFLEN ];
649 gchar *fileSpec;
650 gint len, lenlbl, i;
651 FILE *fp;
652
653 homedir = get_home_dir();
654 if( ! homedir ) return NULL;
655
656 strcpy( str, homedir );
657 len = strlen( str );
658 if( len > 0 ) {
659 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
660 str[ len ] = G_DIR_SEPARATOR;
661 str[ ++len ] = '\0';
662 }
663 }
664 strcat( str, GNOMECARD_DIR );
665 strcat( str, G_DIR_SEPARATOR_S );
666 strcat( str, GNOMECARD_FILE );
667
668 fileSpec = NULL;
669 if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
670 /* Read configuration file */
671 lenlbl = strlen( GNOMECARD_SECTION );
672 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
673 if( 0 == g_ascii_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
674 break;
675 }
676 }
677
678 while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
679 g_strchomp( buf );
680 if( buf[0] == '[' ) break;
681 for( i = 0; i < lenlbl; i++ ) {
682 if( buf[i] == '=' ) {
683 if( 0 == g_ascii_strncasecmp( buf, GNOMECARD_PARAM, i ) ) {
684 fileSpec = g_strdup( buf + i + 1 );
685 g_strstrip( fileSpec );
686 }
687 }
688 }
689 }
690 fclose( fp );
691 }
692
693 if( fileSpec == NULL ) {
694 /* Use the home directory */
695 str[ len ] = '\0';
696 fileSpec = g_strdup( str );
697 }
698
699 return fileSpec;
700}
701
702/*
703* Attempt to read file, testing for valid vCard format.
704* Return: TRUE if file appears to be valid format.
705*/
706gint vcard_test_read_file( const gchar *fileSpec ) {
707 gboolean haveStart;
708 gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line;
709 VCardFile *cardFile;
710 gint retVal, lines;
711
712 if( ! fileSpec ) return MGU_NO_FILE;
713
714 cardFile = vcard_create_path( fileSpec );
715 cardFile->retVal = MGU_SUCCESS;
716 vcard_open_file( cardFile );
717 if( cardFile->retVal == MGU_SUCCESS ) {
718 cardFile->retVal = MGU_BAD_FORMAT;
719 haveStart = FALSE;
720 lines = VCARD_TEST_LINES;
721 while( lines > 0 ) {
722 lines--;
723 if( ( line = vcard_get_line( cardFile ) ) == NULL ) break;
724
725 /* Parse line */
726 tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
727 if( tagtemp == NULL ) {
728 g_free( line );
729 continue;
730 }
731
732 tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
733 if( tagvalue == NULL ) {
734 g_free( tagtemp );
735 g_free( line );
736 continue;
737 }
738
739 tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
740 tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
741 if( tagname == NULL ) {
742 tagname = tagtemp;
743 tagtemp = NULL;
744 }
745
746 if( g_ascii_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
747 /* Quoted-Printable: could span multiple lines */
748 tagvalue = vcard_read_qp( cardFile, tagvalue );
749 vcard_unescape_qp( tagvalue );
750 }
751 if( g_ascii_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
752 g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
753 haveStart = TRUE;
754 }
755 if( g_ascii_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
756 g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
757 /* vCard is complete */
758 if( haveStart ) cardFile->retVal = MGU_SUCCESS;
759 }
760
761 g_free( tagname );
762 g_free( tagtype );
763 g_free( tagvalue );
764 g_free( tagtemp );
765 g_free( line );
766 }
767 vcard_close_file( cardFile );
768 }
769 retVal = cardFile->retVal;
770 vcard_free( cardFile );
771 cardFile = NULL;
772 return retVal;
773}
774
775/*
776* End of Source.
777*/