mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-09-19 01:22:20 +08:00
修正取消下载的闪退问题
This commit is contained in:
parent
815036e28f
commit
4ccc8c0dae
@ -7,6 +7,8 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QDir>
|
||||||
|
|
||||||
DownloadWorker::DownloadWorker(QObject *parent)
|
DownloadWorker::DownloadWorker(QObject *parent)
|
||||||
{
|
{
|
||||||
@ -49,19 +51,22 @@ void DownloadWorker::doWork()
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
connect(reply, &QNetworkReply::finished, mgr, &QNetworkAccessManager::deleteLater);
|
connect(reply, &QNetworkReply::finished, mgr, &QNetworkAccessManager::deleteLater);
|
||||||
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
|
||||||
connect(reply, &QNetworkReply::readyRead, this, &DownloadWorker::dataReady);
|
connect(reply, &QNetworkReply::readyRead, this, &DownloadWorker::dataReady);
|
||||||
connect(reply, &QNetworkReply::finished, this, &DownloadWorker::slotFinish);
|
connect(reply, &QNetworkReply::finished, this, &DownloadWorker::slotFinish);
|
||||||
connect(reply, &QNetworkReply::downloadProgress, this, &DownloadWorker::handleProcess);
|
connect(reply, &QNetworkReply::downloadProgress, this, &DownloadWorker::handleProcess);
|
||||||
|
// connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
||||||
|
connect(reply, &QNetworkReply::finished, this, &DownloadWorker::doStop);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWorker::doStop()
|
void DownloadWorker::doStop()
|
||||||
{
|
{
|
||||||
reply->disconnect();
|
if (reply) {
|
||||||
reply->aboutToClose();
|
reply->disconnect();
|
||||||
reply->deleteLater();
|
reply->aboutToClose();
|
||||||
reply = nullptr;
|
reply->deleteLater();
|
||||||
|
reply = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWorker::dataReady()
|
void DownloadWorker::dataReady()
|
||||||
@ -95,17 +100,19 @@ DownloadController::DownloadController(QObject *parent)
|
|||||||
"sucdn4.jerrywang.top",
|
"sucdn4.jerrywang.top",
|
||||||
"sucdn5.jerrywang.top"
|
"sucdn5.jerrywang.top"
|
||||||
};
|
};
|
||||||
this->threadNum = domains.size() > 6 ? 6 : domains.size();
|
this->threadNum = domains.size() > 4 ? 4 : domains.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
DownloadController::~DownloadController()
|
DownloadController::~DownloadController()
|
||||||
{
|
{
|
||||||
for(int i = 0; i < workers.size(); i++) {
|
if (workers.size() > 0) {
|
||||||
workers.at(i)->doStop();
|
for(int i = 0; i < workers.size(); i++) {
|
||||||
workers.at(i)->disconnect();
|
workers.at(i)->doStop();
|
||||||
workers.at(i)->deleteLater();
|
workers.at(i)->disconnect();
|
||||||
|
workers.at(i)->deleteLater();
|
||||||
|
}
|
||||||
|
workers.clear();
|
||||||
}
|
}
|
||||||
workers.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadController::setFilename(QString filename)
|
void DownloadController::setFilename(QString filename)
|
||||||
@ -123,6 +130,10 @@ void DownloadController::setThreadNum(int threadNum)
|
|||||||
*/
|
*/
|
||||||
void DownloadController::startDownload(const QString &url)
|
void DownloadController::startDownload(const QString &url)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
finish = 0;
|
||||||
|
|
||||||
// 下载任务等分,计算每个线程的下载数据
|
// 下载任务等分,计算每个线程的下载数据
|
||||||
fileSize = getFileSize(url);
|
fileSize = getFileSize(url);
|
||||||
if (fileSize == 0) {
|
if (fileSize == 0) {
|
||||||
@ -141,8 +152,10 @@ void DownloadController::startDownload(const QString &url)
|
|||||||
ranges[threadNum-1].second = fileSize; // 余数部分加入最后一个
|
ranges[threadNum-1].second = fileSize; // 余数部分加入最后一个
|
||||||
|
|
||||||
// 打开文件
|
// 打开文件
|
||||||
|
QDir tmpdir("/tmp/spark-store");
|
||||||
file = new QFile;
|
file = new QFile;
|
||||||
file->setFileName(filename);
|
file->setFileName(tmpdir.absoluteFilePath(filename));
|
||||||
|
|
||||||
if (file->exists())
|
if (file->exists())
|
||||||
file->remove();
|
file->remove();
|
||||||
if (!file->open(QIODevice::WriteOnly)) {
|
if (!file->open(QIODevice::WriteOnly)) {
|
||||||
@ -180,7 +193,9 @@ void DownloadController::stopDownload()
|
|||||||
workers.at(i)->deleteLater();
|
workers.at(i)->deleteLater();
|
||||||
}
|
}
|
||||||
workers.clear();
|
workers.clear();
|
||||||
// file->flush();
|
|
||||||
|
qDebug() << "文件下载路径:" << QFileInfo(file->fileName()).absoluteFilePath();
|
||||||
|
file->flush();
|
||||||
file->close();
|
file->close();
|
||||||
delete file;
|
delete file;
|
||||||
file = nullptr;
|
file = nullptr;
|
||||||
@ -200,17 +215,9 @@ void DownloadController::handleProcess()
|
|||||||
void DownloadController::chunkDownloadFinish()
|
void DownloadController::chunkDownloadFinish()
|
||||||
{
|
{
|
||||||
finish++;
|
finish++;
|
||||||
|
qDebug() << QString("已下载了%1块,共%2块!!!").arg(finish).arg(threadNum);
|
||||||
if (finish == threadNum) {
|
if (finish == threadNum) {
|
||||||
file->flush();
|
stopDownload();
|
||||||
file->close();
|
|
||||||
delete file;
|
|
||||||
file = nullptr;
|
|
||||||
for(int i = 0; i < workers.size(); i++) {
|
|
||||||
workers.at(i)->doStop();
|
|
||||||
workers.at(i)->disconnect();
|
|
||||||
workers.at(i)->deleteLater();
|
|
||||||
}
|
|
||||||
workers.clear();
|
|
||||||
emit downloadFinished();
|
emit downloadFinished();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -566,10 +566,6 @@ void Widget::startRequest(QUrl url, QString fileName)
|
|||||||
isdownload=true;
|
isdownload=true;
|
||||||
download_list[allDownload-1].free=false;
|
download_list[allDownload-1].free=false;
|
||||||
|
|
||||||
// reply = manager->get(QNetworkRequest(url));
|
|
||||||
// connect(reply,SIGNAL(finished()),this,SLOT(httpFinished()));
|
|
||||||
// connect(reply,SIGNAL(readyRead()),this,SLOT(httpReadyRead()));
|
|
||||||
// connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(updateDataReadProgress(qint64,qint64)));
|
|
||||||
connect(downloadController, &DownloadController::downloadProcess, this, &Widget::updateDataReadProgress);
|
connect(downloadController, &DownloadController::downloadProcess, this, &Widget::updateDataReadProgress);
|
||||||
connect(downloadController, &DownloadController::downloadFinished, this, &Widget::httpFinished);
|
connect(downloadController, &DownloadController::downloadFinished, this, &Widget::httpFinished);
|
||||||
connect(downloadController, &DownloadController::errorOccur, [this](QString msg){
|
connect(downloadController, &DownloadController::errorOccur, [this](QString msg){
|
||||||
@ -807,12 +803,6 @@ void Widget::sltAppinfoFinish()
|
|||||||
|
|
||||||
void Widget::httpFinished() // 完成下载
|
void Widget::httpFinished() // 完成下载
|
||||||
{
|
{
|
||||||
// file->flush();
|
|
||||||
// file->close();
|
|
||||||
// reply->deleteLater();
|
|
||||||
// reply = nullptr;
|
|
||||||
// delete file;
|
|
||||||
// file = nullptr;
|
|
||||||
isdownload=false;
|
isdownload=false;
|
||||||
isBusy=false;
|
isBusy=false;
|
||||||
download_list[nowDownload-1].readyInstall();
|
download_list[nowDownload-1].readyInstall();
|
||||||
@ -823,13 +813,6 @@ void Widget::httpFinished() // 完成下载
|
|||||||
nowDownload+=1;
|
nowDownload+=1;
|
||||||
}
|
}
|
||||||
QString fileName=download_list[nowDownload-1].getName();
|
QString fileName=download_list[nowDownload-1].getName();
|
||||||
// file = new QFile(fileName);
|
|
||||||
// if(!file->open(QIODevice::WriteOnly))
|
|
||||||
// {
|
|
||||||
// delete file;
|
|
||||||
// file = nullptr;
|
|
||||||
// return ;
|
|
||||||
// }
|
|
||||||
startRequest(urList.at(nowDownload-1), fileName);
|
startRequest(urList.at(nowDownload-1), fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ void SpkAppInfoLoaderThread::run()
|
|||||||
httpClient->get(targetUrl.toString())
|
httpClient->get(targetUrl.toString())
|
||||||
.header("content-type", "application/json")
|
.header("content-type", "application/json")
|
||||||
.onResponse([this](QByteArray json_array) {
|
.onResponse([this](QByteArray json_array) {
|
||||||
|
qDebug() << "请求应用信息 " << json_array;
|
||||||
QString urladdress, deatils, more, packagename, appweb;
|
QString urladdress, deatils, more, packagename, appweb;
|
||||||
bool isInstalled;
|
bool isInstalled;
|
||||||
|
|
||||||
@ -83,7 +84,8 @@ void SpkAppInfoLoaderThread::run()
|
|||||||
.onError([this](QString errorStr) {
|
.onError([this](QString errorStr) {
|
||||||
Widget::sendNotification(tr("Failed to load application icon."));
|
Widget::sendNotification(tr("Failed to load application icon."));
|
||||||
})
|
})
|
||||||
.timeout(10 * 100)
|
.block()
|
||||||
|
.timeout(5 * 100)
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
|
|
||||||
@ -103,27 +105,23 @@ void SpkAppInfoLoaderThread::run()
|
|||||||
qDebug() << "截图下载失败";
|
qDebug() << "截图下载失败";
|
||||||
// Widget::sendNotification(tr("Failed to load application screenshot."));
|
// Widget::sendNotification(tr("Failed to load application screenshot."));
|
||||||
})
|
})
|
||||||
.timeout(10 * 100)
|
.block()
|
||||||
|
.timeout(4 * 100)
|
||||||
.exec();
|
.exec();
|
||||||
}
|
}
|
||||||
emit finishAllLoading();
|
emit finishAllLoading();
|
||||||
|
|
||||||
|
httpClient->deleteLater();
|
||||||
})
|
})
|
||||||
.onError([](QString errorStr) {
|
.onError([](QString errorStr) {
|
||||||
Widget::sendNotification(tr("Failed to download app info. Please check internet connection."));
|
Widget::sendNotification(tr("Failed to download app info. Please check internet connection."));
|
||||||
})
|
})
|
||||||
.timeout(10 * 100)
|
.timeout(5 * 100)
|
||||||
|
.block()
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SpkAppInfoLoaderThread::~SpkAppInfoLoaderThread()
|
|
||||||
{
|
|
||||||
if (httpClient) {
|
|
||||||
httpClient->deleteLater();
|
|
||||||
httpClient = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpkAppInfoLoaderThread::setUrl(const QUrl &url)
|
void SpkAppInfoLoaderThread::setUrl(const QUrl &url)
|
||||||
{
|
{
|
||||||
|
@ -17,7 +17,6 @@ class SpkAppInfoLoaderThread Q_DECL_FINAL : public QThread
|
|||||||
public:
|
public:
|
||||||
//explicit SpkAppInfoLoaderThread() = default;
|
//explicit SpkAppInfoLoaderThread() = default;
|
||||||
void run() Q_DECL_OVERRIDE;
|
void run() Q_DECL_OVERRIDE;
|
||||||
~SpkAppInfoLoaderThread();
|
|
||||||
public slots:
|
public slots:
|
||||||
void setUrl(const QUrl &url);
|
void setUrl(const QUrl &url);
|
||||||
void setServer(const QString &server);
|
void setServer(const QString &server);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user