Akonadi

akapplication.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Volker Krause <[email protected]>
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 <akonadifull-version.h>
12 #include <private/instance_p.h>
13 
14 #include <QCoreApplication>
15 #include <QTimer>
16 
17 #include <iostream>
18 
19 AkApplicationBase *AkApplicationBase::sInstance = nullptr;
20 
21 AkApplicationBase::AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory)
22  : QObject(nullptr)
23  , mApp(std::move(app))
24  , mLoggingCategory(loggingCategory)
25 {
26  Q_ASSERT(!sInstance);
27  sInstance = this;
28 
29  QCoreApplication::setApplicationName(QStringLiteral("Akonadi"));
30  QCoreApplication::setApplicationVersion(QStringLiteral(AKONADI_FULL_VERSION));
31  mCmdLineParser.addHelpOption();
32  mCmdLineParser.addVersionOption();
33 }
34 
35 AkApplicationBase::~AkApplicationBase()
36 {
37 }
38 
40 {
41  Q_ASSERT(sInstance);
42  return sInstance;
43 }
44 
45 void AkApplicationBase::init()
46 {
47  akInit(mApp->applicationName());
48  akInitRemoteLog();
49 
50  if (!QDBusConnection::sessionBus().isConnected()) {
51  qFatal("D-Bus session bus is not available!");
52  }
53 
54  // there doesn't seem to be a signal to indicate that the session bus went down, so lets use polling for now
55  auto timer = new QTimer(this);
56  connect(timer, &QTimer::timeout, this, &AkApplicationBase::pollSessionBus);
57  timer->start(10 * 1000);
58 }
59 
60 void AkApplicationBase::setDescription(const QString &desc)
61 {
62  mCmdLineParser.setApplicationDescription(desc);
63 }
64 
65 void AkApplicationBase::parseCommandLine()
66 {
67  const QCommandLineOption instanceOption(QStringList() << QStringLiteral("instance"),
68  QStringLiteral("Namespace for starting multiple Akonadi instances in the same user session"),
69  QStringLiteral("name"));
70  mCmdLineParser.addOption(instanceOption);
71  const QCommandLineOption verboseOption(QStringLiteral("verbose"), QStringLiteral("Make Akonadi very chatty"));
72  mCmdLineParser.addOption(verboseOption);
73 
74  mCmdLineParser.process(QCoreApplication::arguments());
75 
76  if (mCmdLineParser.isSet(instanceOption)) {
77  Akonadi::Instance::setIdentifier(mCmdLineParser.value(instanceOption));
78  }
79  if (mCmdLineParser.isSet(verboseOption)) {
80  akMakeVerbose(mLoggingCategory.categoryName());
81  }
82 }
83 
84 void AkApplicationBase::pollSessionBus() const
85 {
86  if (!QDBusConnection::sessionBus().isConnected()) {
87  qCritical("D-Bus session bus went down - quitting");
88  mApp->quit();
89  }
90 }
91 
92 void AkApplicationBase::addCommandLineOptions(const QCommandLineOption &option)
93 {
94  mCmdLineParser.addOption(option);
95 }
96 
97 void AkApplicationBase::addPositionalCommandLineOption(const QString &name, const QString &description, const QString &syntax)
98 {
99  mCmdLineParser.addPositionalArgument(name, description, syntax);
100 }
101 
102 void AkApplicationBase::printUsage() const
103 {
104  std::cout << qPrintable(mCmdLineParser.helpText()) << std::endl;
105 }
106 
108 {
109  return mApp->exec();
110 }
111 
112 QString akGetEnv(const char *name, const QString &defaultValue)
113 {
114  const QString v = QString::fromLocal8Bit(qgetenv(name));
115  return !v.isEmpty() ? v : defaultValue;
116 }
void setApplicationVersion(const QString &version)
int exec()
Forward to Q[Core]Application for convenience.
QDBusConnection sessionBus()
QString fromLocal8Bit(const char *str, int size)
void timeout()
bool isEmpty() const const
static AkApplicationBase * instance()
Returns the AkApplication instance.
void setApplicationName(const QString &application)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
D-Bus session bus monitoring and command line handling.
Definition: akapplication.h:24
A glue between Qt and the standard library.
QStringList arguments()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.