Akonadi

akapplication.h
1 /*
2  SPDX-FileCopyrightText: 2008 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef AKAPPLICATION_H
8 #define AKAPPLICATION_H
9 
10 #include <QObject>
11 #include <QCommandLineParser>
12 #include <QLoggingCategory>
13 #include <QDBusConnection>
14 #include <QDBusError>
15 
16 #include <memory>
17 
18 class QCoreApplication;
19 class QApplication;
20 class QGuiApplication;
21 
25 class AkApplicationBase : public QObject
26 {
27  Q_OBJECT
28 public:
30  void parseCommandLine();
31  void setDescription(const QString &desc);
32 
33  void addCommandLineOptions(const QCommandLineOption &option);
34  void addPositionalCommandLineOption(const QString &name, const QString &description = QString(), const QString &syntax = QString());
35  const QCommandLineParser &commandLineArguments() const
36  {
37  return mCmdLineParser;
38  }
39 
40  void printUsage() const;
41 
43  static AkApplicationBase *instance();
44 
46  int exec();
47 
48 protected:
49  AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory);
50  void init();
51 
52  std::unique_ptr<QCoreApplication> mApp;
53 
54 private Q_SLOTS:
55  void pollSessionBus() const;
56 
57 private:
58  QString mInstanceId;
59  const QLoggingCategory &mLoggingCategory;
60  static AkApplicationBase *sInstance;
61 
62  QCommandLineParser mCmdLineParser;
63 };
64 
65 template <typename T>
66 class AkApplicationImpl : public AkApplicationBase
67 {
68 public:
69  AkApplicationImpl(int &argc, char **argv, const QLoggingCategory &loggingCategory = *QLoggingCategory::defaultCategory())
70  : AkApplicationBase(std::make_unique<T>(argc, argv), loggingCategory)
71  {
72  init();
73  }
74 };
75 
76 template<typename T>
77 class AkUniqueApplicationImpl : public AkApplicationBase
78 {
79 public:
80  AkUniqueApplicationImpl(int &argc, char **argv, const QString &serviceName, const QLoggingCategory &loggingCategory = *QLoggingCategory::defaultCategory())
81  : AkApplicationBase(std::make_unique<T>(argc, argv), loggingCategory)
82  {
83  registerUniqueServiceOrTerminate(serviceName, loggingCategory);
84  init();
85  }
86 
87 private:
88  void registerUniqueServiceOrTerminate(const QString &serviceName, const QLoggingCategory &log)
89  {
90  auto bus = QDBusConnection::sessionBus();
91  if (!bus.isConnected()) {
92  qCCritical(log, "Session bus not found. Is DBus running?");
93  exit(1);
94  }
95 
96  if (!bus.registerService(serviceName)) {
97  // We couldn't register. Most likely, it's already running.
98  const QString lastError = bus.lastError().message();
99  if (lastError.isEmpty()) {
100  qCInfo(log, "Service %s already registered, terminating now.", qUtf8Printable(serviceName));
101  exit(0); // already running, so it's OK. Terminate now.
102  } else {
103  qCCritical(log, "Unable to register service as %s due to an error: %s", qUtf8Printable(serviceName), qUtf8Printable(lastError));
104  exit(1); // :(
105  }
106  }
107  }
108 };
109 
114 QString akGetEnv(const char *name, const QString &defaultValue = QString());
115 
116 typedef AkApplicationImpl<QCoreApplication> AkCoreApplication;
117 typedef AkApplicationImpl<QApplication> AkApplication;
118 typedef AkApplicationImpl<QGuiApplication> AkGuiApplication;
119 typedef AkUniqueApplicationImpl<QGuiApplication> AkUniqueGuiApplication;
120 
121 #endif
D-Bus session bus monitoring and command line handling.
Definition: akapplication.h:25
QDBusConnection sessionBus()
int exec()
Forward to Q[Core]Application for convenience.
static AkApplicationBase * instance()
Returns the AkApplication instance.
Q_OBJECTQ_OBJECT
bool isEmpty() const const
QCA_EXPORT void init()
QLoggingCategory * defaultCategory()
Q_SLOTSQ_SLOTS
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 23:16:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.