Skip to content

Commit fcb35b4

Browse files
rework the update checker, menu item, and notifier (#4481)
1 parent 5a075c1 commit fcb35b4

File tree

5 files changed

+68
-29
lines changed

5 files changed

+68
-29
lines changed

src/core/flameshotdaemon.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ FlameshotDaemon::FlameshotDaemon()
7272
, m_trayIcon(nullptr)
7373
#if !defined(DISABLE_UPDATE_CHECKER)
7474
, m_appLatestVersion(QStringLiteral(APP_VERSION).replace("v", ""))
75-
, m_showCheckAppUpdateStatus(false)
75+
, m_showManualCheckAppUpdateStatus(false)
7676
, m_networkCheckUpdates(nullptr)
7777
#endif
7878
{
@@ -238,11 +238,27 @@ void FlameshotDaemon::getLatestAvailableVersion()
238238

239239
void FlameshotDaemon::checkForUpdates()
240240
{
241-
if (m_appLatestUrl.isEmpty()) {
242-
m_showCheckAppUpdateStatus = true;
243-
getLatestAvailableVersion();
241+
bool autoCheckEnabled = ConfigHandler().checkForUpdates();
242+
243+
if (autoCheckEnabled) {
244+
if (!m_appLatestUrl.isEmpty()) {
245+
QDesktopServices::openUrl(QUrl(m_appLatestUrl));
246+
}
244247
} else {
245-
QDesktopServices::openUrl(QUrl(m_appLatestUrl));
248+
m_showManualCheckAppUpdateStatus = true;
249+
250+
if (m_appLatestUrl.isEmpty()) {
251+
getLatestAvailableVersion();
252+
} else {
253+
QVersionNumber appLatestVersion =
254+
QVersionNumber::fromString(m_appLatestVersion);
255+
if (Flameshot::instance()->getVersion() < appLatestVersion) {
256+
QDesktopServices::openUrl(QUrl(m_appLatestUrl));
257+
} else {
258+
sendTrayNotification(tr("You have the latest version"),
259+
"Flameshot");
260+
}
261+
}
246262
}
247263
}
248264
#endif
@@ -387,9 +403,11 @@ void FlameshotDaemon::enableTrayIcon(bool enable)
387403
#if !defined(DISABLE_UPDATE_CHECKER)
388404
void FlameshotDaemon::handleReplyCheckUpdates(QNetworkReply* reply)
389405
{
390-
if (!ConfigHandler().checkForUpdates()) {
406+
if (!ConfigHandler().checkForUpdates() &&
407+
!m_showManualCheckAppUpdateStatus) {
391408
return;
392409
}
410+
393411
if (reply->error() == QNetworkReply::NoError) {
394412
QJsonDocument response = QJsonDocument::fromJson(reply->readAll());
395413
QJsonObject json = response.object();
@@ -400,28 +418,25 @@ void FlameshotDaemon::handleReplyCheckUpdates(QNetworkReply* reply)
400418
if (Flameshot::instance()->getVersion() < appLatestVersion) {
401419
emit newVersionAvailable(appLatestVersion);
402420
m_appLatestUrl = json["html_url"].toString();
403-
QString newVersion =
404-
tr("New version %1 is available").arg(m_appLatestVersion);
405-
if (m_showCheckAppUpdateStatus) {
406-
sendTrayNotification(newVersion, "Flameshot");
421+
if (m_showManualCheckAppUpdateStatus) {
407422
QDesktopServices::openUrl(QUrl(m_appLatestUrl));
408423
}
409-
} else if (m_showCheckAppUpdateStatus) {
424+
} else if (m_showManualCheckAppUpdateStatus) {
410425
sendTrayNotification(tr("You have the latest version"),
411426
"Flameshot");
412427
}
413428
} else {
414429
qWarning() << "Failed to get information about the latest version. "
415430
<< reply->errorString();
416-
if (m_showCheckAppUpdateStatus) {
431+
if (m_showManualCheckAppUpdateStatus) {
417432
if (FlameshotDaemon::instance()) {
418433
FlameshotDaemon::instance()->sendTrayNotification(
419434
tr("Failed to get information about the latest version."),
420435
"Flameshot");
421436
}
422437
}
423438
}
424-
m_showCheckAppUpdateStatus = false;
439+
m_showManualCheckAppUpdateStatus = false;
425440
}
426441
#endif
427442

src/core/flameshotdaemon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private slots:
8787
#if !defined(DISABLE_UPDATE_CHECKER)
8888
QString m_appLatestUrl;
8989
QString m_appLatestVersion;
90-
bool m_showCheckAppUpdateStatus;
90+
bool m_showManualCheckAppUpdateStatus;
9191
QNetworkAccessManager* m_networkCheckUpdates;
9292
#endif
9393

src/widgets/trayicon.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ void TrayIcon::initMenu()
135135
&QAction::triggered,
136136
Flameshot::instance(),
137137
&Flameshot::config);
138-
auto* infoAction = new QAction(tr("&About"), this);
139-
connect(
140-
infoAction, &QAction::triggered, Flameshot::instance(), &Flameshot::info);
138+
m_infoAction = new QAction(tr("&About"), this);
139+
connect(m_infoAction,
140+
&QAction::triggered,
141+
Flameshot::instance(),
142+
&Flameshot::info);
141143

142144
#if !defined(DISABLE_UPDATE_CHECKER)
143145
m_appUpdates = new QAction(tr("Check for updates"), this);
@@ -150,10 +152,20 @@ void TrayIcon::initMenu()
150152
&FlameshotDaemon::newVersionAvailable,
151153
this,
152154
[this](const QVersionNumber& version) {
153-
QString newVersion =
154-
tr("New version %1 is available").arg(version.toString());
155-
m_appUpdates->setText(newVersion);
155+
if (ConfigHandler().checkForUpdates()) {
156+
QString newVersion =
157+
tr("Download version %1").arg(version.toString());
158+
m_appUpdates->setText(newVersion);
159+
m_appUpdates->setVisible(true);
160+
161+
// hack to work around menu not updating when the text /
162+
// visibility is modified Force menu refresh by removing and
163+
// re-adding the action
164+
m_menu->removeAction(m_appUpdates);
165+
m_menu->insertAction(m_infoAction, m_appUpdates);
166+
}
156167
});
168+
updateCheckUpdatesMenuVisibility();
157169
#endif
158170

159171
QAction* quitAction = new QAction(tr("&Quit"), this);
@@ -186,7 +198,7 @@ void TrayIcon::initMenu()
186198
#if !defined(DISABLE_UPDATE_CHECKER)
187199
m_menu->addAction(m_appUpdates);
188200
#endif
189-
m_menu->addAction(infoAction);
201+
m_menu->addAction(m_infoAction);
190202
m_menu->addSeparator();
191203
m_menu->addAction(quitAction);
192204
}
@@ -204,14 +216,20 @@ void TrayIcon::updateCaptureActionShortcut()
204216
}
205217

206218
#if !defined(DISABLE_UPDATE_CHECKER)
207-
void TrayIcon::enableCheckUpdatesAction(bool enable)
219+
void TrayIcon::updateCheckUpdatesMenuVisibility()
208220
{
209-
if (m_appUpdates != nullptr) {
210-
m_appUpdates->setVisible(enable);
211-
m_appUpdates->setEnabled(enable);
221+
if (m_appUpdates == nullptr) {
222+
return;
212223
}
213-
if (enable) {
214-
FlameshotDaemon::instance()->getLatestAvailableVersion();
224+
225+
bool autoCheckEnabled = ConfigHandler().checkForUpdates();
226+
if (autoCheckEnabled) {
227+
// When auto-check is enabled, hide the menu item initially
228+
// It will be shown when a new version is available via a callback
229+
m_appUpdates->setVisible(false);
230+
} else {
231+
m_appUpdates->setVisible(true);
232+
m_appUpdates->setText(tr("Check for updates"));
215233
}
216234
}
217235
#endif

src/widgets/trayicon.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ class TrayIcon : public QSystemTrayIcon
2020
void initMenu();
2121
void updateCaptureActionShortcut();
2222
#if !defined(DISABLE_UPDATE_CHECKER)
23-
void enableCheckUpdatesAction(bool enable);
23+
void updateCheckUpdatesMenuVisibility();
2424
#endif
2525

2626
void startGuiCapture();
2727

2828
QMenu* m_menu;
2929
QAction* m_captureAction;
30+
QAction* m_infoAction;
3031
#if !defined(DISABLE_UPDATE_CHECKER)
3132
QAction* m_appUpdates;
3233
#endif

src/widgets/updatenotificationwidget.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,16 @@ void UpdateNotificationWidget::ignoreButton()
8484

8585
void UpdateNotificationWidget::updateButton()
8686
{
87-
QDesktopServices::openUrl(m_appLatestUrl);
87+
// Store URL before closing widgets
88+
QString url = m_appLatestUrl;
89+
8890
hide();
8991
if (parentWidget()) {
9092
parentWidget()->close();
9193
}
94+
95+
// Open URL after closing widgets
96+
QDesktopServices::openUrl(QUrl(url));
9297
}
9398

9499
void UpdateNotificationWidget::initInternalPanel()

0 commit comments

Comments
 (0)