Okular

scripter.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Pino Toscano <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "scripter.h"
8 
9 #include <QDebug>
10 #include <QFile>
11 
12 #include "debug_p.h"
13 #include "script/executor_kjs_p.h"
14 
15 using namespace Okular;
16 
17 class Okular::ScripterPrivate
18 {
19 public:
20  explicit ScripterPrivate(DocumentPrivate *doc)
21  : m_doc(doc)
22 #ifdef WITH_KJS
23  , m_kjs(nullptr)
24 #endif
25  , m_event(nullptr)
26  {
27  }
28 
29  DocumentPrivate *m_doc;
30 #ifdef WITH_KJS
32 #endif
33  Event *m_event;
34 };
35 
36 Scripter::Scripter(DocumentPrivate *doc)
37  : d(new ScripterPrivate(doc))
38 {
39 }
40 
41 Scripter::~Scripter()
42 {
43  delete d;
44 }
45 
46 void Scripter::execute(ScriptType type, const QString &script)
47 {
48  qCDebug(OkularCoreDebug) << "executing the script:";
49 #ifdef WITH_KJS
50  static QString builtInScript;
51  if (builtInScript.isNull()) {
52  QFile builtInResource(QStringLiteral(":/script/builtin.js"));
53  if (!builtInResource.open(QIODevice::ReadOnly)) {
54  qCDebug(OkularCoreDebug) << "failed to load builtin script";
55  } else {
56  builtInScript = QString::fromUtf8(builtInResource.readAll());
57  builtInResource.close();
58  }
59  }
60 
61  switch (type) {
62  case JavaScript:
63  if (!d->m_kjs) {
64  d->m_kjs.reset(new ExecutorKJS(d->m_doc));
65  }
66  d->m_kjs->execute(builtInScript + script, d->m_event);
67  }
68 #endif
69 }
70 
71 void Scripter::setEvent(Event *event)
72 {
73  d->m_event = event;
74 }
75 
76 Event *Scripter::event() const
77 {
78  return d->m_event;
79 }
bool isNull() const const
@ JavaScript
JavaScript code.
Definition: global.h:75
QString fromUtf8(const char *str, int size)
The documentation to the global Okular namespace.
Definition: action.h:16
ScriptType
Describes the possible script types.
Definition: global.h:74
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
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.