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

KDE's Doxygen guidelines are available online.