File indexing completed on 2024-04-28 11:20:42

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004     SPDX-FileCopyrightText: 2023 Alexander Semke <alexander.semke@web.de>
0005 */
0006 
0007 #ifndef _SAGESESSION_H
0008 #define _SAGESESSION_H
0009 
0010 #include "session.h"
0011 #include "expression.h"
0012 
0013 #include <KDirWatch>
0014 #include <QProcess>
0015 
0016 class SageSession : public Cantor::Session
0017 {
0018   Q_OBJECT
0019   public:
0020     static const QByteArray SagePrompt;
0021     static const QByteArray SageAlternativePrompt;
0022 
0023     //small helper class to deal with sage versions
0024     //Note: major version -1 is treated as most current
0025     class VersionInfo{
0026       public:
0027         explicit VersionInfo(int major = -1, int minor = -1);
0028 
0029         //bool operator <=(VersionInfo v2);
0030         bool operator <(VersionInfo other) const;
0031         bool operator <=(VersionInfo other) const;
0032         bool operator >(VersionInfo other) const;
0033         bool operator >=(VersionInfo other) const;
0034         bool operator ==( VersionInfo other) const;
0035 
0036         // These are not called major() and minor() because some libc's have
0037         // macros with those names.
0038         int majorVersion() const;
0039         int minorVersion() const;
0040       private:
0041         int m_major;
0042         int m_minor;
0043     };
0044 
0045     explicit SageSession(Cantor::Backend*);
0046     ~SageSession() override;
0047 
0048     void login() override;
0049     void logout() override;
0050 
0051     Cantor::Expression* evaluateExpression(const QString& command,Cantor::Expression::FinishingBehavior behave = Cantor::Expression::FinishingBehavior::DoNotDelete, bool internal = false) override;
0052 
0053     void runFirstExpression() override;
0054 
0055     void interrupt() override;
0056 
0057     void sendInputToProcess(const QString& input);
0058 
0059     void setTypesettingEnabled(bool) override;
0060     void setWorksheetPath(const QString&) override;
0061 
0062     Cantor::CompletionObject* completionFor(const QString& command, int index=-1) override;
0063     QSyntaxHighlighter* syntaxHighlighter(QObject* parent) override;
0064 
0065     VersionInfo sageVersion();
0066 
0067   public Q_SLOTS:
0068     void readStdOut();
0069     void readStdErr();
0070 
0071   private Q_SLOTS:
0072     void processFinished(int exitCode, QProcess::ExitStatus);
0073     void reportProcessError(QProcess::ProcessError);
0074     void fileCreated(const QString& path);
0075 
0076   private:
0077     void defineCustomFunctions();
0078     bool updateSageVersion();
0079 
0080     QProcess* m_process{nullptr};
0081     bool m_isInitialized{false};
0082     QString m_tmpPath;
0083     KDirWatch m_dirWatch;
0084     bool m_waitingForPrompt{false};
0085     QString m_outputCache;
0086     VersionInfo m_sageVersion;
0087     bool m_haveSentInitCmd{false};
0088     QString m_worksheetPath;
0089 };
0090 
0091 #endif /* _SAGESESSION_H */