2006-11-01 [colin] 2.5.6cvs18
[claws.git] / src / jpilot.c
index 4af264962f4f9beb388dd1cbdefa407af4a04ec2..f3d43142a9b39c32ace2a436fed2106c338a1895 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001-2003 Match Grun
+ * Copyright (C) 2001-2006 Match Grun and the Sylpheed-Claws 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
@@ -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.
  */
 
 /*
 #  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"
 #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 +151,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.
 */
@@ -629,7 +640,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 +694,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 +726,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 +835,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 +965,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 +1101,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,92 +1144,136 @@ static void jpilot_load_address(
        GList *node;
        gchar* extID;
        struct AddressAppInfo *ai;
+       gchar **firstName = NULL;
+       gchar **lastName = NULL;
+#if (PILOT_LINK_MAJOR > 11)
+       pi_buffer_t *RecordBuffer;
+#endif /* PILOT_LINK_0_12 */
 
        /* 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;
-
-               *fullName = '\0';
-               if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
-                       strcat( fullName, addrEnt[ IND_LABEL_FIRSTNAME ] );
-               }
+#if (PILOT_LINK_MAJOR < 12)
+       num = unpack_Address(&addr, buf->buf, buf->size);
+       if (num <= 0) {
+               return;
+       }
+#else /* PILOT_LINK_0_12 */
+       RecordBuffer = pi_buffer_new(buf->size);
+       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);
+#endif
+       addrEnt = addr.entry;
+       attrib = buf->attrib;
+       unique_id = buf->unique_id;
+       cat_id = attrib & 0x0F;
 
-               if( addrEnt[ IND_LABEL_LASTNAME ] ) {
-                       strcat( fullName, " " );
-                       strcat( fullName, addrEnt[ IND_LABEL_LASTNAME ] );
-               }
-               g_strchug( fullName );
-               g_strchomp( fullName );
+       *fullName = '\0';
+       if( addrEnt[ IND_LABEL_FIRSTNAME ] ) {
+               firstName = g_strsplit( addrEnt[ IND_LABEL_FIRSTNAME ], "\01", 2 );
+       }
 
-               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 );
+       if( addrEnt[ IND_LABEL_LASTNAME ] ) {
+               lastName = g_strsplit( addrEnt[ IND_LABEL_LASTNAME ], "\01", 2 );
+       }
 
-               extID = g_strdup_printf( "%d", unique_id );
-               addritem_person_set_external_id( person, extID );
-               g_free( extID );
-               extID = NULL;
+       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] : "" );
+       }
 
-               /* Pointer to address metadata. */
-               ai = & pilotFile->addrInfo;
+       if( firstName ) {
+               g_strfreev( firstName );
+       }
+       if( lastName ) {
+               g_strfreev( lastName );
+       }
 
-               /* Add entry for each email address listed under phone labels. */
-               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,
-                       * ai->phoneLabels[ind], addrEnt[3+k] );
-                       */
-                       if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
-                               labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
-                               jpilot_parse_label( pilotFile, labelEntry, person );
-                       }
+       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 );
+       }
+
+       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;
+
+               ind = indPhoneLbl[k];
+               /*
+               * fprintf( stdout, "%d : %d : %20s : %s\n", k, ind,
+               * ai->phoneLabels[ind], addrEnt[3+k] );
+               */
+               if( indPhoneLbl[k] == IND_PHONE_EMAIL ) {
+                       labelEntry = addrEnt[ OFFSET_PHONE_LABEL + k ];
+                       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 );
-                       }
+       /* Add entry for each custom label */
+       node = pilotFile->labelInd;
+       while( node ) {
+               gint ind;
 
-                       node = g_list_next( node );
+               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 );
                }
 
-               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 );
-                       }
+               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 {
-                       addritem_free_item_person( person );
-                       person = NULL;
+                       /* Add to root folder */
+                       addrcache_add_person(
+                               pilotFile->addressCache, person );
                }
        }
-
+       else {
+               addritem_free_item_person( person );
+               person = NULL;
+       }
        /* Free up pointer allocated inside address */
        free_Address( & addr );
 }
@@ -1298,6 +1366,17 @@ 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(), 
+                                                       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 +1406,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 +1500,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 +1556,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 +1681,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 +1791,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 +1807,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 {