2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Functions necessary to access JPilot database files.
22 * JPilot is Copyright(c) by Judd Montgomery.
23 * Visit http://www.jpilot.org for more details.
38 #include <netinet/in.h>
40 #ifdef HAVE_LIBPISOCK_PI_ARGS_H
41 # include <libpisock/pi-args.h>
42 # include <libpisock/pi-appinfo.h>
43 # include <libpisock/pi-address.h>
44 # include <libpisock/pi-version.h>
47 # include <pi-appinfo.h>
48 # include <pi-address.h>
49 # include <pi-version.h>
54 #include "addrcache.h"
57 #include "adbookbase.h"
60 #define JPILOT_DBHOME_DIR ".jpilot"
61 #define JPILOT_DBHOME_FILE "AddressDB.pdb"
62 #define PILOT_LINK_LIB_NAME "libpisock.so"
64 #define IND_LABEL_LASTNAME 0 /* Index of last name in address data */
65 #define IND_LABEL_FIRSTNAME 1 /* Index of first name in address data */
66 #define IND_PHONE_EMAIL 4 /* Index of E-Mail address in phone labels */
67 #define OFFSET_PHONE_LABEL 3 /* Offset to phone data in address data */
68 #define IND_CUSTOM_LABEL 14 /* Offset to custom label names */
69 #define NUM_CUSTOM_LABEL 4 /* Number of custom labels */
71 /* Shamelessly copied from JPilot (libplugin.h) */
73 unsigned char db_name[32];
74 unsigned char flags[2];
75 unsigned char version[2];
76 unsigned char creation_time[4];
77 unsigned char modification_time[4];
78 unsigned char backup_time[4];
79 unsigned char modification_number[4];
80 unsigned char app_info_offset[4];
81 unsigned char sort_info_offset[4];
82 unsigned char type[4];/*Database ID */
83 unsigned char creator_id[4];/*Application ID */
84 unsigned char unique_id_seed[4];
85 unsigned char next_record_list_id[4];
86 unsigned char number_of_records[2];
89 /* Shamelessly copied from JPilot (libplugin.h) */
95 time_t modification_time;
97 unsigned int modification_number;
98 unsigned int app_info_offset;
99 unsigned int sort_info_offset;
100 char type[5];/*Database ID */
101 char creator_id[5];/*Application ID */
102 char unique_id_seed[5];
103 unsigned int next_record_list_id;
104 unsigned int number_of_records;
107 /* Shamelessly copied from JPilot (libplugin.h) */
109 unsigned char Offset[4]; /*4 bytes offset from BOF to record */
110 unsigned char attrib;
111 unsigned char unique_ID[3];
114 /* Shamelessly copied from JPilot (libplugin.h) */
115 typedef struct mem_rec_header_s {
116 unsigned int rec_num;
118 unsigned int unique_id;
119 unsigned char attrib;
120 struct mem_rec_header_s *next;
123 /* Shamelessly copied from JPilot (libplugin.h) */
124 #define SPENT_PC_RECORD_BIT 256
128 MODIFIED_PALM_REC = 101L,
129 DELETED_PALM_REC = 102L,
131 DELETED_PC_REC = SPENT_PC_RECORD_BIT + 104L,
132 DELETED_DELETED_PALM_REC = SPENT_PC_RECORD_BIT + 105L
135 /* Shamelessly copied from JPilot (libplugin.h) */
138 unsigned int unique_id;
139 unsigned char attrib;
144 /* Shamelessly copied from JPilot (libplugin.h) */
146 unsigned long header_len;
147 unsigned long header_version;
148 unsigned long rec_len;
149 unsigned long unique_id;
150 unsigned long rt; /* Record Type */
151 unsigned char attrib;
159 gboolean convert_charcode = TRUE;
161 static const gchar *jpilot_get_charset(void)
163 static const gchar *charset = NULL;
166 charset = getenv("PILOT_CHARSET");
175 * Create new pilot file object.
176 * \return Initialized JPilot file object.
178 JPilotFile *jpilot_create() {
179 JPilotFile *pilotFile;
180 pilotFile = g_new0( JPilotFile, 1 );
181 pilotFile->type = ADBOOKTYPE_JPILOT;
182 pilotFile->addressCache = addrcache_create();
183 pilotFile->retVal = MGU_SUCCESS;
185 pilotFile->file = NULL;
186 pilotFile->path = NULL;
187 pilotFile->readMetadata = FALSE;
188 pilotFile->customLabels = NULL;
189 pilotFile->labelInd = NULL;
190 pilotFile->havePC3 = FALSE;
191 pilotFile->pc3ModifyTime = 0;
196 * Create new pilot file object for specified file.
197 * \param path Path to JPilot address book.
198 * \return Initialized JPilot file object.
200 JPilotFile *jpilot_create_path( const gchar *path ) {
201 JPilotFile *pilotFile;
202 pilotFile = jpilot_create();
203 jpilot_set_file( pilotFile, path );
210 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
211 g_return_if_fail( pilotFile != NULL );
212 addrcache_set_name( pilotFile->addressCache, value );
214 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
215 g_return_if_fail( pilotFile != NULL );
216 addrcache_refresh( pilotFile->addressCache );
217 pilotFile->readMetadata = FALSE;
218 pilotFile->path = mgu_replace_string( pilotFile->path, value );
220 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
221 g_return_if_fail( pilotFile != NULL );
222 pilotFile->addressCache->accessFlag = value;
225 gint jpilot_get_status( JPilotFile *pilotFile ) {
226 g_return_val_if_fail( pilotFile != NULL, -1 );
227 return pilotFile->retVal;
229 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
230 g_return_val_if_fail( pilotFile != NULL, NULL );
231 return addrcache_get_root_folder( pilotFile->addressCache );
233 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
234 g_return_val_if_fail( pilotFile != NULL, NULL );
235 return addrcache_get_name( pilotFile->addressCache );
239 * Test whether file was read.
240 * \param pilotFile JPilot control data.
241 * \return <i>TRUE</i> if file was read.
243 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
244 g_return_val_if_fail( pilotFile != NULL, FALSE );
245 return pilotFile->addressCache->dataRead;
249 * Free up custom label list.
250 * \param pilotFile JPilot control data.
252 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
255 g_return_if_fail( pilotFile != NULL );
257 /* Release custom labels */
258 mgu_free_dlist( pilotFile->customLabels );
259 pilotFile->customLabels = NULL;
261 /* Release indexes */
262 node = pilotFile->labelInd;
265 node = g_list_next( node );
267 g_list_free( pilotFile->labelInd );
268 pilotFile->labelInd = NULL;
270 /* Force a fresh read */
271 addrcache_refresh( pilotFile->addressCache );
275 * Append a custom label, representing an E-Mail address field to the
277 * \param pilotFile JPilot control data.
279 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
280 g_return_if_fail( pilotFile != NULL );
283 gchar *labelCopy = g_strdup( labelName );
284 g_strstrip( labelCopy );
285 if( *labelCopy == '\0' ) {
289 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
290 /* Force a fresh read */
291 addrcache_refresh( pilotFile->addressCache );
297 * Get list of custom labels.
298 * \param pilotFile JPilot control data.
299 * \return List of labels. Must use g_free() when done.
301 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
302 GList *retVal = NULL;
305 g_return_val_if_fail( pilotFile != NULL, NULL );
307 node = pilotFile->customLabels;
309 retVal = g_list_append( retVal, g_strdup( node->data ) );
310 node = g_list_next( node );
316 * Return filespec of PC3 file corresponding to JPilot PDB file.
317 * \param pilotFile JPilot control data.
318 * \return File specification; should be g_free() when done.
320 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
324 if( pilotFile == NULL ) return NULL;
325 if( pilotFile->path == NULL ) return NULL;
327 fileSpec = g_strdup( pilotFile->path );
328 len = strlen( fileSpec );
331 for( i = len; i > 0; i-- ) {
332 if( *(fileSpec + i) == '.' ) {
339 if( len - pos == 3 ) {
340 *r++ = 'p'; *r++ = 'c'; *r = '3';
349 * Save PC3 file time to cache.
350 * \param pilotFile JPilot control data.
351 * \return <i>TRUE</i> if time marked.
353 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
354 gboolean retVal = FALSE;
355 struct stat filestat;
358 /* Mark PDB file cache */
359 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
361 /* Now mark PC3 file */
362 pilotFile->havePC3 = FALSE;
363 pilotFile->pc3ModifyTime = 0;
364 pcFile = jpilot_get_pc3_file( pilotFile );
365 if( pcFile == NULL ) return retVal;
366 if( 0 == stat( pcFile, &filestat ) ) {
367 pilotFile->havePC3 = TRUE;
368 pilotFile->pc3ModifyTime = filestat.st_mtime;
376 * Check whether JPilot PDB or PC3 file has changed by comparing
378 * \param pilotFile JPilot control data.
379 * \return <i>TRUE</i> if file has changed.
381 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
382 gboolean retVal = TRUE;
383 struct stat filestat;
386 /* Check main file */
387 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
391 if( ! pilotFile->havePC3 ) return FALSE;
392 pcFile = jpilot_get_pc3_file( pilotFile );
393 if( pcFile == NULL ) return FALSE;
395 if( 0 == stat( pcFile, &filestat ) ) {
396 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
403 * Test whether file was modified since last access.
404 * Return: TRUE if file was modified.
406 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
407 g_return_val_if_fail( pilotFile != NULL, FALSE );
408 pilotFile->addressCache->modified = jpilot_check_files( pilotFile );
409 return pilotFile->addressCache->modified;
411 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
412 g_return_val_if_fail( pilotFile != NULL, FALSE );
413 return pilotFile->addressCache->accessFlag;
417 * Free up pilot file object by releasing internal memory.
418 * \param pilotFile JPilot control data.
420 void jpilot_free( JPilotFile *pilotFile ) {
421 g_return_if_fail( pilotFile != NULL );
423 /* Release custom labels */
424 jpilot_clear_custom_labels( pilotFile );
427 addrcache_clear( pilotFile->addressCache );
428 addrcache_free( pilotFile->addressCache );
430 /* Free internal stuff */
431 g_free( pilotFile->path );
433 pilotFile->file = NULL;
434 pilotFile->path = NULL;
435 pilotFile->readMetadata = FALSE;
436 pilotFile->havePC3 = FALSE;
437 pilotFile->pc3ModifyTime = 0;
439 pilotFile->type = ADBOOKTYPE_NONE;
440 pilotFile->addressCache = NULL;
441 pilotFile->retVal = MGU_SUCCESS;
443 /* Now release file object */
447 /* Shamelessly copied from JPilot (libplugin.c) */
448 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
451 for (i=0;i<num_bytes;i++) {
457 /* Shamelessly copied from JPilot (utils.c) */
458 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
459 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
460 #define PILOT_TIME_DELTA (unsigned)(2082844800)
462 static time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
463 return (time_t)(raw_time - PILOT_TIME_DELTA);
466 /* Shamelessly copied from JPilot (libplugin.c) */
467 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
470 strncpy(dbh->db_name, rdbh->db_name, 31);
471 dbh->db_name[31] = '\0';
472 dbh->flags = bytes_to_bin(rdbh->flags, 2);
473 dbh->version = bytes_to_bin(rdbh->version, 2);
474 temp = bytes_to_bin(rdbh->creation_time, 4);
475 dbh->creation_time = pilot_time_to_unix_time(temp);
476 temp = bytes_to_bin(rdbh->modification_time, 4);
477 dbh->modification_time = pilot_time_to_unix_time(temp);
478 temp = bytes_to_bin(rdbh->backup_time, 4);
479 dbh->backup_time = pilot_time_to_unix_time(temp);
480 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
481 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
482 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
483 strncpy(dbh->type, rdbh->type, 4);
485 strncpy(dbh->creator_id, rdbh->creator_id, 4);
486 dbh->creator_id[4] = '\0';
487 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
488 dbh->unique_id_seed[4] = '\0';
489 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
490 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
494 /* Shamelessly copied from JPilot (libplugin.c) */
495 /* returns 1 if found */
497 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
498 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
500 mem_rec_header *temp_mem_rh;
501 unsigned char found = 0;
502 unsigned long found_at;
505 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
506 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
507 found_at = temp_mem_rh->offset;
508 /* *attrib = temp_mem_rh->attrib; */
509 /* *unique_id = temp_mem_rh->unique_id; */
511 if ((temp_mem_rh->offset == fpos)) {
513 *attrib = temp_mem_rh->attrib;
514 *unique_id = temp_mem_rh->unique_id;
517 *next_offset = found_at;
521 /* Shamelessly copied from JPilot (libplugin.c) */
522 static void free_mem_rec_header(mem_rec_header **mem_rh) {
523 mem_rec_header *h, *next_h;
524 for (h=*mem_rh; h; h=next_h) {
531 /* Shamelessly copied from JPilot (libplugin.c) */
533 static int jpilot_get_info_size( FILE *in, int *size ) {
539 fseek(in, 0, SEEK_SET);
540 fread(&rdbh, sizeof(RawDBHeader), 1, in);
545 raw_header_to_header(&rdbh, &dbh);
546 if (dbh.app_info_offset==0) {
550 if (dbh.sort_info_offset!=0) {
551 *size = dbh.sort_info_offset - dbh.app_info_offset;
554 if (dbh.number_of_records==0) {
555 fseek(in, 0, SEEK_END);
556 *size=ftell(in) - dbh.app_info_offset;
560 fread(&rh, sizeof(record_header), 1, in);
561 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
562 *size=offset - dbh.app_info_offset;
568 * Read address file into address list. Based on JPilot's
569 * libplugin.c (jp_get_app_info)
571 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
574 unsigned int rec_size;
578 if( ( !buf_size ) || ( ! buf ) ) {
585 if( pilotFile->path ) {
586 in = g_fopen( pilotFile->path, "rb" );
588 return MGU_OPEN_FILE;
595 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
599 return MGU_ERROR_READ;
607 /* Convert header into something recognizable */
608 raw_header_to_header(&rdbh, &dbh);
610 num = jpilot_get_info_size(in, &rec_size);
613 return MGU_ERROR_READ;
616 fseek(in, dbh.app_info_offset, SEEK_SET);
617 *buf = ( char * ) malloc(rec_size);
620 return MGU_OO_MEMORY;
622 num = fread(*buf, rec_size, 1, in);
627 return MGU_ERROR_READ;
632 *buf_size = rec_size;
637 /* Shamelessly copied from JPilot (libplugin.c) */
638 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
644 memcpy(&l, p, sizeof(l));
645 header->header_len=ntohl(l);
648 memcpy(&l, p, sizeof(l));
649 header->header_version=ntohl(l);
652 memcpy(&l, p, sizeof(l));
653 header->rec_len=ntohl(l);
656 memcpy(&l, p, sizeof(l));
657 header->unique_id=ntohl(l);
660 memcpy(&l, p, sizeof(l));
664 memcpy(&(header->attrib), p, sizeof(unsigned char));
665 p+=sizeof(unsigned char);
670 /* Shamelessly copied from JPilot (libplugin.c) */
671 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
674 unsigned char packed_header[256];
677 num = fread(&l, sizeof(l), 1, pc_in);
684 memcpy(packed_header, &l, sizeof(l));
689 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
696 unpack_header(header, packed_header);
701 * Read next record from PC3 file. Based on JPilot function
702 * <code>pc_read_next_rec()</code> (libplugin.c)
704 * \param in File handle.
705 * \param br Record buffer.
706 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
709 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
710 PC3RecordHeader header;
717 num = read_header( in, &header );
720 return MGU_ERROR_READ;
726 rec_len = header.rec_len;
727 record = malloc( rec_len );
729 return MGU_OO_MEMORY;
731 num = fread( record, rec_len, 1, in );
735 return MGU_ERROR_READ;
739 br->unique_id = header.unique_id;
740 br->attrib = header.attrib;
748 * Read address file into a linked list. Based on JPilot function
749 * <code>jp_read_DB_files()</code> (from libplugin.c)
751 * \param pilotFile JPilot control data.
752 * \param records Pointer to linked list of records read.
753 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
756 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
760 int num_records, recs_returned, i, num, r;
761 unsigned int offset, prev_offset, next_offset, rec_size;
763 long fpos; /*file position indicator */
764 unsigned char attrib;
765 unsigned int unique_id;
766 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
773 mem_rh = last_mem_rh = NULL;
777 if( pilotFile->path == NULL ) {
781 in = g_fopen( pilotFile->path, "rb" );
783 return MGU_OPEN_FILE;
786 /* Read the database header */
787 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
791 return MGU_ERROR_READ;
798 raw_header_to_header( &rdbh, &dbh );
800 /* Read each record entry header */
801 num_records = dbh.number_of_records;
805 for( i = 1; i < num_records + 1; i++ ) {
806 num = fread( &rh, sizeof( record_header ), 1, in );
818 ( ( rh.Offset[0] * 256 + rh.Offset[1] ) * 256
819 + rh.Offset[2] ) * 256
821 if( offset < prev_offset ) {
824 prev_offset = offset;
825 temp_mem_rh = ( mem_rec_header * ) malloc( sizeof( mem_rec_header ) );
826 if( ! temp_mem_rh ) {
829 temp_mem_rh->next = NULL;
830 temp_mem_rh->rec_num = i;
831 temp_mem_rh->offset = offset;
832 temp_mem_rh->attrib = rh.attrib;
833 temp_mem_rh->unique_id =
834 ( rh.unique_ID[0] * 256 + rh.unique_ID[1] ) * 256
836 if( mem_rh == NULL ) {
837 mem_rh = temp_mem_rh;
838 last_mem_rh = temp_mem_rh;
841 last_mem_rh->next = temp_mem_rh;
842 last_mem_rh = temp_mem_rh;
846 temp_mem_rh = mem_rh;
851 mem_rh, 0, &next_offset, &attrib, &unique_id );
855 next_offset = mem_rh->offset;
856 attrib = mem_rh->attrib;
857 unique_id = mem_rh->unique_id;
860 fseek( in, next_offset, SEEK_SET );
861 while( ! feof( in ) ) {
865 mem_rh, fpos, &next_offset, &attrib,
868 next_offset = 0xFFFFFF;
870 attrib = temp_mem_rh->attrib;
871 unique_id = temp_mem_rh->unique_id;
872 if ( temp_mem_rh->next ) {
873 temp_mem_rh = temp_mem_rh->next;
874 next_offset = temp_mem_rh->offset;
878 rec_size = next_offset - fpos;
879 buf = malloc( rec_size );
881 num = fread( buf, rec_size, 1, in );
889 temp_br = malloc( sizeof( buf_rec ) );
894 temp_br->rt = PALM_REC;
895 temp_br->unique_id = unique_id;
896 temp_br->attrib = attrib;
898 temp_br->size = rec_size;
900 *records = g_list_append( *records, temp_br );
906 free_mem_rec_header( &mem_rh );
908 /* Read the PC3 file, if present */
909 pcFile = jpilot_get_pc3_file( pilotFile );
910 if( pcFile == NULL ) return MGU_SUCCESS;
911 pc_in = g_fopen( pcFile, "rb");
914 if( pc_in == NULL ) {
918 while( ! feof( pc_in ) ) {
921 temp_br = malloc( sizeof( buf_rec ) );
925 r = jpilot_read_next_pc( pc_in, temp_br );
926 if( r != MGU_SUCCESS ) {
927 if( (r != MGU_EOF) && (r != MGU_ERROR_READ) ) {
928 free( temp_br->buf );
935 if( ( temp_br->rt != DELETED_PC_REC )
936 && ( temp_br->rt != DELETED_PALM_REC )
937 && ( temp_br->rt != MODIFIED_PALM_REC )
938 && ( temp_br->rt != DELETED_DELETED_PALM_REC ) )
940 *records = g_list_append( *records, temp_br );
945 if( ( temp_br->rt == DELETED_PALM_REC )
946 || ( temp_br->rt == MODIFIED_PALM_REC ) )
948 temp_list = *records;
950 while( temp_list->next ) {
951 temp_list=temp_list->next;
954 for( ; temp_list; temp_list=temp_list->prev ) {
955 if( ( ( buf_rec * )temp_list->data )->unique_id ==
956 temp_br->unique_id ) {
957 ( ( buf_rec * )temp_list->data )->rt =
964 free( temp_br->buf );
974 * Parse buffer containing multiple e-mail addresses into a linked list of
975 * addresses. Separator characters are " ,;|" and control characters. Address
976 * is only extracted if it contains an "at" (@) character.
978 * \param buf Buffer to process.
979 * \return List of strings.
981 static GList *jpilot_parse_email( gchar *buf ) {
986 gboolean valid, done;
988 valid = done = FALSE;
993 if( *p == ' ' || *p == ',' || *p == ';' || *p == '|' || *p < 32 ) {
1008 em = g_strndup( st, len );
1009 list = g_list_append( list, em );
1017 if( *p == '@' ) valid = TRUE;
1025 #define FULLNAME_BUFSIZE 256
1026 #define EMAIL_BUFSIZE 256
1029 * Process a single label entry field, parsing multiple e-mail address entries.
1031 * \param pilotFile JPilot control data.
1032 * \param labelEntry Label entry data.
1033 * \param person Person.
1035 static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPerson *person ) {
1036 gchar buffer[ EMAIL_BUFSIZE ];
1042 strcpy( buffer, labelEntry );
1043 node = list = jpilot_parse_email( buffer );
1045 email = addritem_create_item_email();
1046 addritem_email_set_address( email, node->data );
1047 if (convert_charcode) {
1048 gchar *convertBuff = NULL;
1049 convertBuff = conv_codeset_strdup( labelEntry,
1050 jpilot_get_charset(),
1053 addritem_email_set_remarks( email, convertBuff );
1054 g_free( convertBuff );
1057 addritem_email_set_remarks(email, buffer);
1060 addrcache_id_email( pilotFile->addressCache, email );
1061 addrcache_person_add_email( pilotFile->addressCache, person, email );
1062 node = g_list_next( node );
1064 mgu_free_dlist( list );
1070 * Unpack address, building new data inside cache.
1071 * \param pilotFile JPilot control data.
1072 * \param buf Record buffer.
1073 * \param folderInd Array of (category) folders to load.
1075 static void jpilot_load_address(
1076 JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] )
1078 struct Address addr;
1084 gchar fullName[ FULLNAME_BUFSIZE ];
1090 struct AddressAppInfo *ai;
1091 gchar **firstName = NULL;
1092 gchar **lastName = NULL;
1093 #if (PILOT_LINK_MAJOR > 11)
1094 pi_buffer_t *RecordBuffer;
1095 RecordBuffer = pi_buffer_new(buf->size);
1097 memcpy(RecordBuffer->data, buf->buf, buf->size);
1098 RecordBuffer->used = buf->size;
1099 if (unpack_Address(&addr, RecordBuffer, address_v1) == -1) {
1100 pi_buffer_free(RecordBuffer);
1103 pi_buffer_free(RecordBuffer);
1107 num = unpack_Address(&addr, buf->buf, buf->size);
1111 #endif /* PILOT_LINK_0_12 */
1113 addrEnt = addr.entry;
1114 attrib = buf->attrib;
1115 unique_id = buf->unique_id;
1116 cat_id = attrib & 0x0F;
1119 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1120 firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
1123 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1124 lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
1127 if( name_order == FAMILY_LAST ) {
1128 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1129 firstName ? firstName[0] : "",
1130 lastName ? lastName[0] : "" );
1133 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1134 lastName ? lastName[0] : "",
1135 firstName ? firstName[0] : "" );
1139 g_strfreev( firstName );
1142 g_strfreev( lastName );
1145 g_strstrip( fullName );
1147 if( convert_charcode ) {
1148 gchar *nameConv = NULL;
1149 nameConv = conv_codeset_strdup( fullName,
1150 jpilot_get_charset(),
1153 strncpy2( fullName, nameConv, FULLNAME_BUFSIZE );
1157 person = addritem_create_item_person();
1158 addritem_person_set_common_name( person, fullName );
1159 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1160 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1161 addrcache_id_person( pilotFile->addressCache, person );
1163 extID = g_strdup_printf( "%d", unique_id );
1164 addritem_person_set_external_id( person, extID );
1168 /* Pointer to address metadata. */
1169 ai = & pilotFile->addrInfo;
1171 /* Add entry for each email address listed under phone labels. */
1172 indPhoneLbl = addr.phoneLabel;
1173 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1176 ind = indPhoneLbl[k];
1178 * g_print( "%d : %d : %20s : %s\n", k, ind,
1179 * ai->phoneLabels[ind], addrEnt[3+k] );
1181 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1182 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1183 jpilot_parse_label( pilotFile, labelEntry, person );
1187 /* Add entry for each custom label */
1188 node = pilotFile->labelInd;
1192 ind = GPOINTER_TO_INT( node->data );
1195 * g_print( "%d : %20s : %s\n", ind, ai->labels[ind],
1198 labelEntry = addrEnt[ind];
1199 jpilot_parse_label( pilotFile, labelEntry, person );
1202 node = g_list_next( node );
1205 if( person->listEMail ) {
1206 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1207 /* Add to specified category */
1208 addrcache_folder_add_person(
1209 pilotFile->addressCache,
1210 folderInd[cat_id], person );
1213 /* Add to root folder */
1214 addrcache_add_person(
1215 pilotFile->addressCache, person );
1219 addritem_free_item_person( person );
1222 /* Free up pointer allocated inside address */
1223 free_Address( & addr );
1227 * Free up address list.
1228 * \param records List of records to free.
1230 static void jpilot_free_addrlist( GList *records ) {
1240 node = g_list_next( node );
1244 g_list_free( records );
1248 * Read metadata from file.
1249 * \param pilotFile JPilot control data.
1250 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
1253 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1255 unsigned int rec_size;
1259 g_return_val_if_fail( pilotFile != NULL, -1 );
1261 pilotFile->readMetadata = FALSE;
1262 addrcache_clear( pilotFile->addressCache );
1264 /* Read file info */
1265 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1266 if( retVal != MGU_SUCCESS ) {
1267 pilotFile->retVal = retVal;
1268 return pilotFile->retVal;
1271 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1276 pilotFile->retVal = MGU_ERROR_READ;
1277 return pilotFile->retVal;
1280 pilotFile->readMetadata = TRUE;
1281 pilotFile->retVal = MGU_SUCCESS;
1282 return pilotFile->retVal;
1286 * Setup labels and indexes from metadata.
1287 * \param pilotFile JPilot control data.
1288 * \return <i>TRUE</i> is setup successfully.
1290 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1291 gboolean retVal = FALSE;
1292 struct AddressAppInfo *ai;
1295 g_return_val_if_fail( pilotFile != NULL, -1 );
1297 /* Release indexes */
1298 node = pilotFile->labelInd;
1301 node = g_list_next( node );
1303 pilotFile->labelInd = NULL;
1305 if( pilotFile->readMetadata ) {
1306 ai = & pilotFile->addrInfo;
1307 node = pilotFile->customLabels;
1309 gchar *lbl = node->data;
1312 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1313 gchar *labelName = ai->labels[i];
1315 if( convert_charcode ) {
1316 gchar *convertBuff = NULL;
1317 convertBuff = conv_codeset_strdup( labelName,
1318 jpilot_get_charset(),
1321 labelName = convertBuff;
1325 if( g_utf8_collate( labelName, lbl ) == 0 ) {
1330 pilotFile->labelInd = g_list_append(
1331 pilotFile->labelInd, GINT_TO_POINTER(ind) );
1332 node = g_list_next( node );
1340 * Load list with character strings of custom label names. Only none blank
1342 * \param pilotFile JPilot control data.
1343 * \param labelList List of label names to load.
1344 * \return List of label names loaded. Should be freed when done.
1346 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1349 g_return_val_if_fail( pilotFile != NULL, NULL );
1351 if( pilotFile->readMetadata ) {
1352 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1353 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1354 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1356 g_strchomp( labelName );
1357 g_strchug( labelName );
1358 if( *labelName != '\0' ) {
1359 if( convert_charcode ) {
1360 gchar *convertBuff = NULL;
1361 convertBuff = conv_codeset_strdup( labelName,
1362 jpilot_get_charset(),
1365 labelName = convertBuff;
1369 labelName = g_strdup( labelName );
1371 labelList = g_list_append( labelList, labelName );
1380 * Build folder in address book for each category.
1381 * \param pilotFile JPilot control data.
1383 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1384 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1385 struct CategoryAppInfo *cat = & ai->category;
1388 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1389 ItemFolder *folder = addritem_create_item_folder();
1391 if( convert_charcode ) {
1392 gchar *convertBuff = NULL;
1393 convertBuff = conv_codeset_strdup( cat->name[i],
1394 jpilot_get_charset(),
1397 addritem_folder_set_name( folder, convertBuff );
1398 g_free( convertBuff );
1400 addritem_folder_set_name( folder, cat->name[i] );
1404 addritem_folder_set_name( folder, cat->name[i] );
1407 addrcache_id_folder( pilotFile->addressCache, folder );
1408 addrcache_add_folder( pilotFile->addressCache, folder );
1413 * Remove empty (category) folders.
1414 * \param pilotFile JPilot control data.
1416 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1422 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1426 ItemFolder *folder = node->data;
1427 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1428 if( folder->listPerson ) {
1429 /* Give name to folder */
1431 sprintf( name, "? %d", i );
1432 addritem_folder_set_name( folder, name );
1435 /* Mark for removal */
1436 remList = g_list_append( remList, folder );
1439 node = g_list_next( node );
1444 ItemFolder *folder = node->data;
1445 addrcache_remove_folder( pilotFile->addressCache, folder );
1446 node = g_list_next( node );
1448 g_list_free( remList );
1452 * Read address file into address cache.
1453 * \param pilotFile JPilot control data.
1454 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1457 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1459 GList *records = NULL;
1462 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1464 /* Read list of records from JPilot files */
1465 retVal = jpilot_read_db_files( pilotFile, &records );
1466 if( retVal != MGU_SUCCESS ) {
1467 jpilot_free_addrlist( records );
1471 /* Setup labels and category folders */
1472 jpilot_setup_labels( pilotFile );
1473 jpilot_build_category_list( pilotFile );
1475 /* Build array of pointers to categories */
1477 node = addrcache_get_list_folder( pilotFile->addressCache );
1479 if( i < JPILOT_NUM_CATEG ) {
1480 folderInd[i] = node->data;
1482 node = g_list_next( node );
1486 /* Load all addresses, free up old stuff as we go */
1490 if( ( br->rt != DELETED_PC_REC ) &&
1491 ( br->rt != DELETED_PALM_REC ) &&
1492 ( br->rt != MODIFIED_PALM_REC ) &&
1493 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1494 jpilot_load_address( pilotFile, br, folderInd );
1499 node = g_list_next( node );
1503 g_list_free( records );
1505 /* Remove empty category folders */
1506 jpilot_remove_empty( pilotFile );
1507 jpilot_mark_files( pilotFile );
1513 * Read file into list. Main entry point
1514 * \param pilotFile JPilot control data.
1515 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1518 gint jpilot_read_data( JPilotFile *pilotFile ) {
1519 const gchar *cur_locale;
1521 name_order = FAMILY_LAST;
1523 cur_locale = conv_get_current_locale();
1525 if( g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ) {
1526 name_order = FAMILY_FIRST;
1529 g_return_val_if_fail( pilotFile != NULL, -1 );
1531 pilotFile->retVal = MGU_SUCCESS;
1532 pilotFile->addressCache->accessFlag = FALSE;
1533 if( jpilot_check_files( pilotFile ) ) {
1534 addrcache_clear( pilotFile->addressCache );
1535 jpilot_read_metadata( pilotFile );
1536 if( pilotFile->retVal == MGU_SUCCESS ) {
1537 pilotFile->retVal = jpilot_read_file( pilotFile );
1538 if( pilotFile->retVal == MGU_SUCCESS ) {
1539 pilotFile->addressCache->modified = FALSE;
1540 pilotFile->addressCache->dataRead = TRUE;
1544 return pilotFile->retVal;
1548 * Return linked list of persons. This is a list of references to ItemPerson
1549 * objects. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1550 * functions... this will destroy the addressbook data!
1552 * \param pilotFile JPilot control data.
1553 * \return List of persons.
1555 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1556 g_return_val_if_fail( pilotFile != NULL, NULL );
1557 return addrcache_get_list_person( pilotFile->addressCache );
1561 * Return linked list of folders. This is a list of references to non-empty
1562 * category folders. Do <b>NOT</b> attempt to use the
1563 * <code>addrbook_free_xxx()</code> functions... this will destroy the
1566 * \param pilotFile JPilot control data.
1567 * \return List of ItemFolder objects. This should not be freed.
1569 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1570 g_return_val_if_fail( pilotFile != NULL, NULL );
1571 return addrcache_get_list_folder( pilotFile->addressCache );
1575 * Return linked list of all persons. Note that the list contains references
1576 * to items. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1577 * functions... this will destroy the addressbook data!
1579 * \param pilotFile JPilot control data.
1580 * \return List of items, or NULL if none.
1582 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1583 g_return_val_if_fail( pilotFile != NULL, NULL );
1584 return addrcache_get_all_persons( pilotFile->addressCache );
1587 #define WORK_BUFLEN 1024
1590 * Attempt to find a valid JPilot file.
1591 * \param pilotFile JPilot control data.
1592 * \return Filename, or home directory if not found, or empty string if
1593 * no home. Filename should be <code>g_free()</code> when done.
1595 gchar *jpilot_find_pilotdb( void ) {
1596 const gchar *homedir;
1597 gchar str[ WORK_BUFLEN ];
1601 homedir = get_home_dir();
1602 if( ! homedir ) return g_strdup( "" );
1604 strcpy( str, homedir );
1605 len = strlen( str );
1607 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1608 str[ len ] = G_DIR_SEPARATOR;
1609 str[ ++len ] = '\0';
1612 strcat( str, JPILOT_DBHOME_DIR );
1613 strcat( str, G_DIR_SEPARATOR_S );
1614 strcat( str, JPILOT_DBHOME_FILE );
1616 /* Attempt to open */
1617 if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
1621 /* Truncate filename */
1624 return g_strdup( str );
1628 * Check whether label is in list of custom labels.
1629 * \param pilotFile JPilot control data.
1630 * \param labelName to test.
1631 * \return <i>TRUE</i> if found.
1633 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1637 g_return_val_if_fail( pilotFile != NULL, FALSE );
1641 node = pilotFile->customLabels;
1643 if( g_utf8_collate( labelName, ( gchar * ) node->data ) == 0 ) {
1647 node = g_list_next( node );
1654 * Test whether pilot link library installed.
1655 * \return <i>TRUE</i> if library available.
1657 gboolean jpilot_test_pilot_lib( void ) {
1661 #endif /* USE_JPILOT */