From 29c8a74dcfdb4953a6feb89b47d06379636ed3e6 Mon Sep 17 00:00:00 2001 From: Frank Osterfeld Date: Mon, 11 Nov 2013 20:01:22 +0100 Subject: [PATCH] Store enum values as string Do not rely on int value and casting back ints to enum values. Also actually set the data type accordingly in ReadPasswordJob. --- keychain_dbus.cpp | 41 ++++++++++++++++++++++++++++++++++++----- keychain_p.h | 4 ++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/keychain_dbus.cpp b/keychain_dbus.cpp index f415cd2..db711b7 100644 --- a/keychain_dbus.cpp +++ b/keychain_dbus.cpp @@ -251,11 +251,14 @@ void ReadPasswordJobPrivate::fallbackOnError(const QDBusError& err ) { QScopedPointer local( !q->settings() ? new QSettings( q->service() ) : 0 ); QSettings* actual = q->settings() ? q->settings() : local.data(); - WritePasswordJobPrivate::Mode mode; if ( q->insecureFallback() && actual->contains( dataKey( key ) ) ) { - mode = (WritePasswordJobPrivate::Mode)actual->value( typeKey( key ) ).toInt(); + const WritePasswordJobPrivate::Mode mode = WritePasswordJobPrivate::stringToMode( actual->value( typeKey( key ) ).toString() ); + if (mode == WritePasswordJobPrivate::Binary) + dataType = Binary; + else + dataType = Text; data = actual->value( dataKey( key ) ).toByteArray(); q->emitFinished(); @@ -273,7 +276,6 @@ void ReadPasswordJobPrivate::kwalletOpenFinished( QDBusPendingCallWatcher* watch QScopedPointer local( !q->settings() ? new QSettings( q->service() ) : 0 ); QSettings* actual = q->settings() ? q->settings() : local.data(); - WritePasswordJobPrivate::Mode mode; if ( reply.isError() ) { fallbackOnError( reply.error() ); @@ -285,7 +287,7 @@ void ReadPasswordJobPrivate::kwalletOpenFinished( QDBusPendingCallWatcher* watch // Do the migration data = actual->value( dataKey( key ) ).toByteArray(); - mode = (WritePasswordJobPrivate::Mode)actual->value( typeKey( key ) ).toInt(); + const WritePasswordJobPrivate::Mode mode = WritePasswordJobPrivate::stringToMode( actual->value( typeKey( key ) ).toString() ); actual->remove( key ); q->emitFinished(); @@ -417,6 +419,35 @@ void WritePasswordJobPrivate::scheduledStart() { } } +QString WritePasswordJobPrivate::modeToString(Mode m) +{ + switch (m) { + case Delete: + return QLatin1String("Delete"); + case Text: + return QLatin1String("Text"); + case Binary: + return QLatin1String("Binary"); + } + + Q_ASSERT_X(false, Q_FUNC_INFO, "Unhandled Mode value"); + return QString(); +} + +WritePasswordJobPrivate::Mode WritePasswordJobPrivate::stringToMode(const QString& s) +{ + if (s == QLatin1String("Delete") || s == QLatin1String("0")) + return Delete; + if (s == QLatin1String("Text") || s == QLatin1String("1")) + return Text; + if (s == QLatin1String("Binary") || s == QLatin1String("2")) + return Binary; + + qCritical("Unexpected mode string '%1'", qPrintable(s)); + + return Text; +} + void WritePasswordJobPrivate::fallbackOnError(const QDBusError &err) { QScopedPointer local( !q->settings() ? new QSettings( q->service() ) : 0 ); @@ -435,7 +466,7 @@ void WritePasswordJobPrivate::fallbackOnError(const QDBusError &err) return; } - actual->setValue( QString::fromLatin1( "%1/type" ).arg( key ), (int)mode ); + actual->setValue( QString::fromLatin1( "%1/type" ).arg( key ), mode ); if ( mode == Text ) actual->setValue( QString::fromLatin1( "%1/data" ).arg( key ), textData.toUtf8() ); else if ( mode == Binary ) diff --git a/keychain_p.h b/keychain_p.h index 34cc165..95946a0 100644 --- a/keychain_p.h +++ b/keychain_p.h @@ -97,6 +97,10 @@ public: Text, Binary }; + + static QString modeToString(Mode m); + static Mode stringToMode(const QString& s); + WritePasswordJob* const q; Mode mode; QString key;