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"
57 #define JPILOT_DBHOME_DIR ".jpilot"
58 #define JPILOT_DBHOME_FILE "AddressDB.pdb"
59 #define PILOT_LINK_LIB_NAME "libpisock.so"
61 #define IND_LABEL_LASTNAME 0 /* Index of last name in address data */
62 #define IND_LABEL_FIRSTNAME 1 /* Index of first name in address data */
63 #define IND_PHONE_EMAIL 4 /* Index of E-Mail address in phone labels */
64 #define OFFSET_PHONE_LABEL 3 /* Offset to phone data in address data */
65 #define IND_CUSTOM_LABEL 14 /* Offset to custom label names */
66 #define NUM_CUSTOM_LABEL 4 /* Number of custom labels */
68 /* Shamelessly copied from JPilot (libplugin.h) */
70 unsigned char db_name[32];
71 unsigned char flags[2];
72 unsigned char version[2];
73 unsigned char creation_time[4];
74 unsigned char modification_time[4];
75 unsigned char backup_time[4];
76 unsigned char modification_number[4];
77 unsigned char app_info_offset[4];
78 unsigned char sort_info_offset[4];
79 unsigned char type[4];/*Database ID */
80 unsigned char creator_id[4];/*Application ID */
81 unsigned char unique_id_seed[4];
82 unsigned char next_record_list_id[4];
83 unsigned char number_of_records[2];
86 /* Shamelessly copied from JPilot (libplugin.h) */
92 time_t modification_time;
94 unsigned int modification_number;
95 unsigned int app_info_offset;
96 unsigned int sort_info_offset;
97 char type[5];/*Database ID */
98 char creator_id[5];/*Application ID */
99 char unique_id_seed[5];
100 unsigned int next_record_list_id;
101 unsigned int number_of_records;
104 /* Shamelessly copied from JPilot (libplugin.h) */
106 unsigned char Offset[4]; /*4 bytes offset from BOF to record */
107 unsigned char attrib;
108 unsigned char unique_ID[3];
111 /* Shamelessly copied from JPilot (libplugin.h) */
112 typedef struct mem_rec_header_s {
113 unsigned int rec_num;
115 unsigned int unique_id;
116 unsigned char attrib;
117 struct mem_rec_header_s *next;
120 /* Shamelessly copied from JPilot (libplugin.h) */
121 #define SPENT_PC_RECORD_BIT 256
125 MODIFIED_PALM_REC = 101L,
126 DELETED_PALM_REC = 102L,
128 DELETED_PC_REC = SPENT_PC_RECORD_BIT + 104L,
129 DELETED_DELETED_PALM_REC = SPENT_PC_RECORD_BIT + 105L
132 /* Shamelessly copied from JPilot (libplugin.h) */
135 unsigned int unique_id;
136 unsigned char attrib;
141 /* Shamelessly copied from JPilot (libplugin.h) */
143 unsigned long header_len;
144 unsigned long header_version;
145 unsigned long rec_len;
146 unsigned long unique_id;
147 unsigned long rt; /* Record Type */
148 unsigned char attrib;
156 gboolean convert_charcode;
159 * Create new pilot file object.
160 * \return Initialized JPilot file object.
162 JPilotFile *jpilot_create() {
163 JPilotFile *pilotFile;
164 pilotFile = g_new0( JPilotFile, 1 );
165 pilotFile->type = ADBOOKTYPE_JPILOT;
166 pilotFile->addressCache = addrcache_create();
167 pilotFile->retVal = MGU_SUCCESS;
169 pilotFile->file = NULL;
170 pilotFile->path = NULL;
171 pilotFile->readMetadata = FALSE;
172 pilotFile->customLabels = NULL;
173 pilotFile->labelInd = NULL;
174 pilotFile->havePC3 = FALSE;
175 pilotFile->pc3ModifyTime = 0;
180 * Create new pilot file object for specified file.
181 * \param path Path to JPilot address book.
182 * \return Initialized JPilot file object.
184 JPilotFile *jpilot_create_path( const gchar *path ) {
185 JPilotFile *pilotFile;
186 pilotFile = jpilot_create();
187 jpilot_set_file( pilotFile, path );
194 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
195 g_return_if_fail( pilotFile != NULL );
196 addrcache_set_name( pilotFile->addressCache, value );
198 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
199 g_return_if_fail( pilotFile != NULL );
200 addrcache_refresh( pilotFile->addressCache );
201 pilotFile->readMetadata = FALSE;
202 pilotFile->path = mgu_replace_string( pilotFile->path, value );
204 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
205 g_return_if_fail( pilotFile != NULL );
206 pilotFile->addressCache->accessFlag = value;
209 gint jpilot_get_status( JPilotFile *pilotFile ) {
210 g_return_val_if_fail( pilotFile != NULL, -1 );
211 return pilotFile->retVal;
213 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
214 g_return_val_if_fail( pilotFile != NULL, NULL );
215 return addrcache_get_root_folder( pilotFile->addressCache );
217 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
218 g_return_val_if_fail( pilotFile != NULL, NULL );
219 return addrcache_get_name( pilotFile->addressCache );
223 * Test whether file was read.
224 * \param pilotFile JPilot control data.
225 * \return <i>TRUE</i> if file was read.
227 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
228 g_return_val_if_fail( pilotFile != NULL, FALSE );
229 return pilotFile->addressCache->dataRead;
233 * Free up custom label list.
234 * \param pilotFile JPilot control data.
236 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
239 g_return_if_fail( pilotFile != NULL );
241 /* Release custom labels */
242 mgu_free_dlist( pilotFile->customLabels );
243 pilotFile->customLabels = NULL;
245 /* Release indexes */
246 node = pilotFile->labelInd;
249 node = g_list_next( node );
251 g_list_free( pilotFile->labelInd );
252 pilotFile->labelInd = NULL;
254 /* Force a fresh read */
255 addrcache_refresh( pilotFile->addressCache );
259 * Append a custom label, representing an E-Mail address field to the
261 * \param pilotFile JPilot control data.
263 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
264 g_return_if_fail( pilotFile != NULL );
267 gchar *labelCopy = g_strdup( labelName );
268 g_strstrip( labelCopy );
269 if( *labelCopy == '\0' ) {
273 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
274 /* Force a fresh read */
275 addrcache_refresh( pilotFile->addressCache );
281 * Get list of custom labels.
282 * \param pilotFile JPilot control data.
283 * \return List of labels. Must use g_free() when done.
285 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
286 GList *retVal = NULL;
289 g_return_val_if_fail( pilotFile != NULL, NULL );
291 node = pilotFile->customLabels;
293 retVal = g_list_append( retVal, g_strdup( node->data ) );
294 node = g_list_next( node );
300 * Return filespec of PC3 file corresponding to JPilot PDB file.
301 * \param pilotFile JPilot control data.
302 * \return File specification; should be g_free() when done.
304 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
308 if( pilotFile == NULL ) return NULL;
309 if( pilotFile->path == NULL ) return NULL;
311 fileSpec = g_strdup( pilotFile->path );
312 len = strlen( fileSpec );
315 for( i = len; i > 0; i-- ) {
316 if( *(fileSpec + i) == '.' ) {
323 if( len - pos == 3 ) {
324 *r++ = 'p'; *r++ = 'c'; *r = '3';
333 * Save PC3 file time to cache.
334 * \param pilotFile JPilot control data.
335 * \return <i>TRUE</i> if time marked.
337 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
338 gboolean retVal = FALSE;
339 struct stat filestat;
342 /* Mark PDB file cache */
343 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
345 /* Now mark PC3 file */
346 pilotFile->havePC3 = FALSE;
347 pilotFile->pc3ModifyTime = 0;
348 pcFile = jpilot_get_pc3_file( pilotFile );
349 if( pcFile == NULL ) return retVal;
350 if( 0 == stat( pcFile, &filestat ) ) {
351 pilotFile->havePC3 = TRUE;
352 pilotFile->pc3ModifyTime = filestat.st_mtime;
360 * Check whether JPilot PDB or PC3 file has changed by comparing
362 * \param pilotFile JPilot control data.
363 * \return <i>TRUE</i> if file has changed.
365 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
366 gboolean retVal = TRUE;
367 struct stat filestat;
370 /* Check main file */
371 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
375 if( ! pilotFile->havePC3 ) return FALSE;
376 pcFile = jpilot_get_pc3_file( pilotFile );
377 if( pcFile == NULL ) return FALSE;
379 if( 0 == stat( pcFile, &filestat ) ) {
380 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
387 * Test whether file was modified since last access.
388 * Return: TRUE if file was modified.
390 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
391 g_return_val_if_fail( pilotFile != NULL, FALSE );
392 pilotFile->addressCache->modified = jpilot_check_files( pilotFile );
393 return pilotFile->addressCache->modified;
395 void jpilot_set_modified( JPilotFile *pilotFile, const gboolean value ) {
396 g_return_if_fail( pilotFile != NULL );
397 pilotFile->addressCache->modified = value;
399 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
400 g_return_val_if_fail( pilotFile != NULL, FALSE );
401 return pilotFile->addressCache->accessFlag;
405 * Free up pilot file object by releasing internal memory.
406 * \param pilotFile JPilot control data.
408 void jpilot_free( JPilotFile *pilotFile ) {
409 g_return_if_fail( pilotFile != NULL );
411 /* Release custom labels */
412 jpilot_clear_custom_labels( pilotFile );
415 addrcache_clear( pilotFile->addressCache );
416 addrcache_free( pilotFile->addressCache );
418 /* Free internal stuff */
419 g_free( pilotFile->path );
421 pilotFile->file = NULL;
422 pilotFile->path = NULL;
423 pilotFile->readMetadata = FALSE;
424 pilotFile->havePC3 = FALSE;
425 pilotFile->pc3ModifyTime = 0;
427 pilotFile->type = ADBOOKTYPE_NONE;
428 pilotFile->addressCache = NULL;
429 pilotFile->retVal = MGU_SUCCESS;
431 /* Now release file object */
436 * Refresh internal variables to force a file read.
438 void jpilot_force_refresh( JPilotFile *pilotFile ) {
439 addrcache_refresh( pilotFile->addressCache );
443 * Print object to specified stream.
445 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
448 g_return_if_fail( pilotFile != NULL );
450 fprintf( stream, "JPilotFile:\n" );
451 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
452 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
453 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
455 node = pilotFile->customLabels;
457 fprintf( stream, " c label: %s\n", (gchar *)node->data );
458 node = g_list_next( node );
461 node = pilotFile->labelInd;
463 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
464 node = g_list_next( node );
467 addrcache_print( pilotFile->addressCache, stream );
468 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
469 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
470 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
471 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
475 * Print summary of object to specified stream.
477 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
479 g_return_if_fail( pilotFile != NULL );
480 fprintf( stream, "JPilotFile:\n" );
481 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
482 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
483 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
485 node = pilotFile->customLabels;
487 fprintf( stream, " c label: %s\n", (gchar *)node->data );
488 node = g_list_next( node );
491 node = pilotFile->labelInd;
493 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
494 node = g_list_next( node );
496 addrcache_print( pilotFile->addressCache, stream );
497 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
498 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
501 /* Shamelessly copied from JPilot (libplugin.c) */
502 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
505 for (i=0;i<num_bytes;i++) {
511 /* Shamelessly copied from JPilot (utils.c) */
512 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
513 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
514 #define PILOT_TIME_DELTA (unsigned)(2082844800)
516 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
517 return (time_t)(raw_time - PILOT_TIME_DELTA);
520 /* Shamelessly copied from JPilot (libplugin.c) */
521 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
524 strncpy(dbh->db_name, rdbh->db_name, 31);
525 dbh->db_name[31] = '\0';
526 dbh->flags = bytes_to_bin(rdbh->flags, 2);
527 dbh->version = bytes_to_bin(rdbh->version, 2);
528 temp = bytes_to_bin(rdbh->creation_time, 4);
529 dbh->creation_time = pilot_time_to_unix_time(temp);
530 temp = bytes_to_bin(rdbh->modification_time, 4);
531 dbh->modification_time = pilot_time_to_unix_time(temp);
532 temp = bytes_to_bin(rdbh->backup_time, 4);
533 dbh->backup_time = pilot_time_to_unix_time(temp);
534 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
535 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
536 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
537 strncpy(dbh->type, rdbh->type, 4);
539 strncpy(dbh->creator_id, rdbh->creator_id, 4);
540 dbh->creator_id[4] = '\0';
541 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
542 dbh->unique_id_seed[4] = '\0';
543 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
544 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
548 /* Shamelessly copied from JPilot (libplugin.c) */
549 /* returns 1 if found */
551 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
552 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
554 mem_rec_header *temp_mem_rh;
555 unsigned char found = 0;
556 unsigned long found_at;
559 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
560 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
561 found_at = temp_mem_rh->offset;
562 /* *attrib = temp_mem_rh->attrib; */
563 /* *unique_id = temp_mem_rh->unique_id; */
565 if ((temp_mem_rh->offset == fpos)) {
567 *attrib = temp_mem_rh->attrib;
568 *unique_id = temp_mem_rh->unique_id;
571 *next_offset = found_at;
575 /* Shamelessly copied from JPilot (libplugin.c) */
576 static void free_mem_rec_header(mem_rec_header **mem_rh) {
577 mem_rec_header *h, *next_h;
578 for (h=*mem_rh; h; h=next_h) {
585 /* Shamelessly copied from JPilot (libplugin.c) */
587 static int jpilot_get_info_size( FILE *in, int *size ) {
593 fseek(in, 0, SEEK_SET);
594 fread(&rdbh, sizeof(RawDBHeader), 1, in);
599 raw_header_to_header(&rdbh, &dbh);
600 if (dbh.app_info_offset==0) {
604 if (dbh.sort_info_offset!=0) {
605 *size = dbh.sort_info_offset - dbh.app_info_offset;
608 if (dbh.number_of_records==0) {
609 fseek(in, 0, SEEK_END);
610 *size=ftell(in) - dbh.app_info_offset;
614 fread(&rh, sizeof(record_header), 1, in);
615 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
616 *size=offset - dbh.app_info_offset;
622 * Read address file into address list. Based on JPilot's
623 * libplugin.c (jp_get_app_info)
625 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
628 unsigned int rec_size;
632 if( ( !buf_size ) || ( ! buf ) ) {
639 if( pilotFile->path ) {
640 in = fopen( pilotFile->path, "rb" );
642 return MGU_OPEN_FILE;
649 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
653 return MGU_ERROR_READ;
661 /* Convert header into something recognizable */
662 raw_header_to_header(&rdbh, &dbh);
664 num = jpilot_get_info_size(in, &rec_size);
667 return MGU_ERROR_READ;
670 fseek(in, dbh.app_info_offset, SEEK_SET);
671 *buf = ( char * ) malloc(rec_size);
674 return MGU_OO_MEMORY;
676 num = fread(*buf, rec_size, 1, in);
681 return MGU_ERROR_READ;
686 *buf_size = rec_size;
691 /* Shamelessly copied from JPilot (libplugin.c) */
692 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
698 memcpy(&l, p, sizeof(l));
699 header->header_len=ntohl(l);
702 memcpy(&l, p, sizeof(l));
703 header->header_version=ntohl(l);
706 memcpy(&l, p, sizeof(l));
707 header->rec_len=ntohl(l);
710 memcpy(&l, p, sizeof(l));
711 header->unique_id=ntohl(l);
714 memcpy(&l, p, sizeof(l));
718 memcpy(&(header->attrib), p, sizeof(unsigned char));
719 p+=sizeof(unsigned char);
724 /* Shamelessly copied from JPilot (libplugin.c) */
725 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
726 unsigned long l, len;
727 unsigned char packed_header[256];
730 num = fread(&l, sizeof(l), 1, pc_in);
737 memcpy(packed_header, &l, sizeof(l));
742 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
749 unpack_header(header, packed_header);
754 * Read next record from PC3 file. Based on JPilot function
755 * <code>pc_read_next_rec()</code> (libplugin.c)
757 * \param in File handle.
758 * \param br Record buffer.
759 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
762 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
763 PC3RecordHeader header;
770 num = read_header( in, &header );
773 return MGU_ERROR_READ;
779 rec_len = header.rec_len;
780 record = malloc( rec_len );
782 return MGU_OO_MEMORY;
784 num = fread( record, rec_len, 1, in );
788 return MGU_ERROR_READ;
792 br->unique_id = header.unique_id;
793 br->attrib = header.attrib;
801 * Read address file into a linked list. Based on JPilot function
802 * <code>jp_read_DB_files()</code> (from libplugin.c)
804 * \param pilotFile JPilot control data.
805 * \param records Pointer to linked list of records read.
806 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
809 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
813 int num_records, recs_returned, i, num, r;
814 unsigned int offset, prev_offset, next_offset, rec_size;
816 long fpos; /*file position indicator */
817 unsigned char attrib;
818 unsigned int unique_id;
819 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
826 mem_rh = last_mem_rh = NULL;
830 if( pilotFile->path == NULL ) {
834 in = fopen( pilotFile->path, "rb" );
836 return MGU_OPEN_FILE;
839 /* Read the database header */
840 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
844 return MGU_ERROR_READ;
851 raw_header_to_header( &rdbh, &dbh );
853 /* Read each record entry header */
854 num_records = dbh.number_of_records;
858 for( i = 1; i < num_records + 1; i++ ) {
859 num = fread( &rh, sizeof( record_header ), 1, in );
871 ( ( rh.Offset[0] * 256 + rh.Offset[1] ) * 256
872 + rh.Offset[2] ) * 256
874 if( offset < prev_offset ) {
877 prev_offset = offset;
878 temp_mem_rh = ( mem_rec_header * ) malloc( sizeof( mem_rec_header ) );
879 if( ! temp_mem_rh ) {
882 temp_mem_rh->next = NULL;
883 temp_mem_rh->rec_num = i;
884 temp_mem_rh->offset = offset;
885 temp_mem_rh->attrib = rh.attrib;
886 temp_mem_rh->unique_id =
887 ( rh.unique_ID[0] * 256 + rh.unique_ID[1] ) * 256
889 if( mem_rh == NULL ) {
890 mem_rh = temp_mem_rh;
891 last_mem_rh = temp_mem_rh;
894 last_mem_rh->next = temp_mem_rh;
895 last_mem_rh = temp_mem_rh;
899 temp_mem_rh = mem_rh;
904 mem_rh, 0, &next_offset, &attrib, &unique_id );
908 next_offset = mem_rh->offset;
909 attrib = mem_rh->attrib;
910 unique_id = mem_rh->unique_id;
913 fseek( in, next_offset, SEEK_SET );
914 while( ! feof( in ) ) {
918 mem_rh, fpos, &next_offset, &attrib,
921 next_offset = 0xFFFFFF;
923 attrib = temp_mem_rh->attrib;
924 unique_id = temp_mem_rh->unique_id;
925 if ( temp_mem_rh->next ) {
926 temp_mem_rh = temp_mem_rh->next;
927 next_offset = temp_mem_rh->offset;
931 rec_size = next_offset - fpos;
932 buf = malloc( rec_size );
934 num = fread( buf, rec_size, 1, in );
942 temp_br = malloc( sizeof( buf_rec ) );
947 temp_br->rt = PALM_REC;
948 temp_br->unique_id = unique_id;
949 temp_br->attrib = attrib;
951 temp_br->size = rec_size;
953 *records = g_list_append( *records, temp_br );
959 free_mem_rec_header( &mem_rh );
961 /* Read the PC3 file, if present */
962 pcFile = jpilot_get_pc3_file( pilotFile );
963 if( pcFile == NULL ) return MGU_SUCCESS;
964 pc_in = fopen( pcFile, "rb" );
967 if( pc_in == NULL ) {
971 while( ! feof( pc_in ) ) {
974 temp_br = malloc( sizeof( buf_rec ) );
978 r = jpilot_read_next_pc( pc_in, temp_br );
979 if( r != MGU_SUCCESS ) {
980 if( (r != MGU_EOF) && (r != MGU_ERROR_READ) ) {
981 free( temp_br->buf );
988 if( ( temp_br->rt != DELETED_PC_REC )
989 && ( temp_br->rt != DELETED_PALM_REC )
990 && ( temp_br->rt != MODIFIED_PALM_REC )
991 && ( temp_br->rt != DELETED_DELETED_PALM_REC ) )
993 *records = g_list_append( *records, temp_br );
998 if( ( temp_br->rt == DELETED_PALM_REC )
999 || ( temp_br->rt == MODIFIED_PALM_REC ) )
1001 temp_list = *records;
1003 while( temp_list->next ) {
1004 temp_list=temp_list->next;
1007 for( ; temp_list; temp_list=temp_list->prev ) {
1008 if( ( ( buf_rec * )temp_list->data )->unique_id ==
1009 temp_br->unique_id ) {
1010 ( ( buf_rec * )temp_list->data )->rt =
1017 free( temp_br->buf );
1027 * Parse buffer containing multiple e-mail addresses into a linked list of
1028 * addresses. Separator characters are " ,;|" and control characters. Address
1029 * is only extracted if it contains an "at" (@) character.
1031 * \param buf Buffer to process.
1032 * \return List of strings.
1034 static GList *jpilot_parse_email( gchar *buf ) {
1039 gboolean valid, done;
1041 valid = done = FALSE;
1046 if( *p == ' ' || *p == ',' || *p == ';' || *p == '|' || *p < 32 ) {
1061 em = g_strndup( st, len );
1062 list = g_list_append( list, em );
1070 if( *p == '@' ) valid = TRUE;
1078 #define FULLNAME_BUFSIZE 256
1079 #define EMAIL_BUFSIZE 256
1082 * Process a single label entry field, parsing multiple e-mail address entries.
1084 * \param pilotFile JPilot control data.
1085 * \param labelEntry Label entry data.
1086 * \param person Person.
1088 static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPerson *person ) {
1089 gchar buffer[ EMAIL_BUFSIZE ];
1095 strcpy( buffer, labelEntry );
1096 node = list = jpilot_parse_email( buffer );
1098 gchar convertBuff[JPILOT_LEN_LABEL];
1099 email = addritem_create_item_email();
1100 addritem_email_set_address( email, node->data );
1101 if (convert_charcode) {
1102 conv_sjistoeuc(convertBuff, JPILOT_LEN_LABEL, buffer);
1103 addritem_email_set_remarks(email, convertBuff);
1106 addritem_email_set_remarks(email, buffer);
1109 addrcache_id_email( pilotFile->addressCache, email );
1110 addrcache_person_add_email( pilotFile->addressCache, person, email );
1111 node = g_list_next( node );
1113 mgu_free_dlist( list );
1119 * Unpack address, building new data inside cache.
1120 * \param pilotFile JPilot control data.
1121 * \param buf Record buffer.
1122 * \param folderInd Array of (category) folders to load.
1124 static void jpilot_load_address(
1125 JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] )
1127 struct Address addr;
1133 gchar fullName[ FULLNAME_BUFSIZE ];
1139 struct AddressAppInfo *ai;
1140 gchar **firstName = NULL;
1141 gchar **lastName = NULL;
1143 /* Retrieve address */
1144 num = unpack_Address( & addr, buf->buf, buf->size );
1146 addrEnt = addr.entry;
1147 attrib = buf->attrib;
1148 unique_id = buf->unique_id;
1149 cat_id = attrib & 0x0F;
1153 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1154 firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
1157 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1158 lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
1161 if( name_order == FAMILY_LAST ) {
1162 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1163 firstName ? firstName[0] : "",
1164 lastName ? lastName[0] : "" );
1167 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1168 lastName ? lastName[0] : "",
1169 firstName ? firstName[0] : "" );
1173 g_strfreev( firstName );
1176 g_strfreev( lastName );
1179 g_strstrip( fullName );
1181 if( convert_charcode ) {
1183 nameConv = g_strdup( fullName );
1184 conv_sjistoeuc( fullName, FULLNAME_BUFSIZE, nameConv );
1188 person = addritem_create_item_person();
1189 addritem_person_set_common_name( person, fullName );
1190 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1191 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1192 addrcache_id_person( pilotFile->addressCache, person );
1194 extID = g_strdup_printf( "%d", unique_id );
1195 addritem_person_set_external_id( person, extID );
1199 /* Pointer to address metadata. */
1200 ai = & pilotFile->addrInfo;
1202 /* Add entry for each email address listed under phone labels. */
1203 indPhoneLbl = addr.phoneLabel;
1204 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1207 ind = indPhoneLbl[k];
1209 * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
1210 * ai->phoneLabels[ind], addrEnt[3+k] );
1212 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1213 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1214 jpilot_parse_label( pilotFile, labelEntry, person );
1218 /* Add entry for each custom label */
1219 node = pilotFile->labelInd;
1221 gchar convertBuff[JPILOT_LEN_LABEL];
1224 ind = GPOINTER_TO_INT( node->data );
1227 * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
1230 labelEntry = addrEnt[ind];
1231 jpilot_parse_label( pilotFile, labelEntry, person );
1234 node = g_list_next( node );
1237 if( person->listEMail ) {
1238 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1239 /* Add to specified category */
1240 addrcache_folder_add_person(
1241 pilotFile->addressCache,
1242 folderInd[cat_id], person );
1245 /* Add to root folder */
1246 addrcache_add_person(
1247 pilotFile->addressCache, person );
1251 addritem_free_item_person( person );
1256 /* Free up pointer allocated inside address */
1257 free_Address( & addr );
1261 * Free up address list.
1262 * \param records List of records to free.
1264 static void jpilot_free_addrlist( GList *records ) {
1274 node = g_list_next( node );
1278 g_list_free( records );
1282 * Read metadata from file.
1283 * \param pilotFile JPilot control data.
1284 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
1287 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1289 unsigned int rec_size;
1293 g_return_val_if_fail( pilotFile != NULL, -1 );
1295 pilotFile->readMetadata = FALSE;
1296 addrcache_clear( pilotFile->addressCache );
1298 /* Read file info */
1299 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1300 if( retVal != MGU_SUCCESS ) {
1301 pilotFile->retVal = retVal;
1302 return pilotFile->retVal;
1305 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1310 pilotFile->retVal = MGU_ERROR_READ;
1311 return pilotFile->retVal;
1314 pilotFile->readMetadata = TRUE;
1315 pilotFile->retVal = MGU_SUCCESS;
1316 return pilotFile->retVal;
1320 * Setup labels and indexes from metadata.
1321 * \param pilotFile JPilot control data.
1322 * \return <i>TRUE</i> is setup successfully.
1324 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1325 gboolean retVal = FALSE;
1326 struct AddressAppInfo *ai;
1329 g_return_val_if_fail( pilotFile != NULL, -1 );
1331 /* Release indexes */
1332 node = pilotFile->labelInd;
1335 node = g_list_next( node );
1337 pilotFile->labelInd = NULL;
1339 if( pilotFile->readMetadata ) {
1340 ai = & pilotFile->addrInfo;
1341 node = pilotFile->customLabels;
1343 gchar *lbl = node->data;
1346 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1347 gchar *labelName = ai->labels[i];
1348 gchar convertBuff[ JPILOT_LEN_LABEL ];
1350 if( convert_charcode ) {
1351 conv_sjistoeuc( convertBuff, JPILOT_LEN_LABEL, labelName );
1352 labelName = convertBuff;
1355 if( g_utf8_collate( labelName, lbl ) == 0 ) {
1360 pilotFile->labelInd = g_list_append(
1361 pilotFile->labelInd, GINT_TO_POINTER(ind) );
1362 node = g_list_next( node );
1370 * Load list with character strings of label names.
1371 * \param pilotFile JPilot control data.
1372 * \param labelList List of label names to load.
1373 * \return List of label names loaded.
1375 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
1378 g_return_val_if_fail( pilotFile != NULL, NULL );
1380 if( pilotFile->readMetadata ) {
1381 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1382 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1383 gchar *labelName = ai->labels[i];
1384 gchar convertBuff[JPILOT_LEN_LABEL];
1387 if( convert_charcode ) {
1388 conv_sjistoeuc( convertBuff, JPILOT_LEN_LABEL, labelName );
1389 labelName = convertBuff;
1391 labelList = g_list_append( labelList, g_strdup( labelName ) );
1394 labelList = g_list_append(
1395 labelList, g_strdup( "" ) );
1403 * Return category name for specified category ID.
1404 * \param pilotFile JPilot control data.
1405 * \param catID Category ID.
1406 * \return Category name, or empty string if not invalid ID. Name should be
1407 * <code>g_free()</code> when done.
1409 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
1410 gchar *catName = NULL;
1412 g_return_val_if_fail( pilotFile != NULL, NULL );
1414 if( pilotFile->readMetadata ) {
1415 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1416 struct CategoryAppInfo *cat = & ai->category;
1417 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
1420 catName = g_strdup( cat->name[catID] );
1423 if( ! catName ) catName = g_strdup( "" );
1428 * Load list with character strings of phone label names.
1429 * \param pilotFile JPilot control data.
1430 * \param labelList List of label names to load.
1431 * \return List of label names loaded.
1433 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
1436 g_return_val_if_fail( pilotFile != NULL, NULL );
1438 if( pilotFile->readMetadata ) {
1439 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1440 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1441 gchar *labelName = ai->phoneLabels[i];
1443 labelList = g_list_append(
1444 labelList, g_strdup( labelName ) );
1447 labelList = g_list_append(
1448 labelList, g_strdup( "" ) );
1456 * Load list with character strings of custom label names. Only none blank
1458 * \param pilotFile JPilot control data.
1459 * \param labelList List of label names to load.
1460 * \return List of label names loaded. Should be freed when done.
1462 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1464 char convertBuff[JPILOT_LEN_LABEL];
1466 g_return_val_if_fail( pilotFile != NULL, NULL );
1468 if( pilotFile->readMetadata ) {
1469 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1470 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1471 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1473 g_strchomp( labelName );
1474 g_strchug( labelName );
1475 if( *labelName != '\0' ) {
1476 if( convert_charcode ) {
1477 conv_sjistoeuc( convertBuff, JPILOT_LEN_LABEL, labelName );
1478 labelName = convertBuff;
1480 labelList = g_list_append( labelList, g_strdup( labelName ) );
1489 * Load list with character strings of category names.
1490 * \param pilotFile JPilot control data.
1491 * \return List of label names loaded. Should be freed when done.
1493 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1494 GList *catList = NULL;
1497 g_return_val_if_fail( pilotFile != NULL, NULL );
1499 if( pilotFile->readMetadata ) {
1500 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1501 struct CategoryAppInfo *cat = & ai->category;
1502 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1503 gchar *catName = cat->name[i];
1505 catList = g_list_append(
1506 catList, g_strdup( catName ) );
1509 catList = g_list_append(
1510 catList, g_strdup( "" ) );
1518 * Build folder in address book for each category.
1519 * \param pilotFile JPilot control data.
1521 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1522 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1523 struct CategoryAppInfo *cat = & ai->category;
1526 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1527 ItemFolder *folder = addritem_create_item_folder();
1529 if( convert_charcode ) {
1530 gchar catName[ JPILOT_LEN_CATEG ];
1531 conv_sjistoeuc( catName, JPILOT_LEN_CATEG, cat->name[i] );
1532 addritem_folder_set_name( folder, catName );
1535 addritem_folder_set_name( folder, cat->name[i] );
1538 addrcache_id_folder( pilotFile->addressCache, folder );
1539 addrcache_add_folder( pilotFile->addressCache, folder );
1544 * Remove empty (category) folders.
1545 * \param pilotFile JPilot control data.
1547 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1553 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1557 ItemFolder *folder = node->data;
1558 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1559 if( folder->listPerson ) {
1560 /* Give name to folder */
1562 sprintf( name, "? %d", i );
1563 addritem_folder_set_name( folder, name );
1566 /* Mark for removal */
1567 remList = g_list_append( remList, folder );
1570 node = g_list_next( node );
1575 ItemFolder *folder = node->data;
1576 addrcache_remove_folder( pilotFile->addressCache, folder );
1577 node = g_list_next( node );
1579 g_list_free( remList );
1583 * Read address file into address cache.
1584 * \param pilotFile JPilot control data.
1585 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1588 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1590 GList *records = NULL;
1593 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1595 /* Read list of records from JPilot files */
1596 retVal = jpilot_read_db_files( pilotFile, &records );
1597 if( retVal != MGU_SUCCESS ) {
1598 jpilot_free_addrlist( records );
1602 /* Setup labels and category folders */
1603 jpilot_setup_labels( pilotFile );
1604 jpilot_build_category_list( pilotFile );
1606 /* Build array of pointers to categories */
1608 node = addrcache_get_list_folder( pilotFile->addressCache );
1610 if( i < JPILOT_NUM_CATEG ) {
1611 folderInd[i] = node->data;
1613 node = g_list_next( node );
1617 /* Load all addresses, free up old stuff as we go */
1621 if( ( br->rt != DELETED_PC_REC ) &&
1622 ( br->rt != DELETED_PALM_REC ) &&
1623 ( br->rt != MODIFIED_PALM_REC ) &&
1624 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1625 jpilot_load_address( pilotFile, br, folderInd );
1630 node = g_list_next( node );
1634 g_list_free( records );
1636 /* Remove empty category folders */
1637 jpilot_remove_empty( pilotFile );
1638 jpilot_mark_files( pilotFile );
1644 * Read file into list. Main entry point
1645 * \param pilotFile JPilot control data.
1646 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1649 gint jpilot_read_data( JPilotFile *pilotFile ) {
1650 const gchar *cur_locale;
1652 name_order = FAMILY_LAST;
1653 convert_charcode = FALSE;
1655 cur_locale = conv_get_current_locale();
1657 if( g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ) {
1658 name_order = FAMILY_FIRST;
1661 if( conv_get_current_charset() == C_EUC_JP ) {
1662 convert_charcode = TRUE;
1665 g_return_val_if_fail( pilotFile != NULL, -1 );
1667 pilotFile->retVal = MGU_SUCCESS;
1668 pilotFile->addressCache->accessFlag = FALSE;
1669 if( jpilot_check_files( pilotFile ) ) {
1670 addrcache_clear( pilotFile->addressCache );
1671 jpilot_read_metadata( pilotFile );
1672 if( pilotFile->retVal == MGU_SUCCESS ) {
1673 pilotFile->retVal = jpilot_read_file( pilotFile );
1674 if( pilotFile->retVal == MGU_SUCCESS ) {
1675 pilotFile->addressCache->modified = FALSE;
1676 pilotFile->addressCache->dataRead = TRUE;
1680 return pilotFile->retVal;
1684 * Return linked list of persons. This is a list of references to ItemPerson
1685 * objects. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1686 * functions... this will destroy the addressbook data!
1688 * \param pilotFile JPilot control data.
1689 * \return List of persons.
1691 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1692 g_return_val_if_fail( pilotFile != NULL, NULL );
1693 return addrcache_get_list_person( pilotFile->addressCache );
1697 * Return linked list of folders. This is a list of references to non-empty
1698 * category folders. Do <b>NOT</b> attempt to use the
1699 * <code>addrbook_free_xxx()</code> functions... this will destroy the
1702 * \param pilotFile JPilot control data.
1703 * \return List of ItemFolder objects. This should not be freed.
1705 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1706 g_return_val_if_fail( pilotFile != NULL, NULL );
1707 return addrcache_get_list_folder( pilotFile->addressCache );
1711 * Return linked list of all persons. Note that the list contains references
1712 * to items. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1713 * functions... this will destroy the addressbook data!
1715 * \param pilotFile JPilot control data.
1716 * \return List of items, or NULL if none.
1718 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1719 g_return_val_if_fail( pilotFile != NULL, NULL );
1720 return addrcache_get_all_persons( pilotFile->addressCache );
1724 * Validate that all parameters specified.
1725 * \param pilotFile JPilot control data.
1726 * \return <i>TRUE</i> if data is good.
1728 gboolean jpilot_validate( JPilotFile *pilotFile ) {
1732 g_return_val_if_fail( pilotFile != NULL, FALSE );
1735 if( pilotFile->path ) {
1736 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1741 name = jpilot_get_name( pilotFile );
1743 if( strlen( name ) < 1 ) retVal = FALSE;
1751 #define WORK_BUFLEN 1024
1754 * Attempt to find a valid JPilot file.
1755 * \param pilotFile JPilot control data.
1756 * \return Filename, or home directory if not found, or empty string if
1757 * no home. Filename should be <code>g_free()</code> when done.
1759 gchar *jpilot_find_pilotdb( void ) {
1760 const gchar *homedir;
1761 gchar str[ WORK_BUFLEN ];
1765 homedir = g_get_home_dir();
1766 if( ! homedir ) return g_strdup( "" );
1768 strcpy( str, homedir );
1769 len = strlen( str );
1771 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1772 str[ len ] = G_DIR_SEPARATOR;
1773 str[ ++len ] = '\0';
1776 strcat( str, JPILOT_DBHOME_DIR );
1777 strcat( str, G_DIR_SEPARATOR_S );
1778 strcat( str, JPILOT_DBHOME_FILE );
1780 /* Attempt to open */
1781 if( ( fp = fopen( str, "rb" ) ) != NULL ) {
1785 /* Truncate filename */
1788 return g_strdup( str );
1792 * Attempt to read file, testing for valid JPilot format.
1793 * \param fileSpec File specification to read.
1794 * \return <i>TRUE<i> if file appears to be valid format.
1796 gint jpilot_test_read_file( const gchar *fileSpec ) {
1797 JPilotFile *pilotFile;
1801 pilotFile = jpilot_create_path( fileSpec );
1802 retVal = jpilot_read_metadata( pilotFile );
1803 jpilot_free( pilotFile );
1807 retVal = MGU_NO_FILE;
1813 * Check whether label is in list of custom labels.
1814 * \param pilotFile JPilot control data.
1815 * \param labelName to test.
1816 * \return <i>TRUE</i> if found.
1818 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1822 g_return_val_if_fail( pilotFile != NULL, FALSE );
1826 node = pilotFile->customLabels;
1828 if( g_utf8_collate( labelName, ( gchar * ) node->data ) == 0 ) {
1832 node = g_list_next( node );
1839 * Test whether pilot link library installed.
1840 * \return <i>TRUE</i> if library available.
1842 gboolean jpilot_test_pilot_lib( void ) {
1846 #endif /* USE_JPILOT */