Akonadi

akapplication.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "akapplication.h"
8#include "akdebug.h"
9#include "akremotelog.h"
10
11#include "private/instance_p.h"
12#include <akonadifull-version.h>
13
14#include <KLocalizedString>
15
16#include <QCoreApplication>
17#include <QTimer>
18
19#include <iostream>
20
21AkApplicationBase *AkApplicationBase::sInstance = nullptr;
22
23AkApplicationBase::AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory)
24 : QObject(nullptr)
25 , mApp(std::move(app))
26 , mLoggingCategory(loggingCategory)
27{
28 Q_ASSERT(!sInstance);
29 sInstance = this;
30
31 QCoreApplication::setApplicationName(QStringLiteral("Akonadi"));
32 QCoreApplication::setApplicationVersion(QStringLiteral(AKONADI_FULL_VERSION));
33 mCmdLineParser.addHelpOption();
34 mCmdLineParser.addVersionOption();
35}
36
37AkApplicationBase::~AkApplicationBase()
38{
39}
40
42{
43 Q_ASSERT(sInstance);
44 return sInstance;
45}
46
47void AkApplicationBase::init()
48{
49 akInit(mApp->applicationName());
50 akInitRemoteLog();
51
52 if (!QDBusConnection::sessionBus().isConnected()) {
53 qFatal("D-Bus session bus is not available!");
54 }
55
56 // there doesn't seem to be a signal to indicate that the session bus went down, so lets use polling for now
57 auto timer = new QTimer(this);
58 connect(timer, &QTimer::timeout, this, &AkApplicationBase::pollSessionBus);
59 timer->start(10 * 1000);
60}
61
62void AkApplicationBase::setDescription(const QString &desc)
63{
64 mCmdLineParser.setApplicationDescription(desc);
65}
66
67void AkApplicationBase::parseCommandLine()
68{
69 const QCommandLineOption instanceOption(QStringList() << QStringLiteral("instance"),
70 i18n("Namespace for starting multiple Akonadi instances in the same user session"),
71 QStringLiteral("name"));
72 mCmdLineParser.addOption(instanceOption);
73 const QCommandLineOption verboseOption(QStringLiteral("verbose"), QStringLiteral("Make Akonadi very chatty"));
74 mCmdLineParser.addOption(verboseOption);
75
76 mCmdLineParser.process(QCoreApplication::arguments());
77
78 if (mCmdLineParser.isSet(instanceOption)) {
79 Akonadi::Instance::setIdentifier(mCmdLineParser.value(instanceOption));
80 }
81 if (mCmdLineParser.isSet(verboseOption)) {
82 akMakeVerbose(mLoggingCategory.categoryName());
83 }
84}
85
86void AkApplicationBase::pollSessionBus() const
87{
88 if (!QDBusConnection::sessionBus().isConnected()) {
89 qCritical("D-Bus session bus went down - quitting");
90 mApp->quit();
91 }
92}
93
94void AkApplicationBase::addCommandLineOptions(const QCommandLineOption &option)
95{
96 mCmdLineParser.addOption(option);
97}
98
99void AkApplicationBase::addPositionalCommandLineOption(const QString &name, const QString &description, const QString &syntax)
100{
101 mCmdLineParser.addPositionalArgument(name, description, syntax);
102}
103
104void AkApplicationBase::printUsage() const
105{
106 std::cout << qPrintable(mCmdLineParser.helpText()) << std::endl;
107}
108
110{
111 return mApp->exec();
112}
113
114QString akGetEnv(const char *name, const QString &defaultValue)
115{
116 const QString v = QString::fromLocal8Bit(qgetenv(name));
117 return !v.isEmpty() ? v : defaultValue;
118}
119
120#include "moc_akapplication.cpp"
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.
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
bool addOption(const QCommandLineOption &option)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QString helpText() const const
bool isSet(const QCommandLineOption &option) const const
void process(const QCoreApplication &app)
void setApplicationDescription(const QString &description)
QString value(const QCommandLineOption &option) const const
void setApplicationName(const QString &application)
void setApplicationVersion(const QString &version)
QStringList arguments()
QDBusConnection sessionBus()
const char * categoryName() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLocal8Bit(QByteArrayView str)
bool isEmpty() const const
void timeout()
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.