/
model.cpp
281 lines (232 loc) · 6.62 KB
/
model.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
#include "model.h"
#include <QtWidgets>
#include <QtSql>
#include <QtNetwork>
#include <QDebug>
namespace {
QString outputFilename(const QString &outputDir, const QString &id) {
return (outputDir + "/" + id + ".mp4");
}
} // namespace
ModelItem::ModelItem()
: status(Unknown)
{
}
Model::Model(QObject *parent)
: QAbstractTableModel(parent)
, reply(NULL)
, itemsIndex(0)
{
networkManager = new QNetworkAccessManager(this);
}
int Model::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return items.count();
}
int Model::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant Model::data(const QModelIndex &index, int role) const
{
switch (role)
{
case Qt::DisplayRole:
switch (index.column()) {
case 0:
return items.at(index.row()).id;
case 1:
return items.at(index.row()).progress;
case 2:
return items.at(index.row()).vod_path;
}
case Qt::BackgroundRole:
switch (items.at(index.row()).status) {
case ModelItem::Unknown:
return QVariant();
case ModelItem::Ready:
return QColor("gray");
case ModelItem::AuthFailed:
return QColor("pink");
case ModelItem::InProgress:
return QColor("yellow");
case ModelItem::Finished:
return QColor("green");
case ModelItem::Error:
return QColor("red");
}
}
return QVariant();
}
QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical) {
if (role == Qt::DisplayRole )
return section + 1;
return QVariant();
}
switch (role)
{
case Qt::DisplayRole:
switch (section) {
case 0:
return "id";
case 1:
return "progress";
case 2:
return "vod_path";
}
}
return QVariant();
}
void Model::reset()
{
beginResetModel();
items.clear();
endResetModel();
}
void Model::addItem(const QString &id, const QString &vod_path)
{
ModelItem item;
item.id = id;
item.vod_path = vod_path;
initItemStatus(&item);
items << item;
}
void Model::initItemStatus(ModelItem *item)
{
const QFileInfo fi(outputFilename(outputDir, item->id));
if (fi.exists()) {
item->status = ModelItem::Finished;
return;
}
if (item->vod_path.isEmpty()) {
item->status = ModelItem::Error;
return;
}
item->status = ModelItem::Ready;
}
void Model::startItemDownloading()
{
QVector<ModelItem>::iterator it = items.begin();
do {
if (it == items.end()) {
qDebug() << "FINISHED";
emit errorMessage("Finished");
return;
}
if ((it->status == ModelItem::Ready)
||(it->status == ModelItem::AuthFailed))
break;
++it;
} while(true);
qDebug() << "===== new reply";
if (reply)
reply->deleteLater();
networkManager->clearAccessCache();
reply = networkManager->get(QNetworkRequest(QUrl(it->vod_path)));
connect(reply, &QNetworkReply::downloadProgress, this, &Model::onDownloadProgress);
connect(reply, &QNetworkReply::readyRead, this, &Model::onReadyRead);
connect(reply, &QNetworkReply::finished, this, &Model::onReplyFinished);
connect(reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), this, &Model::slotError);
connect(reply, &QNetworkReply::sslErrors, this, &Model::slotSslErrors);
outfile.setFileName(outputFilename(outputDir, it->id));
outfile.open(QIODevice::WriteOnly|QIODevice::Truncate);
it->status = ModelItem::InProgress;
emit dataChanged(index(itemsIndex,0), index(itemsIndex, columnCount() - 1));
}
void Model::readDatabase(const QString &databaseFile)
{
QFileInfo checkFile(databaseFile);
if (!checkFile.isFile()) {
emit errorMessage("Cannot open database file.");
return;
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(databaseFile);
const bool ok = db.open();
if (!ok) {
emit errorMessage("Cannot read database file.");
return;
}
QSqlQuery query;
if (!query.exec("SELECT id, vod_path FROM VideoMessages")) {
db.close();
emit errorMessage("Cannot query video path from database.");
return;
}
reset();
while (query.next()) {
const QString id = query.value(0).toString();
const QString vodPath = query.value(1).toString();
addItem(id, vodPath);
}
db.close();
}
void Model::downloadVideos()
{
itemsIndex = 0;
startItemDownloading();
}
void Model::setOutputDir(const QString &outputDir)
{
this->outputDir = outputDir;
}
void Model::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << "onDownloadProgress";
items[itemsIndex].progress = QString("%1K of %2K").arg(bytesReceived/1024).arg(bytesTotal/1024);
emit dataChanged(index(itemsIndex,0), index(itemsIndex, columnCount() - 1));
}
void Model::onReadyRead()
{
qDebug() << "onReadyRead " << reply->bytesAvailable() << reply->size() << reply->pos();
items[itemsIndex].status = ModelItem::InProgress;
outfile.write(reply->readAll());
emit dataChanged(index(itemsIndex,0), index(itemsIndex, columnCount() - 1));
}
void Model::onReplyFinished()
{
qDebug() << "onReplyFinished";
outfile.write(reply->readAll());
outfile.close();
reply->disconnect();
reply->deleteLater();
reply = NULL;
items[itemsIndex].status = ModelItem::Finished;
emit dataChanged(index(itemsIndex,0), index(itemsIndex, columnCount() - 1));
itemsIndex++;
startItemDownloading();
}
void Model::slotError(QNetworkReply::NetworkError error)
{
qDebug() << "slotError";
outfile.remove();
switch (error) {
case QNetworkReply::AuthenticationRequiredError:
items[itemsIndex].status = ModelItem::AuthFailed;
break;
default:
items[itemsIndex].status = ModelItem::Error;
}
emit dataChanged(index(itemsIndex,0), index(itemsIndex, columnCount() - 1));
reply->disconnect();
reply->deleteLater();
reply = NULL;
itemsIndex++;
startItemDownloading();
}
void Model::slotSslErrors(QList<QSslError>)
{
qDebug() << "slotSslErrors";
outfile.close();
reply->disconnect();
reply->deleteLater();
reply = NULL;
items[itemsIndex].status = ModelItem::Error;
emit dataChanged(index(itemsIndex,0), index(itemsIndex, columnCount() - 1));
itemsIndex++;
startItemDownloading();
}