2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 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 <dlfcn.h> */
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"
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->name = NULL;
157 pilotFile->file = NULL;
158 pilotFile->path = NULL;
159 pilotFile->addressCache = addrcache_create();
160 pilotFile->readMetadata = FALSE;
161 pilotFile->customLabels = NULL;
162 pilotFile->labelInd = NULL;
163 pilotFile->retVal = MGU_SUCCESS;
164 pilotFile->accessFlag = FALSE;
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 pilotFile->name = mgu_replace_string( pilotFile->name, 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->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 pilotFile->name;
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 return jpilot_check_files( pilotFile );
376 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
377 g_return_val_if_fail( pilotFile != NULL, FALSE );
378 return pilotFile->accessFlag;
382 * Free up pilot file object by releasing internal memory.
384 void jpilot_free( JPilotFile *pilotFile ) {
385 g_return_if_fail( pilotFile != NULL );
387 /* Free internal stuff */
388 g_free( pilotFile->path );
390 /* Release custom labels */
391 jpilot_clear_custom_labels( pilotFile );
394 addrcache_clear( pilotFile->addressCache );
395 addrcache_free( pilotFile->addressCache );
396 pilotFile->addressCache = NULL;
397 pilotFile->readMetadata = FALSE;
398 pilotFile->accessFlag = FALSE;
399 pilotFile->havePC3 = FALSE;
400 pilotFile->pc3ModifyTime = 0;
402 /* Now release file object */
407 * Refresh internal variables to force a file read.
409 void jpilot_force_refresh( JPilotFile *pilotFile ) {
410 addrcache_refresh( pilotFile->addressCache );
414 * Print object to specified stream.
416 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
419 g_return_if_fail( pilotFile != NULL );
421 fprintf( stream, "JPilotFile:\n" );
422 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
423 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
424 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
426 node = pilotFile->customLabels;
428 fprintf( stream, " c label: %s\n", (gchar *)node->data );
429 node = g_list_next( node );
432 node = pilotFile->labelInd;
434 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
435 node = g_list_next( node );
438 addrcache_print( pilotFile->addressCache, stream );
439 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
440 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
441 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
442 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
446 * Print summary of object to specified stream.
448 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
450 g_return_if_fail( pilotFile != NULL );
451 fprintf( stream, "JPilotFile:\n" );
452 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
453 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
454 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
456 node = pilotFile->customLabels;
458 fprintf( stream, " c label: %s\n", (gchar *)node->data );
459 node = g_list_next( node );
462 node = pilotFile->labelInd;
464 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
465 node = g_list_next( node );
467 addrcache_print( pilotFile->addressCache, stream );
468 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
469 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
472 /* Shamelessly copied from JPilot (libplugin.c) */
473 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
476 for (i=0;i<num_bytes;i++) {
482 /* Shamelessly copied from JPilot (utils.c) */
483 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
484 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
485 #define PILOT_TIME_DELTA (unsigned)(2082844800)
487 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
488 return (time_t)(raw_time - PILOT_TIME_DELTA);
491 /* Shamelessly copied from JPilot (libplugin.c) */
492 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
495 strncpy(dbh->db_name, rdbh->db_name, 31);
496 dbh->db_name[31] = '\0';
497 dbh->flags = bytes_to_bin(rdbh->flags, 2);
498 dbh->version = bytes_to_bin(rdbh->version, 2);
499 temp = bytes_to_bin(rdbh->creation_time, 4);
500 dbh->creation_time = pilot_time_to_unix_time(temp);
501 temp = bytes_to_bin(rdbh->modification_time, 4);
502 dbh->modification_time = pilot_time_to_unix_time(temp);
503 temp = bytes_to_bin(rdbh->backup_time, 4);
504 dbh->backup_time = pilot_time_to_unix_time(temp);
505 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
506 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
507 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
508 strncpy(dbh->type, rdbh->type, 4);
510 strncpy(dbh->creator_id, rdbh->creator_id, 4);
511 dbh->creator_id[4] = '\0';
512 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
513 dbh->unique_id_seed[4] = '\0';
514 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
515 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
519 /* Shamelessly copied from JPilot (libplugin.c) */
520 /* returns 1 if found */
522 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
523 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
525 mem_rec_header *temp_mem_rh;
526 unsigned char found = 0;
527 unsigned long found_at;
530 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
531 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
532 found_at = temp_mem_rh->offset;
533 /* *attrib = temp_mem_rh->attrib; */
534 /* *unique_id = temp_mem_rh->unique_id; */
536 if ((temp_mem_rh->offset == fpos)) {
538 *attrib = temp_mem_rh->attrib;
539 *unique_id = temp_mem_rh->unique_id;
542 *next_offset = found_at;
546 /* Shamelessly copied from JPilot (libplugin.c) */
547 static void free_mem_rec_header(mem_rec_header **mem_rh) {
548 mem_rec_header *h, *next_h;
549 for (h=*mem_rh; h; h=next_h) {
556 /* Shamelessly copied from JPilot (libplugin.c) */
557 static int jpilot_free_db_list( GList **br_list ) {
558 GList *temp_list, *first;
561 /* Go to first entry in the list */
563 for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
566 for (temp_list = first; temp_list; temp_list = temp_list->next) {
567 if (temp_list->data) {
571 temp_list->data=NULL;
576 g_list_free(*br_list);
581 /* Shamelessly copied from JPilot (libplugin.c) */
583 static int jpilot_get_info_size( FILE *in, int *size ) {
589 fseek(in, 0, SEEK_SET);
590 fread(&rdbh, sizeof(RawDBHeader), 1, in);
595 raw_header_to_header(&rdbh, &dbh);
596 if (dbh.app_info_offset==0) {
600 if (dbh.sort_info_offset!=0) {
601 *size = dbh.sort_info_offset - dbh.app_info_offset;
604 if (dbh.number_of_records==0) {
605 fseek(in, 0, SEEK_END);
606 *size=ftell(in) - dbh.app_info_offset;
610 fread(&rh, sizeof(record_header), 1, in);
611 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
612 *size=offset - dbh.app_info_offset;
618 * Read address file into address list. Based on JPilot's
619 * libplugin.c (jp_get_app_info)
621 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
624 unsigned int rec_size;
628 if( ( !buf_size ) || ( ! buf ) ) {
635 if( pilotFile->path ) {
636 in = fopen( pilotFile->path, "r" );
638 return MGU_OPEN_FILE;
645 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
649 return MGU_ERROR_READ;
657 /* Convert header into something recognizable */
658 raw_header_to_header(&rdbh, &dbh);
660 num = jpilot_get_info_size(in, &rec_size);
663 return MGU_ERROR_READ;
666 fseek(in, dbh.app_info_offset, SEEK_SET);
667 *buf = ( char * ) malloc(rec_size);
670 return MGU_OO_MEMORY;
672 num = fread(*buf, rec_size, 1, in);
677 return MGU_ERROR_READ;
682 *buf_size = rec_size;
687 /* Shamelessly copied from JPilot (libplugin.c) */
688 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
694 memcpy(&l, p, sizeof(l));
695 header->header_len=ntohl(l);
698 memcpy(&l, p, sizeof(l));
699 header->header_version=ntohl(l);
702 memcpy(&l, p, sizeof(l));
703 header->rec_len=ntohl(l);
706 memcpy(&l, p, sizeof(l));
707 header->unique_id=ntohl(l);
710 memcpy(&l, p, sizeof(l));
714 memcpy(&(header->attrib), p, sizeof(unsigned char));
715 p+=sizeof(unsigned char);
720 /* Shamelessly copied from JPilot (libplugin.c) */
721 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
722 unsigned long l, len;
723 unsigned char packed_header[256];
726 num = fread(&l, sizeof(l), 1, pc_in);
733 memcpy(packed_header, &l, sizeof(l));
738 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
745 unpack_header(header, packed_header);
749 /* Read next record from PC3 file. Based on JPilot's
750 * pc_read_next_rec (libplugin.c) */
751 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
752 PC3RecordHeader header;
759 num = read_header(in, &header);
762 return MGU_ERROR_READ;
768 rec_len = header.rec_len;
769 record = malloc(rec_len);
771 return MGU_OO_MEMORY;
773 num = fread(record, rec_len, 1, in);
777 return MGU_ERROR_READ;
781 br->unique_id = header.unique_id;
782 br->attrib = header.attrib;
790 * Read address file into a linked list. Based on JPilot's
791 * jp_read_DB_files (from libplugin.c)
793 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
797 int num_records, recs_returned, i, num, r;
798 unsigned int offset, prev_offset, next_offset, rec_size;
800 long fpos; /*file position indicator */
801 unsigned char attrib;
802 unsigned int unique_id;
803 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
810 mem_rh = last_mem_rh = NULL;
814 if( pilotFile->path == NULL ) {
818 in = fopen( pilotFile->path, "r" );
820 return MGU_OPEN_FILE;
823 /* Read the database header */
824 num = fread(&rdbh, sizeof(RawDBHeader), 1, in);
828 return MGU_ERROR_READ;
834 raw_header_to_header(&rdbh, &dbh);
836 /* Read each record entry header */
837 num_records = dbh.number_of_records;
841 for (i = 1; i < num_records + 1; i++) {
842 num = fread(&rh, sizeof(record_header), 1, in);
852 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
853 if (offset < prev_offset) {
856 prev_offset = offset;
857 temp_mem_rh = (mem_rec_header *)malloc(sizeof(mem_rec_header));
861 temp_mem_rh->next = NULL;
862 temp_mem_rh->rec_num = i;
863 temp_mem_rh->offset = offset;
864 temp_mem_rh->attrib = rh.attrib;
865 temp_mem_rh->unique_id = (rh.unique_ID[0]*256+rh.unique_ID[1])*256+rh.unique_ID[2];
866 if (mem_rh == NULL) {
867 mem_rh = temp_mem_rh;
868 last_mem_rh = temp_mem_rh;
870 last_mem_rh->next = temp_mem_rh;
871 last_mem_rh = temp_mem_rh;
875 temp_mem_rh = mem_rh;
879 find_next_offset(mem_rh, 0, &next_offset, &attrib, &unique_id);
882 next_offset = mem_rh->offset;
883 attrib = mem_rh->attrib;
884 unique_id = mem_rh->unique_id;
887 fseek(in, next_offset, SEEK_SET);
891 find_next_offset(mem_rh, fpos, &next_offset, &attrib, &unique_id);
893 next_offset = 0xFFFFFF;
895 attrib = temp_mem_rh->attrib;
896 unique_id = temp_mem_rh->unique_id;
897 if (temp_mem_rh->next) {
898 temp_mem_rh = temp_mem_rh->next;
899 next_offset = temp_mem_rh->offset;
903 rec_size = next_offset - fpos;
904 buf = malloc(rec_size);
906 num = fread(buf, rec_size, 1, in);
914 temp_br = malloc(sizeof(buf_rec));
918 temp_br->rt = PALM_REC;
919 temp_br->unique_id = unique_id;
920 temp_br->attrib = attrib;
922 temp_br->size = rec_size;
924 *records = g_list_append(*records, temp_br);
930 free_mem_rec_header(&mem_rh);
932 /* Read the PC3 file, if present */
933 pcFile = jpilot_get_pc3_file( pilotFile );
934 if( pcFile == NULL ) return MGU_SUCCESS;
935 pc_in = fopen( pcFile, "r");
938 if( pc_in == NULL ) {
942 while( ! feof( pc_in ) ) {
943 temp_br = malloc(sizeof(buf_rec));
947 r = jpilot_read_next_pc( pc_in, temp_br );
948 if ( r != MGU_SUCCESS ) {
952 if ((temp_br->rt!=DELETED_PC_REC)
953 &&(temp_br->rt!=DELETED_PALM_REC)
954 &&(temp_br->rt!=MODIFIED_PALM_REC)
955 &&(temp_br->rt!=DELETED_DELETED_PALM_REC)) {
956 *records = g_list_append(*records, temp_br);
959 if ((temp_br->rt==DELETED_PALM_REC) || (temp_br->rt==MODIFIED_PALM_REC)) {
962 while(temp_list->next) {
963 temp_list=temp_list->next;
966 for (; temp_list; temp_list=temp_list->prev) {
967 if (((buf_rec *)temp_list->data)->unique_id == temp_br->unique_id) {
968 ((buf_rec *)temp_list->data)->rt = temp_br->rt;
978 #define FULLNAME_BUFSIZE 256
979 #define EMAIL_BUFSIZE 256
981 * Unpack address, building new data inside cache.
983 static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] ) {
990 gchar fullName[ FULLNAME_BUFSIZE ];
991 gchar bufEMail[ EMAIL_BUFSIZE ];
998 struct AddressAppInfo *ai;
1000 /* Retrieve address */
1001 num = unpack_Address( & addr, buf->buf, buf->size );
1003 addrEnt = addr.entry;
1004 attrib = buf->attrib;
1005 unique_id = buf->unique_id;
1006 cat_id = attrib & 0x0F;
1008 *fullName = *bufEMail = '\0';
1009 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1010 strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
1013 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1014 strcat( fullName, " " );
1015 strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
1017 g_strchug( fullName );
1018 g_strchomp( fullName );
1020 person = addritem_create_item_person();
1021 addritem_person_set_common_name( person, fullName );
1022 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1023 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1024 addrcache_id_person( pilotFile->addressCache, person );
1026 extID = g_strdup_printf( "%d", unique_id );
1027 addritem_person_set_external_id( person, extID );
1031 /* Pointer to address metadata. */
1032 ai = & pilotFile->addrInfo;
1034 /* Add entry for each email address listed under phone labels. */
1035 indPhoneLbl = addr.phoneLabel;
1036 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1038 ind = indPhoneLbl[k];
1040 * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
1041 * ai->phoneLabels[ind], addrEnt[3+k] );
1043 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1044 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1046 strcpy( bufEMail, labelEntry );
1047 g_strchug( bufEMail );
1048 g_strchomp( bufEMail );
1050 email = addritem_create_item_email();
1051 addritem_email_set_address( email, bufEMail );
1052 addrcache_id_email( pilotFile->addressCache, email );
1053 addrcache_person_add_email
1054 ( pilotFile->addressCache, person, email );
1059 /* Add entry for each custom label */
1060 node = pilotFile->labelInd;
1063 ind = GPOINTER_TO_INT( node->data );
1066 * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
1069 labelEntry = addrEnt[ind];
1071 strcpy( bufEMail, labelEntry );
1072 g_strchug( bufEMail );
1073 g_strchomp( bufEMail );
1075 email = addritem_create_item_email();
1076 addritem_email_set_address( email, bufEMail );
1077 addritem_email_set_remarks( email, ai->labels[ind] );
1078 addrcache_id_email( pilotFile->addressCache, email );
1079 addrcache_person_add_email
1080 ( pilotFile->addressCache, person, email );
1084 node = g_list_next( node );
1087 if( person->listEMail ) {
1088 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1089 /* Add to specified category */
1090 addrcache_folder_add_person
1091 ( pilotFile->addressCache, folderInd[cat_id], person );
1093 /* Add to root folder */
1094 addrcache_add_person( pilotFile->addressCache, person );
1097 addritem_free_item_person( person );
1104 * Free up address list.
1106 static void jpilot_free_addrlist( GList *records ) {
1115 node = g_list_next( node );
1119 g_list_free( records );
1123 * Read address file into address cache.
1125 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1127 GList *records = NULL;
1130 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1132 retVal = jpilot_read_db_files( pilotFile, &records );
1133 if( retVal != MGU_SUCCESS ) {
1134 jpilot_free_addrlist( records );
1138 /* Build array of pointers to categories */
1140 node = addrcache_get_list_folder( pilotFile->addressCache );
1142 if( i < JPILOT_NUM_CATEG ) {
1143 folderInd[i] = node->data;
1145 node = g_list_next( node );
1149 /* Load all addresses, free up old stuff as we go */
1153 if( ( br->rt != DELETED_PC_REC ) &&
1154 ( br->rt != DELETED_PALM_REC ) &&
1155 ( br->rt != MODIFIED_PALM_REC ) &&
1156 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1157 jpilot_load_address( pilotFile, br, folderInd );
1161 node = g_list_next( node );
1165 g_list_free( records );
1172 * Read metadata from file.
1174 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1176 unsigned int rec_size;
1180 g_return_val_if_fail( pilotFile != NULL, -1 );
1182 pilotFile->readMetadata = FALSE;
1183 addrcache_clear( pilotFile->addressCache );
1185 /* Read file info */
1186 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1187 if( retVal != MGU_SUCCESS ) {
1188 pilotFile->retVal = retVal;
1189 return pilotFile->retVal;
1192 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1197 pilotFile->retVal = MGU_ERROR_READ;
1198 return pilotFile->retVal;
1201 pilotFile->readMetadata = TRUE;
1202 pilotFile->retVal = MGU_SUCCESS;
1203 return pilotFile->retVal;
1207 * Setup labels and indexes from metadata.
1208 * Return: TRUE is setup successfully.
1210 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1211 gboolean retVal = FALSE;
1212 struct AddressAppInfo *ai;
1215 g_return_val_if_fail( pilotFile != NULL, -1 );
1217 /* Release indexes */
1218 node = pilotFile->labelInd;
1221 node = g_list_next( node );
1223 pilotFile->labelInd = NULL;
1225 if( pilotFile->readMetadata ) {
1226 ai = & pilotFile->addrInfo;
1227 node = pilotFile->customLabels;
1229 gchar *lbl = node->data;
1232 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1233 gchar *labelName = ai->labels[i];
1234 if( g_strcasecmp( labelName, lbl ) == 0 ) {
1239 pilotFile->labelInd = g_list_append( pilotFile->labelInd, GINT_TO_POINTER(ind) );
1240 node = g_list_next( node );
1248 * Load list with character strings of label names.
1250 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
1253 g_return_val_if_fail( pilotFile != NULL, NULL );
1255 if( pilotFile->readMetadata ) {
1256 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1257 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1258 gchar *labelName = ai->labels[i];
1260 labelList = g_list_append( labelList, g_strdup( labelName ) );
1263 labelList = g_list_append( labelList, g_strdup( "" ) );
1271 * Return category name for specified category ID.
1272 * Enter: Category ID.
1273 * Return: Name, or empty string if not invalid ID. Name should be g_free() when done.
1275 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
1276 gchar *catName = NULL;
1278 g_return_val_if_fail( pilotFile != NULL, NULL );
1280 if( pilotFile->readMetadata ) {
1281 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1282 struct CategoryAppInfo *cat = & ai->category;
1283 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
1286 catName = g_strdup( cat->name[catID] );
1289 if( ! catName ) catName = g_strdup( "" );
1294 * Load list with character strings of phone label names.
1296 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
1299 g_return_val_if_fail( pilotFile != NULL, NULL );
1301 if( pilotFile->readMetadata ) {
1302 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1303 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1304 gchar *labelName = ai->phoneLabels[i];
1306 labelList = g_list_append( labelList, g_strdup( labelName ) );
1309 labelList = g_list_append( labelList, g_strdup( "" ) );
1317 * Load list with character strings of label names. Only none blank names
1320 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1323 g_return_val_if_fail( pilotFile != NULL, NULL );
1325 if( pilotFile->readMetadata ) {
1326 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1327 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1328 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1330 g_strchomp( labelName );
1331 g_strchug( labelName );
1332 if( *labelName != '\0' ) {
1333 labelList = g_list_append( labelList, g_strdup( labelName ) );
1342 * Load list with character strings of category names.
1344 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1345 GList *catList = NULL;
1348 g_return_val_if_fail( pilotFile != NULL, NULL );
1350 if( pilotFile->readMetadata ) {
1351 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1352 struct CategoryAppInfo *cat = & ai->category;
1353 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1354 gchar *catName = cat->name[i];
1356 catList = g_list_append( catList, g_strdup( catName ) );
1359 catList = g_list_append( catList, g_strdup( "" ) );
1367 * Build folder for each category.
1369 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1370 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1371 struct CategoryAppInfo *cat = & ai->category;
1374 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1375 ItemFolder *folder = addritem_create_item_folder();
1376 addritem_folder_set_name( folder, cat->name[i] );
1377 addrcache_id_folder( pilotFile->addressCache, folder );
1378 addrcache_add_folder( pilotFile->addressCache, folder );
1383 * Remove empty folders (categories).
1385 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1391 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1395 ItemFolder *folder = node->data;
1396 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1397 if( folder->listPerson ) {
1398 /* Give name to folder */
1400 sprintf( name, "? %d", i );
1401 addritem_folder_set_name( folder, name );
1404 /* Mark for removal */
1405 remList = g_list_append( remList, folder );
1408 node = g_list_next( node );
1413 ItemFolder *folder = node->data;
1414 addrcache_remove_folder( pilotFile->addressCache, folder );
1415 node = g_list_next( node );
1417 g_list_free( remList );
1421 * ============================================================================================
1422 * Read file into list. Main entry point
1423 * Return: TRUE if file read successfully.
1424 * ============================================================================================
1426 gint jpilot_read_data( JPilotFile *pilotFile ) {
1427 g_return_val_if_fail( pilotFile != NULL, -1 );
1429 pilotFile->retVal = MGU_SUCCESS;
1430 pilotFile->accessFlag = FALSE;
1432 if( jpilot_check_files( pilotFile ) ) {
1433 addrcache_clear( pilotFile->addressCache );
1434 jpilot_read_metadata( pilotFile );
1435 if( pilotFile->retVal == MGU_SUCCESS ) {
1436 jpilot_setup_labels( pilotFile );
1437 jpilot_build_category_list( pilotFile );
1438 pilotFile->retVal = jpilot_read_file( pilotFile );
1439 if( pilotFile->retVal == MGU_SUCCESS ) {
1440 jpilot_remove_empty( pilotFile );
1441 jpilot_mark_files( pilotFile );
1442 pilotFile->addressCache->modified = FALSE;
1443 pilotFile->addressCache->dataRead = TRUE;
1447 return pilotFile->retVal;
1451 * Return link list of persons.
1453 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1454 g_return_val_if_fail( pilotFile != NULL, NULL );
1455 return addrcache_get_list_person( pilotFile->addressCache );
1459 * Return link list of folders. This is always NULL since there are
1460 * no folders in GnomeCard.
1463 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1464 g_return_val_if_fail( pilotFile != NULL, NULL );
1465 return addrcache_get_list_folder( pilotFile->addressCache );
1469 * Return link list of all persons. Note that the list contains references
1470 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
1471 * this will destroy the addressbook data!
1472 * Return: List of items, or NULL if none.
1474 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1475 g_return_val_if_fail( pilotFile != NULL, NULL );
1476 return addrcache_get_all_persons( pilotFile->addressCache );
1480 * Check label list for specified label.
1482 gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1486 if( lblCheck == NULL ) return -1;
1487 if( strlen( lblCheck ) < 1 ) return -1;
1488 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1489 lblName = ai->labels[i];
1491 if( strlen( lblName ) ) {
1492 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1500 * Validate that all parameters specified.
1501 * Return: TRUE if data is good.
1503 gboolean jpilot_validate( const JPilotFile *pilotFile ) {
1506 g_return_val_if_fail( pilotFile != NULL, FALSE );
1509 if( pilotFile->path ) {
1510 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1515 if( pilotFile->name ) {
1516 if( strlen( pilotFile->name ) < 1 ) retVal = FALSE;
1524 #define WORK_BUFLEN 1024
1527 * Attempt to find a valid JPilot file.
1528 * Return: Filename, or home directory if not found, or empty string if
1529 * no home. Filename should be g_free() when done.
1531 gchar *jpilot_find_pilotdb( void ) {
1533 gchar str[ WORK_BUFLEN ];
1537 homedir = g_get_home_dir();
1538 if( ! homedir ) return g_strdup( "" );
1540 strcpy( str, homedir );
1541 len = strlen( str );
1543 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1544 str[ len ] = G_DIR_SEPARATOR;
1545 str[ ++len ] = '\0';
1548 strcat( str, JPILOT_DBHOME_DIR );
1549 strcat( str, G_DIR_SEPARATOR_S );
1550 strcat( str, JPILOT_DBHOME_FILE );
1552 /* Attempt to open */
1553 if( ( fp = fopen( str, "r" ) ) != NULL ) {
1557 /* Truncate filename */
1560 return g_strdup( str );
1564 * Attempt to read file, testing for valid JPilot format.
1565 * Return: TRUE if file appears to be valid format.
1567 gint jpilot_test_read_file( const gchar *fileSpec ) {
1568 JPilotFile *pilotFile;
1572 pilotFile = jpilot_create_path( fileSpec );
1573 retVal = jpilot_read_metadata( pilotFile );
1574 jpilot_free( pilotFile );
1578 retVal = MGU_NO_FILE;
1584 * Check whether label is in custom labels.
1585 * Return: TRUE if found.
1587 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1591 g_return_val_if_fail( pilotFile != NULL, FALSE );
1595 node = pilotFile->customLabels;
1597 if( g_strcasecmp( labelName, node->data ) == 0 ) {
1601 node = g_list_next( node );
1608 * Test whether pilot link library installed.
1609 * Return: TRUE if library available.
1612 gboolean jpilot_test_pilot_lib( void ) {
1615 handle = dlopen( PILOT_LINK_LIB_NAME, RTLD_LAZY );
1620 /* Test for symbols we need */
1621 fun = dlsym( handle, "unpack_Address" );
1627 fun = dlsym( handle, "unpack_AddressAppInfo" );
1637 #endif /* USE_JPILOT */