2005-10-26 [colin] 1.9.15cvs111
[claws.git] / src / jpilot.c
index 5d86f878b18ccf7683bb77ce8c0e1cc59b4ca9c9..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 );
@@ -574,31 +583,6 @@ static void free_mem_rec_header(mem_rec_header **mem_rh) {
        *mem_rh = NULL;
 }
 
-/* Shamelessly copied from JPilot (libplugin.c) */
-static int jpilot_free_db_list( GList **br_list ) {
-       GList *temp_list, *first;
-       buf_rec *br;
-
-       /* Go to first entry in the list */
-       first=NULL;
-       for( temp_list = *br_list; temp_list; temp_list = temp_list->prev ) {
-               first = temp_list;
-       }
-       for (temp_list = first; temp_list; temp_list = temp_list->next) {
-               if (temp_list->data) {
-                       br=temp_list->data;
-                       if (br->buf) {
-                               free(br->buf);
-                               temp_list->data=NULL;
-                       }
-                       free(br);
-               }
-       }
-       g_list_free(*br_list);
-       *br_list=NULL;
-       return 0;
-}
-
 /* Shamelessly copied from JPilot (libplugin.c) */
 /* Read file size */
 static int jpilot_get_info_size( FILE *in, int *size ) {
@@ -654,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;
                }
@@ -708,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;
 
@@ -740,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;
 
@@ -848,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;
        }
 
@@ -978,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 ) {
@@ -1114,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 );
@@ -1145,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 );
@@ -1155,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 );
@@ -1184,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,
@@ -1199,6 +1226,7 @@ static void jpilot_load_address(
                node = pilotFile->labelInd;
                while( node ) {
                        gint ind;
+
                        ind = GPOINTER_TO_INT( node->data );
                        if( ind > -1 ) {
                                /*
@@ -1323,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;
                                }
@@ -1352,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(
@@ -1438,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 );
                                }
                        }
                }
@@ -1487,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 );
        }
@@ -1600,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;
@@ -1658,30 +1734,6 @@ GList *jpilot_get_all_persons( JPilotFile *pilotFile ) {
        return addrcache_get_all_persons( pilotFile->addressCache );
 }
 
-/**
- * Check label list for specified label name.
- * \param ai Address info to check.
- * \param lblCheck Label name to check.
- * \return Index of label in address structure. <code>-1</code> if NULL/empty
- *         label name, <code>-2</code> if label not found.
-*/
-static gint jpilot_check_label( struct AddressAppInfo *ai, gchar *lblCheck ) {
-       gint i;
-       gchar *lblName;
-
-       if( lblCheck == NULL ) return -1;
-       if( strlen( lblCheck ) < 1 ) return -1;
-       for( i = 0; i < JPILOT_NUM_LABELS; i++ ) {
-               lblName = ai->labels[i];
-               if( lblName ) {
-                       if( strlen( lblName ) ) {
-                               if( g_strcasecmp( lblName, lblCheck ) == 0 ) return i;
-                       }
-               }
-       }
-       return -2;
-}
-
 /**
  * Validate that all parameters specified.
  * \param pilotFile  JPilot control data.
@@ -1719,12 +1771,12 @@ gboolean jpilot_validate( JPilotFile *pilotFile ) {
  *         no home. Filename should be <code>g_free()</code> when done.
  */
 gchar *jpilot_find_pilotdb( void ) {
-       gchar *homedir;
+       const gchar *homedir;
        gchar str[ WORK_BUFLEN ];
        gint len;
        FILE *fp;
 
-       homedir = g_get_home_dir();
+       homedir = get_home_dir();
        if( ! homedir ) return g_strdup( "" );
 
        strcpy( str, homedir );
@@ -1740,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 {
@@ -1787,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;
                        }