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

refactor: remove Core::getInstance from AvatarBroadcaster

During this process, make AvatarBroadcaster a non-static class.
This commit is contained in:
sudden6 2020-05-10 00:26:01 +02:00
parent 82e0852f3c
commit f3a10815ed
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
4 changed files with 39 additions and 28 deletions

View File

@ -33,13 +33,9 @@
* so we don't spam avatar transfers to a friend who already has it. * so we don't spam avatar transfers to a friend who already has it.
*/ */
QByteArray AvatarBroadcaster::avatarData; AvatarBroadcaster::AvatarBroadcaster(Core& _core)
QMap<uint32_t, bool> AvatarBroadcaster::friendsSentTo; : core{_core}
{}
static QMetaObject::Connection autoBroadcastConn;
static auto autoBroadcast = [](uint32_t friendId, Status::Status) {
AvatarBroadcaster::sendAvatarTo(friendId);
};
/** /**
* @brief Set our current avatar. * @brief Set our current avatar.
@ -47,15 +43,17 @@ static auto autoBroadcast = [](uint32_t friendId, Status::Status) {
*/ */
void AvatarBroadcaster::setAvatar(QByteArray data) void AvatarBroadcaster::setAvatar(QByteArray data)
{ {
if (avatarData == data) if (avatarData == data) {
return; return;
}
avatarData = data; avatarData = data;
friendsSentTo.clear(); friendsSentTo.clear();
QVector<uint32_t> friends = Core::getInstance()->getFriendList(); QVector<uint32_t> friends = core.getFriendList();
for (uint32_t friendId : friends) for (uint32_t friendId : friends) {
sendAvatarTo(friendId); sendAvatarTo(friendId);
}
} }
/** /**
@ -64,11 +62,15 @@ void AvatarBroadcaster::setAvatar(QByteArray data)
*/ */
void AvatarBroadcaster::sendAvatarTo(uint32_t friendId) void AvatarBroadcaster::sendAvatarTo(uint32_t friendId)
{ {
if (friendsSentTo.contains(friendId) && friendsSentTo[friendId]) if (friendsSentTo.contains(friendId) && friendsSentTo[friendId]) {
return; return;
if (!Core::getInstance()->isFriendOnline(friendId)) }
if (!core.isFriendOnline(friendId)) {
return; return;
CoreFile* coreFile = Core::getInstance()->getCoreFile(); }
CoreFile* coreFile = core.getCoreFile();
coreFile->sendAvatarFile(friendId, avatarData); coreFile->sendAvatarFile(friendId, avatarData);
friendsSentTo[friendId] = true; friendsSentTo[friendId] = true;
} }
@ -79,8 +81,9 @@ void AvatarBroadcaster::sendAvatarTo(uint32_t friendId)
*/ */
void AvatarBroadcaster::enableAutoBroadcast(bool state) void AvatarBroadcaster::enableAutoBroadcast(bool state)
{ {
QObject::disconnect(autoBroadcastConn); this->disconnect(&core, nullptr, this, nullptr);
if (state) if (state) {
autoBroadcastConn = connect(&core, &Core::friendStatusChanged,
QObject::connect(Core::getInstance(), &Core::friendStatusChanged, autoBroadcast); [=](uint32_t friendId, Status::Status) { this->sendAvatarTo(friendId); });
}
} }

View File

@ -22,18 +22,21 @@
#include <QByteArray> #include <QByteArray>
#include <QMap> #include <QMap>
#include <QObject>
class AvatarBroadcaster class Core;
class AvatarBroadcaster : public QObject
{ {
private: Q_OBJECT
AvatarBroadcaster() = delete;
public: public:
static void setAvatar(QByteArray data); AvatarBroadcaster(Core& _core);
static void sendAvatarTo(uint32_t friendId);
static void enableAutoBroadcast(bool state = true); void setAvatar(QByteArray data);
void sendAvatarTo(uint32_t friendId);
void enableAutoBroadcast(bool state = true);
private: private:
static QByteArray avatarData; Core& core;
static QMap<uint32_t, bool> friendsSentTo; QByteArray avatarData;
QMap<uint32_t, bool> friendsSentTo;
}; };

View File

@ -275,6 +275,8 @@ void Profile::initCore(const QByteArray& toxsave, const ICoreSettings& s, bool i
connect(core.get(), &Core::friendAvatarChanged, this, &Profile::setFriendAvatar); connect(core.get(), &Core::friendAvatarChanged, this, &Profile::setFriendAvatar);
connect(core.get(), &Core::fileAvatarOfferReceived, this, &Profile::onAvatarOfferReceived, connect(core.get(), &Core::fileAvatarOfferReceived, this, &Profile::onAvatarOfferReceived,
Qt::ConnectionType::QueuedConnection); Qt::ConnectionType::QueuedConnection);
// broadcast our own avatar
avatarBroadcaster = std::unique_ptr<AvatarBroadcaster>(new AvatarBroadcaster(*core));
} }
Profile::Profile(const QString& name, const QString& password, std::unique_ptr<ToxEncrypt> passkey, Paths& paths) Profile::Profile(const QString& name, const QString& password, std::unique_ptr<ToxEncrypt> passkey, Paths& paths)
@ -648,8 +650,8 @@ void Profile::setAvatar(QByteArray pic)
saveAvatar(selfPk, avatarData); saveAvatar(selfPk, avatarData);
emit selfAvatarChanged(pixmap); emit selfAvatarChanged(pixmap);
AvatarBroadcaster::setAvatar(avatarData); avatarBroadcaster->setAvatar(avatarData);
AvatarBroadcaster::enableAutoBroadcast(); avatarBroadcaster->enableAutoBroadcast();
} }

View File

@ -24,6 +24,8 @@
#include "src/core/toxencrypt.h" #include "src/core/toxencrypt.h"
#include "src/core/toxid.h" #include "src/core/toxid.h"
#include "src/net/avatarbroadcaster.h"
#include "src/persistence/history.h" #include "src/persistence/history.h"
#include "src/net/bootstrapnodeupdater.h" #include "src/net/bootstrapnodeupdater.h"
@ -109,6 +111,7 @@ private:
void initCore(const QByteArray& toxsave, const ICoreSettings& s, bool isNewProfile); void initCore(const QByteArray& toxsave, const ICoreSettings& s, bool isNewProfile);
private: private:
std::unique_ptr<AvatarBroadcaster> avatarBroadcaster;
std::unique_ptr<Core> core; std::unique_ptr<Core> core;
QString name; QString name;
std::unique_ptr<ToxEncrypt> passkey; std::unique_ptr<ToxEncrypt> passkey;