diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-17 21:28:45 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-17 21:34:53 +0200 |
commit | cb33be1c50b68d6022f344ddac923c7aac3a11d5 (patch) | |
tree | 1840e3f9829aa69f10f81804f655e97be3f2eca8 /options/group.cpp | |
parent | 41ba8aa5329c16a797140dc23650ef45f42753a3 (diff) |
move options framework into its own library
- adjust usages
- add support for QList signals and metatype
Diffstat (limited to 'options/group.cpp')
-rw-r--r-- | options/group.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/options/group.cpp b/options/group.cpp new file mode 100644 index 00000000..8aa381fc --- /dev/null +++ b/options/group.cpp @@ -0,0 +1,114 @@ +#include "group.hpp" +#include "defs.hpp" +#include <QStandardPaths> +#include <QDir> +#include <QDebug> + +namespace options { + +group::group(const QString& name) : name(name) +{ + if (name == "") + return; + + auto conf = ini_file(); + conf->beginGroup(name); + for (auto& k_ : conf->childKeys()) + { + auto tmp = k_.toUtf8(); + QString k(tmp); + kvs[k] = conf->value(k_); + } + conf->endGroup(); +} + +void group::save() const +{ + if (name == "") + return; + + auto s = ini_file(); + s->beginGroup(name); + for (auto& i : kvs) + s->setValue(i.first, i.second); + s->endGroup(); +} + +void group::put(const QString &s, const QVariant &d) +{ + kvs[s] = d; +} + +bool group::contains(const QString &s) const +{ + return kvs.count(s) != 0; +} + +QString group::ini_directory() +{ + const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + if (dirs.size() == 0) + return ""; + if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) + return dirs[0] + "/" OPENTRACK_ORG; + return ""; +} + +QString group::ini_filename() +{ + QSettings settings(OPENTRACK_ORG); + return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); +} + +QString group::ini_pathname() +{ + const auto dir = ini_directory(); + if (dir == "") + return ""; + return dir + "/" + ini_filename(); +} + +const QStringList group::ini_list() +{ + const auto dirname = ini_directory(); + if (dirname == "") + return QStringList(); + QDir settings_dir(dirname); + QStringList list = settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); + std::sort(list.begin(), list.end()); + return list; +} + +const std::shared_ptr<QSettings> group::ini_file() +{ + const auto pathname = ini_pathname(); + if (pathname != "") + return std::make_shared<QSettings>(ini_pathname(), QSettings::IniFormat); + return std::make_shared<QSettings>(); +} + +bool group::operator==(const group& other) const +{ + for (const auto& kv : kvs) + { + const QVariant val = other.get<QVariant>(kv.first); + if (!other.contains(kv.first) || kv.second != val) + { + qDebug() << "bundle" << name << "modified" << "key" << kv.first << "-" << val << "<>" << kv.second; + return false; + } + } + + for (const auto& kv : other.kvs) + { + const QVariant val = get<QVariant>(kv.first); + if (!contains(kv.first) || kv.second != val) + { + qDebug() << "bundle" << name << "modified" << "key" << kv.first << "-" << kv.second << "<>" << val; + return false; + } + } + return true; +} + +} |