This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
presencemodel.cpp
296 lines (252 loc) · 9.37 KB
/
presencemodel.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "stdafx.h"
#include "presencemodel.h"
#include "scopedguard.h"
PresenceModel::PresenceModel(QObject *parent, Sqlite3HelperPtr sqlite3Helper) : QAbstractTableModel(parent), m_sqlite3Helper(sqlite3Helper)
{
connect(this, &PresenceModel::gotPresences, &PresenceModel::onGetPrsences);
}
PresenceModel::~PresenceModel() {}
QModelIndex PresenceModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
int PresenceModel::rowCount(const QModelIndex & /*parent*/) const
{
return m_presences.size();
}
int PresenceModel::columnCount(const QModelIndex & /*parent*/) const
{
return m_fullJIDList.size() + 2;
}
QVariant PresenceModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::BackgroundRole && index.column() > 1 && index.column() % 2 == 0)
{
return QVariant(QColor(224, 224, 224));
}
if (role != Qt::DisplayRole && role != Qt::ToolTipRole)
return QVariant();
if (index.row() < 0 || index.row() >= m_presences.size())
return QVariant();
auto it = m_presences.at(index.row());
switch (index.column())
{
case 0:
return QVariant(it->id);
case 1:
return QVariant(it->time.toString("yyyy-MM-dd hh:mm:ss.zzz"));
default:
if (index.column() - 2 < it->presences.size())
{
QString text = it->presences.at(index.column() - 2);
if (!text.isEmpty())
{
int startPos = text.indexOf(QChar('<'));
int endPos = text.lastIndexOf(QChar('>'));
if (startPos > 0 && endPos > startPos)
{
QString xmlIn = text.mid(startPos, endPos - startPos + 1);
QString xmlOut;
QDomDocument doc;
doc.setContent(xmlIn, false);
if (role == Qt::DisplayRole)
{
QTextStream writer(&xmlOut);
doc.save(writer, 4);
return QVariant(xmlOut.trimmed());
}
if (role == Qt::ToolTipRole)
{
// extract x node
QDomElement it = doc.documentElement().firstChildElement("x");
if (!it.isNull())
{
QString var = it.attribute("var");
bool ok = false;
int value = var.toInt(&ok, 10);
if (ok)
{
QMap<int, QString> m = {
{1, "Desktop Share"},
{2, "VOIP"},
{4, "Video"},
{8, "On a Call"},
{16, "In Webex Meeting"},
{32, "In Outlook Meeting"},
{64, "Tele Conf"},
{256, "AES"},
{512, "Sharing in Webex Meeting"},
};
QString res("Mask:");
for (auto kv = m.begin(); m.end() != kv; ++kv)
{
if (value & kv.key())
{
res.append("\n " + kv.value());
}
}
if (res != "Mask:")
return QVariant(res);
}
}
}
}
}
}
}
return QVariant();
}
Qt::ItemFlags PresenceModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return QAbstractItemModel::flags(index);
}
QVariant PresenceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch (section)
{
case 0:
return QVariant(tr("Id"));
case 1:
return QVariant(tr("Time"));
default:
if (section - 2 < m_fullJIDList.size())
return QVariant(m_fullJIDList.at(section - 2));
break;
}
}
return QVariant();
}
void PresenceModel::requestReceivedPresenceBuddyList()
{
QtConcurrent::run(this, &PresenceModel::doRequestReceivedPresenceBuddyList);
}
void PresenceModel::onDatabaseCreated(const QString &dbFile)
{
m_dbFile = dbFile;
}
void PresenceModel::onSelectedJIDChanged(const QString &text)
{
// clear all
if (!m_presences.empty())
{
beginRemoveRows(QModelIndex(), 0, m_presences.size() - 1);
m_presences.clear();
endRemoveRows();
}
if (!m_fullJIDList.empty())
{
beginRemoveColumns(QModelIndex(), 2, m_fullJIDList.size() + 1);
m_fullJIDList.clear();
endRemoveColumns();
}
if (text != "--NONE--")
{
QtConcurrent::run(this, &PresenceModel::doQueryPresence, text);
}
}
void PresenceModel::onGetPrsences(QStringList jidList, QList<QSharedPointer<PresenceItem>> presences)
{
beginInsertColumns(QModelIndex(), 2, jidList.size() + 1);
m_fullJIDList = jidList;
endInsertColumns();
beginInsertRows(QModelIndex(), 0, presences.size() - 1);
m_presences = presences;
endInsertRows();
emit resizeTableCells(jidList.size() + 2, presences.size());
}
void PresenceModel::doQueryPresence(const QString &jid)
{
QMutexLocker lock(&m_mutex);
QStringList jids;
QList<QSharedPointer<PresenceItem>> results;
bool eof = false;
int nRet = 0;
sqlite3_stmt *pVM = nullptr;
do
{
pVM = m_sqlite3Helper->compile("SELECT id,time,content FROM logs WHERE content LIKE '%'||?||'%' ORDER BY epoch LIMIT 1, 200000;");
m_sqlite3Helper->bind(pVM, 1, "Recv:<presence from=\"" + jid);
nRet = m_sqlite3Helper->execQuery(pVM, eof);
if (nRet == SQLITE_DONE || nRet == SQLITE_ROW)
{
QRegularExpression re("from=\"([^\"]+)");
while (!eof)
{
QSharedPointer<PresenceItem> p = QSharedPointer<PresenceItem>(new PresenceItem);
p->id = sqlite3_column_int(pVM, 0);
p->time = QDateTime::fromString(QString((const char *)sqlite3_column_text(pVM, 1)), Qt::ISODate);
QString content((const char *)sqlite3_column_text(pVM, 2));
// extract from JID
QRegularExpressionMatch m = re.match(content);
if (m.hasMatch())
{
QString jid = m.captured(1);
int index = jids.indexOf(jid);
if (index < 0)
{
for (int i = 0; i < jids.size(); i++)
p->presences.append("");
jids.append(jid);
}
else
{
for (int i = 0; i < index; i++)
p->presences.append("");
}
p->presences.append(content);
}
results.append(p);
m_sqlite3Helper->nextRow(pVM, eof);
}
break;
}
} while (nRet == SQLITE_SCHEMA);
if (!jids.empty() && !results.empty())
emit gotPresences(jids, results);
}
void PresenceModel::doRequestReceivedPresenceBuddyList()
{
if (!m_mutex.tryLock())
{
qDebug() << "obtaining lock failed";
return;
}
ScopedGuard queryMutexUnlock([this]() { m_mutex.unlock(); });
QStringList result;
bool eof = false;
int nRet = 0;
sqlite3_stmt *pVM = nullptr;
do
{
pVM = m_sqlite3Helper->compile("SELECT content FROM logs WHERE content LIKE '%'||?||'%' ORDER BY epoch LIMIT 1, 200000;");
m_sqlite3Helper->bind(pVM, 1, "Recv:<presence from=");
nRet = m_sqlite3Helper->execQuery(pVM, eof);
if (nRet == SQLITE_DONE || nRet == SQLITE_ROW)
{
// extract from JID
QRegularExpression re("from=\"([^/\"]+)");
while (!eof)
{
QString content((const char *)sqlite3_column_text(pVM, 0));
QRegularExpressionMatch m = re.match(content);
if (m.hasMatch())
{
QString s = m.captured(1);
if (!result.contains(s))
result.append(s);
}
m_sqlite3Helper->nextRow(pVM, eof);
}
break;
}
} while (nRet == SQLITE_SCHEMA);
emit receivedPresenceBuddyList(result);
}