Debian Codegraph @

src/librssguard/gui/dialogs/formupdate.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/dialogs/formupdate.h"
4
5 #include "definitions/definitions.h"
6 #include "gui/guiutilities.h"
7 #include "miscellaneous/iconfactory.h"
8 #include "network-web/downloader.h"
9 #include "network-web/networkfactory.h"
10 #include "network-web/webfactory.h"
11
12 #include <QNetworkReply>
13
14 #if defined(Q_OS_WIN)
15 #include <windows.h>
16 #endif
17
18 FormUpdate::FormUpdate(QWidget* parent) : QDialog(parent) {
19 m_ui.setupUi(this);
20 m_ui.m_lblCurrentRelease->setText(QSL(APP_VERSION));
21 m_ui.m_tabInfo->removeTab(1);
22 m_ui.m_buttonBox->setEnabled(false);
23
24 // Set flags and attributes.
25 GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("help-about")));
26
27 connect(&m_downloader, &Downloader::progress, this, &FormUpdate::updateProgress);
28 connect(&m_downloader, &Downloader::completed, this, &FormUpdate::updateCompleted);
29
30 if (isSelfUpdateSupported()) {
31 m_btnUpdate = m_ui.m_buttonBox->addButton(tr("Download selected update"), QDialogButtonBox::ButtonRole::ActionRole);
32 m_btnUpdate->setToolTip(tr("Download new installation files."));
33 }
34 else {
35 m_btnUpdate =
36 m_ui.m_buttonBox->addButton(tr("Go to application website"), QDialogButtonBox::ButtonRole::ActionRole);
37 m_btnUpdate->setToolTip(tr("Go to application website to get update packages manually."));
38 }
39
40 m_btnUpdate->setVisible(false);
41 connect(m_btnUpdate, &QPushButton::clicked, this, &FormUpdate::startUpdate);
42 checkForUpdates();
43 }
44
45 bool FormUpdate::isSelfUpdateSupported() const {
46 #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
47 return true;
48 #else
49 return false;
50 #endif
51 }
52
53 void FormUpdate::checkForUpdates() {
54 connect(qApp->system(),
55 &SystemFactory::updatesChecked,
56 this,
57 [this](const QPair<QList<UpdateInfo>, QNetworkReply::NetworkError>& update) {
58 m_ui.m_buttonBox->setEnabled(true);
59 disconnect(qApp->system(), &SystemFactory::updatesChecked, nullptr, nullptr);
60
61 if (update.second != QNetworkReply::NoError) {
62 m_updateInfo = UpdateInfo();
63 m_ui.m_tabInfo->setEnabled(false);
64
65 //: Unknown release.
66 m_ui.m_lblAvailableRelease->setText(tr("unknown"));
67 m_ui.m_txtChanges->clear();
68 m_ui.m_lblStatus->setStatus(WidgetWithStatus::StatusType::Error,
69 tr("Error: '%1'.").arg(NetworkFactory::networkErrorText(update.second)),
70 tr("List with updates was not\ndownloaded successfully."));
71 }
72 else {
73 const bool self_update_supported = isSelfUpdateSupported();
74 m_updateInfo = update.first.at(0);
75 m_ui.m_tabInfo->setEnabled(true);
76 m_ui.m_lblAvailableRelease->setText(m_updateInfo.m_availableVersion);
77
78 #if QT_VERSION >= 0x050E00 // Qt >= 5.14.0
79 m_ui.m_txtChanges->setMarkdown(m_updateInfo.m_changes);
80 #else
81 m_ui.m_txtChanges->setText(m_updateInfo.m_changes);
82 #endif
83
84 if (SystemFactory::isVersionNewer(m_updateInfo.m_availableVersion, QSL(APP_VERSION))) {
85 m_btnUpdate->setVisible(true);
86 m_ui.m_lblStatus->setStatus(WidgetWithStatus::StatusType::Ok,
87 tr("New release available."),
88 tr("This is new version which can be\ndownloaded."));
89
90 if (self_update_supported) {
91 loadAvailableFiles();
92 }
93 }
94 else {
95 m_ui.m_lblStatus->setStatus(WidgetWithStatus::StatusType::Warning,
96 tr("No new release available."),
97 tr("This release is not newer than\ncurrently installed one."));
98 }
99 }
100 });
101
102 qApp->system()->checkForUpdates();
103 }
104
105 void FormUpdate::updateProgress(qint64 bytes_received, qint64 bytes_total) {
106 if (bytes_received - m_lastDownloadedBytes > 500000 || m_lastDownloadedBytes == 0) {
107 m_ui.m_lblStatus->setStatus(WidgetWithStatus::StatusType::Information,
108 tr("Downloaded %1% (update size is %2 kB).")
109 .arg(QString::number(bytes_total == 0L ? 0L : (bytes_received * 100.0) / bytes_total,
110 'f',
111 2),
112 QString::number(bytes_total / 1000.0, 'f', 2)),
113 tr("Downloading update..."));
114 m_ui.m_lblStatus->repaint();
115 m_lastDownloadedBytes = bytes_received;
116 }
117 }
118
119 void FormUpdate::saveUpdateFile(const QByteArray& file_contents) {
120 const QString url_file = m_ui.m_listFiles->currentItem()->data(Qt::UserRole).toString();
121 const QString temp_directory = qApp->tempFolder();
122
123 if (!temp_directory.isEmpty()) {
124 const QString output_file_name = url_file.mid(url_file.lastIndexOf('/') + 1);
125 QFile output_file(temp_directory + QDir::separator() + output_file_name);
126
127 if (output_file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
128 qDebugNN << "Storing update file to temporary location"
129 << QUOTE_W_SPACE_DOT(QDir::toNativeSeparators(output_file.fileName()));
130
131 output_file.write(file_contents);
132 output_file.flush();
133 output_file.close();
134
135 qDebugNN << "Update file contents was successfuly saved.";
136
137 m_updateFilePath = output_file.fileName();
138 m_readyToInstall = true;
139 }
140 else {
141 qDebugNN << LOGSEC_GUI << "Cannot save downloaded update file because target temporary file '" << output_file_name
142 << "' cannot be opened for writing.";
143 }
144 }
145 else {
146 qDebugNN << LOGSEC_GUI << "Cannot save downloaded update file because no TEMP directory is available.";
147 }
148 }
149
150 void FormUpdate::loadAvailableFiles() {
151 m_ui.m_listFiles->clear();
152
153 for (const UpdateUrl& url : std::as_const(m_updateInfo.m_urls)) {
154 if (SystemFactory::supportedUpdateFiles().match(url.m_name).hasMatch()) {
155 QListWidgetItem* item = new QListWidgetItem(url.m_name + tr(" (size ") + url.m_size + QSL(")"));
156
157 item->setData(Qt::ItemDataRole::UserRole, url.m_fileUrl);
158 item->setToolTip(url.m_fileUrl);
159 m_ui.m_listFiles->addItem(item);
160 }
161 }
162
163 if (m_ui.m_listFiles->count() > 0) {
164 m_ui.m_listFiles->setCurrentRow(0);
165 }
166 else {
167 m_btnUpdate->setEnabled(false);
168 }
169
170 m_ui.m_tabInfo->addTab(m_ui.tabFiles, tr("Available update files"));
171 m_ui.m_tabInfo->setCurrentIndex(1);
172 }
173
174 void FormUpdate::updateCompleted(const QUrl& url,
175 QNetworkReply::NetworkError status,
176 int http_code,
177 const QByteArray& contents) {
178 Q_UNUSED(url)
179 Q_UNUSED(http_code)
180
181 qDebugNN << LOGSEC_GUI << "Download of application update file was completed with code" << QUOTE_W_SPACE_DOT(status);
182
183 switch (status) {
184 case QNetworkReply::NetworkError::NoError:
185 saveUpdateFile(contents);
186 m_ui.m_lblStatus->setStatus(WidgetWithStatus::StatusType::Ok,
187 tr("Downloaded successfully"),
188 tr("Package was downloaded successfully.\nYou can install it now."));
189 m_btnUpdate->setText(tr("Install"));
190 m_btnUpdate->setEnabled(true);
191 break;
192
193 default:
194 m_ui.m_lblStatus->setStatus(WidgetWithStatus::StatusType::Error,
195 tr("Error occured"),
196 tr("Error occured during downloading of the package."));
197 m_btnUpdate->setText(tr("Error occured"));
198 break;
199 }
200 }
201
202 void FormUpdate::startUpdate() {
203 QString url_file;
204 const bool update_for_this_system = isSelfUpdateSupported();
205
206 if (update_for_this_system && m_ui.m_listFiles->currentItem() != nullptr) {
207 url_file = m_ui.m_listFiles->currentItem()->data(Qt::UserRole).toString();
208 m_ui.m_listFiles->setEnabled(false);
209 }
210 else {
211 url_file = QSL(APP_URL);
212 }
213
214 if (m_readyToInstall) {
215 close();
216 qDebugNN << LOGSEC_GUI << "Preparing to launch external installer '" << QDir::toNativeSeparators(m_updateFilePath)
217 << "'.";
218
219 #if defined(Q_OS_WIN)
220 HINSTANCE exec_result =
221 ShellExecute(nullptr,
222 nullptr,
223 reinterpret_cast<const WCHAR*>(QDir::toNativeSeparators(m_updateFilePath).utf16()),
224 nullptr,
225 nullptr,
226 SW_NORMAL);
227
228 if (exec_result <= HINSTANCE(32)) {
229 qDebugNN << LOGSEC_GUI << "External updater was not launched due to error.";
230 qApp->showGuiMessage(Notification::Event::GeneralEvent,
231 {tr("Cannot update application"),
232 tr("Cannot launch external updater. Update application manually."),
233 QSystemTrayIcon::MessageIcon::Warning},
234 {},
235 {},
236 this);
237 }
238 else {
239 qApp->quit();
240 }
241 #endif
242 }
243 else if (update_for_this_system) {
244 updateProgress(0, 100);
245 m_btnUpdate->setText(tr("Downloading update..."));
246 m_btnUpdate->setEnabled(false);
247 m_downloader.downloadFile(url_file);
248 }
249 else {
250 // Self-update and package are not available.
251 qApp->web()->openUrlInExternalBrowser(url_file);
252 }
253 }