qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
group.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-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 #include "group.h"
21 #include "friend.h"
22 #include "src/core/contactid.h"
23 #include "src/core/groupid.h"
24 #include "src/core/toxpk.h"
25 #include "src/friendlist.h"
26 
27 #include <cassert>
28 
29 #include <QDebug>
30 
31 static const int MAX_GROUP_TITLE_LENGTH = 128;
32 
33 Group::Group(int groupId, const GroupId persistentGroupId, const QString& name, bool isAvGroupchat,
34  const QString& selfName, ICoreGroupQuery& groupQuery, ICoreIdHandler& idHandler)
35  : groupQuery(groupQuery)
36  , idHandler(idHandler)
37  , selfName{selfName}
38  , title{name}
39  , toxGroupNum(groupId)
40  , groupId{persistentGroupId}
41  , avGroupchat{isAvGroupchat}
42 {
43  // in groupchats, we only notify on messages containing your name <-- dumb
44  // sound notifications should be on all messages, but system popup notification
45  // on naming is appropriate
46  hasNewMessages = 0;
47  userWasMentioned = 0;
48  regeneratePeerList();
49 }
50 
51 void Group::setName(const QString& newTitle)
52 {
53  const QString shortTitle = newTitle.left(MAX_GROUP_TITLE_LENGTH);
54  if (!shortTitle.isEmpty() && title != shortTitle) {
55  title = shortTitle;
59  }
60 }
61 
62 void Group::setTitle(const QString& author, const QString& newTitle)
63 {
64  const QString shortTitle = newTitle.left(MAX_GROUP_TITLE_LENGTH);
65  if (!shortTitle.isEmpty() && title != shortTitle) {
66  title = shortTitle;
68  emit titleChanged(author, title);
69  }
70 }
71 
72 QString Group::getName() const
73 {
74  return title;
75 }
76 
77 QString Group::getDisplayedName() const
78 {
79  return getName();
80 }
81 
83 {
84  // NOTE: there's a bit of a race here. Core emits a signal for both groupPeerlistChanged and
85  // groupPeerNameChanged back-to-back when a peer joins our group. If we get both before we
86  // process this slot, core->getGroupPeerNames will contain the new peer name, and we'll ignore
87  // the name changed signal, and emit a single userJoined with the correct name. But, if we
88  // receive the name changed signal a little later, we will emit userJoined before we have their
89  // username, using just their ToxPk, then shortly after emit another peerNameChanged signal.
90  // This can cause double-updated to UI and chatlog, but is unavoidable given the API of toxcore.
91  QStringList peers = groupQuery.getGroupPeerNames(toxGroupNum);
92  const auto oldPeerNames = peerDisplayNames;
93  peerDisplayNames.clear();
94  const int nPeers = peers.size();
95  for (int i = 0; i < nPeers; ++i) {
96  const auto pk = groupQuery.getGroupPeerPk(toxGroupNum, i);
97  if (pk == idHandler.getSelfPublicKey()) {
99  } else {
100  peerDisplayNames[pk] = FriendList::decideNickname(pk, peers[i]);
101  }
102  }
103  for (const auto& pk : oldPeerNames.keys()) {
104  if (!peerDisplayNames.contains(pk)) {
105  emit userLeft(pk, oldPeerNames.value(pk));
106  }
107  }
108  for (const auto& pk : peerDisplayNames.keys()) {
109  if (!oldPeerNames.contains(pk)) {
110  emit userJoined(pk, peerDisplayNames.value(pk));
111  }
112  }
113  for (const auto& pk : peerDisplayNames.keys()) {
114  if (oldPeerNames.contains(pk) && oldPeerNames.value(pk) != peerDisplayNames.value(pk)) {
115  emit peerNameChanged(pk, oldPeerNames.value(pk), peerDisplayNames.value(pk));
116  }
117  }
118  if (oldPeerNames.size() != nPeers) {
119  emit numPeersChanged(nPeers);
120  }
121 }
122 
123 void Group::updateUsername(ToxPk pk, const QString newName)
124 {
125  const QString displayName = FriendList::decideNickname(pk, newName);
126  assert(peerDisplayNames.contains(pk));
127  if (peerDisplayNames[pk] != displayName) {
128  // there could be no actual change even if their username changed due to an alias being set
129  const auto oldName = peerDisplayNames[pk];
130  peerDisplayNames[pk] = displayName;
131  emit peerNameChanged(pk, oldName, displayName);
132  }
133 }
134 
136 {
137  return avGroupchat;
138 }
139 
140 uint32_t Group::getId() const
141 {
142  return toxGroupNum;
143 }
144 
146 {
147  return groupId;
148 }
149 
151 {
152  return peerDisplayNames.size();
153 }
154 
159 const QMap<ToxPk, QString>& Group::getPeerList() const
160 {
161  return peerDisplayNames;
162 }
163 
165 {
166  hasNewMessages = f;
167 }
168 
170 {
171  return hasNewMessages;
172 }
173 
175 {
176  userWasMentioned = f;
177 }
178 
180 {
181  return userWasMentioned;
182 }
183 
184 QString Group::resolveToxPk(const ToxPk& id) const
185 {
186  auto it = peerDisplayNames.find(id);
187 
188  if (it != peerDisplayNames.end()) {
189  return *it;
190  }
191 
192  return QString();
193 }
194 
195 void Group::setSelfName(const QString& name)
196 {
197  selfName = name;
198 }
199 
200 QString Group::getSelfName() const
201 {
202  return selfName;
203 }
Group::getName
QString getName() const
Definition: group.cpp:72
friend.h
friendlist.h
Group::resolveToxPk
QString resolveToxPk(const ToxPk &id) const
Definition: group.cpp:184
FriendList::decideNickname
static QString decideNickname(const ToxPk &friendPk, const QString &origName)
Definition: friendlist.cpp:84
ICoreGroupQuery::getGroupPeerPk
virtual ToxPk getGroupPeerPk(int groupId, int peerId) const =0
Group::getPeerList
const QMap< ToxPk, QString > & getPeerList() const
Gets the PKs and names of all peers.
Definition: group.cpp:159
Group::isAvGroupchat
bool isAvGroupchat() const
Definition: group.cpp:135
Group::avGroupchat
bool avGroupchat
Definition: group.h:81
ICoreIdHandler::getUsername
virtual QString getUsername() const =0
Group::Group
Group(int groupId, const GroupId persistentGroupId, const QString &name, bool isAvGroupchat, const QString &selfName, ICoreGroupQuery &groupQuery, ICoreIdHandler &idHandler)
Definition: group.cpp:33
group.h
Group::titleChanged
void titleChanged(const QString &author, const QString &title)
Group::getPeersCount
int getPeersCount() const
Definition: group.cpp:150
Group::regeneratePeerList
void regeneratePeerList()
Definition: group.cpp:82
Group::hasNewMessages
bool hasNewMessages
Definition: group.h:77
toxpk.h
Group::numPeersChanged
void numPeersChanged(int numPeers)
Group::setEventFlag
void setEventFlag(bool f) override
Definition: group.cpp:164
Group::getId
uint32_t getId() const override
Definition: group.cpp:140
Group::userJoined
void userJoined(const ToxPk &user, const QString &name)
contactid.h
Group::idHandler
ICoreIdHandler & idHandler
Definition: group.h:73
Group::selfName
QString selfName
Definition: group.h:74
Group::setSelfName
void setSelfName(const QString &name)
Definition: group.cpp:195
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
Group::peerDisplayNames
QMap< ToxPk, QString > peerDisplayNames
Definition: group.h:76
Group::peerNameChanged
void peerNameChanged(const ToxPk &peer, const QString &oldName, const QString &newName)
ICoreIdHandler::getSelfPublicKey
virtual ToxPk getSelfPublicKey() const =0
Group::getMentionedFlag
bool getMentionedFlag() const
Definition: group.cpp:179
Group::getEventFlag
bool getEventFlag() const override
Definition: group.cpp:169
Group::setTitle
void setTitle(const QString &author, const QString &newTitle)
Definition: group.cpp:62
ICoreGroupQuery
Definition: icoregroupquery.h:31
Group::getDisplayedName
QString getDisplayedName() const override
Definition: group.cpp:77
Group::getPersistentId
const GroupId & getPersistentId() const override
Definition: group.cpp:145
Contact::displayedNameChanged
void displayedNameChanged(const QString &newName)
groupid.h
GroupId
This class represents a long term persistent group identifier.
Definition: groupid.h:26
ICoreIdHandler
Definition: icoreidhandler.h:25
Group::setName
void setName(const QString &newTitle) override
Definition: group.cpp:51
Group::setMentionedFlag
void setMentionedFlag(bool f)
Definition: group.cpp:174
Group::userWasMentioned
bool userWasMentioned
Definition: group.h:78
Group::updateUsername
void updateUsername(ToxPk pk, const QString newName)
Definition: group.cpp:123
Group::toxGroupNum
int toxGroupNum
Definition: group.h:79
Group::groupId
const GroupId groupId
Definition: group.h:80
Group::titleChangedByUser
void titleChangedByUser(const QString &title)
Group::getSelfName
QString getSelfName() const
Definition: group.cpp:200
Group::groupQuery
ICoreGroupQuery & groupQuery
Definition: group.h:72
Group::title
QString title
Definition: group.h:75
ICoreGroupQuery::getGroupPeerNames
virtual QStringList getGroupPeerNames(int groupId) const =0
Group::userLeft
void userLeft(const ToxPk &user, const QString &name)