2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Match Grun
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Functions necessary to access JPilot database files.
22 * JPilot is Copyright(c) by Judd Montgomery.
23 * Visit http://www.jpilot.org for more details.
38 #include <pi-appinfo.h>
39 #include <pi-address.h>
44 #define JPILOT_DBHOME_DIR ".jpilot"
45 #define JPILOT_DBHOME_FILE "AddressDB.pdb"
46 #define PILOT_LINK_LIB_NAME "libpisock.so"
48 #define IND_LABEL_LASTNAME 0 // Index of last name in address data
49 #define IND_LABEL_FIRSTNAME 1 // Index of first name in address data
50 #define IND_PHONE_EMAIL 4 // Index of E-Mail address in phone labels
51 #define OFFSET_PHONE_LABEL 3 // Offset to phone data in address data
52 #define IND_CUSTOM_LABEL 14 // Offset to custom label names
53 #define NUM_CUSTOM_LABEL 4 // Number of custom labels
55 typedef struct _JPilotCategory JPilotCategory;
56 struct _JPilotCategory {
57 AddressItem *category;
63 * Specify name to be used.
65 void jpilot_set_name( JPilotFile* pilotFile, const gchar *name ) {
66 if( pilotFile->name ) g_free( pilotFile->name );
67 if( name ) pilotFile->name = g_strdup( name );
71 * Specify file to be used.
73 void jpilot_set_file( JPilotFile* pilotFile, const gchar *path ) {
74 g_return_if_fail( pilotFile != NULL );
75 mgu_refresh_cache( pilotFile->addressCache );
76 pilotFile->readMetadata = FALSE;
79 if( pilotFile->path ) g_free( pilotFile->path );
80 if( path ) pilotFile->path = g_strdup( path );
84 * Create new pilot file object.
86 JPilotFile *jpilot_create() {
87 JPilotFile *pilotFile;
88 pilotFile = g_new( JPilotFile, 1 );
89 pilotFile->name = NULL;
90 pilotFile->path = NULL;
91 pilotFile->file = NULL;
92 pilotFile->addressCache = mgu_create_cache();
93 pilotFile->readMetadata = FALSE;
94 pilotFile->customLabels = NULL;
95 pilotFile->labelInd = NULL;
96 pilotFile->retVal = MGU_SUCCESS;
97 pilotFile->categoryList = NULL;
98 pilotFile->catAddrList = NULL;
103 * Create new pilot file object for specified file.
105 JPilotFile *jpilot_create_path( const gchar *path ) {
106 JPilotFile *pilotFile;
107 pilotFile = jpilot_create();
108 jpilot_set_file( pilotFile, path );
113 * Test whether file was modified since last access.
114 * Return: TRUE if file was modified.
116 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
117 g_return_if_fail( pilotFile != NULL );
118 return mgu_check_file( pilotFile->addressCache, pilotFile->path );
122 * Free up custom label list.
124 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
126 g_return_if_fail( pilotFile != NULL );
128 // Release custom labels
129 mgu_free_list( pilotFile->customLabels );
130 pilotFile->customLabels = NULL;
133 node = pilotFile->labelInd;
136 node = g_slist_next( node );
138 g_slist_free( pilotFile->labelInd );
139 pilotFile->labelInd = NULL;
141 // Force a fresh read
142 mgu_refresh_cache( pilotFile->addressCache );
146 * Append a custom label, representing an E-Mail address field to the
149 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
150 g_return_if_fail( pilotFile != NULL );
153 gchar *labelCopy = g_strdup( labelName );
154 g_strstrip( labelCopy );
155 if( *labelCopy == '\0' ) {
159 pilotFile->customLabels = g_slist_append( pilotFile->customLabels, labelCopy );
160 // Force a fresh read
161 mgu_refresh_cache( pilotFile->addressCache );
167 * Get list of custom labels.
168 * Return: List of labels. Must use g_free() when done.
170 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
171 GList *retVal = NULL;
173 g_return_if_fail( pilotFile != NULL );
174 node = pilotFile->customLabels;
176 retVal = g_list_append( retVal, g_strdup( node->data ) );
177 node = g_slist_next( node );
183 * Free up pilot file object by releasing internal memory.
185 void jpilot_free( JPilotFile *pilotFile ) {
186 g_return_if_fail( pilotFile != NULL );
188 /* Free internal stuff */
189 g_free( pilotFile->path );
191 // Release custom labels
192 jpilot_clear_custom_labels( pilotFile );
195 mgu_clear_cache( pilotFile->addressCache );
196 mgu_free_cache( pilotFile->addressCache );
197 mgu_free_dlist( pilotFile->categoryList );
198 pilotFile->addressCache = NULL;
199 pilotFile->readMetadata = FALSE;
200 pilotFile->categoryList = NULL;
201 pilotFile->catAddrList = NULL;
203 /* Now release file object */
208 * Refresh internal variables to force a file read.
210 void jpilot_force_refresh( JPilotFile *pilotFile ) {
211 mgu_refresh_cache( pilotFile->addressCache );
215 * Print category address list for specified category.
217 void jpilot_print_category( JPilotCategory *jpcat, FILE *stream ) {
218 fprintf( stream, "category: %s : count: %d\n", jpcat->category->name, jpcat->count );
219 if( jpcat->addressList ) {
220 mgu_print_address_list( jpcat->addressList, stream );
222 fprintf( stream, "=========================================\n" );
226 * Print address list for all categories.
228 void jpilot_print_category_list( GList *catAddrList, FILE *stream ) {
230 JPilotCategory *jpcat;
231 if( catAddrList == NULL ) return;
233 fprintf( stream, "Address list by category\n" );
237 jpilot_print_category( jpcat, stream );
238 node = g_list_next( node );
243 * Print object to specified stream.
245 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
247 g_return_if_fail( pilotFile != NULL );
248 fprintf( stream, "JPilotFile:\n" );
249 fprintf( stream, "file spec: '%s'\n", pilotFile->path );
250 fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
251 fprintf( stream, " ret val: %d\n", pilotFile->retVal );
253 node = pilotFile->customLabels;
255 fprintf( stream, " c label: %s\n", node->data );
256 node = g_slist_next( node );
259 node = pilotFile->labelInd;
261 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
262 node = g_slist_next( node );
265 mgu_print_cache( pilotFile->addressCache, stream );
266 jpilot_print_category_list( pilotFile->catAddrList, stream );
269 // Shamelessly copied from JPilot (libplugin.c)
270 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
273 for (i=0;i<num_bytes;i++) {
279 // Shamelessly copied from JPilot (utils.c)
280 /*These next 2 functions were copied from pi-file.c in the pilot-link app */
281 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
282 #define PILOT_TIME_DELTA (unsigned)(2082844800)
284 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
285 return (time_t)(raw_time - PILOT_TIME_DELTA);
288 // Shamelessly copied from JPilot (libplugin.c)
289 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
291 strncpy(dbh->db_name, rdbh->db_name, 31);
292 dbh->db_name[31] = '\0';
293 dbh->flags = bytes_to_bin(rdbh->flags, 2);
294 dbh->version = bytes_to_bin(rdbh->version, 2);
295 temp = bytes_to_bin(rdbh->creation_time, 4);
296 dbh->creation_time = pilot_time_to_unix_time(temp);
297 temp = bytes_to_bin(rdbh->modification_time, 4);
298 dbh->modification_time = pilot_time_to_unix_time(temp);
299 temp = bytes_to_bin(rdbh->backup_time, 4);
300 dbh->backup_time = pilot_time_to_unix_time(temp);
301 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
302 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
303 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
304 strncpy(dbh->type, rdbh->type, 4);
306 strncpy(dbh->creator_id, rdbh->creator_id, 4);
307 dbh->creator_id[4] = '\0';
308 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
309 dbh->unique_id_seed[4] = '\0';
310 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
311 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
315 // Shamelessly copied from JPilot (libplugin.c)
316 /*returns 1 if found */
318 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
319 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
321 mem_rec_header *temp_mem_rh;
322 unsigned char found = 0;
323 unsigned long found_at;
326 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
327 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
328 found_at = temp_mem_rh->offset;
329 /* *attrib = temp_mem_rh->attrib; */
330 /* *unique_id = temp_mem_rh->unique_id; */
332 if ((temp_mem_rh->offset == fpos)) {
334 *attrib = temp_mem_rh->attrib;
335 *unique_id = temp_mem_rh->unique_id;
338 *next_offset = found_at;
342 // Shamelessly copied from JPilot (libplugin.c)
343 static void free_mem_rec_header(mem_rec_header **mem_rh) {
344 mem_rec_header *h, *next_h;
345 for (h=*mem_rh; h; h=next_h) {
352 // Shamelessly copied from JPilot (libplugin.c)
353 int jpilot_free_db_list( GList **br_list ) {
354 GList *temp_list, *first;
357 /* Go to first entry in the list */
359 for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
362 for (temp_list = first; temp_list; temp_list = temp_list->next) {
363 if (temp_list->data) {
367 temp_list->data=NULL;
372 g_list_free(*br_list);
377 // Shamelessly copied from JPilot (libplugin.c)
379 int jpilot_get_info_size( FILE *in, int *size ) {
385 fseek(in, 0, SEEK_SET);
386 fread(&rdbh, sizeof(RawDBHeader), 1, in);
388 // fprintf( stderr, "error reading file in 'jpilot_get_info_size'\n" );
392 raw_header_to_header(&rdbh, &dbh);
393 if (dbh.app_info_offset==0) {
397 if (dbh.sort_info_offset!=0) {
398 *size = dbh.sort_info_offset - dbh.app_info_offset;
401 if (dbh.number_of_records==0) {
402 fseek(in, 0, SEEK_END);
403 *size=ftell(in) - dbh.app_info_offset;
407 fread(&rh, sizeof(record_header), 1, in);
408 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
409 *size=offset - dbh.app_info_offset;
414 // Read address file into address list. Based on JPilot's
415 // libplugin.c (jp_get_app_info)
416 gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
419 unsigned int rec_size;
423 if( ( !buf_size ) || ( ! buf ) ) {
430 if( pilotFile->path ) {
431 in = fopen( pilotFile->path, "r" );
433 // fprintf( stderr, "can't open %s\n", pilotFile->path );
434 return MGU_OPEN_FILE;
438 // fprintf( stderr, "file not specified\n" );
442 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
445 // fprintf( stderr, "error reading %s\n", pilotFile->path );
447 return MGU_ERROR_READ;
455 // Convert header into something recognizable
456 raw_header_to_header(&rdbh, &dbh);
458 num = jpilot_get_info_size(in, &rec_size);
461 return MGU_ERROR_READ;
464 fseek(in, dbh.app_info_offset, SEEK_SET);
465 *buf = ( char * ) malloc(rec_size);
467 // fprintf( stderr, "jpilot_get_file_info(): Out of memory\n" );
469 return MGU_OO_MEMORY;
471 num = fread(*buf, rec_size, 1, in);
476 // fprintf( stderr, "Error reading %s\n", pilotFile->path );
477 return MGU_ERROR_READ;
482 *buf_size = rec_size;
487 #define FULLNAME_BUFSIZE 256
488 #define EMAIL_BUFSIZE 256
489 // Read address file into address cache. Based on JPilot's
490 // jp_read_DB_files (from libplugin.c)
491 gint jpilot_read_cache( JPilotFile *pilotFile ) {
494 int num_records, recs_returned, i, num, r;
495 unsigned int offset, prev_offset, next_offset, rec_size;
497 long fpos; /*file position indicator */
498 unsigned char attrib;
499 unsigned int unique_id;
501 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
507 struct AddressAppInfo *ai;
510 gchar fullName[ FULLNAME_BUFSIZE ];
511 gchar bufEMail[ EMAIL_BUFSIZE ];
513 AddressItem *addrItem = NULL;
518 retVal = MGU_SUCCESS;
519 mem_rh = last_mem_rh = NULL;
522 // Pointer to address metadata.
523 ai = & pilotFile->addrInfo;
525 // Open file for read
526 if( pilotFile->path ) {
527 in = fopen( pilotFile->path, "r" );
529 // fprintf( stderr, "can't open %s\n", pilotFile->path );
530 return MGU_OPEN_FILE;
534 // fprintf( stderr, "file not specified\n" );
538 /* Read the database header */
539 num = fread(&rdbh, sizeof(RawDBHeader), 1, in);
542 // fprintf( stderr, "error reading '%s'\n", pilotFile->path );
544 return MGU_ERROR_READ;
551 raw_header_to_header(&rdbh, &dbh);
553 /*Read each record entry header */
554 num_records = dbh.number_of_records;
558 for (i=1; i<num_records+1; i++) {
559 num = fread( &rh, sizeof( record_header ), 1, in );
562 // fprintf( stderr, "error reading '%s'\n", pilotFile->path );
563 retVal = MGU_ERROR_READ;
571 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
572 if (offset < prev_offset) {
575 prev_offset = offset;
577 temp_mem_rh = (mem_rec_header *)malloc(sizeof(mem_rec_header));
579 // fprintf( stderr, "jpilot_read_db_file(): Out of memory 1\n" );
580 retVal = MGU_OO_MEMORY;
584 temp_mem_rh->next = NULL;
585 temp_mem_rh->rec_num = i;
586 temp_mem_rh->offset = offset;
587 temp_mem_rh->attrib = rh.attrib;
588 temp_mem_rh->unique_id = (rh.unique_ID[0]*256+rh.unique_ID[1])*256+rh.unique_ID[2];
590 if (mem_rh == NULL) {
591 mem_rh = temp_mem_rh;
592 last_mem_rh = temp_mem_rh;
595 last_mem_rh->next = temp_mem_rh;
596 last_mem_rh = temp_mem_rh;
601 temp_mem_rh = mem_rh;
605 find_next_offset(mem_rh, 0, &next_offset, &attrib, &unique_id);
609 next_offset = mem_rh->offset;
610 attrib = mem_rh->attrib;
611 unique_id = mem_rh->unique_id;
614 fseek(in, next_offset, SEEK_SET);
616 // Now go load all records
618 struct CategoryAppInfo *cat = & ai->category;
621 find_next_offset(mem_rh, fpos, &next_offset, &attrib, &unique_id);
624 next_offset = 0xFFFFFF;
626 attrib = temp_mem_rh->attrib;
627 unique_id = temp_mem_rh->unique_id;
628 cat_id = attrib & 0x0F;
629 if (temp_mem_rh->next) {
630 temp_mem_rh = temp_mem_rh->next;
631 next_offset = temp_mem_rh->offset;
635 rec_size = next_offset - fpos;
637 buf = ( char * ) malloc(rec_size);
639 num = fread(buf, rec_size, 1, in);
642 // fprintf( stderr, "Error reading %s 5\n", pilotFile );
644 retVal = MGU_ERROR_READ;
650 inum = unpack_Address( & addr, buf, rec_size );
652 addrEnt = addr.entry;
654 *fullName = *bufEMail = '\0';
655 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
656 strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
659 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
660 strcat( fullName, " " );
661 strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
663 g_strchug( fullName );
664 g_strchomp( fullName );
665 extID = g_strdup_printf( "%d", unique_id );
667 // Add entry for each email address listed under phone labels.
668 indPhoneLbl = addr.phoneLabel;
669 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
671 ind = indPhoneLbl[k];
672 // fprintf( stdout, "%d : %d : %20s : %s\n", k, ind, ai->phoneLabels[ind], addrEnt[3+k] );
673 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
674 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
676 strcpy( bufEMail, labelEntry );
677 g_strchug( bufEMail );
678 g_strchomp( bufEMail );
680 addrItem = mgu_create_address();
681 addrItem->name = g_strdup( fullName );
682 addrItem->address = g_strdup( bufEMail );
683 addrItem->remarks = g_strdup( "" );
684 addrItem->externalID = g_strdup( extID );
685 addrItem->categoryID = cat_id;
686 mgu_add_cache( pilotFile->addressCache, addrItem );
691 // Add entry for each custom label
692 node = pilotFile->labelInd;
695 ind = GPOINTER_TO_INT( node->data );
697 // fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind], addrEnt[ind] );
698 labelEntry = addrEnt[ind];
700 strcpy( bufEMail, labelEntry );
701 g_strchug( bufEMail );
702 g_strchomp( bufEMail );
704 addrItem = mgu_create_address();
705 addrItem->name = g_strdup( fullName );
706 addrItem->address = g_strdup( bufEMail );
707 addrItem->remarks = g_strdup( ai->labels[ind] );
708 addrItem->externalID = g_strdup( extID );
709 addrItem->categoryID = cat_id;
710 mgu_add_cache( pilotFile->addressCache, addrItem );
715 node = g_slist_next( node );
725 free_mem_rec_header(&mem_rh);
730 * Read metadata from file.
732 gint jpilot_read_metadata( JPilotFile *pilotFile ) {
734 unsigned int rec_size;
738 g_return_if_fail( pilotFile != NULL );
740 pilotFile->readMetadata = FALSE;
743 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
744 if( retVal != MGU_SUCCESS ) {
745 pilotFile->retVal = retVal;
746 return pilotFile->retVal;
749 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
754 // fprintf( stderr, "error reading '%s'\n", pilotFile->path );
755 pilotFile->retVal = MGU_ERROR_READ;
756 return pilotFile->retVal;
759 pilotFile->readMetadata = TRUE;
760 pilotFile->retVal = MGU_SUCCESS;
761 return pilotFile->retVal;
765 * Setup labels and indexes from metadata.
766 * Return: TRUE is setup successfully.
768 gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
769 gboolean retVal = FALSE;
770 struct AddressAppInfo *ai;
773 g_return_if_fail( pilotFile != NULL );
776 node = pilotFile->labelInd;
779 node = g_slist_next( node );
781 pilotFile->labelInd = NULL;
783 if( pilotFile->readMetadata ) {
784 ai = & pilotFile->addrInfo;
785 node = pilotFile->customLabels;
787 gchar *lbl, *labelName;
792 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
793 labelName = ai->labels[i];
794 if( g_strcasecmp( labelName, lbl ) == 0 ) {
799 pilotFile->labelInd = g_slist_append( pilotFile->labelInd, GINT_TO_POINTER(ind) );
800 node = g_slist_next( node );
808 * Load list with character strings of label names.
810 GSList *jpilot_load_label( JPilotFile *pilotFile, GSList *labelList ) {
812 g_return_if_fail( pilotFile != NULL );
813 if( pilotFile->readMetadata ) {
814 struct AddressAppInfo *ai = & pilotFile->addrInfo;
815 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
816 gchar *labelName = ai->labels[i];
818 labelList = g_slist_append( labelList, g_strdup( labelName ) );
821 labelList = g_slist_append( labelList, g_strdup( "" ) );
829 * Return category name for specified category ID.
830 * Enter: Category ID.
831 * Return: Name, or empty string if not invalid ID. Name should be g_free() when done.
833 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
834 gchar *catName = NULL;
835 g_return_if_fail( pilotFile != NULL );
836 if( pilotFile->readMetadata ) {
837 struct AddressAppInfo *ai = & pilotFile->addrInfo;
838 struct CategoryAppInfo *cat = & ai->category;
839 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
842 catName = g_strdup( cat->name[catID] );
845 if( ! catName ) catName = g_strdup( "" );
850 * Load list with character strings of phone label names.
852 GSList *jpilot_load_phone_label( JPilotFile *pilotFile, GSList *labelList ) {
854 g_return_if_fail( pilotFile != NULL );
855 if( pilotFile->readMetadata ) {
856 struct AddressAppInfo *ai = & pilotFile->addrInfo;
857 for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
858 gchar *labelName = ai->phoneLabels[i];
860 labelList = g_slist_append( labelList, g_strdup( labelName ) );
863 labelList = g_slist_append( labelList, g_strdup( "" ) );
871 * Load list with character strings of label names. Only none blank names
874 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
876 g_return_if_fail( pilotFile != NULL );
878 if( pilotFile->readMetadata ) {
879 struct AddressAppInfo *ai = & pilotFile->addrInfo;
880 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
881 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
883 g_strchomp( labelName );
884 g_strchug( labelName );
885 if( *labelName != '\0' ) {
886 labelList = g_list_append( labelList, g_strdup( labelName ) );
895 * Load list with character strings of category names.
897 GSList *jpilot_get_category_list( JPilotFile *pilotFile ) {
898 GSList *catList = NULL;
900 g_return_if_fail( pilotFile != NULL );
901 if( pilotFile->readMetadata ) {
902 struct AddressAppInfo *ai = & pilotFile->addrInfo;
903 struct CategoryAppInfo *cat = & ai->category;
904 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
905 gchar *catName = cat->name[i];
907 catList = g_slist_append( catList, g_strdup( catName ) );
910 catList = g_slist_append( catList, g_strdup( "" ) );
918 * Free category address list.
920 void jpilot_free_address_list( JPilotFile *pilotFile ) {
922 g_return_if_fail( pilotFile != NULL );
924 addrList = pilotFile->catAddrList;
926 JPilotCategory *jpcat = addrList->data;
927 mgu_free_address( jpcat->category );
928 mgu_free_address_list( jpcat->addressList );
929 jpcat->category = NULL;
930 jpcat->addressList = NULL;
934 addrList->data = NULL;
935 addrList = g_list_next( addrList );
937 pilotFile->catAddrList = NULL;
941 * Move data from address list and group into category address list.
943 void jpilot_load_category_items( JPilotFile *pilotFile ) {
944 JPilotCategory *jpcat[ 1 + JPILOT_NUM_CATEG ];
945 struct AddressAppInfo *ai = & pilotFile->addrInfo;
946 struct CategoryAppInfo *cat = & ai->category;
947 AddressItem *itemCat;
951 // Create array for data by category
952 for( i = 0; i < 1 + JPILOT_NUM_CATEG; i++ ) {
953 itemCat = mgu_create_address_item( ADDR_CATEGORY );
954 itemCat->categoryID = i;
955 jpcat[i] = g_new( JPilotCategory, 1 );
956 jpcat[i]->category = itemCat;
957 jpcat[i]->addressList = NULL;
959 if( i < JPILOT_NUM_CATEG ) {
960 gchar *catName = cat->name[i];
961 if( catName && strlen( catName ) > 0 ) {
962 itemCat->name = g_strdup( catName );
967 // Process address list moving data to category
968 addrList = pilotFile->addressCache->addressList;
972 item = addrList->data;
973 i = item->categoryID;
974 if( i < 0 || i > JPILOT_NUM_CATEG ) i = JPILOT_NUM_CATEG; // Position at end of array
976 // Add item to category list
977 addrLink = jpcat[i]->addressList;
978 addrLink = g_list_append( addrLink, item );
979 jpcat[i]->addressList = addrLink;
982 // Clear from cache list
983 addrList->data = NULL;
984 addrList = g_list_next( addrList );
987 // Remove entries from address cache
988 mgu_clear_cache_null( pilotFile->addressCache );
991 printf( "dump jpcArray[]...\n" );
992 for( i = 0; i < 1 + JPILOT_NUM_CATEG; i++ ) {
993 jpilot_print_category( jpcat[i], stdout );
997 // Free up existing category address list
998 jpilot_free_address_list( pilotFile );
1000 // Move categories from array to category address list
1002 for( i = 0; i < 1 + JPILOT_NUM_CATEG; i++ ) {
1003 if( jpcat[i]->count > 0 ) {
1004 itemCat = jpcat[i]->category;
1005 if( ! itemCat->name ) {
1006 // Create a category name
1007 itemCat->name = g_strdup_printf( "? %d", itemCat->categoryID );
1010 addrList = g_list_append( addrList, jpcat[i] );
1013 pilotFile->catAddrList = addrList;
1015 // jpilot_print_category_list( pilotFile->catAddrList, stdout );
1020 * Build linked list of address items for each category.
1022 GList *jpilot_build_category_list( JPilotFile *pilotFile ) {
1023 GList *catList = NULL;
1025 node = pilotFile->catAddrList;
1027 JPilotCategory *jpcat = node->data;
1028 AddressItem *catItem = jpcat->category;
1030 catItem = jpcat->category;
1031 if( jpcat->count > 0 || catItem->name ) {
1032 AddressItem *itemNew = mgu_copy_address_item( catItem );
1034 catList = g_list_append( catList, itemNew );
1037 node = g_list_next( node );
1039 mgu_free_address_list( pilotFile->categoryList );
1040 pilotFile->categoryList = catList;
1041 // mgu_print_address_list( catList, stdout );
1045 // ============================================================================================
1047 * Read file into list. Main entry point
1048 * Return: TRUE if file read successfully.
1050 // ============================================================================================
1051 gint jpilot_read_data( JPilotFile *pilotFile ) {
1052 g_return_if_fail( pilotFile != NULL );
1053 pilotFile->retVal = MGU_SUCCESS;
1054 if( mgu_check_file( pilotFile->addressCache, pilotFile->path ) ) {
1055 mgu_clear_cache( pilotFile->addressCache );
1056 jpilot_read_metadata( pilotFile );
1057 if( pilotFile->retVal == MGU_SUCCESS ) {
1058 jpilot_setup_labels( pilotFile );
1059 pilotFile->retVal = jpilot_read_cache( pilotFile );
1060 if( pilotFile->retVal == MGU_SUCCESS ) {
1061 mgu_mark_cache( pilotFile->addressCache, pilotFile->path );
1062 pilotFile->addressCache->modified = FALSE;
1063 pilotFile->addressCache->dataRead = TRUE;
1065 jpilot_load_category_items( pilotFile );
1066 jpilot_build_category_list( pilotFile );
1069 return pilotFile->retVal;
1073 * Return linked list of address items for loaded category names.
1075 GList *jpilot_get_category_items( JPilotFile *pilotFile ) {
1076 g_return_if_fail( pilotFile != NULL );
1077 return pilotFile->categoryList;
1081 * Return address list for specified category.
1083 GList *jpilot_get_address_list_cat( JPilotFile *pilotFile, const gint catID ) {
1084 GList *addrList = NULL, *node;
1085 g_return_if_fail( pilotFile != NULL );
1087 node = pilotFile->catAddrList;
1089 JPilotCategory *jpcat = node->data;
1090 AddressItem *catItem = jpcat->category;
1091 if( catItem->categoryID == catID ) {
1092 addrList = jpcat->addressList;
1095 node = g_list_next( node );
1101 * Return linked list of address items.
1103 GList *jpilot_get_address_list( JPilotFile *pilotFile ) {
1104 g_return_if_fail( pilotFile != NULL );
1105 return pilotFile->addressCache->addressList;
1109 * Check label list for specified label.
1111 gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1114 if( lblCheck == NULL ) return -1;
1115 if( strlen( lblCheck ) < 1 ) return -1;
1116 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1117 lblName = ai->labels[i];
1119 if( strlen( lblName ) ) {
1120 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1128 * Validate that all parameters specified.
1129 * Return: TRUE if data is good.
1131 gboolean jpilot_validate( const JPilotFile *pilotFile ) {
1133 g_return_if_fail( pilotFile != NULL );
1136 if( pilotFile->path ) {
1137 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1142 if( pilotFile->name ) {
1143 if( strlen( pilotFile->name ) < 1 ) retVal = FALSE;
1151 #define WORK_BUFLEN 1024
1154 * Attempt to find a valid JPilot file.
1155 * Return: Filename, or home directory if not found, or empty string if
1156 * no home. Filename should be g_free() when done.
1158 gchar *jpilot_find_pilotdb( void ) {
1160 gchar str[ WORK_BUFLEN ];
1164 homedir = g_get_home_dir();
1165 if( ! homedir ) return g_strdup( "" );
1167 strcpy( str, homedir );
1168 len = strlen( str );
1170 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1171 str[ len ] = G_DIR_SEPARATOR;
1172 str[ ++len ] = '\0';
1175 strcat( str, JPILOT_DBHOME_DIR );
1176 strcat( str, G_DIR_SEPARATOR_S );
1177 strcat( str, JPILOT_DBHOME_FILE );
1179 // Attempt to open
\e
1180 if( ( fp = fopen( str, "r" ) ) != NULL ) {
1184 // Truncate filename
1187 return g_strdup( str );
1191 * Attempt to read file, testing for valid JPilot format.
1192 * Return: TRUE if file appears to be valid format.
1194 gint jpilot_test_read_file( const gchar *fileSpec ) {
1195 JPilotFile *pilotFile;
1198 pilotFile = jpilot_create_path( fileSpec );
1199 retVal = jpilot_read_metadata( pilotFile );
1200 jpilot_free( pilotFile );
1204 retVal = MGU_NO_FILE;
1210 * Check whether label is in custom labels.
1211 * Return: TRUE if found.
1213 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1216 g_return_if_fail( pilotFile != NULL );
1220 node = pilotFile->customLabels;
1222 if( g_strcasecmp( labelName, node->data ) == 0 ) {
1226 node = g_slist_next( node );
1233 * Test whether pilot link library installed.
1234 * Return: TRUE if library available.
1236 gboolean jpilot_test_pilot_lib() {
1239 handle = dlopen( PILOT_LINK_LIB_NAME, RTLD_LAZY );
1244 // Test for symbols we need
1245 fun = dlsym( handle, "unpack_Address" );
1251 fun = dlsym( handle, "unpack_AddressAppInfo" );
1260 #endif /* USE_JPILOT */