qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
friend.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 
21 #include "friend.h"
22 #include "src/model/status.h"
25 
26 #include <QDebug>
27 #include <memory>
28 
29 Friend::Friend(uint32_t friendId, const ToxPk& friendPk, const QString& userAlias, const QString& userName)
30  : userName{userName}
31  , userAlias{userAlias}
32  , friendPk{friendPk}
33  , friendId{friendId}
34  , hasNewEvents{false}
35  , friendStatus{Status::Status::Offline}
36  , isNegotiating{false}
37 {
38  if (userName.isEmpty()) {
39  this->userName = friendPk.toString();
40  }
41 }
42 
47 void Friend::setName(const QString& _name)
48 {
49  QString name = _name;
50  if (name.isEmpty()) {
51  name = friendPk.toString();
52  }
53 
54  // save old displayed name to be able to compare for changes
55  const auto oldDisplayed = getDisplayedName();
56  if (userName == userAlias) {
57  userAlias.clear(); // Because userAlias was set on name change before (issue #5013)
58  // we clear alias if equal to old name so that name change is visible.
59  // TODO: We should not modify alias on setName.
60  }
61  if (userName != name) {
62  userName = name;
63  emit nameChanged(friendPk, name);
64  }
65 
66  const auto newDisplayed = getDisplayedName();
67  if (oldDisplayed != newDisplayed) {
68  emit displayedNameChanged(newDisplayed);
69  }
70 }
71 
76 void Friend::setAlias(const QString& alias)
77 {
78  if (userAlias == alias) {
79  return;
80  }
81  emit aliasChanged(friendPk, alias);
82 
83  // save old displayed name to be able to compare for changes
84  const auto oldDisplayed = getDisplayedName();
85  userAlias = alias;
86 
87  const auto newDisplayed = getDisplayedName();
88  if (oldDisplayed != newDisplayed) {
89  emit displayedNameChanged(newDisplayed);
90  }
91 }
92 
93 void Friend::setStatusMessage(const QString& message)
94 {
95  if (statusMessage != message) {
98  }
99 }
100 
102 {
103  return statusMessage;
104 }
105 
113 {
114  if (userAlias.isEmpty()) {
115  return userName;
116  }
117 
118  return userAlias;
119 }
120 
121 bool Friend::hasAlias() const
122 {
123  return !userAlias.isEmpty();
124 }
125 
126 QString Friend::getUserName() const
127 {
128  return userName;
129 }
130 
132 {
133  return friendPk;
134 }
135 
136 uint32_t Friend::getId() const
137 {
138  return friendId;
139 }
140 
142 {
143  return friendPk;
144 }
145 
146 void Friend::setEventFlag(bool flag)
147 {
148  hasNewEvents = flag;
149 }
150 
152 {
153  return hasNewEvents;
154 }
155 
157 {
158  // Internal status should never be negotiating. We only expose this externally through the use of isNegotiating
159  assert(s != Status::Status::Negotiating);
160 
161  const bool wasOnline = Status::isOnline(getStatus());
162  if (friendStatus == s) {
163  return;
164  }
165 
166  // When a friend goes online we want to give them some time to negotiate
167  // extension support
168  const auto startNegotating = friendStatus == Status::Status::Offline;
169 
170  if (startNegotating) {
171  qDebug() << "Starting negotiation with friend " << friendId;
172  isNegotiating = true;
173  }
174 
175  friendStatus = s;
176  const bool nowOnline = Status::isOnline(getStatus());
177 
178  const auto emitStatusChange = startNegotating || !isNegotiating;
179  if (emitStatusChange) {
180  const auto statusToEmit = isNegotiating ? Status::Status::Negotiating : friendStatus;
181  emit statusChanged(friendPk, statusToEmit);
182  if (wasOnline && !nowOnline) {
183  emit onlineOfflineChanged(friendPk, false);
184  } else if (!wasOnline && nowOnline) {
185  emit onlineOfflineChanged(friendPk, true);
186  }
187  }
188 }
189 
191 {
193 }
194 
196 {
199 
200  // If all extensions are supported we can exit early
201  if (supportedExtensions.all()) {
203  }
204 }
205 
207 {
208  return supportedExtensions;
209 }
210 
212  if (!isNegotiating) {
213  return;
214  }
215 
216  qDebug() << "Negotiation complete for friend " << friendId;
217 
218  isNegotiating = false;
220 
221  if (Status::isOnline(getStatus())) {
222  emit onlineOfflineChanged(friendPk, true);
223  }
224 }
profile.h
Status::Status
Status
Definition: status.h:28
friend.h
Friend::onlineOfflineChanged
void onlineOfflineChanged(const ToxPk &friendId, bool isOnline)
Status::Status::Offline
@ Offline
Friend::onNegotiationComplete
void onNegotiationComplete()
Definition: friend.cpp:211
Friend::getEventFlag
bool getEventFlag() const override
Definition: friend.cpp:151
Friend::friendId
uint32_t friendId
Definition: friend.h:77
Friend::setStatus
void setStatus(Status::Status s)
Definition: friend.cpp:156
Friend::supportedExtensions
ExtensionSet supportedExtensions
Definition: friend.h:81
Friend::userName
QString userName
Definition: friend.h:73
Friend::hasNewEvents
bool hasNewEvents
Definition: friend.h:78
ExtensionType::messages
@ messages
Definition: extension.h:28
chatform.h
Friend::getStatus
Status::Status getStatus() const
Definition: friend.cpp:190
Friend::statusMessageChanged
void statusMessageChanged(const ToxPk &friendId, const QString &message)
Friend::getSupportedExtensions
ExtensionSet getSupportedExtensions() const
Definition: friend.cpp:206
Friend::isNegotiating
bool isNegotiating
Definition: friend.h:80
Friend::aliasChanged
void aliasChanged(const ToxPk &friendId, QString alias)
HistMessageContentType::message
@ message
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
Friend::setEventFlag
void setEventFlag(bool f) override
Definition: friend.cpp:146
Friend::setExtendedMessageSupport
void setExtendedMessageSupport(bool supported)
Definition: friend.cpp:195
Friend::setStatusMessage
void setStatusMessage(const QString &message)
Definition: friend.cpp:93
Friend::statusMessage
QString statusMessage
Definition: friend.h:75
Friend::getDisplayedName
QString getDisplayedName() const override
Friend::getDisplayedName Gets the name that should be displayed for a user.
Definition: friend.cpp:112
Friend::extensionSupportChanged
void extensionSupportChanged(ExtensionSet extensions)
Contact::displayedNameChanged
void displayedNameChanged(const QString &newName)
ExtensionSet
std::bitset< ExtensionType::max > ExtensionSet
Definition: extension.h:32
Friend::friendStatus
Status::Status friendStatus
Definition: friend.h:79
Friend::getPublicKey
const ToxPk & getPublicKey() const
Definition: friend.cpp:131
Friend::userAlias
QString userAlias
Definition: friend.h:74
ContactId
Definition: contactid.h:28
Status::Status::Negotiating
@ Negotiating
Friend::getStatusMessage
QString getStatusMessage() const
Definition: friend.cpp:101
Friend::setName
void setName(const QString &name) override
Friend::setName sets a new username for the friend.
Definition: friend.cpp:47
ContactId::toString
QString toString() const
Converts the ContactId to a uppercase hex string.
Definition: contactid.cpp:78
Friend::statusChanged
void statusChanged(const ToxPk &friendId, Status::Status status)
Friend::getPersistentId
const ContactId & getPersistentId() const override
Definition: friend.cpp:141
Friend::Friend
Friend(uint32_t friendId, const ToxPk &friendPk, const QString &userAlias={}, const QString &userName={})
Definition: friend.cpp:29
Friend::getId
uint32_t getId() const override
Definition: friend.cpp:136
Status::isOnline
bool isOnline(Status status)
Definition: status.cpp:83
Friend::hasAlias
bool hasAlias() const
Definition: friend.cpp:121
Friend::nameChanged
void nameChanged(const ToxPk &friendId, const QString &name)
Friend::getUserName
QString getUserName() const
Definition: friend.cpp:126
status.h
Friend::friendPk
ToxPk friendPk
Definition: friend.h:76
Friend::setAlias
void setAlias(const QString &name)
Friend::setAlias sets the alias for the friend.
Definition: friend.cpp:76