2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2002 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"
54 #include "adbookbase.h"
56 #define JPILOT_DBHOME_DIR ".jpilot"
57 #define JPILOT_DBHOME_FILE "AddressDB.pdb"
58 #define PILOT_LINK_LIB_NAME "libpisock.so"
60 #define IND_LABEL_LASTNAME 0 /* Index of last name in address data */
61 #define IND_LABEL_FIRSTNAME 1 /* Index of first name in address data */
62 #define IND_PHONE_EMAIL 4 /* Index of E-Mail address in phone labels */
63 #define OFFSET_PHONE_LABEL 3 /* Offset to phone data in address data */
64 #define IND_CUSTOM_LABEL 14 /* Offset to custom label names */
65 #define NUM_CUSTOM_LABEL 4 /* Number of custom labels */
67 /* Shamelessly copied from JPilot (libplugin.h) */
69 unsigned char db_name[32];
70 unsigned char flags[2];
71 unsigned char version[2];
72 unsigned char creation_time[4];
73 unsigned char modification_time[4];
74 unsigned char backup_time[4];
75 unsigned char modification_number[4];
76 unsigned char app_info_offset[4];
77 unsigned char sort_info_offset[4];
78 unsigned char type[4];/*Database ID */
79 unsigned char creator_id[4];/*Application ID */
80 unsigned char unique_id_seed[4];
81 unsigned char next_record_list_id[4];
82 unsigned char number_of_records[2];
85 /* Shamelessly copied from JPilot (libplugin.h) */
91 time_t modification_time;
93 unsigned int modification_number;
94 unsigned int app_info_offset;
95 unsigned int sort_info_offset;
96 char type[5];/*Database ID */
97 char creator_id[5];/*Application ID */
98 char unique_id_seed[5];
99 unsigned int next_record_list_id;
100 unsigned int number_of_records;
103 /* Shamelessly copied from JPilot (libplugin.h) */
105 unsigned char Offset[4]; /*4 bytes offset from BOF to record */
106 unsigned char attrib;
107 unsigned char unique_ID[3];
110 /* Shamelessly copied from JPilot (libplugin.h) */
111 typedef struct mem_rec_header_s {
112 unsigned int rec_num;
114 unsigned int unique_id;
115 unsigned char attrib;
116 struct mem_rec_header_s *next;
119 /* Shamelessly copied from JPilot (libplugin.h) */
120 #define SPENT_PC_RECORD_BIT 256
124 MODIFIED_PALM_REC = 101L,
125 DELETED_PALM_REC = 102L,
127 DELETED_PC_REC = SPENT_PC_RECORD_BIT + 104L,
128 DELETED_DELETED_PALM_REC = SPENT_PC_RECORD_BIT + 105L
131 /* Shamelessly copied from JPilot (libplugin.h) */
134 unsigned int unique_id;
135 unsigned char attrib;
140 /* Shamelessly copied from JPilot (libplugin.h) */
142 unsigned long header_len;
143 unsigned long header_version;
144 unsigned long rec_len;
145 unsigned long unique_id;
146 unsigned long rt; /* Record Type */
147 unsigned char attrib;
151 * Create new pilot file object.
153 JPilotFile *jpilot_create() {
154 JPilotFile *pilotFile;
155 pilotFile = g_new0( JPilotFile, 1 );
156 pilotFile->type = ADBOOKTYPE_JPILOT;
157 pilotFile->addressCache = addrcache_create();
158 pilotFile->retVal = MGU_SUCCESS;
160 pilotFile->file = NULL;
161 pilotFile->path = NULL;
162 pilotFile->readMetadata = FALSE;
163 pilotFile->customLabels = NULL;
164 pilotFile->labelInd = NULL;
165 pilotFile->havePC3 = FALSE;
166 pilotFile->pc3ModifyTime = 0;
171 * Create new pilot file object for specified file.
173 JPilotFile *jpilot_create_path( const gchar *path ) {
174 JPilotFile *pilotFile;
175 pilotFile = jpilot_create();
176 jpilot_set_file( pilotFile, path );
183 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
184 g_return_if_fail( pilotFile != NULL );
185 addrcache_set_name( pilotFile->addressCache, value );
187 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
188 g_return_if_fail( pilotFile != NULL );
189 addrcache_refresh( pilotFile->addressCache );
190 pilotFile->readMetadata = FALSE;
191 pilotFile->path = mgu_replace_string( pilotFile->path, value );
193 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
194 g_return_if_fail( pilotFile != NULL );
195 pilotFile->addressCache->accessFlag = value;
198 gint jpilot_get_status( JPilotFile *pilotFile ) {
199 g_return_val_if_fail( pilotFile != NULL, -1 );
200 return pilotFile->retVal;
202 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
203 g_return_val_if_fail( pilotFile != NULL, NULL );
204 return addrcache_get_root_folder( pilotFile->addressCache );
206 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
207 g_return_val_if_fail( pilotFile != NULL, NULL );
208 return addrcache_get_name( pilotFile->addressCache );
212 * Test whether file was read.
213 * Return: TRUE if file was read.
215 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
216 g_return_val_if_fail( pilotFile != NULL, FALSE );
217 return pilotFile->addressCache->dataRead;
221 * Free up custom label list.
223 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
226 g_return_if_fail( pilotFile != NULL );
228 /* Release custom labels */
229 mgu_free_dlist( pilotFile->customLabels );
230 pilotFile->customLabels = NULL;
232 /* Release indexes */
233 node = pilotFile->labelInd;
236 node = g_list_next( node );
238 g_list_free( pilotFile->labelInd );
239 pilotFile->labelInd = NULL;
241 /* Force a fresh read */
242 addrcache_refresh( pilotFile->addressCache );
246 * Append a custom label, representing an E-Mail address field to the
249 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
250 g_return_if_fail( pilotFile != NULL );
253 gchar *labelCopy = g_strdup( labelName );
254 g_strstrip( labelCopy );
255 if( *labelCopy == '\0' ) {
259 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
260 /* Force a fresh read */
261 addrcache_refresh( pilotFile->addressCache );
267 * Get list of custom labels.
268 * Return: List of labels. Must use g_free() when done.
270 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
271 GList *retVal = NULL;
274 g_return_val_if_fail( pilotFile != NULL, NULL );
276 node = pilotFile->customLabels;
278 retVal = g_list_append( retVal, g_strdup( node->data ) );
279 node = g_list_next( node );
285 * Return filespec of PC3 file corresponding to JPilot PDB file.
286 * Note: Filespec should be g_free() when done.
288 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
292 if( pilotFile == NULL ) return NULL;
293 if( pilotFile->path == NULL ) return NULL;
295 fileSpec = g_strdup( pilotFile->path );
296 len = strlen( fileSpec );
299 for( i = len; i > 0; i-- ) {
300 if( *(fileSpec + i) == '.' ) {
307 if( len - pos == 3 ) {
308 *r++ = 'p'; *r++ = 'c'; *r = '3';
317 * Save PC3 file time to cache.
318 * return: TRUE if time marked.
320 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
321 gboolean retVal = FALSE;
322 struct stat filestat;
325 /* Mark PDB file cache */
326 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
328 /* Now mark PC3 file */
329 pilotFile->havePC3 = FALSE;
330 pilotFile->pc3ModifyTime = 0;
331 pcFile = jpilot_get_pc3_file( pilotFile );
332 if( pcFile == NULL ) return retVal;
333 if( 0 == lstat( pcFile, &filestat ) ) {
334 pilotFile->havePC3 = TRUE;
335 pilotFile->pc3ModifyTime = filestat.st_mtime;
343 * Check whether JPilot PDB or PC3 file has changed by comparing
345 * return: TRUE if file has changed.
347 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
348 gboolean retVal = TRUE;
349 struct stat filestat;
352 /* Check main file */
353 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
357 if( ! pilotFile->havePC3 ) return FALSE;
358 pcFile = jpilot_get_pc3_file( pilotFile );
359 if( pcFile == NULL ) return FALSE;
361 if( 0 == lstat( pcFile, &filestat ) ) {
362 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
369 * Test whether file was modified since last access.
370 * Return: TRUE if file was modified.
372 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
373 g_return_val_if_fail( pilotFile != NULL, FALSE );
374 pilotFile->addressCache->modified = jpilot_check_files( pilotFile );
375 return pilotFile->addressCache->modified;
377 void jpilot_set_modified( JPilotFile *pilotFile, const gboolean value ) {
378 g_return_if_fail( pilotFile != NULL );
379 pilotFile->addressCache->modified = value;
381 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
382 g_return_val_if_fail( pilotFile != NULL, FALSE );
383 return pilotFile->addressCache->accessFlag;
387 * Free up pilot file object by releasing internal memory.
389 void jpilot_free( JPilotFile *pilotFile ) {
390 g_return_if_fail( pilotFile != NULL );
392 /* Release custom labels */
393 jpilot_clear_custom_labels( pilotFile );
396 addrcache_clear( pilotFile->addressCache );
397 addrcache_free( pilotFile->addressCache );
399 /* Free internal stuff */
400 g_free( pilotFile->path );
402 pilotFile->file = NULL;
403 pilotFile->path = NULL;
404 pilotFile->readMetadata = FALSE;
405 pilotFile->havePC3 = FALSE;
406 pilotFile->pc3ModifyTime = 0;
408 pilotFile->type = ADBOOKTYPE_NONE;
409 pilotFile->addressCache = NULL;
410 pilotFile->retVal = MGU_SUCCESS;
412 /* Now release file object */
417 * Refresh internal variables to force a file read.
419 void jpilot_force_refresh( JPilotFile *pilotFile ) {
420 addrcache_refresh( pilotFile->addressCache );
424 * Print object to specified stream.
426 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
429 g_return_if_fail( pilotFile != NULL );
431 fprintf( stream, "JPilotFile:\n" );
432 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
433 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
434 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
436 node = pilotFile->customLabels;
438 fprintf( stream, " c label: %s\n", (gchar *)node->data );
439 node = g_list_next( node );
442 node = pilotFile->labelInd;
444 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
445 node = g_list_next( node );
448 addrcache_print( pilotFile->addressCache, stream );
449 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
450 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
451 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
452 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
456 * Print summary of object to specified stream.
458 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
460 g_return_if_fail( pilotFile != NULL );
461 fprintf( stream, "JPilotFile:\n" );
462 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
463 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
464 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
466 node = pilotFile->customLabels;
468 fprintf( stream, " c label: %s\n", (gchar *)node->data );
469 node = g_list_next( node );
472 node = pilotFile->labelInd;
474 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
475 node = g_list_next( node );
477 addrcache_print( pilotFile->addressCache, stream );
478 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
479 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
482 /* Shamelessly copied from JPilot (libplugin.c) */
483 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
486 for (i=0;i<num_bytes;i++) {
492 /* Shamelessly copied from JPilot (utils.c) */
493 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
494 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
495 #define PILOT_TIME_DELTA (unsigned)(2082844800)
497 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
498 return (time_t)(raw_time - PILOT_TIME_DELTA);
501 /* Shamelessly copied from JPilot (libplugin.c) */
502 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
505 strncpy(dbh->db_name, rdbh->db_name, 31);
506 dbh->db_name[31] = '\0';
507 dbh->flags = bytes_to_bin(rdbh->flags, 2);
508 dbh->version = bytes_to_bin(rdbh->version, 2);
509 temp = bytes_to_bin(rdbh->creation_time, 4);
510 dbh->creation_time = pilot_time_to_unix_time(temp);
511 temp = bytes_to_bin(rdbh->modification_time, 4);
512 dbh->modification_time = pilot_time_to_unix_time(temp);
513 temp = bytes_to_bin(rdbh->backup_time, 4);
514 dbh->backup_time = pilot_time_to_unix_time(temp);
515 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
516 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
517 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
518 strncpy(dbh->type, rdbh->type, 4);
520 strncpy(dbh->creator_id, rdbh->creator_id, 4);
521 dbh->creator_id[4] = '\0';
522 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
523 dbh->unique_id_seed[4] = '\0';
524 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
525 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
529 /* Shamelessly copied from JPilot (libplugin.c) */
530 /* returns 1 if found */
532 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
533 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
535 mem_rec_header *temp_mem_rh;
536 unsigned char found = 0;
537 unsigned long found_at;
540 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
541 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
542 found_at = temp_mem_rh->offset;
543 /* *attrib = temp_mem_rh->attrib; */
544 /* *unique_id = temp_mem_rh->unique_id; */
546 if ((temp_mem_rh->offset == fpos)) {
548 *attrib = temp_mem_rh->attrib;
549 *unique_id = temp_mem_rh->unique_id;
552 *next_offset = found_at;
556 /* Shamelessly copied from JPilot (libplugin.c) */
557 static void free_mem_rec_header(mem_rec_header **mem_rh) {
558 mem_rec_header *h, *next_h;
559 for (h=*mem_rh; h; h=next_h) {
566 /* Shamelessly copied from JPilot (libplugin.c) */
567 static int jpilot_free_db_list( GList **br_list ) {
568 GList *temp_list, *first;
571 /* Go to first entry in the list */
573 for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
576 for (temp_list = first; temp_list; temp_list = temp_list->next) {
577 if (temp_list->data) {
581 temp_list->data=NULL;
586 g_list_free(*br_list);
591 /* Shamelessly copied from JPilot (libplugin.c) */
593 static int jpilot_get_info_size( FILE *in, int *size ) {
599 fseek(in, 0, SEEK_SET);
600 fread(&rdbh, sizeof(RawDBHeader), 1, in);
605 raw_header_to_header(&rdbh, &dbh);
606 if (dbh.app_info_offset==0) {
610 if (dbh.sort_info_offset!=0) {
611 *size = dbh.sort_info_offset - dbh.app_info_offset;
614 if (dbh.number_of_records==0) {
615 fseek(in, 0, SEEK_END);
616 *size=ftell(in) - dbh.app_info_offset;
620 fread(&rh, sizeof(record_header), 1, in);
621 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
622 *size=offset - dbh.app_info_offset;
628 * Read address file into address list. Based on JPilot's
629 * libplugin.c (jp_get_app_info)
631 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
634 unsigned int rec_size;
638 if( ( !buf_size ) || ( ! buf ) ) {
645 if( pilotFile->path ) {
646 in = fopen( pilotFile->path, "rb" );
648 return MGU_OPEN_FILE;
655 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
659 return MGU_ERROR_READ;
667 /* Convert header into something recognizable */
668 raw_header_to_header(&rdbh, &dbh);
670 num = jpilot_get_info_size(in, &rec_size);
673 return MGU_ERROR_READ;
676 fseek(in, dbh.app_info_offset, SEEK_SET);
677 *buf = ( char * ) malloc(rec_size);
680 return MGU_OO_MEMORY;
682 num = fread(*buf, rec_size, 1, in);
687 return MGU_ERROR_READ;
692 *buf_size = rec_size;
697 /* Shamelessly copied from JPilot (libplugin.c) */
698 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
704 memcpy(&l, p, sizeof(l));
705 header->header_len=ntohl(l);
708 memcpy(&l, p, sizeof(l));
709 header->header_version=ntohl(l);
712 memcpy(&l, p, sizeof(l));
713 header->rec_len=ntohl(l);
716 memcpy(&l, p, sizeof(l));
717 header->unique_id=ntohl(l);
720 memcpy(&l, p, sizeof(l));
724 memcpy(&(header->attrib), p, sizeof(unsigned char));
725 p+=sizeof(unsigned char);
730 /* Shamelessly copied from JPilot (libplugin.c) */
731 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
732 unsigned long l, len;
733 unsigned char packed_header[256];
736 num = fread(&l, sizeof(l), 1, pc_in);
743 memcpy(packed_header, &l, sizeof(l));
748 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
755 unpack_header(header, packed_header);
759 /* Read next record from PC3 file. Based on JPilot's
760 * pc_read_next_rec (libplugin.c) */
761 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
762 PC3RecordHeader header;
769 num = read_header(in, &header);
772 return MGU_ERROR_READ;
778 rec_len = header.rec_len;
779 record = malloc(rec_len);
781 return MGU_OO_MEMORY;
783 num = fread(record, rec_len, 1, in);
787 return MGU_ERROR_READ;
791 br->unique_id = header.unique_id;
792 br->attrib = header.attrib;
800 * Read address file into a linked list. Based on JPilot's
801 * jp_read_DB_files (from libplugin.c)
803 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
807 int num_records, recs_returned, i, num, r;
808 unsigned int offset, prev_offset, next_offset, rec_size;
810 long fpos; /*file position indicator */
811 unsigned char attrib;
812 unsigned int unique_id;
813 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
820 mem_rh = last_mem_rh = NULL;
824 if( pilotFile->path == NULL ) {
828 in = fopen( pilotFile->path, "rb" );
830 return MGU_OPEN_FILE;
833 /* Read the database header */
834 num = fread(&rdbh, sizeof(RawDBHeader), 1, in);
838 return MGU_ERROR_READ;
845 raw_header_to_header(&rdbh, &dbh);
847 /* Read each record entry header */
848 num_records = dbh.number_of_records;
852 for (i = 1; i < num_records + 1; i++) {
853 num = fread(&rh, sizeof(record_header), 1, in);
864 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
865 if (offset < prev_offset) {
868 prev_offset = offset;
869 temp_mem_rh = (mem_rec_header *)malloc(sizeof(mem_rec_header));
873 temp_mem_rh->next = NULL;
874 temp_mem_rh->rec_num = i;
875 temp_mem_rh->offset = offset;
876 temp_mem_rh->attrib = rh.attrib;
877 temp_mem_rh->unique_id = (rh.unique_ID[0]*256+rh.unique_ID[1])*256+rh.unique_ID[2];
878 if (mem_rh == NULL) {
879 mem_rh = temp_mem_rh;
880 last_mem_rh = temp_mem_rh;
882 last_mem_rh->next = temp_mem_rh;
883 last_mem_rh = temp_mem_rh;
887 temp_mem_rh = mem_rh;
891 find_next_offset(mem_rh, 0, &next_offset, &attrib, &unique_id);
894 next_offset = mem_rh->offset;
895 attrib = mem_rh->attrib;
896 unique_id = mem_rh->unique_id;
899 fseek(in, next_offset, SEEK_SET);
903 find_next_offset(mem_rh, fpos, &next_offset, &attrib, &unique_id);
905 next_offset = 0xFFFFFF;
907 attrib = temp_mem_rh->attrib;
908 unique_id = temp_mem_rh->unique_id;
909 if (temp_mem_rh->next) {
910 temp_mem_rh = temp_mem_rh->next;
911 next_offset = temp_mem_rh->offset;
915 rec_size = next_offset - fpos;
916 buf = malloc(rec_size);
918 num = fread(buf, rec_size, 1, in);
926 temp_br = malloc(sizeof(buf_rec));
930 temp_br->rt = PALM_REC;
931 temp_br->unique_id = unique_id;
932 temp_br->attrib = attrib;
934 temp_br->size = rec_size;
936 *records = g_list_append(*records, temp_br);
942 free_mem_rec_header(&mem_rh);
944 /* Read the PC3 file, if present */
945 pcFile = jpilot_get_pc3_file( pilotFile );
946 if( pcFile == NULL ) return MGU_SUCCESS;
947 pc_in = fopen( pcFile, "rb" );
950 if( pc_in == NULL ) {
954 while( ! feof( pc_in ) ) {
955 temp_br = malloc(sizeof(buf_rec));
959 r = jpilot_read_next_pc( pc_in, temp_br );
960 if ( r != MGU_SUCCESS ) {
964 if ((temp_br->rt!=DELETED_PC_REC)
965 &&(temp_br->rt!=DELETED_PALM_REC)
966 &&(temp_br->rt!=MODIFIED_PALM_REC)
967 &&(temp_br->rt!=DELETED_DELETED_PALM_REC)) {
968 *records = g_list_append(*records, temp_br);
971 if ((temp_br->rt==DELETED_PALM_REC) || (temp_br->rt==MODIFIED_PALM_REC)) {
974 while(temp_list->next) {
975 temp_list=temp_list->next;
978 for (; temp_list; temp_list=temp_list->prev) {
979 if (((buf_rec *)temp_list->data)->unique_id == temp_br->unique_id) {
980 ((buf_rec *)temp_list->data)->rt = temp_br->rt;
990 #define FULLNAME_BUFSIZE 256
991 #define EMAIL_BUFSIZE 256
993 * Unpack address, building new data inside cache.
995 static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] ) {
1002 gchar fullName[ FULLNAME_BUFSIZE ];
1003 gchar bufEMail[ EMAIL_BUFSIZE ];
1010 struct AddressAppInfo *ai;
1012 /* Retrieve address */
1013 num = unpack_Address( & addr, buf->buf, buf->size );
1015 addrEnt = addr.entry;
1016 attrib = buf->attrib;
1017 unique_id = buf->unique_id;
1018 cat_id = attrib & 0x0F;
1020 *fullName = *bufEMail = '\0';
1021 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1022 strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
1025 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1026 strcat( fullName, " " );
1027 strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
1029 g_strchug( fullName );
1030 g_strchomp( fullName );
1032 person = addritem_create_item_person();
1033 addritem_person_set_common_name( person, fullName );
1034 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1035 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1036 addrcache_id_person( pilotFile->addressCache, person );
1038 extID = g_strdup_printf( "%d", unique_id );
1039 addritem_person_set_external_id( person, extID );
1043 /* Pointer to address metadata. */
1044 ai = & pilotFile->addrInfo;
1046 /* Add entry for each email address listed under phone labels. */
1047 indPhoneLbl = addr.phoneLabel;
1048 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1050 ind = indPhoneLbl[k];
1052 * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
1053 * ai->phoneLabels[ind], addrEnt[3+k] );
1055 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1056 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1058 strcpy( bufEMail, labelEntry );
1059 g_strchug( bufEMail );
1060 g_strchomp( bufEMail );
1062 email = addritem_create_item_email();
1063 addritem_email_set_address( email, bufEMail );
1064 addrcache_id_email( pilotFile->addressCache, email );
1065 addrcache_person_add_email
1066 ( pilotFile->addressCache, person, email );
1071 /* Add entry for each custom label */
1072 node = pilotFile->labelInd;
1075 ind = GPOINTER_TO_INT( node->data );
1078 * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
1081 labelEntry = addrEnt[ind];
1083 strcpy( bufEMail, labelEntry );
1084 g_strchug( bufEMail );
1085 g_strchomp( bufEMail );
1087 email = addritem_create_item_email();
1088 addritem_email_set_address( email, bufEMail );
1089 addritem_email_set_remarks( email, ai->labels[ind] );
1090 addrcache_id_email( pilotFile->addressCache, email );
1091 addrcache_person_add_email
1092 ( pilotFile->addressCache, person, email );
1096 node = g_list_next( node );
1099 if( person->listEMail ) {
1100 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1101 /* Add to specified category */
1102 addrcache_folder_add_person
1103 ( pilotFile->addressCache, folderInd[cat_id], person );
1105 /* Add to root folder */
1106 addrcache_add_person( pilotFile->addressCache, person );
1109 addritem_free_item_person( person );
1116 * Free up address list.
1118 static void jpilot_free_addrlist( GList *records ) {
1127 node = g_list_next( node );
1131 g_list_free( records );
1135 * Read address file into address cache.
1137 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1139 GList *records = NULL;
1142 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1144 retVal = jpilot_read_db_files( pilotFile, &records );
1145 if( retVal != MGU_SUCCESS ) {
1146 jpilot_free_addrlist( records );
1150 /* Build array of pointers to categories */
1152 node = addrcache_get_list_folder( pilotFile->addressCache );
1154 if( i < JPILOT_NUM_CATEG ) {
1155 folderInd[i] = node->data;
1157 node = g_list_next( node );
1161 /* Load all addresses, free up old stuff as we go */
1165 if( ( br->rt != DELETED_PC_REC ) &&
1166 ( br->rt != DELETED_PALM_REC ) &&
1167 ( br->rt != MODIFIED_PALM_REC ) &&
1168 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1169 jpilot_load_address( pilotFile, br, folderInd );
1173 node = g_list_next( node );
1177 g_list_free( records );
1184 * Read metadata from file.
1186 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1188 unsigned int rec_size;
1192 g_return_val_if_fail( pilotFile != NULL, -1 );
1194 pilotFile->readMetadata = FALSE;
1195 addrcache_clear( pilotFile->addressCache );
1197 /* Read file info */
1198 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1199 if( retVal != MGU_SUCCESS ) {
1200 pilotFile->retVal = retVal;
1201 return pilotFile->retVal;
1204 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1209 pilotFile->retVal = MGU_ERROR_READ;
1210 return pilotFile->retVal;
1213 pilotFile->readMetadata = TRUE;
1214 pilotFile->retVal = MGU_SUCCESS;
1215 return pilotFile->retVal;
1219 * Setup labels and indexes from metadata.
1220 * Return: TRUE is setup successfully.
1222 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1223 gboolean retVal = FALSE;
1224 struct AddressAppInfo *ai;
1227 g_return_val_if_fail( pilotFile != NULL, -1 );
1229 /* Release indexes */
1230 node = pilotFile->labelInd;
1233 node = g_list_next( node );
1235 pilotFile->labelInd = NULL;
1237 if( pilotFile->readMetadata ) {
1238 ai = & pilotFile->addrInfo;
1239 node = pilotFile->customLabels;
1241 gchar *lbl = node->data;
1244 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1245 gchar *labelName = ai->labels[i];
1246 if( g_strcasecmp( labelName, lbl ) == 0 ) {
1251 pilotFile->labelInd = g_list_append( pilotFile->labelInd, GINT_TO_POINTER(ind) );
1252 node = g_list_next( node );
1260 * Load list with character strings of label names.
1262 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
1265 g_return_val_if_fail( pilotFile != NULL, NULL );
1267 if( pilotFile->readMetadata ) {
1268 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1269 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1270 gchar *labelName = ai->labels[i];
1272 labelList = g_list_append( labelList, g_strdup( labelName ) );
1275 labelList = g_list_append( labelList, g_strdup( "" ) );
1283 * Return category name for specified category ID.
1284 * Enter: Category ID.
1285 * Return: Name, or empty string if not invalid ID. Name should be g_free() when done.
1287 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
1288 gchar *catName = NULL;
1290 g_return_val_if_fail( pilotFile != NULL, NULL );
1292 if( pilotFile->readMetadata ) {
1293 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1294 struct CategoryAppInfo *cat = & ai->category;
1295 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
1298 catName = g_strdup( cat->name[catID] );
1301 if( ! catName ) catName = g_strdup( "" );
1306 * Load list with character strings of phone label names.
1308 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
1311 g_return_val_if_fail( pilotFile != NULL, NULL );
1313 if( pilotFile->readMetadata ) {
1314 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1315 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1316 gchar *labelName = ai->phoneLabels[i];
1318 labelList = g_list_append( labelList, g_strdup( labelName ) );
1321 labelList = g_list_append( labelList, g_strdup( "" ) );
1329 * Load list with character strings of label names. Only none blank names
1331 * Return: list of labels. Should by g_free()'d when done.
1333 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1336 g_return_val_if_fail( pilotFile != NULL, NULL );
1338 if( pilotFile->readMetadata ) {
1339 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1340 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1341 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1343 g_strchomp( labelName );
1344 g_strchug( labelName );
1345 if( *labelName != '\0' ) {
1346 labelList = g_list_append( labelList, g_strdup( labelName ) );
1355 * Load list with character strings of category names.
1357 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1358 GList *catList = NULL;
1361 g_return_val_if_fail( pilotFile != NULL, NULL );
1363 if( pilotFile->readMetadata ) {
1364 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1365 struct CategoryAppInfo *cat = & ai->category;
1366 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1367 gchar *catName = cat->name[i];
1369 catList = g_list_append( catList, g_strdup( catName ) );
1372 catList = g_list_append( catList, g_strdup( "" ) );
1380 * Build folder for each category.
1382 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1383 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1384 struct CategoryAppInfo *cat = & ai->category;
1387 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1388 ItemFolder *folder = addritem_create_item_folder();
1389 addritem_folder_set_name( folder, cat->name[i] );
1390 addrcache_id_folder( pilotFile->addressCache, folder );
1391 addrcache_add_folder( pilotFile->addressCache, folder );
1396 * Remove empty folders (categories).
1398 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1404 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1408 ItemFolder *folder = node->data;
1409 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1410 if( folder->listPerson ) {
1411 /* Give name to folder */
1413 sprintf( name, "? %d", i );
1414 addritem_folder_set_name( folder, name );
1417 /* Mark for removal */
1418 remList = g_list_append( remList, folder );
1421 node = g_list_next( node );
1426 ItemFolder *folder = node->data;
1427 addrcache_remove_folder( pilotFile->addressCache, folder );
1428 node = g_list_next( node );
1430 g_list_free( remList );
1434 * ============================================================================================
1435 * Read file into list. Main entry point
1436 * Return: TRUE if file read successfully.
1437 * ============================================================================================
1439 gint jpilot_read_data( JPilotFile *pilotFile ) {
1440 g_return_val_if_fail( pilotFile != NULL, -1 );
1442 pilotFile->retVal = MGU_SUCCESS;
1443 pilotFile->addressCache->accessFlag = FALSE;
1444 if( jpilot_check_files( pilotFile ) ) {
1445 addrcache_clear( pilotFile->addressCache );
1446 jpilot_read_metadata( pilotFile );
1447 if( pilotFile->retVal == MGU_SUCCESS ) {
1448 jpilot_setup_labels( pilotFile );
1449 jpilot_build_category_list( pilotFile );
1450 pilotFile->retVal = jpilot_read_file( pilotFile );
1451 if( pilotFile->retVal == MGU_SUCCESS ) {
1452 jpilot_remove_empty( pilotFile );
1453 jpilot_mark_files( pilotFile );
1454 pilotFile->addressCache->modified = FALSE;
1455 pilotFile->addressCache->dataRead = TRUE;
1459 return pilotFile->retVal;
1463 * Return link list of persons.
1465 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1466 g_return_val_if_fail( pilotFile != NULL, NULL );
1467 return addrcache_get_list_person( pilotFile->addressCache );
1471 * Return link list of folders. This is always NULL since there are
1472 * no folders in GnomeCard.
1475 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1476 g_return_val_if_fail( pilotFile != NULL, NULL );
1477 return addrcache_get_list_folder( pilotFile->addressCache );
1481 * Return link list of all persons. Note that the list contains references
1482 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
1483 * this will destroy the addressbook data!
1484 * Return: List of items, or NULL if none.
1486 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1487 g_return_val_if_fail( pilotFile != NULL, NULL );
1488 return addrcache_get_all_persons( pilotFile->addressCache );
1492 * Check label list for specified label.
1494 gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1498 if( lblCheck == NULL ) return -1;
1499 if( strlen( lblCheck ) < 1 ) return -1;
1500 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1501 lblName = ai->labels[i];
1503 if( strlen( lblName ) ) {
1504 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1512 * Validate that all parameters specified.
1513 * Return: TRUE if data is good.
1515 gboolean jpilot_validate( JPilotFile *pilotFile ) {
1519 g_return_val_if_fail( pilotFile != NULL, FALSE );
1522 if( pilotFile->path ) {
1523 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1528 name = jpilot_get_name( pilotFile );
1530 if( strlen( name ) < 1 ) retVal = FALSE;
1538 #define WORK_BUFLEN 1024
1541 * Attempt to find a valid JPilot file.
1542 * Return: Filename, or home directory if not found, or empty string if
1543 * no home. Filename should be g_free() when done.
1545 gchar *jpilot_find_pilotdb( void ) {
1547 gchar str[ WORK_BUFLEN ];
1551 homedir = g_get_home_dir();
1552 if( ! homedir ) return g_strdup( "" );
1554 strcpy( str, homedir );
1555 len = strlen( str );
1557 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1558 str[ len ] = G_DIR_SEPARATOR;
1559 str[ ++len ] = '\0';
1562 strcat( str, JPILOT_DBHOME_DIR );
1563 strcat( str, G_DIR_SEPARATOR_S );
1564 strcat( str, JPILOT_DBHOME_FILE );
1566 /* Attempt to open */
1567 if( ( fp = fopen( str, "r" ) ) != NULL ) {
1571 /* Truncate filename */
1574 return g_strdup( str );
1578 * Attempt to read file, testing for valid JPilot format.
1579 * Return: TRUE if file appears to be valid format.
1581 gint jpilot_test_read_file( const gchar *fileSpec ) {
1582 JPilotFile *pilotFile;
1586 pilotFile = jpilot_create_path( fileSpec );
1587 retVal = jpilot_read_metadata( pilotFile );
1588 jpilot_free( pilotFile );
1592 retVal = MGU_NO_FILE;
1598 * Check whether label is in custom labels.
1599 * Return: TRUE if found.
1601 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1605 g_return_val_if_fail( pilotFile != NULL, FALSE );
1609 node = pilotFile->customLabels;
1611 if( g_strcasecmp( labelName, ( gchar * ) node->data ) == 0 ) {
1615 node = g_list_next( node );
1622 * Test whether pilot link library installed.
1623 * Return: TRUE if library available.
1625 gboolean jpilot_test_pilot_lib( void ) {
1629 #endif /* USE_JPILOT */