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->accessFlag = FALSE;
159 pilotFile->retVal = MGU_SUCCESS;
161 pilotFile->file = NULL;
162 pilotFile->path = NULL;
163 pilotFile->readMetadata = FALSE;
164 pilotFile->customLabels = NULL;
165 pilotFile->labelInd = NULL;
166 pilotFile->havePC3 = FALSE;
167 pilotFile->pc3ModifyTime = 0;
172 * Create new pilot file object for specified file.
174 JPilotFile *jpilot_create_path( const gchar *path ) {
175 JPilotFile *pilotFile;
176 pilotFile = jpilot_create();
177 jpilot_set_file( pilotFile, path );
184 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
185 g_return_if_fail( pilotFile != NULL );
186 addrcache_set_name( pilotFile->addressCache, value );
188 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
189 g_return_if_fail( pilotFile != NULL );
190 addrcache_refresh( pilotFile->addressCache );
191 pilotFile->readMetadata = FALSE;
192 pilotFile->path = mgu_replace_string( pilotFile->path, value );
194 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
195 g_return_if_fail( pilotFile != NULL );
196 pilotFile->accessFlag = value;
199 gint jpilot_get_status( JPilotFile *pilotFile ) {
200 g_return_val_if_fail( pilotFile != NULL, -1 );
201 return pilotFile->retVal;
203 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
204 g_return_val_if_fail( pilotFile != NULL, NULL );
205 return addrcache_get_root_folder( pilotFile->addressCache );
207 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
208 g_return_val_if_fail( pilotFile != NULL, NULL );
209 return addrcache_get_name( pilotFile->addressCache );
213 * Test whether file was read.
214 * Return: TRUE if file was read.
216 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
217 g_return_val_if_fail( pilotFile != NULL, FALSE );
218 return pilotFile->addressCache->dataRead;
222 * Free up custom label list.
224 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
227 g_return_if_fail( pilotFile != NULL );
229 /* Release custom labels */
230 mgu_free_dlist( pilotFile->customLabels );
231 pilotFile->customLabels = NULL;
233 /* Release indexes */
234 node = pilotFile->labelInd;
237 node = g_list_next( node );
239 g_list_free( pilotFile->labelInd );
240 pilotFile->labelInd = NULL;
242 /* Force a fresh read */
243 addrcache_refresh( pilotFile->addressCache );
247 * Append a custom label, representing an E-Mail address field to the
250 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
251 g_return_if_fail( pilotFile != NULL );
254 gchar *labelCopy = g_strdup( labelName );
255 g_strstrip( labelCopy );
256 if( *labelCopy == '\0' ) {
260 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
261 /* Force a fresh read */
262 addrcache_refresh( pilotFile->addressCache );
268 * Get list of custom labels.
269 * Return: List of labels. Must use g_free() when done.
271 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
272 GList *retVal = NULL;
275 g_return_val_if_fail( pilotFile != NULL, NULL );
277 node = pilotFile->customLabels;
279 retVal = g_list_append( retVal, g_strdup( node->data ) );
280 node = g_list_next( node );
286 * Return filespec of PC3 file corresponding to JPilot PDB file.
287 * Note: Filespec should be g_free() when done.
289 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
293 if( pilotFile == NULL ) return NULL;
294 if( pilotFile->path == NULL ) return NULL;
296 fileSpec = g_strdup( pilotFile->path );
297 len = strlen( fileSpec );
300 for( i = len; i > 0; i-- ) {
301 if( *(fileSpec + i) == '.' ) {
308 if( len - pos == 3 ) {
309 *r++ = 'p'; *r++ = 'c'; *r = '3';
318 * Save PC3 file time to cache.
319 * return: TRUE if time marked.
321 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
322 gboolean retVal = FALSE;
323 struct stat filestat;
326 /* Mark PDB file cache */
327 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
329 /* Now mark PC3 file */
330 pilotFile->havePC3 = FALSE;
331 pilotFile->pc3ModifyTime = 0;
332 pcFile = jpilot_get_pc3_file( pilotFile );
333 if( pcFile == NULL ) return retVal;
334 if( 0 == lstat( pcFile, &filestat ) ) {
335 pilotFile->havePC3 = TRUE;
336 pilotFile->pc3ModifyTime = filestat.st_mtime;
344 * Check whether JPilot PDB or PC3 file has changed by comparing
346 * return: TRUE if file has changed.
348 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
349 gboolean retVal = TRUE;
350 struct stat filestat;
353 /* Check main file */
354 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
358 if( ! pilotFile->havePC3 ) return FALSE;
359 pcFile = jpilot_get_pc3_file( pilotFile );
360 if( pcFile == NULL ) return FALSE;
362 if( 0 == lstat( pcFile, &filestat ) ) {
363 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
370 * Test whether file was modified since last access.
371 * Return: TRUE if file was modified.
373 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
374 g_return_val_if_fail( pilotFile != NULL, FALSE );
375 return jpilot_check_files( pilotFile );
377 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
378 g_return_val_if_fail( pilotFile != NULL, FALSE );
379 return pilotFile->accessFlag;
383 * Free up pilot file object by releasing internal memory.
385 void jpilot_free( JPilotFile *pilotFile ) {
386 g_return_if_fail( pilotFile != NULL );
388 /* Release custom labels */
389 jpilot_clear_custom_labels( pilotFile );
392 addrcache_clear( pilotFile->addressCache );
393 addrcache_free( pilotFile->addressCache );
395 /* Free internal stuff */
396 g_free( pilotFile->path );
398 pilotFile->file = NULL;
399 pilotFile->path = NULL;
400 pilotFile->readMetadata = FALSE;
401 pilotFile->havePC3 = FALSE;
402 pilotFile->pc3ModifyTime = 0;
404 pilotFile->type = ADBOOKTYPE_NONE;
405 pilotFile->addressCache = NULL;
406 pilotFile->accessFlag = FALSE;
407 pilotFile->retVal = MGU_SUCCESS;
409 /* Now release file object */
414 * Refresh internal variables to force a file read.
416 void jpilot_force_refresh( JPilotFile *pilotFile ) {
417 addrcache_refresh( pilotFile->addressCache );
421 * Print object to specified stream.
423 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
426 g_return_if_fail( pilotFile != NULL );
428 fprintf( stream, "JPilotFile:\n" );
429 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
430 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
431 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
433 node = pilotFile->customLabels;
435 fprintf( stream, " c label: %s\n", (gchar *)node->data );
436 node = g_list_next( node );
439 node = pilotFile->labelInd;
441 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
442 node = g_list_next( node );
445 addrcache_print( pilotFile->addressCache, stream );
446 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
447 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
448 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
449 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
453 * Print summary of object to specified stream.
455 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
457 g_return_if_fail( pilotFile != NULL );
458 fprintf( stream, "JPilotFile:\n" );
459 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
460 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
461 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
463 node = pilotFile->customLabels;
465 fprintf( stream, " c label: %s\n", (gchar *)node->data );
466 node = g_list_next( node );
469 node = pilotFile->labelInd;
471 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
472 node = g_list_next( node );
474 addrcache_print( pilotFile->addressCache, stream );
475 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
476 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
479 /* Shamelessly copied from JPilot (libplugin.c) */
480 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
483 for (i=0;i<num_bytes;i++) {
489 /* Shamelessly copied from JPilot (utils.c) */
490 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
491 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
492 #define PILOT_TIME_DELTA (unsigned)(2082844800)
494 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
495 return (time_t)(raw_time - PILOT_TIME_DELTA);
498 /* Shamelessly copied from JPilot (libplugin.c) */
499 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
502 strncpy(dbh->db_name, rdbh->db_name, 31);
503 dbh->db_name[31] = '\0';
504 dbh->flags = bytes_to_bin(rdbh->flags, 2);
505 dbh->version = bytes_to_bin(rdbh->version, 2);
506 temp = bytes_to_bin(rdbh->creation_time, 4);
507 dbh->creation_time = pilot_time_to_unix_time(temp);
508 temp = bytes_to_bin(rdbh->modification_time, 4);
509 dbh->modification_time = pilot_time_to_unix_time(temp);
510 temp = bytes_to_bin(rdbh->backup_time, 4);
511 dbh->backup_time = pilot_time_to_unix_time(temp);
512 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
513 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
514 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
515 strncpy(dbh->type, rdbh->type, 4);
517 strncpy(dbh->creator_id, rdbh->creator_id, 4);
518 dbh->creator_id[4] = '\0';
519 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
520 dbh->unique_id_seed[4] = '\0';
521 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
522 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
526 /* Shamelessly copied from JPilot (libplugin.c) */
527 /* returns 1 if found */
529 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
530 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
532 mem_rec_header *temp_mem_rh;
533 unsigned char found = 0;
534 unsigned long found_at;
537 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
538 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
539 found_at = temp_mem_rh->offset;
540 /* *attrib = temp_mem_rh->attrib; */
541 /* *unique_id = temp_mem_rh->unique_id; */
543 if ((temp_mem_rh->offset == fpos)) {
545 *attrib = temp_mem_rh->attrib;
546 *unique_id = temp_mem_rh->unique_id;
549 *next_offset = found_at;
553 /* Shamelessly copied from JPilot (libplugin.c) */
554 static void free_mem_rec_header(mem_rec_header **mem_rh) {
555 mem_rec_header *h, *next_h;
556 for (h=*mem_rh; h; h=next_h) {
563 /* Shamelessly copied from JPilot (libplugin.c) */
564 static int jpilot_free_db_list( GList **br_list ) {
565 GList *temp_list, *first;
568 /* Go to first entry in the list */
570 for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
573 for (temp_list = first; temp_list; temp_list = temp_list->next) {
574 if (temp_list->data) {
578 temp_list->data=NULL;
583 g_list_free(*br_list);
588 /* Shamelessly copied from JPilot (libplugin.c) */
590 static int jpilot_get_info_size( FILE *in, int *size ) {
596 fseek(in, 0, SEEK_SET);
597 fread(&rdbh, sizeof(RawDBHeader), 1, in);
602 raw_header_to_header(&rdbh, &dbh);
603 if (dbh.app_info_offset==0) {
607 if (dbh.sort_info_offset!=0) {
608 *size = dbh.sort_info_offset - dbh.app_info_offset;
611 if (dbh.number_of_records==0) {
612 fseek(in, 0, SEEK_END);
613 *size=ftell(in) - dbh.app_info_offset;
617 fread(&rh, sizeof(record_header), 1, in);
618 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
619 *size=offset - dbh.app_info_offset;
625 * Read address file into address list. Based on JPilot's
626 * libplugin.c (jp_get_app_info)
628 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
631 unsigned int rec_size;
635 if( ( !buf_size ) || ( ! buf ) ) {
642 if( pilotFile->path ) {
643 in = fopen( pilotFile->path, "r" );
645 return MGU_OPEN_FILE;
652 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
656 return MGU_ERROR_READ;
664 /* Convert header into something recognizable */
665 raw_header_to_header(&rdbh, &dbh);
667 num = jpilot_get_info_size(in, &rec_size);
670 return MGU_ERROR_READ;
673 fseek(in, dbh.app_info_offset, SEEK_SET);
674 *buf = ( char * ) malloc(rec_size);
677 return MGU_OO_MEMORY;
679 num = fread(*buf, rec_size, 1, in);
684 return MGU_ERROR_READ;
689 *buf_size = rec_size;
694 /* Shamelessly copied from JPilot (libplugin.c) */
695 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
701 memcpy(&l, p, sizeof(l));
702 header->header_len=ntohl(l);
705 memcpy(&l, p, sizeof(l));
706 header->header_version=ntohl(l);
709 memcpy(&l, p, sizeof(l));
710 header->rec_len=ntohl(l);
713 memcpy(&l, p, sizeof(l));
714 header->unique_id=ntohl(l);
717 memcpy(&l, p, sizeof(l));
721 memcpy(&(header->attrib), p, sizeof(unsigned char));
722 p+=sizeof(unsigned char);
727 /* Shamelessly copied from JPilot (libplugin.c) */
728 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
729 unsigned long l, len;
730 unsigned char packed_header[256];
733 num = fread(&l, sizeof(l), 1, pc_in);
740 memcpy(packed_header, &l, sizeof(l));
745 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
752 unpack_header(header, packed_header);
756 /* Read next record from PC3 file. Based on JPilot's
757 * pc_read_next_rec (libplugin.c) */
758 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
759 PC3RecordHeader header;
766 num = read_header(in, &header);
769 return MGU_ERROR_READ;
775 rec_len = header.rec_len;
776 record = malloc(rec_len);
778 return MGU_OO_MEMORY;
780 num = fread(record, rec_len, 1, in);
784 return MGU_ERROR_READ;
788 br->unique_id = header.unique_id;
789 br->attrib = header.attrib;
797 * Read address file into a linked list. Based on JPilot's
798 * jp_read_DB_files (from libplugin.c)
800 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
804 int num_records, recs_returned, i, num, r;
805 unsigned int offset, prev_offset, next_offset, rec_size;
807 long fpos; /*file position indicator */
808 unsigned char attrib;
809 unsigned int unique_id;
810 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
817 mem_rh = last_mem_rh = NULL;
821 if( pilotFile->path == NULL ) {
825 in = fopen( pilotFile->path, "r" );
827 return MGU_OPEN_FILE;
830 /* Read the database header */
831 num = fread(&rdbh, sizeof(RawDBHeader), 1, in);
835 return MGU_ERROR_READ;
842 raw_header_to_header(&rdbh, &dbh);
844 /* Read each record entry header */
845 num_records = dbh.number_of_records;
849 for (i = 1; i < num_records + 1; i++) {
850 num = fread(&rh, sizeof(record_header), 1, in);
861 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
862 if (offset < prev_offset) {
865 prev_offset = offset;
866 temp_mem_rh = (mem_rec_header *)malloc(sizeof(mem_rec_header));
870 temp_mem_rh->next = NULL;
871 temp_mem_rh->rec_num = i;
872 temp_mem_rh->offset = offset;
873 temp_mem_rh->attrib = rh.attrib;
874 temp_mem_rh->unique_id = (rh.unique_ID[0]*256+rh.unique_ID[1])*256+rh.unique_ID[2];
875 if (mem_rh == NULL) {
876 mem_rh = temp_mem_rh;
877 last_mem_rh = temp_mem_rh;
879 last_mem_rh->next = temp_mem_rh;
880 last_mem_rh = temp_mem_rh;
884 temp_mem_rh = mem_rh;
888 find_next_offset(mem_rh, 0, &next_offset, &attrib, &unique_id);
891 next_offset = mem_rh->offset;
892 attrib = mem_rh->attrib;
893 unique_id = mem_rh->unique_id;
896 fseek(in, next_offset, SEEK_SET);
900 find_next_offset(mem_rh, fpos, &next_offset, &attrib, &unique_id);
902 next_offset = 0xFFFFFF;
904 attrib = temp_mem_rh->attrib;
905 unique_id = temp_mem_rh->unique_id;
906 if (temp_mem_rh->next) {
907 temp_mem_rh = temp_mem_rh->next;
908 next_offset = temp_mem_rh->offset;
912 rec_size = next_offset - fpos;
913 buf = malloc(rec_size);
915 num = fread(buf, rec_size, 1, in);
923 temp_br = malloc(sizeof(buf_rec));
927 temp_br->rt = PALM_REC;
928 temp_br->unique_id = unique_id;
929 temp_br->attrib = attrib;
931 temp_br->size = rec_size;
933 *records = g_list_append(*records, temp_br);
939 free_mem_rec_header(&mem_rh);
941 /* Read the PC3 file, if present */
942 pcFile = jpilot_get_pc3_file( pilotFile );
943 if( pcFile == NULL ) return MGU_SUCCESS;
944 pc_in = fopen( pcFile, "r");
947 if( pc_in == NULL ) {
951 while( ! feof( pc_in ) ) {
952 temp_br = malloc(sizeof(buf_rec));
956 r = jpilot_read_next_pc( pc_in, temp_br );
957 if ( r != MGU_SUCCESS ) {
961 if ((temp_br->rt!=DELETED_PC_REC)
962 &&(temp_br->rt!=DELETED_PALM_REC)
963 &&(temp_br->rt!=MODIFIED_PALM_REC)
964 &&(temp_br->rt!=DELETED_DELETED_PALM_REC)) {
965 *records = g_list_append(*records, temp_br);
968 if ((temp_br->rt==DELETED_PALM_REC) || (temp_br->rt==MODIFIED_PALM_REC)) {
971 while(temp_list->next) {
972 temp_list=temp_list->next;
975 for (; temp_list; temp_list=temp_list->prev) {
976 if (((buf_rec *)temp_list->data)->unique_id == temp_br->unique_id) {
977 ((buf_rec *)temp_list->data)->rt = temp_br->rt;
987 #define FULLNAME_BUFSIZE 256
988 #define EMAIL_BUFSIZE 256
990 * Unpack address, building new data inside cache.
992 static void jpilot_load_address( JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] ) {
999 gchar fullName[ FULLNAME_BUFSIZE ];
1000 gchar bufEMail[ EMAIL_BUFSIZE ];
1007 struct AddressAppInfo *ai;
1009 /* Retrieve address */
1010 num = unpack_Address( & addr, buf->buf, buf->size );
1012 addrEnt = addr.entry;
1013 attrib = buf->attrib;
1014 unique_id = buf->unique_id;
1015 cat_id = attrib & 0x0F;
1017 *fullName = *bufEMail = '\0';
1018 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1019 strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
1022 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1023 strcat( fullName, " " );
1024 strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
1026 g_strchug( fullName );
1027 g_strchomp( fullName );
1029 person = addritem_create_item_person();
1030 addritem_person_set_common_name( person, fullName );
1031 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1032 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1033 addrcache_id_person( pilotFile->addressCache, person );
1035 extID = g_strdup_printf( "%d", unique_id );
1036 addritem_person_set_external_id( person, extID );
1040 /* Pointer to address metadata. */
1041 ai = & pilotFile->addrInfo;
1043 /* Add entry for each email address listed under phone labels. */
1044 indPhoneLbl = addr.phoneLabel;
1045 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1047 ind = indPhoneLbl[k];
1049 * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
1050 * ai->phoneLabels[ind], addrEnt[3+k] );
1052 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1053 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1055 strcpy( bufEMail, labelEntry );
1056 g_strchug( bufEMail );
1057 g_strchomp( bufEMail );
1059 email = addritem_create_item_email();
1060 addritem_email_set_address( email, bufEMail );
1061 addrcache_id_email( pilotFile->addressCache, email );
1062 addrcache_person_add_email
1063 ( pilotFile->addressCache, person, email );
1068 /* Add entry for each custom label */
1069 node = pilotFile->labelInd;
1072 ind = GPOINTER_TO_INT( node->data );
1075 * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
1078 labelEntry = addrEnt[ind];
1080 strcpy( bufEMail, labelEntry );
1081 g_strchug( bufEMail );
1082 g_strchomp( bufEMail );
1084 email = addritem_create_item_email();
1085 addritem_email_set_address( email, bufEMail );
1086 addritem_email_set_remarks( email, ai->labels[ind] );
1087 addrcache_id_email( pilotFile->addressCache, email );
1088 addrcache_person_add_email
1089 ( pilotFile->addressCache, person, email );
1093 node = g_list_next( node );
1096 if( person->listEMail ) {
1097 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1098 /* Add to specified category */
1099 addrcache_folder_add_person
1100 ( pilotFile->addressCache, folderInd[cat_id], person );
1102 /* Add to root folder */
1103 addrcache_add_person( pilotFile->addressCache, person );
1106 addritem_free_item_person( person );
1113 * Free up address list.
1115 static void jpilot_free_addrlist( GList *records ) {
1124 node = g_list_next( node );
1128 g_list_free( records );
1132 * Read address file into address cache.
1134 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1136 GList *records = NULL;
1139 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1141 retVal = jpilot_read_db_files( pilotFile, &records );
1142 if( retVal != MGU_SUCCESS ) {
1143 jpilot_free_addrlist( records );
1147 /* Build array of pointers to categories */
1149 node = addrcache_get_list_folder( pilotFile->addressCache );
1151 if( i < JPILOT_NUM_CATEG ) {
1152 folderInd[i] = node->data;
1154 node = g_list_next( node );
1158 /* Load all addresses, free up old stuff as we go */
1162 if( ( br->rt != DELETED_PC_REC ) &&
1163 ( br->rt != DELETED_PALM_REC ) &&
1164 ( br->rt != MODIFIED_PALM_REC ) &&
1165 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1166 jpilot_load_address( pilotFile, br, folderInd );
1170 node = g_list_next( node );
1174 g_list_free( records );
1181 * Read metadata from file.
1183 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1185 unsigned int rec_size;
1189 g_return_val_if_fail( pilotFile != NULL, -1 );
1191 pilotFile->readMetadata = FALSE;
1192 addrcache_clear( pilotFile->addressCache );
1194 /* Read file info */
1195 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1196 if( retVal != MGU_SUCCESS ) {
1197 pilotFile->retVal = retVal;
1198 return pilotFile->retVal;
1201 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1206 pilotFile->retVal = MGU_ERROR_READ;
1207 return pilotFile->retVal;
1210 pilotFile->readMetadata = TRUE;
1211 pilotFile->retVal = MGU_SUCCESS;
1212 return pilotFile->retVal;
1216 * Setup labels and indexes from metadata.
1217 * Return: TRUE is setup successfully.
1219 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1220 gboolean retVal = FALSE;
1221 struct AddressAppInfo *ai;
1224 g_return_val_if_fail( pilotFile != NULL, -1 );
1226 /* Release indexes */
1227 node = pilotFile->labelInd;
1230 node = g_list_next( node );
1232 pilotFile->labelInd = NULL;
1234 if( pilotFile->readMetadata ) {
1235 ai = & pilotFile->addrInfo;
1236 node = pilotFile->customLabels;
1238 gchar *lbl = node->data;
1241 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1242 gchar *labelName = ai->labels[i];
1243 if( g_strcasecmp( labelName, lbl ) == 0 ) {
1248 pilotFile->labelInd = g_list_append( pilotFile->labelInd, GINT_TO_POINTER(ind) );
1249 node = g_list_next( node );
1257 * Load list with character strings of label names.
1259 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
1262 g_return_val_if_fail( pilotFile != NULL, NULL );
1264 if( pilotFile->readMetadata ) {
1265 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1266 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1267 gchar *labelName = ai->labels[i];
1269 labelList = g_list_append( labelList, g_strdup( labelName ) );
1272 labelList = g_list_append( labelList, g_strdup( "" ) );
1280 * Return category name for specified category ID.
1281 * Enter: Category ID.
1282 * Return: Name, or empty string if not invalid ID. Name should be g_free() when done.
1284 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
1285 gchar *catName = NULL;
1287 g_return_val_if_fail( pilotFile != NULL, NULL );
1289 if( pilotFile->readMetadata ) {
1290 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1291 struct CategoryAppInfo *cat = & ai->category;
1292 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
1295 catName = g_strdup( cat->name[catID] );
1298 if( ! catName ) catName = g_strdup( "" );
1303 * Load list with character strings of phone label names.
1305 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
1308 g_return_val_if_fail( pilotFile != NULL, NULL );
1310 if( pilotFile->readMetadata ) {
1311 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1312 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1313 gchar *labelName = ai->phoneLabels[i];
1315 labelList = g_list_append( labelList, g_strdup( labelName ) );
1318 labelList = g_list_append( labelList, g_strdup( "" ) );
1326 * Load list with character strings of label names. Only none blank names
1328 * Return: list of labels. Should by g_free()'d when done.
1330 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1333 g_return_val_if_fail( pilotFile != NULL, NULL );
1335 if( pilotFile->readMetadata ) {
1336 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1337 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1338 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1340 g_strchomp( labelName );
1341 g_strchug( labelName );
1342 if( *labelName != '\0' ) {
1343 labelList = g_list_append( labelList, g_strdup( labelName ) );
1352 * Load list with character strings of category names.
1354 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1355 GList *catList = NULL;
1358 g_return_val_if_fail( pilotFile != NULL, NULL );
1360 if( pilotFile->readMetadata ) {
1361 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1362 struct CategoryAppInfo *cat = & ai->category;
1363 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1364 gchar *catName = cat->name[i];
1366 catList = g_list_append( catList, g_strdup( catName ) );
1369 catList = g_list_append( catList, g_strdup( "" ) );
1377 * Build folder for each category.
1379 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1380 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1381 struct CategoryAppInfo *cat = & ai->category;
1384 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1385 ItemFolder *folder = addritem_create_item_folder();
1386 addritem_folder_set_name( folder, cat->name[i] );
1387 addrcache_id_folder( pilotFile->addressCache, folder );
1388 addrcache_add_folder( pilotFile->addressCache, folder );
1393 * Remove empty folders (categories).
1395 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1401 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1405 ItemFolder *folder = node->data;
1406 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1407 if( folder->listPerson ) {
1408 /* Give name to folder */
1410 sprintf( name, "? %d", i );
1411 addritem_folder_set_name( folder, name );
1414 /* Mark for removal */
1415 remList = g_list_append( remList, folder );
1418 node = g_list_next( node );
1423 ItemFolder *folder = node->data;
1424 addrcache_remove_folder( pilotFile->addressCache, folder );
1425 node = g_list_next( node );
1427 g_list_free( remList );
1431 * ============================================================================================
1432 * Read file into list. Main entry point
1433 * Return: TRUE if file read successfully.
1434 * ============================================================================================
1436 gint jpilot_read_data( JPilotFile *pilotFile ) {
1437 g_return_val_if_fail( pilotFile != NULL, -1 );
1439 pilotFile->retVal = MGU_SUCCESS;
1440 pilotFile->accessFlag = FALSE;
1442 if( jpilot_check_files( pilotFile ) ) {
1443 addrcache_clear( pilotFile->addressCache );
1444 jpilot_read_metadata( pilotFile );
1445 if( pilotFile->retVal == MGU_SUCCESS ) {
1446 jpilot_setup_labels( pilotFile );
1447 jpilot_build_category_list( pilotFile );
1448 pilotFile->retVal = jpilot_read_file( pilotFile );
1449 if( pilotFile->retVal == MGU_SUCCESS ) {
1450 jpilot_remove_empty( pilotFile );
1451 jpilot_mark_files( pilotFile );
1452 pilotFile->addressCache->modified = FALSE;
1453 pilotFile->addressCache->dataRead = TRUE;
1457 return pilotFile->retVal;
1461 * Return link list of persons.
1463 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1464 g_return_val_if_fail( pilotFile != NULL, NULL );
1465 return addrcache_get_list_person( pilotFile->addressCache );
1469 * Return link list of folders. This is always NULL since there are
1470 * no folders in GnomeCard.
1473 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1474 g_return_val_if_fail( pilotFile != NULL, NULL );
1475 return addrcache_get_list_folder( pilotFile->addressCache );
1479 * Return link list of all persons. Note that the list contains references
1480 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
1481 * this will destroy the addressbook data!
1482 * Return: List of items, or NULL if none.
1484 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1485 g_return_val_if_fail( pilotFile != NULL, NULL );
1486 return addrcache_get_all_persons( pilotFile->addressCache );
1490 * Check label list for specified label.
1492 gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1496 if( lblCheck == NULL ) return -1;
1497 if( strlen( lblCheck ) < 1 ) return -1;
1498 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1499 lblName = ai->labels[i];
1501 if( strlen( lblName ) ) {
1502 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1510 * Validate that all parameters specified.
1511 * Return: TRUE if data is good.
1513 gboolean jpilot_validate( JPilotFile *pilotFile ) {
1517 g_return_val_if_fail( pilotFile != NULL, FALSE );
1520 if( pilotFile->path ) {
1521 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1526 name = jpilot_get_name( pilotFile );
1528 if( strlen( name ) < 1 ) retVal = FALSE;
1536 #define WORK_BUFLEN 1024
1539 * Attempt to find a valid JPilot file.
1540 * Return: Filename, or home directory if not found, or empty string if
1541 * no home. Filename should be g_free() when done.
1543 gchar *jpilot_find_pilotdb( void ) {
1545 gchar str[ WORK_BUFLEN ];
1549 homedir = g_get_home_dir();
1550 if( ! homedir ) return g_strdup( "" );
1552 strcpy( str, homedir );
1553 len = strlen( str );
1555 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1556 str[ len ] = G_DIR_SEPARATOR;
1557 str[ ++len ] = '\0';
1560 strcat( str, JPILOT_DBHOME_DIR );
1561 strcat( str, G_DIR_SEPARATOR_S );
1562 strcat( str, JPILOT_DBHOME_FILE );
1564 /* Attempt to open */
1565 if( ( fp = fopen( str, "r" ) ) != NULL ) {
1569 /* Truncate filename */
1572 return g_strdup( str );
1576 * Attempt to read file, testing for valid JPilot format.
1577 * Return: TRUE if file appears to be valid format.
1579 gint jpilot_test_read_file( const gchar *fileSpec ) {
1580 JPilotFile *pilotFile;
1584 pilotFile = jpilot_create_path( fileSpec );
1585 retVal = jpilot_read_metadata( pilotFile );
1586 jpilot_free( pilotFile );
1590 retVal = MGU_NO_FILE;
1596 * Check whether label is in custom labels.
1597 * Return: TRUE if found.
1599 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1603 g_return_val_if_fail( pilotFile != NULL, FALSE );
1607 node = pilotFile->customLabels;
1609 if( g_strcasecmp( labelName, ( gchar * ) node->data ) == 0 ) {
1613 node = g_list_next( node );
1620 * Test whether pilot link library installed.
1621 * Return: TRUE if library available.
1623 gboolean jpilot_test_pilot_lib( void ) {
1627 #endif /* USE_JPILOT */