2021-05-24 07:25:56 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2008-04-13 22:31:59 +00:00
|
|
|
|
|
|
|
#include "scripter.h"
|
|
|
|
|
2023-05-16 07:16:04 +00:00
|
|
|
#include "config-okular.h"
|
|
|
|
|
2018-08-31 09:23:45 +00:00
|
|
|
#include <QDebug>
|
2018-02-21 22:56:51 +00:00
|
|
|
#include <QFile>
|
2008-04-13 22:31:59 +00:00
|
|
|
|
|
|
|
#include "debug_p.h"
|
2023-06-22 15:40:50 +00:00
|
|
|
#include "script/executor_js_p.h"
|
2008-04-13 22:31:59 +00:00
|
|
|
|
|
|
|
using namespace Okular;
|
|
|
|
|
|
|
|
class Okular::ScripterPrivate
|
|
|
|
{
|
|
|
|
public:
|
2021-08-21 23:14:34 +00:00
|
|
|
explicit ScripterPrivate(DocumentPrivate *doc)
|
2018-04-16 20:35:40 +00:00
|
|
|
: m_doc(doc)
|
2023-06-22 15:24:36 +00:00
|
|
|
#if HAVE_JS
|
2023-06-22 15:45:08 +00:00
|
|
|
, m_js(nullptr)
|
2020-09-09 12:13:09 +00:00
|
|
|
#endif
|
2018-04-16 20:35:40 +00:00
|
|
|
, m_event(nullptr)
|
2008-04-13 22:31:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentPrivate *m_doc;
|
2023-06-22 15:24:36 +00:00
|
|
|
#if HAVE_JS
|
2023-06-22 15:45:08 +00:00
|
|
|
QScopedPointer<ExecutorJS> m_js;
|
2020-09-09 12:13:09 +00:00
|
|
|
#endif
|
2018-02-21 22:54:16 +00:00
|
|
|
Event *m_event;
|
2008-04-13 22:31:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Scripter::Scripter(DocumentPrivate *doc)
|
|
|
|
: d(new ScripterPrivate(doc))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Scripter::~Scripter()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2019-06-28 12:39:02 +00:00
|
|
|
void Scripter::execute(ScriptType type, const QString &script)
|
2008-04-13 22:31:59 +00:00
|
|
|
{
|
2023-04-07 17:34:31 +00:00
|
|
|
qCDebug(OkularCoreDebug) << "executing the script:" << script;
|
2023-06-22 15:24:36 +00:00
|
|
|
#if HAVE_JS
|
2018-02-21 22:56:51 +00:00
|
|
|
static QString builtInScript;
|
|
|
|
if (builtInScript.isNull()) {
|
2019-04-25 21:10:48 +00:00
|
|
|
QFile builtInResource(QStringLiteral(":/script/builtin.js"));
|
2018-02-21 22:56:51 +00:00
|
|
|
if (!builtInResource.open(QIODevice::ReadOnly)) {
|
|
|
|
qCDebug(OkularCoreDebug) << "failed to load builtin script";
|
|
|
|
} else {
|
|
|
|
builtInScript = QString::fromUtf8(builtInResource.readAll());
|
|
|
|
builtInResource.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-13 22:31:59 +00:00
|
|
|
switch (type) {
|
|
|
|
case JavaScript:
|
2023-06-22 15:45:08 +00:00
|
|
|
if (!d->m_js) {
|
|
|
|
d->m_js.reset(new ExecutorJS(d->m_doc));
|
2019-06-28 12:39:02 +00:00
|
|
|
}
|
2023-06-22 15:45:08 +00:00
|
|
|
d->m_js->execute(builtInScript + script, d->m_event);
|
2020-07-10 22:15:05 +00:00
|
|
|
}
|
2020-09-09 12:13:09 +00:00
|
|
|
#endif
|
2008-04-13 22:31:59 +00:00
|
|
|
}
|
2018-02-21 22:54:16 +00:00
|
|
|
|
|
|
|
void Scripter::setEvent(Event *event)
|
|
|
|
{
|
|
|
|
d->m_event = event;
|
|
|
|
}
|
|
|
|
|
|
|
|
Event *Scripter::event() const
|
|
|
|
{
|
|
|
|
return d->m_event;
|
|
|
|
}
|