Okular

executor_kjs.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Pino Toscano <[email protected]>
3  SPDX-FileCopyrightText: 2008 Harri Porten <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "executor_kjs_p.h"
9 
10 #include <kjs/kjsarguments.h>
11 #include <kjs/kjsinterpreter.h>
12 #include <kjs/kjsobject.h>
13 #include <kjs/kjsprototype.h>
14 #include <kjs_version.h>
15 
16 #include <QDebug>
17 
18 #include "../debug_p.h"
19 #include "../document_p.h"
20 
21 #include "config-okular.h"
22 #include "event_p.h"
23 #include "kjs_app_p.h"
24 #include "kjs_console_p.h"
25 #include "kjs_data_p.h"
26 #include "kjs_display_p.h"
27 #include "kjs_document_p.h"
28 #include "kjs_event_p.h"
29 #include "kjs_field_p.h"
30 #include "kjs_fullscreen_p.h"
31 #include "kjs_ocg_p.h"
32 #include "kjs_spell_p.h"
33 #include "kjs_util_p.h"
34 
35 using namespace Okular;
36 
37 class Okular::ExecutorKJSPrivate
38 {
39 public:
40  explicit ExecutorKJSPrivate(DocumentPrivate *doc)
41  : m_doc(doc)
42  {
43  initTypes();
44  }
45  ~ExecutorKJSPrivate()
46  {
47  delete m_interpreter;
48  }
49 
50  void initTypes();
51 
52  DocumentPrivate *m_doc;
53  KJSInterpreter *m_interpreter;
54  KJSGlobalObject m_docObject;
55 };
56 
57 void ExecutorKJSPrivate::initTypes()
58 {
59  m_docObject = JSDocument::wrapDocument(m_doc);
60  m_interpreter = new KJSInterpreter(m_docObject);
61 
62  m_interpreter->setTimeoutTime(2000); // max 2 secs allowed
63  KJSContext *ctx = m_interpreter->globalContext();
64 
65  JSApp::initType(ctx);
66  JSFullscreen::initType(ctx);
67  JSConsole::initType(ctx);
68  JSData::initType(ctx);
69  JSDisplay::initType(ctx);
70  JSDocument::initType(ctx);
71  JSEvent::initType(ctx);
72  JSField::initType(ctx);
73  JSOCG::initType(ctx);
74  JSSpell::initType(ctx);
75  JSUtil::initType(ctx);
76 
77  m_docObject.setProperty(ctx, QStringLiteral("app"), JSApp::object(ctx, m_doc));
78  m_docObject.setProperty(ctx, QStringLiteral("console"), JSConsole::object(ctx));
79  m_docObject.setProperty(ctx, QStringLiteral("Doc"), m_docObject);
80  m_docObject.setProperty(ctx, QStringLiteral("display"), JSDisplay::object(ctx));
81  m_docObject.setProperty(ctx, QStringLiteral("OCG"), JSOCG::object(ctx));
82  m_docObject.setProperty(ctx, QStringLiteral("spell"), JSSpell::object(ctx));
83  m_docObject.setProperty(ctx, QStringLiteral("util"), JSUtil::object(ctx));
84 }
85 
86 ExecutorKJS::ExecutorKJS(DocumentPrivate *doc)
87  : d(new ExecutorKJSPrivate(doc))
88 {
89 }
90 
91 ExecutorKJS::~ExecutorKJS()
92 {
93  JSField::clearCachedFields();
94  JSApp::clearCachedFields();
95  JSOCG::clearCachedFields();
96  delete d;
97 }
98 
99 void ExecutorKJS::execute(const QString &script, Event *event)
100 {
101  KJSContext *ctx = d->m_interpreter->globalContext();
102 
103  d->m_docObject.setProperty(ctx, QStringLiteral("event"), event ? JSEvent::wrapEvent(ctx, event) : KJSUndefined());
104 
105  d->m_interpreter->startTimeoutCheck();
106  KJSResult result = d->m_interpreter->evaluate(QStringLiteral("okular.js"), 1, script, &d->m_docObject);
107  d->m_interpreter->stopTimeoutCheck();
108 
109  if (result.isException() || ctx->hasException()) {
110  qCDebug(OkularCoreDebug) << "JS exception" << result.errorMessage();
111  } else {
112  qCDebug(OkularCoreDebug) << "result:" << result.value().toString(ctx);
113 
114  if (event) {
115  qCDebug(OkularCoreDebug) << "Event Result:" << event->name() << event->type() << "value:" << event->value();
116  }
117  }
118 }
The documentation to the global Okular namespace.
Definition: action.h:16
bool isException() const
bool hasException() const
QString errorMessage() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Mar 23 2023 04:04:24 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.