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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/**
* Copyright (C) 2021-2024 Saturneric <eric@bktus.com>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <eric@bktus.com> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "UpdateTab.h"
#include "GFModule.h"
#include "UpdateChecker.h"
#include "UpdatePresentation.h"
#include "VersionCheckingModule.h"
namespace {
constexpr auto kDownloadPage =
"https://www.gpgfrontend.bktus.com/overview/downloads/";
auto ToneIcon(const QStyle* style, UpdateTone tone) -> QIcon {
switch (tone) {
case UpdateTone::kGood:
return style->standardIcon(QStyle::SP_DialogApplyButton);
case UpdateTone::kAttention:
return style->standardIcon(QStyle::SP_MessageBoxWarning);
case UpdateTone::kNeutral:
break;
}
return style->standardIcon(QStyle::SP_MessageBoxInformation);
}
} // namespace
UpdateTab::UpdateTab(QWidget* parent)
: QWidget(parent), checker_(VersionChecker()) {
icon_ = new QLabel(this);
icon_->setAlignment(Qt::AlignTop);
headline_ = new QLabel(this);
headline_->setWordWrap(true);
auto headline_font = headline_->font();
headline_font.setBold(true);
headline_font.setPointSizeF(headline_font.pointSizeF() * 1.25);
headline_->setFont(headline_font);
detail_ = new QLabel(this);
detail_->setWordWrap(true);
notice_ = new QLabel(this);
notice_->setWordWrap(true);
freshness_ = new QLabel(this);
freshness_->setWordWrap(true);
freshness_->setForegroundRole(QPalette::PlaceholderText);
auto small_font = freshness_->font();
small_font.setPointSizeF(small_font.pointSizeF() * 0.9);
freshness_->setFont(small_font);
auto* text_column = new QVBoxLayout();
text_column->setSpacing(4);
text_column->addWidget(headline_);
text_column->addWidget(detail_);
text_column->addWidget(notice_);
text_column->addWidget(freshness_);
auto* header = new QHBoxLayout();
header->setSpacing(12);
header->addWidget(icon_, 0, Qt::AlignTop);
header->addLayout(text_column, 1);
busy_ = new QProgressBar(this);
busy_->setRange(0, 0);
busy_->setTextVisible(false);
busy_->setMaximumHeight(6);
check_btn_ = new QPushButton(this);
check_btn_->setIcon(QIcon::fromTheme("view-refresh"));
download_btn_ = new QPushButton(this);
download_btn_->setIcon(QIcon::fromTheme("download"));
download_btn_->setDefault(true);
auto* actions = new QHBoxLayout();
actions->addWidget(busy_, 1);
actions->addStretch();
actions->addWidget(check_btn_);
actions->addWidget(download_btn_);
notes_toggle_ = new QToolButton(this);
notes_toggle_->setText(tr("Release notes"));
notes_toggle_->setCheckable(true);
notes_toggle_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
notes_toggle_->setArrowType(Qt::RightArrow);
notes_toggle_->setAutoRaise(true);
notes_ = new QTextBrowser(this);
notes_->setOpenExternalLinks(true);
notes_->hide();
auto* layout = new QVBoxLayout(this);
layout->addLayout(header);
layout->addSpacing(8);
layout->addLayout(actions);
layout->addSpacing(8);
layout->addWidget(notes_toggle_, 0, Qt::AlignLeft);
layout->addWidget(notes_, 1);
layout->addStretch();
// Wide enough that a headline wraps at most once; the height follows the
// content (see fit_window()).
setMinimumWidth(fontMetrics().averageCharWidth() * 64);
connect(notes_toggle_, &QToolButton::toggled, this, [this](bool open) {
notes_toggle_->setArrowType(open ? Qt::DownArrow : Qt::RightArrow);
notes_->setVisible(open);
fit_window();
});
connect(check_btn_, &QPushButton::clicked, this, [this] {
if (!checker_.isNull()) checker_->Start(CheckMode::kForce);
});
connect(download_btn_, &QPushButton::clicked, this, [] {
QDesktopServices::openUrl(QUrl(QString::fromLatin1(kDownloadPage)));
});
if (!checker_.isNull()) {
connect(checker_, &UpdateChecker::Changed, this, [this] {
// An update found while the page is open has been seen.
if (isVisible()) checker_->Acknowledge();
render();
});
}
// Readable at once: a check that finished before this dialog existed has
// left its answer in the checker, not in a signal already gone.
render();
}
void UpdateTab::render() {
if (checker_.isNull()) {
UpdateView view;
view.headline = tr("Update checking is unavailable");
view.check_enabled = false;
view.check_label = tr("Check now");
apply(view);
return;
}
apply(PresentUpdate(checker_->State(), QDateTime::currentDateTime()));
}
void UpdateTab::fit_window() {
// Deferred: the layout settles the new content first. The frame is the
// window; this page fills it, so fitting the window fits the page. It
// keeps its place on screen, and overrides a size remembered from a page
// that showed something else.
QTimer::singleShot(0, this, [this] {
if (isVisible()) window()->adjustSize();
});
}
void UpdateTab::apply(const UpdateView& view) {
const auto icon_size = style()->pixelMetric(QStyle::PM_MessageBoxIconSize);
icon_->setPixmap(ToneIcon(style(), view.tone).pixmap(icon_size, icon_size));
headline_->setText(view.headline);
detail_->setText(view.detail);
detail_->setVisible(!view.detail.isEmpty());
notice_->setText(view.notice);
notice_->setVisible(!view.notice.isEmpty());
freshness_->setText(view.freshness);
freshness_->setVisible(!view.freshness.isEmpty());
busy_->setVisible(view.busy);
check_btn_->setText(view.check_label);
check_btn_->setEnabled(view.check_enabled);
download_btn_->setText(view.download_label);
download_btn_->setVisible(view.show_download);
notes_toggle_->setVisible(view.show_notes);
if (view.show_notes) {
notes_->setMarkdown(view.notes);
notes_->setVisible(notes_toggle_->isChecked());
} else {
notes_->clear();
notes_->hide();
}
fit_window();
}
void UpdateTab::showEvent(QShowEvent* event) {
QWidget::showEvent(event);
// Opening the page is seeing whatever update it advertises: the badge on
// the menu entry goes, until a still newer release turns up.
if (!checker_.isNull()) checker_->Acknowledge();
render();
// The Host's own switch, the one the setup wizard and this module's
// settings page share. Off, the dialog still offers the button: pressing
// it is the user asking.
const auto prohibit = gf::sdk::Setting(GFModuleSdkContext(), GF_SETTING_HOST,
"network/prohibit_update_check", false)
.toBool();
if (!prohibit && !checker_.isNull()) checker_->Start(CheckMode::kIfStale);
}
|