summaryrefslogtreecommitdiff
path: root/plugin/src/Caelestia/appdb.cpp
blob: 20e37bbe40b72b4c8025b7e56a867d7b3a9a46ab (plain)
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
#include "appdb.hpp"

#include <qsqldatabase.h>
#include <qsqlquery.h>
#include <quuid.h>

namespace caelestia {

AppEntry::AppEntry(QObject* entry, unsigned int frequency, QObject* parent)
    : QObject(parent)
    , m_entry(entry)
    , m_frequency(frequency) {}

QObject* AppEntry::entry() const {
    return m_entry;
}

quint32 AppEntry::frequency() const {
    return m_frequency;
}

void AppEntry::setFrequency(unsigned int frequency) {
    if (m_frequency != frequency) {
        m_frequency = frequency;
        emit frequencyChanged();
    }
}

void AppEntry::incrementFrequency() {
    m_frequency++;
    emit frequencyChanged();
}

QString AppEntry::id() const {
    return m_entry->property("id").toString();
}

QString AppEntry::name() const {
    return m_entry->property("name").toString();
}

QString AppEntry::desc() const {
    return m_entry->property("comment").toString();
}

QString AppEntry::execString() const {
    return m_entry->property("execString").toString();
}

QString AppEntry::wmClass() const {
    return m_entry->property("startupClass").toString();
}

QString AppEntry::genericName() const {
    return m_entry->property("genericName").toString();
}

QString AppEntry::categories() const {
    return m_entry->property("categories").toStringList().join(" ");
}

QString AppEntry::keywords() const {
    return m_entry->property("keywords").toStringList().join(" ");
}

AppDb::AppDb(QObject* parent)
    : QObject(parent)
    , m_uuid(QUuid::createUuid().toString()) {
    auto db = QSqlDatabase::addDatabase("QSQLITE", m_uuid);
    db.setDatabaseName(":memory:");
    db.open();

    QSqlQuery query(db);
    query.exec("CREATE TABLE IF NOT EXISTS frequencies (id TEXT PRIMARY KEY, frequency INTEGER)");
}

QString AppDb::uuid() const {
    return m_uuid;
}

QString AppDb::path() const {
    return m_path;
}

void AppDb::setPath(const QString& path) {
    if (m_path == path) {
        return;
    }

    m_path = path;
    emit pathChanged();

    auto db = QSqlDatabase::database(m_uuid, false);
    db.close();
    db.setDatabaseName(path);

    updateAppFrequencies();
}

QList<QObject*> AppDb::entries() const {
    return m_entries;
}

void AppDb::setEntries(const QList<QObject*>& entries) {
    if (m_entries == entries) {
        return;
    }

    m_entries = entries;
    emit entriesChanged();

    updateApps();
}

QList<AppEntry*> AppDb::apps() const {
    auto apps = m_apps.values();
    std::sort(apps.begin(), apps.end(), [](AppEntry* a, AppEntry* b) {
        if (a->frequency() != b->frequency()) {
            return a->frequency() > b->frequency();
        }
        return a->name().localeAwareCompare(b->name()) < 0;
    });
    return apps;
}

void AppDb::incrementFrequency(const QString& id) {
    auto db = QSqlDatabase::database(m_uuid);
    QSqlQuery query(db);

    query.prepare("INSERT INTO frequencies (id, frequency) "
                  "VALUES (:id, 1) "
                  "ON CONFLICT (id) DO UPDATE SET frequency = frequency + 1");
    query.bindValue(":id", id);
    query.exec();

    for (auto app : m_apps) {
        if (app->id() == id) {
            const auto before = apps();

            app->incrementFrequency();

            if (before != apps()) {
                emit appsChanged();
            }

            return;
        }
    }

    qWarning() << "AppDb::incrementFrequency: could not find app with id" << id;
}

quint32 AppDb::getFrequency(const QString& id) const {
    auto db = QSqlDatabase::database(m_uuid);
    QSqlQuery query(db);

    query.prepare("SELECT frequency FROM frequencies WHERE id = :id");
    query.bindValue(":id", id);

    if (query.exec() && query.next()) {
        return query.value(0).toUInt();
    }

    return 0;
}

void AppDb::updateAppFrequencies() {
    for (auto app : m_apps) {
        app->setFrequency(getFrequency(app->id()));
    }
}

void AppDb::updateApps() {
    bool dirty = false;

    for (auto entry : m_entries) {
        const auto id = entry->property("id").toString();
        if (!m_apps.contains(id)) {
            dirty = true;
            m_apps.insert(id, new AppEntry(entry, getFrequency(id), this));
        }
    }

    QSet<QString> newIds;
    for (auto entry : m_entries) {
        newIds.insert(entry->property("id").toString());
    }

    for (auto id : m_apps.keys()) {
        if (!newIds.contains(id)) {
            dirty = true;
            delete m_apps.take(id);
        }
    }

    if (dirty) {
        emit appsChanged();
    }
}

} // namespace caelestia