UI: RegKey: Refactor setValue()

This commit is contained in:
Nodir Temirkhodjaev 2023-07-11 12:49:36 +03:00
parent 36eaac6f00
commit 7224c81664

View File

@ -58,7 +58,13 @@ bool RegKey::removeValue(const QString &name)
bool RegKey::setValue(const QString &name, const QVariant &value, bool expand)
{
const unsigned char *data = nullptr;
QString dataText;
union {
qint64 i64;
qint32 i32;
} dataNum;
const unsigned char *dataPtr;
DWORD size;
DWORD type;
@ -66,33 +72,34 @@ bool RegKey::setValue(const QString &name, const QVariant &value, bool expand)
case QMetaType::UnknownType:
case QMetaType::Void:
case QMetaType::Nullptr: {
dataPtr = nullptr;
size = 0;
type = REG_SZ;
} break;
case QMetaType::Bool:
case QMetaType::Int:
case QMetaType::UInt: {
const qint32 i32 = value.toInt();
data = (const unsigned char *) &i32;
dataNum.i32 = value.toInt();
dataPtr = (const unsigned char *) &dataNum.i32;
size = sizeof(qint32);
type = REG_DWORD;
} break;
case QMetaType::LongLong:
case QMetaType::ULongLong: {
const qint64 i64 = value.toLongLong();
data = (const unsigned char *) &i64;
dataNum.i64 = value.toLongLong();
dataPtr = (const unsigned char *) &dataNum.i64;
size = sizeof(qint64);
type = REG_QWORD;
} break;
default: {
const QString text = value.toString();
data = (const unsigned char *) text.utf16();
size = DWORD(sizeof(wchar_t) * (text.size() + 1)); /* + terminating null character */
dataText = value.toString();
dataPtr = (const unsigned char *) dataText.utf16();
size = DWORD(sizeof(wchar_t) * (dataText.size() + 1)); /* + terminating null character */
type = expand ? REG_EXPAND_SZ : REG_SZ;
} break;
}
return !RegSetValueEx((HKEY) handle(), (LPCWSTR) name.utf16(), 0, type, data, size);
return !RegSetValueEx((HKEY) handle(), (LPCWSTR) name.utf16(), 0, type, dataPtr, size);
}
QVariant RegKey::value(const QString &name, bool *expand) const