File indexing completed on 2024-04-28 05:36:15

0001 /*
0002  *   SPDX-FileCopyrightText: 2010 Dario Freddi <drf@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "powerdevilapp.h"
0008 
0009 #include "powerdevilfdoconnector.h"
0010 #include "powermanagementadaptor.h"
0011 #include "powermanagementpolicyagentadaptor.h"
0012 
0013 #include "powerdevil_debug.h"
0014 #include "powerdevil_version.h"
0015 #include "powerdevilcore.h"
0016 #include "../osd/osd.h"
0017 
0018 #include <QAction>
0019 #include <QDBusConnection>
0020 #include <QDBusConnectionInterface>
0021 #include <QDebug>
0022 #include <QFileInfo>
0023 #include <QKeySequence>
0024 #include <QSessionManager>
0025 #include <QTimer>
0026 
0027 #include <KAboutData>
0028 #include <KCrash>
0029 #include <KDBusService>
0030 #include <KLocalizedString>
0031 
0032 #include <KActionCollection>
0033 #include <KGlobalAccel>
0034 
0035 #include <qnamespace.h>
0036 
0037 PowerDevilApp::PowerDevilApp(int &argc, char **argv)
0038     : QGuiApplication(argc, argv)
0039     , m_core(nullptr)
0040 {
0041 }
0042 
0043 PowerDevilApp::~PowerDevilApp()
0044 {
0045     delete m_core;
0046 }
0047 
0048 void PowerDevilApp::init()
0049 {
0050     KLocalizedString::setApplicationDomain(QByteArrayLiteral("powerdevil"));
0051 
0052     KAboutData aboutData(QStringLiteral("org_kde_powerdevil"),
0053                          i18n("KDE Power Management System"),
0054                          QStringLiteral(POWERDEVIL_VERSION_STRING),
0055                          i18nc("@title", "PowerDevil, an advanced, modular and lightweight power management daemon"),
0056                          KAboutLicense::GPL,
0057                          i18nc("@info:credit", "(c) 2015-2019 Kai Uwe Broulik"));
0058     aboutData.addAuthor(i18nc("@info:credit", "Kai Uwe Broulik"), i18nc("@info:credit", "Maintainer"), QStringLiteral("kde@privat.broulik.de"));
0059     aboutData.addAuthor(i18nc("@info:credit", "Dario Freddi"), i18nc("@info:credit", "Previous maintainer"), QStringLiteral("drf@kde.org"));
0060     aboutData.setProductName("Powerdevil");
0061 
0062     KAboutData::setApplicationData(aboutData);
0063 
0064     if (QDBusConnection::systemBus().interface()->isServiceRegistered(QLatin1String("org.freedesktop.PowerManagement"))
0065         || QDBusConnection::systemBus().interface()->isServiceRegistered(QLatin1String("org.freedesktop.Policy.Power"))) {
0066         qCCritical(POWERDEVIL) << "KDE Power Management system not initialized, another power manager has been detected";
0067         return;
0068     }
0069 
0070     // not parenting Core to PowerDevilApp as it is the deleted too late on teardown
0071     // where the X connection is already lost leading to a crash (Bug 371127)
0072     m_core = new PowerDevil::Core(nullptr /*, KComponentData(aboutData)*/);
0073 
0074     connect(m_core, &PowerDevil::Core::coreReady, this, &PowerDevilApp::onCoreReady);
0075 
0076     m_core->loadCore();
0077 }
0078 
0079 void PowerDevilApp::onCoreReady()
0080 {
0081     qCDebug(POWERDEVIL) << "Core is ready, registering various services on the bus...";
0082     // DBus logic for the core
0083     new PowerManagementAdaptor(m_core);
0084     new PowerDevil::FdoConnector(m_core);
0085 
0086     QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.Solid.PowerManagement"));
0087     QDBusConnection::sessionBus().registerObject(QLatin1String("/org/kde/Solid/PowerManagement"), m_core);
0088 
0089     QDBusConnection::systemBus().interface()->registerService("org.freedesktop.Policy.Power");
0090 
0091     // Start the Policy Agent service
0092     qDBusRegisterMetaType<QList<InhibitionInfo>>();
0093     qDBusRegisterMetaType<InhibitionInfo>();
0094     new PowerManagementPolicyAgentAdaptor(PowerDevil::PolicyAgent::instance());
0095 
0096     QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.Solid.PowerManagement.PolicyAgent"));
0097     QDBusConnection::sessionBus().registerObject(QLatin1String("/org/kde/Solid/PowerManagement/PolicyAgent"), PowerDevil::PolicyAgent::instance());
0098 
0099     KActionCollection *coll = new KActionCollection(this);
0100     QAction *action = coll->addAction(QStringLiteral("powerProfile"));
0101     action->setText(i18n("Switch Power Profile"));
0102     KGlobalAccel::self()->setGlobalShortcut(action, QList<QKeySequence>{Qt::Key_Battery, Qt::MetaModifier | Qt::Key_B});
0103     connect(action, &QAction::triggered, this, &PowerDevilApp::showOsd);
0104 }
0105 
0106 int main(int argc, char **argv)
0107 {
0108     QGuiApplication::setDesktopSettingsAware(false);
0109     QGuiApplication::setAttribute(Qt::AA_DisableSessionManager);
0110     PowerDevilApp app(argc, argv);
0111     KCrash::initialize();
0112 
0113     bool replace = false;
0114     {
0115         QCommandLineParser parser;
0116         QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance"));
0117 
0118         parser.addOption(replaceOption);
0119 
0120         KAboutData aboutData = KAboutData::applicationData();
0121         aboutData.setupCommandLine(&parser);
0122 
0123         parser.process(app);
0124         aboutData.processCommandLine(&parser);
0125 
0126         replace = parser.isSet(replaceOption);
0127     }
0128     KDBusService service(KDBusService::Unique | KDBusService::StartupOption(replace ? KDBusService::Replace : 0));
0129     KCrash::setFlags(KCrash::AutoRestart);
0130 
0131     app.setQuitOnLastWindowClosed(false);
0132     app.setQuitLockEnabled(false);
0133     app.init();
0134 
0135     return app.exec();
0136 }
0137 
0138 void PowerDevilApp::showOsd() {
0139     QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.powerdevil.powerProfileOsdService"),
0140                                                           QStringLiteral("/org/kde/powerdevil/powerProfileOsdService"),
0141                                                           QStringLiteral("org.kde.powerdevil.powerProfileOsdService"),
0142                                                           QStringLiteral("showOsd"));
0143     QDBusConnection::sessionBus().asyncCall(message);
0144 }
0145 
0146 #include "moc_powerdevilapp.cpp"