1 /*
2  *  Copyright (C) 2012 Tobias Tangemann
3  *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
4  *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
5  *
6  *  This program 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 2 or (at your option)
9  *  version 3 of the License.
10  *
11  *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef KEEPASSX_APPLICATION_H
21 #define KEEPASSX_APPLICATION_H
22 
23 #include <QApplication>
24 #include <QtNetwork/QLocalServer>
25 
26 #if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
27 #include <QScopedPointer>
28 
29 class OSEventFilter;
30 #endif
31 class QLockFile;
32 class QSocketNotifier;
33 
34 constexpr int RESTART_EXITCODE = -1;
35 
36 class Application : public QApplication
37 {
38     Q_OBJECT
39 
40 public:
41     Application(int& argc, char** argv);
42     ~Application() override;
43 
44     void applyTheme();
45 
46     bool event(QEvent* event) override;
47     bool isAlreadyRunning() const;
48     bool isDarkTheme() const;
49 
50     bool sendFileNamesToRunningInstance(const QStringList& fileNames);
51     bool sendLockToInstance();
52 
53     void restart();
54 
55 signals:
56     void openFile(const QString& filename);
57     void anotherInstanceStarted();
58     void applicationActivated();
59     void quitSignalReceived();
60 
61 private slots:
62 #if defined(Q_OS_UNIX)
63     void quitBySignal();
64 #endif
65     void processIncomingConnection();
66     void socketReadyRead();
67 
68 private:
69 #if defined(Q_OS_UNIX)
70     /**
71      * Register Unix signals such as SIGINT and SIGTERM for clean shutdown.
72      */
73     void registerUnixSignals();
74     QSocketNotifier* m_unixSignalNotifier;
75     static void handleUnixSignal(int sig);
76     static int unixSignalSocket[2];
77 #endif
78     bool m_alreadyRunning;
79     bool m_darkTheme = false;
80     QLockFile* m_lockFile;
81     QLocalServer m_lockServer;
82     QString m_socketName;
83 #if defined(Q_OS_WIN) || (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
84     QScopedPointer<OSEventFilter> m_osEventFilter;
85 #endif
86 };
87 
88 #define kpxcApp qobject_cast<Application*>(Application::instance())
89 
90 #endif // KEEPASSX_APPLICATION_H
91