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