-
Notifications
You must be signed in to change notification settings - Fork 1
/
buddylistitemmodel.cpp
186 lines (163 loc) · 5.98 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
/* 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 <QRegExp>
#include "platform.h"
#include "peer.h"
BuddyListItemModel::BuddyListItemModel() :
QStandardItemModel(NULL)
{
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";
setRoleNames(roleNames);
}
void BuddyListItemModel::addMeElement()
{
addBuddy("",
0,
Platform::getSystemUsername() + " (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(QString ip, qint16 port, QString username, QString system, QString platform, QUrl avatarPath)
{
QStandardItem* it = NULL;
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);
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);
// Update generic avatar
if ((platform.toLower() == "symbian") || (platform.toLower() == "android") || (platform.toLower() == "ios") || (platform.toLower() == "blackberry") || (platform.toLower() == "windowsphone"))
it->setData("SmartphoneLogo.png", BuddyListItemModel::GenericAvatar);
else if (platform.toLower() == "ip")
it->setData("IpLogo.png", BuddyListItemModel::GenericAvatar);
else
it->setData("PcLogo.png", BuddyListItemModel::GenericAvatar);
// Update logo
if (platform.toLower() == "windows")
it->setData("WindowsLogo.png", BuddyListItemModel::OsLogo);
else if (platform.toLower() == "macintosh")
it->setData("AppleLogo.png", BuddyListItemModel::OsLogo);
else if (platform.toLower() == "linux")
it->setData("LinuxLogo.png", BuddyListItemModel::OsLogo);
else if (platform.toLower() == "symbian")
it->setData("SymbianLogo.png", BuddyListItemModel::OsLogo);
else if (platform.toLower() == "ios")
it->setData("IosLogo.png", BuddyListItemModel::OsLogo);
else if (platform.toLower() == "windowsphone")
it->setData("WindowsPhoneLogo.png", BuddyListItemModel::OsLogo);
else if (platform.toLower() == "blackberry")
it->setData("BlackberryLogo.png", BuddyListItemModel::OsLogo);
else if (platform.toLower() == "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 != "")
mItemsMap.insert(ip, it);
else
mMeItem = it;
}
}
void BuddyListItemModel::addBuddy(Peer &peer)
{
QRegExp rx("^(.*)\\sat\\s(.*)\\s\\((.*)\\)$");
rx.indexIn(peer.name);
QStringList data = rx.capturedTexts();
QString username = data[1];
QString system = data[2];
QString platform = data[3];
QUrl avatarPath = QUrl("http://" + peer.address.toString() + ":" + QString::number(peer.port + 1) + "/dukto/avatar");
addBuddy(peer.address.toString(),
peer.port,
username,
system,
platform,
avatarPath);
}
void BuddyListItemModel::removeBuddy(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::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(QString ip)
{
if (!mItemsMap.contains(ip)) return "";
return mItemsMap.value(ip)->data(BuddyListItemModel::Username).toString();
}
QStandardItem* BuddyListItemModel::buddyByIp(QString ip)
{
if (!mItemsMap.contains(ip)) return NULL;
return mItemsMap.value(ip);
}
QString BuddyListItemModel::fistBuddyIp()
{
if (this->rowCount() < 3) return "";
return this->index(2, 0).data(BuddyListItemModel::Ip).toString();
}
void BuddyListItemModel::updateMeElement()
{
mMeItem->setData(Platform::getSystemUsername(), BuddyListItemModel::Username);
}