Changeset View
Changeset View
Standalone View
Standalone View
src/backends/julia/juliasession.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This program is free software; you can redistribute it and/or | ||||
3 | modify it under the terms of the GNU General Public License | ||||
4 | as published by the Free Software Foundation; either version 2 | ||||
5 | of the License, or (at your option) any later version. | ||||
6 | | ||||
7 | This program is distributed in the hope that it will be useful, | ||||
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
10 | GNU General Public License for more details. | ||||
11 | | ||||
12 | You should have received a copy of the GNU General Public License | ||||
13 | along with this program; if not, write to the Free Software | ||||
14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
15 | Boston, MA 02110-1301, USA. | ||||
16 | | ||||
17 | --- | ||||
18 | Copyright (C) 2016 Ivan Lakhtanov <ivan.lakhtanov@gmail.com> | ||||
19 | */ | ||||
20 | #include "juliasession.h" | ||||
21 | | ||||
22 | #include <KProcess> | ||||
23 | #include <QDBusConnection> | ||||
24 | #include <QDBusInterface> | ||||
25 | #include <QDBusReply> | ||||
26 | #include <QStandardPaths> | ||||
27 | | ||||
28 | #include "juliaexpression.h" | ||||
29 | #include "settings.h" | ||||
30 | | ||||
31 | JuliaSession::JuliaSession(Cantor::Backend *backend) | ||||
32 | : Session(backend) | ||||
33 | , m_process(nullptr) | ||||
34 | , m_interface(nullptr) | ||||
35 | , m_currentExpression(nullptr) | ||||
36 | { | ||||
37 | } | ||||
38 | | ||||
39 | void JuliaSession::login() | ||||
40 | { | ||||
41 | if (m_process) { | ||||
42 | m_process->deleteLater(); | ||||
43 | } | ||||
44 | | ||||
45 | m_process = new KProcess(this); | ||||
46 | m_process->setOutputChannelMode(KProcess::SeparateChannels); | ||||
47 | | ||||
48 | (*m_process) | ||||
49 | << QStandardPaths::findExecutable(QLatin1String("cantor_juliaserver")); | ||||
50 | | ||||
51 | m_process->start(); | ||||
52 | | ||||
53 | m_process->waitForStarted(); | ||||
54 | m_process->waitForReadyRead(); | ||||
55 | QTextStream stream(m_process->readAllStandardOutput()); | ||||
56 | | ||||
57 | QString readyStatus = QLatin1String("ready"); | ||||
58 | while (m_process->state() == QProcess::Running) { | ||||
59 | const QString &rl = stream.readLine(); | ||||
60 | if (rl == readyStatus) { | ||||
61 | break; | ||||
62 | } | ||||
63 | } | ||||
64 | | ||||
65 | if (!QDBusConnection::sessionBus().isConnected()) { | ||||
66 | qWarning() << "Can't connect to the D-Bus session bus.\n" | ||||
67 | "To start it, run: eval `dbus-launch --auto-syntax`"; | ||||
68 | return; | ||||
69 | } | ||||
70 | | ||||
71 | const QString &serviceName = | ||||
72 | QString::fromLatin1("org.kde.Cantor.Julia-%1").arg(m_process->pid()); | ||||
73 | | ||||
74 | m_interface = new QDBusInterface( | ||||
75 | serviceName, | ||||
76 | QString::fromLatin1("/"), | ||||
77 | QString(), | ||||
78 | QDBusConnection::sessionBus() | ||||
79 | ); | ||||
80 | | ||||
81 | if (not m_interface->isValid()) { | ||||
82 | qWarning() << QDBusConnection::sessionBus().lastError().message(); | ||||
83 | return; | ||||
84 | } | ||||
85 | | ||||
86 | m_interface->call( | ||||
87 | QString::fromLatin1("login"), | ||||
88 | JuliaSettings::self()->replPath().path() | ||||
89 | ); | ||||
90 | | ||||
91 | emit ready(); | ||||
92 | } | ||||
93 | | ||||
94 | void JuliaSession::logout() | ||||
95 | { | ||||
96 | m_process->terminate(); | ||||
97 | changeStatus(Cantor::Session::Done); | ||||
98 | } | ||||
99 | | ||||
100 | void JuliaSession::interrupt() | ||||
101 | { | ||||
102 | if (m_process->pid()) { | ||||
103 | m_process->kill(); | ||||
104 | } | ||||
105 | | ||||
106 | for (Cantor::Expression *e : m_runningExpressions) { | ||||
107 | e->interrupt(); | ||||
108 | } | ||||
109 | | ||||
110 | m_runningExpressions.clear(); | ||||
111 | changeStatus(Cantor::Session::Done); | ||||
112 | } | ||||
113 | | ||||
114 | Cantor::Expression *JuliaSession::evaluateExpression( | ||||
115 | const QString &cmd, | ||||
116 | Cantor::Expression::FinishingBehavior behave) | ||||
117 | { | ||||
118 | JuliaExpression *expr = new JuliaExpression(this); | ||||
119 | | ||||
120 | changeStatus(Cantor::Session::Running); | ||||
121 | | ||||
122 | expr->setFinishingBehavior(behave); | ||||
123 | expr->setCommand(cmd); | ||||
124 | expr->evaluate(); | ||||
125 | | ||||
126 | return expr; | ||||
127 | } | ||||
128 | | ||||
129 | Cantor::CompletionObject *JuliaSession::completionFor( | ||||
130 | const QString &command, | ||||
131 | int index) | ||||
132 | { | ||||
133 | Q_UNUSED(command); | ||||
134 | Q_UNUSED(index); | ||||
135 | return nullptr; | ||||
136 | } | ||||
137 | | ||||
138 | void JuliaSession::runJuliaCommand(const QString &command) const | ||||
139 | { | ||||
140 | m_interface->call(QLatin1String("runJuliaCommand"), command); | ||||
141 | } | ||||
142 | | ||||
143 | void JuliaSession::runJuliaCommandAsync(const QString &command) | ||||
144 | { | ||||
145 | m_interface->callWithCallback( | ||||
146 | QLatin1String("runJuliaCommand"), | ||||
147 | {command}, | ||||
148 | this, | ||||
149 | SLOT(onResultReady()) | ||||
150 | ); | ||||
151 | } | ||||
152 | | ||||
153 | void JuliaSession::onResultReady() | ||||
154 | { | ||||
155 | m_currentExpression->finalize(); | ||||
156 | m_runningExpressions.removeAll(m_currentExpression); | ||||
157 | | ||||
158 | changeStatus(Cantor::Session::Done); | ||||
159 | } | ||||
160 | | ||||
161 | void JuliaSession::runExpression(JuliaExpression *expr) | ||||
162 | { | ||||
163 | m_runningExpressions.append(expr); | ||||
164 | m_currentExpression = expr; | ||||
165 | runJuliaCommandAsync(expr->command()); | ||||
166 | } | ||||
167 | | ||||
168 | QString JuliaSession::getStringFromServer(const QString &method) | ||||
169 | { | ||||
170 | const QDBusReply<QString> &reply = m_interface->call(method); | ||||
171 | return (reply.isValid() ? reply.value() : reply.error().message()); | ||||
172 | } | ||||
173 | | ||||
174 | QString JuliaSession::getOutput() | ||||
175 | { | ||||
176 | return getStringFromServer(QLatin1String("getOutput")); | ||||
177 | } | ||||
178 | | ||||
179 | QString JuliaSession::getError() | ||||
180 | { | ||||
181 | return getStringFromServer(QLatin1String("getError")); | ||||
182 | } | ||||
183 | | ||||
184 | bool JuliaSession::getWasException() | ||||
185 | { | ||||
186 | const QDBusReply<bool> &reply = | ||||
187 | m_interface->call(QLatin1String("getWasException")); | ||||
188 | return reply.isValid() and reply.value(); | ||||
189 | } | ||||
190 | | ||||
191 | #include "juliasession.moc" |