File indexing completed on 2024-04-28 11:37:13

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
0005     SPDX-FileCopyrightText: 2007 Michael Jansen <kde@michael-jansen.biz>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "kglobalaccel_version.h"
0011 #include "kglobalacceld.h"
0012 #include "logging_p.h"
0013 
0014 #include <KAboutData>
0015 #include <KCrash>
0016 #include <KDBusService>
0017 #include <QCommandLineParser>
0018 #include <QGuiApplication>
0019 
0020 #ifdef Q_OS_UNIX
0021 #include <unistd.h>
0022 #endif
0023 
0024 static bool isEnabled()
0025 {
0026     // TODO: Check if kglobalaccel can be disabled
0027     return true;
0028 }
0029 
0030 extern "C" Q_DECL_EXPORT int main(int argc, char **argv)
0031 {
0032     // Disable Session Management the right way (C)
0033     //
0034     // ksmserver has global shortcuts. disableSessionManagement() does not prevent Qt from
0035     // registering the app with the session manager. We remove the address to make sure we do not
0036     // get a hang on kglobalaccel restart (kglobalaccel tries to register with ksmserver,
0037     // ksmserver tries to register with kglobalaccel).
0038     qunsetenv("SESSION_MANAGER");
0039 
0040     QGuiApplication::setDesktopSettingsAware(false);
0041     QGuiApplication app(argc, argv);
0042     KAboutData aboutdata(QStringLiteral("kglobalaccel"),
0043                          QObject::tr("KDE Global Shortcuts Service"),
0044                          QStringLiteral(KGLOBALACCEL_VERSION_STRING),
0045                          QObject::tr("KDE Global Shortcuts Service"),
0046                          KAboutLicense::LGPL,
0047                          QStringLiteral("(C) 2007-2009  Andreas Hartmetz, Michael Jansen"));
0048     aboutdata.addAuthor(QStringLiteral("Andreas Hartmetz"), QObject::tr("Maintainer"), QStringLiteral("ahartmetz@gmail.com"));
0049     aboutdata.addAuthor(QStringLiteral("Michael Jansen"), QObject::tr("Maintainer"), QStringLiteral("kde@michael-jansen.biz"));
0050 
0051     KAboutData::setApplicationData(aboutdata);
0052 
0053     {
0054         QCommandLineParser parser;
0055         aboutdata.setupCommandLine(&parser);
0056         parser.process(app);
0057         aboutdata.processCommandLine(&parser);
0058     }
0059 
0060     // check if kglobalaccel is disabled
0061     if (!isEnabled()) {
0062         qCDebug(KGLOBALACCELD) << "kglobalaccel is disabled!";
0063         return 0;
0064     }
0065 
0066 #ifdef Q_OS_UNIX
0067     // It's possible that kglobalaccel gets started as the wrong user by
0068     // accident, e.g. kdesu dolphin leads to dbus activation. It then installs
0069     // its grabs and the actions are run as the wrong user.
0070     bool isUidset = false;
0071     const int sessionuid = qEnvironmentVariableIntValue("KDE_SESSION_UID", &isUidset);
0072     if(isUidset && static_cast<uid_t>(sessionuid) != getuid()) {
0073         qCWarning(KGLOBALACCELD) << "kglobalaccel running as wrong user, exiting.";
0074         return 0;
0075     }
0076 #endif
0077 
0078     KDBusService service(KDBusService::Unique);
0079 
0080     app.setQuitOnLastWindowClosed(false);
0081 
0082     // Not needed in Qt6, the feature and the workaround (i.e. setFallbackSessionManagementEnabled)
0083     // are both gone. See:
0084     // https://code.qt.io/cgit/qt/qtbase.git/commit/?id=7f878c6217754dd2b63401bf1c006476e5dc27eb
0085 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0086 #ifndef QT_NO_SESSIONMANAGER
0087     QGuiApplication::setFallbackSessionManagementEnabled(false);
0088 #endif
0089 #endif
0090 
0091     // Restart on a crash
0092     KCrash::setFlags(KCrash::AutoRestart);
0093 
0094     KGlobalAccelD globalaccel;
0095     if (!globalaccel.init()) {
0096         return -1;
0097     }
0098 
0099     return app.exec();
0100 }