RunApplicationPlugin.cpp 3.76 KB
Newer Older
1
/*
2
 *   SPDX-FileCopyrightText: 2012, 2013, 2014 Ivan Cukic <ivan.cukic(at)kde.org>
3
 *
4
 *   SPDX-License-Identifier: GPL-2.0-or-later
5 6 7 8 9 10 11 12 13 14 15
 */

#include "RunApplicationPlugin.h"

#include <QString>
#include <QStandardPaths>
#include <QDir>
#include <QUrl>
#include <QProcess>
#include <QDesktopServices>

16
#include "DebugApplication.h"
17 18 19 20 21 22

#include <KService>

namespace {
    enum ActivityState {
        Running = 2,
Alexander Lohnau's avatar
Alexander Lohnau committed
23
        Stopped = 4,
24 25 26 27 28 29 30
    };
}

KAMD_EXPORT_PLUGIN(runapplicationplugin, RunApplicationPlugin, "kactivitymanagerd-plugin-runapplication.json")

RunApplicationPlugin::RunApplicationPlugin(QObject *parent, const QVariantList &args)
    : Plugin(parent)
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
31
    , m_activitiesService(nullptr)
32 33 34
{
    Q_UNUSED(args);

Laurent Montel's avatar
Laurent Montel committed
35
    setName(QStringLiteral("org.kde.ActivityManager.RunApplication"));
36 37 38 39 40 41 42 43 44 45
}

RunApplicationPlugin::~RunApplicationPlugin()
{
}

bool RunApplicationPlugin::init(QHash<QString, QObject *> &modules)
{
    Plugin::init(modules);

Laurent Montel's avatar
Laurent Montel committed
46
    m_activitiesService = modules[QStringLiteral("activities")];
47 48 49 50 51 52 53

    connect(m_activitiesService, SIGNAL(CurrentActivityChanged(QString)),
            this, SLOT(currentActivityChanged(QString)));
    connect(m_activitiesService, SIGNAL(ActivityStateChanged(QString, int)),
            this, SLOT(activityStateChanged(QString, int)));

    const auto currentActivity = Plugin::retrieve<QString>(
Laurent Montel's avatar
Laurent Montel committed
54
            m_activitiesService, "CurrentActivity", "QString");
55 56 57 58 59 60 61 62 63

    currentActivityChanged(currentActivity);

    return true;
}

QString RunApplicationPlugin::activityDirectory(const QString &activity) const
{
    return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
Laurent Montel's avatar
Laurent Montel committed
64
           + QStringLiteral("/kactivitymanagerd/activities/") + activity + QLatin1Char('/');
65 66 67 68 69 70 71 72 73
}

void RunApplicationPlugin::currentActivityChanged(const QString &activity)
{
    if (m_currentActivity == activity) {
        return;
    }

    if (!m_currentActivity.isEmpty()) {
74
        executeIn(activityDirectory(m_currentActivity) + QStringLiteral("deactivated"));
75 76 77 78
    }

    m_currentActivity = activity;

Laurent Montel's avatar
Laurent Montel committed
79
    executeIn(activityDirectory(activity) + QStringLiteral("activated"));
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

    if (!m_previousActivities.contains(activity)) {
        // This is the first time we have switched
        // to this activity in the current session,
        // pretending it has just been started
        activityStateChanged(activity, Running);

        m_previousActivities << activity;
    }
}

void RunApplicationPlugin::activityStateChanged(const QString &activity, int state)
{
    auto directory = (state == Running) ? QStringLiteral("started")
                   : (state == Stopped) ? QStringLiteral("stopped")
                   : QString();

97
    if (directory.isEmpty()) {
98 99 100 101 102 103 104 105 106 107 108
        return;
    }

    executeIn(activityDirectory(activity) + directory);
}

void RunApplicationPlugin::executeIn(const QString &path) const
{
    QDir directory(path);
    for (const auto& item: directory.entryList(QDir::Files)) {
        QString filePath = directory.filePath(item);
109 110 111 112 113 114
        if (QFileInfo fileInfo(filePath); fileInfo.suffix() == "sh" && fileInfo.isExecutable()) {
            qCDebug(KAMD_LOG_APPLICATION) << "Starting a shell script: " << filePath;
            QProcess::startDetached(filePath, QStringList());

        } else if (KService service(filePath); service.isValid() && service.isApplication()) {
            qCDebug(KAMD_LOG_APPLICATION) << "Starting application: " << service.exec();
115
            QProcess::startDetached(service.exec(), QStringList());
116

117
        } else {
Patrick Franz's avatar
Patrick Franz committed
118
            qCDebug(KAMD_LOG_APPLICATION) << "Opening file: " << QUrl::fromLocalFile(filePath);
119 120 121 122 123 124 125 126 127 128 129 130
            QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
        }
    }
}

// void RunApplicationPlugin::activityRemoved(const QString &activity)
// {
//     // TODO: Clean up the directory
// }

#include "RunApplicationPlugin.moc"