1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Merge pull request #4624

Diadlo (5):
      refactor: Improve interface
      refactor: Use signal + slot to update view
      refactor: Add IFriendSettings
      refactor: Add using IFriendSettings in AboutFriend model
      refactor: Update AboutFriend with new interface
This commit is contained in:
sudden6 2017-12-25 12:24:13 +01:00
commit a3797ee8c5
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
11 changed files with 153 additions and 98 deletions

View File

@ -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

View File

@ -3,24 +3,23 @@
#include "src/model/friend.h"
#include "src/nexus.h"
#include "src/persistence/profile.h"
#include "src/persistence/settings.h"
#include "src/persistence/ifriendsettings.h"
AboutFriend::AboutFriend(const Friend* f)
AboutFriend::AboutFriend(const Friend* f, IFriendSettings* const s)
: f{f}
, settings{s}
{
Settings* s = &Settings::getInstance();
connect(s, &Settings::contactNoteChanged, [=](const ToxPk& pk, const QString& note) {
s->connectTo_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<int>(Settings::getInstance().getAutoAcceptCall(pk));
const AutoAcceptCallFlags sFlag = static_cast<IAboutFriend::AutoAcceptCall>(value);
emit autoAcceptCallChanged(sFlag);
s->connectTo_autoAcceptCallChanged(
[=](const ToxPk& pk, IFriendSettings::AutoAcceptCallFlags flag) {
emit autoAcceptCallChanged(flag);
});
connect(s, &Settings::autoAcceptDirChanged, [=](const ToxPk& pk, const QString& dir) {
s->connectTo_autoAcceptDirChanged([=](const ToxPk& pk, const QString& dir) {
emit autoAcceptDirChanged(dir);
});
connect(s, &Settings::autoGroupInviteChanged, [=](const ToxPk& pk, bool enable) {
s->connectTo_autoGroupInviteChanged([=](const ToxPk& pk, bool enable) {
emit autoGroupInviteChanged(enable);
});
}
@ -51,56 +50,53 @@ QPixmap AboutFriend::getAvatar() const
QString AboutFriend::getNote() const
{
const ToxPk pk = f->getPublicKey();
return Settings::getInstance().getContactNote(pk);
return settings->getContactNote(pk);
}
void AboutFriend::setNote(const QString& note)
{
const ToxPk pk = f->getPublicKey();
Settings::getInstance().setContactNote(pk, note);
Settings::getInstance().savePersonal();
settings->setContactNote(pk, note);
settings->saveFriendSettings(pk);
}
QString AboutFriend::getAutoAcceptDir() const
{
const ToxPk pk = f->getPublicKey();
return Settings::getInstance().getAutoAcceptDir(pk);
return settings->getAutoAcceptDir(pk);
}
void AboutFriend::setAutoAcceptDir(const QString& path)
{
const ToxPk pk = f->getPublicKey();
Settings::getInstance().setAutoAcceptDir(pk, path);
Settings::getInstance().savePersonal();
settings->setAutoAcceptDir(pk, path);
settings->saveFriendSettings(pk);
}
IAboutFriend::AutoAcceptCallFlags AboutFriend::getAutoAcceptCall() const
IFriendSettings::AutoAcceptCallFlags AboutFriend::getAutoAcceptCall() const
{
const ToxPk pk = f->getPublicKey();
const int value = static_cast<int>(Settings::getInstance().getAutoAcceptCall(pk));
return static_cast<IAboutFriend::AutoAcceptCallFlags>(value);
return settings->getAutoAcceptCall(pk);
}
void AboutFriend::setAutoAcceptCall(AutoAcceptCallFlags flag)
void AboutFriend::setAutoAcceptCall(IFriendSettings::AutoAcceptCallFlags flag)
{
const ToxPk pk = f->getPublicKey();
const int value = static_cast<int>(flag);
const Settings::AutoAcceptCallFlags sFlag(value);
Settings::getInstance().setAutoAcceptCall(pk, sFlag);
Settings::getInstance().savePersonal();
settings->setAutoAcceptCall(pk, flag);
settings->saveFriendSettings(pk);
}
bool AboutFriend::getAutoGroupInvite() const
{
const ToxPk pk = f->getPublicKey();
return Settings::getInstance().getAutoGroupInvite(pk);
return settings->getAutoGroupInvite(pk);
}
void AboutFriend::setAutoGroupInvite(bool enabled)
{
const ToxPk pk = f->getPublicKey();
Settings::getInstance().setAutoGroupInvite(pk, enabled);
Settings::getInstance().savePersonal();
settings->setAutoGroupInvite(pk, enabled);
settings->saveFriendSettings(pk);
}
bool AboutFriend::clearHistory()

View File

@ -3,16 +3,19 @@
#include "iaboutfriend.h"
#include "src/persistence/ifriendsettings.h"
#include <QObject>
class Friend;
class IFriendSettings;
class AboutFriend : public IAboutFriend
{
Q_OBJECT
public:
explicit AboutFriend(const Friend* f);
AboutFriend(const Friend* f, IFriendSettings* const settings);
QString getName() const override;
QString getStatusMessage() const override;
@ -26,8 +29,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,11 +45,12 @@ 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:
const Friend* const f;
IFriendSettings* const settings;
};
#endif // ABOUT_FRIEND_H

View File

@ -2,6 +2,7 @@
#define I_ABOUT_FRIEND_H
#include "src/model/interface.h"
#include "src/persistence/ifriendsettings.h"
#include <QObject>
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<AutoAcceptCall>;
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;
@ -47,7 +39,7 @@ public:
DECLARE_SIGNAL(noteChanged, const QString&);
DECLARE_SIGNAL(autoAcceptDirChanged, const QString&);
DECLARE_SIGNAL(autoAcceptCallChanged, AutoAcceptCallFlags);
DECLARE_SIGNAL(autoAcceptCallChanged, IFriendSettings::AutoAcceptCallFlags);
DECLARE_SIGNAL(autoGroupInviteChanged, bool);
};

View File

@ -27,6 +27,9 @@
* SIGNAL_IMPL(Example, valueChanged, int value);
* };
*/
#define DECLARE_SIGNAL(name, ...) \
using Slot_##name = std::function<void (__VA_ARGS__)>; \
virtual void connectTo_##name(Slot_##name slot) const = 0
/**
* @def DECLARE_SIGNAL

View File

@ -0,0 +1,55 @@
#ifndef I_FRIEND_SETTINGS_H
#define I_FRIEND_SETTINGS_H
#include "src/model/interface.h"
#include <QObject>
#include <QFlag>
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

View File

@ -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};

View File

@ -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 <QDate>
@ -36,14 +37,14 @@
#include <QObject>
#include <QPixmap>
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

View File

@ -19,6 +19,7 @@ AboutFriendForm::AboutFriendForm(QPointer<IAboutFriend> 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);
about.data()->connectTo_autoAcceptDirChanged([=](const QString& dir){ onAutoAcceptDirChanged(dir); });
const QString dir = about->getAutoAcceptDir();
ui->autoacceptfile->setChecked(!dir.isEmpty());
@ -43,33 +44,39 @@ AboutFriendForm::AboutFriendForm(QPointer<IAboutFriend> about, QWidget* parent)
ui->avatar->setPixmap(about->getAvatar());
}
static QString getAutoAcceptDir(const QString& dir)
{
//: popup title
const QString title = AboutFriendForm::tr("Choose an auto accept directory");
return QFileDialog::getExistingDirectory(Q_NULLPTR, title, dir);
}
void AboutFriendForm::onAutoAcceptDirClicked()
{
if (!ui->autoacceptfile->isChecked()) {
ui->autoacceptfile->setChecked(false);
about->setAutoAcceptDir("");
ui->selectSaveDir->setText(tr("Auto accept for this contact is disabled"));
} else if (ui->autoacceptfile->isChecked()) {
QString dir = about->getAutoAcceptDir();
dir = QFileDialog::getExistingDirectory(
Q_NULLPTR, tr("Choose an auto accept directory", "popup title"), dir);
if (dir.isEmpty()) {
ui->autoacceptfile->setChecked(false);
return; // user canellced
const QString dir = [&]{
if (!ui->autoacceptfile->isChecked()) {
return QString{};
}
about->setAutoAcceptDir(dir);
ui->selectSaveDir->setText(about->getAutoAcceptDir());
}
return getAutoAcceptDir(about->getAutoAcceptDir());
}();
ui->selectSaveDir->setEnabled(ui->autoacceptfile->isChecked());
about->setAutoAcceptDir(dir);
}
void AboutFriendForm::onAutoAcceptDirChanged(const QString& path)
{
const bool enabled = path.isNull();
ui->autoacceptfile->setChecked(enabled);
ui->selectSaveDir->setEnabled(enabled);
ui->selectSaveDir->setText(enabled ? path : tr("Auto accept for this contact is disabled"));
}
void AboutFriendForm::onAutoAcceptCallClicked()
{
const int index = ui->autoacceptcall->currentIndex();
const IAboutFriend::AutoAcceptCall flag = static_cast<IAboutFriend::AutoAcceptCall>(index);
const IFriendSettings::AutoAcceptCallFlags flag{index};
about->setAutoAcceptCall(flag);
}
@ -83,10 +90,7 @@ void AboutFriendForm::onAutoGroupInvite()
void AboutFriendForm::onSelectDirClicked()
{
QString dir = about->getAutoAcceptDir();
dir = QFileDialog::getExistingDirectory(
Q_NULLPTR, tr("Choose an auto accept directory", "popup title"), dir);
const QString dir = getAutoAcceptDir(about->getAutoAcceptDir());
about->setAutoAcceptDir(dir);
}

View File

@ -15,7 +15,7 @@ class AboutFriendForm : public QDialog
Q_OBJECT
public:
explicit AboutFriendForm(QPointer<IAboutFriend> about, QWidget* parent = 0);
AboutFriendForm(QPointer<IAboutFriend> about, QWidget* parent = 0);
~AboutFriendForm();
private:
@ -23,6 +23,7 @@ private:
QPointer<IAboutFriend> about;
private slots:
void onAutoAcceptDirChanged(const QString& path);
void onAcceptedClicked();
void onAutoAcceptDirClicked();
void onAutoAcceptCallClicked();

View File

@ -230,7 +230,7 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
}
} else if (selectedItem == aboutWindow) {
const Friend* const f = FriendList::findFriend(friendId);
const QPointer<IAboutFriend> about = new AboutFriend(f);
const QPointer<IAboutFriend> about = new AboutFriend(f, &Settings::getInstance());
AboutFriendForm* aboutUser = new AboutFriendForm(about, Widget::getInstance());
aboutUser->show();
} else if (selectedItem == newGroupAction) {