mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-09-10 21:32:20 +08:00
chore:更新代码
This commit is contained in:
parent
90b21d40cf
commit
f7baf015c0
@ -1,8 +1,7 @@
|
||||
#include "downloadmanager.h"
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
#include <QFileInfo>
|
||||
#include <QRegularExpression>
|
||||
#include <QFileInfo> // 添加 QFileInfo 头文件
|
||||
#include <QDebug>
|
||||
|
||||
DownloadManager::DownloadManager(QObject *parent) : QObject(parent) {}
|
||||
|
||||
@ -10,22 +9,22 @@ void DownloadManager::startDownload(const QString &packageName, const QString &u
|
||||
{
|
||||
QString metalinkUrl = url + ".metalink";
|
||||
QStringList arguments;
|
||||
arguments << "--enable-rpc=false"
|
||||
arguments << "--enable-rpc=false"
|
||||
<< "--console-log-level=warn"
|
||||
<< "--summary-interval=1"
|
||||
<< "--summary-interval=1"
|
||||
<< "--dir=" + QFileInfo(outputPath).absolutePath()
|
||||
<< "--out=" + QFileInfo(outputPath).fileName()
|
||||
<< "--out=" + QFileInfo(outputPath).fileName()
|
||||
<< metalinkUrl;
|
||||
|
||||
QProcess* process = new QProcess(this);
|
||||
QProcess *process = new QProcess(this);
|
||||
m_processes.insert(packageName, process);
|
||||
|
||||
// ✅ 改为按行读取(逐行处理)
|
||||
connect(process, &QProcess::readyReadStandardOutput, [this, process, packageName]() {
|
||||
QString output = process->readAllStandardOutput();
|
||||
QRegularExpression regex(R"(\((\d+)%\))");
|
||||
QRegularExpressionMatchIterator i = regex.globalMatch(output);
|
||||
while (i.hasNext()) {
|
||||
QRegularExpressionMatch match = i.next();
|
||||
while (process->canReadLine()) {
|
||||
QString line = process->readLine().trimmed();
|
||||
QRegularExpression regex(R"(\((\d+)%\))");
|
||||
QRegularExpressionMatch match = regex.match(line);
|
||||
if (match.hasMatch()) {
|
||||
int progress = match.captured(1).toInt();
|
||||
emit downloadProgress(packageName, progress);
|
||||
@ -34,12 +33,13 @@ void DownloadManager::startDownload(const QString &packageName, const QString &u
|
||||
}
|
||||
});
|
||||
|
||||
// 下载完成
|
||||
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
||||
[this, packageName](int exitCode, QProcess::ExitStatus exitStatus) {
|
||||
bool success = (exitCode == 0 && exitStatus == QProcess::NormalExit);
|
||||
emit downloadFinished(packageName, success);
|
||||
m_processes.remove(packageName);
|
||||
});
|
||||
[this, packageName](int exitCode, QProcess::ExitStatus exitStatus) {
|
||||
bool success = (exitCode == 0 && exitStatus == QProcess::NormalExit);
|
||||
emit downloadFinished(packageName, success);
|
||||
m_processes.remove(packageName);
|
||||
});
|
||||
|
||||
process->start("aria2c", arguments);
|
||||
}
|
||||
@ -47,7 +47,7 @@ void DownloadManager::startDownload(const QString &packageName, const QString &u
|
||||
void DownloadManager::cancelDownload(const QString &packageName)
|
||||
{
|
||||
if (m_processes.contains(packageName)) {
|
||||
QProcess* process = m_processes[packageName];
|
||||
QProcess *process = m_processes[packageName];
|
||||
process->terminate();
|
||||
process->waitForFinished();
|
||||
m_processes.remove(packageName);
|
||||
|
@ -2,25 +2,24 @@
|
||||
#define DOWNLOADMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QProcess>
|
||||
#include <QMap>
|
||||
|
||||
class DownloadManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DownloadManager(QObject *parent = nullptr);
|
||||
void startDownload(const QString &packageName, const QString &url, const QString &outputPath); // 修改参数列表
|
||||
void cancelDownload(const QString &packageName); // 移动到public区域
|
||||
void startDownload(const QString &packageName, const QString &url, const QString &outputPath);
|
||||
void cancelDownload(const QString &packageName);
|
||||
|
||||
signals:
|
||||
void downloadProgress(const QString &packageName, int progress);
|
||||
void downloadFinished(const QString &packageName, bool success);
|
||||
|
||||
private:
|
||||
QHash<QString, QProcess*> m_processes; // 移除旧的m_aria2Process
|
||||
// 移除旧的onAria2Progress和onAria2Finished声明
|
||||
QMap<QString, QProcess*> m_processes;
|
||||
};
|
||||
|
||||
#endif // DOWNLOADMANAGER_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user