-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.cpp
135 lines (112 loc) · 3.83 KB
/
library.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
#include "library.h"
#include "documenthandler.h"
#include <QSettings>
#include <QStandardPaths>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
Q_LOGGING_CATEGORY(EDIM_LIBRARY, "edim.Library")
Library::Library(QObject* parent) :
QSortFilterProxyModel(parent),
_sourceModel(this)
{
setupDatabase();
_sourceModel.setRootPath(basePath().absolutePath());
setSourceModel(&_sourceModel);
}
QDir Library::basePath() const
{
QSettings settings;
return QDir(settings.value("library/basePath", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString());
}
bool Library::contains(const QFileInfo& document) const
{
QSqlQuery q;
q.prepare("SELECT * FROM document WHERE libraryPath = :libraryPath");
q.bindValue(":libraryPath", libraryPath(document));
q.exec();
// TODO: Size hack for SQLite since it doesn't support .size() :-(
q.last();
return q.at() + 1 > 0;
}
QList<QFileInfo> Library::search(const QString& text) const
{
QSqlQuery q;
q.prepare("SELECT libraryPath FROM document WHERE content LIKE :searchTerm");
q.bindValue(":searchTerm", QString("%%1%").arg(text));
q.exec();
QList<QFileInfo> result;
QString base(basePath().absolutePath());
while (q.next()) {
QString path(q.value("libraryPath").toString());
result.append(QFileInfo(path.replace(0, 1, base)));
}
return result;
}
QModelIndex Library::index(const QFileInfo& document) const
{
return mapFromSource(_sourceModel.index(document.absoluteFilePath()));
}
QFileInfo Library::fileInfo(const QModelIndex& index) const
{
return _sourceModel.fileInfo(mapToSource(index));
}
QVariant Library::data(const QModelIndex& index, int role) const
{
// Enhance source model with data from database
if (role == DocumentContentRole) {
QSqlQuery q;
q.prepare("SELECT content FROM document WHERE libraryPath = :path");
q.bindValue(":path", libraryPath(fileInfo(index)));
q.exec();
if (q.next()) {
return q.value(0);
} else {
return QVariant();
}
}
return QSortFilterProxyModel::data(index, role);
}
void Library::import(const QFileInfo& document)
{
QSqlQuery q;
q.prepare("INSERT INTO document (libraryPath, content) VALUES (:libraryPath, :content)");
q.bindValue(":libraryPath", libraryPath(document));
q.bindValue(":content", _documentHandler.text(document));
if (!q.exec()) {
qCWarning(EDIM_LIBRARY) << "Could not import document into database" << q.lastError();
}
}
void Library::update(const QFileInfo& document)
{
Q_UNUSED(document)
}
void Library::setupDatabase()
{
// Check data location
QDir dataLocation(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
if (!dataLocation.exists()) {
if (!dataLocation.mkpath(".")) {
qCCritical(EDIM_LIBRARY) << "Could not access data location at" << dataLocation.absolutePath();
}
}
// Connect to database
QString databaseFile(dataLocation.absolutePath().append(QDir::separator()).append("data.sqlite"));
QSqlDatabase database(QSqlDatabase::addDatabase("QSQLITE"));
database.setDatabaseName(databaseFile);
if (!database.open()) {
qCCritical(EDIM_LIBRARY) << "Could not connect to database";
}
// Enable FOREIGN KEY support
QSqlQuery q;
if (!q.exec("PRAGMA foreign_keys = ON")) {
qCWarning(EDIM_LIBRARY) << "Could not enable FOREIGN KEY support";
}
if (!q.exec("CREATE TABLE IF NOT EXISTS document (id INTEGER PRIMARY KEY AUTOINCREMENT, libraryPath TEXT, content TEXT)")) {
qCWarning(EDIM_LIBRARY) << "Could not create database layout";
}
}
QString Library::libraryPath(const QFileInfo& document) const
{
return document.absoluteFilePath().replace(0, basePath().absolutePath().length(), ".");
}