1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*
Actiona
Copyright (C) 2005 Jonathan Mercier-Ganady
Actiona is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Actiona is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact : jmgr@jmgr.info
*/
#pragma once
#include "actiontools_global.h"
#include "version.h"
#include "scriptparameter.h"
#include "resource.h"
#include <QVariant>
#include <QStringList>
#include <QMap>
#include <QStack>
#include <functional>
#include <utility>
class QIODevice;
namespace ActionTools
{
class ActionInstance;
class ActionFactory;
class ElementDefinition;
class ScriptLineModel;
class ACTIONTOOLSSHARED_EXPORT Script : public QObject
{
Q_OBJECT
public:
enum ReadResult
{
ReadSuccess, // Ok
ReadInternal, // Internal error
ReadInvalidSchema, // Did not pass schema validation
ReadInvalidScriptVersion, // Script version is newer than ours
ReadCanceled // Loading was canceled
};
static const QRegExp CodeVariableDeclarationRegExp;
Script(ActionFactory *actionFactory, QObject *parent = nullptr);
~Script() override;
void appendAction(ActionInstance *actionInstance);
ActionInstance *appendAction(const QString &actionDefinitionId);
ActionInstance *actionAt(int line) const;
void insertAction(int line, ActionInstance *actionInstance);
void setAction(int line, ActionInstance *actionInstance);
void removeActions(int line, int count);
void removeAction(int line);
void removeAction(ActionInstance *actionInstance);
void removeAll();
void moveAction(int startLine, int endLine);
int actionCount() const { return mActionInstances.count(); }
int labelLine(const QString &label) const;
bool hasEnabledActions() const;
QSet<int> usedActions() const;
void invalidateLabelList() { mRebuildLabelList = true; }
bool write(QIODevice *device, const Tools::Version &programVersion, const Tools::Version &scriptVersion, std::function<void(int, int, QString)> *progressCallback = nullptr);
ReadResult read(QIODevice *device,
const Tools::Version &scriptVersion,
std::function<void(int, int, QString)> *progressCallback = nullptr,
std::function<void()> *resetCallback = nullptr,
std::function<void(QList<ActionTools::ActionInstance *>)> *addActionsCallback = nullptr);
bool validateContent(const QString &content, const Tools::Version &scriptVersion);
const QString &statusMessage() const { return mStatusMessage; }
int line() const { return mLine; }
int column() const { return mColumn; }
const QString &programName() const { return mProgramName; }
const Tools::Version &programVersion() const { return mProgramVersion; }
const Tools::Version &scriptVersion() const { return mScriptVersion; }
const QString &os() const { return mOs; }
const QStringList &missingActions() const { return mMissingActions; }
int pauseBefore() const { return mPauseBefore; }
int pauseAfter() const { return mPauseAfter; }
void addParameter(const ScriptParameter ¶meter) { mParameters.append(parameter); }
int parameterCount() const { return mParameters.count(); }
const ScriptParameter ¶meter(int index) const { return mParameters.at(index); }
void removeAllParameters() { mParameters.clear(); }
QList<ScriptParameter> parameters() const { return mParameters; }
void setPauseBefore(int pauseBefore) { mPauseBefore = pauseBefore; }
void setPauseAfter(int pauseAfter) { mPauseAfter = pauseAfter; }
void addProcedure(const QString &procedureName, int line) { mProcedures.insert(procedureName, line); }
int findProcedure(const QString &procedureName) const { return mProcedures.value(procedureName, -1); }
void clearProcedures() { mProcedures.clear(); }
void addProcedureCall(int callerLine) { mCallStack.push(callerLine); }
bool hasProcedureCall() const { return !mCallStack.isEmpty(); }
int popProcedureCall() { return mCallStack.pop(); }
void clearCallStack() { mCallStack.clear(); }
void addResource(const QString &id, const QByteArray &data, Resource::Type type){ mResources.insert(id, Resource(data, type)); }
bool hasResource(const QString &id) const { return mResources.contains(id); }
Resource resource(const QString &id) const { return mResources.value(id); }
void clearResources() { mResources.clear(); }
const QMap<QString, Resource> &resources() const { return mResources; }
std::pair<int, int> minMaxExecutionCounter() const { return mMinMaxExecutionCounter; }
qint64 executionDuration() const { return mExecutionDuration; }
bool hasBeenExecuted() const { return mMinMaxExecutionCounter.first > 0; }
int actionIndexFromRuntimeId(qint64 runtimeId) const;
QStringList procedureNames() const;
QStringList labels() const;
QSet<QString> findVariables(ActionInstance *actionInstance = nullptr, ActionInstance *excludedActionInstance = nullptr) const;
void executionStopped();
ScriptLineModel *lineModel() const { return mLineModel; }
void updateLineModel();
private:
Script::ReadResult validateSchema(QIODevice *device, const Tools::Version &scriptVersion, bool tryOlderVersions = true);
void parametersFromDefinition(QSet<QString> &variables, const ActionInstance *actionInstance, const ActionTools::ElementDefinition *elementDefinition) const;
void findVariablesInAction(ActionInstance *actionInstance, QSet<QString> &result) const;
QList<ScriptParameter> mParameters;
QList<ActionInstance *> mActionInstances;
ActionFactory *mActionFactory;
QString mStatusMessage;
int mLine{-1};
int mColumn{-1};
QString mProgramName;
Tools::Version mProgramVersion;
Tools::Version mScriptVersion;
QString mOs;
QStringList mMissingActions;
int mPauseBefore{0};
int mPauseAfter{0};
QMap<QString, int> mProcedures;
QStack<int> mCallStack;
QMap<QString, Resource> mResources;
std::pair<int, int> mMinMaxExecutionCounter;
qint64 mExecutionDuration;
ScriptLineModel *mLineModel;
mutable QStringList mLabels;
mutable bool mRebuildLabelList{};
Q_DISABLE_COPY(Script)
};
}
|