File: runtimecontroller.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (166 lines) | stat: -rw-r--r-- 5,576 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
155
156
157
158
159
160
161
162
163
164
165
166
/*
   Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#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"