2
Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License version 2 as published by the Free Software Foundation.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public License
14
along with this library; see the file COPYING.LIB. If not, write to
15
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
Boston, MA 02110-1301, USA.
19
#include "runtimecontroller.h"
22
#include <KActionCollection>
23
#include <KLocalizedString>
25
#include <util/path.h>
27
#include "uicontroller.h"
28
#include "mainwindow.h"
31
using namespace KDevelop;
33
class IdentityRuntime : public IRuntime
35
QString name() const override { return i18n("Host System"); }
37
void startProcess(KProcess *process) const override {
38
connect(process, &QProcess::errorOccurred, this, [process](QProcess::ProcessError error) {
39
qCWarning(SHELL) << "error:" << error << process->program() << process->errorString();
43
void startProcess(QProcess *process) const override {
44
connect(process, &QProcess::errorOccurred, this, [process](QProcess::ProcessError error) {
45
qCWarning(SHELL) << "error:" << error << process->program() << process->errorString();
49
KDevelop::Path pathInHost(const KDevelop::Path & runtimePath) const override { return runtimePath; }
50
KDevelop::Path pathInRuntime(const KDevelop::Path & localPath) const override { return localPath; }
51
void setEnabled(bool /*enabled*/) override {}
52
QByteArray getenv(const QByteArray & varname) const override { return qgetenv(varname); }
55
KDevelop::RuntimeController::RuntimeController(KDevelop::Core* core)
58
const bool haveUI = (core->setupFlags() != Core::NoUi);
60
m_runtimesMenu.reset(new QMenu());
63
addRuntimes(new IdentityRuntime);
64
setCurrentRuntime(m_runtimes.constFirst());
71
KDevelop::RuntimeController::~RuntimeController()
73
m_currentRuntime->setEnabled(false);
74
m_currentRuntime = nullptr;
77
void RuntimeController::setupActions()
79
// TODO not multi-window friendly, FIXME
80
KActionCollection* ac = m_core->uiControllerInternal()->defaultMainWindow()->actionCollection();
82
auto action = new QAction(this);
83
action->setStatusTip(i18n("Allows to select a runtime"));
84
action->setMenu(m_runtimesMenu.data());
85
action->setIcon(QIcon::fromTheme(QStringLiteral("file-library-symbolic")));
86
auto updateActionText = [action](IRuntime* currentRuntime){
87
action->setText(i18n("Runtime: %1", currentRuntime->name()));
89
connect(this, &RuntimeController::currentRuntimeChanged, action, updateActionText);
90
updateActionText(m_currentRuntime);
92
ac->addAction(QStringLiteral("switch_runtimes"), action);
95
void KDevelop::RuntimeController::initialize()
99
KDevelop::IRuntime * KDevelop::RuntimeController::currentRuntime() const
101
Q_ASSERT(m_currentRuntime);
102
return m_currentRuntime;
105
QVector<KDevelop::IRuntime *> KDevelop::RuntimeController::availableRuntimes() const
110
void KDevelop::RuntimeController::setCurrentRuntime(KDevelop::IRuntime* runtime)
112
if (m_currentRuntime == runtime)
115
Q_ASSERT(m_runtimes.contains(runtime));
117
if (m_currentRuntime) {
118
m_currentRuntime->setEnabled(false);
120
qCDebug(SHELL) << "setting runtime..." << runtime->name() << "was" << m_currentRuntime;
121
m_currentRuntime = runtime;
122
m_currentRuntime->setEnabled(true);
123
Q_EMIT currentRuntimeChanged(runtime);
126
void KDevelop::RuntimeController::addRuntimes(KDevelop::IRuntime * runtime)
128
if (!runtime->parent())
129
runtime->setParent(this);
131
if (m_core->setupFlags() != Core::NoUi) {
132
QAction* runtimeAction = new QAction(runtime->name(), m_runtimesMenu.data());
133
runtimeAction->setCheckable(true);
134
connect(runtimeAction, &QAction::triggered, runtime, [this, runtime]() {
135
setCurrentRuntime(runtime);
137
connect(this, &RuntimeController::currentRuntimeChanged, runtimeAction, [runtimeAction, runtime](IRuntime* currentRuntime) {
138
runtimeAction->setChecked(runtime == currentRuntime);
141
connect(runtime, &QObject::destroyed, this, [this, runtimeAction](QObject* obj) {
142
Q_ASSERT(m_currentRuntime != obj);
143
m_runtimes.removeAll(qobject_cast<KDevelop::IRuntime *>(obj));
144
delete runtimeAction;
146
m_runtimesMenu->addAction(runtimeAction);
148
connect(runtime, &QObject::destroyed, this, [this](QObject* obj) {
149
Q_ASSERT(m_currentRuntime != obj);
150
m_runtimes.removeAll(qobject_cast<KDevelop::IRuntime *>(obj));
154
m_runtimes << runtime;