forked from adamreichold/QMediathekView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
271 lines (209 loc) · 6.02 KB
/
database.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
/*
Copyright 2016 Adam Reichold
This file is part of QMediathekView.
QMediathekView 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 3 of the License, or
(at your option) any later version.
QMediathekView 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 QMediathekView. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database.h"
#include <QDebug>
#include <QStandardPaths>
#include "settings.h"
namespace
{
struct StringData
{
const char* ptr;
std::size_t len;
};
inline StringData fromBytes(const QByteArray& bytes)
{
return { bytes.constData(), static_cast< std::size_t >(bytes.length()) };
}
inline QString toString(const StringData& data)
{
return QString::fromUtf8(data.ptr, data.len);
}
struct ShowData
{
StringData channel;
StringData topic;
StringData title;
std::int64_t date;
std::uint32_t time;
std::uint32_t duration;
StringData description;
StringData website;
StringData url;
StringData url_small;
StringData url_large;
};
}
extern "C"
{
void append_integer(void* integers, std::int64_t data)
{
static_cast< QVector< quintptr >* >(integers)->append(data);
}
void append_string(void* strings, StringData data)
{
static_cast< QStringList* >(strings)->append(toString(data));
}
void fetch_show(void* show, const ShowData* data)
{
const auto show_ = static_cast< QMediathekView::Show* >(show);
show_->channel = toString(data->channel);
show_->topic = toString(data->topic);
show_->title = toString(data->title);
show_->date = QDate::fromJulianDay(data->date);
show_->time = QTime::fromMSecsSinceStartOfDay(data->time * 1000);
show_->duration = QTime::fromMSecsSinceStartOfDay(data->duration * 1000);
show_->description = toString(data->description);
show_->website = toString(data->website);
show_->url = toString(data->url);
show_->urlSmall = toString(data->url_small);
show_->urlLarge = toString(data->url_large);
}
Internals* internals_init(const char* path, bool* needs_update);
void internals_drop(Internals* internals);
struct Completion
{
void* context;
void (*action)(void* context, const char* error);
};
void internals_full_update(
Internals* internals,
const char* url,
Completion completion);
void internals_partial_update(
Internals* internals,
const char* url,
Completion completion);
void internals_channels(
Internals* internals,
void* channels);
void internals_topics(
Internals* internals,
StringData channel,
void* topics);
void internals_query(
Internals* internals,
StringData channel,
StringData topic,
StringData title,
QMediathekView::Database::SortColumn sortColumn,
QMediathekView::Database::SortOrder sortOrder,
void* ids);
void internals_fetch(
Internals* internals,
std::int64_t id,
void* show);
}
namespace QMediathekView
{
Database::Database(Settings& settings, QObject* parent)
: QObject(parent)
, m_settings(settings)
{
const auto path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
bool needsUpdate = false;
m_internals = internals_init(path.toLocal8Bit().constData(), &needsUpdate);
if(needsUpdate)
{
m_settings.resetDatabaseUpdatedOn();
}
}
Database::~Database()
{
if(m_internals != nullptr)
{
internals_drop(m_internals);
}
}
void Database::fullUpdate(const QString& url)
{
if(m_internals != nullptr)
{
internals_full_update(
m_internals,
url.toUtf8().constData(),
Completion { this, updateCompleted }
);
}
}
void Database::partialUpdate(const QString& url)
{
if(m_internals != nullptr)
{
internals_partial_update(
m_internals,
url.toUtf8().constData(),
Completion { this, updateCompleted }
);
}
}
void Database::updateCompleted(void* context, const char* error)
{
Database* self = static_cast< Database* >(context);
if (error != nullptr)
{
emit self->failedToUpdate(QString::fromUtf8(error));
return;
}
self->m_settings.setDatabaseUpdatedOn();
emit self->updated();
}
QVector< quintptr > Database::query(const QString& channel, const QString& topic, const QString& title, SortColumn sortColumn, SortOrder sortOrder) const
{
QVector< quintptr > ids;
if(m_internals != nullptr)
{
const auto channel_ = channel.toUtf8();
const auto topic_ = topic.toUtf8();
const auto title_ = title.toUtf8();
internals_query(
m_internals,
fromBytes(channel_), fromBytes(topic_), fromBytes(title_),
sortColumn, sortOrder,
&ids);
}
return ids;
}
std::unique_ptr< Show > Database::show(const quintptr id) const
{
std::unique_ptr< Show > show(new Show);
if(m_internals != nullptr)
{
internals_fetch(m_internals, id, show.get());
}
return show;
}
QStringList Database::channels() const
{
QStringList channels;
channels.append(QString());
if(m_internals != nullptr)
{
internals_channels(m_internals, &channels);
}
return channels;
}
QStringList Database::topics(const QString& channel) const
{
QStringList topics;
topics.append(QString());
if(m_internals != nullptr)
{
const auto channel_ = channel.toUtf8();
internals_topics(m_internals, fromBytes(channel_), &topics);
}
return topics;
}
} // QMediathekView