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.
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"
55 #define JPILOT_DBHOME_DIR ".jpilot"
56 #define JPILOT_DBHOME_FILE "AddressDB.pdb"
57 #define PILOT_LINK_LIB_NAME "libpisock.so"
59 #define IND_LABEL_LASTNAME 0 /* Index of last name in address data */
60 #define IND_LABEL_FIRSTNAME 1 /* Index of first name in address data */
61 #define IND_PHONE_EMAIL 4 /* Index of E-Mail address in phone labels */
62 #define OFFSET_PHONE_LABEL 3 /* Offset to phone data in address data */
63 #define IND_CUSTOM_LABEL 14 /* Offset to custom label names */
64 #define NUM_CUSTOM_LABEL 4 /* Number of custom labels */
66 /* Shamelessly copied from JPilot (libplugin.h) */
68 unsigned char db_name[32];
69 unsigned char flags[2];
70 unsigned char version[2];
71 unsigned char creation_time[4];
72 unsigned char modification_time[4];
73 unsigned char backup_time[4];
74 unsigned char modification_number[4];
75 unsigned char app_info_offset[4];
76 unsigned char sort_info_offset[4];
77 unsigned char type[4];/*Database ID */
78 unsigned char creator_id[4];/*Application ID */
79 unsigned char unique_id_seed[4];
80 unsigned char next_record_list_id[4];
81 unsigned char number_of_records[2];
84 /* Shamelessly copied from JPilot (libplugin.h) */
90 time_t modification_time;
92 unsigned int modification_number;
93 unsigned int app_info_offset;
94 unsigned int sort_info_offset;
95 char type[5];/*Database ID */
96 char creator_id[5];/*Application ID */
97 char unique_id_seed[5];
98 unsigned int next_record_list_id;
99 unsigned int number_of_records;
102 /* Shamelessly copied from JPilot (libplugin.h) */
104 unsigned char Offset[4]; /*4 bytes offset from BOF to record */
105 unsigned char attrib;
106 unsigned char unique_ID[3];
109 /* Shamelessly copied from JPilot (libplugin.h) */
110 typedef struct mem_rec_header_s {
111 unsigned int rec_num;
113 unsigned int unique_id;
114 unsigned char attrib;
115 struct mem_rec_header_s *next;
118 /* Shamelessly copied from JPilot (libplugin.h) */
119 #define SPENT_PC_RECORD_BIT 256
123 MODIFIED_PALM_REC = 101L,
124 DELETED_PALM_REC = 102L,
126 DELETED_PC_REC = SPENT_PC_RECORD_BIT + 104L,
127 DELETED_DELETED_PALM_REC = SPENT_PC_RECORD_BIT + 105L
130 /* Shamelessly copied from JPilot (libplugin.h) */
133 unsigned int unique_id;
134 unsigned char attrib;
140 * Create new pilot file object.
142 JPilotFile *jpilot_create() {
143 JPilotFile *pilotFile;
144 pilotFile = g_new0( JPilotFile, 1 );
145 pilotFile->name = NULL;
146 pilotFile->file = NULL;
147 pilotFile->path = NULL;
148 pilotFile->addressCache = addrcache_create();
149 pilotFile->readMetadata = FALSE;
150 pilotFile->customLabels = NULL;
151 pilotFile->labelInd = NULL;
152 pilotFile->retVal = MGU_SUCCESS;
153 pilotFile->accessFlag = FALSE;
158 * Create new pilot file object for specified file.
160 JPilotFile *jpilot_create_path( const gchar *path ) {
161 JPilotFile *pilotFile;
162 pilotFile = jpilot_create();
163 jpilot_set_file( pilotFile, path );
170 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
171 g_return_if_fail( pilotFile != NULL );
172 pilotFile->name = mgu_replace_string( pilotFile->name, value );
174 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
175 g_return_if_fail( pilotFile != NULL );
176 addrcache_refresh( pilotFile->addressCache );
177 pilotFile->readMetadata = FALSE;
178 pilotFile->path = mgu_replace_string( pilotFile->path, value );
180 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
181 g_return_if_fail( pilotFile != NULL );
182 pilotFile->accessFlag = value;
185 gint jpilot_get_status( JPilotFile *pilotFile ) {
186 g_return_val_if_fail( pilotFile != NULL, -1 );
187 return pilotFile->retVal;
189 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
190 g_return_val_if_fail( pilotFile != NULL, NULL );
191 return addrcache_get_root_folder( pilotFile->addressCache );
193 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
194 g_return_val_if_fail( pilotFile != NULL, NULL );
195 return pilotFile->name;
199 * Test whether file was modified since last access.
200 * Return: TRUE if file was modified.
202 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
203 g_return_val_if_fail( pilotFile != NULL, FALSE );
204 return addrcache_check_file( pilotFile->addressCache, pilotFile->path );
206 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
207 g_return_val_if_fail( pilotFile != NULL, FALSE );
208 return pilotFile->accessFlag;
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 * Free up pilot file object by releasing internal memory.
287 void jpilot_free( JPilotFile *pilotFile ) {
288 g_return_if_fail( pilotFile != NULL );
290 /* Free internal stuff */
291 g_free( pilotFile->path );
293 /* Release custom labels */
294 jpilot_clear_custom_labels( pilotFile );
297 addrcache_clear( pilotFile->addressCache );
298 addrcache_free( pilotFile->addressCache );
299 pilotFile->addressCache = NULL;
300 pilotFile->readMetadata = FALSE;
301 pilotFile->accessFlag = FALSE;
303 /* Now release file object */
308 * Refresh internal variables to force a file read.
310 void jpilot_force_refresh( JPilotFile *pilotFile ) {
311 addrcache_refresh( pilotFile->addressCache );
315 * Print object to specified stream.
317 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
320 g_return_if_fail( pilotFile != NULL );
322 fprintf( stream, "JPilotFile:\n" );
323 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
324 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
325 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
327 node = pilotFile->customLabels;
329 fprintf( stream, " c label: %s\n", (gchar *)node->data );
330 node = g_list_next( node );
333 node = pilotFile->labelInd;
335 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
336 node = g_list_next( node );
339 addrcache_print( pilotFile->addressCache, stream );
340 addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
344 * Print summary of object to specified stream.
346 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
348 g_return_if_fail( pilotFile != NULL );
349 fprintf( stream, "JPilotFile:\n" );
350 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
351 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
352 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
354 node = pilotFile->customLabels;
356 fprintf( stream, " c label: %s\n", (gchar *)node->data );
357 node = g_list_next( node );
360 node = pilotFile->labelInd;
362 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
363 node = g_list_next( node );
365 addrcache_print( pilotFile->addressCache, stream );
368 /* Shamelessly copied from JPilot (libplugin.c) */
369 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
372 for (i=0;i<num_bytes;i++) {
378 /* Shamelessly copied from JPilot (utils.c)
379 These next 2 functions were copied from pi-file.c in the pilot-link app
380 Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
381 #define PILOT_TIME_DELTA (unsigned)(2082844800)
383 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
384 return (time_t)(raw_time - PILOT_TIME_DELTA);
387 /* Shamelessly copied from JPilot (libplugin.c) */
388 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
391 strncpy(dbh->db_name, rdbh->db_name, 31);
392 dbh->db_name[31] = '\0';
393 dbh->flags = bytes_to_bin(rdbh->flags, 2);
394 dbh->version = bytes_to_bin(rdbh->version, 2);
395 temp = bytes_to_bin(rdbh->creation_time, 4);
396 dbh->creation_time = pilot_time_to_unix_time(temp);
397 temp = bytes_to_bin(rdbh->modification_time, 4);
398 dbh->modification_time = pilot_time_to_unix_time(temp);
399 temp = bytes_to_bin(rdbh->backup_time, 4);
400 dbh->backup_time = pilot_time_to_unix_time(temp);
401 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
402 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
403 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
404 strncpy(dbh->type, rdbh->type, 4);
406 strncpy(dbh->creator_id, rdbh->creator_id, 4);
407 dbh->creator_id[4] = '\0';
408 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
409 dbh->unique_id_seed[4] = '\0';
410 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
411 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
415 /* Shamelessly copied from JPilot (libplugin.c) */
416 /*returns 1 if found */
418 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
419 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
421 mem_rec_header *temp_mem_rh;
422 unsigned char found = 0;
423 unsigned long found_at;
426 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
427 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
428 found_at = temp_mem_rh->offset;
429 /* *attrib = temp_mem_rh->attrib; */
430 /* *unique_id = temp_mem_rh->unique_id; */
432 if ((temp_mem_rh->offset == fpos)) {
434 *attrib = temp_mem_rh->attrib;
435 *unique_id = temp_mem_rh->unique_id;
438 *next_offset = found_at;
442 /* Shamelessly copied from JPilot (libplugin.c) */
443 static void free_mem_rec_header(mem_rec_header **mem_rh) {
444 mem_rec_header *h, *next_h;
445 for (h=*mem_rh; h; h=next_h) {
452 /* Shamelessly copied from JPilot (libplugin.c) */
453 static int jpilot_free_db_list( GList **br_list ) {
454 GList *temp_list, *first;
457 /* Go to first entry in the list */
459 for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
462 for (temp_list = first; temp_list; temp_list = temp_list->next) {
463 if (temp_list->data) {
467 temp_list->data=NULL;
472 g_list_free(*br_list);
477 /* Shamelessly copied from JPilot (libplugin.c)
479 static int jpilot_get_info_size( FILE *in, int *size ) {
485 fseek(in, 0, SEEK_SET);
486 fread(&rdbh, sizeof(RawDBHeader), 1, in);
488 /* fprintf( stderr, "error reading file in 'jpilot_get_info_size'\n" ); */
492 raw_header_to_header(&rdbh, &dbh);
493 if (dbh.app_info_offset==0) {
497 if (dbh.sort_info_offset!=0) {
498 *size = dbh.sort_info_offset - dbh.app_info_offset;
501 if (dbh.number_of_records==0) {
502 fseek(in, 0, SEEK_END);
503 *size=ftell(in) - dbh.app_info_offset;
507 fread(&rh, sizeof(record_header), 1, in);
508 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
509 *size=offset - dbh.app_info_offset;
514 /* Read address file into address list. Based on JPilot's
515 libplugin.c (jp_get_app_info) */
516 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
519 unsigned int rec_size;
523 if( ( !buf_size ) || ( ! buf ) ) {
530 if( pilotFile->path ) {
531 in = fopen( pilotFile->path, "r" );
533 /* fprintf( stderr, "can't open %s\n", pilotFile->path ); */
534 return MGU_OPEN_FILE;
538 /* fprintf( stderr, "file not specified\n" ); */
542 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
545 /* fprintf( stderr, "error reading %s\n", pilotFile->path ); */
547 return MGU_ERROR_READ;
555 /* Convert header into something recognizable */
556 raw_header_to_header(&rdbh, &dbh);
558 num = jpilot_get_info_size(in, &rec_size);
561 return MGU_ERROR_READ;
564 fseek(in, dbh.app_info_offset, SEEK_SET);
565 *buf = ( char * ) malloc(rec_size);
567 /* fprintf( stderr, "jpilot_get_file_info(): Out of memory\n" ); */
569 return MGU_OO_MEMORY;
571 num = fread(*buf, rec_size, 1, in);
576 /* fprintf( stderr, "Error reading %s\n", pilotFile->path ); */
577 return MGU_ERROR_READ;
582 *buf_size = rec_size;
587 #define FULLNAME_BUFSIZE 256
588 #define EMAIL_BUFSIZE 256
589 /* Read address file into address cache. Based on JPilot's
590 jp_read_DB_files (from libplugin.c) */
591 static gint jpilot_read_file( JPilotFile *pilotFile ) {
594 gint num_records, recs_returned, i, num;
595 guint offset, prev_offset, next_offset, rec_size;
597 glong fpos; /*file position indicator */
601 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
607 struct AddressAppInfo *ai;
610 gchar fullName[ FULLNAME_BUFSIZE ];
611 gchar bufEMail[ EMAIL_BUFSIZE ];
618 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
620 retVal = MGU_SUCCESS;
621 mem_rh = last_mem_rh = NULL;
624 /* Pointer to address metadata. */
625 ai = & pilotFile->addrInfo;
627 /* Open file for read */
628 if( pilotFile->path ) {
629 in = fopen( pilotFile->path, "r" );
631 /* fprintf( stderr, "can't open %s\n", pilotFile->path ); */
632 return MGU_OPEN_FILE;
636 /* fprintf( stderr, "file not specified\n" ); */
640 /* Read the database header */
641 num = fread(&rdbh, sizeof(RawDBHeader), 1, in);
644 /* fprintf( stderr, "error reading '%s'\n", pilotFile->path ); */
646 return MGU_ERROR_READ;
653 raw_header_to_header(&rdbh, &dbh);
655 /*Read each record entry header */
656 num_records = dbh.number_of_records;
660 for (i=1; i<num_records+1; i++) {
661 num = fread( &rh, sizeof( record_header ), 1, in );
664 /* fprintf( stderr, "error reading '%s'\n", pilotFile->path ); */
665 retVal = MGU_ERROR_READ;
673 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
674 if (offset < prev_offset) {
677 prev_offset = offset;
679 temp_mem_rh = (mem_rec_header *)malloc(sizeof(mem_rec_header));
681 /* fprintf( stderr, "jpilot_read_db_file(): Out of memory 1\n" ); */
682 retVal = MGU_OO_MEMORY;
686 temp_mem_rh->next = NULL;
687 temp_mem_rh->rec_num = i;
688 temp_mem_rh->offset = offset;
689 temp_mem_rh->attrib = rh.attrib;
690 temp_mem_rh->unique_id = (rh.unique_ID[0]*256+rh.unique_ID[1])*256+rh.unique_ID[2];
692 if (mem_rh == NULL) {
693 mem_rh = temp_mem_rh;
694 last_mem_rh = temp_mem_rh;
697 last_mem_rh->next = temp_mem_rh;
698 last_mem_rh = temp_mem_rh;
703 temp_mem_rh = mem_rh;
707 find_next_offset(mem_rh, 0, &next_offset, &attrib, &unique_id);
711 next_offset = mem_rh->offset;
712 attrib = mem_rh->attrib;
713 unique_id = mem_rh->unique_id;
716 fseek(in, next_offset, SEEK_SET);
718 /* Build array of pointers to categories; */
720 node = addrcache_get_list_folder( pilotFile->addressCache );
722 if( i < JPILOT_NUM_CATEG ) {
723 folderInd[i] = node->data;
725 node = g_list_next( node );
729 /* Now go load all records */
731 /* struct CategoryAppInfo *cat = &ai->category; */
735 find_next_offset(mem_rh, fpos, &next_offset, &attrib, &unique_id);
738 next_offset = 0xFFFFFF;
740 attrib = temp_mem_rh->attrib;
741 unique_id = temp_mem_rh->unique_id;
742 cat_id = attrib & 0x0F;
743 if (temp_mem_rh->next) {
744 temp_mem_rh = temp_mem_rh->next;
745 next_offset = temp_mem_rh->offset;
749 rec_size = next_offset - fpos;
751 buf = ( char * ) malloc(rec_size);
753 num = fread(buf, rec_size, 1, in);
756 /* fprintf( stderr, "Error reading %s 5\n", pilotFile ); */
758 retVal = MGU_ERROR_READ;
763 /* Retrieve address */
764 inum = unpack_Address( & addr, buf, rec_size );
766 addrEnt = addr.entry;
768 *fullName = *bufEMail = '\0';
769 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
770 strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
773 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
774 strcat( fullName, " " );
775 strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
777 g_strchug( fullName );
778 g_strchomp( fullName );
780 person = addritem_create_item_person();
781 addritem_person_set_common_name( person, fullName );
782 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
783 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
784 addrcache_id_person( pilotFile->addressCache, person );
786 extID = g_strdup_printf( "%d", unique_id );
787 addritem_person_set_external_id( person, extID );
791 /* Add entry for each email address listed under phone labels. */
792 indPhoneLbl = addr.phoneLabel;
793 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
795 ind = indPhoneLbl[k];
797 fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
798 ai->phoneLabels[ind], addrEnt[3+k] );
800 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
801 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
803 strcpy( bufEMail, labelEntry );
804 g_strchug( bufEMail );
805 g_strchomp( bufEMail );
807 email = addritem_create_item_email();
808 addritem_email_set_address( email, bufEMail );
809 addrcache_id_email( pilotFile->addressCache, email );
810 addrcache_person_add_email(
811 pilotFile->addressCache, person, email );
816 /* Add entry for each custom label */
817 node = pilotFile->labelInd;
820 ind = GPOINTER_TO_INT( node->data );
823 fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
826 labelEntry = addrEnt[ind];
828 strcpy( bufEMail, labelEntry );
829 g_strchug( bufEMail );
830 g_strchomp( bufEMail );
832 email = addritem_create_item_email();
833 addritem_email_set_address( email, bufEMail );
834 addritem_email_set_remarks( email, ai->labels[ind] );
835 addrcache_id_email( pilotFile->addressCache, email );
836 addrcache_person_add_email(
837 pilotFile->addressCache, person, email );
842 node = g_list_next( node );
845 if( person->listEMail ) {
846 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
847 /* Add to specified category */
848 addrcache_folder_add_person(
849 pilotFile->addressCache, folderInd[cat_id], person );
852 /* Add to root folder */
853 addrcache_add_person( pilotFile->addressCache, person );
857 addritem_free_item_person( person );
866 free_mem_rec_header(&mem_rh);
871 * Read metadata from file.
873 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
875 unsigned int rec_size;
879 g_return_val_if_fail( pilotFile != NULL, -1 );
881 pilotFile->readMetadata = FALSE;
882 addrcache_clear( pilotFile->addressCache );
885 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
886 if( retVal != MGU_SUCCESS ) {
887 pilotFile->retVal = retVal;
888 return pilotFile->retVal;
891 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
896 /* fprintf( stderr, "error reading '%s'\n", pilotFile->path ); */
897 pilotFile->retVal = MGU_ERROR_READ;
898 return pilotFile->retVal;
901 pilotFile->readMetadata = TRUE;
902 pilotFile->retVal = MGU_SUCCESS;
903 return pilotFile->retVal;
907 * Setup labels and indexes from metadata.
908 * Return: TRUE is setup successfully.
910 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
911 gboolean retVal = FALSE;
912 struct AddressAppInfo *ai;
915 g_return_val_if_fail( pilotFile != NULL, -1 );
917 /* Release indexes */
918 node = pilotFile->labelInd;
921 node = g_list_next( node );
923 pilotFile->labelInd = NULL;
925 if( pilotFile->readMetadata ) {
926 ai = & pilotFile->addrInfo;
927 node = pilotFile->customLabels;
929 gchar *lbl = node->data;
932 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
933 gchar *labelName = ai->labels[i];
934 if( g_strcasecmp( labelName, lbl ) == 0 ) {
939 pilotFile->labelInd = g_list_append( pilotFile->labelInd, GINT_TO_POINTER(ind) );
940 node = g_list_next( node );
948 * Load list with character strings of label names.
950 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
953 g_return_val_if_fail( pilotFile != NULL, NULL );
955 if( pilotFile->readMetadata ) {
956 struct AddressAppInfo *ai = & pilotFile->addrInfo;
957 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
958 gchar *labelName = ai->labels[i];
960 labelList = g_list_append( labelList, g_strdup( labelName ) );
963 labelList = g_list_append( labelList, g_strdup( "" ) );
971 * Return category name for specified category ID.
972 * Enter: Category ID.
973 * Return: Name, or empty string if not invalid ID. Name should be g_free() when done.
975 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
976 gchar *catName = NULL;
978 g_return_val_if_fail( pilotFile != NULL, NULL );
980 if( pilotFile->readMetadata ) {
981 struct AddressAppInfo *ai = & pilotFile->addrInfo;
982 struct CategoryAppInfo *cat = & ai->category;
983 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
986 catName = g_strdup( cat->name[catID] );
989 if( ! catName ) catName = g_strdup( "" );
994 * Load list with character strings of phone label names.
996 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
999 g_return_val_if_fail( pilotFile != NULL, NULL );
1001 if( pilotFile->readMetadata ) {
1002 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1003 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
1004 gchar *labelName = ai->phoneLabels[i];
1006 labelList = g_list_append( labelList, g_strdup( labelName ) );
1009 labelList = g_list_append( labelList, g_strdup( "" ) );
1017 * Load list with character strings of label names. Only none blank names
1020 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1023 g_return_val_if_fail( pilotFile != NULL, NULL );
1025 if( pilotFile->readMetadata ) {
1026 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1027 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1028 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1030 g_strchomp( labelName );
1031 g_strchug( labelName );
1032 if( *labelName != '\0' ) {
1033 labelList = g_list_append( labelList, g_strdup( labelName ) );
1042 * Load list with character strings of category names.
1044 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1045 GList *catList = NULL;
1048 g_return_val_if_fail( pilotFile != NULL, NULL );
1050 if( pilotFile->readMetadata ) {
1051 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1052 struct CategoryAppInfo *cat = & ai->category;
1053 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1054 gchar *catName = cat->name[i];
1056 catList = g_list_append( catList, g_strdup( catName ) );
1059 catList = g_list_append( catList, g_strdup( "" ) );
1067 * Build folder for each category.
1069 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1070 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1071 struct CategoryAppInfo *cat = & ai->category;
1074 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1075 ItemFolder *folder = addritem_create_item_folder();
1076 addritem_folder_set_name( folder, cat->name[i] );
1077 addrcache_id_folder( pilotFile->addressCache, folder );
1078 addrcache_add_folder( pilotFile->addressCache, folder );
1083 * Remove empty folders (categories).
1085 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1091 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1095 ItemFolder *folder = node->data;
1096 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1097 if( folder->listPerson ) {
1098 /* Give name to folder */
1100 sprintf( name, "? %d", i );
1101 addritem_folder_set_name( folder, name );
1104 /* Mark for removal */
1105 remList = g_list_append( remList, folder );
1108 node = g_list_next( node );
1113 ItemFolder *folder = node->data;
1114 addrcache_remove_folder( pilotFile->addressCache, folder );
1115 node = g_list_next( node );
1117 g_list_free( remList );
1120 /* ============================================================================================
1122 * Read file into list. Main entry point
1123 * Return: TRUE if file read successfully.
1125 ============================================================================================ */
1126 gint jpilot_read_data( JPilotFile *pilotFile ) {
1127 g_return_val_if_fail( pilotFile != NULL, -1 );
1129 pilotFile->retVal = MGU_SUCCESS;
1130 pilotFile->accessFlag = FALSE;
1131 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) ) {
1132 addrcache_clear( pilotFile->addressCache );
1133 jpilot_read_metadata( pilotFile );
1134 if( pilotFile->retVal == MGU_SUCCESS ) {
1135 jpilot_setup_labels( pilotFile );
1136 jpilot_build_category_list( pilotFile );
1137 pilotFile->retVal = jpilot_read_file( pilotFile );
1138 if( pilotFile->retVal == MGU_SUCCESS ) {
1139 jpilot_remove_empty( pilotFile );
1140 addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
1141 pilotFile->addressCache->modified = FALSE;
1142 pilotFile->addressCache->dataRead = TRUE;
1146 return pilotFile->retVal;
1150 * Return link list of persons.
1152 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1153 g_return_val_if_fail( pilotFile != NULL, NULL );
1154 return addrcache_get_list_person( pilotFile->addressCache );
1158 * Return link list of folders. This is always NULL since there are
1159 * no folders in GnomeCard.
1162 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1163 g_return_val_if_fail( pilotFile != NULL, NULL );
1164 return addrcache_get_list_folder( pilotFile->addressCache );
1168 * Return link list of all persons. Note that the list contains references
1169 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
1170 * this will destroy the addressbook data!
1171 * Return: List of items, or NULL if none.
1173 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1174 g_return_val_if_fail( pilotFile != NULL, NULL );
1175 return addrcache_get_all_persons( pilotFile->addressCache );
1179 * Check label list for specified label.
1181 gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1185 if( lblCheck == NULL ) return -1;
1186 if( strlen( lblCheck ) < 1 ) return -1;
1187 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1188 lblName = ai->labels[i];
1190 if( strlen( lblName ) ) {
1191 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1199 * Validate that all parameters specified.
1200 * Return: TRUE if data is good.
1202 gboolean jpilot_validate( const JPilotFile *pilotFile ) {
1205 g_return_val_if_fail( pilotFile != NULL, FALSE );
1208 if( pilotFile->path ) {
1209 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1214 if( pilotFile->name ) {
1215 if( strlen( pilotFile->name ) < 1 ) retVal = FALSE;
1223 #define WORK_BUFLEN 1024
1226 * Attempt to find a valid JPilot file.
1227 * Return: Filename, or home directory if not found, or empty string if
1228 * no home. Filename should be g_free() when done.
1230 gchar *jpilot_find_pilotdb( void ) {
1232 gchar str[ WORK_BUFLEN ];
1236 homedir = g_get_home_dir();
1237 if( ! homedir ) return g_strdup( "" );
1239 strcpy( str, homedir );
1240 len = strlen( str );
1242 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1243 str[ len ] = G_DIR_SEPARATOR;
1244 str[ ++len ] = '\0';
1247 strcat( str, JPILOT_DBHOME_DIR );
1248 strcat( str, G_DIR_SEPARATOR_S );
1249 strcat( str, JPILOT_DBHOME_FILE );
1251 /* Attempt to open */
1252 if( ( fp = fopen( str, "r" ) ) != NULL ) {
1256 /* Truncate filename */
1259 return g_strdup( str );
1263 * Attempt to read file, testing for valid JPilot format.
1264 * Return: TRUE if file appears to be valid format.
1266 gint jpilot_test_read_file( const gchar *fileSpec ) {
1267 JPilotFile *pilotFile;
1271 pilotFile = jpilot_create_path( fileSpec );
1272 retVal = jpilot_read_metadata( pilotFile );
1273 jpilot_free( pilotFile );
1277 retVal = MGU_NO_FILE;
1283 * Check whether label is in custom labels.
1284 * Return: TRUE if found.
1286 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1290 g_return_val_if_fail( pilotFile != NULL, FALSE );
1294 node = pilotFile->customLabels;
1296 if( g_strcasecmp( labelName, node->data ) == 0 ) {
1300 node = g_list_next( node );
1307 * Test whether pilot link library installed.
1308 * Return: TRUE if library available.
1310 gboolean jpilot_test_pilot_lib( void ) {
1313 handle = dlopen( PILOT_LINK_LIB_NAME, RTLD_LAZY );
1318 /* Test for symbols we need */
1319 fun = dlsym( handle, "unpack_Address" );
1325 fun = dlsym( handle, "unpack_AddressAppInfo" );
1334 #endif /* USE_JPILOT */