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 <private/instance_p.h>
12 #include <akonadifull-version.h>
13 
14 #include <QDBusConnection>
15 #include <QTimer>
16 #include <QCoreApplication>
17 
18 #include <iostream>
19 
20 AkApplicationBase *AkApplicationBase::sInstance = nullptr;
21 
22 AkApplicationBase::AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory)
23  : QObject(nullptr)
24  , mApp(std::move(app))
25  , mLoggingCategory(loggingCategory)
26 {
27  Q_ASSERT(!sInstance);
28  sInstance = this;
29 
30  QCoreApplication::setApplicationName(QStringLiteral("Akonadi"));
31  QCoreApplication::setApplicationVersion(QStringLiteral(AKONADI_FULL_VERSION));
32  mCmdLineParser.addHelpOption();
33  mCmdLineParser.addVersionOption();
34 }
35 
36 AkApplicationBase::~AkApplicationBase()
37 {
38 }
39 
41 {
42  Q_ASSERT(sInstance);
43  return sInstance;
44 }
45 
46 void AkApplicationBase::init()
47 {
48  akInit(mApp->applicationName());
49  akInitRemoteLog();
50 
51  if (!QDBusConnection::sessionBus().isConnected()) {
52  qFatal("D-Bus session bus is not available!");
53  }
54 
55  // there doesn't seem to be a signal to indicate that the session bus went down, so lets use polling for now
56  QTimer *timer = new QTimer(this);
57  connect(timer, &QTimer::timeout, this, &AkApplicationBase::pollSessionBus);
58  timer->start(10 * 1000);
59 }
60 
61 void AkApplicationBase::setDescription(const QString &desc)
62 {
63  mCmdLineParser.setApplicationDescription(desc);
64 }
65 
66 void AkApplicationBase::parseCommandLine()
67 {
68  const QCommandLineOption instanceOption(QStringList() << QStringLiteral("instance"),
69  QStringLiteral("Namespace for starting multiple Akonadi instances in the same user session"),
70  QStringLiteral("name"));
71  mCmdLineParser.addOption(instanceOption);
72  const QCommandLineOption verboseOption(QStringLiteral("verbose"),
73  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 
86 void AkApplicationBase::pollSessionBus() const
87 {
88  if (!QDBusConnection::sessionBus().isConnected()) {
89  qCritical("D-Bus session bus went down - quitting");
90  mApp->quit();
91  }
92 }
93 
94 void AkApplicationBase::addCommandLineOptions(const QCommandLineOption &option)
95 {
96  mCmdLineParser.addOption(option);
97 }
98 
99 void AkApplicationBase::addPositionalCommandLineOption(const QString &name, const QString &description, const QString &syntax)
100 {
101  mCmdLineParser.addPositionalArgument(name, description, syntax);
102 }
103 
104 void AkApplicationBase::printUsage() const
105 {
106  std::cout << qPrintable(mCmdLineParser.helpText()) << std::endl;
107 }
108 
110 {
111  return mApp->exec();
112 }
113 
114 QString akGetEnv(const char *name, const QString &defaultValue)
115 {
116  const QString v = QString::fromLocal8Bit(qgetenv(name));
117  return !v.isEmpty() ? v : defaultValue;
118 }
D-Bus session bus monitoring and command line handling.
Definition: akapplication.h:25
QDBusConnection sessionBus()
A glue between Qt and the standard library.
int exec()
Forward to Q[Core]Application for convenience.
void timeout()
QString fromLocal8Bit(const char *str, int size)
static AkApplicationBase * instance()
Returns the AkApplication instance.
void setApplicationVersion(const QString &version)
bool isEmpty() const const
void start(int msec)
KIOCORE_EXPORT CopyJob * move(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QStringList arguments()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setApplicationName(const QString &application)
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.