Cosmetic: use always g_getenv instead of getenv
[claws.git] / src / jpilot.c
index 367c4e133bec39a53fd5ff3552b0abb4b9b47aaa..cedfca999426f5a7b5ffa3ea52ffadda6e449a49 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001-2006 Match Grun and the Sylpheed-Claws team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
@@ -25,6 +24,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #ifdef USE_JPILOT
 #  include <libpisock/pi-args.h>
 #  include <libpisock/pi-appinfo.h>
 #  include <libpisock/pi-address.h>
+#  include <libpisock/pi-version.h>
 #else
 #  include <pi-args.h>
 #  include <pi-appinfo.h>
 #  include <pi-address.h>
+#  include <pi-version.h>
 #endif
 
 #include "mgutils.h"
@@ -156,6 +158,19 @@ enum {
 
 gboolean convert_charcode = TRUE;
 
+static const gchar *jpilot_get_charset(void)
+{
+       static const gchar *charset = NULL;
+
+       if (charset == NULL)
+               charset = g_getenv("PILOT_CHARSET");
+
+       if (charset == NULL)
+               charset = CS_CP1252;
+       
+       return charset;
+}
+
 /*
 * Create new pilot file object.
 * \return Initialized JPilot file object.
@@ -193,30 +208,30 @@ JPilotFile *jpilot_create_path( const gchar *path ) {
 * Properties...
 */
 void jpilot_set_name( JPilotFile* pilotFile, const gchar *value ) {
-       g_return_if_fail( pilotFile != NULL );
+       cm_return_if_fail( pilotFile != NULL );
        addrcache_set_name( pilotFile->addressCache, value );
 }
 void jpilot_set_file( JPilotFile* pilotFile, const gchar *value ) {
-       g_return_if_fail( pilotFile != NULL );
+       cm_return_if_fail( pilotFile != NULL );
        addrcache_refresh( pilotFile->addressCache );
        pilotFile->readMetadata = FALSE;
        pilotFile->path = mgu_replace_string( pilotFile->path, value );
 }
 void jpilot_set_accessed( JPilotFile *pilotFile, const gboolean value ) {
-       g_return_if_fail( pilotFile != NULL );
+       cm_return_if_fail( pilotFile != NULL );
        pilotFile->addressCache->accessFlag = value;
 }
 
 gint jpilot_get_status( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, -1 );
+       cm_return_val_if_fail( pilotFile != NULL, -1 );
        return pilotFile->retVal;
 }
 ItemFolder *jpilot_get_root_folder( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, NULL );
+       cm_return_val_if_fail( pilotFile != NULL, NULL );
        return addrcache_get_root_folder( pilotFile->addressCache );
 }
 gchar *jpilot_get_name( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, NULL );
+       cm_return_val_if_fail( pilotFile != NULL, NULL );
        return addrcache_get_name( pilotFile->addressCache );
 }
 
@@ -226,7 +241,7 @@ gchar *jpilot_get_name( JPilotFile *pilotFile ) {
  * \return <i>TRUE</i> if file was read.
  */
 gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, FALSE );
+       cm_return_val_if_fail( pilotFile != NULL, FALSE );
        return pilotFile->addressCache->dataRead;
 }
 
@@ -237,7 +252,7 @@ gboolean jpilot_get_read_flag( JPilotFile *pilotFile ) {
 void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
        GList *node;
 
-       g_return_if_fail( pilotFile != NULL );
+       cm_return_if_fail( pilotFile != NULL );
 
        /* Release custom labels */
        mgu_free_dlist( pilotFile->customLabels );
@@ -262,7 +277,7 @@ void jpilot_clear_custom_labels( JPilotFile *pilotFile ) {
  * \param pilotFile  JPilot control data.
  */
 void jpilot_add_custom_label( JPilotFile *pilotFile, const gchar *labelName ) {
-       g_return_if_fail( pilotFile != NULL );
+       cm_return_if_fail( pilotFile != NULL );
 
        if( labelName ) {
                gchar *labelCopy = g_strdup( labelName );
@@ -287,7 +302,7 @@ GList *jpilot_get_custom_labels( JPilotFile *pilotFile ) {
        GList *retVal = NULL;
        GList *node;
 
-       g_return_val_if_fail( pilotFile != NULL, NULL );
+       cm_return_val_if_fail( pilotFile != NULL, NULL );
 
        node = pilotFile->customLabels;
        while( node ) {
@@ -337,7 +352,7 @@ static gchar *jpilot_get_pc3_file( JPilotFile *pilotFile ) {
  */
 static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
        gboolean retVal = FALSE;
-       struct stat filestat;
+       GStatBuf filestat;
        gchar *pcFile;
 
        /* Mark PDB file cache */
@@ -348,7 +363,7 @@ static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
        pilotFile->pc3ModifyTime = 0;
        pcFile = jpilot_get_pc3_file( pilotFile );
        if( pcFile == NULL ) return retVal;
-       if( 0 == stat( pcFile, &filestat ) ) {
+       if( 0 == g_stat( pcFile, &filestat ) ) {
                pilotFile->havePC3 = TRUE;
                pilotFile->pc3ModifyTime = filestat.st_mtime;
                retVal = TRUE;
@@ -365,7 +380,7 @@ static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
  */
 static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
        gboolean retVal = TRUE;
-       struct stat filestat;
+       GStatBuf filestat;
        gchar *pcFile;
 
        /* Check main file */
@@ -377,7 +392,7 @@ static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
        pcFile = jpilot_get_pc3_file( pilotFile );
        if( pcFile == NULL ) return FALSE;
 
-       if( 0 == stat( pcFile, &filestat ) ) {
+       if( 0 == g_stat( pcFile, &filestat ) ) {
                if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
        }
        g_free( pcFile );
@@ -389,16 +404,12 @@ static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
 * Return: TRUE if file was modified.
 */
 gboolean jpilot_get_modified( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, FALSE );
+       cm_return_val_if_fail( pilotFile != NULL, FALSE );
        pilotFile->addressCache->modified = jpilot_check_files( pilotFile );
        return pilotFile->addressCache->modified;
 }
-void jpilot_set_modified( JPilotFile *pilotFile, const gboolean value ) {
-       g_return_if_fail( pilotFile != NULL );
-       pilotFile->addressCache->modified = value;
-}
 gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, FALSE );
+       cm_return_val_if_fail( pilotFile != NULL, FALSE );
        return pilotFile->addressCache->accessFlag;
 }
 
@@ -407,7 +418,7 @@ gboolean jpilot_get_accessed( JPilotFile *pilotFile ) {
  * \param pilotFile  JPilot control data.
  */
 void jpilot_free( JPilotFile *pilotFile ) {
-       g_return_if_fail( pilotFile != NULL );
+       cm_return_if_fail( pilotFile != NULL );
 
        /* Release custom labels */
        jpilot_clear_custom_labels( pilotFile );
@@ -433,72 +444,6 @@ void jpilot_free( JPilotFile *pilotFile ) {
        g_free( pilotFile );
 }
 
-/*
-* Refresh internal variables to force a file read.
-*/
-void jpilot_force_refresh( JPilotFile *pilotFile ) {
-       addrcache_refresh( pilotFile->addressCache );
-}
-
-/*
-* Print object to specified stream.
-*/
-void jpilot_print_file( JPilotFile *pilotFile, FILE *stream ) {
-       GList *node;
-
-       g_return_if_fail( pilotFile != NULL );
-
-       fprintf( stream, "JPilotFile:\n" );
-       fprintf( stream, "file spec: '%s'\n", pilotFile->path );
-       fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
-       fprintf( stream, "  ret val: %d\n", pilotFile->retVal );
-
-       node = pilotFile->customLabels;
-       while( node ) {
-               fprintf( stream, "  c label: %s\n", (gchar *)node->data );
-               node = g_list_next( node );
-       }
-
-       node = pilotFile->labelInd;
-       while( node ) {
-               fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
-               node = g_list_next( node );
-       }
-
-       addrcache_print( pilotFile->addressCache, stream );
-       fprintf( stream, "  ret val: %d\n", pilotFile->retVal );
-       fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
-       fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
-       addritem_print_item_folder( pilotFile->addressCache->rootFolder, stream );
-}
-
-/*
-* Print summary of object to specified stream.
-*/
-void jpilot_print_short( JPilotFile *pilotFile, FILE *stream ) {
-       GList *node;
-       g_return_if_fail( pilotFile != NULL );
-       fprintf( stream, "JPilotFile:\n" );
-       fprintf( stream, "file spec: '%s'\n", pilotFile->path );
-       fprintf( stream, " metadata: %s\n", pilotFile->readMetadata ? "yes" : "no" );
-       fprintf( stream, "  ret val: %d\n", pilotFile->retVal );
-
-       node = pilotFile->customLabels;
-       while( node ) {
-               fprintf( stream, "  c label: %s\n", (gchar *)node->data );
-               node = g_list_next( node );
-       }
-
-       node = pilotFile->labelInd;
-       while( node ) {
-               fprintf( stream, " labelind: %d\n", GPOINTER_TO_INT(node->data) );
-               node = g_list_next( node );
-       }
-       addrcache_print( pilotFile->addressCache, stream );
-       fprintf( stream, " have pc3: %s\n", pilotFile->havePC3 ? "yes" : "no" );
-       fprintf( stream, " pc3 time: %lu\n", pilotFile->pc3ModifyTime );
-}
-
 /* Shamelessly copied from JPilot (libplugin.c) */
 static unsigned int bytes_to_bin(unsigned char *bytes, unsigned int num_bytes) {
 unsigned int i, n;
@@ -514,7 +459,7 @@ unsigned int i, n;
 /* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
 #define PILOT_TIME_DELTA (unsigned)(2082844800)
 
-time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
+static time_t pilot_time_to_unix_time ( unsigned long raw_time ) {
    return (time_t)(raw_time - PILOT_TIME_DELTA);
 }
 
@@ -590,9 +535,12 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
        DBHeader dbh;
        unsigned int offset;
        record_header rh;
+       int r;
 
        fseek(in, 0, SEEK_SET);
-       fread(&rdbh, sizeof(RawDBHeader), 1, in);
+       r = fread(&rdbh, sizeof(RawDBHeader), 1, in);
+       if (r < 1)
+               return MGU_ERROR_READ;
        if (feof(in)) {
                return MGU_EOF;
        }
@@ -612,7 +560,10 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
                return MGU_SUCCESS;
        }
 
-       fread(&rh, sizeof(record_header), 1, in);
+       r = fread(&rh, sizeof(record_header), 1, in);
+       if (r < 1)
+               return MGU_ERROR_READ;
+
        offset = ((rh.Offset[0]*256+rh.Offset[1])*256+rh.Offset[2])*256+rh.Offset[3];
        *size=offset - dbh.app_info_offset;
 
@@ -668,7 +619,10 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
                return MGU_ERROR_READ;
        }
 
-       fseek(in, dbh.app_info_offset, SEEK_SET);
+       if (fseek(in, dbh.app_info_offset, SEEK_SET) < 0) {
+               fclose(in);
+               return MGU_ERROR_READ;
+       }
        *buf = ( char * ) malloc(rec_size);
        if (!(*buf)) {
                fclose(in);
@@ -729,6 +683,8 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
        unsigned char packed_header[256];
        int num;
 
+       memset(header, 0, sizeof(PC3RecordHeader));
+
        num = fread(&l, sizeof(l), 1, pc_in);
        if (feof(pc_in)) {
                return -1;
@@ -738,7 +694,7 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
        }
        memcpy(packed_header, &l, sizeof(l));
        len=ntohl(l);
-       if (len > 255) {
+       if (len > 255 || len < sizeof(l)) {
                return -1;
        }
        num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
@@ -771,12 +727,12 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
        }
        num = read_header( in, &header );
        if( num < 1 ) {
-               if( ferror( in ) ) {
+               if( ferror( in ) )
                        return MGU_ERROR_READ;
-               }
-               if( feof( in ) ) {
+               else if( feof( in ) )
                        return MGU_EOF;
-               }
+               else
+                       return -1;
        }
        rec_len = header.rec_len;
        record = malloc( rec_len );
@@ -813,11 +769,11 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
        char *buf;
        GList *temp_list;
        int num_records, recs_returned, i, num, r;
-       unsigned int offset, prev_offset, next_offset, rec_size;
+       unsigned int offset, prev_offset, next_offset = 0, rec_size;
        int out_of_order;
        long fpos;  /*file position indicator */
-       unsigned char attrib;
-       unsigned int unique_id;
+       unsigned char attrib = '\0';
+       unsigned int unique_id = 0;
        mem_rec_header *mem_rh, *temp_mem_rh, *last_mem_rh;
        record_header rh;
        RawDBHeader rdbh;
@@ -865,6 +821,8 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                        }
                        if( feof( in ) ) {
                                fclose( in );
+                               if (mem_rh)
+                                       free_mem_rec_header( &mem_rh );
                                return MGU_EOF;
                        }
                }
@@ -912,7 +870,11 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                                unique_id = mem_rh->unique_id;
                        }
                }
-               fseek( in, next_offset, SEEK_SET );
+               if (fseek( in, next_offset, SEEK_SET ) < 0) {
+                       free_mem_rec_header( &mem_rh );
+                       fclose(in);
+                       return MGU_ERROR_READ;
+               }
                while( ! feof( in ) ) {
                        fpos = ftell( in );
                        if( out_of_order ) {
@@ -1094,17 +1056,18 @@ static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPe
 
        if( labelEntry ) {
                *buffer = '\0';
-               strcpy( buffer, labelEntry );
+               g_strlcpy( buffer, labelEntry, sizeof(buffer) );
                node = list = jpilot_parse_email( buffer );
                while( node ) {
                        email = addritem_create_item_email();
                        addritem_email_set_address( email, node->data );
                        if (convert_charcode) {
-                               gchar *convertBuff;
+                               gchar *convertBuff = NULL;
                                convertBuff = conv_codeset_strdup( labelEntry, 
-                                               conv_get_locale_charset_str_no_utf8(), 
+                                               jpilot_get_charset(), 
                                                CS_INTERNAL );
-                               addritem_email_set_remarks( email, convertBuff );
+                               if (convertBuff)
+                                       addritem_email_set_remarks( email, convertBuff );
                                g_free( convertBuff );
                        }
                        else {
@@ -1131,7 +1094,7 @@ static void jpilot_load_address(
 {
        struct Address addr;
        gchar **addrEnt;
-       gint num, k;
+       gint k;
        gint cat_id = 0;
        guint unique_id;
        guchar attrib;
@@ -1141,124 +1104,120 @@ static void jpilot_load_address(
        gchar *labelEntry;
        GList *node;
        gchar* extID;
-       struct AddressAppInfo *ai;
        gchar **firstName = NULL;
        gchar **lastName = NULL;
+#if (PILOT_LINK_MAJOR > 11)
+       pi_buffer_t *RecordBuffer;
+       RecordBuffer = pi_buffer_new(buf->size);
 
-       /* Retrieve address */
-       num = unpack_Address( & addr, buf->buf, buf->size );
-       if( num > 0 ) {
-               addrEnt = addr.entry;
-               attrib = buf->attrib;
-               unique_id = buf->unique_id;
-               cat_id = attrib & 0x0F;
+       memcpy(RecordBuffer->data, buf->buf, buf->size);
+       RecordBuffer->used = buf->size;
+       if (unpack_Address(&addr, RecordBuffer, address_v1) == -1) {
+               pi_buffer_free(RecordBuffer);
+               return;
+       }
+       pi_buffer_free(RecordBuffer);
+#else
+       gint num;
 
-               *fullName = '\0';
+       num = unpack_Address(&addr, buf->buf, buf->size);
+       if (num <= 0) {
+               return;
+       }
+#endif /* PILOT_LINK_0_12 */
 
-               if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
-                       firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
-               }
+       addrEnt = addr.entry;
+       attrib = buf->attrib;
+       unique_id = buf->unique_id;
+       cat_id = attrib & 0x0F;
 
-               if( addrEnt[ IND_LABEL_LASTNAME ] ) {
-                       lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
-               }
+       *fullName = '\0';
+       if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
+               firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
+       }
 
-               if( name_order == FAMILY_LAST ) {
-                       g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
-                                   firstName ? firstName[0] : "",
-                                   lastName ? lastName[0] : "" );
-               }
-               else {
-                       g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
-                                   lastName ? lastName[0] : "",
-                                   firstName ? firstName[0] : "" );
-               }
+       if( addrEnt[ IND_LABEL_LASTNAME ] ) {
+               lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
+       }
 
-               if( firstName ) {
-                       g_strfreev( firstName );
-               }
-               if( lastName ) {
-                       g_strfreev( lastName );
-               }
+       if( name_order == FAMILY_LAST ) {
+               g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
+                           firstName ? firstName[0] : "",
+                           lastName ? lastName[0] : "" );
+       }
+       else {
+               g_snprintf( fullName, FULLNAME_BUFSIZE, "%s %s",
+                           lastName ? lastName[0] : "",
+                           firstName ? firstName[0] : "" );
+       }
 
-               g_strstrip( fullName );
+       if( firstName ) {
+               g_strfreev( firstName );
+       }
+       if( lastName ) {
+               g_strfreev( lastName );
+       }
 
-               if( convert_charcode ) {
-                       gchar *nameConv;
-                       nameConv = conv_codeset_strdup( fullName, 
-                                       conv_get_locale_charset_str_no_utf8(), 
-                                       CS_INTERNAL );
+       g_strstrip( fullName );
+
+       if( convert_charcode ) {
+               gchar *nameConv = NULL;
+               nameConv = conv_codeset_strdup( fullName, 
+                               jpilot_get_charset(), 
+                               CS_INTERNAL );
+               if (nameConv)
                        strncpy2( fullName, nameConv, FULLNAME_BUFSIZE );
-                       g_free( nameConv );
+               g_free( nameConv );
+       }
+
+       person = addritem_create_item_person();
+       addritem_person_set_common_name( person, fullName );
+       addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
+       addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
+       addrcache_id_person( pilotFile->addressCache, person );
+
+       extID = g_strdup_printf( "%d", unique_id );
+       addritem_person_set_external_id( person, extID );
+       g_free( extID );
+       extID = NULL;
+
+       /* Add entry for each email address listed under phone labels. */
+       indPhoneLbl = addr.phoneLabel;
+       for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
+               if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
+                       labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
+                       jpilot_parse_label( pilotFile, labelEntry, person );
                }
+       }
 
-               person = addritem_create_item_person();
-               addritem_person_set_common_name( person, fullName );
-               addritem_person_set_first_name( person, addrEnt[ IND_LABEL_FIRSTNAME ] );
-               addritem_person_set_last_name( person, addrEnt[ IND_LABEL_LASTNAME ] );
-               addrcache_id_person( pilotFile->addressCache, person );
-
-               extID = g_strdup_printf( "%d", unique_id );
-               addritem_person_set_external_id( person, extID );
-               g_free( extID );
-               extID = NULL;
-
-               /* Pointer to address metadata. */
-               ai = & pilotFile->addrInfo;
-
-               /* Add entry for each email address listed under phone labels. */
-               indPhoneLbl = addr.phoneLabel;
-               for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
-                       gint ind;
+       /* Add entry for each custom label */
+       node = pilotFile->labelInd;
+       while( node ) {
+               gint ind;
 
-                       ind = indPhoneLbl[k];
+               ind = GPOINTER_TO_INT( node->data );
+               if( ind > -1 ) {
                        /*
-                       * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
-                       * ai->phoneLabels[ind], addrEnt[3+k] );
+                       * g_print( "%d : %20s : %s\n", ind, ai->labels[ind],
+                       * addrEnt[ind] );
                        */
-                       if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
-                               labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
-                               jpilot_parse_label( pilotFile, labelEntry, person );
-                       }
+                       labelEntry = addrEnt[ind];
+                       jpilot_parse_label( pilotFile, labelEntry, person );
                }
 
-               /* Add entry for each custom label */
-               node = pilotFile->labelInd;
-               while( node ) {
-                       gint ind;
-
-                       ind = GPOINTER_TO_INT( node->data );
-                       if( ind > -1 ) {
-                               /*
-                               * fprintf( stdout, "%d : %20s : %s\n", ind, ai->labels[ind],
-                               * addrEnt[ind] );
-                               */
-                               labelEntry = addrEnt[ind];
-                               jpilot_parse_label( pilotFile, labelEntry, person );
-                       }
-
-                       node = g_list_next( node );
-               }
-
-               if( person->listEMail ) {
-                       if( cat_id > -1 && cat_id < JPILOT_NUM_CATEG ) {
-                               /* Add to specified category */
-                               addrcache_folder_add_person(
-                                       pilotFile->addressCache,
-                                       folderInd[cat_id], person );
-                       }
-                       else {
-                               /* Add to root folder */
-                               addrcache_add_person(
-                                       pilotFile->addressCache, person );
-                       }
-               }
-               else {
-                       addritem_free_item_person( person );
-                       person = NULL;
-               }
+               node = g_list_next( node );
        }
 
+       if( person->listEMail ) {
+               /* Add to specified category */
+               addrcache_folder_add_person(
+                       pilotFile->addressCache,
+                       folderInd[cat_id], person );
+       }
+       else {
+               addritem_free_item_person( person );
+               person = NULL;
+       }
        /* Free up pointer allocated inside address */
        free_Address( & addr );
 }
@@ -1296,7 +1255,7 @@ static gint jpilot_read_metadata( JPilotFile *pilotFile ) {
        unsigned char *buf;
        int num;
 
-       g_return_val_if_fail( pilotFile != NULL, -1 );
+       cm_return_val_if_fail( pilotFile != NULL, -1 );
 
        pilotFile->readMetadata = FALSE;
        addrcache_clear( pilotFile->addressCache );
@@ -1332,7 +1291,7 @@ static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
        struct AddressAppInfo *ai;
        GList *node;
 
-       g_return_val_if_fail( pilotFile != NULL, -1 );
+       cm_return_val_if_fail( pilotFile != NULL, -1 );
 
        /* Release indexes */
        node = pilotFile->labelInd;
@@ -1351,15 +1310,15 @@ static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
                        gint i;
                        for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
                                gchar *labelName = ai->labels[i];
-                               gchar convertBuff[ JPILOT_LEN_LABEL ];
 
                                if( convert_charcode ) {
-                                       labelName = conv_codeset_strdup( labelName, 
-                                                       conv_get_locale_charset_str_no_utf8(), 
+                                       gchar *convertBuff = NULL;
+                                       convertBuff = conv_codeset_strdup( labelName, 
+                                                       jpilot_get_charset(), 
                                                        CS_INTERNAL );
-                                       strncpy2( convertBuff, labelName, JPILOT_LEN_LABEL );
-                                       g_free( labelName );
-                                       labelName = convertBuff;
+                                       if (convertBuff) {
+                                               labelName = convertBuff;
+                                       }
                                }
 
                                if( g_utf8_collate( labelName, lbl ) == 0 ) {
@@ -1376,95 +1335,6 @@ static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
        return retVal;
 }
 
-/**
- * Load list with character strings of label names.
- * \param pilotFile  JPilot control data.
- * \param labelList List of label names to load.
- * \return List of label names loaded.
- */
-GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
-       int i;
-
-       g_return_val_if_fail( pilotFile != NULL, NULL );
-
-       if( pilotFile->readMetadata ) {
-               struct AddressAppInfo *ai = & pilotFile->addrInfo;
-               for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
-                       gchar *labelName = ai->labels[i];
-
-                       if( labelName ) {
-                               if( convert_charcode ) {
-                                       labelName = conv_codeset_strdup( labelName, 
-                                                       conv_get_locale_charset_str_no_utf8(), 
-                                                       CS_INTERNAL );
-                               }
-                               else {
-                                       labelName = g_strdup( labelName );
-                               }
-                               labelList = g_list_append( labelList, labelName );
-                       }
-                       else {
-                               labelList = g_list_append(
-                                       labelList, g_strdup( "" ) );
-                       }
-               }
-       }
-       return labelList;
-}
-
-/**
- * Return category name for specified category ID.
- * \param pilotFile  JPilot control data.
- * \param catID      Category ID.
- * \return Category name, or empty string if not invalid ID. Name should be
- *         <code>g_free()</code> when done.
- */
-gchar *jpilot_get_category_name( JPilotFile *pilotFile, gint catID ) {
-       gchar *catName = NULL;
-
-       g_return_val_if_fail( pilotFile != NULL, NULL );
-
-       if( pilotFile->readMetadata ) {
-               struct AddressAppInfo *ai = & pilotFile->addrInfo;
-               struct CategoryAppInfo *cat = & ai->category;
-               if( catID < 0 || catID > JPILOT_NUM_CATEG ) {
-               }
-               else {
-                       catName = g_strdup( cat->name[catID] );
-               }
-       }
-       if( ! catName ) catName = g_strdup( "" );
-       return catName;
-}
-
-/**
- * Load list with character strings of phone label names.
- * \param pilotFile  JPilot control data.
- * \param labelList List of label names to load.
- * \return List of label names loaded.
- */
-GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
-       gint i;
-
-       g_return_val_if_fail( pilotFile != NULL, NULL );
-
-       if( pilotFile->readMetadata ) {
-               struct AddressAppInfo *ai = & pilotFile->addrInfo;
-               for( i = 0; i < JPILOT_NUM_PHONELABELS; i++ ) {
-                       gchar   *labelName = ai->phoneLabels[i];
-                       if( labelName ) {
-                               labelList = g_list_append(
-                                       labelList, g_strdup( labelName ) );
-                       }
-                       else {
-                               labelList = g_list_append(
-                                       labelList, g_strdup( "" ) );
-                       }
-               }
-       }
-       return labelList;
-}
-
 /**
  * Load list with character strings of custom label names. Only none blank
  * names are loaded.
@@ -1475,20 +1345,23 @@ GList *jpilot_load_phone_label( JPilotFile *pilotFile, GList *labelList ) {
 GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
        gint i;
 
-       g_return_val_if_fail( pilotFile != NULL, NULL );
+       cm_return_val_if_fail( pilotFile != NULL, NULL );
 
        if( pilotFile->readMetadata ) {
                struct AddressAppInfo *ai = & pilotFile->addrInfo;
                for( i = 0; i < NUM_CUSTOM_LABEL; i++ ) {
                        gchar *labelName = ai->labels[i+IND_CUSTOM_LABEL];
                        if( labelName ) {
-                               g_strchomp( labelName );
-                               g_strchug( labelName );
+                               g_strstrip( labelName );
                                if( *labelName != '\0' ) {
                                        if( convert_charcode ) {
-                                               labelName = conv_codeset_strdup( labelName, 
-                                                               conv_get_locale_charset_str_no_utf8(), 
+                                               gchar *convertBuff = NULL;
+                                               convertBuff = conv_codeset_strdup( labelName, 
+                                                               jpilot_get_charset(), 
                                                                CS_INTERNAL );
+                                               if (convertBuff) {
+                                                       labelName = convertBuff;
+                                               }
                                        }
                                        else {
                                                labelName = g_strdup( labelName );
@@ -1501,35 +1374,6 @@ GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
        return labelList;
 }
 
-/**
- * Load list with character strings of category names.
- * \param pilotFile  JPilot control data.
- * \return List of label names loaded. Should be freed when done.
- */
-GList *jpilot_get_category_list( JPilotFile *pilotFile ) {
-       GList *catList = NULL;
-       gint i;
-
-       g_return_val_if_fail( pilotFile != NULL, NULL );
-
-       if( pilotFile->readMetadata ) {
-               struct AddressAppInfo *ai = & pilotFile->addrInfo;
-               struct CategoryAppInfo *cat = & ai->category;
-               for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
-                       gchar *catName = cat->name[i];
-                       if( catName ) {
-                               catList = g_list_append(
-                                       catList, g_strdup( catName ) );
-                       }
-                       else {
-                               catList = g_list_append(
-                                       catList, g_strdup( "" ) );
-                       }
-               }
-       }
-       return catList;
-}
-
 /**
  * Build folder in address book for each category.
  * \param pilotFile  JPilot control data.
@@ -1543,12 +1387,16 @@ static void jpilot_build_category_list( JPilotFile *pilotFile ) {
                ItemFolder *folder = addritem_create_item_folder();
 
                if( convert_charcode ) {
-                       gchar *catName;
-                       catName = conv_codeset_strdup( cat->name[i], 
-                                       conv_get_locale_charset_str_no_utf8(), 
+                       gchar *convertBuff = NULL;
+                       convertBuff = conv_codeset_strdup( cat->name[i], 
+                                       jpilot_get_charset(), 
                                        CS_INTERNAL );
-                       addritem_folder_set_name( folder, catName );
-                       g_free( catName );
+                       if (convertBuff) {
+                               addritem_folder_set_name( folder, convertBuff );
+                               g_free( convertBuff );
+                       } else {
+                               addritem_folder_set_name( folder, cat->name[i] );
+                       }
                }
                else {
                        addritem_folder_set_name( folder, cat->name[i] );
@@ -1672,11 +1520,15 @@ gint jpilot_read_data( JPilotFile *pilotFile ) {
 
        cur_locale = conv_get_current_locale();
 
-       if( g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ) {
+       if( g_ascii_strncasecmp( cur_locale, "hu", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "ko", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "vi", 2 ) == 0 ||
+               g_ascii_strncasecmp( cur_locale, "zh", 2 ) == 0 ) {
                name_order = FAMILY_FIRST;
        }
 
-       g_return_val_if_fail( pilotFile != NULL, -1 );
+       cm_return_val_if_fail( pilotFile != NULL, -1 );
 
        pilotFile->retVal = MGU_SUCCESS;
        pilotFile->addressCache->accessFlag = FALSE;
@@ -1703,7 +1555,7 @@ gint jpilot_read_data( JPilotFile *pilotFile ) {
  * \return List of persons.
  */
 GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, NULL );
+       cm_return_val_if_fail( pilotFile != NULL, NULL );
        return addrcache_get_list_person( pilotFile->addressCache );
 }
 
@@ -1717,7 +1569,7 @@ GList *jpilot_get_list_person( JPilotFile *pilotFile ) {
  * \return List of ItemFolder objects. This should not be freed.
  */
 GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, NULL );
+       cm_return_val_if_fail( pilotFile != NULL, NULL );
        return addrcache_get_list_folder( pilotFile->addressCache );
 }
 
@@ -1730,38 +1582,10 @@ GList *jpilot_get_list_folder( JPilotFile *pilotFile ) {
  * \return List of items, or NULL if none.
  */
 GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
-       g_return_val_if_fail( pilotFile != NULL, NULL );
+       cm_return_val_if_fail( pilotFile != NULL, NULL );
        return addrcache_get_all_persons( pilotFile->addressCache );
 }
 
-/**
- * Validate that all parameters specified.
- * \param pilotFile  JPilot control data.
- * \return <i>TRUE</i> if data is good.
- */
-gboolean jpilot_validate( JPilotFile *pilotFile ) {
-       gboolean retVal;
-       gchar *name;
-
-       g_return_val_if_fail( pilotFile != NULL, FALSE );
-
-       retVal = TRUE;
-       if( pilotFile->path ) {
-               if( strlen( pilotFile->path ) < 1 ) retVal = FALSE;
-       }
-       else {
-               retVal = FALSE;
-       }
-       name = jpilot_get_name( pilotFile );
-       if( name ) {
-               if( strlen( name ) < 1 ) retVal = FALSE;
-       }
-       else {
-               retVal = FALSE;
-       }
-       return retVal;
-}
-
 #define WORK_BUFLEN 1024
 
 /**
@@ -1772,14 +1596,14 @@ gboolean jpilot_validate( JPilotFile *pilotFile ) {
  */
 gchar *jpilot_find_pilotdb( void ) {
        const gchar *homedir;
-       gchar str[ WORK_BUFLEN ];
+       gchar str[ WORK_BUFLEN + 1 ];
        gint len;
        FILE *fp;
 
        homedir = get_home_dir();
        if( ! homedir ) return g_strdup( "" );
 
-       strcpy( str, homedir );
+       g_strlcpy( str, homedir , sizeof(str));
        len = strlen( str );
        if( len > 0 ) {
                if( str[ len-1 ] != G_DIR_SEPARATOR ) {
@@ -1787,9 +1611,9 @@ gchar *jpilot_find_pilotdb( void ) {
                        str[ ++len ] = '\0';
                }
        }
-       strcat( str, JPILOT_DBHOME_DIR );
-       strcat( str, G_DIR_SEPARATOR_S );
-       strcat( str, JPILOT_DBHOME_FILE );
+       strncat( str, JPILOT_DBHOME_DIR, WORK_BUFLEN - strlen(str) );
+       strncat( str, G_DIR_SEPARATOR_S, WORK_BUFLEN - strlen(str) );
+       strncat( str, JPILOT_DBHOME_FILE, WORK_BUFLEN - strlen(str) );
 
        /* Attempt to open */
        if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
@@ -1802,27 +1626,6 @@ gchar *jpilot_find_pilotdb( void ) {
        return g_strdup( str );
 }
 
-/**
- * Attempt to read file, testing for valid JPilot format.
- * \param fileSpec File specification to read.
- * \return <i>TRUE<i> if file appears to be valid format.
- */
-gint jpilot_test_read_file( const gchar *fileSpec ) {
-       JPilotFile *pilotFile;
-       gint retVal;
-
-       if( fileSpec ) {
-               pilotFile = jpilot_create_path( fileSpec );
-               retVal = jpilot_read_metadata( pilotFile );
-               jpilot_free( pilotFile );
-               pilotFile = NULL;
-       }
-       else {
-               retVal = MGU_NO_FILE;
-       }
-       return retVal;
-}
-
 /**
  * Check whether label is in list of custom labels.
  * \param pilotFile JPilot control data.
@@ -1833,7 +1636,7 @@ gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName
        gboolean retVal;
        GList *node;
 
-       g_return_val_if_fail( pilotFile != NULL, FALSE );
+       cm_return_val_if_fail( pilotFile != NULL, FALSE );
 
        retVal = FALSE;
        if( labelName ) {