forked from xuzhen/dukto-qt5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuddylistitemmodel.cpp
225 lines (197 loc) · 7.11 KB
/
buddylistitemmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* DUKTO - A simple, fast and multi-platform file transfer tool for LAN users
* Copyright (C) 2011 Emanuele Colombo
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "buddylistitemmodel.h"
#include <QUrl>
#include <QRegularExpression>
#include "platform.h"
#include "peer.h"
BuddyListItemModel::BuddyListItemModel() :
QStandardItemModel(nullptr)
{
QHash<int, QByteArray> roleNames;
roleNames[Ip] = "ip";
roleNames[Port] = "port";
roleNames[Username] = "username";
roleNames[System] = "system";
roleNames[Platform] = "platform";
roleNames[GenericAvatar] = "generic";
roleNames[Avatar] = "avatar";
roleNames[OsLogo] = "oslogo";
roleNames[ShowBack] = "showback";
setItemRoleNames(roleNames);
}
void BuddyListItemModel::addMeElement()
{
addBuddy("",
0,
Platform::getUsername() + " (You)",
Platform::getHostname(),
Platform::getPlatformName(),
QUrl::fromLocalFile(Platform::getAvatarPath()));
}
void BuddyListItemModel::addIpElement()
{
addBuddy("IP",
0,
"IP connection",
"Send data to a remote device",
"IP",
QUrl(""));
}
void BuddyListItemModel::addBuddy(const QString &ip, qint16 port, const QString &username, const QString &system, const QString &platform, const QUrl &avatarPath)
{
QStandardItem* it = nullptr;
bool add = true;
// Check if the same IP is alreay in the buddy list
if (mItemsMap.contains(ip)) {
it = mItemsMap[ip];
add = false;
}
else
it = new QStandardItem();
it->setData(ip, BuddyListItemModel::Ip);
it->setData(port, BuddyListItemModel::Port);
if (add) {
it->setData(false, BuddyListItemModel::ShowBack);
}
// Set (or update) data
it->setData(username, BuddyListItemModel::Username);
if (ip != "IP")
it->setData("at " + system, BuddyListItemModel::System);
else
it->setData(system, BuddyListItemModel::System);
it->setData(platform, BuddyListItemModel::Platform);
it->setData(avatarPath, BuddyListItemModel::Avatar);
QString platform_lower = platform.toLower();
// Update generic avatar
if ((platform_lower == "symbian") || (platform_lower == "android") || (platform_lower == "ios") || (platform_lower == "blackberry") || (platform_lower == "windowsphone"))
it->setData("SmartphoneLogo.png", BuddyListItemModel::GenericAvatar);
else if (platform_lower == "ip")
it->setData("IpLogo.png", BuddyListItemModel::GenericAvatar);
else
it->setData("PcLogo.png", BuddyListItemModel::GenericAvatar);
// Update logo
if (platform_lower == "windows")
it->setData("WindowsLogo.png", BuddyListItemModel::OsLogo);
else if (platform_lower == "macintosh")
it->setData("AppleLogo.png", BuddyListItemModel::OsLogo);
else if (platform_lower == "linux")
it->setData("LinuxLogo.png", BuddyListItemModel::OsLogo);
else if (platform_lower == "symbian")
it->setData("SymbianLogo.png", BuddyListItemModel::OsLogo);
else if (platform_lower == "ios")
it->setData("IosLogo.png", BuddyListItemModel::OsLogo);
else if (platform_lower == "windowsphone")
it->setData("WindowsPhoneLogo.png", BuddyListItemModel::OsLogo);
else if (platform_lower == "blackberry")
it->setData("BlackberryLogo.png", BuddyListItemModel::OsLogo);
else if (platform_lower == "android")
it->setData("AndroidLogo.png", BuddyListItemModel::OsLogo);
else
it->setData("UnknownLogo.png", BuddyListItemModel::OsLogo);
// Add elemento to the list
if (add) {
appendRow(it);
if (!ip.isEmpty())
mItemsMap.insert(ip, it);
else
mMeItem = it;
}
}
void BuddyListItemModel::addBuddy(const Peer &peer)
{
static int seq = 0;
static QRegularExpression rx("^(.*)\\sat\\s(.*)\\s\\((.*)\\)$");
QRegularExpressionMatch match = rx.match(peer.name);
QString username = match.captured(1);
QString system = match.captured(2);
QString platform = match.captured(3);
QUrl avatarPath = QUrl("http://" + peer.address.toString() + ":" + QString::number(peer.port + 1) + "/dukto/avatar");
avatarPath.setQuery(QString::number(seq++));
addBuddy(peer.address.toString(),
peer.port,
username,
system,
platform,
avatarPath);
}
void BuddyListItemModel::removeBuddy(const QString &ip)
{
// Check for element
if (!mItemsMap.contains(ip)) return;
// Get element
QStandardItem* it = mItemsMap[ip];
// Remove element
mItemsMap.remove(ip);
this->removeRow(this->indexFromItem(it).row());
}
void BuddyListItemModel::clearBuddies() {
// Keep first two rows: Me and Ip
removeRows(2, rowCount() - 2);
QMutableHashIterator<QString, QStandardItem*> iter(mItemsMap);
while (iter.hasNext()) {
iter.next();
if (iter.key() != "IP") {
iter.remove();
}
}
}
void BuddyListItemModel::showSingleBack(int idx)
{
for (int i = 0; i < rowCount(); i++)
itemFromIndex(index(i, 0))->setData(false, BuddyListItemModel::ShowBack);
itemFromIndex(index(idx, 0))->setData(true, BuddyListItemModel::ShowBack);
}
QString BuddyListItemModel::buddyNameByIp(const QString &ip)
{
if (!mItemsMap.contains(ip)) return "";
return mItemsMap.value(ip)->data(BuddyListItemModel::Username).toString();
}
QStandardItem* BuddyListItemModel::buddyByIp(const QString &ip)
{
if (!mItemsMap.contains(ip)) return nullptr;
return mItemsMap.value(ip);
}
QString BuddyListItemModel::fistBuddyIp()
{
if (this->rowCount() < 3) return "";
return this->index(2, 0).data(BuddyListItemModel::Ip).toString();
}
void BuddyListItemModel::updateMeElement()
{
if (mMeItem == nullptr) {
return;
}
static int seq = 0;
QString path = Platform::getAvatarPath();
if (path.isEmpty()) {
mMeItem->setData("", BuddyListItemModel::Avatar);
} else {
QUrl url = QUrl::fromLocalFile(path);
// force update with a different url each time
url.setQuery("m=" + QString::number(++seq));
mMeItem->setData(url, BuddyListItemModel::Avatar);
}
mMeItem->setData(Platform::getUsername() + " (You)", BuddyListItemModel::Username);
}
QString BuddyListItemModel::getMeGenericAvatar() {
if (mMeItem == nullptr) {
return QString();
}
return mMeItem->data(BuddyListItemModel::GenericAvatar).toString();
}