From d3aacf860dc387fe9960a1f8cafc217d1d4bbe89 Mon Sep 17 00:00:00 2001 From: Anne Jan Brouwer Date: Tue, 9 Jun 2015 00:54:24 +0200 Subject: trying old trayicon code --- mainwindow.cpp | 17 ++++++++++++ mainwindow.h | 4 +++ qtpass.pro | 6 +++-- trayicon.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ trayicon.h | 39 +++++++++++++++++++++++++++ 5 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 trayicon.cpp create mode 100644 trayicon.h diff --git a/mainwindow.cpp b/mainwindow.cpp index 00454f20..e625d124 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -37,6 +37,8 @@ MainWindow::MainWindow(QWidget *parent) : ui->statusBar->showMessage(tr("Welcome to QtPass %1").arg(VERSION), 2000); firstRun = true; startupPhase = true; + + initTrayIcon(); } /** @@ -1106,3 +1108,18 @@ void MainWindow::on_profileBox_currentIndexChanged(QString name) ui->treeView->setRootIndex(proxyModel.mapFromSource(model.setRootPath(passStore))); } + +void MainWindow::initTrayIcon() +{ + if(QSystemTrayIcon::isSystemTrayAvailable() == true) { + // Setup tray icon + this->tray = new trayIcon(this); + if(tray == NULL){ + qDebug() << "Allocating tray icon failed."; + exit(1); + } + qDebug() << "Ehm.."; + } else { + qDebug() << "Whut?"; + } +} diff --git a/mainwindow.h b/mainwindow.h index 5aafd89c..dbef6d5f 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -8,6 +8,7 @@ #include #include #include "storemodel.h" +#include "trayicon.h" #if SINGLE_APP #include "singleapplication.h" #else @@ -102,6 +103,7 @@ private: QHash profiles; QString profile; bool startupPhase; + trayIcon *tray; void updateText(); void executePass(QString, QString = QString()); void executeWrapper(QString, QString, QString = QString()); @@ -119,6 +121,8 @@ private: void mountWebDav(); void updateEnv(); void updateProfileBox(); + void initTrayIcon(); + }; #endif // MAINWINDOW_H diff --git a/qtpass.pro b/qtpass.pro index 50881eb3..e9f668aa 100644 --- a/qtpass.pro +++ b/qtpass.pro @@ -29,7 +29,8 @@ SOURCES += main.cpp\ util.cpp \ usersdialog.cpp \ keygendialog.cpp \ - progressindicator.cpp + progressindicator.cpp \ + trayicon.cpp HEADERS += mainwindow.h \ dialog.h \ @@ -37,7 +38,8 @@ HEADERS += mainwindow.h \ util.h \ usersdialog.h \ keygendialog.h \ - progressindicator.h + progressindicator.h \ + trayicon.h FORMS += mainwindow.ui \ dialog.ui \ diff --git a/trayicon.cpp b/trayicon.cpp new file mode 100644 index 00000000..452560fa --- /dev/null +++ b/trayicon.cpp @@ -0,0 +1,84 @@ +#include "trayicon.h" + +trayIcon::trayIcon(QMainWindow *parent) +{ + parentwin = parent; + + createActions(); + createTrayIcon(); + + sysTrayIcon->setIcon(QIcon(":/artwork/logo.png")); + + sysTrayIcon->show(); + + QObject::connect(sysTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); + +} + +void trayIcon::setVisible(bool visible) +{ + if(visible == true) { + parentwin->show(); + } else { + parentwin->hide(); + } +} + +void trayIcon::createActions() +{ + minimizeAction = new QAction(tr("Mi&nimize"), this); + connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); + + maximizeAction = new QAction(tr("Ma&ximize"), this); + connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); + + restoreAction = new QAction(tr("&Restore"), this); + connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); + + quitAction = new QAction(tr("&Quit"), this); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); +} + + +void trayIcon::createTrayIcon() +{ + trayIconMenu = new QMenu(this); + trayIconMenu->addAction(minimizeAction); + trayIconMenu->addAction(maximizeAction); + trayIconMenu->addAction(restoreAction); + trayIconMenu->addSeparator(); + trayIconMenu->addAction(quitAction); + + sysTrayIcon = new QSystemTrayIcon(this); + sysTrayIcon->setContextMenu(trayIconMenu); +} + +void trayIcon::showHideParent() +{ + if(parentwin->isVisible() == true) { + parentwin->hide(); + } else { + parentwin->show(); + } +} + +void trayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason) +{ + switch (reason) { + case QSystemTrayIcon::Trigger: + case QSystemTrayIcon::DoubleClick: + showHideParent(); + break; + case QSystemTrayIcon::MiddleClick: + showMessage("test", "test msg", 1000); + break; + default: + ; + } +} + +void trayIcon::showMessage(QString title, QString msg, int time) +{ + sysTrayIcon->showMessage(title, msg, QSystemTrayIcon::Information, time); +} diff --git a/trayicon.h b/trayicon.h new file mode 100644 index 00000000..3daa8dfe --- /dev/null +++ b/trayicon.h @@ -0,0 +1,39 @@ +#ifndef TRAYICON_H +#define TRAYICON_H + +#include +#include +#include +#include +#include +#include + +class trayIcon : public QWidget +{ + Q_OBJECT +public: + explicit trayIcon(QMainWindow *parent); + void showMessage(QString title, QString msg, int time); + void setVisible(bool visible); + +signals: + +public slots: + void showHideParent(); + void iconActivated(QSystemTrayIcon::ActivationReason reason); + +private: + void createActions(); + void createTrayIcon(); + + QAction *minimizeAction; + QAction *maximizeAction; + QAction *restoreAction; + QAction *quitAction; + + QSystemTrayIcon *sysTrayIcon; + QMenu *trayIconMenu; + QMainWindow *parentwin; +}; + +#endif // TRAYICON_H -- cgit v1.2.3 From 305627d005bfea0e45428c75c7794464a21e2183 Mon Sep 17 00:00:00 2001 From: Anne Jan Brouwer Date: Tue, 9 Jun 2015 17:42:49 +0200 Subject: correct icon --- trayicon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trayicon.cpp b/trayicon.cpp index 452560fa..ed6bf4b8 100644 --- a/trayicon.cpp +++ b/trayicon.cpp @@ -7,7 +7,7 @@ trayIcon::trayIcon(QMainWindow *parent) createActions(); createTrayIcon(); - sysTrayIcon->setIcon(QIcon(":/artwork/logo.png")); + sysTrayIcon->setIcon(QIcon(":/artwork/icon.png")); sysTrayIcon->show(); -- cgit v1.2.3 From aa7b5eb10ba0bd024d972b2c0486d04e2362b98b Mon Sep 17 00:00:00 2001 From: Anne Jan Brouwer Date: Tue, 9 Jun 2015 22:34:21 +0200 Subject: cleaned up and added config --- dialog.cpp | 47 +++++++++++++++++++++++++++++++++++++++ dialog.h | 5 +++++ dialog.ui | 16 +++++++++++++- mainwindow.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++--------- mainwindow.h | 9 ++++++-- trayicon.cpp | 20 ++++++++--------- trayicon.h | 6 ++--- 7 files changed, 146 insertions(+), 27 deletions(-) diff --git a/dialog.cpp b/dialog.cpp index da65c069..e5ae51ea 100644 --- a/dialog.cpp +++ b/dialog.cpp @@ -536,3 +536,50 @@ void Dialog::wizard() //ui->gpgPath->setText(gpg); } + +/** + * @brief Dialog::useTrayIcon + * @return + */ +bool Dialog::useTrayIcon() { + return ui->checkBoxUseTrayIcon->isChecked(); +} + +/** + * @brief Dialog::hideOnClose + * @return + */ +bool Dialog::hideOnClose() { + return ui->checkBoxHideOnClose->isEnabled() && ui->checkBoxHideOnClose->isChecked(); +} + +/** + * @brief Dialog::useTrayIcon + * @param useSystray + */ +void Dialog::useTrayIcon(bool useSystray) { + ui->checkBoxUseTrayIcon->setChecked(useSystray); + ui->checkBoxHideOnClose->setEnabled(useSystray); + if (!useSystray) { + ui->checkBoxHideOnClose->setChecked(false); + } +} + +/** + * @brief Dialog::hideOnClose + * @param hideOnClose + */ +void Dialog::hideOnClose(bool hideOnClose) { + ui->checkBoxHideOnClose->setChecked(hideOnClose); +} + +/** + * @brief Dialog::on_checkBoxUseTrayIcon_clicked + */ +void Dialog::on_checkBoxUseTrayIcon_clicked() { + if (ui->checkBoxUseTrayIcon->isChecked()) { + ui->checkBoxHideOnClose->setEnabled(true); + } else { + ui->checkBoxHideOnClose->setEnabled(false); + } +} diff --git a/dialog.h b/dialog.h index 9499c3ea..c5a5ae73 100644 --- a/dialog.h +++ b/dialog.h @@ -46,6 +46,10 @@ public: bool addGPGId(); void wizard(); void genKey(QString, QDialog *); + bool useTrayIcon(); + bool hideOnClose(); + void useTrayIcon(bool); + void hideOnClose(bool); private slots: void on_radioButtonNative_clicked(); @@ -59,6 +63,7 @@ private slots: void on_addButton_clicked(); void on_profileTable_currentItemChanged(QTableWidgetItem*); void on_deleteButton_clicked(); + void on_checkBoxUseTrayIcon_clicked(); private: QScopedPointer ui; diff --git a/dialog.ui b/dialog.ui index b69b56e9..a1168261 100644 --- a/dialog.ui +++ b/dialog.ui @@ -6,7 +6,7 @@ 0 0 - 491 + 495 501 @@ -269,6 +269,20 @@ + + + + Use TrayIcon + + + + + + + Hide on close + + + diff --git a/mainwindow.cpp b/mainwindow.cpp index e625d124..d9198efb 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef Q_OS_WIN #include #include @@ -37,8 +38,6 @@ MainWindow::MainWindow(QWidget *parent) : ui->statusBar->showMessage(tr("Welcome to QtPass %1").arg(VERSION), 2000); firstRun = true; startupPhase = true; - - initTrayIcon(); } /** @@ -180,6 +179,15 @@ bool MainWindow::checkConfig() { } } + useTrayIcon = settings.value("useTrayIcon").toBool(); + hideOnClose = settings.value("hideOnClose").toBool(); + + if (useTrayIcon && tray == NULL) { + initTrayIcon(); + } else if (!useTrayIcon && tray != NULL) { + destroyTrayIcon(); + } + firstRun = false; // TODO: this needs to be before we try to access the store, @@ -254,6 +262,8 @@ void MainWindow::config() { d->hidePassword(hidePassword); d->hideContent(hideContent); d->addGPGId(addGPGId); + d->useTrayIcon(useTrayIcon); + d->hideOnClose(hideOnClose); d->setProfiles(profiles, profile); d->wizard(); // does shit @@ -270,6 +280,8 @@ void MainWindow::config() { hidePassword = d->hidePassword(); hideContent = d->hideContent(); addGPGId = d->addGPGId(); + useTrayIcon = d->useTrayIcon(); + hideOnClose = d->hideOnClose(); profiles = d->getProfiles(); QSettings &settings(getSettings()); @@ -285,6 +297,9 @@ void MainWindow::config() { settings.setValue("hidePassword", hidePassword ? "true" : "false"); settings.setValue("hideContent", hideContent ? "true" : "false"); settings.setValue("addGPGId", addGPGId ? "true" : "false"); + settings.setValue("useTrayIcon", useTrayIcon ? "true" : "false"); + settings.setValue("hideOnClose", hideOnClose ? "true" : "false"); + if (!profiles.isEmpty()) { settings.beginGroup("profiles"); settings.remove(""); @@ -313,6 +328,11 @@ void MainWindow::config() { config(); } updateEnv(); + if (useTrayIcon && tray == NULL) { + initTrayIcon(); + } else if (!useTrayIcon && tray != NULL) { + destroyTrayIcon(); + } } firstRun = false; } @@ -1109,17 +1129,45 @@ void MainWindow::on_profileBox_currentIndexChanged(QString name) ui->treeView->setRootIndex(proxyModel.mapFromSource(model.setRootPath(passStore))); } +/** + * @brief MainWindow::initTrayIcon + */ void MainWindow::initTrayIcon() { - if(QSystemTrayIcon::isSystemTrayAvailable() == true) { - // Setup tray icon - this->tray = new trayIcon(this); + if(tray != NULL){ + qDebug() << "Creating tray icon again?"; + return; + } + if(QSystemTrayIcon::isSystemTrayAvailable() == true) { + // Setup tray icon + this->tray = new trayIcon(this); + if(tray == NULL){ + qDebug() << "Allocating tray icon failed."; + } + } else { + qDebug() << "No tray icon for this OS possibly also not show options?"; + } +} + +/** + * @brief MainWindow::destroyTrayIcon + */ +void MainWindow::destroyTrayIcon() +{ if(tray == NULL){ - qDebug() << "Allocating tray icon failed."; - exit(1); + qDebug() << "Destroy non existing tray icon?"; + return; + } + delete this->tray; + tray = NULL; +} + +void MainWindow::closeEvent(QCloseEvent *event) +{ + if (hideOnClose) { + this->hide(); + event->ignore(); + } else { + event->accept(); } - qDebug() << "Ehm.."; - } else { - qDebug() << "Whut?"; - } } diff --git a/mainwindow.h b/mainwindow.h index dbef6d5f..e8960ef9 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -47,6 +47,9 @@ public: void genKey(QString, QDialog *); void userDialog(QString = ""); +protected: + void closeEvent(QCloseEvent *event); + private slots: void on_updateButton_clicked(); void on_pushButton_clicked(); @@ -103,7 +106,9 @@ private: QHash profiles; QString profile; bool startupPhase; - trayIcon *tray; + trayIcon *tray = NULL; + bool useTrayIcon; + bool hideOnClose; void updateText(); void executePass(QString, QString = QString()); void executeWrapper(QString, QString, QString = QString()); @@ -122,7 +127,7 @@ private: void updateEnv(); void updateProfileBox(); void initTrayIcon(); - + void destroyTrayIcon(); }; #endif // MAINWINDOW_H diff --git a/trayicon.cpp b/trayicon.cpp index ed6bf4b8..8dace210 100644 --- a/trayicon.cpp +++ b/trayicon.cpp @@ -27,14 +27,14 @@ void trayIcon::setVisible(bool visible) void trayIcon::createActions() { - minimizeAction = new QAction(tr("Mi&nimize"), this); - connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); +// minimizeAction = new QAction(tr("Mi&nimize"), this); +// connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide())); - maximizeAction = new QAction(tr("Ma&ximize"), this); - connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); +// maximizeAction = new QAction(tr("Ma&ximize"), this); +// connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized())); - restoreAction = new QAction(tr("&Restore"), this); - connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); +// restoreAction = new QAction(tr("&Restore"), this); +// connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal())); quitAction = new QAction(tr("&Quit"), this); connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); @@ -44,10 +44,10 @@ void trayIcon::createActions() void trayIcon::createTrayIcon() { trayIconMenu = new QMenu(this); - trayIconMenu->addAction(minimizeAction); - trayIconMenu->addAction(maximizeAction); - trayIconMenu->addAction(restoreAction); - trayIconMenu->addSeparator(); +// trayIconMenu->addAction(minimizeAction); +// trayIconMenu->addAction(maximizeAction); +// trayIconMenu->addAction(restoreAction); +// trayIconMenu->addSeparator(); trayIconMenu->addAction(quitAction); sysTrayIcon = new QSystemTrayIcon(this); diff --git a/trayicon.h b/trayicon.h index 3daa8dfe..3ad4f00d 100644 --- a/trayicon.h +++ b/trayicon.h @@ -26,9 +26,9 @@ private: void createActions(); void createTrayIcon(); - QAction *minimizeAction; - QAction *maximizeAction; - QAction *restoreAction; +// QAction *minimizeAction; +// QAction *maximizeAction; +// QAction *restoreAction; QAction *quitAction; QSystemTrayIcon *sysTrayIcon; -- cgit v1.2.3 From a481e70404902106a0356d223ee5e303b775d434 Mon Sep 17 00:00:00 2001 From: Anne Jan Brouwer Date: Tue, 9 Jun 2015 23:21:09 +0200 Subject: missing comment --- mainwindow.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mainwindow.cpp b/mainwindow.cpp index d9198efb..6964dfc3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1162,6 +1162,10 @@ void MainWindow::destroyTrayIcon() tray = NULL; } +/** + * @brief MainWindow::closeEvent + * @param event + */ void MainWindow::closeEvent(QCloseEvent *event) { if (hideOnClose) { @@ -1171,3 +1175,4 @@ void MainWindow::closeEvent(QCloseEvent *event) event->accept(); } } + -- cgit v1.2.3