Statistics
| Revision:

root / src / vcard.c @ 56

History | View | Annotate | Download (20.3 kB)

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