aboutsummaryrefslogtreecommitdiff
path: root/jiddb.cpp
blob: 3102e8137afdb2551f062e38b8b89f921c711f5a (plain) (blame)
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
#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;
}

void JidDb::addToRoster(const QString &jid)
{
    QSqlQuery q(db);

    if (!q.exec("insert or ignore into roster (jid) values ('" + jid + "');"))
        throw std::runtime_error("JidDb::addToRoster: query exec failed");

    Q_EMIT addedToRoster(jid);
}

void JidDb::addToRoster(const QStringList &roster)
{
    for (const auto &r : roster)
        addToRoster(r);
}

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;
}

void 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;"))
        throw std::runtime_error("JidDb::storeMessage: query exec failed");
}

QList<JidDb::Message> JidDb::getMessages(const QString &jid,
    const int tail) const
{
    QList<Message> ret;
    QSqlQuery q(db);

    ensureContactDb(jid);

    if (tail < 0)
    {
        if (!q.exec("select * from '" + jid + "' order by time;"))
            throw std::runtime_error("JidDb::getMessages: query exec failed");
    }
    else if (!q.exec("select * from '" + jid + "' order by time desc limit "
        + QString::number(tail) + ";"))
        throw std::runtime_error("JidDb::getMessages: query exec failed");

    while (q.next())
    {
        Message m;
        bool ok;
        qlonglong t = q.value("time").toLongLong(&ok);

        if (!ok)
            throw std::runtime_error("JidDb::getMessages: invalid time");

        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
            throw std::invalid_argument("JidDb::getMessages: invalid direction");

        ret << m;
    }

    return ret;
}

void 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 + "')"))
        throw std::runtime_error("JidDb::storeMessage: query exec 2 failed");
}

QList<JidDb::Conversation> JidDb::getConversations() const
{
    QSqlQuery q(db);

    if (!q.exec("select name from sqlite_schema where "
        "type = 'table' and name not like 'sqlite_%'"))
        throw std::runtime_error("JidDb::getConversations: query failed");

    QStringList conversations;

    while (q.next())
        conversations << q.value("name").toString();

    QList<Conversation> ret;

    for (const auto &jid : conversations)
        if (jid.contains('@'))
        {
            const auto messages = getMessages(jid, 1);

            if (!messages.isEmpty())
            {
                const auto &m = messages.first();

                ret << Conversation(jid, m.body, m.dt);
            }
        }

    return ret;
}