forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
136 lines (123 loc) · 4.32 KB
/
database.cpp
File metadata and controls
136 lines (123 loc) · 4.32 KB
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
/*
* Copyright (c) 2013-2015 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database.h"
#include "models/playlistmodel.h"
#include <QtSql>
#include <QStandardPaths>
#include <QDir>
#include <QMutexLocker>
#include <QtDebug>
static Database* instance = 0;
Database::Database(QObject *parent) :
QObject(parent)
{
QDir dir(QStandardPaths::standardLocations(QStandardPaths::DataLocation).first());
if (!dir.exists())
dir.mkpath(dir.path());
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dir.filePath("db.sqlite3"));
db.open();
// Initialize version table, if needed.
int version = 0;
QSqlQuery query;
if (query.exec("CREATE TABLE version (version INTEGER);")) {
if (!query.exec("INSERT INTO version VALUES (0);"))
qCritical() << __PRETTY_FUNCTION__ << "Failed to create version table.";
} else if (query.exec("SELECT version FROM version")) {
query.next();
version = query.value(0).toInt();
} else {
qCritical() << __PRETTY_FUNCTION__ << "Failed to get version.";
}
if (version < 1 && upgradeVersion1())
version = 1;
qDebug() << "Database version is" << version;
}
Database &Database::singleton(QWidget *parent)
{
if (!instance)
instance = new Database(parent);
return *instance;
}
Database::~Database()
{
QString connection = QSqlDatabase::database().connectionName();
QSqlDatabase::database().close();
QSqlDatabase::removeDatabase(connection);
instance = 0;
}
bool Database::upgradeVersion1()
{
QMutexLocker lock(&m_mutex);
bool success = false;
QSqlQuery query;
if (query.exec("CREATE TABLE thumbnails (hash TEXT PRIMARY KEY NOT NULL, accessed DATETIME NOT NULL, image BLOB);")) {
success = query.exec("UPDATE version SET version = 1;");
if (!success)
qCritical() << __FUNCTION__ << query.lastError();
} else {
qCritical() << __PRETTY_FUNCTION__ << "Failed to create thumbnails table.";
}
return success;
}
bool Database::putThumbnail(const QString& hash, const QImage& image)
{
QMutexLocker lock(&m_mutex);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QSqlQuery query;
query.prepare("DELETE FROM thumbnails WHERE hash = :hash;");
query.bindValue(":hash", hash);
query.exec();
query.prepare("INSERT INTO thumbnails VALUES (:hash, datetime('now'), :image);");
query.bindValue(":hash", hash);
query.bindValue(":image", ba);
bool result = query.exec();
if (!result)
qCritical() << __FUNCTION__ << query.lastError();
deleteOldThumbnails();
return result;
}
QImage Database::getThumbnail(const QString &hash)
{
QMutexLocker lock(&m_mutex);
QImage result;
QSqlQuery query;
query.prepare("SELECT image FROM thumbnails WHERE hash = :hash;");
query.bindValue(":hash", hash);
if (query.exec() && query.first()) {
result.loadFromData(query.value(0).toByteArray(), "PNG");
QSqlQuery update;
update.prepare("UPDATE thumbnails SET accessed = datetime('now') WHERE hash = :hash ;");
update.bindValue(":hash", hash);
if (!update.exec())
qCritical() << __FUNCTION__ << update.lastError();
}
// qDebug() << __FUNCTION__ << result.byteCount();
deleteOldThumbnails();
return result;
}
void Database::deleteOldThumbnails()
{
QSqlQuery query;
// OFFSET is the numner of thumbnails to cache.
if (!query.exec("DELETE FROM thumbnails WHERE hash IN (SELECT hash FROM thumbnails ORDER BY accessed DESC LIMIT -1 OFFSET 10000);"))
qCritical() << __FUNCTION__ << query.lastError();
}