重写下载按钮逻辑

This commit is contained in:
2022-12-13 20:32:39 +08:00
parent 922cb6a34f
commit 83ee212484
6 changed files with 52 additions and 47 deletions

View File

@@ -51,7 +51,7 @@ void AppIntoPage::setDownloadWidget(DownloadListWidget *w)
{ {
dw=w; dw=w;
connect(w, &DownloadListWidget::downloadFinished, [=]() { connect(w, &DownloadListWidget::downloadFinished, [=]() {
isDownloading(); isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
}); });
} }
void AppIntoPage::openUrl(QUrl url) void AppIntoPage::openUrl(QUrl url)
@@ -144,7 +144,7 @@ void AppIntoPage::openUrl(QUrl url)
else else
{ {
ui->downloadButton->setText(tr("Download")); ui->downloadButton->setText(tr("Download"));
isDownloading(); isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
ui->downloadButton->setEnabled(true); ui->downloadButton->setEnabled(true);
ui->downloadButton->show(); ui->downloadButton->show();
} }
@@ -193,24 +193,36 @@ void AppIntoPage::openUrl(QUrl url)
api1->getAppDownloadTimes(url); api1->getAppDownloadTimes(url);
} }
void AppIntoPage::isDownloading() void AppIntoPage::isDownloading(QUrl url)
{ {
switch (dw->isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString())) { if(dw->getUrlList().lastIndexOf(url) == -1)
case 3:{ {
ui->downloadButton->setEnabled(true); ui->downloadButton->setEnabled(true);
ui->downloadButton->setText(tr("Download")); return;
break; }else{
} ui->downloadButton->setEnabled(false);
case 1:{ }
ui->downloadButton->setEnabled(false); if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->download == 2)
ui->downloadButton->setText(tr("Downloading")); {
break; ui->downloadButton->setEnabled(true);
} ui->downloadButton->setText(tr("Download"));
case 2:{ }
ui->downloadButton->setEnabled(false); if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->download == 1)
ui->downloadButton->setText(tr("Downloaded")); {
break; ui->downloadButton->setEnabled(true);
} ui->downloadButton->setText(tr("Install"));
}
if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->isInstall)
{
ui->downloadButton->setEnabled(false);
ui->downloadButton->setText(tr("Installing"));
}
if(dw->getDIList()[dw->getUrlList().lastIndexOf(url)]->download == 3)
{
ui->downloadButton->setEnabled(true);
ui->downloadButton->setText(tr("Reinstall"));
ui->downloadButton->show();
ui->pushButton_3->show();
} }
} }
@@ -292,13 +304,20 @@ AppIntoPage::~AppIntoPage()
void AppIntoPage::on_downloadButton_clicked() void AppIntoPage::on_downloadButton_clicked()
{ {
if(ui->downloadButton->text() == tr("Install"))
{
dw->getDIList()[dw->getUrlList().lastIndexOf(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString())]->install(0);
isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
return;
}
emit clickedDownloadBtn(); emit clickedDownloadBtn();
dw->addItem(info["Name"].toString(),info["Filename"].toString(),info["Pkgname"].toString(),iconpixmap,SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString()); dw->addItem(info["Name"].toString(),info["Filename"].toString(),info["Pkgname"].toString(),iconpixmap,SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
if(ui->downloadButton->text() == tr("Reinstall")) if(ui->downloadButton->text() == tr("Reinstall"))
{ {
dw->getDIList()[dw->allDownload - 1]->reinstall = true; dw->getDIList()[dw->allDownload - 1]->reinstall = true;
} }
isDownloading(); ui->downloadButton->setEnabled(false);
isDownloading(SparkAPI::getServerUrl()+"store"+spk.path()+"/"+info["Filename"].toString());
} }
void AppIntoPage::on_pushButton_3_clicked() void AppIntoPage::on_pushButton_3_clicked()

View File

@@ -40,7 +40,7 @@ private:
QJsonObject info; QJsonObject info;
QPixmap iconpixmap; QPixmap iconpixmap;
QUrl spk; QUrl spk;
void isDownloading(); void isDownloading(QUrl url);
void sltAppinfoTags(QStringList *tagList); void sltAppinfoTags(QStringList *tagList);
DownloadListWidget *dw; DownloadListWidget *dw;
Ui::AppIntoPage *ui; Ui::AppIntoPage *ui;

View File

@@ -36,6 +36,7 @@ void DownloadItem::setValue(qint64 value)
ui->label_2->setText(QString::number(double(value) / 100) + "% (" + speed + ")"); ui->label_2->setText(QString::number(double(value) / 100) + "% (" + speed + ")");
if(ui->label_2->text().left(4) == "100%") if(ui->label_2->text().left(4) == "100%")
{ {
download = 1;
ui->label_2->setText(tr("Download Complete.")); ui->label_2->setText(tr("Download Complete."));
} }
} }
@@ -140,12 +141,14 @@ void DownloadItem::install(int t)
ui->pushButton_install->hide(); ui->pushButton_install->hide();
Utils::sendNotification("spark-store",tr("Spark Store"),ui->label->text() + " " + tr("Installation complete.")); Utils::sendNotification("spark-store",tr("Spark Store"),ui->label->text() + " " + tr("Installation complete."));
ui->label_2->setText(tr("Finish")); ui->label_2->setText(tr("Finish"));
download = 3;
ui->pushButton_3->show(); ui->pushButton_3->show();
} }
else else
{ {
ui->pushButton_install->show(); ui->pushButton_install->show();
ui->pushButton_install->setText(tr("Retry")); ui->pushButton_install->setText(tr("Retry"));
download = 1;
Utils::sendNotification("spark-store",tr("Spark Store"),tr("Error happened in dpkg progress , you can try it again.")); Utils::sendNotification("spark-store",tr("Spark Store"),tr("Error happened in dpkg progress , you can try it again."));
ui->label_2->setText(tr("Error happened in dpkg progress , you can try it again")); ui->label_2->setText(tr("Error happened in dpkg progress , you can try it again"));
ui->pushButton_3->show(); ui->pushButton_3->show();
@@ -176,6 +179,7 @@ void DownloadItem::on_pushButton_install_clicked()
void DownloadItem::on_pushButton_2_clicked() void DownloadItem::on_pushButton_2_clicked()
{ {
ui->label_2->setText(tr("Download canceled")); ui->label_2->setText(tr("Download canceled"));
download = 2;
ui->pushButton_2->setEnabled(false); ui->pushButton_2->setEnabled(false);
ui->progressBar->hide(); ui->progressBar->hide();
close = true; close = true;

View File

@@ -29,7 +29,7 @@ public:
QString out; QString out;
QString pkgName; QString pkgName;
bool close; bool close;
int download;
void setValue(qint64); void setValue(qint64);
void setMax(qint64); void setMax(qint64);
void setName(QString); void setName(QString);

View File

@@ -72,16 +72,12 @@ void DownloadListWidget::clearItem()
} }
void DownloadListWidget::addItem(QString name,QString fileName,QString pkgName,const QPixmap icon,QString downloadurl) void DownloadListWidget::addItem(QString name,QString fileName,QString pkgName,const QPixmap icon,QString downloadurl)
{ {
allDownload += 1;
urList.append(downloadurl); urList.append(downloadurl);
if(dlist.contains(downloadurl))
{
return;
}
if(fileName.isEmpty()) if(fileName.isEmpty())
{ {
return; return;
} }
allDownload += 1;
DownloadItem *di=new DownloadItem(this); DownloadItem *di=new DownloadItem(this);
dlist<<downloadurl; dlist<<downloadurl;
downloaditemlist<<di; downloaditemlist<<di;
@@ -105,6 +101,11 @@ QList<DownloadItem *> DownloadListWidget::getDIList()
return downloaditemlist; return downloaditemlist;
} }
QList<QUrl> DownloadListWidget::getUrlList()
{
return urList;
}
void DownloadListWidget::startRequest(QUrl url, QString fileName) void DownloadListWidget::startRequest(QUrl url, QString fileName)
{ {
ui->listWidget->show(); ui->listWidget->show();
@@ -130,7 +131,6 @@ void DownloadListWidget::httpFinished() // 完成下载
emit downloadFinished(); emit downloadFinished();
if(nowDownload < allDownload) if(nowDownload < allDownload)
{ {
// 如果有排队则下载下一个 // 如果有排队则下载下一个
qDebug() << "切换下一个下载..."; qDebug() << "切换下一个下载...";
nowDownload += 1; nowDownload += 1;
@@ -143,24 +143,6 @@ void DownloadListWidget::httpFinished() // 完成下载
} }
} }
int DownloadListWidget::isDownloading(QString url)
{
int i = urList.indexOf(QUrl(url),0);
if(i == -1){
return 3;
}else if(i==nowDownload-1 && isdownload)
{
return 1;
}else if(i==nowDownload-1 && !isdownload)
{
return 2;
}else if(i<nowDownload-1)
{
return 2;
}
return 0;
}
void DownloadListWidget::updateDataReadProgress(QString speedInfo, qint64 bytesRead, qint64 totalBytes) void DownloadListWidget::updateDataReadProgress(QString speedInfo, qint64 bytesRead, qint64 totalBytes)
{ {
if(totalBytes <= 0) if(totalBytes <= 0)

View File

@@ -21,10 +21,10 @@ class DownloadListWidget : public DBlurEffectWidget
public: public:
void addItem(QString name, QString fileName, QString pkgName, const QPixmap icon, QString downloadurl); void addItem(QString name, QString fileName, QString pkgName, const QPixmap icon, QString downloadurl);
int isDownloading(QString url);
int nowDownload = 0; int nowDownload = 0;
int allDownload = 0; int allDownload = 0;
QList<DownloadItem *> getDIList(); QList<DownloadItem *> getDIList();
QList<QUrl> getUrlList();
void m_move(int x, int y); void m_move(int x, int y);
explicit DownloadListWidget(QWidget *parent = nullptr); explicit DownloadListWidget(QWidget *parent = nullptr);
~DownloadListWidget(); ~DownloadListWidget();