2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2003 Match Grun
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Functions necessary to access JPilot database files.
22 * JPilot is Copyright(c) by Judd Montgomery.
23 * Visit http://www.jpilot.org for more details.
38 #include <netinet/in.h>
40 #ifdef HAVE_LIBPISOCK_PI_ARGS_H
41 # include <libpisock/pi-args.h>
42 # include <libpisock/pi-appinfo.h>
43 # include <libpisock/pi-address.h>
46 # include <pi-appinfo.h>
47 # include <pi-address.h>
52 #include "addrcache.h"
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.
152 * \return Initialized JPilot 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->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.
173 * \param path Path to JPilot address book.
174 * \return Initialized JPilot file object.
176 JPilotFile *jpilot_create_path( const gchar *path ) {
177 JPilotFile *pilotFile;
178 pilotFile = jpilot_create();
179 jpilot_set_file( pilotFile, path );
186 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
187 g_return_if_fail( pilotFile != NULL );
188 addrcache_set_name( pilotFile->addressCache, value );
190 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
191 g_return_if_fail( pilotFile != NULL );
192 addrcache_refresh( pilotFile->addressCache );
193 pilotFile->readMetadata = FALSE;
194 pilotFile->path = mgu_replace_string( pilotFile->path, value );
196 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
197 g_return_if_fail( pilotFile != NULL );
198 pilotFile->addressCache->accessFlag = value;
201 gint jpilot_get_status( JPilotFile *pilotFile ) {
202 g_return_val_if_fail( pilotFile != NULL, -1 );
203 return pilotFile->retVal;
205 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
206 g_return_val_if_fail( pilotFile != NULL, NULL );
207 return addrcache_get_root_folder( pilotFile->addressCache );
209 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
210 g_return_val_if_fail( pilotFile != NULL, NULL );
211 return addrcache_get_name( pilotFile->addressCache );
215 * Test whether file was read.
216 * \param pilotFile JPilot control data.
217 * \return <i>TRUE</i> if file was read.
219 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
220 g_return_val_if_fail( pilotFile != NULL, FALSE );
221 return pilotFile->addressCache->dataRead;
225 * Free up custom label list.
226 * \param pilotFile JPilot control data.
228 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
231 g_return_if_fail( pilotFile != NULL );
233 /* Release custom labels */
234 mgu_free_dlist( pilotFile->customLabels );
235 pilotFile->customLabels = NULL;
237 /* Release indexes */
238 node = pilotFile->labelInd;
241 node = g_list_next( node );
243 g_list_free( pilotFile->labelInd );
244 pilotFile->labelInd = NULL;
246 /* Force a fresh read */
247 addrcache_refresh( pilotFile->addressCache );
251 * Append a custom label, representing an E-Mail address field to the
253 * \param pilotFile JPilot control data.
255 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
256 g_return_if_fail( pilotFile != NULL );
259 gchar *labelCopy = g_strdup( labelName );
260 g_strstrip( labelCopy );
261 if( *labelCopy == '\0' ) {
265 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
266 /* Force a fresh read */
267 addrcache_refresh( pilotFile->addressCache );
273 * Get list of custom labels.
274 * \param pilotFile JPilot control data.
275 * \return List of labels. Must use g_free() when done.
277 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
278 GList *retVal = NULL;
281 g_return_val_if_fail( pilotFile != NULL, NULL );
283 node = pilotFile->customLabels;
285 retVal = g_list_append( retVal, g_strdup( node->data ) );
286 node = g_list_next( node );
292 * Return filespec of PC3 file corresponding to JPilot PDB file.
293 * \param pilotFile JPilot control data.
294 * \return File specification; should be g_free() when done.
296 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
300 if( pilotFile == NULL ) return NULL;
301 if( pilotFile->path == NULL ) return NULL;
303 fileSpec = g_strdup( pilotFile->path );
304 len = strlen( fileSpec );
307 for( i = len; i > 0; i-- ) {
308 if( *(fileSpec + i) == '.' ) {
315 if( len - pos == 3 ) {
316 *r++ = 'p'; *r++ = 'c'; *r = '3';
325 * Save PC3 file time to cache.
326 * \param pilotFile JPilot control data.
327 * \return <i>TRUE</i> if time marked.
329 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
330 gboolean retVal = FALSE;
331 struct stat filestat;
334 /* Mark PDB file cache */
335 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
337 /* Now mark PC3 file */
338 pilotFile->havePC3 = FALSE;
339 pilotFile->pc3ModifyTime = 0;
340 pcFile = jpilot_get_pc3_file( pilotFile );
341 if( pcFile == NULL ) return retVal;
342 if( 0 == lstat( pcFile, &filestat ) ) {
343 pilotFile->havePC3 = TRUE;
344 pilotFile->pc3ModifyTime = filestat.st_mtime;
352 * Check whether JPilot PDB or PC3 file has changed by comparing
354 * \param pilotFile JPilot control data.
355 * \return <i>TRUE</i> if file has changed.
357 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
358 gboolean retVal = TRUE;
359 struct stat filestat;
362 /* Check main file */
363 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
367 if( ! pilotFile->havePC3 ) return FALSE;
368 pcFile = jpilot_get_pc3_file( pilotFile );
369 if( pcFile == NULL ) return FALSE;
371 if( 0 == lstat( pcFile, &filestat ) ) {
372 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
379 * Test whether file was modified since last access.
380 * Return: TRUE if file was modified.
382 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
383 g_return_val_if_fail( pilotFile != NULL, FALSE );
384 pilotFile->addressCache->modified = jpilot_check_files( pilotFile );
385 return pilotFile->addressCache->modified;
387 void jpilot_set_modified( JPilotFile *pilotFile, const gboolean value ) {
388 g_return_if_fail( pilotFile != NULL );
389 pilotFile->addressCache->modified = value;
391 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
392 g_return_val_if_fail( pilotFile != NULL, FALSE );
393 return pilotFile->addressCache->accessFlag;
397 * Free up pilot file object by releasing internal memory.
398 * \param pilotFile JPilot control data.
400 void jpilot_free( JPilotFile *pilotFile ) {
401 g_return_if_fail( pilotFile != NULL );
403 /* Release custom labels */
404 jpilot_clear_custom_labels( pilotFile );
407 addrcache_clear( pilotFile->addressCache );
408 addrcache_free( pilotFile->addressCache );
410 /* Free internal stuff */
411 g_free( pilotFile->path );
413 pilotFile->file = NULL;
414 pilotFile->path = NULL;
415 pilotFile->readMetadata = FALSE;
416 pilotFile->havePC3 = FALSE;
417 pilotFile->pc3ModifyTime = 0;
419 pilotFile->type = ADBOOKTYPE_NONE;
420 pilotFile->addressCache = NULL;
421 pilotFile->retVal = MGU_SUCCESS;
423 /* Now release file object */
428 * Refresh internal variables to force a file read.
430 void jpilot_force_refresh( JPilotFile *pilotFile ) {
431 addrcache_refresh( pilotFile->addressCache );
435 * Print object to specified stream.
437 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
440 g_return_if_fail( pilotFile != NULL );
442 fprintf( stream, "JPilotFile:\n" );
443 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
444 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
445 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
447 node = pilotFile->customLabels;
449 fprintf( stream, " c label: %s\n", (gchar *)node->data );
450 node = g_list_next( node );
453 node = pilotFile->labelInd;
455 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
456 node = g_list_next( node );
459 addrcache_print( pilotFile->addressCache, stream );
460 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
461 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
462 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
463 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
467 * Print summary of object to specified stream.
469 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
471 g_return_if_fail( pilotFile != NULL );
472 fprintf( stream, "JPilotFile:\n" );
473 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
474 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
475 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
477 node = pilotFile->customLabels;
479 fprintf( stream, " c label: %s\n", (gchar *)node->data );
480 node = g_list_next( node );
483 node = pilotFile->labelInd;
485 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
486 node = g_list_next( node );
488 addrcache_print( pilotFile->addressCache, stream );
489 fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
490 fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
493 /* Shamelessly copied from JPilot (libplugin.c) */
494 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
497 for (i=0;i<num_bytes;i++) {
503 /* Shamelessly copied from JPilot (utils.c) */
504 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
505 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
506 #define PILOT_TIME_DELTA (unsigned)(2082844800)
508 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
509 return (time_t)(raw_time - PILOT_TIME_DELTA);
512 /* Shamelessly copied from JPilot (libplugin.c) */
513 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
516 strncpy(dbh->db_name, rdbh->db_name, 31);
517 dbh->db_name[31] = '\0';
518 dbh->flags = bytes_to_bin(rdbh->flags, 2);
519 dbh->version = bytes_to_bin(rdbh->version, 2);
520 temp = bytes_to_bin(rdbh->creation_time, 4);
521 dbh->creation_time = pilot_time_to_unix_time(temp);
522 temp = bytes_to_bin(rdbh->modification_time, 4);
523 dbh->modification_time = pilot_time_to_unix_time(temp);
524 temp = bytes_to_bin(rdbh->backup_time, 4);
525 dbh->backup_time = pilot_time_to_unix_time(temp);
526 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
527 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
528 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
529 strncpy(dbh->type, rdbh->type, 4);
531 strncpy(dbh->creator_id, rdbh->creator_id, 4);
532 dbh->creator_id[4] = '\0';
533 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
534 dbh->unique_id_seed[4] = '\0';
535 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
536 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
540 /* Shamelessly copied from JPilot (libplugin.c) */
541 /* returns 1 if found */
543 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
544 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
546 mem_rec_header *temp_mem_rh;
547 unsigned char found = 0;
548 unsigned long found_at;
551 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
552 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
553 found_at = temp_mem_rh->offset;
554 /* *attrib = temp_mem_rh->attrib; */
555 /* *unique_id = temp_mem_rh->unique_id; */
557 if ((temp_mem_rh->offset == fpos)) {
559 *attrib = temp_mem_rh->attrib;
560 *unique_id = temp_mem_rh->unique_id;
563 *next_offset = found_at;
567 /* Shamelessly copied from JPilot (libplugin.c) */
568 static void free_mem_rec_header(mem_rec_header **mem_rh) {
569 mem_rec_header *h, *next_h;
570 for (h=*mem_rh; h; h=next_h) {
577 /* Shamelessly copied from JPilot (libplugin.c) */
578 static int jpilot_free_db_list( GList **br_list ) {
579 GList *temp_list, *first;
582 /* Go to first entry in the list */
584 for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
587 for (temp_list = first; temp_list; temp_list = temp_list->next) {
588 if (temp_list->data) {
592 temp_list->data=NULL;
597 g_list_free(*br_list);
602 /* Shamelessly copied from JPilot (libplugin.c) */
604 static int jpilot_get_info_size( FILE *in, int *size ) {
610 fseek(in, 0, SEEK_SET);
611 fread(&rdbh, sizeof(RawDBHeader), 1, in);
616 raw_header_to_header(&rdbh, &dbh);
617 if (dbh.app_info_offset==0) {
621 if (dbh.sort_info_offset!=0) {
622 *size = dbh.sort_info_offset - dbh.app_info_offset;
625 if (dbh.number_of_records==0) {
626 fseek(in, 0, SEEK_END);
627 *size=ftell(in) - dbh.app_info_offset;
631 fread(&rh, sizeof(record_header), 1, in);
632 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
633 *size=offset - dbh.app_info_offset;
639 * Read address file into address list. Based on JPilot's
640 * libplugin.c (jp_get_app_info)
642 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
645 unsigned int rec_size;
649 if( ( !buf_size ) || ( ! buf ) ) {
656 if( pilotFile->path ) {
657 in = fopen( pilotFile->path, "rb" );
659 return MGU_OPEN_FILE;
666 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
670 return MGU_ERROR_READ;
678 /* Convert header into something recognizable */
679 raw_header_to_header(&rdbh, &dbh);
681 num = jpilot_get_info_size(in, &rec_size);
684 return MGU_ERROR_READ;
687 fseek(in, dbh.app_info_offset, SEEK_SET);
688 *buf = ( char * ) malloc(rec_size);
691 return MGU_OO_MEMORY;
693 num = fread(*buf, rec_size, 1, in);
698 return MGU_ERROR_READ;
703 *buf_size = rec_size;
708 /* Shamelessly copied from JPilot (libplugin.c) */
709 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
715 memcpy(&l, p, sizeof(l));
716 header->header_len=ntohl(l);
719 memcpy(&l, p, sizeof(l));
720 header->header_version=ntohl(l);
723 memcpy(&l, p, sizeof(l));
724 header->rec_len=ntohl(l);
727 memcpy(&l, p, sizeof(l));
728 header->unique_id=ntohl(l);
731 memcpy(&l, p, sizeof(l));
735 memcpy(&(header->attrib), p, sizeof(unsigned char));
736 p+=sizeof(unsigned char);
741 /* Shamelessly copied from JPilot (libplugin.c) */
742 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
743 unsigned long l, len;
744 unsigned char packed_header[256];
747 num = fread(&l, sizeof(l), 1, pc_in);
754 memcpy(packed_header, &l, sizeof(l));
759 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
766 unpack_header(header, packed_header);
771 * Read next record from PC3 file. Based on JPilot function
772 * <code>pc_read_next_rec()</code> (libplugin.c)
774 * \param in File handle.
775 * \param br Record buffer.
776 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
779 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
780 PC3RecordHeader header;
787 num = read_header( in, &header );
790 return MGU_ERROR_READ;
796 rec_len = header.rec_len;
797 record = malloc( rec_len );
799 return MGU_OO_MEMORY;
801 num = fread( record, rec_len, 1, in );
805 return MGU_ERROR_READ;
809 br->unique_id = header.unique_id;
810 br->attrib = header.attrib;
818 * Read address file into a linked list. Based on JPilot function
819 * <code>jp_read_DB_files()</code> (from libplugin.c)
821 * \param pilotFile JPilot control data.
822 * \param records Pointer to linked list of records read.
823 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
826 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
830 int num_records, recs_returned, i, num, r;
831 unsigned int offset, prev_offset, next_offset, rec_size;
833 long fpos; /*file position indicator */
834 unsigned char attrib;
835 unsigned int unique_id;
836 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
843 mem_rh = last_mem_rh = NULL;
847 if( pilotFile->path == NULL ) {
851 in = fopen( pilotFile->path, "rb" );
853 return MGU_OPEN_FILE;
856 /* Read the database header */
857 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
861 return MGU_ERROR_READ;
868 raw_header_to_header( &rdbh, &dbh );
870 /* Read each record entry header */
871 num_records = dbh.number_of_records;
875 for( i = 1; i < num_records + 1; i++ ) {
876 num = fread( &rh, sizeof( record_header ), 1, in );
888 ( ( rh.Offset[0] * 256 + rh.Offset[1] ) * 256
889 + rh.Offset[2] ) * 256
891 if( offset < prev_offset ) {
894 prev_offset = offset;
895 temp_mem_rh = ( mem_rec_header * ) malloc( sizeof( mem_rec_header ) );
896 if( ! temp_mem_rh ) {
899 temp_mem_rh->next = NULL;
900 temp_mem_rh->rec_num = i;
901 temp_mem_rh->offset = offset;
902 temp_mem_rh->attrib = rh.attrib;
903 temp_mem_rh->unique_id =
904 ( rh.unique_ID[0] * 256 + rh.unique_ID[1] ) * 256
906 if( mem_rh == NULL ) {
907 mem_rh = temp_mem_rh;
908 last_mem_rh = temp_mem_rh;
911 last_mem_rh->next = temp_mem_rh;
912 last_mem_rh = temp_mem_rh;
916 temp_mem_rh = mem_rh;
921 mem_rh, 0, &next_offset, &attrib, &unique_id );
925 next_offset = mem_rh->offset;
926 attrib = mem_rh->attrib;
927 unique_id = mem_rh->unique_id;
930 fseek( in, next_offset, SEEK_SET );
931 while( ! feof( in ) ) {
935 mem_rh, fpos, &next_offset, &attrib,
938 next_offset = 0xFFFFFF;
940 attrib = temp_mem_rh->attrib;
941 unique_id = temp_mem_rh->unique_id;
942 if ( temp_mem_rh->next ) {
943 temp_mem_rh = temp_mem_rh->next;
944 next_offset = temp_mem_rh->offset;
948 rec_size = next_offset - fpos;
949 buf = malloc( rec_size );
951 num = fread( buf, rec_size, 1, in );
959 temp_br = malloc( sizeof( buf_rec ) );
964 temp_br->rt = PALM_REC;
965 temp_br->unique_id = unique_id;
966 temp_br->attrib = attrib;
968 temp_br->size = rec_size;
970 *records = g_list_append( *records, temp_br );
976 free_mem_rec_header( &mem_rh );
978 /* Read the PC3 file, if present */
979 pcFile = jpilot_get_pc3_file( pilotFile );
980 if( pcFile == NULL ) return MGU_SUCCESS;
981 pc_in = fopen( pcFile, "rb" );
984 if( pc_in == NULL ) {
988 while( ! feof( pc_in ) ) {
991 temp_br = malloc( sizeof( buf_rec ) );
995 r = jpilot_read_next_pc( pc_in, temp_br );
996 if( r != MGU_SUCCESS ) {
997 free( temp_br->buf );
1003 if( ( temp_br->rt != DELETED_PC_REC )
1004 && ( temp_br->rt != DELETED_PALM_REC )
1005 && ( temp_br->rt != MODIFIED_PALM_REC )
1006 && ( temp_br->rt != DELETED_DELETED_PALM_REC ) )
1008 *records = g_list_append( *records, temp_br );
1013 if( ( temp_br->rt == DELETED_PALM_REC )
1014 || ( temp_br->rt == MODIFIED_PALM_REC ) )
1016 temp_list = *records;
1018 while( temp_list->next ) {
1019 temp_list=temp_list->next;
1022 for( ; temp_list; temp_list=temp_list->prev ) {
1023 if( ( ( buf_rec * )temp_list->data )->unique_id ==
1024 temp_br->unique_id ) {
1025 ( ( buf_rec * )temp_list->data )->rt =
1032 free( temp_br->buf );
1042 * Parse buffer containing multiple e-mail addresses into a linked list of
1043 * addresses. Separator characters are " ,;|" and control characters. Address
1044 * is only extracted if it contains an "at" (@) character.
1046 * \param buf Buffer to process.
1047 * \return List of strings.
1049 static GList *jpilot_parse_email( gchar *buf ) {
1054 gboolean valid, done;
1056 valid = done = FALSE;
1061 if( *p == ' ' || *p == ',' || *p == ';' || *p == '|' || *p < 32 ) {
1076 em = g_strndup( st, len );
1077 list = g_list_append( list, em );
1085 if( *p == '@' ) valid = TRUE;
1093 #define FULLNAME_BUFSIZE 256
1094 #define EMAIL_BUFSIZE 256
1097 * Process a single label entry field, parsing multiple e-mail address entries.
1099 * \param pilotFile JPilot control data.
1100 * \param labelEntry Label entry data.
1101 * \param person Person.
1103 static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPerson *person ) {
1104 gchar buffer[ EMAIL_BUFSIZE ];
1110 strcpy( buffer, labelEntry );
1111 node = list = jpilot_parse_email( buffer );
1113 email = addritem_create_item_email();
1114 addritem_email_set_address( email, node->data );
1115 addrcache_id_email( pilotFile->addressCache, email );
1116 addrcache_person_add_email( pilotFile->addressCache, person, email );
1117 node = g_list_next( node );
1119 mgu_free_dlist( list );
1125 * Unpack address, building new data inside cache.
1126 * \param pilotFile JPilot control data.
1127 * \param buf Record buffer.
1128 * \param folderInd Array of (category) folders to load.
1130 static void jpilot_load_address(
1131 JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] )
1133 struct Address addr;
1139 gchar fullName[ FULLNAME_BUFSIZE ];
1145 struct AddressAppInfo *ai;
1147 /* Retrieve address */
1148 num = unpack_Address( & addr, buf->buf, buf->size );
1150 addrEnt = addr.entry;
1151 attrib = buf->attrib;
1152 unique_id = buf->unique_id;
1153 cat_id = attrib & 0x0F;
1156 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1157 strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
1160 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1161 strcat( fullName, " " );
1162 strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
1164 g_strchug( fullName );
1165 g_strchomp( fullName );
1167 person = addritem_create_item_person();
1168 addritem_person_set_common_name( person, fullName );
1169 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1170 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1171 addrcache_id_person( pilotFile->addressCache, person );
1173 extID = g_strdup_printf( "%d", unique_id );
1174 addritem_person_set_external_id( person, extID );
1178 /* Pointer to address metadata. */
1179 ai = & pilotFile->addrInfo;
1181 /* Add entry for each email address listed under phone labels. */
1182 indPhoneLbl = addr.phoneLabel;
1183 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1185 ind = indPhoneLbl[k];
1187 * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
1188 * ai->phoneLabels[ind], addrEnt[3+k] );
1190 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1191 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1192 jpilot_parse_label( pilotFile, labelEntry, person );
1196 /* Add entry for each custom label */
1197 node = pilotFile->labelInd;
1200 ind = GPOINTER_TO_INT( node->data );
1203 * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
1206 labelEntry = addrEnt[ind];
1207 jpilot_parse_label( pilotFile, labelEntry, person );
1210 node = g_list_next( node );
1213 if( person->listEMail ) {
1214 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1215 /* Add to specified category */
1216 addrcache_folder_add_person(
1217 pilotFile->addressCache,
1218 folderInd[cat_id], person );
1221 /* Add to root folder */
1222 addrcache_add_person(
1223 pilotFile->addressCache, person );
1227 addritem_free_item_person( person );
1232 /* Free up pointer allocated inside address */
1233 free_Address( & addr );
1237 * Free up address list.
1238 * \param records List of records to free.
1240 static void jpilot_free_addrlist( GList *records ) {
1250 node = g_list_next( node );
1254 g_list_free( records );
1258 * Read metadata from file.
1259 * \param pilotFile JPilot control data.
1260 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
1263 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1265 unsigned int rec_size;
1269 g_return_val_if_fail( pilotFile != NULL, -1 );
1271 pilotFile->readMetadata = FALSE;
1272 addrcache_clear( pilotFile->addressCache );
1274 /* Read file info */
1275 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1276 if( retVal != MGU_SUCCESS ) {
1277 pilotFile->retVal = retVal;
1278 return pilotFile->retVal;
1281 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1286 pilotFile->retVal = MGU_ERROR_READ;
1287 return pilotFile->retVal;
1290 pilotFile->readMetadata = TRUE;
1291 pilotFile->retVal = MGU_SUCCESS;
1292 return pilotFile->retVal;
1296 * Setup labels and indexes from metadata.
1297 * \param pilotFile JPilot control data.
1298 * \return <i>TRUE</i> is setup successfully.
1300 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1301 gboolean retVal = FALSE;
1302 struct AddressAppInfo *ai;
1305 g_return_val_if_fail( pilotFile != NULL, -1 );
1307 /* Release indexes */
1308 node = pilotFile->labelInd;
1311 node = g_list_next( node );
1313 pilotFile->labelInd = NULL;
1315 if( pilotFile->readMetadata ) {
1316 ai = & pilotFile->addrInfo;
1317 node = pilotFile->customLabels;
1319 gchar *lbl = node->data;
1322 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1323 gchar *labelName = ai->labels[i];
1324 if( g_strcasecmp( labelName, lbl ) == 0 ) {
1329 pilotFile->labelInd = g_list_append(
1330 pilotFile->labelInd, GINT_TO_POINTER(ind) );
1331 node = g_list_next( node );
1339 * Load list with character strings of label names.
1340 * \param pilotFile JPilot control data.
1341 * \param labelList List of label names to load.
1342 * \return List of label names loaded.
1344 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
1347 g_return_val_if_fail( pilotFile != NULL, NULL );
1349 if( pilotFile->readMetadata ) {
1350 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1351 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1352 gchar *labelName = ai->labels[i];
1354 labelList = g_list_append(
1355 labelList, g_strdup( labelName ) );
1358 labelList = g_list_append(
1359 labelList, g_strdup( "" ) );
1367 * Return category name for specified category ID.
1368 * \param pilotFile JPilot control data.
1369 * \param catID Category ID.
1370 * \return Category name, or empty string if not invalid ID. Name should be
1371 * <code>g_free()</code> when done.
1373 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
1374 gchar *catName = NULL;
1376 g_return_val_if_fail( pilotFile != NULL, NULL );
1378 if( pilotFile->readMetadata ) {
1379 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1380 struct CategoryAppInfo *cat = & ai->category;
1381 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
1384 catName = g_strdup( cat->name[catID] );
1387 if( ! catName ) catName = g_strdup( "" );
1392 * Load list with character strings of phone label names.
1393 * \param pilotFile JPilot control data.
1394 * \param labelList List of label names to load.
1395 * \return List of label names loaded.
1397 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
1400 g_return_val_if_fail( pilotFile != NULL, NULL );
1402 if( pilotFile->readMetadata ) {
1403 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1404 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1405 gchar *labelName = ai->phoneLabels[i];
1407 labelList = g_list_append(
1408 labelList, g_strdup( labelName ) );
1411 labelList = g_list_append(
1412 labelList, g_strdup( "" ) );
1420 * Load list with character strings of custom label names. Only none blank
1422 * \param pilotFile JPilot control data.
1423 * \param labelList List of label names to load.
1424 * \return List of label names loaded. Should be freed when done.
1426 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1429 g_return_val_if_fail( pilotFile != NULL, NULL );
1431 if( pilotFile->readMetadata ) {
1432 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1433 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1434 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1436 g_strchomp( labelName );
1437 g_strchug( labelName );
1438 if( *labelName != '\0' ) {
1439 labelList = g_list_append( labelList,
1440 g_strdup( labelName ) );
1449 * Load list with character strings of category names.
1450 * \param pilotFile JPilot control data.
1451 * \return List of label names loaded. Should be freed when done.
1453 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1454 GList *catList = NULL;
1457 g_return_val_if_fail( pilotFile != NULL, NULL );
1459 if( pilotFile->readMetadata ) {
1460 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1461 struct CategoryAppInfo *cat = & ai->category;
1462 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1463 gchar *catName = cat->name[i];
1465 catList = g_list_append(
1466 catList, g_strdup( catName ) );
1469 catList = g_list_append(
1470 catList, g_strdup( "" ) );
1478 * Build folder in address book for each category.
1479 * \param pilotFile JPilot control data.
1481 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1482 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1483 struct CategoryAppInfo *cat = & ai->category;
1486 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1487 ItemFolder *folder = addritem_create_item_folder();
1488 addritem_folder_set_name( folder, cat->name[i] );
1489 addrcache_id_folder( pilotFile->addressCache, folder );
1490 addrcache_add_folder( pilotFile->addressCache, folder );
1495 * Remove empty (category) folders.
1496 * \param pilotFile JPilot control data.
1498 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1504 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1508 ItemFolder *folder = node->data;
1509 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1510 if( folder->listPerson ) {
1511 /* Give name to folder */
1513 sprintf( name, "? %d", i );
1514 addritem_folder_set_name( folder, name );
1517 /* Mark for removal */
1518 remList = g_list_append( remList, folder );
1521 node = g_list_next( node );
1526 ItemFolder *folder = node->data;
1527 addrcache_remove_folder( pilotFile->addressCache, folder );
1528 node = g_list_next( node );
1530 g_list_free( remList );
1534 * Read address file into address cache.
1535 * \param pilotFile JPilot control data.
1536 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1539 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1541 GList *records = NULL;
1544 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1546 /* Read list of records from JPilot files */
1547 retVal = jpilot_read_db_files( pilotFile, &records );
1548 if( retVal != MGU_SUCCESS ) {
1549 jpilot_free_addrlist( records );
1553 /* Setup labels and category folders */
1554 jpilot_setup_labels( pilotFile );
1555 jpilot_build_category_list( pilotFile );
1557 /* Build array of pointers to categories */
1559 node = addrcache_get_list_folder( pilotFile->addressCache );
1561 if( i < JPILOT_NUM_CATEG ) {
1562 folderInd[i] = node->data;
1564 node = g_list_next( node );
1568 /* Load all addresses, free up old stuff as we go */
1572 if( ( br->rt != DELETED_PC_REC ) &&
1573 ( br->rt != DELETED_PALM_REC ) &&
1574 ( br->rt != MODIFIED_PALM_REC ) &&
1575 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1576 jpilot_load_address( pilotFile, br, folderInd );
1581 node = g_list_next( node );
1585 g_list_free( records );
1587 /* Remove empty category folders */
1588 jpilot_remove_empty( pilotFile );
1589 jpilot_mark_files( pilotFile );
1595 * Read file into list. Main entry point
1596 * \param pilotFile JPilot control data.
1597 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1600 gint jpilot_read_data( JPilotFile *pilotFile ) {
1601 g_return_val_if_fail( pilotFile != NULL, -1 );
1603 pilotFile->retVal = MGU_SUCCESS;
1604 pilotFile->addressCache->accessFlag = FALSE;
1605 if( jpilot_check_files( pilotFile ) ) {
1606 addrcache_clear( pilotFile->addressCache );
1607 jpilot_read_metadata( pilotFile );
1608 if( pilotFile->retVal == MGU_SUCCESS ) {
1609 pilotFile->retVal = jpilot_read_file( pilotFile );
1610 if( pilotFile->retVal == MGU_SUCCESS ) {
1611 pilotFile->addressCache->modified = FALSE;
1612 pilotFile->addressCache->dataRead = TRUE;
1616 return pilotFile->retVal;
1620 * Return linked list of persons. This is a list of references to ItemPerson
1621 * objects. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1622 * functions... this will destroy the addressbook data!
1624 * \param pilotFile JPilot control data.
1625 * \return List of persons.
1627 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1628 g_return_val_if_fail( pilotFile != NULL, NULL );
1629 return addrcache_get_list_person( pilotFile->addressCache );
1633 * Return linked list of folders. This is a list of references to non-empty
1634 * category folders. Do <b>NOT</b> attempt to use the
1635 * <code>addrbook_free_xxx()</code> functions... this will destroy the
1638 * \param pilotFile JPilot control data.
1639 * \return List of ItemFolder objects. This should not be freed.
1641 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1642 g_return_val_if_fail( pilotFile != NULL, NULL );
1643 return addrcache_get_list_folder( pilotFile->addressCache );
1647 * Return linked list of all persons. Note that the list contains references
1648 * to items. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1649 * functions... this will destroy the addressbook data!
1651 * \param pilotFile JPilot control data.
1652 * \return List of items, or NULL if none.
1654 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1655 g_return_val_if_fail( pilotFile != NULL, NULL );
1656 return addrcache_get_all_persons( pilotFile->addressCache );
1660 * Check label list for specified label name.
1661 * \param ai Address info to check.
1662 * \param lblCheck Label name to check.
1663 * \return Index of label in address structure. <code>-1</code> if NULL/empty
1664 * label name, <code>-2</code> if label not found.
1666 static gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1670 if( lblCheck == NULL ) return -1;
1671 if( strlen( lblCheck ) < 1 ) return -1;
1672 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1673 lblName = ai->labels[i];
1675 if( strlen( lblName ) ) {
1676 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1684 * Validate that all parameters specified.
1685 * \param pilotFile JPilot control data.
1686 * \return <i>TRUE</i> if data is good.
1688 gboolean jpilot_validate( JPilotFile *pilotFile ) {
1692 g_return_val_if_fail( pilotFile != NULL, FALSE );
1695 if( pilotFile->path ) {
1696 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1701 name = jpilot_get_name( pilotFile );
1703 if( strlen( name ) < 1 ) retVal = FALSE;
1711 #define WORK_BUFLEN 1024
1714 * Attempt to find a valid JPilot file.
1715 * \param pilotFile JPilot control data.
1716 * \return Filename, or home directory if not found, or empty string if
1717 * no home. Filename should be <code>g_free()</code> when done.
1719 gchar *jpilot_find_pilotdb( void ) {
1721 gchar str[ WORK_BUFLEN ];
1725 homedir = g_get_home_dir();
1726 if( ! homedir ) return g_strdup( "" );
1728 strcpy( str, homedir );
1729 len = strlen( str );
1731 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1732 str[ len ] = G_DIR_SEPARATOR;
1733 str[ ++len ] = '\0';
1736 strcat( str, JPILOT_DBHOME_DIR );
1737 strcat( str, G_DIR_SEPARATOR_S );
1738 strcat( str, JPILOT_DBHOME_FILE );
1740 /* Attempt to open */
1741 if( ( fp = fopen( str, "rb" ) ) != NULL ) {
1745 /* Truncate filename */
1748 return g_strdup( str );
1752 * Attempt to read file, testing for valid JPilot format.
1753 * \param fileSpec File specification to read.
1754 * \return <i>TRUE<i> if file appears to be valid format.
1756 gint jpilot_test_read_file( const gchar *fileSpec ) {
1757 JPilotFile *pilotFile;
1761 pilotFile = jpilot_create_path( fileSpec );
1762 retVal = jpilot_read_metadata( pilotFile );
1763 jpilot_free( pilotFile );
1767 retVal = MGU_NO_FILE;
1773 * Check whether label is in list of custom labels.
1774 * \param pilotFile JPilot control data.
1775 * \param labelName to test.
1776 * \return <i>TRUE</i> if found.
1778 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1782 g_return_val_if_fail( pilotFile != NULL, FALSE );
1786 node = pilotFile->customLabels;
1788 if( g_strcasecmp( labelName, ( gchar * ) node->data ) == 0 ) {
1792 node = g_list_next( node );
1799 * Test whether pilot link library installed.
1800 * \return <i>TRUE</i> if library available.
1802 gboolean jpilot_test_pilot_lib( void ) {
1806 #endif /* USE_JPILOT */