File: runtimecontroller.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (154 lines) | stat: -rw-r--r-- 4,931 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
    SPDX-FileCopyrightText: 2017 Aleix Pol Gonzalez <aleixpol@kde.org>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "runtimecontroller.h"
#include <QProcess>
#include <QComboBox>
#include <KActionCollection>
#include <KLocalizedString>
#include <KProcess>
#include <util/path.h>
#include "core.h"
#include "uicontroller.h"
#include "mainwindow.h"
#include "debug.h"

using namespace KDevelop;

class IdentityRuntime : public IRuntime
{
    Q_OBJECT
public:
    QString name() const override { return i18n("Host System"); }

    void startProcess(KProcess *process) const override {
        connect(process, &QProcess::errorOccurred,
            this, [](QProcess::ProcessError error) {
            qCWarning(SHELL) << "process finished with error:" << error;
        });
        process->start();
    }
    void startProcess(QProcess *process) const override {
        connect(process, &QProcess::errorOccurred,
            this, [](QProcess::ProcessError error) {
            qCWarning(SHELL) << "process finished with error:" << error;
        });
        process->start();
    }
    KDevelop::Path pathInHost(const KDevelop::Path & runtimePath) const override { return runtimePath; }
    KDevelop::Path pathInRuntime(const KDevelop::Path & localPath) const override { return localPath; }
    QString findExecutable(const QString& executableName) const override
    {
        return QStandardPaths::findExecutable(executableName);
    }
    void setEnabled(bool /*enabled*/) override {}
    QByteArray getenv(const QByteArray & varname) const override { return qgetenv(varname.constData()); }
    KDevelop::Path buildPath() const override { return {}; }
};

KDevelop::RuntimeController::RuntimeController(KDevelop::Core* core)
    : m_core(core)
{
    const bool haveUI = (core->setupFlags() != Core::NoUi);
    if (haveUI) {
        m_runtimesMenu.reset(new QMenu());
    }

    addRuntimes(new IdentityRuntime);
    setCurrentRuntime(m_runtimes.first());

    if (haveUI) {
        setupActions();
    }
}

KDevelop::RuntimeController::~RuntimeController()
{
    m_currentRuntime->setEnabled(false);
    m_currentRuntime = nullptr;
}

void RuntimeController::setupActions()
{
    // TODO not multi-window friendly, FIXME
    KActionCollection* ac = m_core->uiControllerInternal()->defaultMainWindow()->actionCollection();

    auto action = new QAction(this);
    action->setToolTip(i18n("Allows to select a runtime"));
    action->setMenu(m_runtimesMenu.data());
    action->setIcon(QIcon::fromTheme(QStringLiteral("file-library-symbolic")));
    auto updateActionText = [action](IRuntime* currentRuntime){
        action->setText(i18n("Runtime: %1", currentRuntime->name()));
    };
    connect(this, &RuntimeController::currentRuntimeChanged, action, updateActionText);
    updateActionText(m_currentRuntime);

    ac->addAction(QStringLiteral("switch_runtimes"), action);
}

void KDevelop::RuntimeController::initialize()
{
}

KDevelop::IRuntime * KDevelop::RuntimeController::currentRuntime() const
{
    Q_ASSERT(m_currentRuntime);
    return m_currentRuntime;
}

QVector<KDevelop::IRuntime *> KDevelop::RuntimeController::availableRuntimes() const
{
    return m_runtimes;
}

void KDevelop::RuntimeController::setCurrentRuntime(KDevelop::IRuntime* runtime)
{
    if (m_currentRuntime == runtime)
        return;

    Q_ASSERT(m_runtimes.contains(runtime));

    if (m_currentRuntime) {
        m_currentRuntime->setEnabled(false);
    }
    qCDebug(SHELL) << "setting runtime..." << runtime->name() << "was" << m_currentRuntime;
    m_currentRuntime = runtime;
    m_currentRuntime->setEnabled(true);
    Q_EMIT currentRuntimeChanged(runtime);
}

void KDevelop::RuntimeController::addRuntimes(KDevelop::IRuntime * runtime)
{
    if (!runtime->parent())
        runtime->setParent(this);

    if (m_core->setupFlags() != Core::NoUi) {
        auto* runtimeAction = new QAction(runtime->name(), m_runtimesMenu.data());
        runtimeAction->setCheckable(true);
        connect(runtimeAction, &QAction::triggered, runtime, [this, runtime]() {
            setCurrentRuntime(runtime);
        });
        connect(this, &RuntimeController::currentRuntimeChanged, runtimeAction, [runtimeAction, runtime](IRuntime* currentRuntime) {
            runtimeAction->setChecked(runtime == currentRuntime);
        });

        connect(runtime, &QObject::destroyed, this, [this, runtimeAction](QObject* obj) {
            Q_ASSERT(m_currentRuntime != obj);
            m_runtimes.removeAll(qobject_cast<KDevelop::IRuntime *>(obj));
            delete runtimeAction;
        });
        m_runtimesMenu->addAction(runtimeAction);
    } else {
        connect(runtime, &QObject::destroyed, this, [this](QObject* obj) {
            Q_ASSERT(m_currentRuntime != obj);
            m_runtimes.removeAll(qobject_cast<KDevelop::IRuntime *>(obj));
        });
    }

    m_runtimes << runtime;
}

#include "runtimecontroller.moc"