Akonadi

akapplication.h
1/*
2 SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
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
18class QApplication;
19class QGuiApplication;
20
21/**
22 * D-Bus session bus monitoring and command line handling.
23 */
25{
27public:
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 */
43
44 /** Forward to Q[Core]Application for convenience. */
45 int exec();
46
47protected:
48 AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory);
49 void init();
50
51 std::unique_ptr<QCoreApplication> mApp;
52
53private Q_SLOTS:
54 void pollSessionBus() const;
55
56private:
57 QString mInstanceId;
58 const QLoggingCategory &mLoggingCategory;
59 static AkApplicationBase *sInstance;
60
61 QCommandLineParser mCmdLineParser;
62};
63
64template<typename T>
65class AkApplicationImpl : public AkApplicationBase
66{
67public:
68 AkApplicationImpl(int &argc, char **argv, const QLoggingCategory &loggingCategory = *QLoggingCategory::defaultCategory())
69 : AkApplicationBase(std::make_unique<T>(argc, argv), loggingCategory)
70 {
71 init();
72 }
73};
74
75template<typename T>
76class AkUniqueApplicationImpl : public AkApplicationBase
77{
78public:
79 AkUniqueApplicationImpl(int &argc, char **argv, const QString &serviceName, const QLoggingCategory &loggingCategory = *QLoggingCategory::defaultCategory())
80 : AkApplicationBase(std::make_unique<T>(argc, argv), loggingCategory)
81 {
82 registerUniqueServiceOrTerminate(serviceName, loggingCategory);
83 init();
84 }
85
86private:
87 void registerUniqueServiceOrTerminate(const QString &serviceName, const QLoggingCategory &log)
88 {
89 auto bus = QDBusConnection::sessionBus();
90 if (!bus.isConnected()) {
91 qCCritical(log, "Session bus not found. Is DBus running?");
92 exit(1);
93 }
94
95 if (!bus.registerService(serviceName)) {
96 // We couldn't register. Most likely, it's already running.
97 const QString lastError = bus.lastError().message();
98 if (lastError.isEmpty()) {
99 qCInfo(log, "Service %s already registered, terminating now.", qUtf8Printable(serviceName));
100 exit(0); // already running, so it's OK. Terminate now.
101 } else {
102 qCCritical(log, "Unable to register service as %s due to an error: %s", qUtf8Printable(serviceName), qUtf8Printable(lastError));
103 exit(1); // :(
104 }
105 }
106 }
107};
108
109/**
110 * Returns the contents of @p name environment variable if it is defined,
111 * or @p defaultValue otherwise.
112 */
113QString akGetEnv(const char *name, const QString &defaultValue = QString());
114
115using AkCoreApplication = AkApplicationImpl<QCoreApplication>;
116using AkApplication = AkApplicationImpl<QApplication>;
117using AkGuiApplication = AkApplicationImpl<QGuiApplication>;
118using AkUniqueGuiApplication = AkUniqueApplicationImpl<QGuiApplication>;
D-Bus session bus monitoring and command line handling.
int exec()
Forward to Q[Core]Application for convenience.
static AkApplicationBase * instance()
Returns the AkApplication instance.
QDBusConnection sessionBus()
QLoggingCategory * defaultCategory()
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.