qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
friendlist.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 "friendlist.h"
21 #include "src/model/friend.h"
23 #include "src/core/contactid.h"
24 #include "src/core/toxpk.h"
25 #include <QDebug>
26 #include <QHash>
27 #include <QMenu>
28 
31 
32 Friend* FriendList::addFriend(uint32_t friendId, const ToxPk& friendPk)
33 {
34  auto friendChecker = friendList.find(friendPk);
35  if (friendChecker != friendList.end()) {
36  qWarning() << "addFriend: friendPk already taken";
37  }
38 
39  QString alias = Settings::getInstance().getFriendAlias(friendPk);
40  Friend* newfriend = new Friend(friendId, friendPk, alias);
41  friendList[friendPk] = newfriend;
42  id2key[friendId] = friendPk;
43 
44  return newfriend;
45 }
46 
48 {
49  auto f_it = friendList.find(friendPk);
50  if (f_it != friendList.end()) {
51  return *f_it;
52  }
53 
54  return nullptr;
55 }
56 
57 const ToxPk& FriendList::id2Key(uint32_t friendId)
58 {
59  return id2key[friendId];
60 }
61 
62 void FriendList::removeFriend(const ToxPk& friendPk, bool fake)
63 {
64  auto f_it = friendList.find(friendPk);
65  if (f_it != friendList.end()) {
66  if (!fake)
67  Settings::getInstance().removeFriendSettings(f_it.value()->getPublicKey());
68  friendList.erase(f_it);
69  }
70 }
71 
73 {
74  for (auto friendptr : friendList)
75  delete friendptr;
76  friendList.clear();
77 }
78 
80 {
81  return friendList.values();
82 }
83 
84 QString FriendList::decideNickname(const ToxPk& friendPk, const QString& origName)
85 {
86  Friend* f = FriendList::findFriend(friendPk);
87  if (f != nullptr) {
88  return f->getDisplayedName();
89  } else if (!origName.isEmpty()) {
90  return origName;
91  } else {
92  return friendPk.toString();
93  }
94 }
Settings::removeFriendSettings
void removeFriendSettings(const ToxPk &id) override
Definition: settings.cpp:1934
FriendList::removeFriend
static void removeFriend(const ToxPk &friendPk, bool fake=false)
Definition: friendlist.cpp:62
friend.h
friendlist.h
settings.h
Settings::getFriendAlias
QString getFriendAlias(const ToxPk &id) const override
Definition: settings.cpp:1877
FriendList::decideNickname
static QString decideNickname(const ToxPk &friendPk, const QString &origName)
Definition: friendlist.cpp:84
FriendList::friendList
static QHash< ToxPk, Friend * > friendList
Definition: friendlist.h:45
QList
Definition: friendlist.h:25
toxpk.h
FriendList::addFriend
static Friend * addFriend(uint32_t friendId, const ToxPk &friendPk)
Definition: friendlist.cpp:32
FriendList::findFriend
static Friend * findFriend(const ToxPk &friendPk)
Definition: friendlist.cpp:47
FriendList::getAllFriends
static QList< Friend * > getAllFriends()
Definition: friendlist.cpp:79
contactid.h
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
Friend
Definition: friend.h:31
QHash< ToxPk, Friend * >
FriendList::id2key
static QHash< uint32_t, ToxPk > id2key
Definition: friendlist.h:46
Friend::getDisplayedName
QString getDisplayedName() const override
Friend::getDisplayedName Gets the name that should be displayed for a user.
Definition: friend.cpp:112
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
FriendList::clear
static void clear()
Definition: friendlist.cpp:72
ContactId::toString
QString toString() const
Converts the ContactId to a uppercase hex string.
Definition: contactid.cpp:78
FriendList::id2Key
static const ToxPk & id2Key(uint32_t friendId)
Definition: friendlist.cpp:57