Line data Source code
1 : // SPDX-FileCopyrightText: 2015 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief Implements TrayIcon; the tray is only set up when
6 : * QSystemTrayIcon::isSystemTrayAvailable() says there is one.
7 : */
8 : #include "trayicon.h"
9 : #include <QAction>
10 : #include <QApplication>
11 : #include <QMainWindow>
12 : #include <QMenu>
13 :
14 : #include "qtpasslogging.h"
15 :
16 18 : TrayIcon::TrayIcon(QMainWindow *parent) : QObject(parent) {
17 18 : parentwin = parent;
18 :
19 18 : if (QSystemTrayIcon::isSystemTrayAvailable()) {
20 6 : createActions();
21 6 : createTrayIcon();
22 :
23 6 : sysTrayIcon->setIcon(
24 18 : QIcon::fromTheme("qtpass-tray", QIcon(":/artwork/icon.png")));
25 :
26 6 : sysTrayIcon->show();
27 :
28 6 : QObject::connect(sysTrayIcon, &QSystemTrayIcon::activated, this,
29 6 : &TrayIcon::iconActivated);
30 :
31 6 : isAllocated = true;
32 : } else {
33 13 : qCDebug(lcQtPass)
34 1 : << "No tray icon for this OS possibly also not show options?";
35 : }
36 18 : }
37 :
38 6 : auto TrayIcon::getIsAllocated() -> bool { return isAllocated; }
39 :
40 6 : void TrayIcon::createActions() {
41 6 : showAction = new QAction(tr("&Show"), this);
42 6 : connect(showAction, &QAction::triggered, parentwin, &QWidget::show);
43 12 : hideAction = new QAction(tr("&Hide"), this);
44 6 : connect(hideAction, &QAction::triggered, parentwin, &QWidget::hide);
45 :
46 12 : minimizeAction = new QAction(tr("Mi&nimize"), this);
47 6 : connect(minimizeAction, &QAction::triggered, parentwin,
48 6 : &QWidget::showMinimized);
49 12 : maximizeAction = new QAction(tr("Ma&ximize"), this);
50 6 : connect(maximizeAction, &QAction::triggered, parentwin,
51 6 : &QWidget::showMaximized);
52 12 : restoreAction = new QAction(tr("&Restore"), this);
53 6 : connect(restoreAction, &QAction::triggered, parentwin, &QWidget::showNormal);
54 :
55 12 : quitAction = new QAction(tr("&Quit"), this);
56 6 : connect(quitAction, &QAction::triggered, QApplication::instance(),
57 6 : &QApplication::quit);
58 6 : }
59 :
60 6 : void TrayIcon::createTrayIcon() {
61 : // QMenu needs a widget parent; the main window owns the menu, this object
62 : // owns the tray icon.
63 6 : trayIconMenu = new QMenu(parentwin);
64 6 : trayIconMenu->addAction(showAction);
65 6 : trayIconMenu->addAction(hideAction);
66 6 : trayIconMenu->addAction(minimizeAction);
67 6 : trayIconMenu->addAction(maximizeAction);
68 6 : trayIconMenu->addAction(restoreAction);
69 6 : quitSeparator = trayIconMenu->addSeparator();
70 6 : trayIconMenu->addAction(quitAction);
71 :
72 6 : sysTrayIcon = new QSystemTrayIcon(this);
73 6 : sysTrayIcon->setContextMenu(trayIconMenu);
74 6 : }
75 :
76 2 : void TrayIcon::addToggle(QAction *action) {
77 2 : if (trayIconMenu == nullptr) {
78 : return;
79 : }
80 1 : trayIconMenu->insertSeparator(quitSeparator);
81 1 : trayIconMenu->insertAction(quitSeparator, action);
82 : }
83 :
84 6 : void TrayIcon::showHideParent() {
85 6 : if (parentwin->isVisible()) {
86 4 : parentwin->hide();
87 : } else {
88 2 : parentwin->show();
89 : }
90 6 : }
91 :
92 8 : void TrayIcon::iconActivated(QSystemTrayIcon::ActivationReason reason) {
93 8 : switch (reason) {
94 4 : case QSystemTrayIcon::Trigger:
95 : case QSystemTrayIcon::DoubleClick:
96 4 : showHideParent();
97 4 : break;
98 : case QSystemTrayIcon::MiddleClick:
99 : break;
100 8 : default: {
101 : }
102 : }
103 8 : }
|