Only call QSystemTrayIcon::isSystemTrayAvailable once

Workaround file descriptor leak

Fixes #724
This commit is contained in:
Jonas Kvinge
2021-06-15 00:25:54 +02:00
parent ec3bcdcb26
commit 081df59ed7
7 changed files with 28 additions and 19 deletions

View File

@@ -43,6 +43,7 @@ class SystemTrayIcon : public QObject {
~SystemTrayIcon(); ~SystemTrayIcon();
bool isSystemTrayAvailable() { return true; } bool isSystemTrayAvailable() { return true; }
bool IsSystemTrayAvailable() { return true; }
void setVisible(const bool) {} void setVisible(const bool) {}
void SetTrayiconProgress(const bool enabled); void SetTrayiconProgress(const bool enabled);

View File

@@ -927,7 +927,7 @@ MainWindow::MainWindow(Application *app, std::shared_ptr<SystemTrayIcon> tray_ic
show(); show();
break; break;
case BehaviourSettingsPage::Startup_Hide: case BehaviourSettingsPage::Startup_Hide:
if (tray_icon_->isSystemTrayAvailable() && tray_icon_->isVisible()) { if (tray_icon_->IsSystemTrayAvailable() && tray_icon_->isVisible()) {
hide(); hide();
break; break;
} }
@@ -941,7 +941,7 @@ MainWindow::MainWindow(Application *app, std::shared_ptr<SystemTrayIcon> tray_ic
was_minimized_ = settings_.value("minimized", false).toBool(); was_minimized_ = settings_.value("minimized", false).toBool();
if (was_minimized_) setWindowState(windowState() | Qt::WindowMinimized); if (was_minimized_) setWindowState(windowState() | Qt::WindowMinimized);
if (!tray_icon_->isSystemTrayAvailable() || !tray_icon_->isVisible()) { if (!tray_icon_->IsSystemTrayAvailable() || !tray_icon_->isVisible()) {
hidden_ = false; hidden_ = false;
settings_.setValue("hidden", false); settings_.setValue("hidden", false);
show(); show();
@@ -1017,12 +1017,12 @@ void MainWindow::ReloadSettings() {
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS
s.beginGroup(BehaviourSettingsPage::kSettingsGroup); s.beginGroup(BehaviourSettingsPage::kSettingsGroup);
bool showtrayicon = s.value("showtrayicon", tray_icon_->isSystemTrayAvailable()).toBool(); bool showtrayicon = s.value("showtrayicon", tray_icon_->IsSystemTrayAvailable()).toBool();
s.endGroup(); s.endGroup();
if (tray_icon_->isSystemTrayAvailable()) { if (tray_icon_->IsSystemTrayAvailable()) {
tray_icon_->setVisible(showtrayicon); tray_icon_->setVisible(showtrayicon);
} }
if ((!showtrayicon || !tray_icon_->isSystemTrayAvailable()) && !isVisible()) { if ((!showtrayicon || !tray_icon_->IsSystemTrayAvailable()) && !isVisible()) {
show(); show();
} }
#endif #endif
@@ -1182,7 +1182,7 @@ void MainWindow::Exit() {
if (app_->player()->GetState() == Engine::Playing) { if (app_->player()->GetState() == Engine::Playing) {
app_->player()->Stop(); app_->player()->Stop();
hide(); hide();
if (tray_icon_->isSystemTrayAvailable()) { if (tray_icon_->IsSystemTrayAvailable()) {
tray_icon_->setVisible(false); tray_icon_->setVisible(false);
} }
return; // Don't quit the application now: wait for the fadeout finished signal return; // Don't quit the application now: wait for the fadeout finished signal
@@ -1552,7 +1552,7 @@ void MainWindow::showEvent(QShowEvent *e) {
void MainWindow::closeEvent(QCloseEvent *e) { void MainWindow::closeEvent(QCloseEvent *e) {
if (!exit_) { if (!exit_) {
if (!hidden_ && keep_running_ && e->spontaneous() && tray_icon_->isSystemTrayAvailable()) { if (!hidden_ && keep_running_ && e->spontaneous() && tray_icon_->IsSystemTrayAvailable()) {
SetHiddenInTray(true); SetHiddenInTray(true);
} }
else { else {

View File

@@ -49,6 +49,7 @@ SystemTrayIcon::SystemTrayIcon(QObject *parent)
action_stop_(nullptr), action_stop_(nullptr),
action_stop_after_this_track_(nullptr), action_stop_after_this_track_(nullptr),
action_mute_(nullptr), action_mute_(nullptr),
available_(false),
trayicon_progress_(false), trayicon_progress_(false),
song_progress_(0) { song_progress_(0) {
@@ -59,6 +60,7 @@ SystemTrayIcon::SystemTrayIcon(QObject *parent)
} }
if (isSystemTrayAvailable()) { if (isSystemTrayAvailable()) {
available_ = true;
setIcon(normal_icon_); setIcon(normal_icon_);
setToolTip(app_name_); setToolTip(app_name_);
} }
@@ -100,7 +102,7 @@ void SystemTrayIcon::SetupMenu(QAction *previous, QAction *play, QAction *stop,
menu_->addSeparator(); menu_->addSeparator();
menu_->addAction(quit->icon(), quit->text(), quit, &QAction::trigger); menu_->addAction(quit->icon(), quit->text(), quit, &QAction::trigger);
if (isSystemTrayAvailable()) setContextMenu(menu_); if (available_) setContextMenu(menu_);
} }
@@ -123,12 +125,12 @@ void SystemTrayIcon::Clicked(const QSystemTrayIcon::ActivationReason reason) {
} }
void SystemTrayIcon::ShowPopup(const QString &summary, const QString &message, const int timeout) { void SystemTrayIcon::ShowPopup(const QString &summary, const QString &message, const int timeout) {
if (isSystemTrayAvailable()) showMessage(summary, message, QSystemTrayIcon::NoIcon, timeout); if (available_) showMessage(summary, message, QSystemTrayIcon::NoIcon, timeout);
} }
void SystemTrayIcon::UpdateIcon() { void SystemTrayIcon::UpdateIcon() {
if (isSystemTrayAvailable()) setIcon(CreateIcon(normal_icon_, grey_icon_)); if (available_) setIcon(CreateIcon(normal_icon_, grey_icon_));
} }
@@ -187,11 +189,11 @@ void SystemTrayIcon::MuteButtonStateChanged(const bool value) {
} }
void SystemTrayIcon::SetNowPlaying(const Song &song, const QUrl&) { void SystemTrayIcon::SetNowPlaying(const Song &song, const QUrl&) {
if (isSystemTrayAvailable()) setToolTip(song.PrettyTitleWithArtist()); if (available_) setToolTip(song.PrettyTitleWithArtist());
} }
void SystemTrayIcon::ClearNowPlaying() { void SystemTrayIcon::ClearNowPlaying() {
if (isSystemTrayAvailable()) setToolTip(app_name_); if (available_) setToolTip(app_name_);
} }
void SystemTrayIcon::LoveVisibilityChanged(const bool value) { void SystemTrayIcon::LoveVisibilityChanged(const bool value) {

View File

@@ -44,6 +44,8 @@ class SystemTrayIcon : public QSystemTrayIcon {
explicit SystemTrayIcon(QObject *parent = nullptr); explicit SystemTrayIcon(QObject *parent = nullptr);
~SystemTrayIcon() override; ~SystemTrayIcon() override;
bool IsSystemTrayAvailable() const { return available_; }
void SetTrayiconProgress(const bool enabled); void SetTrayiconProgress(const bool enabled);
void SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit); void SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit);
@@ -93,6 +95,7 @@ class SystemTrayIcon : public QSystemTrayIcon {
QAction *action_mute_; QAction *action_mute_;
QAction *action_love_; QAction *action_love_;
bool available_;
bool trayicon_progress_; bool trayicon_progress_;
int song_progress_; int song_progress_;

View File

@@ -435,7 +435,7 @@ bool OSDBase::SupportsNativeNotifications() {
} }
bool OSDBase::SupportsTrayPopups() { bool OSDBase::SupportsTrayPopups() {
return tray_icon_->isSystemTrayAvailable(); return tray_icon_->IsSystemTrayAvailable();
} }
void OSDBase::ShowMessageNative(const QString&, const QString&, const QString&, const QImage&) { void OSDBase::ShowMessageNative(const QString&, const QString&, const QString&, const QImage&) {

View File

@@ -58,11 +58,13 @@ bool LocaleAwareCompare(const QString &a, const QString &b) {
} // namespace } // namespace
#endif #endif
BehaviourSettingsPage::BehaviourSettingsPage(SettingsDialog *dialog) : SettingsPage(dialog), ui_(new Ui_BehaviourSettingsPage) { BehaviourSettingsPage::BehaviourSettingsPage(SettingsDialog *dialog) : SettingsPage(dialog), ui_(new Ui_BehaviourSettingsPage), systemtray_available_(false) {
ui_->setupUi(this); ui_->setupUi(this);
setWindowIcon(IconLoader::Load("strawberry")); setWindowIcon(IconLoader::Load("strawberry"));
systemtray_available_ = QSystemTrayIcon::isSystemTrayAvailable();
QObject::connect(ui_->checkbox_showtrayicon, &QCheckBox::toggled, this, &BehaviourSettingsPage::ShowTrayIconToggled); QObject::connect(ui_->checkbox_showtrayicon, &QCheckBox::toggled, this, &BehaviourSettingsPage::ShowTrayIconToggled);
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
@@ -144,7 +146,7 @@ void BehaviourSettingsPage::Load() {
s.beginGroup(kSettingsGroup); s.beginGroup(kSettingsGroup);
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS
if (QSystemTrayIcon::isSystemTrayAvailable()) { if (systemtray_available_) {
ui_->checkbox_showtrayicon->setEnabled(true); ui_->checkbox_showtrayicon->setEnabled(true);
ui_->checkbox_showtrayicon->setChecked(s.value("showtrayicon", true).toBool()); ui_->checkbox_showtrayicon->setChecked(s.value("showtrayicon", true).toBool());
ui_->radiobutton_hide->setEnabled(true); ui_->radiobutton_hide->setEnabled(true);
@@ -157,7 +159,7 @@ void BehaviourSettingsPage::Load() {
} }
#endif #endif
if (QSystemTrayIcon::isSystemTrayAvailable()) { if (systemtray_available_) {
ui_->checkbox_keeprunning->setEnabled(true); ui_->checkbox_keeprunning->setEnabled(true);
ui_->checkbox_keeprunning->setChecked(s.value("keeprunning", false).toBool()); ui_->checkbox_keeprunning->setChecked(s.value("keeprunning", false).toBool());
ui_->checkbox_trayicon_progress->setEnabled(true); ui_->checkbox_trayicon_progress->setEnabled(true);
@@ -186,7 +188,7 @@ void BehaviourSettingsPage::Load() {
ui_->radiobutton_show_minimized->setChecked(true); ui_->radiobutton_show_minimized->setChecked(true);
break; break;
case Startup_Hide: case Startup_Hide:
if (QSystemTrayIcon::isSystemTrayAvailable()) { if (systemtray_available_) {
ui_->radiobutton_hide->setChecked(true); ui_->radiobutton_hide->setChecked(true);
break; break;
} }

View File

@@ -77,12 +77,13 @@ public:
void Load() override; void Load() override;
void Save() override; void Save() override;
private slots: private slots:
void ShowTrayIconToggled(bool on); void ShowTrayIconToggled(bool on);
private: private:
Ui_BehaviourSettingsPage *ui_; Ui_BehaviourSettingsPage *ui_;
QMap<QString, QString> language_map_; QMap<QString, QString> language_map_;
bool systemtray_available_;
}; };