1 /*
2  * Copyright (c) 2017-2018 Nitrokey UG
3  *
4  * This file is part of Nitrokey App.
5  *
6  * Nitrokey App is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * Nitrokey App is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Nitrokey App. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * SPDX-License-Identifier: GPL-3.0
20  */
21 
22 #ifndef NITROKEYAPP_TRAY_H
23 #define NITROKEYAPP_TRAY_H
24 
25 #define TRAY_MSG_TIMEOUT 5000
26 
27 #include <QObject>
28 enum trayMessageType { INFORMATION, WARNING, CRITICAL };
29 
30 namespace Ui {
31     class Tray;
32 }
33 
34 #include <QMutex>
35 class tray_Worker : public QObject
36 {
37 Q_OBJECT
38 
39 public:
40     QMutex mtx;
41 
42 public slots:
43     void doWork();
44 
45 signals:
46     void resultReady();
47     void progress(int i);
48 };
49 
50 #include <QSystemTrayIcon>
51 #include <QAction>
52 #include <memory>
53 #include <QtWidgets/QMainWindow>
54 #include "StorageActions.h"
55 
56 class Tray : public QObject {
57 Q_OBJECT
58 
59 
60 public:
61   void setAdmin_mode(bool _admin_mode);
62   void setDebug_mode(bool _debug_mode);
63   Tray(QObject * _parent, bool _debug_mode, bool _extended_config, StorageActions *actions);
64     virtual ~Tray();
65     void showTrayMessage(QString message);
66     void showTrayMessage(const QString &title, const QString &msg, enum trayMessageType type = INFORMATION,
67                          int timeout = TRAY_MSG_TIMEOUT);
68   public slots:
69     void regenerateMenu();
70     void updateOperationInProgressBar(int p);
71 
72   signals:
73     void progress(int i);
74 
75 
76 private slots:
77     void iconActivated(QSystemTrayIcon::ActivationReason reason);
78     void generateMenuPasswordSafe();
79     void populateOTPPasswordMenu();
80     void passOTPProgressFurther(int i);
81     void showOTPProgressInTray(int i);
82     void delayedShowIndicator();
83 
84 private:
85     Q_DISABLE_COPY(Tray);
86     virtual bool eventFilter(QObject *obj, QEvent *event) override;
87     void generateMenu(bool init=false, std::function<void(QMenu *)> run_before = [](QMenu*){});
88     void generateMenuForStorageDevice();
89     void generatePasswordMenu();
90     void generateMenuForProDevice();
91     void initActionsForStick10();
92     void initActionsForStick20();
93     void initCommonActions();
94     int UpdateDynamicMenuEntrys(void);
95     void createIndicator();
96 
97     QObject *main_window;
98     QObject *storageActions;
99     bool debug_mode;
100     bool ExtendedConfigActive;
101 
102     QSystemTrayIcon *trayIcon;
103     std::shared_ptr<QMenu> trayMenu;
104     std::shared_ptr<QMenu> windowMenu;
105     QMenu *trayMenuSubConfigure;
106     std::shared_ptr<QMenu> trayMenuPasswdSubMenu;
107 //    QMenu *trayMenuTOTPSubMenu;
108 //    QMenu *trayMenuHOTPSubMenu;
109     QMenu *trayMenuSubSpecialConfigure;
110     QMenuBar* file_menu;
111     QTimer *delayedShowTimer;
112 public:
113   void setFile_menu(QMenuBar *file_menu);
114 
115 private:
116 
117   bool long_operation_in_progress = false;
118   QAction *quitAction;
119     QAction *configureAction;
120     QAction *resetAction;
121     QAction *configureActionStick20;
122     QAction *DebugAction;
123     QAction *ShowWindowAction;
124     QAction *ActionAboutDialog;
125     QAction *ActionHelp;
126 //    QAction *SecPasswordAction;
127 //    QAction *Stick20SetupAction;
128     QAction *Stick10ActionChangeUserPIN;
129     QAction *Stick10ActionChangeAdminPIN;
130     QAction *LockDeviceAction;
131     QAction *UnlockPasswordSafeAction;
132 
133     QAction *Stick20ActionEnableCryptedVolume;
134     QAction *Stick20ActionDisableCryptedVolume;
135     QAction *Stick20ActionEnableHiddenVolume;
136     QAction *Stick20ActionDisableHiddenVolume;
137     QAction *Stick20ActionChangeUserPIN;
138     QAction *Stick20ActionChangeAdminPIN;
139     QAction *Stick20ActionChangeUpdatePIN;
140     QAction *Stick20ActionEnableFirmwareUpdate;
141     QAction *Stick20ActionExportFirmwareToFile;
142     QAction *Stick20ActionDestroyCryptedVolume;
143     QAction *Stick20ActionInitCryptedVolume;
144     QAction *Stick20ActionFillSDCardWithRandomChars;
145     QAction *Stick20ActionGetStickStatus;
146     QAction *Stick20ActionUpdateStickStatus;
147     QAction *Stick20ActionSetReadonlyUncryptedVolume;
148     QAction *Stick20ActionSetReadWriteUncryptedVolume;
149     QAction *Stick20ActionDebugAction;
150     QAction *Stick20ActionSetupHiddenVolume;
151     QAction *Stick20ActionClearNewSDCardFound;
152     QAction *Stick20ActionLockStickHardware;
153     QAction *Stick20ActionResetUserPassword;
154     QAction *Stick20ActionSetReadonlyEncryptedVolume;
155     QAction *Stick20ActionSetReadWriteEncryptedVolume;
156 
157   void destroyThread();
158 
159   tray_Worker *worker;
160   QSignalMapper *mapper_TOTP;
161   QSignalMapper *mapper_HOTP;
162   QSignalMapper *mapper_PWS;
163 
164   QAction *quitAction_tray;
165   QAction *ActionHelp_tray;
166   QAction *ActionAboutDialog_tray;
167   QAction *Stick20ActionEnableCryptedVolume_tray;
168   QAction *Stick20ActionDisableCryptedVolume_tray;
169   QAction *Stick20ActionEnableHiddenVolume_tray;
170   QAction *Stick20ActionDisableHiddenVolume_tray;
171   std::shared_ptr<QMenu> trayMenuPasswdSubMenu_tray;
172   QAction *UnlockPasswordSafeAction_tray;
173 };
174 
175 
176 #endif //NITROKEYAPP_TRAY_H
177