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