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.
39 #include <netinet/in.h>
41 #ifdef HAVE_LIBPISOCK_PI_ARGS_H
42 # include <libpisock/pi-args.h>
43 # include <libpisock/pi-appinfo.h>
44 # include <libpisock/pi-address.h>
47 # include <pi-appinfo.h>
48 # include <pi-address.h>
53 #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;
152 * Create new pilot file object.
154 JPilotFile *jpilot_create() {
155 JPilotFile *pilotFile;
156 pilotFile = g_new0( JPilotFile, 1 );
157 pilotFile->type = ADBOOKTYPE_JPILOT;
158 pilotFile->addressCache = addrcache_create();
159 pilotFile->accessFlag = FALSE;
160 pilotFile->retVal = MGU_SUCCESS;
162 pilotFile->file = NULL;
163 pilotFile->path = NULL;
164 pilotFile->readMetadata = FALSE;
165 pilotFile->customLabels = NULL;
166 pilotFile->labelInd = NULL;
167 pilotFile->havePC3 = FALSE;
168 pilotFile->pc3ModifyTime = 0;
173 * Create new pilot file object for specified file.
175 JPilotFile *jpilot_create_path( const gchar *path ) {
176 JPilotFile *pilotFile;
177 pilotFile = jpilot_create();
178 jpilot_set_file( pilotFile, path );
185 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
186 g_return_if_fail( pilotFile != NULL );
187 addrcache_set_name( pilotFile->addressCache, value );
189 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
190 g_return_if_fail( pilotFile != NULL );
191 addrcache_refresh( pilotFile->addressCache );
192 pilotFile->readMetadata = FALSE;
193 pilotFile->path = mgu_replace_string( pilotFile->path, value );
195 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
196 g_return_if_fail( pilotFile != NULL );
197 pilotFile->accessFlag = value;
200 gint jpilot_get_status( JPilotFile *pilotFile ) {
201 g_return_val_if_fail( pilotFile != NULL, -1 );
202 return pilotFile->retVal;
204 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
205 g_return_val_if_fail( pilotFile != NULL, NULL );
206 return addrcache_get_root_folder( pilotFile->addressCache );
208 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
209 g_return_val_if_fail( pilotFile != NULL, NULL );
210 return addrcache_get_name( pilotFile->addressCache );
214 * Test whether file was read.
215 * Return: TRUE if file was read.
217 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
218 g_return_val_if_fail( pilotFile != NULL, FALSE );
219 return pilotFile->addressCache->dataRead;
223 * Free up custom label list.
225 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
228 g_return_if_fail( pilotFile != NULL );
230 /* Release custom labels */
231 mgu_free_dlist( pilotFile->customLabels );
232 pilotFile->customLabels = NULL;
234 /* Release indexes */
235 node = pilotFile->labelInd;
238 node = g_list_next( node );
240 g_list_free( pilotFile->labelInd );
241 pilotFile->labelInd = NULL;
243 /* Force a fresh read */
244 addrcache_refresh( pilotFile->addressCache );
248 * Append a custom label, representing an E-Mail address field to the
251 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
252 g_return_if_fail( pilotFile != NULL );
255 gchar *labelCopy = g_strdup( labelName );
256 g_strstrip( labelCopy );
257 if( *labelCopy == '\0' ) {
261 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
262 /* Force a fresh read */
263 addrcache_refresh( pilotFile->addressCache );
269 * Get list of custom labels.
270 * Return: List of labels. Must use g_free() when done.
272 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
273 GList *retVal = NULL;
276 g_return_val_if_fail( pilotFile != NULL, NULL );
278 node = pilotFile->customLabels;
280 retVal = g_list_append( retVal, g_strdup( node->data ) );
281 node = g_list_next( node );
287 * Return filespec of PC3 file corresponding to JPilot PDB file.
288 * Note: Filespec should be g_free() when done.
290 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
294 if( pilotFile == NULL ) return NULL;
295 if( pilotFile->path == NULL ) return NULL;
297 fileSpec = g_strdup( pilotFile->path );
298 len = strlen( fileSpec );
301 for( i = len; i > 0; i-- ) {
302 if( *(fileSpec + i) == '.' ) {
309 if( len - pos == 3 ) {
310 *r++ = 'p'; *r++ = 'c'; *r = '3';
319 * Save PC3 file time to cache.
320 * return: TRUE if time marked.
322 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
323 gboolean retVal = FALSE;
324 struct stat filestat;
327 /* Mark PDB file cache */
328 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
330 /* Now mark PC3 file */
331 pilotFile->havePC3 = FALSE;
332 pilotFile->pc3ModifyTime = 0;
333 pcFile = jpilot_get_pc3_file( pilotFile );
334 if( pcFile == NULL ) return retVal;
335 if( 0 == lstat( pcFile, &filestat ) ) {
336 pilotFile->havePC3 = TRUE;
337 pilotFile->pc3ModifyTime = filestat.st_mtime;
345 * Check whether JPilot PDB or PC3 file has changed by comparing
347 * return: TRUE if file has changed.
349 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
350 gboolean retVal = TRUE;
351 struct stat filestat;
354 /* Check main file */
355 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
359 if( ! pilotFile->havePC3 ) return FALSE;
360 pcFile = jpilot_get_pc3_file( pilotFile );
361 if( pcFile == NULL ) return FALSE;
363 if( 0 == lstat( pcFile, &filestat ) ) {
364 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
371 * Test whether file was modified since last access.
372 * Return: TRUE if file was modified.
374 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
375 g_return_val_if_fail( pilotFile != NULL, FALSE );
376 return jpilot_check_files( pilotFile );
378 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
379 g_return_val_if_fail( pilotFile != NULL, FALSE );
380 return pilotFile->accessFlag;
384 * Free up pilot file object by releasing internal memory.
386 void jpilot_free( JPilotFile *pilotFile ) {
387 g_return_if_fail( pilotFile != NULL );
389 /* Release custom labels */
390 jpilot_clear_custom_labels( pilotFile );
393 addrcache_clear( pilotFile->addressCache );
394 addrcache_free( pilotFile->addressCache );
396 /* Free internal stuff */
397 g_free( pilotFile->path );
399 pilotFile->file = NULL;
400 pilotFile->path = NULL;
401 pilotFile->readMetadata = FALSE;
402 pilotFile->havePC3 = FALSE;
403 pilotFile->pc3ModifyTime = 0;
405 pilotFile->type = ADBOOKTYPE_NONE;
406 pilotFile->addressCache = NULL;
407 pilotFile->accessFlag = FALSE;
408 pilotFile->retVal = MGU_SUCCESS;
410 /* Now release file object */
415 * Refresh internal variables to force a file read.
417 void jpilot_force_refresh( JPilotFile *pilotFile ) {
418 addrcache_refresh( pilotFile->addressCache );
422 * Print object to specified stream.
424 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
427 g_return_if_fail( pilotFile != NULL );
429 fprintf( stream, "JPilotFile:\n" );
430 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
431 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
432 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
434 node = pilotFile->customLabels;
436 fprintf( stream, " c label: %s\n", (gchar *)node->data );
437 node = g_list_next( node );
440 node = pilotFile->labelInd;
442 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
443 node = g_list_next( node );
446 addrcache_print( pilotFile->addressCache, stream );
447 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
448 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
449 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
450 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
454 * Print summary of object to specified stream.
456 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
458 g_return_if_fail( pilotFile != NULL );
459 fprintf( stream, "JPilotFile:\n" );
460 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
461 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
462 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
464 node = pilotFile->customLabels;
466 fprintf( stream, " c label: %s\n", (gchar *)node->data );
467 node = g_list_next( node );
470 node = pilotFile->labelInd;
472 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
473 node = g_list_next( node );
475 addrcache_print( pilotFile->addressCache, stream );
476 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
477 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
480 /* Shamelessly copied from JPilot (libplugin.c) */
481 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
484 for (i=0;i<num_bytes;i++) {
490 /* Shamelessly copied from JPilot (utils.c) */
491 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
492 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
493 #define PILOT_TIME_DELTA (unsigned)(2082844800)
495 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
496 return (time_t)(raw_time - PILOT_TIME_DELTA);
499 /* Shamelessly copied from JPilot (libplugin.c) */
500 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
503 strncpy(dbh->db_name, rdbh->db_name, 31);
504 dbh->db_name[31] = '\0';
505 dbh->flags = bytes_to_bin(rdbh->flags, 2);
506 dbh->version = bytes_to_bin(rdbh->version, 2);
507 temp = bytes_to_bin(rdbh->creation_time, 4);
508 dbh->creation_time = pilot_time_to_unix_time(temp);
509 temp = bytes_to_bin(rdbh->modification_time, 4);
510 dbh->modification_time = pilot_time_to_unix_time(temp);
511 temp = bytes_to_bin(rdbh->backup_time, 4);
512 dbh->backup_time = pilot_time_to_unix_time(temp);
513 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
514 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
515 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
516 strncpy(dbh->type, rdbh->type, 4);
518 strncpy(dbh->creator_id, rdbh->creator_id, 4);
519 dbh->creator_id[4] = '\0';
520 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
521 dbh->unique_id_seed[4] = '\0';
522 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
523 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
527 /* Shamelessly copied from JPilot (libplugin.c) */
528 /* returns 1 if found */
530 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
531 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
533 mem_rec_header *temp_mem_rh;
534 unsigned char found = 0;
535 unsigned long found_at;
538 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
539 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
540 found_at = temp_mem_rh->offset;
541 /* *attrib = temp_mem_rh->attrib; */
542 /* *unique_id = temp_mem_rh->unique_id; */
544 if ((temp_mem_rh->offset == fpos)) {
546 *attrib = temp_mem_rh->attrib;
547 *unique_id = temp_mem_rh->unique_id;
550 *next_offset = found_at;
554 /* Shamelessly copied from JPilot (libplugin.c) */
555 static void free_mem_rec_header(mem_rec_header **mem_rh) {
556 mem_rec_header *h, *next_h;
557 for (h=*mem_rh; h; h=next_h) {
564 /* Shamelessly copied from JPilot (libplugin.c) */
565 static int jpilot_free_db_list( GList **br_list ) {
566 GList *temp_list, *first;
569 /* Go to first entry in the list */
571 for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
574 for (temp_list = first; temp_list; temp_list = temp_list->next) {
575 if (temp_list->data) {
579 temp_list->data=NULL;
584 g_list_free(*br_list);
589 /* Shamelessly copied from JPilot (libplugin.c) */
591 static int jpilot_get_info_size( FILE *in, int *size ) {
597 fseek(in, 0, SEEK_SET);
598 fread(&rdbh, sizeof(RawDBHeader), 1, in);
603 raw_header_to_header(&rdbh, &dbh);
604 if (dbh.app_info_offset==0) {
608 if (dbh.sort_info_offset!=0) {
609 *size = dbh.sort_info_offset - dbh.app_info_offset;
612 if (dbh.number_of_records==0) {
613 fseek(in, 0, SEEK_END);
614 *size=ftell(in) - dbh.app_info_offset;
618 fread(&rh, sizeof(record_header), 1, in);
619 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
620 *size=offset - dbh.app_info_offset;
626 * Read address file into address list. Based on JPilot's
627 * libplugin.c (jp_get_app_info)
629 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
632 unsigned int rec_size;
636 if( ( !buf_size ) || ( ! buf ) ) {
643 if( pilotFile->path ) {
644 in = fopen( pilotFile->path, "r" );
646 return MGU_OPEN_FILE;
653 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
657 return MGU_ERROR_READ;
665 /* Convert header into something recognizable */
666 raw_header_to_header(&rdbh, &dbh);
668 num = jpilot_get_info_size(in, &rec_size);
671 return MGU_ERROR_READ;
674 fseek(in, dbh.app_info_offset, SEEK_SET);
675 *buf = ( char * ) malloc(rec_size);
678 return MGU_OO_MEMORY;
680 num = fread(*buf, rec_size, 1, in);
685 return MGU_ERROR_READ;
690 *buf_size = rec_size;
695 /* Shamelessly copied from JPilot (libplugin.c) */
696 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
702 memcpy(&l, p, sizeof(l));
703 header->header_len=ntohl(l);
706 memcpy(&l, p, sizeof(l));
707 header->header_version=ntohl(l);
710 memcpy(&l, p, sizeof(l));
711 header->rec_len=ntohl(l);
714 memcpy(&l, p, sizeof(l));
715 header->unique_id=ntohl(l);
718 memcpy(&l, p, sizeof(l));
722 memcpy(&(header->attrib), p, sizeof(unsigned char));
723 p+=sizeof(unsigned char);
728 /* Shamelessly copied from JPilot (libplugin.c) */
729 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
730 unsigned long l, len;
731 unsigned char packed_header[256];
734 num = fread(&l, sizeof(l), 1, pc_in);
741 memcpy(packed_header, &l, sizeof(l));
746 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
753 unpack_header(header, packed_header);
757 /* Read next record from PC3 file. Based on JPilot's
758 * pc_read_next_rec (libplugin.c) */
759 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
760 PC3RecordHeader header;
767 num = read_header(in, &header);
770 return MGU_ERROR_READ;
776 rec_len = header.rec_len;
777 record = malloc(rec_len);
779 return MGU_OO_MEMORY;
781 num = fread(record, rec_len, 1, in);
785 return MGU_ERROR_READ;
789 br->unique_id = header.unique_id;
790 br->attrib = header.attrib;
798 * Read address file into a linked list. Based on JPilot's
799 * jp_read_DB_files (from libplugin.c)
801 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
805 int num_records, recs_returned, i, num, r;
806 unsigned int offset, prev_offset, next_offset, rec_size;
808 long fpos; /*file position indicator */
809 unsigned char attrib;
810 unsigned int unique_id;
811 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
818 mem_rh = last_mem_rh = NULL;
822 if( pilotFile->path == NULL ) {
826 in = fopen( pilotFile->path, "r" );
828 return MGU_OPEN_FILE;
831 /* Read the database header */
832 num = fread(&rdbh, sizeof(RawDBHeader), 1, in);
836 return MGU_ERROR_READ;
843 raw_header_to_header(&rdbh, &dbh);
845 /* Read each record entry header */
846 num_records = dbh.number_of_records;
850 for (i = 1; i < num_records + 1; i++) {
851 num = fread(&rh, sizeof(record_header), 1, in);
862 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
863 if (offset < prev_offset) {
866 prev_offset = offset;
867 temp_mem_rh = (mem_rec_header *)malloc(sizeof(mem_rec_header));
871 temp_mem_rh->next = NULL;
872 temp_mem_rh->rec_num = i;
873 temp_mem_rh->offset = offset;
874 temp_mem_rh->attrib = rh.attrib;
875 temp_mem_rh->unique_id = (rh.unique_ID[0]*256+rh.unique_ID[1])*256+rh.unique_ID[2];
876 if (mem_rh == NULL) {
877 mem_rh = temp_mem_rh;
878 last_mem_rh = temp_mem_rh;
880 last_mem_rh->next = temp_mem_rh;
881 last_mem_rh = temp_mem_rh;
885 temp_mem_rh = mem_rh;
889 find_next_offset(mem_rh, 0, &next_offset, &attrib, &unique_id);
892 next_offset = mem_rh->offset;
893 attrib = mem_rh->attrib;
894 unique_id = mem_rh->unique_id;
897 fseek(in, next_offset, SEEK_SET);
901 find_next_offset(mem_rh, fpos, &next_offset, &attrib, &unique_id);
903 next_offset = 0xFFFFFF;
905 attrib = temp_mem_rh->attrib;
906 unique_id = temp_mem_rh->unique_id;
907 if (temp_mem_rh->next) {
908 temp_mem_rh = temp_mem_rh->next;
909 next_offset = temp_mem_rh->offset;
913 rec_size = next_offset - fpos;
914 buf = malloc(rec_size);
916 num = fread(buf, rec_size, 1, in);
924 temp_br = malloc(sizeof(buf_rec));
928 temp_br->rt = PALM_REC;
929 temp_br->unique_id = unique_id;
930 temp_br->attrib = attrib;
932 temp_br->size = rec_size;
934 *records = g_list_append(*records, temp_br);
940 free_mem_rec_header(&mem_rh);
942 /* Read the PC3 file, if present */
943 pcFile = jpilot_get_pc3_file( pilotFile );
944 if( pcFile == NULL ) return MGU_SUCCESS;
945 pc_in = fopen( pcFile, "r");
948 if( pc_in == NULL ) {
952 while( ! feof( pc_in ) ) {
953 temp_br = malloc(sizeof(buf_rec));
957 r = jpilot_read_next_pc( pc_in, temp_br );
958 if ( r != MGU_SUCCESS ) {
962 if ((temp_br->rt!=DELETED_PC_REC)
963 &&(temp_br->rt!=DELETED_PALM_REC)
964 &&(temp_br->rt!=MODIFIED_PALM_REC)
965 &&(temp_br->rt!=DELETED_DELETED_PALM_REC)) {
966 *records = g_list_append(*records, temp_br);
969 if ((temp_br->rt==DELETED_PALM_REC) || (temp_br->rt==MODIFIED_PALM_REC)) {
972 while(temp_list->next) {
973 temp_list=temp_list->next;
976 for (; temp_list; temp_list=temp_list->prev) {
977 if (((buf_rec *)temp_list->data)->unique_id == temp_br->unique_id) {
978 ((buf_rec *)temp_list->data)->rt = temp_br->rt;
988 #define FULLNAME_BUFSIZE 256
989 #define EMAIL_BUFSIZE 256
991 * Unpack address, building new data inside cache.
993 static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] ) {
1000 gchar fullName[ FULLNAME_BUFSIZE ];
1001 gchar bufEMail[ EMAIL_BUFSIZE ];
1008 struct AddressAppInfo *ai;
1010 /* Retrieve address */
1011 num = unpack_Address( & addr, buf->buf, buf->size );
1013 addrEnt = addr.entry;
1014 attrib = buf->attrib;
1015 unique_id = buf->unique_id;
1016 cat_id = attrib & 0x0F;
1018 *fullName = *bufEMail = '\0';
1019 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1020 strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
1023 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1024 strcat( fullName, " " );
1025 strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
1027 g_strchug( fullName );
1028 g_strchomp( fullName );
1030 person = addritem_create_item_person();
1031 addritem_person_set_common_name( person, fullName );
1032 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1033 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1034 addrcache_id_person( pilotFile->addressCache, person );
1036 extID = g_strdup_printf( "%d", unique_id );
1037 addritem_person_set_external_id( person, extID );
1041 /* Pointer to address metadata. */
1042 ai = & pilotFile->addrInfo;
1044 /* Add entry for each email address listed under phone labels. */
1045 indPhoneLbl = addr.phoneLabel;
1046 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1048 ind = indPhoneLbl[k];
1050 * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
1051 * ai->phoneLabels[ind], addrEnt[3+k] );
1053 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1054 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1056 strcpy( bufEMail, labelEntry );
1057 g_strchug( bufEMail );
1058 g_strchomp( bufEMail );
1060 email = addritem_create_item_email();
1061 addritem_email_set_address( email, bufEMail );
1062 addrcache_id_email( pilotFile->addressCache, email );
1063 addrcache_person_add_email
1064 ( pilotFile->addressCache, person, email );
1069 /* Add entry for each custom label */
1070 node = pilotFile->labelInd;
1073 ind = GPOINTER_TO_INT( node->data );
1076 * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
1079 labelEntry = addrEnt[ind];
1081 strcpy( bufEMail, labelEntry );
1082 g_strchug( bufEMail );
1083 g_strchomp( bufEMail );
1085 email = addritem_create_item_email();
1086 addritem_email_set_address( email, bufEMail );
1087 addritem_email_set_remarks( email, ai->labels[ind] );
1088 addrcache_id_email( pilotFile->addressCache, email );
1089 addrcache_person_add_email
1090 ( pilotFile->addressCache, person, email );
1094 node = g_list_next( node );
1097 if( person->listEMail ) {
1098 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1099 /* Add to specified category */
1100 addrcache_folder_add_person
1101 ( pilotFile->addressCache, folderInd[cat_id], person );
1103 /* Add to root folder */
1104 addrcache_add_person( pilotFile->addressCache, person );
1107 addritem_free_item_person( person );
1114 * Free up address list.
1116 static void jpilot_free_addrlist( GList *records ) {
1125 node = g_list_next( node );
1129 g_list_free( records );
1133 * Read address file into address cache.
1135 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1137 GList *records = NULL;
1140 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1142 retVal = jpilot_read_db_files( pilotFile, &records );
1143 if( retVal != MGU_SUCCESS ) {
1144 jpilot_free_addrlist( records );
1148 /* Build array of pointers to categories */
1150 node = addrcache_get_list_folder( pilotFile->addressCache );
1152 if( i < JPILOT_NUM_CATEG ) {
1153 folderInd[i] = node->data;
1155 node = g_list_next( node );
1159 /* Load all addresses, free up old stuff as we go */
1163 if( ( br->rt != DELETED_PC_REC ) &&
1164 ( br->rt != DELETED_PALM_REC ) &&
1165 ( br->rt != MODIFIED_PALM_REC ) &&
1166 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1167 jpilot_load_address( pilotFile, br, folderInd );
1171 node = g_list_next( node );
1175 g_list_free( records );
1182 * Read metadata from file.
1184 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1186 unsigned int rec_size;
1190 g_return_val_if_fail( pilotFile != NULL, -1 );
1192 pilotFile->readMetadata = FALSE;
1193 addrcache_clear( pilotFile->addressCache );
1195 /* Read file info */
1196 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1197 if( retVal != MGU_SUCCESS ) {
1198 pilotFile->retVal = retVal;
1199 return pilotFile->retVal;
1202 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1207 pilotFile->retVal = MGU_ERROR_READ;
1208 return pilotFile->retVal;
1211 pilotFile->readMetadata = TRUE;
1212 pilotFile->retVal = MGU_SUCCESS;
1213 return pilotFile->retVal;
1217 * Setup labels and indexes from metadata.
1218 * Return: TRUE is setup successfully.
1220 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1221 gboolean retVal = FALSE;
1222 struct AddressAppInfo *ai;
1225 g_return_val_if_fail( pilotFile != NULL, -1 );
1227 /* Release indexes */
1228 node = pilotFile->labelInd;
1231 node = g_list_next( node );
1233 pilotFile->labelInd = NULL;
1235 if( pilotFile->readMetadata ) {
1236 ai = & pilotFile->addrInfo;
1237 node = pilotFile->customLabels;
1239 gchar *lbl = node->data;
1242 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1243 gchar *labelName = ai->labels[i];
1244 if( g_strcasecmp( labelName, lbl ) == 0 ) {
1249 pilotFile->labelInd = g_list_append( pilotFile->labelInd, GINT_TO_POINTER(ind) );
1250 node = g_list_next( node );
1258 * Load list with character strings of label names.
1260 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
1263 g_return_val_if_fail( pilotFile != NULL, NULL );
1265 if( pilotFile->readMetadata ) {
1266 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1267 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1268 gchar *labelName = ai->labels[i];
1270 labelList = g_list_append( labelList, g_strdup( labelName ) );
1273 labelList = g_list_append( labelList, g_strdup( "" ) );
1281 * Return category name for specified category ID.
1282 * Enter: Category ID.
1283 * Return: Name, or empty string if not invalid ID. Name should be g_free() when done.
1285 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
1286 gchar *catName = NULL;
1288 g_return_val_if_fail( pilotFile != NULL, NULL );
1290 if( pilotFile->readMetadata ) {
1291 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1292 struct CategoryAppInfo *cat = & ai->category;
1293 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
1296 catName = g_strdup( cat->name[catID] );
1299 if( ! catName ) catName = g_strdup( "" );
1304 * Load list with character strings of phone label names.
1306 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
1309 g_return_val_if_fail( pilotFile != NULL, NULL );
1311 if( pilotFile->readMetadata ) {
1312 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1313 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1314 gchar *labelName = ai->phoneLabels[i];
1316 labelList = g_list_append( labelList, g_strdup( labelName ) );
1319 labelList = g_list_append( labelList, g_strdup( "" ) );
1327 * Load list with character strings of label names. Only none blank names
1329 * Return: list of labels. Should by g_free()'d when done.
1331 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1334 g_return_val_if_fail( pilotFile != NULL, NULL );
1336 if( pilotFile->readMetadata ) {
1337 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1338 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1339 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1341 g_strchomp( labelName );
1342 g_strchug( labelName );
1343 if( *labelName != '\0' ) {
1344 labelList = g_list_append( labelList, g_strdup( labelName ) );
1353 * Load list with character strings of category names.
1355 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1356 GList *catList = NULL;
1359 g_return_val_if_fail( pilotFile != NULL, NULL );
1361 if( pilotFile->readMetadata ) {
1362 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1363 struct CategoryAppInfo *cat = & ai->category;
1364 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1365 gchar *catName = cat->name[i];
1367 catList = g_list_append( catList, g_strdup( catName ) );
1370 catList = g_list_append( catList, g_strdup( "" ) );
1378 * Build folder for each category.
1380 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1381 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1382 struct CategoryAppInfo *cat = & ai->category;
1385 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1386 ItemFolder *folder = addritem_create_item_folder();
1387 addritem_folder_set_name( folder, cat->name[i] );
1388 addrcache_id_folder( pilotFile->addressCache, folder );
1389 addrcache_add_folder( pilotFile->addressCache, folder );
1394 * Remove empty folders (categories).
1396 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1402 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1406 ItemFolder *folder = node->data;
1407 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1408 if( folder->listPerson ) {
1409 /* Give name to folder */
1411 sprintf( name, "? %d", i );
1412 addritem_folder_set_name( folder, name );
1415 /* Mark for removal */
1416 remList = g_list_append( remList, folder );
1419 node = g_list_next( node );
1424 ItemFolder *folder = node->data;
1425 addrcache_remove_folder( pilotFile->addressCache, folder );
1426 node = g_list_next( node );
1428 g_list_free( remList );
1432 * ============================================================================================
1433 * Read file into list. Main entry point
1434 * Return: TRUE if file read successfully.
1435 * ============================================================================================
1437 gint jpilot_read_data( JPilotFile *pilotFile ) {
1438 g_return_val_if_fail( pilotFile != NULL, -1 );
1440 pilotFile->retVal = MGU_SUCCESS;
1441 pilotFile->accessFlag = FALSE;
1443 if( jpilot_check_files( pilotFile ) ) {
1444 addrcache_clear( pilotFile->addressCache );
1445 jpilot_read_metadata( pilotFile );
1446 if( pilotFile->retVal == MGU_SUCCESS ) {
1447 jpilot_setup_labels( pilotFile );
1448 jpilot_build_category_list( pilotFile );
1449 pilotFile->retVal = jpilot_read_file( pilotFile );
1450 if( pilotFile->retVal == MGU_SUCCESS ) {
1451 jpilot_remove_empty( pilotFile );
1452 jpilot_mark_files( pilotFile );
1453 pilotFile->addressCache->modified = FALSE;
1454 pilotFile->addressCache->dataRead = TRUE;
1458 return pilotFile->retVal;
1462 * Return link list of persons.
1464 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1465 g_return_val_if_fail( pilotFile != NULL, NULL );
1466 return addrcache_get_list_person( pilotFile->addressCache );
1470 * Return link list of folders. This is always NULL since there are
1471 * no folders in GnomeCard.
1474 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1475 g_return_val_if_fail( pilotFile != NULL, NULL );
1476 return addrcache_get_list_folder( pilotFile->addressCache );
1480 * Return link list of all persons. Note that the list contains references
1481 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
1482 * this will destroy the addressbook data!
1483 * Return: List of items, or NULL if none.
1485 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1486 g_return_val_if_fail( pilotFile != NULL, NULL );
1487 return addrcache_get_all_persons( pilotFile->addressCache );
1491 * Check label list for specified label.
1493 gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1497 if( lblCheck == NULL ) return -1;
1498 if( strlen( lblCheck ) < 1 ) return -1;
1499 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1500 lblName = ai->labels[i];
1502 if( strlen( lblName ) ) {
1503 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1511 * Validate that all parameters specified.
1512 * Return: TRUE if data is good.
1514 gboolean jpilot_validate( JPilotFile *pilotFile ) {
1518 g_return_val_if_fail( pilotFile != NULL, FALSE );
1521 if( pilotFile->path ) {
1522 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1527 name = jpilot_get_name( pilotFile );
1529 if( strlen( name ) < 1 ) retVal = FALSE;
1537 #define WORK_BUFLEN 1024
1540 * Attempt to find a valid JPilot file.
1541 * Return: Filename, or home directory if not found, or empty string if
1542 * no home. Filename should be g_free() when done.
1544 gchar *jpilot_find_pilotdb( void ) {
1546 gchar str[ WORK_BUFLEN ];
1550 homedir = g_get_home_dir();
1551 if( ! homedir ) return g_strdup( "" );
1553 strcpy( str, homedir );
1554 len = strlen( str );
1556 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1557 str[ len ] = G_DIR_SEPARATOR;
1558 str[ ++len ] = '\0';
1561 strcat( str, JPILOT_DBHOME_DIR );
1562 strcat( str, G_DIR_SEPARATOR_S );
1563 strcat( str, JPILOT_DBHOME_FILE );
1565 /* Attempt to open */
1566 if( ( fp = fopen( str, "r" ) ) != NULL ) {
1570 /* Truncate filename */
1573 return g_strdup( str );
1577 * Attempt to read file, testing for valid JPilot format.
1578 * Return: TRUE if file appears to be valid format.
1580 gint jpilot_test_read_file( const gchar *fileSpec ) {
1581 JPilotFile *pilotFile;
1585 pilotFile = jpilot_create_path( fileSpec );
1586 retVal = jpilot_read_metadata( pilotFile );
1587 jpilot_free( pilotFile );
1591 retVal = MGU_NO_FILE;
1597 * Check whether label is in custom labels.
1598 * Return: TRUE if found.
1600 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1604 g_return_val_if_fail( pilotFile != NULL, FALSE );
1608 node = pilotFile->customLabels;
1610 if( g_strcasecmp( labelName, ( gchar * ) node->data ) == 0 ) {
1614 node = g_list_next( node );
1621 * Test whether pilot link library installed.
1622 * Return: TRUE if library available.
1624 gboolean jpilot_test_pilot_lib( void ) {
1627 handle = dlopen( PILOT_LINK_LIB_NAME, RTLD_LAZY );
1632 /* Test for symbols we need */
1633 fun = dlsym( handle, "unpack_Address" );
1639 fun = dlsym( handle, "unpack_AddressAppInfo" );
1648 #endif /* USE_JPILOT */