ダウンローダの別スレッド化。 #32

Merged tamra merged 1 commit into tamra:master from tamra:multithread on 20 May 2018
Showing 5 changed files
View
8
saori.pro
saoriapplication.cpp \
saoricache.cpp \
saoriviewentry.cpp \
saoriconfiguredialog.cpp \
saoritootwidget.cpp
saoritootwidget.cpp \
saoridownloader.cpp
 
HEADERS += \
saoriwindow.h \
saoridon.h \
saoriapplication.h \
saoricache.h \
saoriviewentry.h \
saoriconfiguredialog.h \
saoritootwidget.h
saoritootwidget.h \
saoridownloader.h
 
FORMS += \
saoriwindow.ui \
saoriview.ui \
View
109
saoricache.cpp
 
SaoriCache::SaoriCache(QObject *parent) : QObject(parent)
{
migration();
m_thread = new QThread;
m_downloader = new SaoriDownloader;
m_downloader->moveToThread(m_thread);
connect(this,&SaoriCache::initDownloader,m_downloader,&SaoriDownloader::initialize,Qt::QueuedConnection);
connect(this,&SaoriCache::get,m_downloader,&SaoriDownloader::get,Qt::QueuedConnection);
connect(m_downloader,&SaoriDownloader::downloaded,this,&SaoriCache::downloadFinished,Qt::QueuedConnection);
connect(m_downloader,&SaoriDownloader::error,this,&SaoriCache::networkError,Qt::QueuedConnection);
m_thread->start();
emit initDownloader();
}
 
SaoriCache::~SaoriCache()
{
m_thread->exit();
}
 
void SaoriCache::removeFileCache(const QUrl url)
{
if (isCached(url)) return;
m_nowloading.append(url);
QNetworkRequest request;
request.setUrl(url);
auto *reply = SaoriApplication::saori()->manager->get(request);
connect(reply,&QNetworkReply::finished,this,[=](){
if (reply->error() == QNetworkReply::NoError) {
QFile localfile(urlToFilename(url));
if (localfile.exists()) localfile.remove();
localfile.open(QIODevice::WriteOnly);
localfile.write(reply->readAll());
// TODO 100MBのファイルに100MBのメモリを使うのは如何なものか。
qint64 size = localfile.size();
localfile.close();
QSqlQuery query(*SaoriApplication::saori()->database());
query.prepare("INSERT INTO file_cache(url,filename,size,timestamp) values(?,?,?,?);");
query.addBindValue(url.toString());
query.addBindValue(urlToFilename(url));
query.addBindValue(size);
query.addBindValue(QDateTime::currentSecsSinceEpoch());
query.exec();
emit downloaded(url);
} else {
QSqlQuery query(*SaoriApplication::saori()->database());
query.prepare("INSERT INTO file_cache(url,filename,size,timestamp) values(?,?,?,?);");
query.addBindValue(url.toString());
query.addBindValue(":/icons/ionicons-md/md-alert.svg");
query.addBindValue("-1");
query.addBindValue(QDateTime::currentSecsSinceEpoch());
query.exec();
emit downloaded(url);
}
m_nowloading.removeAll(url);
reply->deleteLater();
});
emit get(request);
}
 
bool SaoriCache::isCached(const QUrl url)
{
d.mkpath(SaoriApplication::saori()->cacheDirectory().absolutePath() + "/" + dir);
return QString(SaoriApplication::saori()->cacheDirectory().absolutePath() + "/" + dir + "/" + file);
}
 
void SaoriCache::downloadFinished(QNetworkRequest req, QByteArray data)
{
QFile localfile(urlToFilename(req.url()));
if (localfile.exists()) localfile.remove();
localfile.open(QIODevice::WriteOnly);
localfile.write(data);
// TODO 100MBのファイルに100MBのメモリを使うのは如何なものか。
qint64 size = localfile.size();
localfile.close();
QSqlQuery query(*SaoriApplication::saori()->database());
query.prepare("INSERT INTO file_cache(url,filename,size,timestamp) values(?,?,?,?);");
query.addBindValue(req.url().toString());
query.addBindValue(urlToFilename(req.url()));
query.addBindValue(size);
query.addBindValue(QDateTime::currentSecsSinceEpoch());
query.exec();
emit downloaded(req.url());
m_nowloading.removeAll(req.url());
}
 
void SaoriCache::networkError(QNetworkRequest req, QNetworkReply::NetworkError error)
{
if (error == QNetworkReply::NoError) return;
QSqlQuery query(*SaoriApplication::saori()->database());
query.prepare("INSERT INTO file_cache(url,filename,size,timestamp) values(?,?,?,?);");
query.addBindValue(req.url().toString());
query.addBindValue(":/icons/ionicons-md/md-alert.svg");
query.addBindValue("-1");
query.addBindValue(QDateTime::currentSecsSinceEpoch());
query.exec();
emit downloaded(req.url());
}
View
saoricache.h
View
saoridownloader.cpp 0 → 100644
View
saoridownloader.h 0 → 100644