summaryrefslogtreecommitdiff
path: root/src/autostart/autostart.cpp
blob: 0168999740e581d0a82138f033b1a9c44723b59e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 *  autostart.cpp - autostart KAlarm when session restoration is complete
 *  Program:  kalarmautostart
 *  Copyright © 2001,2008 by David Jarvie <djarvie@kde.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kalarm.h"
#include "autostart.h"
#include "kalarm_autostart_debug.h"

#include <kcmdlineargs.h>
#include <K4AboutData>
#include <KLocalizedString>
#include <kprocess.h>

#include <QTimer>
#include <QtDBus/QtDBus>
#include <QStandardPaths>

// Number of seconds to wait before autostarting KAlarm.
// Allow plenty of time for session restoration to happen first.
static const int AUTOSTART_DELAY = 30;

#define PROGRAM_VERSION      "1.0"
#define PROGRAM_NAME "kalarmautostart"


int main(int argc, char *argv[])
{
    KLocalizedString::setApplicationDomain("kalarm");
    K4AboutData aboutData(PROGRAM_NAME, "kalarm", ki18n("KAlarm Autostart"),
        PROGRAM_VERSION, ki18n("KAlarm autostart at login"), K4AboutData::License_GPL,
        ki18n("Copyright 2001,2008 David Jarvie"), KLocalizedString(),
        "http://www.astrojar.org.uk/kalarm");
    aboutData.addAuthor(ki18n("David Jarvie"), ki18n("Maintainer"), "djarvie@kde.org");
    aboutData.setOrganizationDomain("kalarm.kde.org");
    KCmdLineArgs::init(argc, argv, &aboutData);

    KCmdLineOptions options;
    options.add("!+app", ki18n("Application to autostart"));
    options.add("+[arg]", ki18n("Command line arguments"));
    KCmdLineArgs::addCmdLineOptions(options);

    AutostartApp app;
    return app.exec();
}



AutostartApp::AutostartApp()
    : KApplication(false)       // initialise as non-GUI application
{
    // Disable session management: there is no state to save, and
    // disabling prevents a crash on logout before this app exits.
    disableSessionManagement();

    // Login session is starting up - need to wait for it to complete
    // in order to avoid starting the client before it is restored by
    // the session (where applicable).
    QTimer::singleShot(AUTOSTART_DELAY * 1000, this, &AutostartApp::slotAutostart);
}

void AutostartApp::slotAutostart()
{
    QDBusReply<bool> reply = QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral(KALARM_DBUS_SERVICE));
    if (reply.isValid()  &&  reply.value())
        qCDebug(KALARMAUTOSTART_LOG) << "KAlarm already running";
    else
    {
        KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
        if (args->count() <= 0)
            qCWarning(KALARMAUTOSTART_LOG) << "No command line";
        else
        {
            const QString prog = args->arg(0);
            const QString exe = QStandardPaths::findExecutable(prog);
            if (exe.isEmpty())
                qCWarning(KALARMAUTOSTART_LOG) << "Executable not found:" << prog;
            else
            {
                qCDebug(KALARMAUTOSTART_LOG) << "Starting" << prog;
                KProcess proc;
                proc << exe;
                for (int i = 1;  i < args->count();  ++i)
                    proc << args->arg(i);
                proc.startDetached();
            }
        }
    }
    exit();
}

// vim: et sw=4: