[vlmc-devel] Settings: Use Base64 to save QByteArray

Yikai Lu git at videolan.org
Mon May 2 13:13:47 CEST 2016


vlmc | branch: master | Yikai Lu <luyikei.qmltu at gmail.com> | Fri Apr 29 17:05:43 2016 +0900| [f67a7e53a6529994fe8b1276893c27e7f96bb72e] | committer: Hugo Beauzée-Luyssen

Settings: Use Base64 to save QByteArray

"Note: A QByteArray can store any byte values including '\0's, but most functions that take char * arguments assume that the data ends at the first '\0' they encounter."

Because of the above, we can't save QByteArray directly.

Signed-off-by: Hugo Beauzée-Luyssen <hugo at beauzee.fr>

> https://code.videolan.org/videolan/vlmc/commit/f67a7e53a6529994fe8b1276893c27e7f96bb72e
---

 src/Settings/Settings.cpp | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/Settings/Settings.cpp b/src/Settings/Settings.cpp
index b8cef75..2092d59 100644
--- a/src/Settings/Settings.cpp
+++ b/src/Settings/Settings.cpp
@@ -159,8 +159,14 @@ Settings::loadJsonFrom( const QJsonObject &object )
         if ( isChildSettings == true )
             continue;
 
-        if ( setValue( it.key(), (*it).toVariant() ) == false )
+        SettingValue* val = value( it.key() );
+        if ( val == nullptr )
             vlmcWarning() << "Loaded invalid project setting:" << it.key();
+
+        if ( val->type() == SettingValue::ByteArray )
+            val->set( QByteArray::fromBase64( (*it).toVariant().toByteArray() ) );
+        else
+            val->set( (*it).toVariant() );
     }
     emit postLoad();
 }
@@ -173,7 +179,10 @@ Settings::saveJsonTo( QJsonObject &object )
     {
         if ( ( val->flags() & SettingValue::Runtime ) != 0 )
             continue ;
-        object.insert( val->key(), QJsonValue::fromVariant( val->get() ) );
+        if ( val->type() == SettingValue::ByteArray )
+            object.insert( val->key(), QJsonValue( QString( val->get().toByteArray().toBase64() ) ) );
+        else
+            object.insert( val->key(), QJsonValue::fromVariant( val->get() ) );
     }
 }
 



More information about the Vlmc-devel mailing list