From e4537c04bcef03202b55c12a00dfc47195f15beb Mon Sep 17 00:00:00 2001 From: Diadlo Date: Wed, 30 Aug 2017 12:04:43 +0300 Subject: [PATCH] refactor: Add IFriendSettings --- CMakeLists.txt | 1 + src/model/about/aboutfriend.cpp | 18 ++++----- src/model/about/aboutfriend.h | 6 +-- src/model/about/iaboutfriend.h | 14 ++----- src/persistence/ifriendsettings.h | 55 +++++++++++++++++++++++++++ src/persistence/settings.cpp | 6 +++ src/persistence/settings.h | 57 ++++++++++++---------------- src/widget/about/aboutfriendform.cpp | 4 +- 8 files changed, 102 insertions(+), 59 deletions(-) create mode 100644 src/persistence/ifriendsettings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c8050ff2..82182bd9b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -286,6 +286,7 @@ set(${PROJECT_NAME}_SOURCES src/persistence/db/rawdatabase.h src/persistence/history.cpp src/persistence/history.h + src/persistence/ifriendsettings.h src/persistence/offlinemsgengine.cpp src/persistence/offlinemsgengine.h src/persistence/profile.cpp diff --git a/src/model/about/aboutfriend.cpp b/src/model/about/aboutfriend.cpp index 2ee40054e..ca04912cc 100644 --- a/src/model/about/aboutfriend.cpp +++ b/src/model/about/aboutfriend.cpp @@ -12,10 +12,9 @@ AboutFriend::AboutFriend(const Friend* f) connect(s, &Settings::contactNoteChanged, [=](const ToxPk& pk, const QString& note) { emit noteChanged(note); }); - connect(s, &Settings::autoAcceptCallChanged, [=](const ToxPk& pk, Settings::AutoAcceptCallFlags flag) { - const int value = static_cast(Settings::getInstance().getAutoAcceptCall(pk)); - const AutoAcceptCallFlags sFlag = static_cast(value); - emit autoAcceptCallChanged(sFlag); + connect(s, &Settings::autoAcceptCallChanged, + [=](const ToxPk& pk, IFriendSettings::AutoAcceptCallFlags flag) { + emit autoAcceptCallChanged(flag); }); connect(s, &Settings::autoAcceptDirChanged, [=](const ToxPk& pk, const QString& dir) { emit autoAcceptDirChanged(dir); @@ -74,19 +73,16 @@ void AboutFriend::setAutoAcceptDir(const QString& path) Settings::getInstance().savePersonal(); } -IAboutFriend::AutoAcceptCallFlags AboutFriend::getAutoAcceptCall() const +IFriendSettings::AutoAcceptCallFlags AboutFriend::getAutoAcceptCall() const { const ToxPk pk = f->getPublicKey(); - const int value = static_cast(Settings::getInstance().getAutoAcceptCall(pk)); - return static_cast(value); + return Settings::getInstance().getAutoAcceptCall(pk); } -void AboutFriend::setAutoAcceptCall(AutoAcceptCallFlags flag) +void AboutFriend::setAutoAcceptCall(IFriendSettings::AutoAcceptCallFlags flag) { const ToxPk pk = f->getPublicKey(); - const int value = static_cast(flag); - const Settings::AutoAcceptCallFlags sFlag(value); - Settings::getInstance().setAutoAcceptCall(pk, sFlag); + Settings::getInstance().setAutoAcceptCall(pk, flag); Settings::getInstance().savePersonal(); } diff --git a/src/model/about/aboutfriend.h b/src/model/about/aboutfriend.h index c67d2dfae..963a6befd 100644 --- a/src/model/about/aboutfriend.h +++ b/src/model/about/aboutfriend.h @@ -26,8 +26,8 @@ public: QString getAutoAcceptDir() const override; void setAutoAcceptDir(const QString& path) override; - AutoAcceptCallFlags getAutoAcceptCall() const override; - void setAutoAcceptCall(AutoAcceptCallFlags flag) override; + IFriendSettings::AutoAcceptCallFlags getAutoAcceptCall() const override; + void setAutoAcceptCall(IFriendSettings::AutoAcceptCallFlags flag) override; bool getAutoGroupInvite() const override; void setAutoGroupInvite(bool enabled) override; @@ -42,7 +42,7 @@ public: SIGNAL_IMPL(AboutFriend, noteChanged, const QString&) SIGNAL_IMPL(AboutFriend, autoAcceptDirChanged, const QString&) - SIGNAL_IMPL(AboutFriend, autoAcceptCallChanged, AutoAcceptCallFlags) + SIGNAL_IMPL(AboutFriend, autoAcceptCallChanged, IFriendSettings::AutoAcceptCallFlags) SIGNAL_IMPL(AboutFriend, autoGroupInviteChanged, bool) private: diff --git a/src/model/about/iaboutfriend.h b/src/model/about/iaboutfriend.h index 32497a090..55dc67560 100644 --- a/src/model/about/iaboutfriend.h +++ b/src/model/about/iaboutfriend.h @@ -2,6 +2,7 @@ #define I_ABOUT_FRIEND_H #include "src/model/interface.h" +#include "src/persistence/ifriendsettings.h" #include class IAboutFriend : public QObject @@ -9,15 +10,6 @@ class IAboutFriend : public QObject Q_OBJECT public: - enum class AutoAcceptCall - { - None = 0x00, - Audio = 0x01, - Video = 0x02, - AV = Audio | Video - }; - using AutoAcceptCallFlags = QFlags; - virtual QString getName() const = 0; virtual QString getStatusMessage() const = 0; virtual QString getPublicKey() const = 0; @@ -30,8 +22,8 @@ public: virtual QString getAutoAcceptDir() const = 0; virtual void setAutoAcceptDir(const QString& path) = 0; - virtual AutoAcceptCallFlags getAutoAcceptCall() const = 0; - virtual void setAutoAcceptCall(AutoAcceptCallFlags flag) = 0; + virtual IFriendSettings::AutoAcceptCallFlags getAutoAcceptCall() const = 0; + virtual void setAutoAcceptCall(IFriendSettings::AutoAcceptCallFlags flag) = 0; virtual bool getAutoGroupInvite() const = 0; virtual void setAutoGroupInvite(bool enabled) = 0; diff --git a/src/persistence/ifriendsettings.h b/src/persistence/ifriendsettings.h new file mode 100644 index 000000000..857fc3be9 --- /dev/null +++ b/src/persistence/ifriendsettings.h @@ -0,0 +1,55 @@ +#ifndef I_FRIEND_SETTINGS_H +#define I_FRIEND_SETTINGS_H + +#include "src/model/interface.h" + +#include +#include + +class ToxPk; + +class IFriendSettings +{ +public: + enum class AutoAcceptCall + { + None = 0x00, + Audio = 0x01, + Video = 0x02, + AV = Audio | Video + }; + Q_DECLARE_FLAGS(AutoAcceptCallFlags, AutoAcceptCall) + + virtual QString getContactNote(const ToxPk& pk) const = 0; + virtual void setContactNote(const ToxPk& pk, const QString& note) = 0; + + virtual QString getAutoAcceptDir(const ToxPk& pk) const = 0; + virtual void setAutoAcceptDir(const ToxPk& pk, const QString& dir) = 0; + + virtual AutoAcceptCallFlags getAutoAcceptCall(const ToxPk& pk) const = 0; + virtual void setAutoAcceptCall(const ToxPk& pk, AutoAcceptCallFlags accept) = 0; + + virtual bool getAutoGroupInvite(const ToxPk& pk) const = 0; + virtual void setAutoGroupInvite(const ToxPk& pk, bool accept) = 0; + + virtual QString getFriendAlias(const ToxPk& pk) const = 0; + virtual void setFriendAlias(const ToxPk& pk, const QString& alias) = 0; + + virtual int getFriendCircleID(const ToxPk& pk) const = 0; + virtual void setFriendCircleID(const ToxPk& pk, int circleID) = 0; + + virtual QDate getFriendActivity(const ToxPk& pk) const = 0; + virtual void setFriendActivity(const ToxPk& pk, const QDate& date) = 0; + + virtual void saveFriendSettings(const ToxPk& pk) = 0; + virtual void removeFriendSettings(const ToxPk& pk) = 0; + +signals: + DECLARE_SIGNAL(autoAcceptCallChanged, const ToxPk& pk, AutoAcceptCallFlags accept); + DECLARE_SIGNAL(autoGroupInviteChanged, const ToxPk& pk, bool accept); + DECLARE_SIGNAL(autoAcceptDirChanged, const ToxPk& pk, const QString& dir); + DECLARE_SIGNAL(contactNoteChanged, const ToxPk& pk, const QString& note); +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(IFriendSettings::AutoAcceptCallFlags) +#endif // I_FRIEND_SETTINGS_H diff --git a/src/persistence/settings.cpp b/src/persistence/settings.cpp index af58507dd..8b4e2bbe6 100644 --- a/src/persistence/settings.cpp +++ b/src/persistence/settings.cpp @@ -2107,6 +2107,12 @@ void Settings::setFriendActivity(const ToxPk& id, const QDate& activity) } } +void Settings::saveFriendSettings(const ToxPk& id) +{ + Q_UNUSED(id); + savePersonal(); +} + void Settings::removeFriendSettings(const ToxPk& id) { QMutexLocker locker{&bigLock}; diff --git a/src/persistence/settings.h b/src/persistence/settings.h index 58a9053d1..3a7a86562 100644 --- a/src/persistence/settings.h +++ b/src/persistence/settings.h @@ -25,6 +25,7 @@ #include "src/core/icoresettings.h" #include "src/core/toxencrypt.h" #include "src/core/toxfile.h" +#include "src/persistence/ifriendsettings.h" #include "src/video/ivideosettings.h" #include @@ -36,14 +37,14 @@ #include #include -class ToxPk; class Profile; namespace Db { enum class syncType; } -class Settings : public QObject, public ICoreSettings, public IAudioSettings, public IVideoSettings +class Settings : public QObject, public ICoreSettings, public IFriendSettings, + public IAudioSettings, public IVideoSettings { Q_OBJECT @@ -126,14 +127,6 @@ public: WITH_CHARS = 1, WITHOUT_CHARS = 2 }; - enum class AutoAcceptCall - { - None = 0x00, - Audio = 0x01, - Video = 0x02, - AV = Audio | Video - }; - Q_DECLARE_FLAGS(AutoAcceptCallFlags, AutoAcceptCall) public: static Settings& getInstance(); @@ -191,12 +184,6 @@ signals: void checkUpdatesChanged(bool enabled); void widgetDataChanged(const QString& key); - // Friend - void autoAcceptCallChanged(const ToxPk& id, AutoAcceptCallFlags accept); - void autoGroupInviteChanged(const ToxPk& id, bool accept); - void autoAcceptDirChanged(const ToxPk& id, const QString& dir); - void contactNoteChanged(const ToxPk& id, const QString& note); - // GUI void autoLoginChanged(bool enabled); void separateWindowChanged(bool enabled); @@ -421,20 +408,20 @@ public: int getEmojiFontPointSize() const; void setEmojiFontPointSize(int value); - QString getContactNote(const ToxPk& id) const; - void setContactNote(const ToxPk& id, const QString& note); + QString getContactNote(const ToxPk& id) const override; + void setContactNote(const ToxPk& id, const QString& note) override; - QString getAutoAcceptDir(const ToxPk& id) const; - void setAutoAcceptDir(const ToxPk& id, const QString& dir); + QString getAutoAcceptDir(const ToxPk& id) const override; + void setAutoAcceptDir(const ToxPk& id, const QString& dir) override; - AutoAcceptCallFlags getAutoAcceptCall(const ToxPk& id) const; - void setAutoAcceptCall(const ToxPk& id, AutoAcceptCallFlags accept); + AutoAcceptCallFlags getAutoAcceptCall(const ToxPk& id) const override; + void setAutoAcceptCall(const ToxPk& id, AutoAcceptCallFlags accept) override; QString getGlobalAutoAcceptDir() const; void setGlobalAutoAcceptDir(const QString& dir); - bool getAutoGroupInvite(const ToxPk& id) const; - void setAutoGroupInvite(const ToxPk& id, bool accept); + bool getAutoGroupInvite(const ToxPk& id) const override; + void setAutoGroupInvite(const ToxPk& id, bool accept) override; // ChatView const QFont& getChatMessageFont() const; @@ -480,16 +467,23 @@ public: QString getFriendAddress(const QString& publicKey) const; void updateFriendAddress(const QString& newAddr); - QString getFriendAlias(const ToxPk& id) const; - void setFriendAlias(const ToxPk& id, const QString& alias); + QString getFriendAlias(const ToxPk& id) const override; + void setFriendAlias(const ToxPk& id, const QString& alias) override; - int getFriendCircleID(const ToxPk& id) const; - void setFriendCircleID(const ToxPk& id, int circleID); + int getFriendCircleID(const ToxPk& id) const override; + void setFriendCircleID(const ToxPk& id, int circleID) override; - QDate getFriendActivity(const ToxPk& id) const; - void setFriendActivity(const ToxPk& id, const QDate& date); + QDate getFriendActivity(const ToxPk& id) const override; + void setFriendActivity(const ToxPk& id, const QDate& date) override; - void removeFriendSettings(const ToxPk& id); + void saveFriendSettings(const ToxPk& id) override; + void removeFriendSettings(const ToxPk& id) override; + + SIGNAL_IMPL(Settings, autoAcceptCallChanged, + const ToxPk& id, IFriendSettings::AutoAcceptCallFlags accept) + SIGNAL_IMPL(Settings, autoGroupInviteChanged, const ToxPk& id, bool accept) + SIGNAL_IMPL(Settings, autoAcceptDirChanged, const ToxPk& id, const QString& dir) + SIGNAL_IMPL(Settings, contactNoteChanged, const ToxPk& id, const QString& note) bool getFauxOfflineMessaging() const; void setFauxOfflineMessaging(bool value); @@ -690,5 +684,4 @@ private: static QThread* settingsThread; }; -Q_DECLARE_OPERATORS_FOR_FLAGS(Settings::AutoAcceptCallFlags) #endif // SETTINGS_HPP diff --git a/src/widget/about/aboutfriendform.cpp b/src/widget/about/aboutfriendform.cpp index f2a6a2a8a..0c47a26cb 100644 --- a/src/widget/about/aboutfriendform.cpp +++ b/src/widget/about/aboutfriendform.cpp @@ -19,7 +19,7 @@ AboutFriendForm::AboutFriendForm(QPointer about, QWidget* parent) connect(ui->autogroupinvite, &QCheckBox::clicked, this, &AboutFriendForm::onAutoGroupInvite); connect(ui->selectSaveDir, &QPushButton::clicked, this, &AboutFriendForm::onSelectDirClicked); connect(ui->removeHistory, &QPushButton::clicked, this, &AboutFriendForm::onRemoveHistoryClicked); - connect(about.data(), &IAboutFriend::autoAcceptDirChanged, this, &AboutFriendForm::onAutoAcceptDirChanged); + about.data()->connectTo_autoAcceptDirChanged([=](const QString& dir){ onAutoAcceptDirChanged(dir); }); const QString dir = about->getAutoAcceptDir(); ui->autoacceptfile->setChecked(!dir.isEmpty()); @@ -76,7 +76,7 @@ void AboutFriendForm::onAutoAcceptDirChanged(const QString& path) void AboutFriendForm::onAutoAcceptCallClicked() { const int index = ui->autoacceptcall->currentIndex(); - const IAboutFriend::AutoAcceptCall flag = static_cast(index); + const IFriendSettings::AutoAcceptCallFlags flag{index}; about->setAutoAcceptCall(flag); }