Skycoder42
6 years ago
9 changed files with 366 additions and 85 deletions
@ -0,0 +1,96 @@ |
|||
#include "datasyncsettingsaccessor.h" |
|||
#include "datasyncsettingsaccessor_p.h" |
|||
#include <QtCore/QRegularExpression> |
|||
|
|||
#undef logDebug |
|||
#undef logInfo |
|||
#undef logWarning |
|||
#undef logCritical |
|||
#include <QtMvvmCore/private/qtmvvm_logging_p.h> |
|||
|
|||
using namespace QtMvvm; |
|||
|
|||
DataSyncSettingsAccessor::DataSyncSettingsAccessor(QObject *parent) : |
|||
DataSyncSettingsAccessor{QtDataSync::DefaultSetup, parent} |
|||
{} |
|||
|
|||
DataSyncSettingsAccessor::DataSyncSettingsAccessor(const QString &setupName, QObject *parent) : |
|||
DataSyncSettingsAccessor{new QtDataSync::DataTypeStore<DataSyncSettingsEntry>{setupName}, parent} |
|||
{ |
|||
d->store->setParent(this); |
|||
} |
|||
|
|||
DataSyncSettingsAccessor::DataSyncSettingsAccessor(QtDataSync::DataStore *store, QObject *parent) : |
|||
DataSyncSettingsAccessor{new QtDataSync::DataTypeStore<DataSyncSettingsEntry>{store}, parent} |
|||
{ |
|||
d->store->setParent(this); |
|||
} |
|||
|
|||
DataSyncSettingsAccessor::DataSyncSettingsAccessor(QtDataSync::DataTypeStore<DataSyncSettingsEntry> *store, QObject *parent) : |
|||
ISettingsAccessor{parent}, |
|||
d{new DataSyncSettingsAccessorPrivate{store}} |
|||
{} |
|||
|
|||
DataSyncSettingsAccessor::~DataSyncSettingsAccessor() = default; |
|||
|
|||
bool DataSyncSettingsAccessor::contains(const QString &key) const |
|||
{ |
|||
try { |
|||
return d->store->keys().contains(key); |
|||
} catch(QException &e) { |
|||
logCritical() << "Failed to check if entry" << key << "exists with error:" |
|||
<< e.what(); |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
QVariant DataSyncSettingsAccessor::load(const QString &key, const QVariant &defaultValue) const |
|||
{ |
|||
try { |
|||
return d->store->load(key).value(); |
|||
} catch (QtDataSync::NoDataException &e) { |
|||
Q_UNUSED(e) |
|||
return defaultValue; |
|||
} catch (QException &e) { |
|||
logCritical() << "Failed to load entry" << key << "from datasync settings with error:" |
|||
<< e.what(); |
|||
return defaultValue; |
|||
} |
|||
} |
|||
|
|||
void DataSyncSettingsAccessor::save(const QString &key, const QVariant &value) |
|||
{ |
|||
try { |
|||
d->store->save({key, value}); |
|||
} catch (QException &e) { |
|||
logCritical() << "Failed to save entry" << key << "to datasync settings with error:" |
|||
<< e.what(); |
|||
} |
|||
} |
|||
|
|||
void DataSyncSettingsAccessor::remove(const QString &key) |
|||
{ |
|||
try { |
|||
auto rmKeys = d->store->keys().filter(QRegularExpression{ |
|||
QStringLiteral(R"__(^%1\/.*)__").arg(QRegularExpression::escape(key)), |
|||
QRegularExpression::DontCaptureOption |
|||
}); |
|||
for(const auto &rmKey : rmKeys) |
|||
d->store->remove(rmKey); |
|||
d->store->remove(key); |
|||
} catch (QException &e) { |
|||
logCritical() << "Failed to remove entry" << key << "from datasync settings with error:" |
|||
<< e.what(); |
|||
} |
|||
} |
|||
|
|||
void DataSyncSettingsAccessor::sync() |
|||
{ |
|||
// nothing needs to be done
|
|||
} |
|||
|
|||
// ------------- Private Implementation -------------
|
|||
|
|||
DataSyncSettingsAccessorPrivate::DataSyncSettingsAccessorPrivate(QtDataSync::DataTypeStore<DataSyncSettingsEntry> *store) : |
|||
store{store} |
|||
{} |
@ -0,0 +1,40 @@ |
|||
#ifndef QTMVVM_DATASYNCSETTINGSACCESSOR_H |
|||
#define QTMVVM_DATASYNCSETTINGSACCESSOR_H |
|||
|
|||
#include <QtCore/qscopedpointer.h> |
|||
#include <QtMvvmCore/isettingsaccessor.h> |
|||
#include <QtDataSync/datatypestore.h> |
|||
|
|||
#include "QtMvvmDataSyncCore/qtmvvmdatasynccore_global.h" |
|||
#include "QtMvvmDataSyncCore/datasyncsettingsentry.h" |
|||
|
|||
namespace QtMvvm { |
|||
|
|||
class DataSyncSettingsAccessorPrivate; |
|||
class Q_MVVMDATASYNCCORE_EXPORT DataSyncSettingsAccessor : public ISettingsAccessor |
|||
{ |
|||
Q_OBJECT |
|||
Q_INTERFACES(QtMvvm::ISettingsAccessor) |
|||
|
|||
public: |
|||
Q_INVOKABLE explicit DataSyncSettingsAccessor(QObject *parent = nullptr); |
|||
explicit DataSyncSettingsAccessor(const QString &setupName, QObject *parent = nullptr); |
|||
explicit DataSyncSettingsAccessor(QtDataSync::DataStore *store, QObject *parent = nullptr); |
|||
explicit DataSyncSettingsAccessor(QtDataSync::DataTypeStore<DataSyncSettingsEntry> *store, QObject *parent = nullptr); |
|||
~DataSyncSettingsAccessor() override; |
|||
|
|||
bool contains(const QString &key) const override; |
|||
QVariant load(const QString &key, const QVariant &defaultValue) const override; |
|||
void save(const QString &key, const QVariant &value) override; |
|||
void remove(const QString &key) override; |
|||
|
|||
public Q_SLOTS: |
|||
void sync() override; |
|||
|
|||
private: |
|||
QScopedPointer<DataSyncSettingsAccessorPrivate> d; |
|||
}; |
|||
|
|||
} |
|||
|
|||
#endif // QTMVVM_DATASYNCSETTINGSACCESSOR_H
|
@ -0,0 +1,19 @@ |
|||
#ifndef QTMVVM_DATASYNCSETTINGSACCESSOR_P_H |
|||
#define QTMVVM_DATASYNCSETTINGSACCESSOR_P_H |
|||
|
|||
#include "qtmvvmdatasynccore_global.h" |
|||
#include "datasyncsettingsaccessor.h" |
|||
|
|||
namespace QtMvvm { |
|||
|
|||
class DataSyncSettingsAccessorPrivate |
|||
{ |
|||
public: |
|||
DataSyncSettingsAccessorPrivate(QtDataSync::DataTypeStore<DataSyncSettingsEntry> *store); |
|||
|
|||
QtDataSync::DataTypeStore<DataSyncSettingsEntry> *store; |
|||
}; |
|||
|
|||
} |
|||
|
|||
#endif // QTMVVM_DATASYNCSETTINGSACCESSOR_P_H
|
@ -0,0 +1,128 @@ |
|||
#include "datasyncsettingsentry.h" |
|||
#include <QtCore/QDataStream> |
|||
#if QT_HAS_INCLUDE(<optional>) |
|||
#include <optional> |
|||
#define QTMVVM_HAS_OPTIONAL |
|||
#endif |
|||
using namespace QtMvvm; |
|||
|
|||
namespace QtMvvm { |
|||
|
|||
class DataSyncSettingsEntryData : public QSharedData |
|||
{ |
|||
public: |
|||
inline DataSyncSettingsEntryData() = default; |
|||
inline DataSyncSettingsEntryData(QString key) : |
|||
key{std::move(key)} |
|||
{} |
|||
|
|||
QString key; |
|||
int version = QDataStream::Qt_DefaultCompiledVersion; |
|||
QByteArray data; |
|||
#ifdef QTMVVM_HAS_OPTIONAL |
|||
mutable std::optional<QVariant> value; |
|||
#else |
|||
mutable QVariant value; |
|||
#endif |
|||
}; |
|||
|
|||
} |
|||
|
|||
DataSyncSettingsEntry::DataSyncSettingsEntry() : |
|||
d{new DataSyncSettingsEntryData{}} |
|||
{} |
|||
|
|||
DataSyncSettingsEntry::~DataSyncSettingsEntry() = default; |
|||
|
|||
DataSyncSettingsEntry::DataSyncSettingsEntry(const DataSyncSettingsEntry &other) = default; |
|||
|
|||
DataSyncSettingsEntry::DataSyncSettingsEntry(DataSyncSettingsEntry &&other) noexcept = default; |
|||
|
|||
DataSyncSettingsEntry &DataSyncSettingsEntry::operator=(const DataSyncSettingsEntry &other) = default; |
|||
|
|||
DataSyncSettingsEntry &DataSyncSettingsEntry::operator=(DataSyncSettingsEntry &&other) noexcept = default; |
|||
|
|||
DataSyncSettingsEntry::DataSyncSettingsEntry(QString key, QVariant value) : |
|||
d{new DataSyncSettingsEntryData{std::move(key)}} |
|||
{ |
|||
setValue(std::move(value)); |
|||
} |
|||
|
|||
QString DataSyncSettingsEntry::key() const |
|||
{ |
|||
return d->key; |
|||
} |
|||
|
|||
int DataSyncSettingsEntry::dataVersion() const |
|||
{ |
|||
return d->version; |
|||
} |
|||
|
|||
QVariant DataSyncSettingsEntry::value() const |
|||
{ |
|||
#ifdef QTMVVM_HAS_OPTIONAL |
|||
if(!d->data.isNull() && !d->value) { |
|||
QDataStream stream{d->data}; |
|||
stream.setVersion(d->version); |
|||
d->value = QVariant{}; |
|||
stream >> d->value.value(); |
|||
} |
|||
return d->value.value_or(QVariant{}); |
|||
#else |
|||
if(!d->data.isNull() && !d->value.isValid()) { |
|||
QDataStream stream{d->data}; |
|||
stream.setVersion(d->version); |
|||
stream >> d->value; |
|||
} |
|||
return d->value; |
|||
#endif |
|||
} |
|||
|
|||
void DataSyncSettingsEntry::setKey(QString key) |
|||
{ |
|||
d->key = std::move(key); |
|||
} |
|||
|
|||
void DataSyncSettingsEntry::setDataVersion(int dataVersion) |
|||
{ |
|||
if(d->version == dataVersion) |
|||
return; |
|||
|
|||
if(d->data.isNull()) |
|||
d->version = dataVersion; |
|||
else { |
|||
// update the internally stored data based on the version
|
|||
auto mValue = value(); |
|||
d->version = dataVersion; |
|||
setValue(mValue); |
|||
} |
|||
} |
|||
|
|||
void DataSyncSettingsEntry::setValue(QVariant value) |
|||
{ |
|||
#ifdef QTMVVM_HAS_OPTIONAL |
|||
d->value.reset(); |
|||
#else |
|||
d->value.clear(); |
|||
#endif |
|||
d->data.clear(); |
|||
QDataStream stream{&d->data, QIODevice::WriteOnly}; |
|||
stream.setVersion(d->version); |
|||
stream << value; |
|||
d->value = std::move(value); |
|||
} |
|||
|
|||
QByteArray DataSyncSettingsEntry::valueData() const |
|||
{ |
|||
return d->data; |
|||
} |
|||
|
|||
void DataSyncSettingsEntry::setValueData(QByteArray data) |
|||
{ |
|||
#ifdef QTMVVM_HAS_OPTIONAL |
|||
d->value.reset(); |
|||
#else |
|||
d->value.clear(); |
|||
#endif |
|||
d->data = std::move(data); |
|||
} |
@ -0,0 +1,70 @@ |
|||
#ifndef QTMVVM_DATASYNCSETTINGSENTRY_H |
|||
#define QTMVVM_DATASYNCSETTINGSENTRY_H |
|||
|
|||
#include <QtCore/qobject.h> |
|||
#include <QtCore/qvariant.h> |
|||
#include <QtCore/qshareddata.h> |
|||
|
|||
#include "QtMvvmDataSyncCore/qtmvvmdatasynccore_global.h" |
|||
|
|||
namespace QtMvvm { |
|||
|
|||
class DataSyncSettingsEntryData; |
|||
struct Q_MVVMDATASYNCCORE_EXPORT DataSyncSettingsEntry |
|||
{ |
|||
Q_GADGET |
|||
|
|||
Q_PROPERTY(QString key READ key WRITE setKey USER true) |
|||
Q_PROPERTY(int dataVersion READ dataVersion WRITE setDataVersion) |
|||
Q_PROPERTY(QByteArray value READ valueData WRITE setValueData) |
|||
|
|||
Q_PROPERTY(QVariant variantValue READ value WRITE setValue STORED false) |
|||
|
|||
public: |
|||
DataSyncSettingsEntry(); |
|||
~DataSyncSettingsEntry(); |
|||
DataSyncSettingsEntry(const DataSyncSettingsEntry &other); |
|||
DataSyncSettingsEntry(DataSyncSettingsEntry &&other) noexcept; |
|||
DataSyncSettingsEntry &operator=(const DataSyncSettingsEntry &other); |
|||
DataSyncSettingsEntry &operator=(DataSyncSettingsEntry &&other) noexcept; |
|||
|
|||
DataSyncSettingsEntry(QString key, QVariant value); |
|||
template <typename T> |
|||
DataSyncSettingsEntry(QString key, const T &value); |
|||
|
|||
QString key() const; |
|||
int dataVersion() const; |
|||
|
|||
QVariant value() const; |
|||
template <typename T> |
|||
T value() const; |
|||
|
|||
void setKey(QString key); |
|||
void setDataVersion(int dataVersion); |
|||
void setValue(QVariant value); |
|||
|
|||
private: |
|||
QByteArray valueData() const; |
|||
void setValueData(QByteArray value); |
|||
|
|||
QSharedDataPointer<DataSyncSettingsEntryData> d; |
|||
}; |
|||
|
|||
// ------------- Generic Implementation -------------
|
|||
|
|||
template<typename T> |
|||
DataSyncSettingsEntry::DataSyncSettingsEntry(QString key, const T &value) : |
|||
DataSyncSettingsEntry{std::move(key), QVariant::fromValue(value)} |
|||
{} |
|||
|
|||
template<typename T> |
|||
T DataSyncSettingsEntry::value() const |
|||
{ |
|||
return value().template value<T>(); |
|||
} |
|||
|
|||
} |
|||
|
|||
Q_DECLARE_METATYPE(QtMvvm::DataSyncSettingsEntry) |
|||
|
|||
#endif // QTMVVM_DATASYNCSETTINGSENTRY_H
|
Loading…
Reference in new issue