forked from lirios/text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistorymanager.cpp
More file actions
307 lines (273 loc) · 9.74 KB
/
historymanager.cpp
File metadata and controls
307 lines (273 loc) · 9.74 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
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
/*
* Copyright © 2016-2017 Andrew Penkrat
*
* This file is part of Liri Text.
*
* Liri Text 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.
*
* Liri Text 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 Liri Text. If not, see <http://www.gnu.org/licenses/>.
*/
#include "historymanager.h"
#include <QSqlDatabase>
#include <QStandardPaths>
#include <QDateTime>
#include <QDir>
#include <QSqlQuery>
#include <QSqlDriver>
#include <QDebug>
const int MAX_HISTORY_SIZE = 24;
HistoryManager::HistoryManager(QObject *parent)
: QAbstractListModel(parent)
, m_connId(QStringLiteral("history"))
{
QDir dataDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (!dataDir.exists())
dataDir.mkpath(QStringLiteral("."));
QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), m_connId);
db.setDatabaseName(dataDir.filePath(QStringLiteral("history.db")));
db.open();
QSqlQuery(QStringLiteral("CREATE TABLE IF NOT EXISTS history "
"(path TEXT PRIMARY KEY, display_name TEXT, last_view_time INTEGER, "
"preview TEXT, cursor_position INTEGER, scroll_position REAL)"),
db);
}
HistoryManager::~HistoryManager()
{
QSqlDatabase::removeDatabase(m_connId);
}
HistoryManager *HistoryManager::getInstance()
{
if (!m_instance)
m_instance = new HistoryManager();
return m_instance;
}
int HistoryManager::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
QSqlQuery query(QSqlDatabase::database(m_connId));
query.exec(QStringLiteral("SELECT Count(*) FROM history"));
if (query.first())
return query.value(0).toInt();
else
return 0;
}
QVariant HistoryManager::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= rowCount())
return QVariant();
QSqlQuery query(QSqlDatabase::database(m_connId));
query.prepare(QStringLiteral("SELECT %1 FROM history "
"ORDER BY last_view_time DESC")
.arg(dbColumnFromRole(role)));
query.exec();
if (query.seek(index.row())) {
switch (role) {
case FileUrlRole:
return QUrl::fromLocalFile(query.value(0).toString());
break;
case LastViewTimeRole:
return QDateTime::fromSecsSinceEpoch(query.value(0).toInt());
break;
default:
return query.value(0);
break;
}
}
return QVariant();
}
bool HistoryManager::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.row() < 0 || index.row() >= rowCount())
return false;
QSqlQuery query(QSqlDatabase::database(m_connId));
query.prepare(QStringLiteral("UPDATE history SET "
"%1=? WHERE path=?")
.arg(dbIdForIndex(index.row())));
query.addBindValue(dbColumnFromRole(role));
query.addBindValue(value.toString());
query.exec();
emit dataChanged(index, index, { role });
return true;
}
bool HistoryManager::removeRow(int row, const QModelIndex &parent)
{
Q_UNUSED(parent)
if (row < 0 || row >= rowCount())
return false;
beginRemoveRows(QModelIndex(), row, row);
QSqlQuery query(QSqlDatabase::database(m_connId));
query.prepare(QStringLiteral("DELETE FROM history "
"WHERE path=?"));
query.addBindValue(dbIdForIndex(row));
query.exec();
endRemoveRows();
emit countChanged();
return true;
}
bool HistoryManager::removeFile(const QUrl &fileUrl)
{
int row = dbIndexForId(fileUrl.path());
beginRemoveRows(QModelIndex(), row, row);
QSqlQuery query(QSqlDatabase::database(m_connId));
query.prepare(QStringLiteral("DELETE FROM history "
"WHERE path=?"));
query.addBindValue(fileUrl.path());
query.exec();
endRemoveRows();
emit countChanged();
return true;
}
Qt::ItemFlags HistoryManager::flags(const QModelIndex &index) const
{
Q_UNUSED(index)
return { Qt::ItemIsEnabled, Qt::ItemIsSelectable, Qt::ItemIsEditable };
}
QVariantMap HistoryManager::getFileEditingInfo(const QUrl &fileUrl) const
{
QSqlQuery query(QSqlDatabase::database(m_connId));
query.prepare(QStringLiteral("SELECT cursor_position, scroll_position FROM history "
"WHERE path=?"));
query.addBindValue(fileUrl.path());
query.exec();
QVariantMap result;
if (query.first()) {
result[QStringLiteral("cursorPosition")] = query.value(0);
result[QStringLiteral("scrollPosition")] = query.value(1);
}
return result;
}
void HistoryManager::touchFile(const QString &name, const QUrl &fileUrl, int cursorPosition,
float scrollPosition, const QString &preview)
{
qint64 currentTime = QDateTime::currentDateTime().toSecsSinceEpoch();
int row = dbIndexForId(fileUrl.path());
QSqlQuery query(QSqlDatabase::database(m_connId));
query.prepare(QStringLiteral(
"UPDATE history SET "
"path=?, display_name=?, last_view_time=?, preview=?, cursor_position=?, scroll_position=? "
"WHERE path=?"));
query.addBindValue(fileUrl.path());
query.addBindValue(name);
query.addBindValue(currentTime);
query.addBindValue(preview);
query.addBindValue(cursorPosition);
query.addBindValue(scrollPosition);
query.addBindValue(fileUrl.path());
query.exec();
query.exec(QStringLiteral("SELECT Changes() FROM history"));
if (!query.first() || query.value(0) == 0) {
// If update failed, insert
beginInsertRows(QModelIndex(), 0, 0);
query.prepare(QStringLiteral(
"INSERT INTO history "
"(path, display_name, last_view_time, preview, cursor_position, scroll_position) "
"VALUES (?, ?, ?, ?, ?, ?)"));
query.addBindValue(fileUrl.path());
query.addBindValue(name);
query.addBindValue(currentTime);
query.addBindValue(preview);
query.addBindValue(cursorPosition);
query.addBindValue(scrollPosition);
query.exec();
endInsertRows();
int entryCount = rowCount();
if (entryCount > MAX_HISTORY_SIZE) {
removeRow(entryCount - 1);
/*
query.exec("SELECT path FROM history "
"ORDER BY last_view_time ASC");
if(query.first()) {
QString id = query.value(0).toString();
emit beginRemoveRows(QModelIndex(), entryCount - 1, entryCount - 1);
query.prepare("DELETE FROM history "
"WHERE path=?");
query.addBindValue(id);
query.exec();
emit endRemoveRows();
}
*/
} else
emit countChanged();
} else {
emit dataChanged(
index(row), index(row),
{ NameRole, LastViewTimeRole, PreviewRole, CursorPositionRole, ScrollPositionRole });
if (row > 0) {
beginMoveRows(QModelIndex(), row, row, QModelIndex(), 0);
endMoveRows();
}
}
}
QHash<int, QByteArray> HistoryManager::roleNames() const
{
return QHash<int, QByteArray>({ { NameRole, "name" },
{ FileUrlRole, "fileUrl" },
{ FilePathRole, "filePath" },
{ LastViewTimeRole, "lastViewTime" },
{ PreviewRole, "previewText" },
{ CursorPositionRole, "cursorPosition" },
{ ScrollPositionRole, "scrollPosition" } });
}
QString HistoryManager::dbColumnFromRole(int role) const
{
switch (role) {
case NameRole:
return QStringLiteral("display_name");
break;
case FileUrlRole:
case FilePathRole:
return QStringLiteral("path");
break;
case LastViewTimeRole:
return QStringLiteral("last_view_time");
break;
case PreviewRole:
return QStringLiteral("preview");
break;
case CursorPositionRole:
return QStringLiteral("cursor_position");
break;
case ScrollPositionRole:
return QStringLiteral("scroll_position");
break;
default:
return QLatin1String("");
}
}
QString HistoryManager::dbIdForIndex(int index) const
{
QSqlQuery query(QSqlDatabase::database(m_connId));
query.exec(QStringLiteral("SELECT path FROM history "
"ORDER BY last_view_time DESC"));
if (query.seek(index))
return query.value(0).toString();
return QLatin1String("");
}
int HistoryManager::dbIndexForId(const QString &id) const
{
QSqlQuery query(QSqlDatabase::database(m_connId));
query.prepare(QStringLiteral("SELECT last_view_time FROM history "
"WHERE path=?"));
query.addBindValue(id);
query.exec();
if (query.first()) {
int time = query.value(0).toInt();
query.prepare(QStringLiteral("SELECT Count(*) FROM history "
"WHERE last_view_time>?"));
query.addBindValue(time);
query.exec();
if (query.first())
return query.value(0).toInt();
}
return 0;
}
HistoryManager *HistoryManager::m_instance = nullptr;