diff --git a/appmanagerjob.cpp b/appmanagerjob.cpp index 74da7e40405bd128bd6211ae4d915fce284b6a9b..c083e823c123a535f5f3bb710d6f43d8ef678d6b 100644 --- a/appmanagerjob.cpp +++ b/appmanagerjob.cpp @@ -203,31 +203,33 @@ void AppManagerJob::downloadPkg(const QString &pkgName) Q_EMIT downloadPkgFinished(pkgName); } +void AppManagerJob::downloadPkgFile(const PkgInfo &info) +{ + m_downloadingPkgInfo = info; + downloadFile(info.downloadUrl); +} + void AppManagerJob::downloadFile(const QString &url) { if (!m_isInitiallized) { init(); } - m_downloadingFileOriginUrl = url; QString realUrl = url; // 获取文件大小 qint64 fileSize = getUrlFileSize(realUrl); qInfo() << Q_FUNC_INFO << fileSize; + if (0 > fileSize) { + Q_EMIT pkgFileDownloadFailed(m_downloadingPkgInfo); + return; + } qint64 endOffset = fileSize; // 创建下载路径 - QFileInfo info(realUrl); - QString fileName = info.fileName(); - - if (!fileName.contains(".")) { - QFileInfo originUrlInfo(url); - fileName = originUrlInfo.fileName(); - } - - if (fileName.isEmpty()) { - fileName = "index.html"; - } + QString fileName = QString("%1_%2_%3.deb") + .arg(m_downloadingPkgInfo.pkgName) + .arg(m_downloadingPkgInfo.version) + .arg(m_downloadingPkgInfo.arch); QDir downloadDir(m_downloadDirPath); if (!downloadDir.exists()) { @@ -270,7 +272,7 @@ void AppManagerJob::onHttpReadyRead() void AppManagerJob::onDownloadProgressChanged(qint64 bytesRead, qint64 totalBytes) { - Q_EMIT fileDownloadProgressChanged(m_downloadingFileOriginUrl, bytesRead, totalBytes); + Q_EMIT pkgFileDownloadProgressChanged(m_downloadingPkgInfo, bytesRead, totalBytes); } void AppManagerJob::onFileDownloadFinished() @@ -284,7 +286,7 @@ void AppManagerJob::onFileDownloadFinished() m_downloadingFile->deleteLater(); m_downloadingFile = nullptr; - Q_EMIT fileDownloadFinished(m_downloadingFileOriginUrl); + Q_EMIT pkgFileDownloadFinished(m_downloadingPkgInfo); } void AppManagerJob::startBuildPkgTask(const AppInfo &info) diff --git a/appmanagerjob.h b/appmanagerjob.h index 52a494e0f26cba1d4ba1e701c8e81888e08424f4..60b2aaf7cb9f5cd4a49534e69436b3a8508994ec 100644 --- a/appmanagerjob.h +++ b/appmanagerjob.h @@ -41,6 +41,7 @@ public Q_SLOTS: void init(); void reloadAppInfos(); void downloadPkg(const QString &pkgName); + void downloadPkgFile(const PkgInfo &info); void downloadFile(const QString &url); void onHttpReadyRead(); void onDownloadProgressChanged(qint64, qint64); @@ -65,8 +66,9 @@ Q_SIGNALS: void runningStatusChanged(RunningStatus status); void loadAppInfosFinished(); void downloadPkgFinished(const QString &pkgName); - void fileDownloadProgressChanged(const QString &url, qint64 bytesRead, qint64 totalBytes); - void fileDownloadFinished(const QString &url); + void pkgFileDownloadProgressChanged(const PkgInfo &info, qint64 bytesRead, qint64 totalBytes); + void pkgFileDownloadFinished(const PkgInfo &info); + void pkgFileDownloadFailed(const PkgInfo &info); // 搜索任务完成 void searchTaskFinished(); @@ -121,7 +123,7 @@ private: QFile *m_downloadingFile; QNetworkAccessManager *m_netManager; QNetworkReply *m_netReply; - QString m_downloadingFileOriginUrl; + PkgInfo m_downloadingPkgInfo; QList m_searchedAppInfoList; // deb构建缓存目录 diff --git a/appmanagermodel.cpp b/appmanagermodel.cpp index 330c3e3bec81209857793530ab1f8d357c38cd7f..5f73f98bc6cb404b69d569d57590ad8be68e88a8 100644 --- a/appmanagermodel.cpp +++ b/appmanagermodel.cpp @@ -229,6 +229,10 @@ void AppManagerModel::initData() qRegisterMetaType>("QList"); qRegisterMetaType("AM::RunningStatus"); qRegisterMetaType("RunningStatus"); + qRegisterMetaType("PkgInfo"); + qRegisterMetaType("AM::PkgInfo"); + qRegisterMetaType>("QList"); + qRegisterMetaType>("QList"); // 线程 m_appManagerJobThread = new QThread; @@ -243,13 +247,14 @@ void AppManagerModel::initConnection() connect(m_appManagerJobThread, &QThread::started, m_appManagerJob, &AppManagerJob::init); connect(this, &AppManagerModel::notifyThreadreloadAppInfos, m_appManagerJob, &AppManagerJob::reloadAppInfos); - connect(this, &AppManagerModel::notifyThreadDownloadFile, m_appManagerJob, &AppManagerJob::downloadFile); - connect(m_appManagerJob, &AppManagerJob::fileDownloadProgressChanged, this, [this](const QString &url, qint64 bytesRead, qint64 totalBytes) { - Q_EMIT this->fileDownloadProgressChanged(url, bytesRead, totalBytes); + connect(this, &AppManagerModel::notifyThreadDownloadPkgFile, m_appManagerJob, &AppManagerJob::downloadPkgFile); + connect(m_appManagerJob, &AppManagerJob::pkgFileDownloadProgressChanged, this, [this](const PkgInfo &info, qint64 bytesRead, qint64 totalBytes) { + Q_EMIT this->pkgFileDownloadProgressChanged(info, bytesRead, totalBytes); }); - connect(m_appManagerJob, &AppManagerJob::fileDownloadFinished, this, [this](const QString &url) { - Q_EMIT this->fileDownloadFinished(url); + connect(m_appManagerJob, &AppManagerJob::pkgFileDownloadFinished, this, [this](const PkgInfo &info) { + Q_EMIT this->pkgFileDownloadFinished(info); }); + connect(m_appManagerJob, &AppManagerJob::pkgFileDownloadFailed, this, &AppManagerModel::pkgFileDownloadFailed); connect(m_appManagerJob, &AppManagerJob::loadAppInfosFinished, this, [this] { Q_EMIT this->loadAppInfosFinished(); }); diff --git a/appmanagermodel.h b/appmanagermodel.h index 9f9a401288be28d94852b8a5e750a893ffac93d2..c58e71bcfdf3122851a45163dc4c337681db661b 100644 --- a/appmanagermodel.h +++ b/appmanagermodel.h @@ -41,9 +41,10 @@ Q_SIGNALS: void notifyThreadreloadAppInfos(); void loadAppInfosFinished(); - void notifyThreadDownloadFile(const QString &url); - void fileDownloadProgressChanged(const QString &url, qint64 bytesRead, qint64 totalBytes); - void fileDownloadFinished(const QString &url); + void notifyThreadDownloadPkgFile(const PkgInfo &info); + void pkgFileDownloadProgressChanged(const PkgInfo &info, qint64 bytesRead, qint64 totalBytes); + void pkgFileDownloadFinished(const PkgInfo &info); + void pkgFileDownloadFailed(const PkgInfo &info); void notifyThreadStartSearchTask(const QString &text); // 搜索任务完成 void searchTaskFinished(); diff --git a/mainwindow.cpp b/mainwindow.cpp index ee58c4335a9e8be0869e2c55f21c9dc7866bbc90..9b36c0bc00f10cbe74f0b474cd541467d69ace38 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -139,6 +139,20 @@ MainWindow::MainWindow(QWidget *parent) } } }); + // 下载失败 + connect(m_appManagerModel, &AppManagerModel::pkgFileDownloadFailed, this, [this](const PkgInfo &info) { + qInfo() << Q_FUNC_INFO << info.downloadUrl << "download failed!"; + DDialog *dlg = new DDialog(this); + QString tip = QString("下载失败,请尝试在终端使用apt download %1命令下载").arg(info.pkgName); + dlg->setMessage("下载失败,请尝试在终端使用以下命令下载"); + QTextEdit *cmdEdit = new QTextEdit(this); + cmdEdit->setText(QString("apt download %1").arg(info.pkgName)); + cmdEdit->setReadOnly(true); + dlg->addContent(cmdEdit); + + dlg->exec(); + dlg->deleteLater(); + }); // post init if (m_isDeepin) { diff --git a/pkgdownloaddlg.cpp b/pkgdownloaddlg.cpp index 41c8d2f42235c32c7d1e886a6ae246e3340359f8..4e3e1b6f66448c21177aaff3a0eed9a90355598e 100644 --- a/pkgdownloaddlg.cpp +++ b/pkgdownloaddlg.cpp @@ -208,9 +208,9 @@ void PkgDownloadDlg::onVerSelectMenuTrigered(QAction *trigeredAction) m_infoEdit->setText(infoText); } -void PkgDownloadDlg::onFileDownloadProgressChanged(const QString &url, qint64 bytesRead, qint64 totalBytes) +void PkgDownloadDlg::onFileDownloadProgressChanged(const AM::PkgInfo &info, qint64 bytesRead, qint64 totalBytes) { - if (m_selectedPkgInfo.downloadUrl != url) { + if (m_selectedPkgInfo.pkgName != info.pkgName) { return; } @@ -224,7 +224,7 @@ void PkgDownloadDlg::initConnection() { connect(m_versionSelectMenu, &QMenu::triggered, this, &PkgDownloadDlg::onVerSelectMenuTrigered); connect(m_downloadBtn, &QPushButton::clicked, this, [this](bool) { - this->m_model->notifyThreadDownloadFile(m_selectedPkgInfo.downloadUrl); + this->m_model->notifyThreadDownloadPkgFile(m_selectedPkgInfo); this->m_isDownloading = true; this->updateUI(); }); @@ -234,9 +234,14 @@ void PkgDownloadDlg::initConnection() QDesktopServices::openUrl(m_model->getDownloadDirPath()); }); - connect(m_model, &AppManagerModel::fileDownloadProgressChanged, this, &PkgDownloadDlg::onFileDownloadProgressChanged); - connect(m_model, &AppManagerModel::fileDownloadFinished, this, [this](const QString &url) { - qInfo() << Q_FUNC_INFO << url << "downloaded"; + connect(m_model, &AppManagerModel::pkgFileDownloadProgressChanged, this, &PkgDownloadDlg::onFileDownloadProgressChanged); + connect(m_model, &AppManagerModel::pkgFileDownloadFinished, this, [this](const AM::PkgInfo &info) { + qInfo() << Q_FUNC_INFO << info.downloadUrl << "downloaded"; + this->m_isDownloading = false; + this->updateUI(); + }); + connect(m_model, &AppManagerModel::pkgFileDownloadFailed, this, [this](const AM::PkgInfo &info) { + qInfo() << Q_FUNC_INFO << info.downloadUrl << "download failed!"; this->m_isDownloading = false; this->updateUI(); }); diff --git a/pkgdownloaddlg.h b/pkgdownloaddlg.h index ed8cfb03e70f1648d1fa2a68330325f5e9b4c660..b6192d5c5758f96651c8bc2406cb4c17ebfa449a 100644 --- a/pkgdownloaddlg.h +++ b/pkgdownloaddlg.h @@ -32,7 +32,7 @@ protected: public Q_SLOTS: void onVerSelectMenuTrigered(QAction *action); - void onFileDownloadProgressChanged(const QString &url, qint64 bytesRead, qint64 totalBytes); + void onFileDownloadProgressChanged(const AM::PkgInfo &info, qint64 bytesRead, qint64 totalBytes); private: void initConnection();