MoneroSettings: portable mode, CWD relative 'monero-storage' dir

This commit is contained in:
xiphon 2020-07-28 17:29:13 +00:00
parent c137a6ea36
commit 1aa98c3cfd
2 changed files with 104 additions and 3 deletions

View file

@ -134,12 +134,13 @@ QVariant MoneroSettings::readProperty(const QMetaProperty &property) const
void MoneroSettings::init()
{
if (!this->m_initialized) {
this->m_settings = this->m_fileName.isEmpty() ? new QSettings() : new QSettings(this->m_fileName, QSettings::IniFormat);
this->m_settings = portableConfigExists() ? portableSettings() : unportableSettings();
#ifdef QT_DEBUG
qDebug() << "QQmlSettings: stored at" << this->m_settings->fileName();
#endif
this->load();
this->m_initialized = true;
emit portableChanged();
}
}
@ -148,11 +149,16 @@ void MoneroSettings::reset()
if (this->m_initialized && this->m_settings && !this->m_changedProperties.isEmpty())
this->store();
if (this->m_settings)
delete this->m_settings;
this->m_settings.reset();
}
void MoneroSettings::store()
{
if (!m_writable)
{
return;
}
QHash<const char *, QVariant>::const_iterator it = this->m_changedProperties.constBegin();
while (it != this->m_changedProperties.constEnd()) {
@ -168,6 +174,58 @@ void MoneroSettings::store()
this->m_changedProperties.clear();
}
bool MoneroSettings::portable() const
{
return this->m_settings && this->m_settings->fileName() == portableFilePath();
}
bool MoneroSettings::portableConfigExists() const
{
QFileInfo info(portableFilePath());
return info.exists() && info.isFile();
}
QString MoneroSettings::portableFilePath() const
{
static QString filename(QDir(portableFolderName()).absoluteFilePath("settings.ini"));
return filename;
}
QString MoneroSettings::portableFolderName() const
{
return "monero-storage";
}
std::unique_ptr<QSettings> MoneroSettings::portableSettings() const
{
return std::unique_ptr<QSettings>(new QSettings(portableFilePath(), QSettings::IniFormat));
}
std::unique_ptr<QSettings> MoneroSettings::unportableSettings() const
{
if (this->m_fileName.isEmpty())
{
return std::unique_ptr<QSettings>(new QSettings());
}
return std::unique_ptr<QSettings>(new QSettings(this->m_fileName, QSettings::IniFormat));
}
void MoneroSettings::swap(std::unique_ptr<QSettings> newSettings)
{
const QMetaObject *mo = this->metaObject();
const int count = mo->propertyCount();
for (int offset = mo->propertyOffset(); offset < count; ++offset)
{
const QMetaProperty &property = mo->property(offset);
const QVariant value = readProperty(property);
newSettings->setValue(property.name(), value);
}
this->m_settings.swap(newSettings);
this->m_settings->sync();
emit portableChanged();
}
void MoneroSettings::setFileName(const QString &fileName)
{
if (fileName != this->m_fileName) {
@ -183,6 +241,30 @@ QString MoneroSettings::fileName() const
return this->m_fileName;
}
bool MoneroSettings::setPortable(bool enabled)
{
std::unique_ptr<QSettings> newSettings = enabled ? portableSettings() : unportableSettings();
if (newSettings->status() != QSettings::NoError)
{
return false;
}
setWritable(true);
swap(std::move(newSettings));
if (!enabled)
{
QFile::remove(portableFilePath());
}
return true;
}
void MoneroSettings::setWritable(bool enabled)
{
m_writable = enabled;
}
void MoneroSettings::timerEvent(QTimerEvent *event)
{
if (event->timerId() == this->m_timerId) {

View file

@ -36,6 +36,8 @@
#ifndef MONEROSETTINGS_H
#define MONEROSETTINGS_H
#include <memory>
#include <QtQml/qqmlparserstatus.h>
#include <QGuiApplication>
#include <QClipboard>
@ -50,15 +52,23 @@ class MoneroSettings : public QObject, public QQmlParserStatus
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(QString fileName READ fileName WRITE setFileName FINAL)
Q_PROPERTY(bool portable READ portable NOTIFY portableChanged)
Q_PROPERTY(QString portableFolderName READ portableFolderName CONSTANT)
public:
explicit MoneroSettings(QObject *parent = nullptr);
QString fileName() const;
void setFileName(const QString &fileName);
Q_INVOKABLE bool setPortable(bool enabled);
Q_INVOKABLE void setWritable(bool enabled);
public slots:
void _q_propertyChanged();
signals:
void portableChanged() const;
protected:
void timerEvent(QTimerEvent *event) override;
void classBegin() override;
@ -71,10 +81,19 @@ private:
void load();
void store();
bool portable() const;
bool portableConfigExists() const;
QString portableFilePath() const;
QString portableFolderName() const;
std::unique_ptr<QSettings> portableSettings() const;
std::unique_ptr<QSettings> unportableSettings() const;
void swap(std::unique_ptr<QSettings> newSettings);
QHash<const char *, QVariant> m_changedProperties;
QSettings *m_settings = NULL;
std::unique_ptr<QSettings> m_settings;
QString m_fileName = QString("");
bool m_initialized = false;
bool m_writable = true;
int m_timerId = 0;
};