From 3757aa3f532c943f8a47e997d0a4f250d9dacdd3 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 2 Sep 2019 15:07:35 -0400 Subject: split into settingsutilities --- src/settingsutilities.cpp | 247 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 src/settingsutilities.cpp (limited to 'src/settingsutilities.cpp') diff --git a/src/settingsutilities.cpp b/src/settingsutilities.cpp new file mode 100644 index 00000000..7ac95b5f --- /dev/null +++ b/src/settingsutilities.cpp @@ -0,0 +1,247 @@ +#include "settingsutilities.h" +#include "expanderwidget.h" +#include + +using namespace MOBase; + +void logRemoval(const QString& name) +{ + log::debug("setting '{}' removed", name); +} + + +QString settingName(const QString& section, const QString& key) +{ + if (section.isEmpty()) { + return key; + } else if (key.isEmpty()) { + return section; + } else { + if (section.compare("General", Qt::CaseInsensitive) == 0) { + return key; + } else { + return section + "/" + key; + } + } +} + +void removeImpl( + QSettings& settings, const QString& displayName, + const QString& section, const QString& key) +{ + if (key.isEmpty()) { + if (!settings.childGroups().contains(section, Qt::CaseInsensitive)) { + // not there + return; + } + } else { + if (!settings.contains(settingName(section, key))) { + // not there + return; + } + } + + logRemoval(displayName); + settings.remove(settingName(section, key)); +} + +void remove(QSettings& settings, const QString& section, const QString& key) +{ + removeImpl(settings, settingName(section, key), section, key); +} + +void removeSection(QSettings& settings, const QString& section) +{ + removeImpl(settings, section, section, ""); +} + + +ScopedGroup::ScopedGroup(QSettings& s, const QString& name) + : m_settings(s), m_name(name) +{ + m_settings.beginGroup(m_name); +} + +ScopedGroup::~ScopedGroup() +{ + m_settings.endGroup(); +} + +void ScopedGroup::remove(const QString& key) +{ + removeImpl(m_settings, settingName(m_name, key), "", key); +} + +QStringList ScopedGroup::keys() const +{ + return m_settings.childKeys(); +} + + +ScopedReadArray::ScopedReadArray(QSettings& s, const QString& section) + : m_settings(s), m_count(0) +{ + m_count = m_settings.beginReadArray(section); +} + +ScopedReadArray::~ScopedReadArray() +{ + m_settings.endArray(); +} + +int ScopedReadArray::count() const +{ + return m_count; +} + +QStringList ScopedReadArray::keys() const +{ + return m_settings.childKeys(); +} + + +ScopedWriteArray::ScopedWriteArray(QSettings& s, const QString& section) + : m_settings(s), m_section(section), m_i(0) +{ + m_settings.beginWriteArray(section); +} + +ScopedWriteArray::~ScopedWriteArray() +{ + m_settings.endArray(); +} + +void ScopedWriteArray::next() +{ + m_settings.setArrayIndex(m_i); + ++m_i; +} + + +QString widgetNameWithTopLevel(const QWidget* widget) +{ + QStringList components; + + auto* tl = widget->window(); + + if (tl == widget) { + // this is a top level widget, such as a dialog + components.push_back(widget->objectName()); + } else { + // this is a widget + const auto toplevelName = tl->objectName(); + if (!toplevelName.isEmpty()) { + components.push_back(toplevelName); + } + + const auto widgetName = widget->objectName(); + if (!widgetName.isEmpty()) { + components.push_back(widgetName); + } + } + + if (components.isEmpty()) { + // can't do much + return "unknown_widget"; + } + + return components.join("_"); +} + +QString widgetName(const QMainWindow* w) +{ + return w->objectName(); +} + +QString widgetName(const QHeaderView* w) +{ + return widgetNameWithTopLevel(w->parentWidget()); +} + +QString widgetName(const ExpanderWidget* w) +{ + return widgetNameWithTopLevel(w->button()); +} + +QString widgetName(const QWidget* w) +{ + return widgetNameWithTopLevel(w); +} + +QString dockSettingName(const QDockWidget* dock) +{ + return "MainWindow_docks_" + dock->objectName() + "_size"; +} + +QString indexSettingName(const QWidget* widget) +{ + return widgetNameWithTopLevel(widget) + "_index"; +} + +QString checkedSettingName(const QAbstractButton* b) +{ + return widgetNameWithTopLevel(b) + "_checked"; +} + +void warnIfNotCheckable(const QAbstractButton* b) +{ + if (!b->isCheckable()) { + log::warn( + "button '{}' used in the settings as a checkbox or radio button " + "but is not checkable", b->objectName()); + } +} + + +bool setWindowsCredential(const QString key, const QString data) +{ + QString finalKey("ModOrganizer2_" + key); + wchar_t* keyData = new wchar_t[finalKey.size()+1]; + finalKey.toWCharArray(keyData); + keyData[finalKey.size()] = L'\0'; + bool result = false; + if (data.isEmpty()) { + result = CredDeleteW(keyData, CRED_TYPE_GENERIC, 0); + if (!result) + if (GetLastError() == ERROR_NOT_FOUND) + result = true; + } else { + wchar_t* charData = new wchar_t[data.size()]; + data.toWCharArray(charData); + + CREDENTIALW cred = {}; + cred.Flags = 0; + cred.Type = CRED_TYPE_GENERIC; + cred.TargetName = keyData; + cred.CredentialBlob = (LPBYTE)charData; + cred.CredentialBlobSize = sizeof(wchar_t) * data.size(); + cred.Persist = CRED_PERSIST_LOCAL_MACHINE; + + result = CredWriteW(&cred, 0); + delete[] charData; + } + delete[] keyData; + return result; +} + +QString getWindowsCredential(const QString key) +{ + QString result; + QString finalKey("ModOrganizer2_" + key); + wchar_t* keyData = new wchar_t[finalKey.size()+1]; + finalKey.toWCharArray(keyData); + keyData[finalKey.size()] = L'\0'; + PCREDENTIALW creds; + if (CredReadW(keyData, 1, 0, &creds)) { + wchar_t *charData = (wchar_t *)creds->CredentialBlob; + result = QString::fromWCharArray(charData, creds->CredentialBlobSize / sizeof(wchar_t)); + CredFree(creds); + } else { + const auto e = GetLastError(); + if (e != ERROR_NOT_FOUND) { + log::error("Retrieving encrypted data failed: {}", formatSystemMessage(e)); + } + } + delete[] keyData; + return result; +} -- cgit v1.3.1 From 209c27c7a27e2f6cb34f122a929c15eb3d1e60b7 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 2 Sep 2019 15:47:21 -0400 Subject: only remove section when the array is larger, prevents logging changes when nothing actually changed changed back section names that were originally lowercase, arrays end up in two different sections --- src/settings.cpp | 69 ++++++++++++++++++++++++++++++----------------- src/settingsutilities.cpp | 8 +++--- src/settingsutilities.h | 4 ++- 3 files changed, 52 insertions(+), 29 deletions(-) (limited to 'src/settingsutilities.cpp') diff --git a/src/settings.cpp b/src/settings.cpp index 14189be3..8ae7c343 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -221,9 +221,19 @@ std::vector> Settings::executables() const void Settings::setExecutables(const std::vector>& v) { - removeSection(m_Settings, "customExecutables"); + const auto current = executables(); - ScopedWriteArray swa(m_Settings, "customExecutables"); + if (current == v) { + // no change + return; + } + + if (current.size() > v.size()) { + // Qt can't remove array elements, the section must be cleared + removeSection(m_Settings, "customExecutables"); + } + + ScopedWriteArray swa(m_Settings, "customExecutables", v.size()); for (const auto& map : v) { swa.next(); @@ -1156,9 +1166,9 @@ void PluginSettings::addBlacklistPlugin(const QString &fileName) void PluginSettings::writePluginBlacklist() { - removeSection(m_Settings, "PluginBlacklist"); + removeSection(m_Settings, "pluginBlacklist"); - ScopedWriteArray swa(m_Settings, "PluginBlacklist"); + ScopedWriteArray swa(m_Settings, "pluginBlacklist"); for (const QString &plugin : m_PluginBlacklist) { swa.next(); @@ -1222,7 +1232,7 @@ std::map PathSettings::recent() const { std::map map; - ScopedReadArray sra(m_Settings, "RecentDirectories"); + ScopedReadArray sra(m_Settings, "recentDirectories"); sra.for_each([&] { const QVariant name = sra.get("name"); @@ -1238,9 +1248,14 @@ std::map PathSettings::recent() const void PathSettings::setRecent(const std::map& map) { - removeSection(m_Settings, "RecentDirectories"); + const auto current = recent(); - ScopedWriteArray swa(m_Settings, "recentDirectories"); + if (current.size() > map.size()) { + // Qt can't remove array elements, the section must be cleared + removeSection(m_Settings, "recentDirectories"); + } + + ScopedWriteArray swa(m_Settings, "recentDirectories", map.size()); for (auto&& p : map) { swa.next(); @@ -1472,33 +1487,37 @@ ServerList NetworkSettings::serversFromOldMap() const return list; } -void NetworkSettings::updateServers(ServerList servers) +void NetworkSettings::updateServers(ServerList newServers) { // clean up unavailable servers - servers.cleanup(); + newServers.cleanup(); - removeSection(m_Settings, "Servers"); + const auto current = servers(); - { - ScopedWriteArray swa(m_Settings, "Servers"); + if (current.size() > newServers.size()) { + // Qt can't remove array elements, the section must be cleared + removeSection(m_Settings, "Servers"); + } - for (const auto& server : servers) { - swa.next(); - swa.set("name", server.name()); - swa.set("premium", server.isPremium()); - swa.set("lastSeen", server.lastSeen().toString(Qt::ISODate)); - swa.set("preferred", server.preferred()); + ScopedWriteArray swa(m_Settings, "Servers", newServers.size()); - QString lastDownloads; - for (const auto& speed : server.lastDownloads()) { - if (speed > 0) { - lastDownloads += QString("%1 ").arg(speed); - } - } + for (const auto& server : newServers) { + swa.next(); - swa.set("lastDownloads", lastDownloads.trimmed()); + swa.set("name", server.name()); + swa.set("premium", server.isPremium()); + swa.set("lastSeen", server.lastSeen().toString(Qt::ISODate)); + swa.set("preferred", server.preferred()); + + QString lastDownloads; + for (const auto& speed : server.lastDownloads()) { + if (speed > 0) { + lastDownloads += QString("%1 ").arg(speed); + } } + + swa.set("lastDownloads", lastDownloads.trimmed()); } } diff --git a/src/settingsutilities.cpp b/src/settingsutilities.cpp index 7ac95b5f..d5e2dd9a 100644 --- a/src/settingsutilities.cpp +++ b/src/settingsutilities.cpp @@ -100,10 +100,12 @@ QStringList ScopedReadArray::keys() const } -ScopedWriteArray::ScopedWriteArray(QSettings& s, const QString& section) - : m_settings(s), m_section(section), m_i(0) +ScopedWriteArray::ScopedWriteArray( + QSettings& s, const QString& section, std::size_t size) + : m_settings(s), m_section(section), m_i(0) { - m_settings.beginWriteArray(section); + m_settings.beginWriteArray( + section, size == NoSize ? -1 : static_cast(size)); } ScopedWriteArray::~ScopedWriteArray() diff --git a/src/settingsutilities.h b/src/settingsutilities.h index b70e55ef..ca754759 100644 --- a/src/settingsutilities.h +++ b/src/settingsutilities.h @@ -204,7 +204,9 @@ private: class ScopedWriteArray { public: - ScopedWriteArray(QSettings& s, const QString& section); + static const auto NoSize = std::numeric_limits::max(); + + ScopedWriteArray(QSettings& s, const QString& section, std::size_t size=NoSize); ~ScopedWriteArray(); ScopedWriteArray(const ScopedWriteArray&) = delete; -- cgit v1.3.1 From 7f0fa1069f07d90a92be7073b11bab86bac7b2d2 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Mon, 2 Sep 2019 16:09:31 -0400 Subject: don't log widget and geometry setting changes fixed mod info dialog tab order using different settings for read and write mod info dialog now doesn't complain when no tab order exists in the settings only remove section when the array is larger, prevents logging changes when nothing actually changed --- src/modinfodialog.cpp | 9 ++++++--- src/settings.cpp | 27 ++++++++++++++++++++------- src/settings.h | 2 +- src/settingsutilities.cpp | 18 ++++++++++++++++++ src/settingsutilities.h | 6 ++++++ 5 files changed, 51 insertions(+), 11 deletions(-) (limited to 'src/settingsutilities.cpp') diff --git a/src/modinfodialog.cpp b/src/modinfodialog.cpp index 2178ef34..c7e071ad 100644 --- a/src/modinfodialog.cpp +++ b/src/modinfodialog.cpp @@ -383,9 +383,12 @@ void ModInfoDialog::reAddTabs( // ordered tab names from settings const auto orderedNames = m_core->settings().geometry().modInfoTabOrder(); - // whether the tabs can be sorted; if the object name of a tab widget is not - // found in orderedNames, the list cannot be sorted safely - bool canSort = true; + // whether the tabs can be sorted + // + // if the object name of a tab widget is not found in orderedNames, the list + // cannot be sorted safely; if the list is empty, it's probably a first run + // and there's nothing to sort + bool canSort = !orderedNames.empty(); // gathering visible tabs std::vector visibleTabs; diff --git a/src/settings.cpp b/src/settings.cpp index 8ae7c343..a33005b6 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -210,7 +210,7 @@ std::vector> Settings::executables() const std::map map; for (auto&& key : sra.keys()) { - map[key] = m_Settings.value(key); + map[key] = sra.get(key); } v.push_back(map); @@ -693,7 +693,7 @@ QStringList GeometrySettings::modInfoTabOrder() const } } else { // string list since 2.2.1 - QString string = m_Settings.value("mod_info_tab_order").toString(); + QString string = get(m_Settings, "Widgets", "ModInfoTabOrder", ""); QTextStream stream(&string); while (!stream.atEnd()) { @@ -708,7 +708,7 @@ QStringList GeometrySettings::modInfoTabOrder() const void GeometrySettings::setModInfoTabOrder(const QString& names) { - set(m_Settings, "Geometry", "mod_info_tab_order", names); + set(m_Settings, "Widgets", "ModInfoTabOrder", names); } void GeometrySettings::centerOnMainWindowMonitor(QWidget* w) @@ -1061,13 +1061,21 @@ void PluginSettings::clearPlugins() { m_Plugins.clear(); m_PluginSettings.clear(); - m_PluginBlacklist.clear(); + m_PluginBlacklist = readPluginBlacklist(); +} + +QSet PluginSettings::readPluginBlacklist() const +{ + QSet set; + ScopedReadArray sra(m_Settings, "pluginBlacklist"); sra.for_each([&]{ - m_PluginBlacklist.insert(sra.get("name")); + set.insert(sra.get("name")); }); + + return set; } void PluginSettings::registerPlugin(IPlugin *plugin) @@ -1166,9 +1174,14 @@ void PluginSettings::addBlacklistPlugin(const QString &fileName) void PluginSettings::writePluginBlacklist() { - removeSection(m_Settings, "pluginBlacklist"); + const auto current = readPluginBlacklist(); + + if (current.size() > m_PluginBlacklist.size()) { + // Qt can't remove array elements, the section must be cleared + removeSection(m_Settings, "pluginBlacklist"); + } - ScopedWriteArray swa(m_Settings, "pluginBlacklist"); + ScopedWriteArray swa(m_Settings, "pluginBlacklist", m_PluginBlacklist.size()); for (const QString &plugin : m_PluginBlacklist) { swa.next(); diff --git a/src/settings.h b/src/settings.h index 68cfb9b7..2ff8da1c 100644 --- a/src/settings.h +++ b/src/settings.h @@ -261,8 +261,8 @@ private: QMap m_PluginDescriptions; QSet m_PluginBlacklist; - void readPluginBlacklist(); void writePluginBlacklist(); + QSet readPluginBlacklist() const; }; diff --git a/src/settingsutilities.cpp b/src/settingsutilities.cpp index d5e2dd9a..7a9dcc35 100644 --- a/src/settingsutilities.cpp +++ b/src/settingsutilities.cpp @@ -4,8 +4,26 @@ using namespace MOBase; +bool shouldLogSetting(const QString& displayName) +{ + // don't log Geometry/ and Widgets/, too noisy and not very useful + static const QStringList ignorePrefixes = {"Geometry/", "Widgets/"}; + + for (auto&& prefix : ignorePrefixes) { + if (displayName.startsWith(prefix, Qt::CaseInsensitive)) { + return false; + } + } + + return true; +} + void logRemoval(const QString& name) { + if (!shouldLogSetting(name)) { + return; + } + log::debug("setting '{}' removed", name); } diff --git a/src/settingsutilities.h b/src/settingsutilities.h index ca754759..d99abb06 100644 --- a/src/settingsutilities.h +++ b/src/settingsutilities.h @@ -24,10 +24,16 @@ struct ValueConverter>> }; +bool shouldLogSetting(const QString& displayName); + template void logChange( const QString& displayName, std::optional oldValue, const T& newValue) { + if (!shouldLogSetting(displayName)) { + return; + } + using VC = ValueConverter; if (oldValue) { -- cgit v1.3.1