dust3d/application/sources/updates_check_widget.cc

108 lines
2.7 KiB
C++
Raw Normal View History

#include "updates_check_widget.h"
#include "updates_checker.h"
#include "version.h"
#include <QDesktopServices>
#include <QProgressBar>
#include <QVBoxLayout>
2019-07-24 12:41:55 +00:00
#define CHECKING_WIDGET_INDEX 0
#define SHOWING_RESULT_WIDGET_INDEX 1
2019-07-24 12:41:55 +00:00
UpdatesCheckWidget::UpdatesCheckWidget()
{
setWindowTitle(applicationTitle(tr("Check for Updates")));
2019-07-24 12:41:55 +00:00
m_stackedWidget = new QStackedWidget;
QVBoxLayout* mainLayout = new QVBoxLayout;
2019-07-24 12:41:55 +00:00
mainLayout->addWidget(m_stackedWidget);
QWidget* checkingWidget = new QWidget;
QWidget* showingResultWidget = new QWidget;
2019-07-24 12:41:55 +00:00
////////// checking ///////////////////
QLabel* busyLabel = new QLabel;
2019-07-24 12:41:55 +00:00
busyLabel->setText(tr("Checking for updates..."));
QProgressBar* busyBar = new QProgressBar;
2019-07-24 12:41:55 +00:00
busyBar->setMaximum(0);
busyBar->setMinimum(0);
busyBar->setValue(0);
QVBoxLayout* checkingLayout = new QVBoxLayout;
2019-07-24 12:41:55 +00:00
checkingLayout->addWidget(busyLabel);
checkingLayout->addWidget(busyBar);
2019-07-24 12:41:55 +00:00
checkingWidget->setLayout(checkingLayout);
2019-07-24 12:41:55 +00:00
////////// showing result /////////////
2019-07-24 12:41:55 +00:00
m_infoLabel = new QLabel;
2019-07-24 12:41:55 +00:00
m_viewButton = new QPushButton(tr("View"));
m_viewButton->hide();
2019-07-24 12:41:55 +00:00
connect(m_viewButton, &QPushButton::clicked, this, &UpdatesCheckWidget::viewUpdates);
QVBoxLayout* showingResultLayout = new QVBoxLayout;
2019-07-24 12:41:55 +00:00
showingResultLayout->addWidget(m_infoLabel);
showingResultLayout->addStretch();
showingResultLayout->addWidget(m_viewButton);
2019-07-24 12:41:55 +00:00
showingResultWidget->setLayout(showingResultLayout);
2019-07-24 12:41:55 +00:00
m_stackedWidget->addWidget(checkingWidget);
m_stackedWidget->addWidget(showingResultWidget);
2019-07-24 12:41:55 +00:00
m_stackedWidget->setCurrentIndex(CHECKING_WIDGET_INDEX);
2019-07-24 12:41:55 +00:00
setLayout(mainLayout);
}
UpdatesCheckWidget::~UpdatesCheckWidget()
{
delete m_updatesChecker;
}
void UpdatesCheckWidget::viewUpdates()
{
if (m_viewUrl.isEmpty())
return;
2019-07-24 12:41:55 +00:00
QDesktopServices::openUrl(QUrl(m_viewUrl));
}
void UpdatesCheckWidget::check()
{
if (nullptr != m_updatesChecker)
return;
2019-07-24 12:41:55 +00:00
m_stackedWidget->setCurrentIndex(CHECKING_WIDGET_INDEX);
2019-07-24 12:41:55 +00:00
m_viewUrl.clear();
2019-07-24 12:41:55 +00:00
m_updatesChecker = new UpdatesChecker;
connect(m_updatesChecker, &UpdatesChecker::finished, this, &UpdatesCheckWidget::checkFinished);
m_updatesChecker->start();
}
void UpdatesCheckWidget::checkFinished()
{
m_infoLabel->setText(m_updatesChecker->message());
if (m_updatesChecker->hasError()) {
m_viewButton->hide();
} else {
if (m_updatesChecker->isLatest()) {
m_viewButton->hide();
} else {
m_viewUrl = m_updatesChecker->matchedUpdateItem().descriptionUrl;
m_viewButton->show();
}
}
m_stackedWidget->setCurrentIndex(SHOWING_RESULT_WIDGET_INDEX);
2019-07-24 12:41:55 +00:00
m_updatesChecker->deleteLater();
m_updatesChecker = nullptr;
}