-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.cpp
368 lines (285 loc) · 9.08 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <QSqlDriver>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include "database.h"
#include "util.h"
#include "settings.h"
Database::Database(QObject *parent)
: QObject(parent)
{
// Library path from settings
mLibraryPath = Settings::value("library_path", Util::getMusicLocation()).toString();
QString database_path = Util::getAppConfigLocation() + "/library.db";
QString database_name("Library");
qDebug() << database_path;
mDatabase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), database_name);
mDatabase.setDatabaseName(QStringLiteral("file:") + database_path);
mDatabase.setConnectOptions(QStringLiteral("foreign_keys = ON;locking_mode = EXCLUSIVE;QSQLITE_OPEN_URI;QSQLITE_BUSY_TIMEOUT=500000"));
// open
auto result = mDatabase.open();
if (!result) {
qDebug() << "database not open";
}
// create db or load
initDatabase();
// nothing in library, rescan
if (isEmpty())
updateLibrary();
}
Database *Database::instance()
{
static Database* instance = new Database;
return instance;
}
QObject *Database::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return Database::instance(); // C++ and QML instance
}
Database::~Database()
{
if (mDatabase.isOpen())
mDatabase.close();
}
QStringList Database::getBookDirs()
{
QStringList result;
// loop dirs
QDir lib_dir(mLibraryPath);
QDirIterator directories(mLibraryPath, QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while(directories.hasNext()) {
directories.next();
// does folder contain any audio files? if we have audio files, this is a book
QDir current_dir(directories.filePath());
QStringList dir_list = current_dir.entryList(mFileFilters, QDir::NoDotAndDotDot | QDir::Files);
if (dir_list.count() == 0)
continue;
result.append(directories.filePath().replace(mLibraryPath, ""));
}
return result;
}
QStringList Database::getBookFiles(const QString &xPath)
{
QDir current_dir(xPath);
return current_dir.entryList(mFileFilters, QDir::NoDotAndDotDot | QDir::Files);
}
void Database::initDatabase()
{
QStringList tables = mDatabase.tables();
QSqlQuery query(mDatabase);
// check if database contains data
if (tables.contains("info", Qt::CaseInsensitive)
&& tables.contains("files", Qt::CaseInsensitive)
&& getInfo("db_version").toInt() == DB_VERSION) {
return;
}
qDebug() << "creating db";
// drop old table data
query.exec("DROP TABLE info");
query.exec("DROP TABLE files");
// create info table
query.exec("create table info "
"(key text primary key, "
"value string)");
// db version
setInfo("db_version", QString::number(DB_VERSION));
//addInfo("library_path", Library::instance()->path()); // done in library update
// create files table
query.exec("create table files "
"(path text primary key, "
"title text, "
"duration integer, "
"artist text, "
"genre text, "
"year integer)");
// create books table
query.exec("create table books "
"(path text primary key, "
"progress integer)");
}
void Database::setInfo(const QString &xKey, const QString &xValue)
{
QSqlQuery query(mDatabase);
query.prepare("REPLACE INTO info (key, value) VALUES (?, ?)");
query.addBindValue(xKey);
query.addBindValue(xValue);
if(!query.exec()) {
qDebug() << "error adding info" << mDatabase.lastError().text();
}
}
QVariant Database::getInfo(const QString &xKey)
{
QSqlQuery query(mDatabase);
query.prepare("SELECT * FROM info WHERE key = ?");
query.addBindValue(xKey);
if (query.exec()) {
while (query.next()) // get first result
return query.value("value");
}
qDebug() << "error getting info" << mDatabase.lastError().text();
return QVariant();
}
void Database::writeChapter(const Chapter &xChapter)
{
QSqlQuery query(mDatabase);
query.prepare("REPLACE INTO files (path, title, duration, artist, genre, year) VALUES (?, ?, ?, ?, ?, ?)");
query.addBindValue(xChapter.path);
query.addBindValue(xChapter.title);
query.addBindValue(xChapter.duration);
query.addBindValue(xChapter.artist);
query.addBindValue(xChapter.genre);
query.addBindValue(xChapter.year);
if(!query.exec()) {
qDebug() << "error adding chapter" << mDatabase.lastError().text();
}
}
Chapter Database::getChapter(const QString &xPath)
{
Chapter c;
c.path = xPath;
QSqlQuery query(mDatabase);
query.prepare("SELECT * FROM files WHERE path = ?");
query.addBindValue(xPath);
if (query.exec()) {
// from db
while (query.next()) { // get first result
c.title = query.value("title").toString();
c.duration = query.value("duration").toInt();
c.artist = query.value("artist").toString();
c.genre = query.value("genre").toString();
c.year = query.value("year").toInt();
}
}
if (c.isEmpty()) {
// from file
QString abs_path = mLibraryPath + xPath;
QFileInfo file_info = QFileInfo(abs_path);
c.duration = Util::getTimeMSec(abs_path);
c.artist = Util::getTagArtist(abs_path);
c.genre = Util::getTagGenre(abs_path);
c.title = Util::getTagTitle(abs_path);
c.year = Util::getTagYear(abs_path);
if (c.title.isEmpty())
c.title = Util::toCamelCase(file_info.baseName().replace("_", " "));
if (c.year == -1)
c.year = file_info.birthTime().date().year();
writeChapter(c);
}
return c;
}
void Database::writeBook(const Book &xBook)
{
QSqlQuery query(mDatabase);
query.prepare("REPLACE INTO books (path, progress) VALUES (?, ?)");
query.addBindValue(xBook.path);
query.addBindValue(xBook.progress);
if(!query.exec()) {
qDebug() << "error adding book" << mDatabase.lastError().text();
}
}
Book Database::getBook(const QString &xPath)
{
Book b;
b.path = xPath;
QSqlQuery query(mDatabase);
// get book info
query.prepare("SELECT * FROM books WHERE path = ?");
query.addBindValue(xPath);
if (query.exec()) {
// from db
while (query.next()) { // get first result
b.progress = query.value("progress").toInt();
}
}
// get chapter files
query = QSqlQuery(mDatabase);
QString q = QString("SELECT * FROM files WHERE path LIKE '%1/%'").arg(xPath);
query.prepare(q); // Note: binds are broken here, so use string arg
if (query.exec()) {
while (query.next()) { // get first result
Chapter c;
c.path = query.value("path").toString();
c.title = query.value("title").toString();
c.duration = query.value("duration").toInt();
c.artist = query.value("artist").toString();
c.genre = query.value("genre").toString();
c.year = query.value("year").toInt();
b.addChapter(c);
}
}
if (b.isEmpty()) {
// from file
QStringList chapter_files = getBookFiles(mLibraryPath + xPath);
for (QString current_file : chapter_files) {
QString chapter_path = xPath + "/" + current_file;
Chapter c = getChapter(chapter_path);
b.addChapter(c);
}
writeBook(b);
}
b.ready();
return b;
}
QString Database::libraryPath() const
{
return mLibraryPath;
}
void Database::setLibraryPath(QString &xPath) {
xPath = xPath.replace("file://","");
if (xPath == mLibraryPath)
return;
mLibraryPath = xPath;
Settings::setValue("library_path", mLibraryPath);
qDebug() << mLibraryPath;
updateLibrary();
emit pathChanged();
}
int Database::size() const
{
return mLibraryItems.size();
}
bool Database::isEmpty() const
{
return mLibraryItems.isEmpty();
}
QVector<Book> Database::getLibraryItems() {
return mLibraryItems;
}
Book * Database::getLibraryItem(QString &xPath)
{
for(Book &b: mLibraryItems) {
if (b.path == xPath)
return &b;
}
return nullptr;
}
Book *Database::getNextLibraryItem(QString &xPath)
{
for(int i=0; i<mLibraryItems.size(); ++i) {
Book &b = mLibraryItems[i];
if (b.path == xPath) {
return &mLibraryItems[(i + 1) % mLibraryItems.size()];
}
}
return nullptr;
}
bool caseInsensitiveLessThan(const Book &s1, const Book &s2) {
return s1.title.toLower() < s2.title.toLower();
}
void Database::updateLibrary() {
// delete old books
mLibraryItems = QVector<Book>();
// set library path
setInfo("library_path", mLibraryPath);
// get book directories
QStringList book_dirs = getBookDirs();
for (QString book_path : book_dirs) {
Book book = getBook(book_path);
mLibraryItems.append(book);
}
// sort
qSort(mLibraryItems.begin(), mLibraryItems.end(), caseInsensitiveLessThan);
emit libraryUpdated();
}