| |
---|
| | |
---|
| | 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()); |
---|
| | } |
---|
| | |
---|
| | |
脱もっさりを目指しダウンローダ部分のQNetworkAccessManagerを別QThreadに逃がす実装。
ダウンローダの別スレッド化。
ce9c0c4
intomaster
frommultithread
on 20 May 2018