2005-10-26 [colin] 1.9.15cvs111
[claws.git] / src / jpilot.c
index b639fe3f4679e2ce586e57f97cf359f370dd918e..5e67c961c605114b282900677588c97a41c0bbf2 100644 (file)
@@ -14,7 +14,7 @@
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 /*
@@ -51,7 +51,9 @@
 #include "addritem.h"
 #include "addrcache.h"
 #include "jpilot.h"
+#include "codeconv.h"
 #include "adbookbase.h"
+#include "utils.h"
 
 #define JPILOT_DBHOME_DIR   ".jpilot"
 #define JPILOT_DBHOME_FILE  "AddressDB.pdb"
@@ -147,7 +149,14 @@ typedef struct {
        unsigned char attrib;
 } PC3RecordHeader;
 
-/**
+enum {
+       FAMILY_LAST = 0,
+       FAMILY_FIRST = 1
+} name_order;
+
+gboolean convert_charcode = TRUE;
+
+/*
 * Create new pilot file object.
 * \return Initialized JPilot file object.
 */
@@ -339,7 +348,7 @@ static gboolean jpilot_mark_files( JPilotFile *pilotFile ) {
        pilotFile->pc3ModifyTime = 0;
        pcFile = jpilot_get_pc3_file( pilotFile );
        if( pcFile == NULL ) return retVal;
-       if( 0 == lstat( pcFile, &filestat ) ) {
+       if( 0 == stat( pcFile, &filestat ) ) {
                pilotFile->havePC3 = TRUE;
                pilotFile->pc3ModifyTime = filestat.st_mtime;
                retVal = TRUE;
@@ -368,7 +377,7 @@ static gboolean jpilot_check_files( JPilotFile *pilotFile ) {
        pcFile = jpilot_get_pc3_file( pilotFile );
        if( pcFile == NULL ) return FALSE;
 
-       if( 0 == lstat( pcFile, &filestat ) ) {
+       if( 0 == stat( pcFile, &filestat ) ) {
                if( filestat.st_mtime == pilotFile->pc3ModifyTime ) retVal = FALSE;
        }
        g_free( pcFile );
@@ -629,7 +638,7 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
        *buf_size=0;
 
        if( pilotFile->path ) {
-               in = fopen( pilotFile->path, "rb" );
+               in = g_fopen( pilotFile->path, "rb" );
                if( !in ) {
                        return MGU_OPEN_FILE;
                }
@@ -683,7 +692,7 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
 /* Shamelessly copied from JPilot (libplugin.c) */
 static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header) {
        unsigned char *p;
-       unsigned long l;
+       guint32 l;
 
        p = packed_header;
 
@@ -715,7 +724,8 @@ static int unpack_header(PC3RecordHeader *header, unsigned char *packed_header)
 
 /* Shamelessly copied from JPilot (libplugin.c) */
 static int read_header(FILE *pc_in, PC3RecordHeader *header) {
-       unsigned long l, len;
+       guint32 l;
+       unsigned long len;
        unsigned char packed_header[256];
        int num;
 
@@ -823,8 +833,8 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                return MGU_BAD_ARGS;
        }
 
-       in = fopen( pilotFile->path, "rb" );
-       if( ! in ) {
+       in = g_fopen( pilotFile->path, "rb" );
+       if (!in) {
                return MGU_OPEN_FILE;
        }
 
@@ -953,7 +963,7 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
        /* Read the PC3 file, if present */
        pcFile = jpilot_get_pc3_file( pilotFile );
        if( pcFile == NULL ) return MGU_SUCCESS;
-       pc_in = fopen( pcFile, "rb" );
+       pc_in = g_fopen( pcFile, "rb");
        g_free( pcFile );
 
        if( pc_in == NULL ) {
@@ -1089,6 +1099,18 @@ static void jpilot_parse_label( JPilotFile *pilotFile, gchar *labelEntry, ItemPe
                while( node ) {
                        email = addritem_create_item_email();
                        addritem_email_set_address( email, node->data );
+                       if (convert_charcode) {
+                               gchar *convertBuff;
+                               convertBuff = conv_codeset_strdup( labelEntry, 
+                                               conv_get_locale_charset_str_no_utf8(), 
+                                               CS_INTERNAL );
+                               addritem_email_set_remarks( email, convertBuff );
+                               g_free( convertBuff );
+                       }
+                       else {
+                               addritem_email_set_remarks(email, buffer);
+                       }
+
                        addrcache_id_email( pilotFile->addressCache, email );
                        addrcache_person_add_email( pilotFile->addressCache, person, email );
                        node = g_list_next( node );
@@ -1120,6 +1142,8 @@ static void jpilot_load_address(
        GList *node;
        gchar* extID;
        struct AddressAppInfo *ai;
+       gchar **firstName = NULL;
+       gchar **lastName = NULL;
 
        /* Retrieve address */
        num = unpack_Address( & addr, buf->buf, buf->size );
@@ -1130,16 +1154,43 @@ static void jpilot_load_address(
                cat_id = attrib & 0x0F;
 
                *fullName = '\0';
+
                if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
-                       strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
+                       firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
                }
 
                if( addrEnt[ IND_LABEL_LASTNAME ] ) {
-                       strcat( fullName, " " );
-                       strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
+                       lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\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( firstName ) {
+                       g_strfreev( firstName );
+               }
+               if( lastName ) {
+                       g_strfreev( lastName );
+               }
+
+               g_strstrip( fullName );
+
+               if( convert_charcode ) {
+                       gchar *nameConv;
+                       nameConv = conv_codeset_strdup( fullName, 
+                                       conv_get_locale_charset_str_no_utf8(), 
+                                       CS_INTERNAL );
+                       strncpy2( fullName, nameConv, FULLNAME_BUFSIZE );
+                       g_free( nameConv );
                }
-               g_strchug( fullName );
-               g_strchomp( fullName );
 
                person = addritem_create_item_person();
                addritem_person_set_common_name( person, fullName );
@@ -1159,6 +1210,7 @@ static void jpilot_load_address(
                indPhoneLbl = addr.phoneLabel;
                for( k = 0; k < JPILOT_NUM_ADDR_PHONE; k++ ) {
                        gint ind;
+
                        ind = indPhoneLbl[k];
                        /*
                        * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
@@ -1174,6 +1226,7 @@ static void jpilot_load_address(
                node = pilotFile->labelInd;
                while( node ) {
                        gint ind;
+
                        ind = GPOINTER_TO_INT( node->data );
                        if( ind > -1 ) {
                                /*
@@ -1298,7 +1351,18 @@ static gboolean jpilot_setup_labels( JPilotFile *pilotFile ) {
                        gint i;
                        for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
                                gchar *labelName = ai->labels[i];
-                               if( g_strcasecmp( labelName, lbl ) == 0 ) {
+                               gchar convertBuff[ JPILOT_LEN_LABEL ];
+
+                               if( convert_charcode ) {
+                                       labelName = conv_codeset_strdup( labelName, 
+                                                       conv_get_locale_charset_str_no_utf8(), 
+                                                       CS_INTERNAL );
+                                       strncpy2( convertBuff, labelName, JPILOT_LEN_LABEL );
+                                       g_free( labelName );
+                                       labelName = convertBuff;
+                               }
+
+                               if( g_utf8_collate( labelName, lbl ) == 0 ) {
                                        ind = i;
                                        break;
                                }
@@ -1327,9 +1391,17 @@ GList *jpilot_load_label( JPilotFile *pilotFile, GList *labelList ) {
                struct AddressAppInfo *ai = & pilotFile->addrInfo;
                for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
                        gchar *labelName = ai->labels[i];
+
                        if( labelName ) {
-                               labelList = g_list_append(
-                                       labelList, g_strdup( 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(
@@ -1413,8 +1485,15 @@ GList *jpilot_load_custom_label( JPilotFile *pilotFile, GList *labelList ) {
                                g_strchomp( labelName );
                                g_strchug( labelName );
                                if( *labelName != '\0' ) {
-                                       labelList = g_list_append( labelList,
-                                               g_strdup( 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 );
                                }
                        }
                }
@@ -1462,7 +1541,19 @@ static void jpilot_build_category_list( JPilotFile *pilotFile ) {
 
        for( i = 0; i < JPILOT_NUM_CATEG; i++ ) {
                ItemFolder *folder = addritem_create_item_folder();
-               addritem_folder_set_name( folder, cat->name[i] );
+
+               if( convert_charcode ) {
+                       gchar *catName;
+                       catName = conv_codeset_strdup( cat->name[i], 
+                                       conv_get_locale_charset_str_no_utf8(), 
+                                       CS_INTERNAL );
+                       addritem_folder_set_name( folder, catName );
+                       g_free( catName );
+               }
+               else {
+                       addritem_folder_set_name( folder, cat->name[i] );
+               }
+
                addrcache_id_folder( pilotFile->addressCache, folder );
                addrcache_add_folder( pilotFile->addressCache, folder );
        }
@@ -1575,6 +1666,16 @@ static gint jpilot_read_file( JPilotFile *pilotFile ) {
  *         successfully.
  */
 gint jpilot_read_data( JPilotFile *pilotFile ) {
+       const gchar *cur_locale;
+
+       name_order = FAMILY_LAST;
+
+       cur_locale = conv_get_current_locale();
+
+       if( g_ascii_strncasecmp( cur_locale, "ja", 2 ) == 0 ) {
+               name_order = FAMILY_FIRST;
+       }
+
        g_return_val_if_fail( pilotFile != NULL, -1 );
 
        pilotFile->retVal = MGU_SUCCESS;
@@ -1675,7 +1776,7 @@ gchar *jpilot_find_pilotdb( void ) {
        gint len;
        FILE *fp;
 
-       homedir = g_get_home_dir();
+       homedir = get_home_dir();
        if( ! homedir ) return g_strdup( "" );
 
        strcpy( str, homedir );
@@ -1691,7 +1792,7 @@ gchar *jpilot_find_pilotdb( void ) {
        strcat( str, JPILOT_DBHOME_FILE );
 
        /* Attempt to open */
-       if( ( fp = fopen( str, "rb" ) ) != NULL ) {
+       if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
                fclose( fp );
        }
        else {
@@ -1738,7 +1839,7 @@ gboolean jpilot_test_custom_label( JPilotFile *pilotFile, const gchar *labelName
        if( labelName ) {
                node = pilotFile->customLabels;
                while( node ) {
-                       if( g_strcasecmp( labelName, ( gchar * ) node->data ) == 0 ) {
+                       if( g_utf8_collate( labelName, ( gchar * ) node->data ) == 0 ) {
                                retVal = TRUE;
                                break;
                        }