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"
22 #include "src/persistence/settings.h"
23 #include "src/core/contactid.h"
24 #include "src/core/toxpk.h"
25 #include <QDebug>
26 #include <QHash>
27 #include <QMenu>
28 
29 QHash<ToxPk, Friend*> FriendList::friendList;
30 QHash<uint32_t, ToxPk> FriendList::id2key;
31 
addFriend(uint32_t friendId,const ToxPk & friendPk)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 
findFriend(const ToxPk & friendPk)47 Friend* FriendList::findFriend(const ToxPk& friendPk)
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 
id2Key(uint32_t friendId)57 const ToxPk& FriendList::id2Key(uint32_t friendId)
58 {
59     return id2key[friendId];
60 }
61 
removeFriend(const ToxPk & friendPk,bool fake)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 
clear()72 void FriendList::clear()
73 {
74     for (auto friendptr : friendList)
75         delete friendptr;
76     friendList.clear();
77 }
78 
getAllFriends()79 QList<Friend*> FriendList::getAllFriends()
80 {
81     return friendList.values();
82 }
83 
decideNickname(const ToxPk & friendPk,const QString & origName)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 }
95