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.
*/
QByteArray AvatarBroadcaster::avatarData;
QMap<uint32_t, bool> AvatarBroadcaster::friendsSentTo;
static QMetaObject::Connection autoBroadcastConn;
static auto autoBroadcast = [](uint32_t friendId, Status::Status) {
AvatarBroadcaster::sendAvatarTo(friendId);
};
AvatarBroadcaster::AvatarBroadcaster(Core& _core)
: core{_core}
{}
/**
* @brief Set our current avatar.
@ -47,15 +43,17 @@ static auto autoBroadcast = [](uint32_t friendId, Status::Status) {
*/
void AvatarBroadcaster::setAvatar(QByteArray data)
{
if (avatarData == data)
if (avatarData == data) {
return;
}
avatarData = data;
friendsSentTo.clear();
QVector<uint32_t> friends = Core::getInstance()->getFriendList();
for (uint32_t friendId : friends)
QVector<uint32_t> friends = core.getFriendList();
for (uint32_t friendId : friends) {
sendAvatarTo(friendId);
}
}
/**
@ -64,11 +62,15 @@ void AvatarBroadcaster::setAvatar(QByteArray data)
*/
void AvatarBroadcaster::sendAvatarTo(uint32_t friendId)
{
if (friendsSentTo.contains(friendId) && friendsSentTo[friendId])
if (friendsSentTo.contains(friendId) && friendsSentTo[friendId]) {
return;
if (!Core::getInstance()->isFriendOnline(friendId))
}
if (!core.isFriendOnline(friendId)) {
return;
CoreFile* coreFile = Core::getInstance()->getCoreFile();
}
CoreFile* coreFile = core.getCoreFile();
coreFile->sendAvatarFile(friendId, avatarData);
friendsSentTo[friendId] = true;
}
@ -79,8 +81,9 @@ void AvatarBroadcaster::sendAvatarTo(uint32_t friendId)
*/
void AvatarBroadcaster::enableAutoBroadcast(bool state)
{
QObject::disconnect(autoBroadcastConn);
if (state)
autoBroadcastConn =
QObject::connect(Core::getInstance(), &Core::friendStatusChanged, autoBroadcast);
this->disconnect(&core, nullptr, this, nullptr);
if (state) {
connect(&core, &Core::friendStatusChanged,
[=](uint32_t friendId, Status::Status) { this->sendAvatarTo(friendId); });
}
}

View File

@ -22,18 +22,21 @@
#include <QByteArray>
#include <QMap>
#include <QObject>
class AvatarBroadcaster
class Core;
class AvatarBroadcaster : public QObject
{
private:
AvatarBroadcaster() = delete;
Q_OBJECT
public:
static void setAvatar(QByteArray data);
static void sendAvatarTo(uint32_t friendId);
static void enableAutoBroadcast(bool state = true);
AvatarBroadcaster(Core& _core);
void setAvatar(QByteArray data);
void sendAvatarTo(uint32_t friendId);
void enableAutoBroadcast(bool state = true);
private:
static QByteArray avatarData;
static QMap<uint32_t, bool> friendsSentTo;
Core& core;
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::fileAvatarOfferReceived, this, &Profile::onAvatarOfferReceived,
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)
@ -648,8 +650,8 @@ void Profile::setAvatar(QByteArray pic)
saveAvatar(selfPk, avatarData);
emit selfAvatarChanged(pixmap);
AvatarBroadcaster::setAvatar(avatarData);
AvatarBroadcaster::enableAutoBroadcast();
avatarBroadcaster->setAvatar(avatarData);
avatarBroadcaster->enableAutoBroadcast();
}

View File

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