core: create qPrefCloudStorage from SettingsObjectWrapper

Update set/get functions to follow common name scheme:
- get function have same name as in struct preferences
- set function have set_<name> (from struct preferences>)
- signal function have <name>_changed (from struct preferences>)

one class one .h/.cpp is the C++ idiom. Having load/sync of each
variable in 1 functions (in contrast to the distributed way SettingsObjectWrapper
handles it) secures the same storage name is used. Having the set/get/load/sync
functions grouped together makes it easier to get an overview.

REMARK: this commit only defines the class, it is not active in production
Signed-off-by: Jan Iversen <jani@apache.org>
This commit is contained in:
jan Iversen 2018-07-12 21:01:31 +02:00 committed by Dirk Hohndel
parent f23425c558
commit 4b68329c9d
6 changed files with 217 additions and 0 deletions

View file

@ -101,6 +101,7 @@ set(SUBSURFACE_CORE_LIB_SRCS
# classes to manage struct preferences for QWidget and QML
settings/qPref.cpp
settings/qPrefAnimations.cpp
settings/qPrefCloudStorage.cpp
settings/qPrefDisplay.cpp
settings/qPrefPrivate.cpp

View file

@ -6,6 +6,7 @@
#include "core/pref.h"
#include "qPrefAnimations.h"
#include "qPrefCloudStorage.h"
#include "qPrefDisplay.h"
class qPref : public QObject {

View file

@ -0,0 +1,116 @@
// SPDX-License-Identifier: GPL-2.0
#include "qPref.h"
#include "qPrefPrivate.h"
static const QString group = QStringLiteral("CloudStorage");
qPrefCloudStorage::qPrefCloudStorage(QObject *parent) : QObject(parent)
{
}
qPrefCloudStorage*qPrefCloudStorage::instance()
{
static qPrefCloudStorage *self = new qPrefCloudStorage;
return self;
}
void qPrefCloudStorage::loadSync(bool doSync)
{
disk_cloud_base_url(doSync);
disk_cloud_git_url(doSync);
disk_cloud_storage_email(doSync);
disk_cloud_storage_email_encoded(doSync);
disk_cloud_storage_password(doSync);
disk_cloud_storage_pin(doSync);
disk_cloud_timeout(doSync);
disk_cloud_verification_status(doSync);
disk_git_local_only(doSync);
disk_save_password_local(doSync);
disk_save_userid_local(doSync);
disk_userid(doSync);
}
GET_PREFERENCE_TXT(CloudStorage, cloud_base_url);
void qPrefCloudStorage::set_cloud_base_url(const QString& value)
{
if (value != prefs.cloud_base_url) {
// only free and set if not default
if (prefs.cloud_base_url != default_prefs.cloud_base_url) {
COPY_TXT(cloud_base_url, value);
COPY_TXT(cloud_git_url, QString(prefs.cloud_base_url) + "/git");
}
disk_cloud_base_url(true);
emit cloud_base_url_changed(value);
}
}
void qPrefCloudStorage::disk_cloud_base_url(bool doSync)
{
LOADSYNC_TXT("/cloud_base_url", cloud_base_url);
LOADSYNC_TXT("/cloud_git_url", cloud_git_url);
}
GET_PREFERENCE_TXT(CloudStorage, cloud_git_url);
void qPrefCloudStorage::set_cloud_git_url(const QString& value)
{
if (value != prefs.cloud_git_url) {
// only free and set if not default
if (prefs.cloud_git_url != default_prefs.cloud_git_url) {
COPY_TXT(cloud_git_url, value);
}
disk_cloud_git_url(true);
emit cloud_git_url_changed(value);
}
}
DISK_LOADSYNC_TXT(CloudStorage, "/cloud_git_url", cloud_git_url)
HANDLE_PREFERENCE_TXT(CloudStorage, "/email", cloud_storage_email);
HANDLE_PREFERENCE_TXT(CloudStorage, "/email_encoded", cloud_storage_email_encoded);
GET_PREFERENCE_TXT(CloudStorage, cloud_storage_newpassword);
void qPrefCloudStorage::set_cloud_storage_newpassword(const QString& value)
{
if (value == prefs.cloud_storage_newpassword)
return;
COPY_TXT(cloud_storage_newpassword, value);
// NOT saved on disk, because it is only temporary
emit cloud_storage_newpassword_changed(value);
}
GET_PREFERENCE_TXT(CloudStorage, cloud_storage_password);
void qPrefCloudStorage::set_cloud_storage_password(const QString& value)
{
if (value != prefs.cloud_storage_password) {
COPY_TXT(cloud_storage_password,value);
disk_cloud_storage_password(true);
emit cloud_storage_password_changed(value);
}
}
void qPrefCloudStorage::disk_cloud_storage_password(bool doSync)
{
if (!doSync || prefs.save_password_local)
LOADSYNC_TXT("/password", cloud_storage_password);
}
HANDLE_PREFERENCE_TXT(CloudStorage, "/pin", cloud_storage_pin);
HANDLE_PREFERENCE_INT(CloudStorage, "/timeout", cloud_timeout);
HANDLE_PREFERENCE_INT(CloudStorage, "/cloud_verification_status", cloud_verification_status);
HANDLE_PREFERENCE_BOOL(CloudStorage, "/git_local_only", git_local_only);
HANDLE_PREFERENCE_BOOL(CloudStorage, "/save_password_local", save_password_local);
HANDLE_PREFERENCE_BOOL(CloudStorage, "/save_userid_local", save_userid_local);
GET_PREFERENCE_TXT(CloudStorage, userid);
SET_PREFERENCE_TXT(CloudStorage, userid);
void qPrefCloudStorage::disk_userid(bool doSync)
{
//WARNING: UserId is stored outside of any group, but it belongs to Cloud Storage.
const QString group = QStringLiteral("");
LOADSYNC_TXT("subsurface_webservice_uid", userid);
}

View file

@ -0,0 +1,94 @@
// SPDX-License-Identifier: GPL-2.0
#ifndef QPREFCLOUDSTORAGE_H
#define QPREFCLOUDSTORAGE_H
#include <QObject>
class qPrefCloudStorage : public QObject {
Q_OBJECT
Q_PROPERTY(QString cloud_base_url READ cloud_base_url WRITE set_cloud_base_url NOTIFY cloud_base_url_changed);
Q_PROPERTY(QString cloud_git_url READ cloud_git_url WRITE set_cloud_git_url NOTIFY cloud_git_url_changed);
Q_PROPERTY(QString cloud_storage_email READ cloud_storage_email WRITE set_cloud_storage_email NOTIFY cloud_storage_email_changed);
Q_PROPERTY(QString cloud_storage_email_encoded READ cloud_storage_email_encoded WRITE set_cloud_storage_email_encoded NOTIFY cloud_storage_email_encoded_changed);
Q_PROPERTY(QString cloud_storage_newpassword READ cloud_storage_newpassword WRITE set_cloud_storage_newpassword NOTIFY cloud_storage_newpassword_changed);
Q_PROPERTY(QString cloud_storage_password READ cloud_storage_password WRITE set_cloud_storage_password NOTIFY cloud_storage_password_changed);
Q_PROPERTY(QString cloud_storage_pin READ cloud_storage_pin WRITE set_cloud_storage_pin NOTIFY cloud_storage_pin_changed);
Q_PROPERTY(int cloud_verification_status READ cloud_verification_status WRITE set_cloud_verification_status NOTIFY cloud_verification_status_changed);
Q_PROPERTY(int cloud_timeout READ cloud_timeout WRITE set_cloud_timeout NOTIFY cloud_timeout_changed);
Q_PROPERTY(bool git_local_only READ git_local_only WRITE set_git_local_only NOTIFY git_local_only_changed);
Q_PROPERTY(bool save_password_local READ save_password_local WRITE set_save_password_local NOTIFY save_password_local_changed);
Q_PROPERTY(bool save_userid_local READ save_userid_local WRITE set_save_userid_local NOTIFY save_userid_local_changed);
Q_PROPERTY(QString userid READ userid WRITE set_userid NOTIFY userid_changed);
public:
qPrefCloudStorage(QObject *parent = NULL);
static qPrefCloudStorage *instance();
// Load/Sync local settings (disk) and struct preference
void loadSync(bool doSync);
void load() { loadSync(false); }
void sync() { loadSync(true); }
public:
const QString cloud_base_url() const;
const QString cloud_git_url() const;
const QString cloud_storage_email() const;
const QString cloud_storage_email_encoded() const;
const QString cloud_storage_newpassword() const;
const QString cloud_storage_password() const;
const QString cloud_storage_pin() const;
int cloud_timeout() const;
int cloud_verification_status() const;
bool git_local_only() const;
bool save_password_local() const;
bool save_userid_local() const;
const QString userid() const;
public slots:
void set_cloud_base_url(const QString& value);
void set_cloud_git_url(const QString& value);
void set_cloud_storage_email(const QString& value);
void set_cloud_storage_email_encoded(const QString& value);
void set_cloud_storage_newpassword(const QString& value);
void set_cloud_storage_password(const QString& value);
void set_cloud_storage_pin(const QString& value);
void set_cloud_timeout(int value);
void set_cloud_verification_status(int value);
void set_git_local_only(bool value);
void set_save_password_local(bool value);
void set_save_userid_local(bool value);
void set_userid(const QString& value);
signals:
void cloud_base_url_changed(const QString& value);
void cloud_git_url_changed(const QString& value);
void cloud_storage_email_changed(const QString& value);
void cloud_storage_email_encoded_changed(const QString& value);
void cloud_storage_newpassword_changed(const QString& value);
void cloud_storage_password_changed(const QString& value);
void cloud_storage_pin_changed(const QString& value);
void cloud_timeout_changed(int value);
void cloud_verification_status_changed(int value);
void git_local_only_changed(bool value);
void save_password_local_changed(bool value);
void save_userid_local_changed(bool value);
void userid_changed(const QString& value);
private:
// functions to load/sync variable with disk
void disk_cloud_base_url(bool doSync);
void disk_cloud_git_url(bool doSync);
void disk_cloud_storage_email(bool doSync);
void disk_cloud_storage_email_encoded(bool doSync);
void disk_cloud_storage_newpassword(bool doSync);
void disk_cloud_storage_password(bool doSync);
void disk_cloud_storage_pin(bool doSync);
void disk_cloud_timeout(bool doSync);
void disk_cloud_verification_status(bool doSync);
void disk_git_local_only(bool doSync);
void disk_save_password_local(bool doSync);
void disk_save_userid_local(bool doSync);
void disk_userid(bool doSync);
};
#endif

View file

@ -79,6 +79,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \
../../core/qt-ble.cpp \
../../core/settings/qPref.cpp \
../../core/settings/qPrefAnimations.cpp \
../../core/settings/qPrefCloudStorage.cpp \
../../core/settings/qPrefDisplay.cpp \
../../core/settings/qPrefPrivate.cpp \
../../core/subsurface-qt/CylinderObjectHelper.cpp \
@ -189,6 +190,7 @@ HEADERS += \
../../core/qt-ble.h \
../../core/settings/qPref.h \
../../core/settings/qPrefAnimations.h \
../../core/settings/qPrefCloudStorage.h \
../../core/settings/qPrefDisplay.h \
../../core/settings/qPrefPrivate.h \
../../core/subsurface-qt/CylinderObjectHelper.h \

View file

@ -148,6 +148,9 @@ void register_qml_types()
rc = qmlRegisterType<qPrefAnimations>("org.subsurfacedivelog.mobile", 1, 0, "SsrfAnimationsPrefs");
if (rc < 0)
qDebug() << "ERROR: Cannot register SsrfAnimationsPrefs (class qPrefAnimations), QML will not work!!";
rc = qmlRegisterType<qPrefCloudStorage>("org.subsurfacedivelog.mobile", 1, 0, "SsrfCloudStoragePrefs");
if (rc < 0)
qDebug() << "ERROR: Cannot register SsrfCloudStoragePrefs (class qPrefCloudStorage), QML will not work!!";
rc = qmlRegisterType<qPrefDisplay>("org.subsurfacedivelog.mobile", 1, 0, "SsrfDisplayPrefs");
if (rc < 0)
qDebug() << "ERROR: Cannot register DisplayPrefs (class qPrefDisplay), QML will not work!!";