-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathupdatescheckwidget.cpp
128 lines (98 loc) · 3.99 KB
/
updatescheckwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2020 Jeremy HU <jeremy-at-dust3d dot org>. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <QVBoxLayout>
#include <QProgressBar>
#include <QDesktopServices>
#include "updatescheckwidget.h"
#include "util.h"
#include "updateschecker.h"
#define CHECKING_WIDGET_INDEX 0
#define SHOWING_RESULT_WIDGET_INDEX 1
UpdatesCheckWidget::UpdatesCheckWidget()
{
setWindowTitle(unifiedWindowTitle(tr("Check for Updates")));
m_stackedWidget = new QStackedWidget;
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_stackedWidget);
QWidget *checkingWidget = new QWidget;
QWidget *showingResultWidget = new QWidget;
////////// checking ///////////////////
QLabel *busyLabel = new QLabel;
busyLabel->setText(tr("Checking for updates..."));
QProgressBar *busyBar = new QProgressBar;
busyBar->setMaximum(0);
busyBar->setMinimum(0);
busyBar->setValue(0);
QVBoxLayout *checkingLayout = new QVBoxLayout;
checkingLayout->addWidget(busyLabel);
checkingLayout->addWidget(busyBar);
checkingWidget->setLayout(checkingLayout);
////////// showing result /////////////
m_infoLabel = new QLabel;
m_viewButton = new QPushButton(tr("View"));
m_viewButton->hide();
connect(m_viewButton, &QPushButton::clicked, this, &UpdatesCheckWidget::viewUpdates);
QVBoxLayout *showingResultLayout = new QVBoxLayout;
showingResultLayout->addWidget(m_infoLabel);
showingResultLayout->addStretch();
showingResultLayout->addWidget(m_viewButton);
showingResultWidget->setLayout(showingResultLayout);
m_stackedWidget->addWidget(checkingWidget);
m_stackedWidget->addWidget(showingResultWidget);
m_stackedWidget->setCurrentIndex(CHECKING_WIDGET_INDEX);
setLayout(mainLayout);
}
UpdatesCheckWidget::~UpdatesCheckWidget()
{
delete m_updatesChecker;
}
void UpdatesCheckWidget::viewUpdates()
{
if (m_viewUrl.isEmpty())
return;
QDesktopServices::openUrl(QUrl(m_viewUrl));
}
void UpdatesCheckWidget::check()
{
if (nullptr != m_updatesChecker)
return;
m_stackedWidget->setCurrentIndex(CHECKING_WIDGET_INDEX);
m_viewUrl.clear();
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);
m_updatesChecker->deleteLater();
m_updatesChecker = nullptr;
}