2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2012 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
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.
28 #include "claws-features.h"
39 #include <netinet/in.h>
41 #ifdef HAVE_LIBPISOCK_PI_ARGS_H
42 # include <libpisock/pi-args.h>
43 # include <libpisock/pi-appinfo.h>
44 # include <libpisock/pi-address.h>
45 # include <libpisock/pi-version.h>
48 # include <pi-appinfo.h>
49 # include <pi-address.h>
50 # include <pi-version.h>
55 #include "addrcache.h"
58 #include "adbookbase.h"
61 #define JPILOT_DBHOME_DIR ".jpilot"
62 #define JPILOT_DBHOME_FILE "AddressDB.pdb"
63 #define PILOT_LINK_LIB_NAME "libpisock.so"
65 #define IND_LABEL_LASTNAME 0 /* Index of last name in address data */
66 #define IND_LABEL_FIRSTNAME 1 /* Index of first name in address data */
67 #define IND_PHONE_EMAIL 4 /* Index of E-Mail address in phone labels */
68 #define OFFSET_PHONE_LABEL 3 /* Offset to phone data in address data */
69 #define IND_CUSTOM_LABEL 14 /* Offset to custom label names */
70 #define NUM_CUSTOM_LABEL 4 /* Number of custom labels */
72 /* Shamelessly copied from JPilot (libplugin.h) */
74 unsigned char db_name[32];
75 unsigned char flags[2];
76 unsigned char version[2];
77 unsigned char creation_time[4];
78 unsigned char modification_time[4];
79 unsigned char backup_time[4];
80 unsigned char modification_number[4];
81 unsigned char app_info_offset[4];
82 unsigned char sort_info_offset[4];
83 unsigned char type[4];/*Database ID */
84 unsigned char creator_id[4];/*Application ID */
85 unsigned char unique_id_seed[4];
86 unsigned char next_record_list_id[4];
87 unsigned char number_of_records[2];
90 /* Shamelessly copied from JPilot (libplugin.h) */
96 time_t modification_time;
98 unsigned int modification_number;
99 unsigned int app_info_offset;
100 unsigned int sort_info_offset;
101 char type[5];/*Database ID */
102 char creator_id[5];/*Application ID */
103 char unique_id_seed[5];
104 unsigned int next_record_list_id;
105 unsigned int number_of_records;
108 /* Shamelessly copied from JPilot (libplugin.h) */
110 unsigned char Offset[4]; /*4 bytes offset from BOF to record */
111 unsigned char attrib;
112 unsigned char unique_ID[3];
115 /* Shamelessly copied from JPilot (libplugin.h) */
116 typedef struct mem_rec_header_s {
117 unsigned int rec_num;
119 unsigned int unique_id;
120 unsigned char attrib;
121 struct mem_rec_header_s *next;
124 /* Shamelessly copied from JPilot (libplugin.h) */
125 #define SPENT_PC_RECORD_BIT 256
129 MODIFIED_PALM_REC = 101L,
130 DELETED_PALM_REC = 102L,
132 DELETED_PC_REC = SPENT_PC_RECORD_BIT + 104L,
133 DELETED_DELETED_PALM_REC = SPENT_PC_RECORD_BIT + 105L
136 /* Shamelessly copied from JPilot (libplugin.h) */
139 unsigned int unique_id;
140 unsigned char attrib;
145 /* Shamelessly copied from JPilot (libplugin.h) */
147 unsigned long header_len;
148 unsigned long header_version;
149 unsigned long rec_len;
150 unsigned long unique_id;
151 unsigned long rt; /* Record Type */
152 unsigned char attrib;
160 gboolean convert_charcode = TRUE;
162 static const gchar *jpilot_get_charset(void)
164 static const gchar *charset = NULL;
167 charset = getenv("PILOT_CHARSET");
176 * Create new pilot file object.
177 * \return Initialized JPilot file object.
179 JPilotFile *jpilot_create() {
180 JPilotFile *pilotFile;
181 pilotFile = g_new0( JPilotFile, 1 );
182 pilotFile->type = ADBOOKTYPE_JPILOT;
183 pilotFile->addressCache = addrcache_create();
184 pilotFile->retVal = MGU_SUCCESS;
186 pilotFile->file = NULL;
187 pilotFile->path = NULL;
188 pilotFile->readMetadata = FALSE;
189 pilotFile->customLabels = NULL;
190 pilotFile->labelInd = NULL;
191 pilotFile->havePC3 = FALSE;
192 pilotFile->pc3ModifyTime = 0;
197 * Create new pilot file object for specified file.
198 * \param path Path to JPilot address book.
199 * \return Initialized JPilot file object.
201 JPilotFile *jpilot_create_path( const gchar *path ) {
202 JPilotFile *pilotFile;
203 pilotFile = jpilot_create();
204 jpilot_set_file( pilotFile, path );
211 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
212 cm_return_if_fail( pilotFile != NULL );
213 addrcache_set_name( pilotFile->addressCache, value );
215 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
216 cm_return_if_fail( pilotFile != NULL );
217 addrcache_refresh( pilotFile->addressCache );
218 pilotFile->readMetadata = FALSE;
219 pilotFile->path = mgu_replace_string( pilotFile->path, value );
221 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
222 cm_return_if_fail( pilotFile != NULL );
223 pilotFile->addressCache->accessFlag = value;
226 gint jpilot_get_status( JPilotFile *pilotFile ) {
227 cm_return_val_if_fail( pilotFile != NULL, -1 );
228 return pilotFile->retVal;
230 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
231 cm_return_val_if_fail( pilotFile != NULL, NULL );
232 return addrcache_get_root_folder( pilotFile->addressCache );
234 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
235 cm_return_val_if_fail( pilotFile != NULL, NULL );
236 return addrcache_get_name( pilotFile->addressCache );
240 * Test whether file was read.
241 * \param pilotFile JPilot control data.
242 * \return <i>TRUE</i> if file was read.
244 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
245 cm_return_val_if_fail( pilotFile != NULL, FALSE );
246 return pilotFile->addressCache->dataRead;
250 * Free up custom label list.
251 * \param pilotFile JPilot control data.
253 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
256 cm_return_if_fail( pilotFile != NULL );
258 /* Release custom labels */
259 mgu_free_dlist( pilotFile->customLabels );
260 pilotFile->customLabels = NULL;
262 /* Release indexes */
263 node = pilotFile->labelInd;
266 node = g_list_next( node );
268 g_list_free( pilotFile->labelInd );
269 pilotFile->labelInd = NULL;
271 /* Force a fresh read */
272 addrcache_refresh( pilotFile->addressCache );
276 * Append a custom label, representing an E-Mail address field to the
278 * \param pilotFile JPilot control data.
280 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
281 cm_return_if_fail( pilotFile != NULL );
284 gchar *labelCopy = g_strdup( labelName );
285 g_strstrip( labelCopy );
286 if( *labelCopy == '\0' ) {
290 pilotFile->customLabels = g_list_append( pilotFile->customLabels, labelCopy );
291 /* Force a fresh read */
292 addrcache_refresh( pilotFile->addressCache );
298 * Get list of custom labels.
299 * \param pilotFile JPilot control data.
300 * \return List of labels. Must use g_free() when done.
302 GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
303 GList *retVal = NULL;
306 cm_return_val_if_fail( pilotFile != NULL, NULL );
308 node = pilotFile->customLabels;
310 retVal = g_list_append( retVal, g_strdup( node->data ) );
311 node = g_list_next( node );
317 * Return filespec of PC3 file corresponding to JPilot PDB file.
318 * \param pilotFile JPilot control data.
319 * \return File specification; should be g_free() when done.
321 static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
325 if( pilotFile == NULL ) return NULL;
326 if( pilotFile->path == NULL ) return NULL;
328 fileSpec = g_strdup( pilotFile->path );
329 len = strlen( fileSpec );
332 for( i = len; i > 0; i-- ) {
333 if( *(fileSpec + i) == '.' ) {
340 if( len - pos == 3 ) {
341 *r++ = 'p'; *r++ = 'c'; *r = '3';
350 * Save PC3 file time to cache.
351 * \param pilotFile JPilot control data.
352 * \return <i>TRUE</i> if time marked.
354 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
355 gboolean retVal = FALSE;
356 struct stat filestat;
359 /* Mark PDB file cache */
360 retVal = addrcache_mark_file( pilotFile->addressCache, pilotFile->path );
362 /* Now mark PC3 file */
363 pilotFile->havePC3 = FALSE;
364 pilotFile->pc3ModifyTime = 0;
365 pcFile = jpilot_get_pc3_file( pilotFile );
366 if( pcFile == NULL ) return retVal;
367 if( 0 == g_stat( pcFile, &filestat ) ) {
368 pilotFile->havePC3 = TRUE;
369 pilotFile->pc3ModifyTime = filestat.st_mtime;
377 * Check whether JPilot PDB or PC3 file has changed by comparing
379 * \param pilotFile JPilot control data.
380 * \return <i>TRUE</i> if file has changed.
382 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
383 gboolean retVal = TRUE;
384 struct stat filestat;
387 /* Check main file */
388 if( addrcache_check_file( pilotFile->addressCache, pilotFile->path ) )
392 if( ! pilotFile->havePC3 ) return FALSE;
393 pcFile = jpilot_get_pc3_file( pilotFile );
394 if( pcFile == NULL ) return FALSE;
396 if( 0 == g_stat( pcFile, &filestat ) ) {
397 if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
404 * Test whether file was modified since last access.
405 * Return: TRUE if file was modified.
407 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
408 cm_return_val_if_fail( pilotFile != NULL, FALSE );
409 pilotFile->addressCache->modified = jpilot_check_files( pilotFile );
410 return pilotFile->addressCache->modified;
412 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
413 cm_return_val_if_fail( pilotFile != NULL, FALSE );
414 return pilotFile->addressCache->accessFlag;
418 * Free up pilot file object by releasing internal memory.
419 * \param pilotFile JPilot control data.
421 void jpilot_free( JPilotFile *pilotFile ) {
422 cm_return_if_fail( pilotFile != NULL );
424 /* Release custom labels */
425 jpilot_clear_custom_labels( pilotFile );
428 addrcache_clear( pilotFile->addressCache );
429 addrcache_free( pilotFile->addressCache );
431 /* Free internal stuff */
432 g_free( pilotFile->path );
434 pilotFile->file = NULL;
435 pilotFile->path = NULL;
436 pilotFile->readMetadata = FALSE;
437 pilotFile->havePC3 = FALSE;
438 pilotFile->pc3ModifyTime = 0;
440 pilotFile->type = ADBOOKTYPE_NONE;
441 pilotFile->addressCache = NULL;
442 pilotFile->retVal = MGU_SUCCESS;
444 /* Now release file object */
448 /* Shamelessly copied from JPilot (libplugin.c) */
449 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
452 for (i=0;i<num_bytes;i++) {
458 /* Shamelessly copied from JPilot (utils.c) */
459 /* These next 2 functions were copied from pi-file.c in the pilot-link app */
460 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
461 #define PILOT_TIME_DELTA (unsigned)(2082844800)
463 static time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
464 return (time_t)(raw_time - PILOT_TIME_DELTA);
467 /* Shamelessly copied from JPilot (libplugin.c) */
468 static int raw_header_to_header(RawDBHeader *rdbh, DBHeader *dbh) {
471 strncpy(dbh->db_name, rdbh->db_name, 31);
472 dbh->db_name[31] = '\0';
473 dbh->flags = bytes_to_bin(rdbh->flags, 2);
474 dbh->version = bytes_to_bin(rdbh->version, 2);
475 temp = bytes_to_bin(rdbh->creation_time, 4);
476 dbh->creation_time = pilot_time_to_unix_time(temp);
477 temp = bytes_to_bin(rdbh->modification_time, 4);
478 dbh->modification_time = pilot_time_to_unix_time(temp);
479 temp = bytes_to_bin(rdbh->backup_time, 4);
480 dbh->backup_time = pilot_time_to_unix_time(temp);
481 dbh->modification_number = bytes_to_bin(rdbh->modification_number, 4);
482 dbh->app_info_offset = bytes_to_bin(rdbh->app_info_offset, 4);
483 dbh->sort_info_offset = bytes_to_bin(rdbh->sort_info_offset, 4);
484 strncpy(dbh->type, rdbh->type, 4);
486 strncpy(dbh->creator_id, rdbh->creator_id, 4);
487 dbh->creator_id[4] = '\0';
488 strncpy(dbh->unique_id_seed, rdbh->unique_id_seed, 4);
489 dbh->unique_id_seed[4] = '\0';
490 dbh->next_record_list_id = bytes_to_bin(rdbh->next_record_list_id, 4);
491 dbh->number_of_records = bytes_to_bin(rdbh->number_of_records, 2);
495 /* Shamelessly copied from JPilot (libplugin.c) */
496 /* returns 1 if found */
498 static int find_next_offset( mem_rec_header *mem_rh, long fpos,
499 unsigned int *next_offset, unsigned char *attrib, unsigned int *unique_id )
501 mem_rec_header *temp_mem_rh;
502 unsigned char found = 0;
503 unsigned long found_at;
506 for (temp_mem_rh=mem_rh; temp_mem_rh; temp_mem_rh = temp_mem_rh->next) {
507 if ((temp_mem_rh->offset > fpos) && (temp_mem_rh->offset < found_at)) {
508 found_at = temp_mem_rh->offset;
509 /* *attrib = temp_mem_rh->attrib; */
510 /* *unique_id = temp_mem_rh->unique_id; */
512 if ((temp_mem_rh->offset == fpos)) {
514 *attrib = temp_mem_rh->attrib;
515 *unique_id = temp_mem_rh->unique_id;
518 *next_offset = found_at;
522 /* Shamelessly copied from JPilot (libplugin.c) */
523 static void free_mem_rec_header(mem_rec_header **mem_rh) {
524 mem_rec_header *h, *next_h;
525 for (h=*mem_rh; h; h=next_h) {
532 /* Shamelessly copied from JPilot (libplugin.c) */
534 static int jpilot_get_info_size( FILE *in, int *size ) {
541 fseek(in, 0, SEEK_SET);
542 r = fread(&rdbh, sizeof(RawDBHeader), 1, in);
547 raw_header_to_header(&rdbh, &dbh);
548 if (dbh.app_info_offset==0) {
552 if (dbh.sort_info_offset!=0) {
553 *size = dbh.sort_info_offset - dbh.app_info_offset;
556 if (dbh.number_of_records==0) {
557 fseek(in, 0, SEEK_END);
558 *size=ftell(in) - dbh.app_info_offset;
562 r = fread(&rh, sizeof(record_header), 1, in);
563 offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
564 *size=offset - dbh.app_info_offset;
570 * Read address file into address list. Based on JPilot's
571 * libplugin.c (jp_get_app_info)
573 static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, int *buf_size ) {
576 unsigned int rec_size;
580 if( ( !buf_size ) || ( ! buf ) ) {
587 if( pilotFile->path ) {
588 in = g_fopen( pilotFile->path, "rb" );
590 return MGU_OPEN_FILE;
597 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
601 return MGU_ERROR_READ;
609 /* Convert header into something recognizable */
610 raw_header_to_header(&rdbh, &dbh);
612 num = jpilot_get_info_size(in, &rec_size);
615 return MGU_ERROR_READ;
618 fseek(in, dbh.app_info_offset, SEEK_SET);
619 *buf = ( char * ) malloc(rec_size);
622 return MGU_OO_MEMORY;
624 num = fread(*buf, rec_size, 1, in);
629 return MGU_ERROR_READ;
634 *buf_size = rec_size;
639 /* Shamelessly copied from JPilot (libplugin.c) */
640 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
646 memcpy(&l, p, sizeof(l));
647 header->header_len=ntohl(l);
650 memcpy(&l, p, sizeof(l));
651 header->header_version=ntohl(l);
654 memcpy(&l, p, sizeof(l));
655 header->rec_len=ntohl(l);
658 memcpy(&l, p, sizeof(l));
659 header->unique_id=ntohl(l);
662 memcpy(&l, p, sizeof(l));
666 memcpy(&(header->attrib), p, sizeof(unsigned char));
667 p+=sizeof(unsigned char);
672 /* Shamelessly copied from JPilot (libplugin.c) */
673 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
676 unsigned char packed_header[256];
679 num = fread(&l, sizeof(l), 1, pc_in);
686 memcpy(packed_header, &l, sizeof(l));
691 num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
698 unpack_header(header, packed_header);
703 * Read next record from PC3 file. Based on JPilot function
704 * <code>pc_read_next_rec()</code> (libplugin.c)
706 * \param in File handle.
707 * \param br Record buffer.
708 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
711 static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
712 PC3RecordHeader header;
719 num = read_header( in, &header );
722 return MGU_ERROR_READ;
728 rec_len = header.rec_len;
729 record = malloc( rec_len );
731 return MGU_OO_MEMORY;
733 num = fread( record, rec_len, 1, in );
737 return MGU_ERROR_READ;
741 br->unique_id = header.unique_id;
742 br->attrib = header.attrib;
750 * Read address file into a linked list. Based on JPilot function
751 * <code>jp_read_DB_files()</code> (from libplugin.c)
753 * \param pilotFile JPilot control data.
754 * \param records Pointer to linked list of records read.
755 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
758 static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
762 int num_records, recs_returned, i, num, r;
763 unsigned int offset, prev_offset, next_offset = 0, rec_size;
765 long fpos; /*file position indicator */
766 unsigned char attrib = '\0';
767 unsigned int unique_id = 0;
768 mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
775 mem_rh = last_mem_rh = NULL;
779 if( pilotFile->path == NULL ) {
783 in = g_fopen( pilotFile->path, "rb" );
785 return MGU_OPEN_FILE;
788 /* Read the database header */
789 num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
793 return MGU_ERROR_READ;
800 raw_header_to_header( &rdbh, &dbh );
802 /* Read each record entry header */
803 num_records = dbh.number_of_records;
807 for( i = 1; i < num_records + 1; i++ ) {
808 num = fread( &rh, sizeof( record_header ), 1, in );
820 ( ( rh.Offset[0] * 256 + rh.Offset[1] ) * 256
821 + rh.Offset[2] ) * 256
823 if( offset < prev_offset ) {
826 prev_offset = offset;
827 temp_mem_rh = ( mem_rec_header * ) malloc( sizeof( mem_rec_header ) );
828 if( ! temp_mem_rh ) {
831 temp_mem_rh->next = NULL;
832 temp_mem_rh->rec_num = i;
833 temp_mem_rh->offset = offset;
834 temp_mem_rh->attrib = rh.attrib;
835 temp_mem_rh->unique_id =
836 ( rh.unique_ID[0] * 256 + rh.unique_ID[1] ) * 256
838 if( mem_rh == NULL ) {
839 mem_rh = temp_mem_rh;
840 last_mem_rh = temp_mem_rh;
843 last_mem_rh->next = temp_mem_rh;
844 last_mem_rh = temp_mem_rh;
848 temp_mem_rh = mem_rh;
853 mem_rh, 0, &next_offset, &attrib, &unique_id );
857 next_offset = mem_rh->offset;
858 attrib = mem_rh->attrib;
859 unique_id = mem_rh->unique_id;
862 fseek( in, next_offset, SEEK_SET );
863 while( ! feof( in ) ) {
867 mem_rh, fpos, &next_offset, &attrib,
870 next_offset = 0xFFFFFF;
872 attrib = temp_mem_rh->attrib;
873 unique_id = temp_mem_rh->unique_id;
874 if ( temp_mem_rh->next ) {
875 temp_mem_rh = temp_mem_rh->next;
876 next_offset = temp_mem_rh->offset;
880 rec_size = next_offset - fpos;
881 buf = malloc( rec_size );
883 num = fread( buf, rec_size, 1, in );
891 temp_br = malloc( sizeof( buf_rec ) );
896 temp_br->rt = PALM_REC;
897 temp_br->unique_id = unique_id;
898 temp_br->attrib = attrib;
900 temp_br->size = rec_size;
902 *records = g_list_append( *records, temp_br );
908 free_mem_rec_header( &mem_rh );
910 /* Read the PC3 file, if present */
911 pcFile = jpilot_get_pc3_file( pilotFile );
912 if( pcFile == NULL ) return MGU_SUCCESS;
913 pc_in = g_fopen( pcFile, "rb");
916 if( pc_in == NULL ) {
920 while( ! feof( pc_in ) ) {
923 temp_br = malloc( sizeof( buf_rec ) );
927 r = jpilot_read_next_pc( pc_in, temp_br );
928 if( r != MGU_SUCCESS ) {
929 if( (r != MGU_EOF) && (r != MGU_ERROR_READ) ) {
930 free( temp_br->buf );
937 if( ( temp_br->rt != DELETED_PC_REC )
938 && ( temp_br->rt != DELETED_PALM_REC )
939 && ( temp_br->rt != MODIFIED_PALM_REC )
940 && ( temp_br->rt != DELETED_DELETED_PALM_REC ) )
942 *records = g_list_append( *records, temp_br );
947 if( ( temp_br->rt == DELETED_PALM_REC )
948 || ( temp_br->rt == MODIFIED_PALM_REC ) )
950 temp_list = *records;
952 while( temp_list->next ) {
953 temp_list=temp_list->next;
956 for( ; temp_list; temp_list=temp_list->prev ) {
957 if( ( ( buf_rec * )temp_list->data )->unique_id ==
958 temp_br->unique_id ) {
959 ( ( buf_rec * )temp_list->data )->rt =
966 free( temp_br->buf );
976 * Parse buffer containing multiple e-mail addresses into a linked list of
977 * addresses. Separator characters are " ,;|" and control characters. Address
978 * is only extracted if it contains an "at" (@) character.
980 * \param buf Buffer to process.
981 * \return List of strings.
983 static GList *jpilot_parse_email( gchar *buf ) {
988 gboolean valid, done;
990 valid = done = FALSE;
995 if( *p == ' ' || *p == ',' || *p == ';' || *p == '|' || *p < 32 ) {
1010 em = g_strndup( st, len );
1011 list = g_list_append( list, em );
1019 if( *p == '@' ) valid = TRUE;
1027 #define FULLNAME_BUFSIZE 256
1028 #define EMAIL_BUFSIZE 256
1031 * Process a single label entry field, parsing multiple e-mail address entries.
1033 * \param pilotFile JPilot control data.
1034 * \param labelEntry Label entry data.
1035 * \param person Person.
1037 static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPerson *person ) {
1038 gchar buffer[ EMAIL_BUFSIZE ];
1044 strcpy( buffer, labelEntry );
1045 node = list = jpilot_parse_email( buffer );
1047 email = addritem_create_item_email();
1048 addritem_email_set_address( email, node->data );
1049 if (convert_charcode) {
1050 gchar *convertBuff = NULL;
1051 convertBuff = conv_codeset_strdup( labelEntry,
1052 jpilot_get_charset(),
1055 addritem_email_set_remarks( email, convertBuff );
1056 g_free( convertBuff );
1059 addritem_email_set_remarks(email, buffer);
1062 addrcache_id_email( pilotFile->addressCache, email );
1063 addrcache_person_add_email( pilotFile->addressCache, person, email );
1064 node = g_list_next( node );
1066 mgu_free_dlist( list );
1072 * Unpack address, building new data inside cache.
1073 * \param pilotFile JPilot control data.
1074 * \param buf Record buffer.
1075 * \param folderInd Array of (category) folders to load.
1077 static void jpilot_load_address(
1078 JPilotFile *pilotFile, buf_rec *buf, ItemFolder *folderInd[] )
1080 struct Address addr;
1086 gchar fullName[ FULLNAME_BUFSIZE ];
1092 struct AddressAppInfo *ai;
1093 gchar **firstName = NULL;
1094 gchar **lastName = NULL;
1095 #if (PILOT_LINK_MAJOR > 11)
1096 pi_buffer_t *RecordBuffer;
1097 RecordBuffer = pi_buffer_new(buf->size);
1099 memcpy(RecordBuffer->data, buf->buf, buf->size);
1100 RecordBuffer->used = buf->size;
1101 if (unpack_Address(&addr, RecordBuffer, address_v1) == -1) {
1102 pi_buffer_free(RecordBuffer);
1105 pi_buffer_free(RecordBuffer);
1109 num = unpack_Address(&addr, buf->buf, buf->size);
1113 #endif /* PILOT_LINK_0_12 */
1115 addrEnt = addr.entry;
1116 attrib = buf->attrib;
1117 unique_id = buf->unique_id;
1118 cat_id = attrib & 0x0F;
1121 if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
1122 firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
1125 if( addrEnt[ IND_LABEL_LASTNAME ] ) {
1126 lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
1129 if( name_order == FAMILY_LAST ) {
1130 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1131 firstName ? firstName[0] : "",
1132 lastName ? lastName[0] : "" );
1135 g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
1136 lastName ? lastName[0] : "",
1137 firstName ? firstName[0] : "" );
1141 g_strfreev( firstName );
1144 g_strfreev( lastName );
1147 g_strstrip( fullName );
1149 if( convert_charcode ) {
1150 gchar *nameConv = NULL;
1151 nameConv = conv_codeset_strdup( fullName,
1152 jpilot_get_charset(),
1155 strncpy2( fullName, nameConv, FULLNAME_BUFSIZE );
1159 person = addritem_create_item_person();
1160 addritem_person_set_common_name( person, fullName );
1161 addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
1162 addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
1163 addrcache_id_person( pilotFile->addressCache, person );
1165 extID = g_strdup_printf( "%d", unique_id );
1166 addritem_person_set_external_id( person, extID );
1170 /* Pointer to address metadata. */
1171 ai = & pilotFile->addrInfo;
1173 /* Add entry for each email address listed under phone labels. */
1174 indPhoneLbl = addr.phoneLabel;
1175 for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
1178 ind = indPhoneLbl[k];
1180 * g_print( "%d : %d : %20s : %s\n", k, ind,
1181 * ai->phoneLabels[ind], addrEnt[3+k] );
1183 if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
1184 labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
1185 jpilot_parse_label( pilotFile, labelEntry, person );
1189 /* Add entry for each custom label */
1190 node = pilotFile->labelInd;
1194 ind = GPOINTER_TO_INT( node->data );
1197 * g_print( "%d : %20s : %s\n", ind, ai->labels[ind],
1200 labelEntry = addrEnt[ind];
1201 jpilot_parse_label( pilotFile, labelEntry, person );
1204 node = g_list_next( node );
1207 if( person->listEMail ) {
1208 if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
1209 /* Add to specified category */
1210 addrcache_folder_add_person(
1211 pilotFile->addressCache,
1212 folderInd[cat_id], person );
1215 /* Add to root folder */
1216 addrcache_add_person(
1217 pilotFile->addressCache, person );
1221 addritem_free_item_person( person );
1224 /* Free up pointer allocated inside address */
1225 free_Address( & addr );
1229 * Free up address list.
1230 * \param records List of records to free.
1232 static void jpilot_free_addrlist( GList *records ) {
1242 node = g_list_next( node );
1246 g_list_free( records );
1250 * Read metadata from file.
1251 * \param pilotFile JPilot control data.
1252 * \return Status/error code. <code>MGU_SUCCESS</code> if data read
1255 static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
1257 unsigned int rec_size;
1261 cm_return_val_if_fail( pilotFile != NULL, -1 );
1263 pilotFile->readMetadata = FALSE;
1264 addrcache_clear( pilotFile->addressCache );
1266 /* Read file info */
1267 retVal = jpilot_get_file_info( pilotFile, &buf, &rec_size);
1268 if( retVal != MGU_SUCCESS ) {
1269 pilotFile->retVal = retVal;
1270 return pilotFile->retVal;
1273 num = unpack_AddressAppInfo( &pilotFile->addrInfo, buf, rec_size );
1278 pilotFile->retVal = MGU_ERROR_READ;
1279 return pilotFile->retVal;
1282 pilotFile->readMetadata = TRUE;
1283 pilotFile->retVal = MGU_SUCCESS;
1284 return pilotFile->retVal;
1288 * Setup labels and indexes from metadata.
1289 * \param pilotFile JPilot control data.
1290 * \return <i>TRUE</i> is setup successfully.
1292 static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
1293 gboolean retVal = FALSE;
1294 struct AddressAppInfo *ai;
1297 cm_return_val_if_fail( pilotFile != NULL, -1 );
1299 /* Release indexes */
1300 node = pilotFile->labelInd;
1303 node = g_list_next( node );
1305 pilotFile->labelInd = NULL;
1307 if( pilotFile->readMetadata ) {
1308 ai = & pilotFile->addrInfo;
1309 node = pilotFile->customLabels;
1311 gchar *lbl = node->data;
1314 for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
1315 gchar *labelName = ai->labels[i];
1317 if( convert_charcode ) {
1318 gchar *convertBuff = NULL;
1319 convertBuff = conv_codeset_strdup( labelName,
1320 jpilot_get_charset(),
1323 labelName = convertBuff;
1327 if( g_utf8_collate( labelName, lbl ) == 0 ) {
1332 pilotFile->labelInd = g_list_append(
1333 pilotFile->labelInd, GINT_TO_POINTER(ind) );
1334 node = g_list_next( node );
1342 * Load list with character strings of custom label names. Only none blank
1344 * \param pilotFile JPilot control data.
1345 * \param labelList List of label names to load.
1346 * \return List of label names loaded. Should be freed when done.
1348 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
1351 cm_return_val_if_fail( pilotFile != NULL, NULL );
1353 if( pilotFile->readMetadata ) {
1354 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1355 for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
1356 gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
1358 g_strchomp( labelName );
1359 g_strchug( labelName );
1360 if( *labelName != '\0' ) {
1361 if( convert_charcode ) {
1362 gchar *convertBuff = NULL;
1363 convertBuff = conv_codeset_strdup( labelName,
1364 jpilot_get_charset(),
1367 labelName = convertBuff;
1371 labelName = g_strdup( labelName );
1373 labelList = g_list_append( labelList, labelName );
1382 * Build folder in address book for each category.
1383 * \param pilotFile JPilot control data.
1385 static void jpilot_build_category_list( JPilotFile *pilotFile ) {
1386 struct AddressAppInfo *ai = & pilotFile->addrInfo;
1387 struct CategoryAppInfo *cat = & ai->category;
1390 for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
1391 ItemFolder *folder = addritem_create_item_folder();
1393 if( convert_charcode ) {
1394 gchar *convertBuff = NULL;
1395 convertBuff = conv_codeset_strdup( cat->name[i],
1396 jpilot_get_charset(),
1399 addritem_folder_set_name( folder, convertBuff );
1400 g_free( convertBuff );
1402 addritem_folder_set_name( folder, cat->name[i] );
1406 addritem_folder_set_name( folder, cat->name[i] );
1409 addrcache_id_folder( pilotFile->addressCache, folder );
1410 addrcache_add_folder( pilotFile->addressCache, folder );
1415 * Remove empty (category) folders.
1416 * \param pilotFile JPilot control data.
1418 static void jpilot_remove_empty( JPilotFile *pilotFile ) {
1424 listFolder = addrcache_get_list_folder( pilotFile->addressCache );
1428 ItemFolder *folder = node->data;
1429 if( ADDRITEM_NAME(folder) == NULL || *ADDRITEM_NAME(folder) == '\0' ) {
1430 if( folder->listPerson ) {
1431 /* Give name to folder */
1433 sprintf( name, "? %d", i );
1434 addritem_folder_set_name( folder, name );
1437 /* Mark for removal */
1438 remList = g_list_append( remList, folder );
1441 node = g_list_next( node );
1446 ItemFolder *folder = node->data;
1447 addrcache_remove_folder( pilotFile->addressCache, folder );
1448 node = g_list_next( node );
1450 g_list_free( remList );
1454 * Read address file into address cache.
1455 * \param pilotFile JPilot control data.
1456 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1459 static gint jpilot_read_file( JPilotFile *pilotFile ) {
1461 GList *records = NULL;
1464 ItemFolder *folderInd[ JPILOT_NUM_CATEG ];
1466 /* Read list of records from JPilot files */
1467 retVal = jpilot_read_db_files( pilotFile, &records );
1468 if( retVal != MGU_SUCCESS ) {
1469 jpilot_free_addrlist( records );
1473 /* Setup labels and category folders */
1474 jpilot_setup_labels( pilotFile );
1475 jpilot_build_category_list( pilotFile );
1477 /* Build array of pointers to categories */
1479 node = addrcache_get_list_folder( pilotFile->addressCache );
1481 if( i < JPILOT_NUM_CATEG ) {
1482 folderInd[i] = node->data;
1484 node = g_list_next( node );
1488 /* Load all addresses, free up old stuff as we go */
1492 if( ( br->rt != DELETED_PC_REC ) &&
1493 ( br->rt != DELETED_PALM_REC ) &&
1494 ( br->rt != MODIFIED_PALM_REC ) &&
1495 ( br->rt != DELETED_DELETED_PALM_REC ) ) {
1496 jpilot_load_address( pilotFile, br, folderInd );
1501 node = g_list_next( node );
1505 g_list_free( records );
1507 /* Remove empty category folders */
1508 jpilot_remove_empty( pilotFile );
1509 jpilot_mark_files( pilotFile );
1515 * Read file into list. Main entry point
1516 * \param pilotFile JPilot control data.
1517 * \return Error/status code. <code>MGU_SUCCESS</code> if data read
1520 gint jpilot_read_data( JPilotFile *pilotFile ) {
1521 const gchar *cur_locale;
1523 name_order = FAMILY_LAST;
1525 cur_locale = conv_get_current_locale();
1527 if( g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ) {
1528 name_order = FAMILY_FIRST;
1531 cm_return_val_if_fail( pilotFile != NULL, -1 );
1533 pilotFile->retVal = MGU_SUCCESS;
1534 pilotFile->addressCache->accessFlag = FALSE;
1535 if( jpilot_check_files( pilotFile ) ) {
1536 addrcache_clear( pilotFile->addressCache );
1537 jpilot_read_metadata( pilotFile );
1538 if( pilotFile->retVal == MGU_SUCCESS ) {
1539 pilotFile->retVal = jpilot_read_file( pilotFile );
1540 if( pilotFile->retVal == MGU_SUCCESS ) {
1541 pilotFile->addressCache->modified = FALSE;
1542 pilotFile->addressCache->dataRead = TRUE;
1546 return pilotFile->retVal;
1550 * Return linked list of persons. This is a list of references to ItemPerson
1551 * objects. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1552 * functions... this will destroy the addressbook data!
1554 * \param pilotFile JPilot control data.
1555 * \return List of persons.
1557 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
1558 cm_return_val_if_fail( pilotFile != NULL, NULL );
1559 return addrcache_get_list_person( pilotFile->addressCache );
1563 * Return linked list of folders. This is a list of references to non-empty
1564 * category folders. Do <b>NOT</b> attempt to use the
1565 * <code>addrbook_free_xxx()</code> functions... this will destroy the
1568 * \param pilotFile JPilot control data.
1569 * \return List of ItemFolder objects. This should not be freed.
1571 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
1572 cm_return_val_if_fail( pilotFile != NULL, NULL );
1573 return addrcache_get_list_folder( pilotFile->addressCache );
1577 * Return linked list of all persons. Note that the list contains references
1578 * to items. Do <b>NOT</b> attempt to use the <code>addrbook_free_xxx()</code>
1579 * functions... this will destroy the addressbook data!
1581 * \param pilotFile JPilot control data.
1582 * \return List of items, or NULL if none.
1584 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
1585 cm_return_val_if_fail( pilotFile != NULL, NULL );
1586 return addrcache_get_all_persons( pilotFile->addressCache );
1589 #define WORK_BUFLEN 1024
1592 * Attempt to find a valid JPilot file.
1593 * \param pilotFile JPilot control data.
1594 * \return Filename, or home directory if not found, or empty string if
1595 * no home. Filename should be <code>g_free()</code> when done.
1597 gchar *jpilot_find_pilotdb( void ) {
1598 const gchar *homedir;
1599 gchar str[ WORK_BUFLEN + 1 ];
1603 homedir = get_home_dir();
1604 if( ! homedir ) return g_strdup( "" );
1606 strcpy( str, homedir );
1607 len = strlen( str );
1609 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
1610 str[ len ] = G_DIR_SEPARATOR;
1611 str[ ++len ] = '\0';
1614 strncat( str, JPILOT_DBHOME_DIR, WORK_BUFLEN - strlen(str) );
1615 strncat( str, G_DIR_SEPARATOR_S, WORK_BUFLEN - strlen(str) );
1616 strncat( str, JPILOT_DBHOME_FILE, WORK_BUFLEN - strlen(str) );
1618 /* Attempt to open */
1619 if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
1623 /* Truncate filename */
1626 return g_strdup( str );
1630 * Check whether label is in list of custom labels.
1631 * \param pilotFile JPilot control data.
1632 * \param labelName to test.
1633 * \return <i>TRUE</i> if found.
1635 gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
1639 cm_return_val_if_fail( pilotFile != NULL, FALSE );
1643 node = pilotFile->customLabels;
1645 if( g_utf8_collate( labelName, ( gchar * ) node->data ) == 0 ) {
1649 node = g_list_next( node );
1656 * Test whether pilot link library installed.
1657 * \return <i>TRUE</i> if library available.
1659 gboolean jpilot_test_pilot_lib( void ) {
1663 #endif /* USE_JPILOT */