mirror of https://github.com/qTox/qTox.git
Browse Source
- Add AvatarBroadcaster, in charge of making sure our friends have our avatar without spamming file transfers - Fix file sending code not closing the file after transfer, which prevented file previews, and make the QFile a shared_ptr to fix the obvious memory leak Some small additions to Core to support AvatarBroadcasterpull/1535/head
7 changed files with 126 additions and 40 deletions
@ -0,0 +1,41 @@ |
|||||||
|
#include "avatarbroadcaster.h" |
||||||
|
#include "src/core/core.h" |
||||||
|
#include <QObject> |
||||||
|
#include <QDebug> |
||||||
|
|
||||||
|
QByteArray AvatarBroadcaster::avatarData; |
||||||
|
QMap<uint32_t, bool> AvatarBroadcaster::friendsSentTo; |
||||||
|
|
||||||
|
static QMetaObject::Connection autoBroadcastConn; |
||||||
|
static auto autoBroadcast = [](uint32_t friendId, Status) |
||||||
|
{ |
||||||
|
AvatarBroadcaster::sendAvatarTo(friendId); |
||||||
|
}; |
||||||
|
|
||||||
|
void AvatarBroadcaster::setAvatar(QByteArray data) |
||||||
|
{ |
||||||
|
avatarData = data; |
||||||
|
friendsSentTo.clear(); |
||||||
|
|
||||||
|
QVector<uint32_t> friends = Core::getInstance()->getFriendList(); |
||||||
|
for (uint32_t friendId : friends) |
||||||
|
sendAvatarTo(friendId); |
||||||
|
} |
||||||
|
|
||||||
|
void AvatarBroadcaster::sendAvatarTo(uint32_t friendId) |
||||||
|
{ |
||||||
|
if (friendsSentTo.contains(friendId) && friendsSentTo[friendId]) |
||||||
|
return; |
||||||
|
if (!Core::getInstance()->isFriendOnline(friendId)) |
||||||
|
return; |
||||||
|
qDebug() << "AvatarBroadcaster: Sending avatar to "<<friendId; |
||||||
|
Core::getInstance()->sendAvatarFile(friendId, avatarData); |
||||||
|
friendsSentTo[friendId] = true; |
||||||
|
} |
||||||
|
|
||||||
|
void AvatarBroadcaster::enableAutoBroadcast(bool state) |
||||||
|
{ |
||||||
|
QObject::disconnect(autoBroadcastConn); |
||||||
|
if (state) |
||||||
|
autoBroadcastConn = QObject::connect(Core::getInstance(), &Core::friendStatusChanged, autoBroadcast); |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
#ifndef AVATARBROADCASTER_H |
||||||
|
#define AVATARBROADCASTER_H |
||||||
|
|
||||||
|
#include <QByteArray> |
||||||
|
#include <QMap> |
||||||
|
|
||||||
|
/// Takes care of broadcasting avatar changes to our friends in a smart way
|
||||||
|
/// Cache a copy of our current avatar and friends who have received it
|
||||||
|
/// so we don't spam avatar transfers to a friend who already has it.
|
||||||
|
class AvatarBroadcaster |
||||||
|
{ |
||||||
|
private: |
||||||
|
AvatarBroadcaster()=delete; |
||||||
|
|
||||||
|
public: |
||||||
|
/// Set our current avatar
|
||||||
|
static void setAvatar(QByteArray data); |
||||||
|
/// Send our current avatar to this friend, if not already sent
|
||||||
|
static void sendAvatarTo(uint32_t friendId); |
||||||
|
/// If true, we automatically broadcast our avatar to friends when they come online
|
||||||
|
static void enableAutoBroadcast(bool state = true); |
||||||
|
|
||||||
|
private: |
||||||
|
static QByteArray avatarData; |
||||||
|
static QMap<uint32_t, bool> friendsSentTo; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif // AVATARBROADCASTER_H
|
Loading…
Reference in new issue