qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
avatarbroadcaster.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include "avatarbroadcaster.h"
22 #include "src/core/core.h"
23 #include "src/core/corefile.h"
24 #include "src/model/status.h"
25 #include <QDebug>
26 #include <QObject>
27 
37  : core{_core}
38 {}
39 
44 void AvatarBroadcaster::setAvatar(QByteArray data)
45 {
46  if (avatarData == data) {
47  return;
48  }
49 
50  avatarData = data;
51  friendsSentTo.clear();
52 
53  QVector<uint32_t> friends = core.getFriendList();
54  for (uint32_t friendId : friends) {
55  sendAvatarTo(friendId);
56  }
57 }
58 
63 void AvatarBroadcaster::sendAvatarTo(uint32_t friendId)
64 {
65  if (friendsSentTo.contains(friendId) && friendsSentTo[friendId]) {
66  return;
67  }
68 
69  if (!core.isFriendOnline(friendId)) {
70  return;
71  }
72 
73  CoreFile* coreFile = core.getCoreFile();
74  coreFile->sendAvatarFile(friendId, avatarData);
75  friendsSentTo[friendId] = true;
76 }
77 
83 {
84  this->disconnect(&core, nullptr, this, nullptr);
85  if (state) {
87  [=](uint32_t friendId, Status::Status) { this->sendAvatarTo(friendId); });
88  }
89 }
Status::Status
Status
Definition: status.h:28
AvatarBroadcaster::AvatarBroadcaster
AvatarBroadcaster(Core &_core)
Definition: avatarbroadcaster.cpp:36
AvatarBroadcaster::setAvatar
void setAvatar(QByteArray data)
Set our current avatar.
Definition: avatarbroadcaster.cpp:44
Core::getCoreFile
CoreFile * getCoreFile() const
Definition: core.cpp:718
AvatarBroadcaster::enableAutoBroadcast
void enableAutoBroadcast(bool state=true)
Setup auto broadcast sending avatar.
Definition: avatarbroadcaster.cpp:82
AvatarBroadcaster::core
Core & core
Definition: avatarbroadcaster.h:39
CoreFile::sendAvatarFile
void sendAvatarFile(uint32_t friendId, const QByteArray &data)
Definition: corefile.cpp:92
avatarbroadcaster.h
Core::isFriendOnline
bool isFriendOnline(uint32_t friendId) const
Checks if a friend is online. Unknown friends are considered offline.
Definition: core.cpp:1693
AvatarBroadcaster::sendAvatarTo
void sendAvatarTo(uint32_t friendId)
Send our current avatar to this friend, if not already sent.
Definition: avatarbroadcaster.cpp:63
AvatarBroadcaster::friendsSentTo
QMap< uint32_t, bool > friendsSentTo
Definition: avatarbroadcaster.h:41
corefile.h
core.h
Core::friendStatusChanged
void friendStatusChanged(uint32_t friendId, Status::Status status)
AvatarBroadcaster::avatarData
QByteArray avatarData
Definition: avatarbroadcaster.h:40
CoreFile
Manages the file transfer service of toxcore.
Definition: corefile.h:46
Core::getFriendList
QVector< uint32_t > getFriendList() const
Returns the list of friendIds in our friendlist, an empty list on error.
Definition: core.cpp:1474
status.h
Core
Definition: core.h:59