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
|
#include "jiddb.h"
#include <QDir>
#include <QSqlQuery>
#include <QStandardPaths>
#include <QTimeZone>
#include <QVariant>
#include <iostream>
#include <stdexcept>
JidDb::JidDb(const QString &jid) :
jid(jid),
db(QSqlDatabase::addDatabase("QSQLITE"))
{
const auto path = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation);
const QDir dir;
if (!dir.exists(path) && !dir.mkdir(path))
throw std::runtime_error("Failed to create app dir");
const auto abspath = path + "/" + jid + ".db";
db.setDatabaseName(abspath);
if (!db.open())
throw std::runtime_error(qPrintable("database" + abspath
+ "could not be created"));
QSqlQuery q(db);
if (!q.exec("create table if not exists roster "
"(jid TEXT unique) strict;"))
std::cerr << "JidDb::JidDb: query exec failed" << std::endl;
}
int JidDb::addToRoster(const QString &jid)
{
QSqlQuery q(db);
if (!q.exec("insert or ignore into roster (jid) values ('" + jid + "');"))
{
std::cerr << "JidDb::addToRoster: query exec failed" << std::endl;
return -1;
}
Q_EMIT addedToRoster(jid);
return 0;
}
int JidDb::addToRoster(const QStringList &roster)
{
for (const auto &r : roster)
if (addToRoster(r))
return -1;
return 0;
}
QStringList JidDb::roster() const
{
QStringList ret;
QSqlQuery q(db);
if (!q.exec("select jid from roster;"))
std::cerr << "query exec failed" << std::endl;
else
while (q.next())
ret.append(q.value("jid").toString());
return ret;
}
int JidDb::ensureContactDb(const QString &jid) const
{
QSqlQuery q(db);
if (!q.exec("create table if not exists '" + jid +
"' (direction TEXT, time INTEGER, body TEXT) strict;"))
{
std::cerr << "JidDb::storeMessage: query exec failed" << std::endl;
return -1;
}
return 0;
}
QList<JidDb::Message> JidDb::messages(const QString &jid,
const int tail) const
{
QSqlQuery q(db);
if (ensureContactDb(jid))
return QList<Message>();
else if (tail < 0)
{
if (!q.exec("select * from '" + jid + "' order by time;"))
{
std::cerr << "JidDb::messages: query exec failed";
return QList<Message>();
}
}
else if (!q.exec("select * from '" + jid + "' order by time desc limit "
+ QString::number(tail) + ";"))
{
std::cerr << "JidDb::messages: query exec failed" << std::endl;
return QList<Message>();
}
QList<Message> ret;
while (q.next())
{
Message m;
bool ok;
qlonglong t = q.value("time").toLongLong(&ok);
if (!ok)
{
std::cerr << "JidDb::messages: invalid time" << std::endl;
// Attempt to read other messages.
continue;
}
m.body = q.value("body").toString();
m.dt = QDateTime::fromSecsSinceEpoch(t, QTimeZone::systemTimeZone());
const auto dirstr = q.value("direction").toString();
if (dirstr == "in")
m.direction = Direction::In;
else if (dirstr == "out")
m.direction = Direction::Out;
else
{
std::cerr << "JidDb::messages: invalid direction" << std::endl;
// Attempt to read other messages.
continue;
}
ret << m;
}
return ret;
}
int JidDb::storeMessage(const JidDb::Message &msg) const
{
QSqlQuery q(db);
QString dir;
switch (msg.direction)
{
case Direction::In:
dir = "in";
break;
case Direction::Out:
dir = "out";
break;
}
ensureContactDb(msg.contact);
if (!q.exec("insert into '" + msg.contact
+ "' (direction, time, body) values "
"('" + dir + "', "
+ QString::number(msg.dt.toSecsSinceEpoch()) + ", "
"'" + msg.body + "')"))
{
std::cerr << "JidDb::storeMessage: query exec 2 failed" << std::endl;
return -1;
}
return 0;
}
QStringList JidDb::tables() const
{
QSqlQuery q(db);
if (!q.exec("select name from sqlite_schema where "
"type = 'table' and name not like 'sqlite_%'"))
{
std::cerr << "JidDb::conversations: query failed" << std::endl;
return QStringList();
}
QStringList ret;
while (q.next())
ret << q.value("name").toString();
return ret;
}
QList<JidDb::Conversation> JidDb::conversations() const
{
const auto conversations = tables();
QList<Conversation> ret;
for (const auto &jid : conversations)
if (jid.contains('@'))
{
const auto msgs = messages(jid, 1);
if (!msgs.isEmpty())
{
const auto &m = msgs.first();
ret << Conversation(jid, m.body, m.dt);
}
}
return ret;
}
|