File indexing completed on 2024-06-16 04:50:34

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "akapplication.h"
0008 #include "akdebug.h"
0009 #include "akremotelog.h"
0010 
0011 #include "private/instance_p.h"
0012 #include <akonadifull-version.h>
0013 
0014 #include <KLocalizedString>
0015 
0016 #include <QCoreApplication>
0017 #include <QTimer>
0018 
0019 #include <iostream>
0020 
0021 AkApplicationBase *AkApplicationBase::sInstance = nullptr;
0022 
0023 AkApplicationBase::AkApplicationBase(std::unique_ptr<QCoreApplication> app, const QLoggingCategory &loggingCategory)
0024     : QObject(nullptr)
0025     , mApp(std::move(app))
0026     , mLoggingCategory(loggingCategory)
0027 {
0028     Q_ASSERT(!sInstance);
0029     sInstance = this;
0030 
0031     QCoreApplication::setApplicationName(QStringLiteral("Akonadi"));
0032     QCoreApplication::setApplicationVersion(QStringLiteral(AKONADI_FULL_VERSION));
0033     mCmdLineParser.addHelpOption();
0034     mCmdLineParser.addVersionOption();
0035 }
0036 
0037 AkApplicationBase::~AkApplicationBase()
0038 {
0039 }
0040 
0041 AkApplicationBase *AkApplicationBase::instance()
0042 {
0043     Q_ASSERT(sInstance);
0044     return sInstance;
0045 }
0046 
0047 void AkApplicationBase::init()
0048 {
0049     akInit(mApp->applicationName());
0050     akInitRemoteLog();
0051 
0052     if (!QDBusConnection::sessionBus().isConnected()) {
0053         qFatal("D-Bus session bus is not available!");
0054     }
0055 
0056     // there doesn't seem to be a signal to indicate that the session bus went down, so lets use polling for now
0057     auto timer = new QTimer(this);
0058     connect(timer, &QTimer::timeout, this, &AkApplicationBase::pollSessionBus);
0059     timer->start(10 * 1000);
0060 }
0061 
0062 void AkApplicationBase::setDescription(const QString &desc)
0063 {
0064     mCmdLineParser.setApplicationDescription(desc);
0065 }
0066 
0067 void AkApplicationBase::parseCommandLine()
0068 {
0069     const QCommandLineOption instanceOption(QStringList() << QStringLiteral("instance"),
0070                                             i18n("Namespace for starting multiple Akonadi instances in the same user session"),
0071                                             QStringLiteral("name"));
0072     mCmdLineParser.addOption(instanceOption);
0073     const QCommandLineOption verboseOption(QStringLiteral("verbose"), QStringLiteral("Make Akonadi very chatty"));
0074     mCmdLineParser.addOption(verboseOption);
0075 
0076     mCmdLineParser.process(QCoreApplication::arguments());
0077 
0078     if (mCmdLineParser.isSet(instanceOption)) {
0079         Akonadi::Instance::setIdentifier(mCmdLineParser.value(instanceOption));
0080     }
0081     if (mCmdLineParser.isSet(verboseOption)) {
0082         akMakeVerbose(mLoggingCategory.categoryName());
0083     }
0084 }
0085 
0086 void AkApplicationBase::pollSessionBus() const
0087 {
0088     if (!QDBusConnection::sessionBus().isConnected()) {
0089         qCritical("D-Bus session bus went down - quitting");
0090         mApp->quit();
0091     }
0092 }
0093 
0094 void AkApplicationBase::addCommandLineOptions(const QCommandLineOption &option)
0095 {
0096     mCmdLineParser.addOption(option);
0097 }
0098 
0099 void AkApplicationBase::addPositionalCommandLineOption(const QString &name, const QString &description, const QString &syntax)
0100 {
0101     mCmdLineParser.addPositionalArgument(name, description, syntax);
0102 }
0103 
0104 void AkApplicationBase::printUsage() const
0105 {
0106     std::cout << qPrintable(mCmdLineParser.helpText()) << std::endl;
0107 }
0108 
0109 int AkApplicationBase::exec()
0110 {
0111     return mApp->exec();
0112 }
0113 
0114 QString akGetEnv(const char *name, const QString &defaultValue)
0115 {
0116     const QString v = QString::fromLocal8Bit(qgetenv(name));
0117     return !v.isEmpty() ? v : defaultValue;
0118 }
0119 
0120 #include "moc_akapplication.cpp"