change comment format
[claws.git] / src / jpilot.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 Match Grun
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #ifdef USE_JPILOT
31
32 #include <glib.h>
33 #include <time.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <dlfcn.h>
39
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>
44 #else
45 #  include <pi-args.h>
46 #  include <pi-appinfo.h>
47 #  include <pi-address.h>
48 #endif
49
50 #include "mgutils.h"
51 #include "addritem.h"
52 #include "addrcache.h"
53 #include "jpilot.h"
54
55 #define JPILOT_DBHOME_DIR   ".jpilot"
56 #define JPILOT_DBHOME_FILE  "AddressDB.pdb"
57 #define PILOT_LINK_LIB_NAME "libpisock.so"
58
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 */
65
66 /* Shamelessly copied from JPilot (libplugin.h) */
67 typedef struct {
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];
82 } RawDBHeader;
83
84 /* Shamelessly copied from JPilot (libplugin.h) */
85 typedef struct {
86         char db_name[32];
87         unsigned int flags;
88         unsigned int version;
89         time_t creation_time;
90         time_t modification_time;
91         time_t backup_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;
100 } DBHeader;
101
102 /* Shamelessly copied from JPilot (libplugin.h) */
103 typedef struct {
104         unsigned char Offset[4];  /*4 bytes offset from BOF to record */
105         unsigned char attrib;
106         unsigned char unique_ID[3];
107 } record_header;
108
109 /* Shamelessly copied from JPilot (libplugin.h) */
110 typedef struct mem_rec_header_s {
111         unsigned int rec_num;
112         unsigned int offset;
113         unsigned int unique_id;
114         unsigned char attrib;
115         struct mem_rec_header_s *next;
116 } mem_rec_header;
117
118 /* Shamelessly copied from JPilot (libplugin.h) */
119 #define SPENT_PC_RECORD_BIT     256
120
121 typedef enum {
122         PALM_REC = 100L,
123         MODIFIED_PALM_REC = 101L,
124         DELETED_PALM_REC = 102L,
125         NEW_PC_REC = 103L,
126         DELETED_PC_REC = SPENT_PC_RECORD_BIT + 104L,
127         DELETED_DELETED_PALM_REC = SPENT_PC_RECORD_BIT + 105L
128 } PCRecType;
129
130 /* Shamelessly copied from JPilot (libplugin.h) */
131 typedef struct {
132         PCRecType rt;
133         unsigned int unique_id;
134         unsigned char attrib;
135         void *buf;
136         int size;
137 } buf_rec;
138
139 /*
140 * Create new pilot file object.
141 */
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;
154         return pilotFile;
155 }
156
157 /*
158 * Create new pilot file object for specified file.
159 */
160 JPilotFile *jpilot_create_path( const gchar *path ) {
161         JPilotFile *pilotFile;
162         pilotFile = jpilot_create();
163         jpilot_set_file( pilotFile, path );
164         return pilotFile;
165 }
166
167 /*
168 * Properties...
169 */
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 );
173 }
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 );
179 }
180 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
181         g_return_if_fail( pilotFile != NULL );
182         pilotFile->accessFlag = value;
183 }
184
185 gint jpilot_get_status( JPilotFile *pilotFile ) {
186         g_return_val_if_fail( pilotFile != NULL, -1 );
187         return pilotFile->retVal;
188 }
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 );
192 }
193 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
194         g_return_val_if_fail( pilotFile != NULL, NULL );
195         return pilotFile->name;
196 }
197
198 /*
199 * Test whether file was modified since last access.
200 * Return: TRUE if file was modified.
201 */
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 );
205 }
206 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
207         g_return_val_if_fail( pilotFile != NULL, FALSE );
208         return pilotFile->accessFlag;
209 }
210
211 /*
212 * Test whether file was read.
213 * Return: TRUE if file was read.
214 */
215 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
216         g_return_val_if_fail( pilotFile != NULL, FALSE );
217         return pilotFile->addressCache->dataRead;
218 }
219
220 /*
221 * Free up custom label list.
222 */
223 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
224         GList *node;
225
226         g_return_if_fail( pilotFile != NULL );
227
228         /* Release custom labels */
229         mgu_free_dlist( pilotFile->customLabels );
230         pilotFile->customLabels = NULL;
231
232         /* Release indexes */
233         node = pilotFile->labelInd;
234         while( node ) {
235                 node->data = NULL;
236                 node = g_list_next( node );
237         }
238         g_list_free( pilotFile->labelInd );
239         pilotFile->labelInd = NULL;
240
241         /* Force a fresh read */
242         addrcache_refresh( pilotFile->addressCache );
243 }
244
245 /*
246 * Append a custom label, representing an E-Mail address field to the
247 * custom label list.
248 */
249 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
250         g_return_if_fail( pilotFile != NULL );
251
252         if( labelName ) {
253                 gchar *labelCopy = g_strdup( labelName );
254                 g_strstrip( labelCopy );
255                 if( *labelCopy == '\0' ) {
256                         g_free( labelCopy );
257                 }
258                 else {
259                         pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
260                         /* Force a fresh read */
261                         addrcache_refresh( pilotFile->addressCache );
262                 }
263         }
264 }
265
266 /*
267 * Get list of custom labels.
268 * Return: List of labels. Must use g_free() when done.
269 */
270 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
271         GList *retVal = NULL;
272         GList *node;
273
274         g_return_val_if_fail( pilotFile != NULL, NULL );
275
276         node = pilotFile->customLabels;
277         while( node ) {
278                 retVal = g_list_append( retVal, g_strdup( node->data ) );
279                 node = g_list_next( node );
280         }
281         return retVal;
282 }
283
284 /*
285 * Free up pilot file object by releasing internal memory.
286 */
287 void jpilot_free( JPilotFile *pilotFile ) {
288         g_return_if_fail( pilotFile != NULL );
289
290         /* Free internal stuff */
291         g_free( pilotFile->path );
292
293         /* Release custom labels */
294         jpilot_clear_custom_labels( pilotFile );
295
296         /* Clear cache */
297         addrcache_clear( pilotFile->addressCache );
298         addrcache_free( pilotFile->addressCache );
299         pilotFile->addressCache = NULL;
300         pilotFile->readMetadata = FALSE;
301         pilotFile->accessFlag = FALSE;
302
303         /* Now release file object */
304         g_free( pilotFile );
305 }
306
307 /*
308 * Refresh internal variables to force a file read.
309 */
310 void jpilot_force_refresh( JPilotFile *pilotFile ) {
311         addrcache_refresh( pilotFile->addressCache );
312 }
313
314 /*
315 * Print object to specified stream.
316 */
317 void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
318         GList *node;
319
320         g_return_if_fail( pilotFile != NULL );
321
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 );
326
327         node = pilotFile->customLabels;
328         while( node ) {
329                 fprintf( stream, "  c label: %s\n", (gchar *)node->data );
330                 node = g_list_next( node );
331         }
332
333         node = pilotFile->labelInd;
334         while( node ) {
335                 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
336                 node = g_list_next( node );
337         }
338
339         addrcache_print( pilotFile->addressCache, stream );
340         addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
341 }
342
343 /*
344 * Print summary of object to specified stream.
345 */
346 void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
347         GList *node;
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 );
353
354         node = pilotFile->customLabels;
355         while( node ) {
356                 fprintf( stream, "  c label: %s\n", (gchar *)node->data );
357                 node = g_list_next( node );
358         }
359
360         node = pilotFile->labelInd;
361         while( node ) {
362                 fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
363                 node = g_list_next( node );
364         }
365         addrcache_print( pilotFile->addressCache, stream );
366 }
367
368 /* Shamelessly copied from JPilot (libplugin.c) */
369 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
370 unsigned int i, n;
371         n=0;
372         for (i=0;i<num_bytes;i++) {
373                 n = n*256+bytes[i];
374         }
375         return n;
376 }
377
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)
382
383 time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
384    return (time_t)(raw_time - PILOT_TIME_DELTA);
385 }
386
387 /* Shamelessly copied from JPilot (libplugin.c) */
388 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
389         unsigned long temp;
390
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);
405         dbh->type[4] = '\0';
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);
412         return 0;
413 }
414
415 /* Shamelessly copied from JPilot (libplugin.c) */
416 /*returns 1 if found */
417 /*        0 if eof */
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 )
420 {
421         mem_rec_header *temp_mem_rh;
422         unsigned char found = 0;
423         unsigned long found_at;
424
425         found_at=0xFFFFFF;
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; */
431                 }
432                 if ((temp_mem_rh->offset == fpos)) {
433                         found = 1;
434                         *attrib = temp_mem_rh->attrib;
435                         *unique_id = temp_mem_rh->unique_id;
436                 }
437         }
438         *next_offset = found_at;
439         return found;
440 }
441
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) {
446                 next_h=h->next;
447                 free(h);
448         }
449         *mem_rh = NULL;
450 }
451
452 /* Shamelessly copied from JPilot (libplugin.c) */
453 static int jpilot_free_db_list( GList **br_list ) {
454         GList *temp_list, *first;
455         buf_rec *br;
456
457         /* Go to first entry in the list */
458         first=NULL;
459         for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
460                 first = temp_list;
461         }
462         for (temp_list = first; temp_list; temp_list = temp_list->next) {
463                 if (temp_list->data) {
464                         br=temp_list->data;
465                         if (br->buf) {
466                                 free(br->buf);
467                                 temp_list->data=NULL;
468                         }
469                         free(br);
470                 }
471         }
472         g_list_free(*br_list);
473         *br_list=NULL;
474         return 0;
475 }
476
477 /* Shamelessly copied from JPilot (libplugin.c)
478    Read file size. */
479 static int jpilot_get_info_size( FILE *in, int *size ) {
480         RawDBHeader rdbh;
481         DBHeader dbh;
482         unsigned int offset;
483         record_header rh;
484
485         fseek(in, 0, SEEK_SET);
486         fread(&rdbh, sizeof(RawDBHeader), 1, in);
487         if (feof(in)) {
488                 /* fprintf( stderr, "error reading file in 'jpilot_get_info_size'\n" ); */
489                 return MGU_EOF;
490         }
491
492         raw_header_to_header(&rdbh, &dbh);
493         if (dbh.app_info_offset==0) {
494                 *size=0;
495                 return MGU_SUCCESS;
496         }
497         if (dbh.sort_info_offset!=0) {
498                 *size = dbh.sort_info_offset - dbh.app_info_offset;
499                 return MGU_SUCCESS;
500         }
501         if (dbh.number_of_records==0) {
502                 fseek(in, 0, SEEK_END);
503                 *size=ftell(in) - dbh.app_info_offset;
504                 return MGU_SUCCESS;
505         }
506
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;
510
511         return MGU_SUCCESS;
512 }
513
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 ) {
517         FILE *in;
518         int num;
519         unsigned int rec_size;
520         RawDBHeader rdbh;
521         DBHeader dbh;
522
523         if( ( !buf_size ) || ( ! buf ) ) {
524                 return MGU_BAD_ARGS;
525         }
526
527         *buf = NULL;
528         *buf_size=0;
529
530         if( pilotFile->path ) {
531                 in = fopen( pilotFile->path, "r" );
532                 if( !in ) {
533                         /* fprintf( stderr, "can't open %s\n", pilotFile->path ); */
534                         return MGU_OPEN_FILE;
535                 }
536         }
537         else {
538                 /* fprintf( stderr, "file not specified\n" ); */
539                 return MGU_NO_FILE;
540         }
541
542         num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
543         if( num != 1 ) {
544                 if( ferror(in) ) {
545                         /* fprintf( stderr, "error reading %s\n", pilotFile->path ); */
546                         fclose(in);
547                         return MGU_ERROR_READ;
548                 }
549         }
550         if (feof(in)) {
551                 fclose(in);
552                 return MGU_EOF;
553         }
554
555         /* Convert header into something recognizable */
556         raw_header_to_header(&rdbh, &dbh);
557
558         num = jpilot_get_info_size(in, &rec_size);
559         if (num) {
560                 fclose(in);
561                 return MGU_ERROR_READ;
562         }
563
564         fseek(in, dbh.app_info_offset, SEEK_SET);
565         *buf = ( char * ) malloc(rec_size);
566         if (!(*buf)) {
567                 /* fprintf( stderr, "jpilot_get_file_info(): Out of memory\n" ); */
568                 fclose(in);
569                 return MGU_OO_MEMORY;
570         }
571         num = fread(*buf, rec_size, 1, in);
572         if (num != 1) {
573                 if (ferror(in)) {
574                         fclose(in);
575                         free(*buf);
576                         /* fprintf( stderr, "Error reading %s\n", pilotFile->path ); */
577                         return MGU_ERROR_READ;
578                 }
579         }
580         fclose(in);
581
582         *buf_size = rec_size;
583
584         return MGU_SUCCESS;
585 }
586
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 ) {
592         FILE *in;
593         gchar *buf;
594         gint num_records, recs_returned, i, num;
595         guint offset, prev_offset, next_offset, rec_size;
596         gint out_of_order;
597         glong fpos;  /*file position indicator */
598         guchar attrib;
599         guint unique_id;
600         gint cat_id = 0;
601         mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
602         record_header rh;
603         RawDBHeader rdbh;
604         DBHeader dbh;
605         gint retVal;
606         struct Address addr;
607         struct AddressAppInfo *ai;
608         gchar **addrEnt;
609         gint inum, k;
610         gchar fullName[ FULLNAME_BUFSIZE ];
611         gchar bufEMail[ EMAIL_BUFSIZE ];
612         gchar* extID;
613         ItemPerson *person;
614         ItemEMail *email;
615         gint *indPhoneLbl;
616         gchar *labelEntry;
617         GList *node;
618         ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
619
620         retVal = MGU_SUCCESS;
621         mem_rh = last_mem_rh = NULL;
622         recs_returned = 0;
623
624         /* Pointer to address metadata. */
625         ai = & pilotFile->addrInfo;
626
627         /* Open file for read */
628         if( pilotFile->path ) {
629                 in = fopen( pilotFile->path, "r" );
630                 if( !in ) {
631                         /* fprintf( stderr, "can't open %s\n", pilotFile->path ); */
632                         return MGU_OPEN_FILE;
633                 }
634         }
635         else {
636                 /* fprintf( stderr, "file not specified\n" ); */
637                 return MGU_NO_FILE;
638         }
639
640         /* Read the database header */
641         num = fread(&rdbh, sizeof(RawDBHeader), 1, in);
642         if (num != 1) {
643                 if (ferror(in)) {
644                         /* fprintf( stderr, "error reading '%s'\n", pilotFile->path ); */
645                         fclose(in);
646                         return MGU_ERROR_READ;
647                 }
648                 if (feof(in)) {
649                         return MGU_EOF;
650                 }
651         }
652
653         raw_header_to_header(&rdbh, &dbh);
654
655         /*Read each record entry header */
656         num_records = dbh.number_of_records;
657         out_of_order = 0;
658         prev_offset = 0;
659
660         for (i=1; i<num_records+1; i++) {
661                 num = fread( &rh, sizeof( record_header ), 1, in );
662                 if (num != 1) {
663                         if (ferror(in)) {
664                                 /* fprintf( stderr, "error reading '%s'\n", pilotFile->path ); */
665                                 retVal = MGU_ERROR_READ;
666                                 break;
667                         }
668                         if (feof(in)) {
669                                 return MGU_EOF;
670                         }
671                 }
672
673                 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
674                 if (offset < prev_offset) {
675                         out_of_order = 1;
676                 }
677                 prev_offset = offset;
678
679                 temp_mem_rh = (mem_rec_header *)malloc(sizeof(mem_rec_header));
680                 if (!temp_mem_rh) {
681                         /* fprintf( stderr, "jpilot_read_db_file(): Out of memory 1\n" ); */
682                         retVal = MGU_OO_MEMORY;
683                         break;
684                 }
685
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];
691
692                 if (mem_rh == NULL) {
693                         mem_rh = temp_mem_rh;
694                         last_mem_rh = temp_mem_rh;
695                 }
696                 else {
697                         last_mem_rh->next = temp_mem_rh;
698                         last_mem_rh = temp_mem_rh;
699                 }
700
701         }       /* for( ;; ) */
702
703         temp_mem_rh = mem_rh;
704
705         if (num_records) {
706                 if (out_of_order) {
707                         find_next_offset(mem_rh, 0, &next_offset, &attrib, &unique_id);
708                 }
709                 else {
710                         if (mem_rh) {
711                                 next_offset = mem_rh->offset;
712                                 attrib = mem_rh->attrib;
713                                 unique_id = mem_rh->unique_id;
714                         }
715                 }
716                 fseek(in, next_offset, SEEK_SET);
717
718                 /* Build array of pointers to categories; */
719                 i = 0;
720                 node = addrcache_get_list_folder( pilotFile->addressCache );
721                 while( node ) {
722                         if( i < JPILOT_NUM_CATEG ) {
723                                 folderInd[i] = node->data;
724                         }
725                         node = g_list_next( node );
726                         i++;
727                 }
728
729                 /* Now go load all records */           
730                 while(!feof(in)) {
731                         /* struct CategoryAppInfo *cat = &ai->category; */
732
733                         fpos = ftell(in);
734                         if (out_of_order) {
735                                 find_next_offset(mem_rh, fpos, &next_offset, &attrib, &unique_id);
736                         }
737                         else {
738                                 next_offset = 0xFFFFFF;
739                                 if (temp_mem_rh) {
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;
746                                         }
747                                 }
748                         }
749                         rec_size = next_offset - fpos;
750
751                         buf = ( char * ) malloc(rec_size);
752                         if (!buf) break;
753                         num = fread(buf, rec_size, 1, in);
754                         if ((num != 1)) {
755                                 if (ferror(in)) {
756                                         /* fprintf( stderr, "Error reading %s 5\n", pilotFile ); */
757                                         free(buf);
758                                         retVal = MGU_ERROR_READ;
759                                         break;
760                                 }
761                         }
762
763                         /* Retrieve address */
764                         inum = unpack_Address( & addr, buf, rec_size );
765                         if( inum > 0 ) {
766                                 addrEnt = addr.entry;
767
768                                 *fullName = *bufEMail = '\0';
769                                 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
770                                         strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
771                                 }
772
773                                 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
774                                         strcat( fullName, " " );
775                                         strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
776                                 }
777                                 g_strchug( fullName );
778                                 g_strchomp( fullName );
779
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 );
785
786                                 extID = g_strdup_printf( "%d", unique_id );
787                                 addritem_person_set_external_id( person, extID );
788                                 g_free( extID );
789                                 extID = NULL;
790
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++ ) {
794                                         gint ind;
795                                         ind = indPhoneLbl[k];
796                                         /*
797                                         fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
798                                                 ai->phoneLabels[ind], addrEnt[3+k] );
799                                         */
800                                         if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
801                                                 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
802                                                 if( labelEntry ) {
803                                                         strcpy( bufEMail, labelEntry );
804                                                         g_strchug( bufEMail );
805                                                         g_strchomp( bufEMail );
806
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 );
812                                                 }
813                                         }
814                                 }
815
816                                 /* Add entry for each custom label */
817                                 node = pilotFile->labelInd;
818                                 while( node ) {
819                                         gint ind;
820                                         ind = GPOINTER_TO_INT( node->data );
821                                         if( ind > -1 ) {
822                                                 /*
823                                                 fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
824                                                         addrEnt[ind] );
825                                                 */
826                                                 labelEntry = addrEnt[ind];
827                                                 if( labelEntry ) {
828                                                         strcpy( bufEMail, labelEntry );
829                                                         g_strchug( bufEMail );
830                                                         g_strchomp( bufEMail );
831
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 );
838                                                 }
839
840                                         }
841
842                                         node = g_list_next( node );
843                                 }
844
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 );
850                                         }
851                                         else {
852                                                 /* Add to root folder */
853                                                 addrcache_add_person( pilotFile->addressCache, person );
854                                         }
855                                 }
856                                 else {
857                                         addritem_free_item_person( person );
858                                         person = NULL;
859                                 }
860
861                         }
862                         recs_returned++;
863                 }
864         }
865         fclose(in);
866         free_mem_rec_header(&mem_rh);
867         return retVal;
868 }
869
870 /*
871 * Read metadata from file.
872 */
873 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
874         gint retVal;
875         unsigned int rec_size;
876         unsigned char *buf;
877         int num;
878
879         g_return_val_if_fail( pilotFile != NULL, -1 );
880
881         pilotFile->readMetadata = FALSE;
882         addrcache_clear( pilotFile->addressCache );
883
884         /* Read file info */
885         retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
886         if( retVal != MGU_SUCCESS ) {
887                 pilotFile->retVal = retVal;
888                 return pilotFile->retVal;
889         }
890
891         num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
892         if( buf ) {
893                 free(buf);
894         }
895         if( num <= 0 ) {
896                 /* fprintf( stderr, "error reading '%s'\n", pilotFile->path ); */
897                 pilotFile->retVal = MGU_ERROR_READ;
898                 return pilotFile->retVal;
899         }
900
901         pilotFile->readMetadata = TRUE;
902         pilotFile->retVal = MGU_SUCCESS;
903         return pilotFile->retVal;
904 }
905
906 /*
907 * Setup labels and indexes from metadata.
908 * Return: TRUE is setup successfully.
909 */
910 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
911         gboolean retVal = FALSE;
912         struct AddressAppInfo *ai;
913         GList *node;
914
915         g_return_val_if_fail( pilotFile != NULL, -1 );
916
917         /* Release indexes */
918         node = pilotFile->labelInd;
919         while( node ) {
920                 node->data = NULL;
921                 node = g_list_next( node );
922         }
923         pilotFile->labelInd = NULL;
924
925         if( pilotFile->readMetadata ) {
926                 ai = & pilotFile->addrInfo;
927                 node = pilotFile->customLabels;
928                 while( node ) {
929                         gchar *lbl = node->data;
930                         gint ind = -1;
931                         gint i;
932                         for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
933                                 gchar *labelName = ai->labels[i];
934                                 if( g_strcasecmp( labelName, lbl ) == 0 ) {
935                                         ind = i;
936                                         break;
937                                 }
938                         }
939                         pilotFile->labelInd = g_list_append( pilotFile->labelInd, GINT_TO_POINTER(ind) );
940                         node = g_list_next( node );
941                 }
942                 retVal = TRUE;
943         }
944         return retVal;
945 }
946
947 /*
948 * Load list with character strings of label names.
949 */
950 GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
951         int i;
952
953         g_return_val_if_fail( pilotFile != NULL, NULL );
954
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];
959                         if( labelName ) {
960                                 labelList = g_list_append( labelList, g_strdup( labelName ) );
961                         }
962                         else {
963                                 labelList = g_list_append( labelList, g_strdup( "" ) );
964                         }
965                 }
966         }
967         return labelList;
968 }
969
970 /*
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.
974 */
975 gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
976         gchar *catName = NULL;
977
978         g_return_val_if_fail( pilotFile != NULL, NULL );
979
980         if( pilotFile->readMetadata ) {
981                 struct AddressAppInfo *ai = & pilotFile->addrInfo;
982                 struct CategoryAppInfo *cat = & ai->category;
983                 if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
984                 }
985                 else {
986                         catName = g_strdup( cat->name[catID] );
987                 }
988         }
989         if( ! catName ) catName = g_strdup( "" );
990         return catName;
991 }
992
993 /*
994 * Load list with character strings of phone label names.
995 */
996 GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
997         gint i;
998
999         g_return_val_if_fail( pilotFile != NULL, NULL );
1000
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];
1005                         if( labelName ) {
1006                                 labelList = g_list_append( labelList, g_strdup( labelName ) );
1007                         }
1008                         else {
1009                                 labelList = g_list_append( labelList, g_strdup( "" ) );
1010                         }
1011                 }
1012         }
1013         return labelList;
1014 }
1015
1016 /*
1017 * Load list with character strings of label names. Only none blank names
1018 * are loaded.
1019 */
1020 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1021         gint i;
1022
1023         g_return_val_if_fail( pilotFile != NULL, NULL );
1024
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];
1029                         if( labelName ) {
1030                                 g_strchomp( labelName );
1031                                 g_strchug( labelName );
1032                                 if( *labelName != '\0' ) {
1033                                         labelList = g_list_append( labelList, g_strdup( labelName ) );
1034                                 }
1035                         }
1036                 }
1037         }
1038         return labelList;
1039 }
1040
1041 /*
1042 * Load list with character strings of category names.
1043 */
1044 GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
1045         GList *catList = NULL;
1046         gint i;
1047
1048         g_return_val_if_fail( pilotFile != NULL, NULL );
1049
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];
1055                         if( catName ) {
1056                                 catList = g_list_append( catList, g_strdup( catName ) );
1057                         }
1058                         else {
1059                                 catList = g_list_append( catList, g_strdup( "" ) );
1060                         }
1061                 }
1062         }
1063         return catList;
1064 }
1065
1066 /*
1067 * Build folder for each category.
1068 */
1069 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1070         struct AddressAppInfo *ai = & pilotFile->addrInfo;
1071         struct CategoryAppInfo *cat = & ai->category;
1072         gint i;
1073
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 );
1079         }
1080 }
1081
1082 /*
1083 * Remove empty folders (categories).
1084 */
1085 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1086         GList *listFolder;
1087         GList *remList;
1088         GList *node;
1089         gint i = 0;
1090
1091         listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1092         node = listFolder;
1093         remList = NULL;
1094         while( node ) {
1095                 ItemFolder *folder = node->data;
1096                 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1097                         if( folder->listPerson ) {
1098                                 /* Give name to folder */
1099                                 gchar name[20];
1100                                 sprintf( name, "? %d", i );
1101                                 addritem_folder_set_name( folder, name );
1102                         }
1103                         else {
1104                                 /* Mark for removal */
1105                                 remList = g_list_append( remList, folder );
1106                         }
1107                 }
1108                 node = g_list_next( node );
1109                 i++;
1110         }
1111         node = remList;
1112         while( node ) {
1113                 ItemFolder *folder = node->data;
1114                 addrcache_remove_folder( pilotFile->addressCache, folder );
1115                 node = g_list_next( node );
1116         }
1117         g_list_free( remList );
1118 }
1119
1120 /* ============================================================================================
1121
1122 * Read file into list. Main entry point
1123 * Return: TRUE if file read successfully.
1124
1125   ============================================================================================ */
1126 gint jpilot_read_data( JPilotFile *pilotFile ) {
1127         g_return_val_if_fail( pilotFile != NULL, -1 );
1128
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;
1143                         }
1144                 }
1145         }
1146         return pilotFile->retVal;
1147 }
1148
1149 /*
1150 * Return link list of persons.
1151 */
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 );
1155 }
1156
1157 /*
1158 * Return link list of folders. This is always NULL since there are
1159 * no folders in GnomeCard.
1160 * Return: NULL.
1161 */
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 );
1165 }
1166
1167 /*
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.
1172 */
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 );
1176 }
1177
1178 /*
1179 * Check label list for specified label.
1180 */
1181 gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
1182         gint i;
1183         gchar *lblName;
1184
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];
1189                 if( lblName ) {
1190                         if( strlen( lblName ) ) {
1191                                 if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
1192                         }
1193                 }
1194         }
1195         return -2;
1196 }
1197
1198 /*
1199 * Validate that all parameters specified.
1200 * Return: TRUE if data is good.
1201 */
1202 gboolean jpilot_validate( const JPilotFile *pilotFile ) {
1203         gboolean retVal;
1204
1205         g_return_val_if_fail( pilotFile != NULL, FALSE );
1206
1207         retVal = TRUE;
1208         if( pilotFile->path ) {
1209                 if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
1210         }
1211         else {
1212                 retVal = FALSE;
1213         }
1214         if( pilotFile->name ) {
1215                 if( strlen( pilotFile->name ) < 1 ) retVal = FALSE;
1216         }
1217         else {
1218                 retVal = FALSE;
1219         }
1220         return retVal;
1221 }
1222
1223 #define WORK_BUFLEN 1024
1224
1225 /*
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.
1229 */
1230 gchar *jpilot_find_pilotdb( void ) {
1231         gchar *homedir;
1232         gchar str[ WORK_BUFLEN ];
1233         gint len;
1234         FILE *fp;
1235
1236         homedir = g_get_home_dir();
1237         if( ! homedir ) return g_strdup( "" );
1238
1239         strcpy( str, homedir );
1240         len = strlen( str );
1241         if( len > 0 ) {
1242                 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1243                         str[ len ] = G_DIR_SEPARATOR;
1244                         str[ ++len ] = '\0';
1245                 }
1246         }
1247         strcat( str, JPILOT_DBHOME_DIR );
1248         strcat( str, G_DIR_SEPARATOR_S );
1249         strcat( str, JPILOT_DBHOME_FILE );
1250
1251         /* Attempt to open */
1252         if( ( fp = fopen( str, "r" ) ) != NULL ) {
1253                 fclose( fp );
1254         }
1255         else {
1256                 /* Truncate filename */
1257                 str[ len ] = '\0';
1258         }
1259         return g_strdup( str );
1260 }
1261
1262 /*
1263 * Attempt to read file, testing for valid JPilot format.
1264 * Return: TRUE if file appears to be valid format.
1265 */
1266 gint jpilot_test_read_file( const gchar *fileSpec ) {
1267         JPilotFile *pilotFile;
1268         gint retVal;
1269
1270         if( fileSpec ) {
1271                 pilotFile = jpilot_create_path( fileSpec );
1272                 retVal = jpilot_read_metadata( pilotFile );
1273                 jpilot_free( pilotFile );
1274                 pilotFile = NULL;
1275         }
1276         else {
1277                 retVal = MGU_NO_FILE;
1278         }
1279         return retVal;
1280 }
1281
1282 /*
1283 * Check whether label is in custom labels.
1284 * Return: TRUE if found.
1285 */
1286 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1287         gboolean retVal;
1288         GList *node;
1289
1290         g_return_val_if_fail( pilotFile != NULL, FALSE );
1291
1292         retVal = FALSE;
1293         if( labelName ) {
1294                 node = pilotFile->customLabels;
1295                 while( node ) {
1296                         if( g_strcasecmp( labelName, node->data ) == 0 ) {
1297                                 retVal = TRUE;
1298                                 break;
1299                         }
1300                         node = g_list_next( node );
1301                 }
1302         }
1303         return retVal;
1304 }
1305
1306 /*
1307 * Test whether pilot link library installed.
1308 * Return: TRUE if library available.
1309 */
1310 gboolean jpilot_test_pilot_lib( void ) {
1311         void *handle, *fun;
1312
1313         handle = dlopen( PILOT_LINK_LIB_NAME, RTLD_LAZY );
1314         if( ! handle ) {
1315                 return FALSE;
1316         }
1317
1318         /* Test for symbols we need */
1319         fun = dlsym( handle, "unpack_Address" );
1320         if( ! fun ) {
1321                 dlclose( handle );
1322                 return FALSE;
1323         }
1324
1325         fun = dlsym( handle, "unpack_AddressAppInfo" );
1326         if( ! fun ) {
1327                 dlclose( handle );
1328                 return FALSE;
1329         }
1330         dlclose( handle );
1331         return TRUE;
1332 }
1333
1334 #endif  /* USE_JPILOT */
1335
1336 /*
1337 * End of Source.
1338 */