File indexing completed on 2024-05-26 11:22:04

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2016 Ivan Lakhtanov <ivan.lakhtanov@gmail.com>
0004 */
0005 #pragma once
0006 
0007 #include <QMap>
0008 #include <QProcess>
0009 #include <QRegularExpression>
0010 
0011 #include "session.h"
0012 
0013 class JuliaExpression;
0014 class JuliaCompletionObject;
0015 class JuliaVariableModel;
0016 class KProcess;
0017 class QDBusInterface;
0018 namespace Cantor {
0019     class DefaultVariableModel;
0020 }
0021 
0022 /**
0023  * Implements a Cantor session for the Julia backend
0024  *
0025  * It communicates through DBus interface with JuliaServer
0026  */
0027 class JuliaSession: public Cantor::Session
0028 {
0029     Q_OBJECT
0030 public:
0031     /**
0032      * Constructs session
0033      *
0034      * @param backend owning backend
0035      */
0036     explicit JuliaSession(Cantor::Backend *backend);
0037     ~JuliaSession() override;
0038 
0039     /**
0040      * @see Cantor::Session::login
0041      */
0042     void login() override;
0043 
0044     /**
0045      * @see Cantor::Session::logout
0046      */
0047     void logout() override;
0048 
0049     /**
0050      * @see Cantor::Session::interrupt
0051      */
0052     void interrupt() override;
0053 
0054     /**
0055      * @see Cantor::Session::evaluateExpression
0056      */
0057     Cantor::Expression *evaluateExpression(
0058         const QString &command,
0059         Cantor::Expression::FinishingBehavior behave = Cantor::Expression::FinishingBehavior::DoNotDelete,
0060         bool internal = false) override;
0061 
0062     /**
0063      * @see Cantor::Session::completionFor
0064      */
0065     Cantor::CompletionObject *completionFor(
0066         const QString &cmd,
0067         int index = -1) override;
0068 
0069     /**
0070      * @see Cantor::Session::syntaxHighlighter
0071      */
0072     QSyntaxHighlighter *syntaxHighlighter(QObject *parent) override;
0073 
0074     QString plotFilePrefixPath() const;
0075 
0076 private Q_SLOTS:
0077     /**
0078      * Called when async call to JuliaServer is finished
0079      */
0080     void onResultReady();
0081 
0082     // Handler for cantor_juliaserver crashes
0083     void reportServerProcessError(QProcess::ProcessError serverError);
0084 
0085 private:
0086     KProcess* m_process{nullptr}; //< process to run JuliaServer inside
0087     QDBusInterface* m_interface{nullptr}; //< interface to JuliaServer
0088 
0089     /// Cache to speedup modules whos calls
0090     QMap<QString, QString> m_whos_cache;
0091 
0092     /// Variables for handling plot integration: settings value and real state
0093     QString m_plotFilePrefixPath;
0094     bool m_isIntegratedPlotsEnabled{false};
0095     bool m_isIntegratedPlotsSettingsEnabled{false};
0096 
0097     void runFirstExpression() override;
0098 
0099     /**
0100      * Runs Julia piece of code in synchronous mode
0101      *
0102      * @param command command to execute
0103      */
0104     void runJuliaCommand(const QString &command) const;
0105 
0106     /**
0107      * Runs Julia piece of code in asynchronous mode. When finished
0108      * onResultReady is called
0109      *
0110      * @param command command to execute
0111      */
0112     void runJuliaCommandAsync(const QString &command);
0113 
0114     /**
0115      * Helper method to get QString returning function result
0116      *
0117      * @param method DBus method to call
0118      * @return result of the method
0119      */
0120     QString getStringFromServer(const QString &method);
0121 
0122     /**
0123      * @return stdout of the last executed command
0124      */
0125     QString getOutput();
0126 
0127     /**
0128      * @return stderr of the last executed command
0129      */
0130     QString getError();
0131 
0132     /**
0133      * @return indicator of exception occurred during the last command execution
0134      */
0135     bool getWasException();
0136 
0137     void updateGraphicPackagesFromSettings();
0138 
0139     QString graphicPackageErrorMessage(QString packageId) const override;
0140 };