1 | /* |
2 | Copyright (c) 2008 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #ifndef AKAPPLICATION_H |
21 | #define AKAPPLICATION_H |
22 | |
23 | #include <QtCore/QObject> |
24 | |
25 | #ifndef Q_MOC_RUN |
26 | #include <boost/program_options.hpp> |
27 | #endif |
28 | |
29 | class QCoreApplication; |
30 | class QApplication; |
31 | |
32 | /** |
33 | * D-Bus session bus monitoring and command line handling. |
34 | */ |
35 | class AkApplication : public QObject |
36 | { |
37 | Q_OBJECT |
38 | public: |
39 | ~AkApplication(); |
40 | void parseCommandLine(); |
41 | void setDescription(const QString &desc) |
42 | { |
43 | mDescription = desc; |
44 | } |
45 | |
46 | void addCommandLineOptions(const boost::program_options::options_description &desc); |
47 | void addPositionalCommandLineOption(const char *option, int count); |
48 | const boost::program_options::variables_map &commandLineArguments() const |
49 | { |
50 | return mCmdLineArguments; |
51 | } |
52 | |
53 | void printUsage() const; |
54 | |
55 | /** Returns the instance identifier when running in multi-instance mode, empty string otherwise. */ |
56 | static QString instanceIdentifier(); |
57 | |
58 | /** Returns @c true if we run in multi-instance mode. */ |
59 | static bool hasInstanceIdentifier(); |
60 | |
61 | /** Returns the AkApplication instance */ |
62 | static AkApplication *instance(); |
63 | |
64 | /** Forward to Q[Core]Application for convenience. */ |
65 | int exec(); |
66 | |
67 | protected: |
68 | AkApplication(int &argc, char **argv); |
69 | void init(); |
70 | QScopedPointer<QCoreApplication> mApp; |
71 | |
72 | private: |
73 | /** Change instane identifier, for unit tests only. */ |
74 | static void setInstanceIdentifier(const QString &instanceId); |
75 | friend void akTestSetInstanceIdentifier(const QString &instanceId); |
76 | |
77 | private Q_SLOTS: |
78 | void pollSessionBus() const; |
79 | |
80 | private: |
81 | int mArgc; |
82 | char **mArgv; |
83 | QString mDescription; |
84 | QString mInstanceId; |
85 | static AkApplication *sInstance; |
86 | |
87 | boost::program_options::options_description mCmdLineOptions; |
88 | boost::program_options::variables_map mCmdLineArguments; |
89 | boost::program_options::positional_options_description mCmdPositionalOptions; |
90 | }; |
91 | |
92 | template <typename T> |
93 | class AkApplicationImpl : public AkApplication |
94 | { |
95 | public: |
96 | AkApplicationImpl(int &argc, char **argv) |
97 | : AkApplication(argc, argv) |
98 | { |
99 | mApp.reset(new T(argc, argv)); |
100 | init(); |
101 | } |
102 | }; |
103 | |
104 | typedef AkApplicationImpl<QCoreApplication> AkCoreApplication; |
105 | typedef AkApplicationImpl<QApplication> AkGuiApplication; |
106 | |
107 | #endif |
108 | |