~kdevelop/kdevplatform/master

« back to all changes in this revision

Viewing changes to shell/runtimecontroller.cpp

  • Committer: Friedrich W. H. Kossebau
  • Date: 2017-08-13 21:54:31 UTC
  • Revision ID: git-v1:8ce76bea4df0831deb2fb243af33cc90d3cc8043
Wipe master branch and point in README to new location

Summary: also add util script for moving over existing personal branches

Reviewers: #kdevelop, apol, kfunk

Reviewed By: #kdevelop, apol, kfunk

Subscribers: kfunk, kdevelop-devel

Differential Revision: https://phabricator.kde.org/D7244

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   Copyright 2017 Aleix Pol Gonzalez <aleixpol@kde.org>
3
 
 
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.
7
 
 
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.
12
 
 
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.
17
 
*/
18
 
 
19
 
#include "runtimecontroller.h"
20
 
#include <QProcess>
21
 
#include <QComboBox>
22
 
#include <KActionCollection>
23
 
#include <KLocalizedString>
24
 
#include <KProcess>
25
 
#include <util/path.h>
26
 
#include "core.h"
27
 
#include "uicontroller.h"
28
 
#include "mainwindow.h"
29
 
#include "debug.h"
30
 
 
31
 
using namespace KDevelop;
32
 
 
33
 
class IdentityRuntime : public IRuntime
34
 
{
35
 
    QString name() const override { return i18n("Host System"); }
36
 
 
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();
40
 
        });
41
 
        process->start();
42
 
    }
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();
46
 
        });
47
 
        process->start();
48
 
    }
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); }
53
 
};
54
 
 
55
 
KDevelop::RuntimeController::RuntimeController(KDevelop::Core* core)
56
 
    : m_core(core)
57
 
{
58
 
    const bool haveUI = (core->setupFlags() != Core::NoUi);
59
 
    if (haveUI) {
60
 
        m_runtimesMenu.reset(new QMenu());
61
 
    }
62
 
 
63
 
    addRuntimes(new IdentityRuntime);
64
 
    setCurrentRuntime(m_runtimes.constFirst());
65
 
 
66
 
    if (haveUI) {
67
 
        setupActions();
68
 
    }
69
 
}
70
 
 
71
 
KDevelop::RuntimeController::~RuntimeController()
72
 
{
73
 
    m_currentRuntime->setEnabled(false);
74
 
    m_currentRuntime = nullptr;
75
 
}
76
 
 
77
 
void RuntimeController::setupActions()
78
 
{
79
 
    // TODO not multi-window friendly, FIXME
80
 
    KActionCollection* ac = m_core->uiControllerInternal()->defaultMainWindow()->actionCollection();
81
 
 
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()));
88
 
    };
89
 
    connect(this, &RuntimeController::currentRuntimeChanged, action, updateActionText);
90
 
    updateActionText(m_currentRuntime);
91
 
 
92
 
    ac->addAction(QStringLiteral("switch_runtimes"), action);
93
 
}
94
 
 
95
 
void KDevelop::RuntimeController::initialize()
96
 
{
97
 
}
98
 
 
99
 
KDevelop::IRuntime * KDevelop::RuntimeController::currentRuntime() const
100
 
{
101
 
    Q_ASSERT(m_currentRuntime);
102
 
    return m_currentRuntime;
103
 
}
104
 
 
105
 
QVector<KDevelop::IRuntime *> KDevelop::RuntimeController::availableRuntimes() const
106
 
{
107
 
    return m_runtimes;
108
 
}
109
 
 
110
 
void KDevelop::RuntimeController::setCurrentRuntime(KDevelop::IRuntime* runtime)
111
 
{
112
 
    if (m_currentRuntime == runtime)
113
 
        return;
114
 
 
115
 
    Q_ASSERT(m_runtimes.contains(runtime));
116
 
 
117
 
    if (m_currentRuntime) {
118
 
        m_currentRuntime->setEnabled(false);
119
 
    }
120
 
    qCDebug(SHELL) << "setting runtime..." << runtime->name() << "was" << m_currentRuntime;
121
 
    m_currentRuntime = runtime;
122
 
    m_currentRuntime->setEnabled(true);
123
 
    Q_EMIT currentRuntimeChanged(runtime);
124
 
}
125
 
 
126
 
void KDevelop::RuntimeController::addRuntimes(KDevelop::IRuntime * runtime)
127
 
{
128
 
    if (!runtime->parent())
129
 
        runtime->setParent(this);
130
 
 
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);
136
 
        });
137
 
        connect(this, &RuntimeController::currentRuntimeChanged, runtimeAction, [runtimeAction, runtime](IRuntime* currentRuntime) {
138
 
            runtimeAction->setChecked(runtime == currentRuntime);
139
 
        });
140
 
 
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;
145
 
        });
146
 
        m_runtimesMenu->addAction(runtimeAction);
147
 
    } else {
148
 
        connect(runtime, &QObject::destroyed, this, [this](QObject* obj) {
149
 
            Q_ASSERT(m_currentRuntime != obj);
150
 
            m_runtimes.removeAll(qobject_cast<KDevelop::IRuntime *>(obj));
151
 
        });
152
 
    }
153
 
 
154
 
    m_runtimes << runtime;
155
 
}