2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2003 Match Grun
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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>
46 # include <pi-appinfo.h>
47 # include <pi-address.h>
52 #include "addrcache.h"
55 #include "adbookbase.h"
58 #define JPILOT_DBHOME_DIR ".jpilot"
59 #define JPILOT_DBHOME_FILE "AddressDB.pdb"
60 #define PILOT_LINK_LIB_NAME "libpisock.so"
62 #define IND_LABEL_LASTNAME 0 /* Index of last name in address data */
63 #define IND_LABEL_FIRSTNAME 1 /* Index of first name in address data */
64 #define IND_PHONE_EMAIL 4 /* Index of E-Mail address in phone labels */
65 #define OFFSET_PHONE_LABEL 3 /* Offset to phone data in address data */
66 #define IND_CUSTOM_LABEL 14 /* Offset to custom label names */
67 #define NUM_CUSTOM_LABEL 4 /* Number of custom labels */
69 /* Shamelessly copied from JPilot (libplugin.h) */
71 unsigned char db_name[32];
72 unsigned char flags[2];
73 unsigned char version[2];
74 unsigned char creation_time[4];
75 unsigned char modification_time[4];
76 unsigned char backup_time[4];
77 unsigned char modification_number[4];
78 unsigned char app_info_offset[4];
79 unsigned char sort_info_offset[4];
80 unsigned char type[4];/*Database ID */
81 unsigned char creator_id[4];/*Application ID */
82 unsigned char unique_id_seed[4];
83 unsigned char next_record_list_id[4];
84 unsigned char number_of_records[2];
87 /* Shamelessly copied from JPilot (libplugin.h) */
93 time_t modification_time;
95 unsigned int modification_number;
96 unsigned int app_info_offset;
97 unsigned int sort_info_offset;
98 char type[5];/*Database ID */
99 char creator_id[5];/*Application ID */
100 char unique_id_seed[5];
101 unsigned int next_record_list_id;
102 unsigned int number_of_records;
105 /* Shamelessly copied from JPilot (libplugin.h) */
107 unsigned char Offset[4]; /*4 bytes offset from BOF to record */
108 unsigned char attrib;
109 unsigned char unique_ID[3];
112 /* Shamelessly copied from JPilot (libplugin.h) */
113 typedef struct mem_rec_header_s {
114 unsigned int rec_num;
116 unsigned int unique_id;
117 unsigned char attrib;
118 struct mem_rec_header_s *next;
121 /* Shamelessly copied from JPilot (libplugin.h) */
122 #define SPENT_PC_RECORD_BIT 256
126 MODIFIED_PALM_REC = 101L,
127 DELETED_PALM_REC = 102L,
129 DELETED_PC_REC = SPENT_PC_RECORD_BIT + 104L,
130 DELETED_DELETED_PALM_REC = SPENT_PC_RECORD_BIT + 105L
133 /* Shamelessly copied from JPilot (libplugin.h) */
136 unsigned int unique_id;
137 unsigned char attrib;
142 /* Shamelessly copied from JPilot (libplugin.h) */
144 unsigned long header_len;
145 unsigned long header_version;
146 unsigned long rec_len;
147 unsigned long unique_id;
148 unsigned long rt; /* Record Type */
149 unsigned char attrib;
157 gboolean convert_charcode;
160 * Create new pilot file object.
161 * \return Initialized JPilot file object.
163 JPilotFile *jpilot_create() {
164 JPilotFile *pilotFile;
165 pilotFile = g_new0( JPilotFile, 1 );
166 pilotFile->type = ADBOOKTYPE_JPILOT;
167 pilotFile->addressCache = addrcache_create();
168 pilotFile->retVal = MGU_SUCCESS;
170 pilotFile->file = NULL;
171 pilotFile->path = NULL;
172 pilotFile->readMetadata = FALSE;
173 pilotFile->customLabels = NULL;
174 pilotFile->labelInd = NULL;
175 pilotFile->havePC3 = FALSE;
176 pilotFile->pc3ModifyTime = 0;
181 * Create new pilot file object for specified file.
182 * \param path Path to JPilot address book.
183 * \return Initialized JPilot file object.
185 JPilotFile *jpilot_create_path( const gchar *path ) {
186 JPilotFile *pilotFile;
187 pilotFile = jpilot_create();
188 jpilot_set_file( pilotFile, path );
195 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
196 g_return_if_fail( pilotFile != NULL );
197 addrcache_set_name( pilotFile->addressCache, value );
199 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
200 g_return_if_fail( pilotFile != NULL );
201 addrcache_refresh( pilotFile->addressCache );
202 pilotFile->readMetadata = FALSE;
203 pilotFile->path = mgu_replace_string( pilotFile->path, value );
205 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
206 g_return_if_fail( pilotFile != NULL );
207 pilotFile->addressCache->accessFlag = value;
210 gint jpilot_get_status( JPilotFile *pilotFile ) {
211 g_return_val_if_fail( pilotFile != NULL, -1 );
212 return pilotFile->retVal;
214 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
215 g_return_val_if_fail( pilotFile != NULL, NULL );
216 return addrcache_get_root_folder( pilotFile->addressCache );
218 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
219 g_return_val_if_fail( pilotFile != NULL, NULL );
220 return addrcache_get_name( pilotFile->addressCache );
224 * Test whether file was read.
225 * \param pilotFile JPilot control data.
226 * \return <i>TRUE</i> if file was read.
228 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
229 g_return_val_if_fail( pilotFile != NULL, FALSE );
230 return pilotFile->addressCache->dataRead;
234 * Free up custom label list.
235 * \param pilotFile JPilot control data.
237 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
240 g_return_if_fail( pilotFile != NULL );
242 /* Release custom labels */
243 mgu_free_dlist( pilotFile->customLabels );
244 pilotFile->customLabels = NULL;
246 /* Release indexes */
247 node = pilotFile->labelInd;
250 node = g_list_next( node );
252 g_list_free( pilotFile->labelInd );
253 pilotFile->labelInd = NULL;
255 /* Force a fresh read */
256 addrcache_refresh( pilotFile->addressCache );
260 * Append a custom label, representing an E-Mail address field to the
262 * \param pilotFile JPilot control data.
264 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
265 g_return_if_fail( pilotFile != NULL );
268 gchar *labelCopy = g_strdup( labelName );
269 g_strstrip( labelCopy );
270 if( *labelCopy == '\0' ) {
274 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
275 /* Force a fresh read */
276 addrcache_refresh( pilotFile->addressCache );
282 * Get list of custom labels.
283 * \param pilotFile JPilot control data.
284 * \return List of labels. Must use g_free() when done.
286 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
287 GList *retVal = NULL;
290 g_return_val_if_fail( pilotFile != NULL, NULL );
292 node = pilotFile->customLabels;
294 retVal = g_list_append( retVal, g_strdup( node->data ) );
295 node = g_list_next( node );
301 * Return filespec of PC3 file corresponding to JPilot PDB file.
302 * \param pilotFile JPilot control data.
303 * \return File specification; should be g_free() when done.
305 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
309 if( pilotFile == NULL ) return NULL;
310 if( pilotFile->path == NULL ) return NULL;
312 fileSpec = g_strdup( pilotFile->path );
313 len = strlen( fileSpec );
316 for( i = len; i > 0; i-- ) {
317 if( *(fileSpec + i) == '.' ) {
324 if( len - pos == 3 ) {
325 *r++ = 'p'; *r++ = 'c'; *r = '3';
334 * Save PC3 file time to cache.
335 * \param pilotFile JPilot control data.
336 * \return <i>TRUE</i> if time marked.
338 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
339 gboolean retVal = FALSE;
340 struct stat filestat;
343 /* Mark PDB file cache */
344 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
346 /* Now mark PC3 file */
347 pilotFile->havePC3 = FALSE;
348 pilotFile->pc3ModifyTime = 0;
349 pcFile = jpilot_get_pc3_file( pilotFile );
350 if( pcFile == NULL ) return retVal;
351 if( 0 == stat( pcFile, &filestat ) ) {
352 pilotFile->havePC3 = TRUE;
353 pilotFile->pc3ModifyTime = filestat.st_mtime;
361 * Check whether JPilot PDB or PC3 file has changed by comparing
363 * \param pilotFile JPilot control data.
364 * \return <i>TRUE</i> if file has changed.
366 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
367 gboolean retVal = TRUE;
368 struct stat filestat;
371 /* Check main file */
372 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
376 if( ! pilotFile->havePC3 ) return FALSE;
377 pcFile = jpilot_get_pc3_file( pilotFile );
378 if( pcFile == NULL ) return FALSE;
380 if( 0 == stat( pcFile, &filestat ) ) {
381 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
388 * Test whether file was modified since last access.
389 * Return: TRUE if file was modified.
391 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
392 g_return_val_if_fail( pilotFile != NULL, FALSE );
393 pilotFile->addressCache->modified = jpilot_check_files( pilotFile );
394 return pilotFile->addressCache->modified;
396 void jpilot_set_modified( JPilotFile *pilotFile, const gboolean value ) {
397 g_return_if_fail( pilotFile != NULL );
398 pilotFile->addressCache->modified = value;
400 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
401 g_return_val_if_fail( pilotFile != NULL, FALSE );
402 return pilotFile->addressCache->accessFlag;
406 * Free up pilot file object by releasing internal memory.
407 * \param pilotFile JPilot control data.
409 void jpilot_free( JPilotFile *pilotFile ) {
410 g_return_if_fail( pilotFile != NULL );
412 /* Release custom labels */
413 jpilot_clear_custom_labels( pilotFile );
416 addrcache_clear( pilotFile->addressCache );
417 addrcache_free( pilotFile->addressCache );
419 /* Free internal stuff */
420 g_free( pilotFile->path );
422 pilotFile->file = NULL;
423 pilotFile->path = NULL;
424 pilotFile->readMetadata = FALSE;
425 pilotFile->havePC3 = FALSE;
426 pilotFile->pc3ModifyTime = 0;
428 pilotFile->type = ADBOOKTYPE_NONE;
429 pilotFile->addressCache = NULL;
430 pilotFile->retVal = MGU_SUCCESS;
432 /* Now release file object */
437 * Refresh internal variables to force a file read.
439 void jpilot_force_refresh( JPilotFile *pilotFile ) {
440 addrcache_refresh( pilotFile->addressCache );
444 * Print object to specified stream.
446 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
449 g_return_if_fail( pilotFile != NULL );
451 fprintf( stream, "JPilotFile:\n" );
452 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
453 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
454 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
456 node = pilotFile->customLabels;
458 fprintf( stream, " c label: %s\n", (gchar *)node->data );
459 node = g_list_next( node );
462 node = pilotFile->labelInd;
464 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
465 node = g_list_next( node );
468 addrcache_print( pilotFile->addressCache, stream );
469 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
470 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
471 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
472 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
476 * Print summary of object to specified stream.
478 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
480 g_return_if_fail( pilotFile != NULL );
481 fprintf( stream, "JPilotFile:\n" );
482 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
483 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
484 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
486 node = pilotFile->customLabels;
488 fprintf( stream, " c label: %s\n", (gchar *)node->data );
489 node = g_list_next( node );
492 node = pilotFile->labelInd;
494 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
495 node = g_list_next( node );
497 addrcache_print( pilotFile->addressCache, stream );
498 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
499 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
502 /* Shamelessly copied from JPilot (libplugin.c) */
503 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
506 for (i=0;i<num_bytes;i++) {
512 /* Shamelessly copied from JPilot (utils.c) */
513 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
514 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
515 #define PILOT_TIME_DELTA (unsigned)(2082844800)
517 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
518 return (time_t)(raw_time - PILOT_TIME_DELTA);
521 /* Shamelessly copied from JPilot (libplugin.c) */
522 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
525 strncpy(dbh->db_name, rdbh->db_name, 31);
526 dbh->db_name[31] = '\0';
527 dbh->flags = bytes_to_bin(rdbh->flags, 2);
528 dbh->version = bytes_to_bin(rdbh->version, 2);
529 temp = bytes_to_bin(rdbh->creation_time, 4);
530 dbh->creation_time = pilot_time_to_unix_time(temp);
531 temp = bytes_to_bin(rdbh->modification_time, 4);
532 dbh->modification_time = pilot_time_to_unix_time(temp);
533 temp = bytes_to_bin(rdbh->backup_time, 4);
534 dbh->backup_time = pilot_time_to_unix_time(temp);
535 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
536 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
537 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
538 strncpy(dbh->type, rdbh->type, 4);
540 strncpy(dbh->creator_id, rdbh->creator_id, 4);
541 dbh->creator_id[4] = '\0';
542 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
543 dbh->unique_id_seed[4] = '\0';
544 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
545 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
549 /* Shamelessly copied from JPilot (libplugin.c) */
550 /* returns 1 if found */
552 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
553 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
555 mem_rec_header *temp_mem_rh;
556 unsigned char found = 0;
557 unsigned long found_at;
560 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
561 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
562 found_at = temp_mem_rh->offset;
563 /* *attrib = temp_mem_rh->attrib; */
564 /* *unique_id = temp_mem_rh->unique_id; */
566 if ((temp_mem_rh->offset == fpos)) {
568 *attrib = temp_mem_rh->attrib;
569 *unique_id = temp_mem_rh->unique_id;
572 *next_offset = found_at;
576 /* Shamelessly copied from JPilot (libplugin.c) */
577 static void free_mem_rec_header(mem_rec_header **mem_rh) {
578 mem_rec_header *h, *next_h;
579 for (h=*mem_rh; h; h=next_h) {
586 /* Shamelessly copied from JPilot (libplugin.c) */
588 static int jpilot_get_info_size( FILE *in, int *size ) {
594 fseek(in, 0, SEEK_SET);
595 fread(&rdbh, sizeof(RawDBHeader), 1, in);
600 raw_header_to_header(&rdbh, &dbh);
601 if (dbh.app_info_offset==0) {
605 if (dbh.sort_info_offset!=0) {
606 *size = dbh.sort_info_offset - dbh.app_info_offset;
609 if (dbh.number_of_records==0) {
610 fseek(in, 0, SEEK_END);
611 *size=ftell(in) - dbh.app_info_offset;
615 fread(&rh, sizeof(record_header), 1, in);
616 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
617 *size=offset - dbh.app_info_offset;
623 * Read address file into address list. Based on JPilot's
624 * libplugin.c (jp_get_app_info)
626 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
629 unsigned int rec_size;
633 if( ( !buf_size ) || ( ! buf ) ) {
640 if( pilotFile->path ) {
641 in = fopen( pilotFile->path, "rb" );
643 return MGU_OPEN_FILE;
650 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
654 return MGU_ERROR_READ;
662 /* Convert header into something recognizable */
663 raw_header_to_header(&rdbh, &dbh);
665 num = jpilot_get_info_size(in, &rec_size);
668 return MGU_ERROR_READ;
671 fseek(in, dbh.app_info_offset, SEEK_SET);
672 *buf = ( char * ) malloc(rec_size);
675 return MGU_OO_MEMORY;
677 num = fread(*buf, rec_size, 1, in);
682 return MGU_ERROR_READ;
687 *buf_size = rec_size;
692 /* Shamelessly copied from JPilot (libplugin.c) */
693 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
699 memcpy(&l, p, sizeof(l));
700 header->header_len=ntohl(l);
703 memcpy(&l, p, sizeof(l));
704 header->header_version=ntohl(l);
707 memcpy(&l, p, sizeof(l));
708 header->rec_len=ntohl(l);
711 memcpy(&l, p, sizeof(l));
712 header->unique_id=ntohl(l);
715 memcpy(&l, p, sizeof(l));
719 memcpy(&(header->attrib), p, sizeof(unsigned char));
720 p+=sizeof(unsigned char);
725 /* Shamelessly copied from JPilot (libplugin.c) */
726 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
729 unsigned char packed_header[256];
732 num = fread(&l, sizeof(l), 1, pc_in);
739 memcpy(packed_header, &l, sizeof(l));
744 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
751 unpack_header(header, packed_header);
756 * Read next record from PC3 file. Based on JPilot function
757 * <code>pc_read_next_rec()</code> (libplugin.c)
759 * \param in File handle.
760 * \param br Record buffer.
761 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
764 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
765 PC3RecordHeader header;
772 num = read_header( in, &header );
775 return MGU_ERROR_READ;
781 rec_len = header.rec_len;
782 record = malloc( rec_len );
784 return MGU_OO_MEMORY;
786 num = fread( record, rec_len, 1, in );
790 return MGU_ERROR_READ;
794 br->unique_id = header.unique_id;
795 br->attrib = header.attrib;
803 * Read address file into a linked list. Based on JPilot function
804 * <code>jp_read_DB_files()</code> (from libplugin.c)
806 * \param pilotFile JPilot control data.
807 * \param records Pointer to linked list of records read.
808 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
811 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
815 int num_records, recs_returned, i, num, r;
816 unsigned int offset, prev_offset, next_offset, rec_size;
818 long fpos; /*file position indicator */
819 unsigned char attrib;
820 unsigned int unique_id;
821 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
828 mem_rh = last_mem_rh = NULL;
832 if( pilotFile->path == NULL ) {
836 in = fopen( pilotFile->path, "rb" );
838 return MGU_OPEN_FILE;
841 /* Read the database header */
842 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
846 return MGU_ERROR_READ;
853 raw_header_to_header( &rdbh, &dbh );
855 /* Read each record entry header */
856 num_records = dbh.number_of_records;
860 for( i = 1; i < num_records + 1; i++ ) {
861 num = fread( &rh, sizeof( record_header ), 1, in );
873 ( ( rh.Offset[0] * 256 + rh.Offset[1] ) * 256
874 + rh.Offset[2] ) * 256
876 if( offset < prev_offset ) {
879 prev_offset = offset;
880 temp_mem_rh = ( mem_rec_header * ) malloc( sizeof( mem_rec_header ) );
881 if( ! temp_mem_rh ) {
884 temp_mem_rh->next = NULL;
885 temp_mem_rh->rec_num = i;
886 temp_mem_rh->offset = offset;
887 temp_mem_rh->attrib = rh.attrib;
888 temp_mem_rh->unique_id =
889 ( rh.unique_ID[0] * 256 + rh.unique_ID[1] ) * 256
891 if( mem_rh == NULL ) {
892 mem_rh = temp_mem_rh;
893 last_mem_rh = temp_mem_rh;
896 last_mem_rh->next = temp_mem_rh;
897 last_mem_rh = temp_mem_rh;
901 temp_mem_rh = mem_rh;
906 mem_rh, 0, &next_offset, &attrib, &unique_id );
910 next_offset = mem_rh->offset;
911 attrib = mem_rh->attrib;
912 unique_id = mem_rh->unique_id;
915 fseek( in, next_offset, SEEK_SET );
916 while( ! feof( in ) ) {
920 mem_rh, fpos, &next_offset, &attrib,
923 next_offset = 0xFFFFFF;
925 attrib = temp_mem_rh->attrib;
926 unique_id = temp_mem_rh->unique_id;
927 if ( temp_mem_rh->next ) {
928 temp_mem_rh = temp_mem_rh->next;
929 next_offset = temp_mem_rh->offset;
933 rec_size = next_offset - fpos;
934 buf = malloc( rec_size );
936 num = fread( buf, rec_size, 1, in );
944 temp_br = malloc( sizeof( buf_rec ) );
949 temp_br->rt = PALM_REC;
950 temp_br->unique_id = unique_id;
951 temp_br->attrib = attrib;
953 temp_br->size = rec_size;
955 *records = g_list_append( *records, temp_br );
961 free_mem_rec_header( &mem_rh );
963 /* Read the PC3 file, if present */
964 pcFile = jpilot_get_pc3_file( pilotFile );
965 if( pcFile == NULL ) return MGU_SUCCESS;
966 pc_in = fopen( pcFile, "rb" );
969 if( pc_in == NULL ) {
973 while( ! feof( pc_in ) ) {
976 temp_br = malloc( sizeof( buf_rec ) );
980 r = jpilot_read_next_pc( pc_in, temp_br );
981 if( r != MGU_SUCCESS ) {
982 if( (r != MGU_EOF) && (r != MGU_ERROR_READ) ) {
983 free( temp_br->buf );
990 if( ( temp_br->rt != DELETED_PC_REC )
991 && ( temp_br->rt != DELETED_PALM_REC )
992 && ( temp_br->rt != MODIFIED_PALM_REC )
993 && ( temp_br->rt != DELETED_DELETED_PALM_REC ) )
995 *records = g_list_append( *records, temp_br );
1000 if( ( temp_br->rt == DELETED_PALM_REC )
1001 || ( temp_br->rt == MODIFIED_PALM_REC ) )
1003 temp_list = *records;
1005 while( temp_list->next ) {
1006 temp_list=temp_list->next;
1009 for( ; temp_list; temp_list=temp_list->prev ) {
1010 if( ( ( buf_rec * )temp_list->data )->unique_id ==
1011 temp_br->unique_id ) {
1012 ( ( buf_rec * )temp_list->data )->rt =
1019 free( temp_br->buf );
1029 * Parse buffer containing multiple e-mail addresses into a linked list of
1030 * addresses. Separator characters are " ,;|" and control characters. Address
1031 * is only extracted if it contains an "at" (@) character.
1033 * \param buf Buffer to process.
1034 * \return List of strings.
1036 static GList *jpilot_parse_email( gchar *buf ) {
1041 gboolean valid, done;
1043 valid = done = FALSE;
1048 if( *p == ' ' || *p == ',' || *p == ';' || *p == '|' || *p < 32 ) {
1063 em = g_strndup( st, len );
1064 list = g_list_append( list, em );
1072 if( *p == '@' ) valid = TRUE;
1080 #define FULLNAME_BUFSIZE 256
1081 #define EMAIL_BUFSIZE 256
1084 * Process a single label entry field, parsing multiple e-mail address entries.
1086 * \param pilotFile JPilot control data.
1087 * \param labelEntry Label entry data.
1088 * \param person Person.
1090 static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPerson *person ) {
1091 gchar buffer[ EMAIL_BUFSIZE ];
1097 strcpy( buffer, labelEntry );
1098 node = list = jpilot_parse_email( buffer );
1100 gchar convertBuff[JPILOT_LEN_LABEL];
1101 email = addritem_create_item_email();
1102 addritem_email_set_address( email, node->data );
1103 if (convert_charcode) {
1105 convertBuff = conv_codeset_strdup( labelEntry, CS_SHIFT_JIS, CS_INTERNAL );
1106 addritem_email_set_remarks( email, convertBuff );
1107 g_free( convertBuff );
1110 addritem_email_set_remarks(email, buffer);
1113 addrcache_id_email( pilotFile->addressCache, email );
1114 addrcache_person_add_email( pilotFile->addressCache, person, email );
1115 node = g_list_next( node );
1117 mgu_free_dlist( list );
1123 * Unpack address, building new data inside cache.
1124 * \param pilotFile JPilot control data.
1125 * \param buf Record buffer.
1126 * \param folderInd Array of (category) folders to load.
1128 static void jpilot_load_address(
1129 JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] )
1131 struct Address addr;
1137 gchar fullName[ FULLNAME_BUFSIZE ];
1143 struct AddressAppInfo *ai;
1144 gchar **firstName = NULL;
1145 gchar **lastName = NULL;
1147 /* Retrieve address */
1148 num = unpack_Address( & addr, buf->buf, buf->size );
1150 addrEnt = addr.entry;
1151 attrib = buf->attrib;
1152 unique_id = buf->unique_id;
1153 cat_id = attrib & 0x0F;
1157 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1158 firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
1161 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1162 lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
1165 if( name_order == FAMILY_LAST ) {
1166 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1167 firstName ? firstName[0] : "",
1168 lastName ? lastName[0] : "" );
1171 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1172 lastName ? lastName[0] : "",
1173 firstName ? firstName[0] : "" );
1177 g_strfreev( firstName );
1180 g_strfreev( lastName );
1183 g_strstrip( fullName );
1185 if( convert_charcode ) {
1187 nameConv = conv_codeset_strdup( fullName, CS_SHIFT_JIS, CS_INTERNAL );
1188 strncpy2( fullName, nameConv, FULLNAME_BUFSIZE );
1192 person = addritem_create_item_person();
1193 addritem_person_set_common_name( person, fullName );
1194 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1195 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1196 addrcache_id_person( pilotFile->addressCache, person );
1198 extID = g_strdup_printf( "%d", unique_id );
1199 addritem_person_set_external_id( person, extID );
1203 /* Pointer to address metadata. */
1204 ai = & pilotFile->addrInfo;
1206 /* Add entry for each email address listed under phone labels. */
1207 indPhoneLbl = addr.phoneLabel;
1208 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1211 ind = indPhoneLbl[k];
1213 * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
1214 * ai->phoneLabels[ind], addrEnt[3+k] );
1216 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1217 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1218 jpilot_parse_label( pilotFile, labelEntry, person );
1222 /* Add entry for each custom label */
1223 node = pilotFile->labelInd;
1227 ind = GPOINTER_TO_INT( node->data );
1230 * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
1233 labelEntry = addrEnt[ind];
1234 jpilot_parse_label( pilotFile, labelEntry, person );
1237 node = g_list_next( node );
1240 if( person->listEMail ) {
1241 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1242 /* Add to specified category */
1243 addrcache_folder_add_person(
1244 pilotFile->addressCache,
1245 folderInd[cat_id], person );
1248 /* Add to root folder */
1249 addrcache_add_person(
1250 pilotFile->addressCache, person );
1254 addritem_free_item_person( person );
1259 /* Free up pointer allocated inside address */
1260 free_Address( & addr );
1264 * Free up address list.
1265 * \param records List of records to free.
1267 static void jpilot_free_addrlist( GList *records ) {
1277 node = g_list_next( node );
1281 g_list_free( records );
1285 * Read metadata from file.
1286 * \param pilotFile JPilot control data.
1287 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
1290 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1292 unsigned int rec_size;
1296 g_return_val_if_fail( pilotFile != NULL, -1 );
1298 pilotFile->readMetadata = FALSE;
1299 addrcache_clear( pilotFile->addressCache );
1301 /* Read file info */
1302 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1303 if( retVal != MGU_SUCCESS ) {
1304 pilotFile->retVal = retVal;
1305 return pilotFile->retVal;
1308 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1313 pilotFile->retVal = MGU_ERROR_READ;
1314 return pilotFile->retVal;
1317 pilotFile->readMetadata = TRUE;
1318 pilotFile->retVal = MGU_SUCCESS;
1319 return pilotFile->retVal;
1323 * Setup labels and indexes from metadata.
1324 * \param pilotFile JPilot control data.
1325 * \return <i>TRUE</i> is setup successfully.
1327 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1328 gboolean retVal = FALSE;
1329 struct AddressAppInfo *ai;
1332 g_return_val_if_fail( pilotFile != NULL, -1 );
1334 /* Release indexes */
1335 node = pilotFile->labelInd;
1338 node = g_list_next( node );
1340 pilotFile->labelInd = NULL;
1342 if( pilotFile->readMetadata ) {
1343 ai = & pilotFile->addrInfo;
1344 node = pilotFile->customLabels;
1346 gchar *lbl = node->data;
1349 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1350 gchar *labelName = ai->labels[i];
1351 gchar convertBuff[ JPILOT_LEN_LABEL ];
1353 if( convert_charcode ) {
1354 labelName = conv_codeset_strdup( labelName, CS_SHIFT_JIS, CS_INTERNAL );
1355 strncpy2( convertBuff, labelName, JPILOT_LEN_LABEL );
1356 g_free( labelName );
1357 labelName = convertBuff;
1360 if( g_utf8_collate( labelName, lbl ) == 0 ) {
1365 pilotFile->labelInd = g_list_append(
1366 pilotFile->labelInd, GINT_TO_POINTER(ind) );
1367 node = g_list_next( node );
1375 * Load list with character strings of label names.
1376 * \param pilotFile JPilot control data.
1377 * \param labelList List of label names to load.
1378 * \return List of label names loaded.
1380 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
1383 g_return_val_if_fail( pilotFile != NULL, NULL );
1385 if( pilotFile->readMetadata ) {
1386 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1387 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1388 gchar *labelName = ai->labels[i];
1391 if( convert_charcode ) {
1392 labelName = conv_codeset_strdup( labelName, CS_SHIFT_JIS, CS_INTERNAL );
1395 labelName = g_strdup( labelName );
1397 labelList = g_list_append( labelList, labelName );
1400 labelList = g_list_append(
1401 labelList, g_strdup( "" ) );
1409 * Return category name for specified category ID.
1410 * \param pilotFile JPilot control data.
1411 * \param catID Category ID.
1412 * \return Category name, or empty string if not invalid ID. Name should be
1413 * <code>g_free()</code> when done.
1415 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
1416 gchar *catName = NULL;
1418 g_return_val_if_fail( pilotFile != NULL, NULL );
1420 if( pilotFile->readMetadata ) {
1421 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1422 struct CategoryAppInfo *cat = & ai->category;
1423 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
1426 catName = g_strdup( cat->name[catID] );
1429 if( ! catName ) catName = g_strdup( "" );
1434 * Load list with character strings of phone label names.
1435 * \param pilotFile JPilot control data.
1436 * \param labelList List of label names to load.
1437 * \return List of label names loaded.
1439 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
1442 g_return_val_if_fail( pilotFile != NULL, NULL );
1444 if( pilotFile->readMetadata ) {
1445 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1446 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1447 gchar *labelName = ai->phoneLabels[i];
1449 labelList = g_list_append(
1450 labelList, g_strdup( labelName ) );
1453 labelList = g_list_append(
1454 labelList, g_strdup( "" ) );
1462 * Load list with character strings of custom label names. Only none blank
1464 * \param pilotFile JPilot control data.
1465 * \param labelList List of label names to load.
1466 * \return List of label names loaded. Should be freed when done.
1468 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1471 g_return_val_if_fail( pilotFile != NULL, NULL );
1473 if( pilotFile->readMetadata ) {
1474 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1475 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1476 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1478 g_strchomp( labelName );
1479 g_strchug( labelName );
1480 if( *labelName != '\0' ) {
1481 if( convert_charcode ) {
1482 labelName = conv_codeset_strdup( labelName, CS_SHIFT_JIS, CS_INTERNAL );
1485 labelName = g_strdup( labelName );
1487 labelList = g_list_append( labelList, labelName );
1496 * Load list with character strings of category names.
1497 * \param pilotFile JPilot control data.
1498 * \return List of label names loaded. Should be freed when done.
1500 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1501 GList *catList = NULL;
1504 g_return_val_if_fail( pilotFile != NULL, NULL );
1506 if( pilotFile->readMetadata ) {
1507 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1508 struct CategoryAppInfo *cat = & ai->category;
1509 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1510 gchar *catName = cat->name[i];
1512 catList = g_list_append(
1513 catList, g_strdup( catName ) );
1516 catList = g_list_append(
1517 catList, g_strdup( "" ) );
1525 * Build folder in address book for each category.
1526 * \param pilotFile JPilot control data.
1528 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1529 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1530 struct CategoryAppInfo *cat = & ai->category;
1533 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1534 ItemFolder *folder = addritem_create_item_folder();
1536 if( convert_charcode ) {
1538 catName = conv_codeset_strdup( cat->name[i], CS_SHIFT_JIS, CS_INTERNAL );
1539 addritem_folder_set_name( folder, catName );
1543 addritem_folder_set_name( folder, cat->name[i] );
1546 addrcache_id_folder( pilotFile->addressCache, folder );
1547 addrcache_add_folder( pilotFile->addressCache, folder );
1552 * Remove empty (category) folders.
1553 * \param pilotFile JPilot control data.
1555 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1561 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1565 ItemFolder *folder = node->data;
1566 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1567 if( folder->listPerson ) {
1568 /* Give name to folder */
1570 sprintf( name, "? %d", i );
1571 addritem_folder_set_name( folder, name );
1574 /* Mark for removal */
1575 remList = g_list_append( remList, folder );
1578 node = g_list_next( node );
1583 ItemFolder *folder = node->data;
1584 addrcache_remove_folder( pilotFile->addressCache, folder );
1585 node = g_list_next( node );
1587 g_list_free( remList );
1591 * Read address file into address cache.
1592 * \param pilotFile JPilot control data.
1593 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1596 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1598 GList *records = NULL;
1601 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1603 /* Read list of records from JPilot files */
1604 retVal = jpilot_read_db_files( pilotFile, &records );
1605 if( retVal != MGU_SUCCESS ) {
1606 jpilot_free_addrlist( records );
1610 /* Setup labels and category folders */
1611 jpilot_setup_labels( pilotFile );
1612 jpilot_build_category_list( pilotFile );
1614 /* Build array of pointers to categories */
1616 node = addrcache_get_list_folder( pilotFile->addressCache );
1618 if( i < JPILOT_NUM_CATEG ) {
1619 folderInd[i] = node->data;
1621 node = g_list_next( node );
1625 /* Load all addresses, free up old stuff as we go */
1629 if( ( br->rt != DELETED_PC_REC ) &&
1630 ( br->rt != DELETED_PALM_REC ) &&
1631 ( br->rt != MODIFIED_PALM_REC ) &&
1632 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1633 jpilot_load_address( pilotFile, br, folderInd );
1638 node = g_list_next( node );
1642 g_list_free( records );
1644 /* Remove empty category folders */
1645 jpilot_remove_empty( pilotFile );
1646 jpilot_mark_files( pilotFile );
1652 * Read file into list. Main entry point
1653 * \param pilotFile JPilot control data.
1654 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1657 gint jpilot_read_data( JPilotFile *pilotFile ) {
1658 const gchar *cur_locale;
1660 name_order = FAMILY_LAST;
1661 convert_charcode = FALSE;
1663 cur_locale = conv_get_current_locale();
1665 if( g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ) {
1666 name_order = FAMILY_FIRST;
1669 if( conv_get_locale_charset() == C_EUC_JP ) {
1670 convert_charcode = TRUE;
1673 g_return_val_if_fail( pilotFile != NULL, -1 );
1675 pilotFile->retVal = MGU_SUCCESS;
1676 pilotFile->addressCache->accessFlag = FALSE;
1677 if( jpilot_check_files( pilotFile ) ) {
1678 addrcache_clear( pilotFile->addressCache );
1679 jpilot_read_metadata( pilotFile );
1680 if( pilotFile->retVal == MGU_SUCCESS ) {
1681 pilotFile->retVal = jpilot_read_file( pilotFile );
1682 if( pilotFile->retVal == MGU_SUCCESS ) {
1683 pilotFile->addressCache->modified = FALSE;
1684 pilotFile->addressCache->dataRead = TRUE;
1688 return pilotFile->retVal;
1692 * Return linked list of persons. This is a list of references to ItemPerson
1693 * objects. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1694 * functions... this will destroy the addressbook data!
1696 * \param pilotFile JPilot control data.
1697 * \return List of persons.
1699 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1700 g_return_val_if_fail( pilotFile != NULL, NULL );
1701 return addrcache_get_list_person( pilotFile->addressCache );
1705 * Return linked list of folders. This is a list of references to non-empty
1706 * category folders. Do <b>NOT</b> attempt to use the
1707 * <code>addrbook_free_xxx()</code> functions... this will destroy the
1710 * \param pilotFile JPilot control data.
1711 * \return List of ItemFolder objects. This should not be freed.
1713 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1714 g_return_val_if_fail( pilotFile != NULL, NULL );
1715 return addrcache_get_list_folder( pilotFile->addressCache );
1719 * Return linked list of all persons. Note that the list contains references
1720 * to items. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1721 * functions... this will destroy the addressbook data!
1723 * \param pilotFile JPilot control data.
1724 * \return List of items, or NULL if none.
1726 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1727 g_return_val_if_fail( pilotFile != NULL, NULL );
1728 return addrcache_get_all_persons( pilotFile->addressCache );
1732 * Validate that all parameters specified.
1733 * \param pilotFile JPilot control data.
1734 * \return <i>TRUE</i> if data is good.
1736 gboolean jpilot_validate( JPilotFile *pilotFile ) {
1740 g_return_val_if_fail( pilotFile != NULL, FALSE );
1743 if( pilotFile->path ) {
1744 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1749 name = jpilot_get_name( pilotFile );
1751 if( strlen( name ) < 1 ) retVal = FALSE;
1759 #define WORK_BUFLEN 1024
1762 * Attempt to find a valid JPilot file.
1763 * \param pilotFile JPilot control data.
1764 * \return Filename, or home directory if not found, or empty string if
1765 * no home. Filename should be <code>g_free()</code> when done.
1767 gchar *jpilot_find_pilotdb( void ) {
1768 const gchar *homedir;
1769 gchar str[ WORK_BUFLEN ];
1773 homedir = get_home_dir();
1774 if( ! homedir ) return g_strdup( "" );
1776 strcpy( str, homedir );
1777 len = strlen( str );
1779 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1780 str[ len ] = G_DIR_SEPARATOR;
1781 str[ ++len ] = '\0';
1784 strcat( str, JPILOT_DBHOME_DIR );
1785 strcat( str, G_DIR_SEPARATOR_S );
1786 strcat( str, JPILOT_DBHOME_FILE );
1788 /* Attempt to open */
1789 if( ( fp = fopen( str, "rb" ) ) != NULL ) {
1793 /* Truncate filename */
1796 return g_strdup( str );
1800 * Attempt to read file, testing for valid JPilot format.
1801 * \param fileSpec File specification to read.
1802 * \return <i>TRUE<i> if file appears to be valid format.
1804 gint jpilot_test_read_file( const gchar *fileSpec ) {
1805 JPilotFile *pilotFile;
1809 pilotFile = jpilot_create_path( fileSpec );
1810 retVal = jpilot_read_metadata( pilotFile );
1811 jpilot_free( pilotFile );
1815 retVal = MGU_NO_FILE;
1821 * Check whether label is in list of custom labels.
1822 * \param pilotFile JPilot control data.
1823 * \param labelName to test.
1824 * \return <i>TRUE</i> if found.
1826 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1830 g_return_val_if_fail( pilotFile != NULL, FALSE );
1834 node = pilotFile->customLabels;
1836 if( g_utf8_collate( labelName, ( gchar * ) node->data ) == 0 ) {
1840 node = g_list_next( node );
1847 * Test whether pilot link library installed.
1848 * \return <i>TRUE</i> if library available.
1850 gboolean jpilot_test_pilot_lib( void ) {
1854 #endif /* USE_JPILOT */