From d8a730302a87feccdf993257e865ffd20b9f3971 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 13 Feb 2019 03:11:13 +0100 Subject: [PATCH] Another attempt to fix copyed hotkeys by updating the identifier * check on name OR id * remote keys not updated, but lenient check on local machine * Remark: Sometimes copied hotkeys d not work --- .../copysettingsandcachescomponent.cpp | 3 +- src/blackmisc/identifier.cpp | 63 ++++++++++++------- src/blackmisc/identifier.h | 9 +++ src/blackmisc/input/actionhotkey.cpp | 17 ++++- src/blackmisc/input/actionhotkey.h | 14 ++++- src/blackmisc/input/actionhotkeylist.cpp | 12 +++- src/blackmisc/input/actionhotkeylist.h | 3 + 7 files changed, 95 insertions(+), 26 deletions(-) diff --git a/src/blackgui/components/copysettingsandcachescomponent.cpp b/src/blackgui/components/copysettingsandcachescomponent.cpp index f826efae9..cbc9ac785 100644 --- a/src/blackgui/components/copysettingsandcachescomponent.cpp +++ b/src/blackgui/components/copysettingsandcachescomponent.cpp @@ -361,7 +361,8 @@ namespace BlackGui const QString joStr = CCacheSettingsUtils::otherVersionSettingsFileContent(otherVersionInfo, m_settingsActionHotkeys.getFilename()); if (!joStr.isEmpty()) { - const CActionHotkeyList hotkeys = CActionHotkeyList::fromJsonNoThrow(joStr, true, success, errMsg); + CActionHotkeyList hotkeys = CActionHotkeyList::fromJsonNoThrow(joStr, true, success, errMsg); + hotkeys.updateToCurrentMachine(); if (this->parsingMessage(success, errMsg, m_settingsActionHotkeys.getKey())) { this->displayStatusMessage(m_settingsActionHotkeys.setAndSave(hotkeys), hotkeys.toQString(true)); diff --git a/src/blackmisc/identifier.cpp b/src/blackmisc/identifier.cpp index 50da7124c..40e96af44 100644 --- a/src/blackmisc/identifier.cpp +++ b/src/blackmisc/identifier.cpp @@ -29,7 +29,8 @@ const QString &cachedLocalHostName() return hostName; } -enum { +enum +{ UuidStringLen = sizeof("00000000-0000-0000-0000-000000000000") }; @@ -40,27 +41,27 @@ QByteArray getMachineUniqueIdImpl() // https://codereview.qt-project.org/#/c/249399 QByteArray machineUniqueId; - #ifdef Q_OS_MAC - char uuid[UuidStringLen]; - size_t uuidlen = sizeof(uuid); - int ret = sysctlbyname("kern.uuid", uuid, &uuidlen, nullptr, 0); - if (ret == 0 && uuidlen == sizeof(uuid)) - { - machineUniqueId = QByteArray(uuid, uuidlen - 1); - } - #elif defined(Q_OS_WIN) && !defined(Q_OS_WINRT) - HKEY key = nullptr; - if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Cryptography", 0, KEY_READ | KEY_WOW64_64KEY, &key) == ERROR_SUCCESS) - { - wchar_t buffer[UuidStringLen]; - DWORD size = sizeof(buffer); - bool ok = (RegQueryValueEx(key, L"MachineGuid", nullptr, nullptr, reinterpret_cast(buffer), &size) == ERROR_SUCCESS); - RegCloseKey(key); - if (ok) { machineUniqueId = QStringView(buffer, (size - 1) / 2).toLatin1(); } - } - #else - machineUniqueId = QSysInfo::machineUniqueId(); - #endif +#ifdef Q_OS_MAC + char uuid[UuidStringLen]; + size_t uuidlen = sizeof(uuid); + int ret = sysctlbyname("kern.uuid", uuid, &uuidlen, nullptr, 0); + if (ret == 0 && uuidlen == sizeof(uuid)) + { + machineUniqueId = QByteArray(uuid, uuidlen - 1); + } +#elif defined(Q_OS_WIN) && !defined(Q_OS_WINRT) + HKEY key = nullptr; + if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Cryptography", 0, KEY_READ | KEY_WOW64_64KEY, &key) == ERROR_SUCCESS) + { + wchar_t buffer[UuidStringLen]; + DWORD size = sizeof(buffer); + bool ok = (RegQueryValueEx(key, L"MachineGuid", nullptr, nullptr, reinterpret_cast(buffer), &size) == ERROR_SUCCESS); + RegCloseKey(key); + if (ok) { machineUniqueId = QStringView(buffer, (size - 1) / 2).toLatin1(); } + } +#else + machineUniqueId = QSysInfo::machineUniqueId(); +#endif return machineUniqueId; } @@ -162,8 +163,14 @@ namespace BlackMisc return !m_machineIdBase64.isEmpty() && m_machineIdBase64 == other.m_machineIdBase64; } + bool CIdentifier::hasSameMachineNameOrId(const CIdentifier &other) const + { + return this->hasSameMachineId(other) || this->hasSameMachineName(other); + } + bool CIdentifier::isFromLocalMachine() const { + //! \fixme KB 2019-02 wonder if we should check on id (strict) or machine name (lenient) return cachedMachineUniqueId() == getMachineId(); } @@ -187,6 +194,18 @@ namespace BlackMisc return &null() == this || null() == *this; } + void CIdentifier::updateToCurrentMachine() + { + m_machineIdBase64 = cachedMachineUniqueId().toBase64(); + m_machineName = cachedLocalHostName(); + } + + void CIdentifier::updateToCurrentProcess() + { + m_processName = QCoreApplication::applicationName(); + m_processId = QCoreApplication::applicationPid(); + } + QString CIdentifier::convertToQString(bool i18n) const { Q_UNUSED(i18n); diff --git a/src/blackmisc/identifier.h b/src/blackmisc/identifier.h index 7dadfbe6f..7fb06d58f 100644 --- a/src/blackmisc/identifier.h +++ b/src/blackmisc/identifier.h @@ -108,6 +108,9 @@ namespace BlackMisc //! Check if other identifier is from the same machine id bool hasSameMachineId(const CIdentifier &other) const; + //! Same machine or id? + bool hasSameMachineNameOrId(const CIdentifier &other) const; + //! Get process id qint64 getProcessId() const {return m_processId;} @@ -129,6 +132,12 @@ namespace BlackMisc //! Null identifier (no name, ids etc) bool isNull() const; + //! Update to current machine + void updateToCurrentMachine(); + + //! Update to current process + void updateToCurrentProcess(); + //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const; diff --git a/src/blackmisc/input/actionhotkey.cpp b/src/blackmisc/input/actionhotkey.cpp index 2ea9065c1..b20d73833 100644 --- a/src/blackmisc/input/actionhotkey.cpp +++ b/src/blackmisc/input/actionhotkey.cpp @@ -38,11 +38,26 @@ namespace BlackMisc m_combination = combination; } - bool CActionHotkey::isForSameMachine(const CActionHotkey &key) const + bool CActionHotkey::isForSameMachineId(const CActionHotkey &key) const { return this->getApplicableMachine().hasSameMachineId(key.getApplicableMachine()); } + bool CActionHotkey::isForSameMachineName(const CActionHotkey &key) const + { + return this->getApplicableMachine().hasSameMachineName(key.getApplicableMachine()); + } + + bool CActionHotkey::isForSameMachine(const CActionHotkey &key) const + { + return this->isForSameMachineId(key) || this->isForSameMachineName(key); + } + + void CActionHotkey::updateToCurrentMachine() + { + m_identifier.updateToCurrentMachine(); + } + void CActionHotkey::setObject(const CActionHotkey &obj) { m_action = obj.m_action; diff --git a/src/blackmisc/input/actionhotkey.h b/src/blackmisc/input/actionhotkey.h index 71d139b32..a6d706b4c 100644 --- a/src/blackmisc/input/actionhotkey.h +++ b/src/blackmisc/input/actionhotkey.h @@ -64,15 +64,27 @@ namespace BlackMisc //! Set function void setAction(const QString &action) { m_action = action; } + //! The identifier + const CIdentifier &getIdentifier() const { return m_identifier; } + //! Set applicable machine void setApplicableMachine(const CIdentifier &identifier) { m_identifier = identifier; } //! Get applicable machine const CIdentifier &getApplicableMachine() const { return m_identifier; } - //! Key for the same machine? + //! Key for the same machine id? + bool isForSameMachineId(const CActionHotkey &key) const; + + //! Key for the same machine name + bool isForSameMachineName(const CActionHotkey &key) const; + + //! Key for the same machine (same name or id)? bool isForSameMachine(const CActionHotkey &key) const; + //! Local machine + void updateToCurrentMachine(); + //! Set object void setObject(const CActionHotkey &obj); diff --git a/src/blackmisc/input/actionhotkeylist.cpp b/src/blackmisc/input/actionhotkeylist.cpp index 559d44335..86f203ca2 100644 --- a/src/blackmisc/input/actionhotkeylist.cpp +++ b/src/blackmisc/input/actionhotkeylist.cpp @@ -51,7 +51,7 @@ namespace BlackMisc CActionHotkeyList sameMachineKeys; for (const CActionHotkey &actionHotkey : *this) { - if (!actionHotkey.isForSameMachine(key)) { continue; } + if (!actionHotkey.isForSameMachineId(key)) { continue; } sameMachineKeys.push_back(actionHotkey); } return sameMachineKeys; @@ -61,5 +61,15 @@ namespace BlackMisc { return this->contains(&CActionHotkey::getAction, action); } + + void CActionHotkeyList::updateToCurrentMachine() + { + const CIdentifier comparison("comparison for local machine"); + for (CActionHotkey &actionHotkey : *this) + { + const bool sameMachine = actionHotkey.getIdentifier().hasSameMachineNameOrId(comparison); + if (sameMachine) { actionHotkey.updateToCurrentMachine(); } + } + } } // ns } // ns diff --git a/src/blackmisc/input/actionhotkeylist.h b/src/blackmisc/input/actionhotkeylist.h index 6ca68108c..0e3dd0f99 100644 --- a/src/blackmisc/input/actionhotkeylist.h +++ b/src/blackmisc/input/actionhotkeylist.h @@ -57,6 +57,9 @@ namespace BlackMisc //! Contains action? bool containsAction(const QString &action) const; + + //! Update for my machine + void updateToCurrentMachine(); }; } // ns } // ns