Skip to content

Latest commit

 

History

History
428 lines (355 loc) · 12.4 KB

declarativetransfermodel.cpp

File metadata and controls

428 lines (355 loc) · 12.4 KB
 
Sep 19, 2019
Sep 19, 2019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* Copyright (c) 2014 - 2019 Jolla Ltd.
*
* All rights reserved.
*
* This file is part of Sailfish Transfer Engine package.
*
* You may use this file under the terms of the GNU Lesser General
* Public License version 2.1 as published by the Free Software Foundation
* and appearing in the file license.lgpl included in the packaging
* of this file.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* and appearing in the file license.lgpl included in the packaging
* of this file.
*
* This library 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
* Lesser General Public License for more details.
*/
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "declarativetransfermodel.h"
#include "synchronizelists_p.h"
#include "transferdbrecord.h"
#include <QQmlEngine>
#include <qqml.h>
#include <qqmlinfo.h>
#include <QSqlQuery>
#include <QSqlError>
#include <QThreadStorage>
#define DB_PATH ".local/nemo-transferengine"
#define DB_NAME "transferdb.sqlite"
template <> bool compareIdentity<TransferDBRecord>(
const TransferDBRecord &item, const TransferDBRecord &reference)
{
return item.transfer_id == reference.transfer_id;
}
Sep 4, 2014
Sep 4, 2014
46
class TransferDatabase : public QSqlDatabase
Sep 3, 2014
Sep 3, 2014
47
48
{
public:
Sep 4, 2014
Sep 4, 2014
49
TransferDatabase() : QSqlDatabase(QLatin1String("QSQLITE")) {}
Sep 3, 2014
Sep 3, 2014
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
TransferModel::TransferModel(QObject *parent)
: QAbstractListModel(parent)
, m_rows(new QVector<TransferDBRecord>())
, m_rowsIndex(0)
, m_asyncIndex(0)
, m_transfersInProgress(0)
, m_status(Null)
, m_asyncStatus(Null)
, m_asyncPending(false)
, m_asyncRunning(false)
, m_notified(false)
, m_complete(false)
, m_rowsChanges(false)
{
setAutoDelete(false);
}
TransferModel::~TransferModel()
{
QMutexLocker locker(&m_mutex);
if (m_asyncRunning) {
m_condition.wait(&m_mutex);
}
delete m_rows;
}
TransferModel::Status TransferModel::status() const
{
return m_status;
}
void TransferModel::refresh()
{
if (!m_complete || m_roles.isEmpty()) {
return;
}
QMutexLocker locker(&m_mutex);
if (!m_asyncRunning) {
m_asyncRunning = true;
QThreadPool::globalInstance()->start(static_cast<QRunnable *>(this));
}
m_status = Querying;
m_asyncStatus = Querying;
m_asyncPending = true;
locker.unlock();
if (m_status != Querying) {
m_status = Querying;
emit statusChanged();
}
}
QJSValue TransferModel::get(int index) const
{
QJSValue properties;
QQmlEngine *engine = qmlEngine(this);
if (engine && index >= 0 && index < m_rows->count()) {
properties = engine->newObject();
const TransferDBRecord &row = m_rows->at(index);
foreach (const int &key, m_roles.keys()) {
const QString name = m_roles.value(key);
if (!name.isEmpty() && key >= 0) {
properties.setProperty(name, engine->toScriptValue(row.value(key)));
}
}
}
return properties;
}
void TransferModel::clearTransfers()
{
if (m_client) {
m_client->clearTransfers();
}
}
Aug 10, 2016
Aug 10, 2016
135
136
137
138
139
140
141
void TransferModel::clearTransfer(int transferId)
{
if (m_client) {
m_client->clearTransfer(transferId);
}
}
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
QHash<int, QByteArray> TransferModel::roleNames() const
{
return m_roles;
}
int TransferModel::rowCount(const QModelIndex &parent) const
{
return !parent.isValid() ? m_rows->count() : 0;
}
QModelIndex TransferModel::index(int row, int column, const QModelIndex &parent) const
{
return !parent.isValid() && row >= 0 && row < m_rows->count() && column == 0
? createIndex(row, column)
: QModelIndex();
}
QVariant TransferModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || role < 0) {
return QVariant();
} else if (role < m_roles.count()) {
return m_rows->at(index.row()).value(role);
} else {
return QVariant();
}
}
void TransferModel::classBegin()
{
}
void TransferModel::componentComplete()
{
m_complete = true;
m_roles[TransferDBRecord::TransferID] = "transferId";
m_roles[TransferDBRecord::TransferType] = "transferType";
m_roles[TransferDBRecord::Progress] = "progress";
m_roles[TransferDBRecord::URL] = "url";
m_roles[TransferDBRecord::Status] = "status";
m_roles[TransferDBRecord::PluginID] = "pluginId";
m_roles[TransferDBRecord::Timestamp] = "timestamp";
m_roles[TransferDBRecord::DisplayName] = "displayName";
m_roles[TransferDBRecord::ResourceName] = "resourceName";
m_roles[TransferDBRecord::MimeType] = "mimeType";
m_roles[TransferDBRecord::FileSize] = "fileSize";
m_roles[TransferDBRecord::ServiceIcon] = "serviceIcon";
m_roles[TransferDBRecord::ApplicationIcon] = "applicationIcon";
m_roles[TransferDBRecord::ThumbnailIcon] = "thumbnailIcon";
m_roles[TransferDBRecord::CancelSupported] = "cancelEnabled";
m_roles[TransferDBRecord::RestartSupported] = "restartEnabled";
refresh();
m_client = new TransferEngineInterface("org.nemo.transferengine",
"/org/nemo/transferengine",
QDBusConnection::sessionBus(),
this);
connect(m_client, SIGNAL(progressChanged(int,double)),
this, SLOT(refresh()));
connect(m_client, SIGNAL(transfersChanged()),
this, SLOT(refresh()));
connect(m_client, SIGNAL(statusChanged(int,int)),
this, SLOT(refresh()));
}
void TransferModel::insertRange(
int index, int count, const QVector<TransferDBRecord> &source, int sourceIndex)
{
if (m_rowsChanges) {
beginInsertRows(QModelIndex(), index, index + count - 1);
for (int i = 0; i < count; ++i) {
m_rows->insert(index + i, source.at(sourceIndex + i));
}
endInsertRows();
emit countChanged();
}
}
void TransferModel::updateRange(
int index, int count, const QVector<TransferDBRecord> &source, int sourceIndex)
{
for (int i = 0; i < count; ++i) {
const int begin = i;
while (i < count
&& m_rows->at(i).transfer_id == source.at(sourceIndex + i).transfer_id
&& (m_rows->at(i).progress != source.at(sourceIndex + i).progress
|| m_rows->at(i).status != source.at(sourceIndex + i).status)) {
TransferDBRecord &destinationRow = (*m_rows)[index + i];
const TransferDBRecord &sourceRow = source.at(sourceIndex + i);
destinationRow = sourceRow;
++i;
}
if (begin != i) {
emit dataChanged(createIndex(index + begin, 0), createIndex(index + i - 1, 0));
}
}
}
void TransferModel::removeRange(int index, int count)
{
if (m_rowsChanges) {
beginRemoveRows(QModelIndex(), index, index + count - 1);
m_rows->remove(index, count);
endRemoveRows();
emit countChanged();
}
}
bool TransferModel::event(QEvent *event)
{
if (event->type() == QEvent::UpdateRequest) {
QMutexLocker locker(&m_mutex);
const Status previousStatus = m_status;
const QString errorString = m_asyncErrorString;
m_status = m_asyncStatus;
m_notified = false;
const QVector<TransferDBRecord> rows = m_asyncRows;
locker.unlock();
m_rowsChanges = true;
synchronizeList(this, *m_rows, m_rowsIndex, rows, m_asyncIndex);
if (m_status >= Finished) {
completeSynchronizeList(this, *m_rows, m_rowsIndex, rows, m_asyncIndex);
}
m_rowsChanges = false;
if (m_asyncTransfersInProgress != m_transfersInProgress) {
m_transfersInProgress = m_asyncTransfersInProgress;
emit transfersInProgressChanged();
}
if (previousStatus != m_status) {
if (m_status == Error) {
qmlInfo(this) << errorString;
}
emit statusChanged();
}
return true;
} else {
return QAbstractListModel::event(event);
}
}
int TransferModel::transfersInProgress() const
{
return m_transfersInProgress;
}
void TransferModel::run()
{
QMutexLocker locker(&m_mutex);
for (;;) {
if (m_asyncStatus == Querying) {
m_asyncPending = false;
if (m_status == Null) {
m_asyncStatus = Null;
continue;
}
locker.unlock();
QVector<TransferDBRecord> rows;
QString errorString;
int activeTransfers = 0;
const bool ok = executeQuery(&rows, &activeTransfers, &errorString);
locker.relock();
m_asyncRows = rows;
m_asyncTransfersInProgress = activeTransfers;
if (!m_asyncPending) {
m_asyncErrorString = errorString;
m_asyncStatus = ok ? Finished : Error;
}
} else {
m_asyncRunning = false;
if (!m_notified) {
m_notified = true;
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
m_condition.wakeOne();
return;
}
}
}
bool TransferModel::executeQuery(QVector<TransferDBRecord> *rows, int *activeTransfers, QString *errorString)
{
// Query items from the database
QSqlDatabase db = database();
if (!db.isValid()) {
qWarning() << Q_FUNC_INFO << "Invalid database!";
return false;
}
QSqlQuery query(db);
query.setForwardOnly(true);
if (!query.exec(QString(QStringLiteral("SELECT * FROM transfers ORDER BY transfer_id DESC")))) {
qWarning() << Q_FUNC_INFO;
qWarning() << "Failed to create tmp table";
qWarning() << query.lastQuery();
qWarning() << query.lastError().text();
*errorString = query.lastError().text();
return false;
}
*activeTransfers = 0;
while (query.next()) {
int i = 0;
TransferDBRecord record;
record.transfer_id = query.value(i++).toInt();
record.transfer_type = query.value(i++).toInt();
record.timestamp = query.value(i++).toString();
record.status = query.value(i++).toInt();
record.progress = query.value(i++).toDouble();
record.display_name = query.value(i++).toString();
record.application_icon = query.value(i++).toString();
record.thumbnail_icon = query.value(i++).toString();
record.service_icon = query.value(i++).toString();
record.url = query.value(i++).toString();
record.resource_name = query.value(i++).toString();
record.mime_type = query.value(i++).toString();
record.size = query.value(i++).toInt();
record.plugin_id = query.value(i++).toString();
i++; // account id
i++; // strip metadata
i++; // scale percent
record.cancel_supported = query.value(i++).toBool();
record.restart_supported = query.value(i++).toBool();
if (record.status == TransferModel::TransferStarted) {
++(*activeTransfers);
}
rows->append(record);
}
QMutexLocker locker(&m_mutex);
m_asyncRows = *rows;
return true;
}
QSqlDatabase TransferModel::database()
{
Sep 3, 2014
Sep 3, 2014
407
static QThreadStorage<QSqlDatabase> thread_database;
408
409
410
411
412
413
if (!thread_database.hasLocalData()) {
const QString absDbPath = QDir::homePath() + QDir::separator()
+ DB_PATH + QDir::separator()
+ DB_NAME;
Sep 4, 2014
Sep 4, 2014
414
TransferDatabase database;
415
416
database.setDatabaseName(absDbPath);
database.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY")); // sanity check
Jan 17, 2018
Jan 17, 2018
417
thread_database.setLocalData(database);
Sep 3, 2014
Sep 3, 2014
420
QSqlDatabase &database = thread_database.localData();
421
422
423
424
425
426
427
428
if (!database.isOpen() && !database.open()) {
qWarning() << "Failed to open transfer engine database";
qWarning() << database.lastError();
return QSqlDatabase();
}
return database;
}