]> granicus.if.org Git - transmission/commitdiff
(trunk, qt) #4760 'transmission-qt breaks unicode characters in the default target...
authorJordan Lee <jordan@transmissionbt.com>
Sun, 19 Aug 2012 16:12:20 +0000 (16:12 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Sun, 19 Aug 2012 16:12:20 +0000 (16:12 +0000)
qt/prefs.cc
qt/prefs.h
qt/session.cc
qt/utils.cc
qt/utils.h

index cf6f8cdf6ca8631757a100c16766f772e7464a5d..cf2cc25faefd78723f8e4ae19ced32746c24b504 100644 (file)
@@ -12,7 +12,6 @@
 
 #include <cassert>
 #include <iostream>
-#include <cstdlib> // strtod
 
 #include <QDir>
 #include <QFile>
 #include <libtransmission/bencode.h>
 #include <libtransmission/json.h>
 #include <libtransmission/utils.h>
+#include <stdlib.h>
 #include "prefs.h"
 #include "types.h"
+#include "utils.h"
 
 /***
 ****
@@ -125,7 +126,7 @@ Prefs::PrefItem Prefs::myItems[] =
 ***/
 
 Prefs :: Prefs( const char * configDir ):
-    myConfigDir( configDir )
+    myConfigDir( QString::fromUtf8( configDir ) )
 {
     assert( sizeof(myItems) / sizeof(myItems[0]) == PREFS_COUNT );
     for( int i=0; i<PREFS_COUNT; ++i )
@@ -163,7 +164,7 @@ Prefs :: Prefs( const char * configDir ):
                 break;
             case QVariant::String:
                 if( tr_bencGetStr( b, &str ) )
-                    myValues[i].setValue( QString::fromUtf8(str) );
+                    myValues[i].setValue( QString::fromUtf8( str ) );
                 break;
             case QVariant::Bool:
                 if( tr_bencGetBool( b, &boolVal ) )
@@ -219,7 +220,12 @@ Prefs :: ~Prefs( )
                 tr_bencDictAddStr( &top, key, val.value<FilterMode>().name().toUtf8().constData() );
                 break;
             case QVariant::String:
-                tr_bencDictAddStr( &top, key, val.toString().toUtf8().constData() );
+                {   const char * s = val.toByteArray().constData();
+                    if ( Utils::isValidUtf8( s ) )
+                        tr_bencDictAddStr( &top, key, s );
+                    else
+                        tr_bencDictAddStr( &top, key, val.toString().toUtf8().constData() );
+                }
                 break;
             case QVariant::Bool:
                 tr_bencDictAddBool( &top, key, val.toBool() );
@@ -253,7 +259,7 @@ Prefs :: initDefaults( tr_benc * d )
     tr_bencDictAddBool( d, keyStr(INHIBIT_HIBERNATION), false );
     tr_bencDictAddInt ( d, keyStr(BLOCKLIST_DATE), 0 );
     tr_bencDictAddBool( d, keyStr(BLOCKLIST_UPDATES_ENABLED), true );
-    tr_bencDictAddStr ( d, keyStr(OPEN_DIALOG_FOLDER), QDir::home().absolutePath().toLatin1() );
+    tr_bencDictAddStr ( d, keyStr(OPEN_DIALOG_FOLDER), QDir::home().absolutePath().toUtf8() );
     tr_bencDictAddInt ( d, keyStr(SHOW_TRACKER_SCRAPES), false );
     tr_bencDictAddBool( d, keyStr(TOOLBAR), true );
     tr_bencDictAddBool( d, keyStr(FILTERBAR), true );
@@ -297,7 +303,10 @@ QString
 Prefs :: getString( int key ) const
 {
     assert( myItems[key].type == QVariant::String );
-    return myValues[key].toString( );
+    QByteArray b = myValues[key].toByteArray();
+    if ( Utils::isValidUtf8( b.constData() ) )
+       myValues[key].setValue( QString::fromUtf8( b.constData() ) );
+    return myValues[key].toString();
 }
 
 int
index 74d97b03883679433efb8f3f8e15f36f6d7b79a1..6bee82b1de96d9982547abb5d70709fc04f54d40 100644 (file)
@@ -141,7 +141,7 @@ class Prefs: public QObject
     private:
         QSet<int> myTemporaryPrefs;
         QString myConfigDir;
-        QVariant myValues[PREFS_COUNT];
+        mutable QVariant myValues[PREFS_COUNT];
         void initDefaults( struct tr_benc* );
 
     public:
index bd67f8142d8c96e979468974910c00d44ec540e3..d5bb61f92bc78add842fb4af37fe611557f5abfa 100644 (file)
@@ -101,7 +101,7 @@ Session :: sessionSet( const char * key, const QVariant& value )
         case QVariant::Bool:   tr_bencDictAddBool ( args, key, value.toBool() ); break;
         case QVariant::Int:    tr_bencDictAddInt  ( args, key, value.toInt() ); break;
         case QVariant::Double: tr_bencDictAddReal ( args, key, value.toDouble() ); break;
-        case QVariant::String: tr_bencDictAddStr  ( args, key, value.toString().toUtf8() ); break;
+        case QVariant::String: tr_bencDictAddStr  ( args, key, value.toString().toUtf8().constData() ); break;
         default: assert( "unknown type" );
     }
     exec( &top );
@@ -245,7 +245,7 @@ Session :: Session( const char * configDir, Prefs& prefs ):
     myBlocklistSize( -1 ),
     myPrefs( prefs ),
     mySession( 0 ),
-    myConfigDir( configDir ),
+    myConfigDir( QString::fromUtf8( configDir ) ),
     myNAM( 0 )
 {
     myStats.ratio = TR_RATIO_NA;
index 0b3a5d968fd83cd13f6a164326cfb866c2b38a4b..c92ed4d3658204b483e0889d205ce4f554596f72 100644 (file)
@@ -117,3 +117,25 @@ Utils :: guessMimeIcon( const QString& filename )
 
     return fallback;
 }
+
+bool
+Utils :: isValidUtf8 ( const char *s )
+{
+    int n;  // number of bytes in a UTF-8 sequence
+
+    for ( const char *c = s;  *c;  c += n )
+    {
+        if ( (*c & 0x80) == 0x00 )    n = 1;        // ASCII
+        else if ((*c & 0xc0) == 0x80) return false; // not valid
+        else if ((*c & 0xe0) == 0xc0) n = 2;
+        else if ((*c & 0xf0) == 0xe0) n = 3;
+        else if ((*c & 0xf8) == 0xf0) n = 4;
+        else if ((*c & 0xfc) == 0xf8) n = 5;
+        else if ((*c & 0xfe) == 0xfc) n = 6;
+        else return false;
+        for ( int m = 1; m < n; m++ )
+            if ( (c[m] & 0xc0) != 0x80 )
+                return false;
+    } 
+    return true;
+}
index 05658ebac36766fbc61a0265c665edc97dc49593..9af8100f07e242f176101677491ac8c764e91995 100644 (file)
@@ -32,6 +32,8 @@ class Utils: public QObject
     public:
         static QString remoteFileChooser( QWidget * parent, const QString& title, const QString& myPath, bool dir, bool local );
         static const QIcon& guessMimeIcon( const QString& filename );
+        // Test if string is UTF-8 or not
+        static bool isValidUtf8 ( const char *s );
 
         // meh
         static void toStderr( const QString& qstr );