File indexing completed on 2024-04-28 04:32:45

0001 /*
0002     SPDX-FileCopyrightText: 2008 Pino Toscano <pino@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "scripter.h"
0008 
0009 #include "config-okular.h"
0010 
0011 #include <QDebug>
0012 #include <QFile>
0013 
0014 #include "debug_p.h"
0015 #include "script/executor_js_p.h"
0016 
0017 using namespace Okular;
0018 
0019 class Okular::ScripterPrivate
0020 {
0021 public:
0022     explicit ScripterPrivate(DocumentPrivate *doc)
0023         : m_doc(doc)
0024 #if HAVE_JS
0025         , m_js(nullptr)
0026 #endif
0027         , m_event(nullptr)
0028     {
0029     }
0030 
0031     DocumentPrivate *m_doc;
0032 #if HAVE_JS
0033     QScopedPointer<ExecutorJS> m_js;
0034 #endif
0035     Event *m_event;
0036 };
0037 
0038 Scripter::Scripter(DocumentPrivate *doc)
0039     : d(new ScripterPrivate(doc))
0040 {
0041 }
0042 
0043 Scripter::~Scripter()
0044 {
0045     delete d;
0046 }
0047 
0048 void Scripter::execute(ScriptType type, const QString &script)
0049 {
0050     qCDebug(OkularCoreDebug) << "executing the script:" << script;
0051 #if HAVE_JS
0052     static QString builtInScript;
0053     if (builtInScript.isNull()) {
0054         QFile builtInResource(QStringLiteral(":/script/builtin.js"));
0055         if (!builtInResource.open(QIODevice::ReadOnly)) {
0056             qCDebug(OkularCoreDebug) << "failed to load builtin script";
0057         } else {
0058             builtInScript = QString::fromUtf8(builtInResource.readAll());
0059             builtInResource.close();
0060         }
0061     }
0062 
0063     switch (type) {
0064     case JavaScript:
0065         if (!d->m_js) {
0066             d->m_js.reset(new ExecutorJS(d->m_doc));
0067         }
0068         d->m_js->execute(builtInScript + script, d->m_event);
0069     }
0070 #endif
0071 }
0072 
0073 void Scripter::setEvent(Event *event)
0074 {
0075     d->m_event = event;
0076 }
0077 
0078 Event *Scripter::event() const
0079 {
0080     return d->m_event;
0081 }