Statistics
| Revision:

root / src / vcard.c @ 874

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