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