chore:aria2下载包

This commit is contained in:
momen 2025-06-11 23:11:29 +08:00
parent a2aadf44e3
commit f071fcb00d
3 changed files with 46 additions and 63 deletions

View File

@ -129,8 +129,6 @@ bool AppDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QS
// 取消按钮区域
QRect cancelButtonRect(rect.right() - 70, rect.top() + (rect.height() - 20) / 2, 60, 20);
if (cancelButtonRect.contains(mouseEvent->pos())) {
// 修正方法调用,假设 DownloadManager 有 cancelDownload 方法
m_downloadManager->cancelDownload();
m_isDownloading = false;
emit updateDisplay(); // 触发重绘
return true;
@ -141,19 +139,26 @@ bool AppDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QS
if (buttonRect.contains(mouseEvent->pos())) {
QString packageName = index.data(Qt::UserRole + 1).toString();
QString downloadUrl = index.data(Qt::UserRole + 7).toString();
qDebug() << "从模型中获取的包名:" << packageName;
qDebug() << "从模型中获取的下载 URL:" << downloadUrl; // 检查模型中是否正确传递 URL
if (downloadUrl.isEmpty()) {
qWarning() << "下载 URL 为空,无法开始下载,包名:" << packageName;
return false;
}
QString outputPath = QString("%1/%2.metalink").arg(QDir::tempPath(), packageName);
m_isDownloading = true;
m_progress = 0;
connect(m_downloadManager, &DownloadManager::downloadProgress, this, [this](int progress) {
m_progress = progress;
emit updateDisplay(); // 更新界面显示
});
connect(m_downloadManager, &DownloadManager::downloadFinished, this, [this](bool success) {
m_isDownloading = false;
emit updateDisplay(); // 更新界面显示
if (success) {
qDebug() << "下载完成";
} else {
qDebug() << "下载失败";
}
});
m_downloadManager->startDownload(downloadUrl, outputPath);
emit updateDisplay(); // 触发重绘
return true;

View File

@ -1,68 +1,47 @@
#include "downloadmanager.h"
#include <QFile>
#include <QDebug>
#include <QRegularExpression>
#include <QFileInfo> // 添加 QFileInfo 头文件
DownloadManager::DownloadManager(QObject *parent) : QObject(parent) {}
void DownloadManager::startDownload(const QString &url, const QString &outputPath)
{
qDebug() << "接收到的下载 URL:" << url; // 检查 URL 是否正确传递
QString metalinkUrl = url + ".metalink"; // 构造 Metalink URL
qDebug() << "开始下载 Metalink 文件:" << metalinkUrl;
// 验证 URL 是否包含协议
QUrl metalinkUrl(url + ".metalink");
if (!metalinkUrl.isValid() || metalinkUrl.scheme().isEmpty()) {
qWarning() << "无效的 URL:" << metalinkUrl.toString();
emit downloadFinished(false);
return;
}
QStringList arguments;
arguments << "--enable-rpc=false" << "--console-log-level=warn"
<< "--summary-interval=1" << "--dir=" + QFileInfo(outputPath).absolutePath()
<< "--out=" + QFileInfo(outputPath).fileName() << metalinkUrl;
QNetworkRequest request(metalinkUrl);
m_reply = m_networkManager.get(request);
connect(&m_aria2Process, &QProcess::readyReadStandardOutput, this, &DownloadManager::onAria2Progress);
connect(&m_aria2Process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &DownloadManager::onAria2Finished);
connect(m_reply, &QNetworkReply::downloadProgress, this, &DownloadManager::onDownloadProgress);
connect(m_reply, &QNetworkReply::finished, this, &DownloadManager::onDownloadFinished);
// 保存路径
m_reply->setProperty("outputPath", outputPath);
m_aria2Process.start("aria2c", arguments);
}
void DownloadManager::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void DownloadManager::onAria2Progress()
{
if (bytesTotal > 0) {
int progress = static_cast<int>((bytesReceived * 100) / bytesTotal);
emit downloadProgress(progress);
QString output = m_aria2Process.readAllStandardOutput();
QRegularExpression regex(R"(Download Progress: (\d+)%.*)");
QRegularExpressionMatch match = regex.match(output);
if (match.hasMatch()) {
int progress = match.captured(1).toInt();
emit downloadProgress(progress); // 发送进度信号
qDebug() << "下载进度:" << progress << "%";
}
}
void DownloadManager::onDownloadFinished()
void DownloadManager::onAria2Finished(int exitCode, QProcess::ExitStatus exitStatus)
{
if (m_reply->error() == QNetworkReply::NoError) {
QString outputPath = m_reply->property("outputPath").toString();
QFile file(outputPath);
if (file.open(QIODevice::WriteOnly)) {
file.write(m_reply->readAll());
file.close();
qDebug() << "文件已保存到路径:" << outputPath; // 输出保存路径
emit downloadFinished(true);
} else {
qWarning() << "无法保存文件到路径:" << outputPath;
emit downloadFinished(false);
}
if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
qDebug() << "下载完成";
emit downloadFinished(true); // 发送完成信号
} else {
qWarning() << "下载失败:" << m_reply->errorString();
emit downloadFinished(false);
}
m_reply->deleteLater();
m_reply = nullptr;
}
void DownloadManager::cancelDownload()
{
if (m_reply) {
m_reply->abort(); // 取消当前下载
m_reply->deleteLater();
m_reply = nullptr;
emit downloadFinished(false); // 发送下载失败信号
qWarning() << "下载失败,退出代码:" << exitCode;
emit downloadFinished(false); // 发送失败信号
}
}

View File

@ -4,6 +4,7 @@
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QProcess>
class DownloadManager : public QObject
{
@ -11,19 +12,17 @@ class DownloadManager : public QObject
public:
explicit DownloadManager(QObject *parent = nullptr);
void startDownload(const QString &url, const QString &outputPath);
void cancelDownload(); // 添加取消下载的方法
signals:
void downloadProgress(int progress); // 下载进度信号
void downloadFinished(bool success); // 下载完成信号
private slots:
void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void onDownloadFinished();
void onAria2Progress(); // 处理 aria2 的进度
void onAria2Finished(int exitCode, QProcess::ExitStatus exitStatus); // 处理 aria2 完成事件
private:
QNetworkAccessManager m_networkManager;
QNetworkReply *m_reply = nullptr;
QProcess m_aria2Process; // 用于运行 aria2 的进程
};
#endif // DOWNLOADMANAGER_H