-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and clean plugin settings stuff.
- Loading branch information
Showing
9 changed files
with
262 additions
and
413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ edit | |
/msbuild.log | ||
/*std*.log | ||
/*build | ||
.vscode | ||
|
||
/src/version.aps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#include "extensionsettings.h" | ||
|
||
#include "settingsutilities.h" | ||
|
||
using namespace MOBase; | ||
|
||
static const QString PLUGINS_GROUP = "Plugins"; | ||
static const QString PLUGINS_PERSISTENT_GROUP = "PluginPersistance"; | ||
|
||
PluginSettings::PluginSettings(QSettings& settings) : m_Settings(settings) {} | ||
|
||
QString PluginSettings::path(const QString& pluginName, const QString& key) | ||
{ | ||
return pluginName + "/" + key; | ||
} | ||
|
||
void PluginSettings::checkPluginSettings(const IPlugin* plugin) const | ||
{ | ||
for (const auto& setting : plugin->settings()) { | ||
const auto settingPath = path(plugin->name(), setting.name()); | ||
|
||
QVariant temp = get<QVariant>(m_Settings, PLUGINS_GROUP, settingPath, QVariant()); | ||
|
||
// No previous enabled? Skip. | ||
if (setting.name() == "enabled" && (!temp.isValid() || !temp.canConvert<bool>())) { | ||
continue; | ||
} | ||
|
||
if (!temp.isValid()) { | ||
temp = setting.defaultValue(); | ||
} else if (!temp.convert(setting.defaultValue().metaType())) { | ||
log::warn("failed to interpret \"{}\" as correct type for \"{}\" in plugin " | ||
"\"{}\", using default", | ||
temp.toString(), setting.name(), plugin->name()); | ||
|
||
temp = setting.defaultValue(); | ||
} | ||
} | ||
} | ||
|
||
void PluginSettings::fixPluginEnabledSetting(const IPlugin* plugin) | ||
{ | ||
// handle previous "enabled" settings | ||
// TODO: keep this? | ||
const auto previousEnabledPath = plugin->name() + "/enabled"; | ||
const QVariant previousEnabled = | ||
get<QVariant>(m_Settings, PLUGINS_GROUP, previousEnabledPath, QVariant()); | ||
if (previousEnabled.isValid()) { | ||
setPersistent(plugin->name(), "enabled", previousEnabled.toBool(), true); | ||
|
||
// We need to drop it manually in Settings since it is not possible to remove | ||
// plugin settings: | ||
remove(m_Settings, PLUGINS_GROUP, previousEnabledPath); | ||
} | ||
} | ||
|
||
QVariant PluginSettings::setting(const QString& pluginName, const QString& key, | ||
const QVariant& defaultValue) const | ||
{ | ||
return get<QVariant>(m_Settings, "Settings", path(pluginName, key), defaultValue); | ||
} | ||
|
||
void PluginSettings::setSetting(const QString& pluginName, const QString& key, | ||
const QVariant& value) | ||
{ | ||
const auto settingPath = path(pluginName, key); | ||
const auto oldValue = | ||
get<QVariant>(m_Settings, PLUGINS_GROUP, settingPath, QVariant()); | ||
set(m_Settings, PLUGINS_GROUP, settingPath, value); | ||
emit pluginSettingChanged(pluginName, key, oldValue, value); | ||
} | ||
|
||
QVariant PluginSettings::persistent(const QString& pluginName, const QString& key, | ||
const QVariant& def) const | ||
{ | ||
return get<QVariant>(m_Settings, "PluginPersistance", pluginName + "/" + key, def); | ||
} | ||
|
||
void PluginSettings::setPersistent(const QString& pluginName, const QString& key, | ||
const QVariant& value, bool sync) | ||
{ | ||
set(m_Settings, PLUGINS_PERSISTENT_GROUP, pluginName + "/" + key, value); | ||
|
||
if (sync) { | ||
m_Settings.sync(); | ||
} | ||
} | ||
|
||
void PluginSettings::addBlacklist(const QString& fileName) | ||
{ | ||
m_PluginBlacklist.insert(fileName); | ||
writeBlacklist(); | ||
} | ||
|
||
bool PluginSettings::blacklisted(const QString& fileName) const | ||
{ | ||
return m_PluginBlacklist.contains(fileName); | ||
} | ||
|
||
void PluginSettings::setBlacklist(const QStringList& pluginNames) | ||
{ | ||
m_PluginBlacklist.clear(); | ||
|
||
for (const auto& name : pluginNames) { | ||
m_PluginBlacklist.insert(name); | ||
} | ||
} | ||
|
||
const QSet<QString>& PluginSettings::blacklist() const | ||
{ | ||
return m_PluginBlacklist; | ||
} | ||
|
||
void PluginSettings::save() | ||
{ | ||
m_Settings.sync(); | ||
writeBlacklist(); | ||
} | ||
|
||
void PluginSettings::writeBlacklist() | ||
{ | ||
const auto current = readBlacklist(); | ||
|
||
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", m_PluginBlacklist.size()); | ||
|
||
for (const QString& plugin : m_PluginBlacklist) { | ||
swa.next(); | ||
swa.set("name", plugin); | ||
} | ||
} | ||
|
||
QSet<QString> PluginSettings::readBlacklist() const | ||
{ | ||
QSet<QString> set; | ||
|
||
ScopedReadArray sra(m_Settings, "pluginBlacklist"); | ||
sra.for_each([&] { | ||
set.insert(sra.get<QString>("name")); | ||
}); | ||
|
||
return set; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#ifndef EXTENSIONSETTINGS_H | ||
#define EXTENSIONSETTINGS_H | ||
|
||
#include <QObject> | ||
#include <QSettings> | ||
|
||
#include <uibase/iplugin.h> | ||
|
||
// settings about plugins | ||
// | ||
class PluginSettings : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
PluginSettings(QSettings& settings); | ||
|
||
// fix enabled settings from previous MO2 installation | ||
// | ||
void fixPluginEnabledSetting(const MOBase::IPlugin* plugin); | ||
|
||
// check that the settings stored for the given plugin are of the appropriate type, | ||
// warning user if not | ||
// | ||
void checkPluginSettings(const MOBase::IPlugin* plugin) const; | ||
|
||
// returns the plugin setting for the given key | ||
// | ||
QVariant setting(const QString& pluginName, const QString& key, | ||
const QVariant& defaultValue = {}) const; | ||
|
||
// sets the plugin setting for the given key | ||
// | ||
void setSetting(const QString& pluginName, const QString& key, const QVariant& value); | ||
|
||
// get/set persistent settings | ||
QVariant persistent(const QString& pluginName, const QString& key, | ||
const QVariant& def) const; | ||
void setPersistent(const QString& pluginName, const QString& key, | ||
const QVariant& value, bool sync); | ||
|
||
// adds the given plugin to the blacklist | ||
// | ||
void addBlacklist(const QString& fileName); | ||
|
||
// returns whether the given plugin is blacklisted | ||
// | ||
bool blacklisted(const QString& fileName) const; | ||
|
||
// overwrites the whole blacklist | ||
// | ||
void setBlacklist(const QStringList& pluginNames); | ||
|
||
// returns the blacklist | ||
// | ||
const QSet<QString>& blacklist() const; | ||
|
||
// commits all the settings to the ini | ||
// | ||
void save(); | ||
|
||
Q_SIGNALS: | ||
|
||
// emitted when a plugin setting changes | ||
// | ||
void pluginSettingChanged(QString const& pluginName, const QString& key, | ||
const QVariant& oldValue, const QVariant& newValue); | ||
|
||
private: | ||
QSettings& m_Settings; | ||
QSet<QString> m_PluginBlacklist; | ||
|
||
// retrieve the path to the given setting | ||
// | ||
static QString path(const QString& pluginName, const QString& key); | ||
|
||
// commits the blacklist to the ini | ||
// | ||
void writeBlacklist(); | ||
|
||
// reads the blacklist from the ini | ||
// | ||
QSet<QString> readBlacklist() const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.