UI: RegKey: Simplify setValue()

This commit is contained in:
Nodir Temirkhodjaev 2023-07-10 14:50:08 +03:00
parent 4d89e9e7f1
commit bd98365a99

View File

@ -58,11 +58,6 @@ bool RegKey::removeValue(const QString &name)
bool RegKey::setValue(const QString &name, const QVariant &value, bool expand) bool RegKey::setValue(const QString &name, const QVariant &value, bool expand)
{ {
QString text;
union {
qint64 i64;
qint32 i32;
} num;
const unsigned char *data = nullptr; const unsigned char *data = nullptr;
DWORD size; DWORD size;
DWORD type; DWORD type;
@ -70,30 +65,31 @@ bool RegKey::setValue(const QString &name, const QVariant &value, bool expand)
switch (value.userType()) { switch (value.userType()) {
case QMetaType::UnknownType: case QMetaType::UnknownType:
case QMetaType::Void: case QMetaType::Void:
case QMetaType::Nullptr: case QMetaType::Nullptr: {
size = 0; size = 0;
type = REG_SZ; type = REG_SZ;
break; } break;
case QMetaType::Bool: case QMetaType::Bool:
case QMetaType::Int: case QMetaType::Int:
case QMetaType::UInt: case QMetaType::UInt: {
num.i32 = value.toInt(); const qint32 i32 = value.toInt();
data = (const unsigned char *) &num.i32; data = (const unsigned char *) &i32;
size = sizeof(qint32); size = sizeof(qint32);
type = REG_DWORD; type = REG_DWORD;
break; } break;
case QMetaType::LongLong: case QMetaType::LongLong:
case QMetaType::ULongLong: case QMetaType::ULongLong: {
num.i64 = value.toLongLong(); const qint64 i64 = value.toLongLong();
data = (const unsigned char *) &num.i64; data = (const unsigned char *) &i64;
size = sizeof(qint64); size = sizeof(qint64);
type = REG_QWORD; type = REG_QWORD;
break; } break;
default: default: {
text = value.toString(); const QString text = value.toString();
data = (const unsigned char *) text.utf16(); data = (const unsigned char *) text.utf16();
size = DWORD(sizeof(wchar_t) * (text.size() + 1)); /* + terminating null character */ size = DWORD(sizeof(wchar_t) * (text.size() + 1)); /* + terminating null character */
type = expand ? REG_EXPAND_SZ : REG_SZ; 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, data, size);