diff options
author | Claudio Maradonna <penguyman@stronzi.org> | 2018-05-01 17:44:34 +0200 |
---|---|---|
committer | Claudio Maradonna <penguyman@stronzi.org> | 2018-05-01 17:44:34 +0200 |
commit | 20f78f150f257f481fc0d49ec59b54ab822f5466 (patch) | |
tree | 36372626c5bfa90283336cae76f810a1e0f7f171 /src | |
parent | 18cdb184367a73a439be37ba168a1ca9061fbe9d (diff) |
Some TrayIcon refactor. Should the mainwindow know if is SystemTrayIsAvailable
Diffstat (limited to 'src')
-rw-r--r-- | src/mainwindow.cpp | 33 | ||||
-rw-r--r-- | src/trayicon.cpp | 32 | ||||
-rw-r--r-- | src/trayicon.h | 3 |
3 files changed, 39 insertions, 29 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9b9413ed..23e81b4e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -285,9 +285,9 @@ bool MainWindow::checkConfig() { // since we are still in constructor, can't directly hide QTimer::singleShot(10, this, SLOT(hide())); } - } else if (!QtPassSettings::isUseTrayIcon() && tray != NULL) { + } /*else if (!QtPassSettings::isUseTrayIcon() && tray != NULL) { destroyTrayIcon(); - } + }*/ // dbg()<< version; @@ -387,11 +387,10 @@ void MainWindow::config() { if (QtPassSettings::isAlwaysOnTop()) { Qt::WindowFlags flags = windowFlags(); this->setWindowFlags(flags | Qt::WindowStaysOnTopHint); - this->show(); } else { this->setWindowFlags(Qt::Window); - this->show(); } + this->show(); updateProfileBox(); ui->treeView->setRootIndex(proxyModel.mapFromSource( @@ -408,8 +407,9 @@ void MainWindow::config() { updateGitButtonVisibility(); if (QtPassSettings::isUseTrayIcon() && tray == NULL) initTrayIcon(); - else if (!QtPassSettings::isUseTrayIcon() && tray != NULL) + else if (!QtPassSettings::isUseTrayIcon() && tray != NULL) { destroyTrayIcon(); + } } freshStart = false; } @@ -1089,17 +1089,14 @@ void MainWindow::on_profileBox_currentIndexChanged(QString name) { * it */ void MainWindow::initTrayIcon() { - if (tray != NULL) { - dbg() << "Creating tray icon again?"; - return; - } - if (QSystemTrayIcon::isSystemTrayAvailable() == true) { - // Setup tray icon - this->tray = new TrayIcon(this); - if (tray == NULL) - dbg() << "Allocating tray icon failed."; - } else { - dbg() << "No tray icon for this OS possibly also not show options?"; + this->tray = new TrayIcon(this); + // Setup tray icon + + if (tray == NULL) + dbg() << "Allocating tray icon failed."; + + if (!tray->getIsAllocated()) { + destroyTrayIcon(); } } @@ -1107,10 +1104,6 @@ void MainWindow::initTrayIcon() { * @brief MainWindow::destroyTrayIcon remove that pesky tray icon */ void MainWindow::destroyTrayIcon() { - if (tray == NULL) { - dbg() << "Destroy non existing tray icon?"; - return; - } delete this->tray; tray = NULL; } diff --git a/src/trayicon.cpp b/src/trayicon.cpp index d08af611..d833f428 100644 --- a/src/trayicon.cpp +++ b/src/trayicon.cpp @@ -1,4 +1,5 @@ #include "trayicon.h" +#include "debughelper.h" #include <QAction> #include <QApplication> #include <QMainWindow> @@ -10,19 +11,27 @@ * @param parent */ TrayIcon::TrayIcon(QMainWindow *parent) { - parentwin = parent; + if (QSystemTrayIcon::isSystemTrayAvailable() == true) { + parentwin = parent; - createActions(); - createTrayIcon(); + createActions(); + createTrayIcon(); - sysTrayIcon->setIcon( - QIcon::fromTheme("qtpass-tray", QIcon(":/artwork/icon.png"))); + sysTrayIcon->setIcon( + QIcon::fromTheme("qtpass-tray", QIcon(":/artwork/icon.png"))); - sysTrayIcon->show(); + sysTrayIcon->show(); - QObject::connect(sysTrayIcon, - SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, - SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); + QObject::connect(sysTrayIcon, + SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, + SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); + + isAllocated = true; + } else { + dbg() << "No tray icon for this OS possibly also not show options?"; + + isAllocated = false; + } } /** @@ -37,6 +46,11 @@ void TrayIcon::setVisible(bool visible) { } /** + * @brief TrayIcon::getIsAllocated return if TrayIcon is allocated + */ +bool TrayIcon::getIsAllocated() { return isAllocated; } + +/** * @brief TrayIcon::createActions setup the signals. */ void TrayIcon::createActions() { diff --git a/src/trayicon.h b/src/trayicon.h index 8662561e..de91d4a6 100644 --- a/src/trayicon.h +++ b/src/trayicon.h @@ -18,6 +18,7 @@ public: explicit TrayIcon(QMainWindow *parent); void showMessage(QString title, QString msg, int time); void setVisible(bool visible); + bool getIsAllocated(); signals: @@ -39,6 +40,8 @@ private: QSystemTrayIcon *sysTrayIcon; QMenu *trayIconMenu; QMainWindow *parentwin; + + bool isAllocated; }; #endif // TRAYICON_H_ |