File indexing completed on 2024-11-24 04:42:15

0001 /*
0002  *  autostart.cpp - autostart KAlarm when session restoration is complete
0003  *  Program:  kalarmautostart
0004  *  SPDX-FileCopyrightText: 2001, 2008, 2018 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "autostart.h"
0010 #include "kalarm.h"
0011 #include "kalarm_autostart_debug.h"
0012 
0013 #include <KAboutData>
0014 #include <KLocalizedString>
0015 
0016 #include <QProcess>
0017 #include <QTimer>
0018 #include <QDBusConnectionInterface>
0019 #include <QCommandLineParser>
0020 #include <QStandardPaths>
0021 
0022 // Number of seconds to wait before autostarting KAlarm.
0023 // Allow plenty of time for session restoration to happen first.
0024 static const int AUTOSTART_DELAY = 30;
0025 
0026 #define PROGRAM_VERSION      "2.0"
0027 #define PROGRAM_NAME "kalarmautostart"
0028 
0029 
0030 int main(int argc, char *argv[])
0031 {
0032     // Before using the command line, remove any options or arguments which are
0033     // for the application to be run.
0034     int ourArgc = argc;
0035     QStringList exeArgs;   // the command line for the application to run
0036     for (int i = 1; i < argc; ++i)
0037         if (*argv[i] != '-')
0038         {
0039             ourArgc = i + 1;
0040             while (i < argc)
0041                 exeArgs.append(QString::fromLocal8Bit(argv[i++]));
0042             break;
0043         }
0044 
0045     AutostartApp app(ourArgc, argv);
0046 
0047     KLocalizedString::setApplicationDomain(QByteArrayLiteral("kalarm"));
0048     KAboutData aboutData(QStringLiteral(PROGRAM_NAME), i18n("KAlarm Autostart"),
0049                          QStringLiteral(PROGRAM_VERSION), i18n("KAlarm autostart at login"),
0050                          KAboutLicense::GPL,
0051                          ki18n("Copyright 2001-%1, David Jarvie").subs(QStringLiteral("2020")).toString(), QString());
0052     aboutData.addAuthor(i18n("David Jarvie"), i18n("Author"), QStringLiteral("djarvie@kde.org"));
0053     aboutData.setOrganizationDomain("kalarm.kde.org");
0054     KAboutData::setApplicationData(aboutData);
0055 
0056     QCommandLineParser parser;
0057     aboutData.setupCommandLine(&parser);
0058     parser.addPositionalArgument(QStringLiteral("application"), i18n("Application to run"));
0059     parser.addPositionalArgument(QStringLiteral("[arg...]"), i18n("Command line arguments to pass to application"));
0060     parser.process(app);
0061     aboutData.processCommandLine(&parser);
0062 
0063     if (exeArgs.isEmpty())
0064     {
0065         qCWarning(KALARMAUTOSTART_LOG) << "No command line";
0066         return 1;
0067     }
0068     const QString exe = QStandardPaths::findExecutable(exeArgs[0]);
0069     if (exe.isEmpty())
0070     {
0071         qCWarning(KALARMAUTOSTART_LOG) << "Executable not found:" << exeArgs[0];
0072         return 1;
0073     }
0074 
0075     app.setCommandLine(exe, exeArgs);
0076     return app.exec();
0077 }
0078 
0079 
0080 
0081 AutostartApp::AutostartApp(int& argc, char** argv)
0082     : QCoreApplication(argc, argv)
0083 {
0084     // Note that this application does not require session management.
0085 }
0086 
0087 void AutostartApp::setCommandLine(const QString& exe, const QStringList& args)
0088 {
0089     // Don't validate the arguments until exec() has been called, so that error
0090     // messages can be output to the correct log file instead of stderr.
0091     mExecutable = exe;
0092     mArgs = args;
0093 
0094     // Login session is starting up - need to wait for it to complete
0095     // in order to avoid starting the client before it is restored by
0096     // the session (where applicable).
0097     QTimer::singleShot(AUTOSTART_DELAY * 1000, this, &AutostartApp::slotAutostart);   //NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
0098 }
0099 
0100 void AutostartApp::slotAutostart()
0101 {
0102     const QString prog = mArgs[0];
0103     if (prog == QLatin1String("kalarm"))
0104     {
0105         QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral(KALARM_DBUS_SERVICE));
0106         if (reply.isValid()  &&  reply.value())
0107         {
0108             qCDebug(KALARMAUTOSTART_LOG) << "KAlarm already running";
0109             exit();
0110         }
0111     }
0112 
0113     qCDebug(KALARMAUTOSTART_LOG) << "Starting" << prog;
0114     QStringList args = mArgs;
0115     args.takeFirst();
0116     QProcess::startDetached(mExecutable, args);
0117     exit();
0118 }
0119 
0120 #include "moc_autostart.cpp"
0121 
0122 // vim: et sw=4: