下载页面雏形,修复SpkPopup(改用弹出),添加应用列表固定图标缓存

This commit is contained in:
RigoLigoRLC
2021-12-12 00:25:19 +08:00
parent f5a31affff
commit 179e57b9b5
19 changed files with 354 additions and 26 deletions

View File

@@ -1,7 +1,10 @@
#include "spkdownload.h"
#include "spkutils.h"
#include "spkui_general.h"
#include "spkpopup.h"
#include <QEventLoop>
#include <QDir>
SpkDownloadMgr::SpkDownloadMgr(QObject *parent)
{
@@ -18,6 +21,12 @@ SpkDownloadMgr::SpkDownloadMgr(QObject *parent)
mCurrentDownloadId = -1;
mActiveWorkerCount = 0;
mDownloadedBytes = 0;
mProgressEmitterTimer.setInterval(800);
connect(&mProgressEmitterTimer, &QTimer::timeout,
this, &SpkDownloadMgr::ProgressTimer);
}
SpkDownloadMgr::~SpkDownloadMgr()
@@ -139,6 +148,8 @@ bool SpkDownloadMgr::StartNewDownload(QString path, int downloadId)
i.Reply->setProperty("failCount", 0); // Used for fail retry algorithm
}
mProgressEmitterTimer.start();
return true;
}
@@ -150,7 +161,27 @@ bool SpkDownloadMgr::PauseCurrentDownload()
bool SpkDownloadMgr::CancelCurrentDownload()
{
// UNIMPLEMENTED
// Don't proceed when no downloads are there
if(mCurrentDownloadId == -1)
return false;
// Terminate all workers
for(auto &i : mScheduledWorkers)
{
auto r = i.Reply;
r->blockSignals(true);
r->abort();
r->deleteLater();
}
// Terminate and delete the temporary file
mDestFile.close();
if(!mDestFile.remove())
{
sErr(tr("SpkDownloadMgr: Cannot remove destination file %1 of a cancelled task")
.arg(mDestFile.fileName()));
SpkUi::Popup->Show(tr("The destination file of the cancelled task can't be deleted!"));
}
return false;
}
@@ -178,6 +209,9 @@ void SpkDownloadMgr::WorkerFinish()
mScheduledWorkers.clear();
mFailureRetryQueue.clear();
mCurrentDownloadId = -1;
mDownloadedBytes = 0;
mProgressEmitterTimer.stop();
}
}
else
@@ -217,6 +251,12 @@ void SpkDownloadMgr::WorkerDownloadProgress()
mDestFile.seek(worker.BeginOffset + worker.BytesRecvd);
mDestFile.write(replyData);
worker.BytesRecvd += replyData.size();
mDownloadedBytes += replyData.size();
}
void SpkDownloadMgr::ProgressTimer()
{
emit DownloadProgressed(mDownloadedBytes, mCurrentRemoteFileInfo.Size, mCurrentDownloadId);
}
void SpkDownloadMgr::LinkReplyWithMe(QNetworkReply *reply)

View File

@@ -63,6 +63,18 @@ void SpkResource::ResourceDownloaded()
ResourceResult ret;
ret.status = ResourceStatus::Ready;
if(reply->error() != QNetworkReply::NoError)
{
ret.status = ResourceStatus::Failed;
sWarn(tr("SpkResource: %1 cannot be downloaded, error code %2")
.arg(reply->url().toString())
.arg(reply->error()));
// Tell ResourceContext
if(!reply->property("outdated").toBool())
AcquisitionFinish(id, ret);
}
ret.data = reply->readAll();
// Save cache to disk
@@ -106,12 +118,13 @@ void SpkResource::Acquire(SpkPageBase *dest, bool stopOngoing, bool clearQueue)
// And abort as requested.
if(stopOngoing)
i->abort();
delete i;
mWorkingRequests.remove(i);
}
mWorkingRequests.clear();
if(stopOngoing)
{
mWorkingRequests.clear();
mRequestSemaphore->release(mMaximumConcurrent); // Release all semaphore users
}
@@ -138,7 +151,23 @@ ResourceResult SpkResource::LocateCachedResource(const ResourceTask &task)
// If there is the desired file, then we return the resource in binary
auto cacheFullPath = GetCachePath(task);
if(list.contains(SpkUtils::CutFileName(cacheFullPath)))
bool isCacheHit;
// Wildcard support
if(task.path == "*")
{
cacheFullPath.chop(1);
auto filterResult = list.filter(SpkUtils::CutFileName(cacheFullPath));
isCacheHit = !filterResult.isEmpty();
if(isCacheHit)
cacheFullPath = SpkUtils::CutPath(cacheFullPath) + '/' + filterResult[0];
}
else
{
isCacheHit = list.contains(SpkUtils::CutFileName(cacheFullPath));
}
if(isCacheHit)
{
// qInfo() << "Cache hit:" << GetCachePath(task);
QFile cacheFile(cacheFullPath);
@@ -198,7 +227,13 @@ void SpkResource::PurgeCachedResource(const QString &aPkgName, SpkResource::Reso
QString SpkResource::GetCachePath(const ResourceTask &task)
{
return mCacheDirectory + task.pkgName + '/' + ResourceName.value(task.type) + '.' +
task.info.toString() + '.' + SpkUtils::CutFileName(task.path);
task.info.toString() + '.' + SpkUtils::CutFileName(task.path);
}
ResourceResult SpkResource::CacheLookup(QString pkgName, ResourceType type, QVariant info)
{
ResourceTask task { .pkgName = pkgName, .path = "*", .type = type, .info = info, .id = -1 };
return LocateCachedResource(task);
}
const QMap<SpkResource::ResourceType, QString> SpkResource::ResourceName

View File

@@ -7,6 +7,7 @@
#include "dtk/spkdtkplugin.h"
#include "gitver.h"
#include "spkmainwindow.h"
#include "spkpopup.h"
#include "spkstore.h"
#include "spkutils.h"

View File

@@ -1,6 +1,7 @@
#include <arpa/inet.h>
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>
#include "spkutils.h"
void SpkUtils::VerifySingleRequest(QPointer<QNetworkReply> aReply)