Changeset View
Changeset View
Standalone View
Standalone View
core/script/kjs_app.cpp
Show All 15 Lines | |||||
16 | 16 | | |||
17 | #include <qapplication.h> | 17 | #include <qapplication.h> | ||
18 | 18 | | |||
19 | #include <QLocale> | 19 | #include <QLocale> | ||
20 | 20 | | |||
21 | #include <KLocalizedString> | 21 | #include <KLocalizedString> | ||
22 | 22 | | |||
23 | #include "../document_p.h" | 23 | #include "../document_p.h" | ||
24 | #include "../scripter.h" | ||||
24 | #include "kjs_fullscreen_p.h" | 25 | #include "kjs_fullscreen_p.h" | ||
25 | 26 | | |||
26 | using namespace Okular; | 27 | using namespace Okular; | ||
27 | 28 | | |||
28 | static KJSPrototype *g_appProto; | 29 | static KJSPrototype *g_appProto; | ||
30 | typedef QHash< int, QTimer * > TimerCache; | ||||
31 | Q_GLOBAL_STATIC( TimerCache, g_timerCache ) | ||||
29 | 32 | | |||
30 | // the acrobat version we fake | 33 | // the acrobat version we fake | ||
31 | static const double fake_acroversion = 8.00; | 34 | static const double fake_acroversion = 8.00; | ||
32 | 35 | | |||
33 | static const struct FakePluginInfo { | 36 | static const struct FakePluginInfo { | ||
34 | const char *name; | 37 | const char *name; | ||
35 | bool certified; | 38 | bool certified; | ||
36 | bool loaded; | 39 | bool loaded; | ||
▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Line(s) | 196 | { | |||
194 | const DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object ); | 197 | const DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object ); | ||
195 | if ( doc->m_parent->historyAtEnd() ) | 198 | if ( doc->m_parent->historyAtEnd() ) | ||
196 | return KJSUndefined(); | 199 | return KJSUndefined(); | ||
197 | 200 | | |||
198 | doc->m_parent->setNextViewport(); | 201 | doc->m_parent->setNextViewport(); | ||
199 | return KJSUndefined(); | 202 | return KJSUndefined(); | ||
200 | } | 203 | } | ||
201 | 204 | | |||
205 | // app.setInterval() | ||||
206 | static KJSObject appSetInterval( KJSContext *ctx, void *object, | ||||
207 | const KJSArguments &arguments ) | ||||
208 | { | ||||
209 | DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object ); | ||||
210 | QString function = arguments.at( 0 ).toString( ctx ) + ';'; | ||||
211 | int interval = arguments.at( 1 ).toInt32( ctx ); | ||||
212 | | ||||
213 | QTimer *timer = new QTimer(); | ||||
214 | | ||||
215 | QObject::connect( timer, &QTimer::timeout, [=](){ doc->m_parent->executeScript( function ); } ); | ||||
216 | | ||||
217 | timer->start( interval ); | ||||
218 | | ||||
219 | return JSApp::wrapTimer( ctx, timer ); | ||||
220 | } | ||||
221 | | ||||
222 | // app.clearInterval() | ||||
223 | static KJSObject appClearInterval( KJSContext *ctx, void *object, | ||||
224 | const KJSArguments &arguments ) | ||||
225 | { | ||||
226 | KJSObject timerObject = arguments.at( 0 ); | ||||
227 | int timerId = timerObject.property( ctx, QStringLiteral( "timerID" ).toLatin1().toBase64() ).toInt32( ctx ); | ||||
228 | QTimer *timer = g_timerCache->value( timerId ); | ||||
229 | if( timer != nullptr ) | ||||
230 | { | ||||
231 | timer->stop(); | ||||
232 | g_timerCache->remove( timerId ); | ||||
233 | delete timer; | ||||
234 | } | ||||
235 | | ||||
236 | return KJSUndefined(); | ||||
237 | } | ||||
238 | | ||||
239 | // app.setTimeOut() | ||||
240 | static KJSObject appSetTimeOut( KJSContext *ctx, void *object, | ||||
241 | const KJSArguments &arguments ) | ||||
242 | { | ||||
243 | DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object ); | ||||
244 | QString function = arguments.at( 0 ).toString( ctx ) + ';'; | ||||
245 | int interval = arguments.at( 1 ).toInt32( ctx ); | ||||
246 | | ||||
247 | QTimer *timer = new QTimer(); | ||||
248 | timer->setSingleShot( true ); | ||||
249 | | ||||
250 | QObject::connect( timer, &QTimer::timeout, [=](){ doc->m_parent->executeScript( function ); } ); | ||||
251 | | ||||
252 | timer->start( interval ); | ||||
253 | | ||||
254 | return JSApp::wrapTimer( ctx, timer ); | ||||
255 | } | ||||
256 | | ||||
257 | // app.clearTimeOut() | ||||
258 | static KJSObject appClearTimeOut( KJSContext *ctx, void *object, | ||||
259 | const KJSArguments &arguments ) | ||||
260 | { | ||||
261 | KJSObject timerObject = arguments.at( 0 ); | ||||
262 | int timerId = timerObject.property( ctx, QStringLiteral( "timerID" ).toLatin1().toBase64() ).toInt32( ctx ); | ||||
263 | QTimer *timer = g_timerCache->value( timerId ); | ||||
264 | | ||||
265 | if( timer != nullptr ) | ||||
266 | { | ||||
267 | timer->stop(); | ||||
268 | g_timerCache->remove( timerId ); | ||||
269 | delete timer; | ||||
270 | } | ||||
271 | | ||||
272 | return KJSUndefined(); | ||||
273 | } | ||||
274 | | ||||
275 | | ||||
202 | void JSApp::initType( KJSContext *ctx ) | 276 | void JSApp::initType( KJSContext *ctx ) | ||
203 | { | 277 | { | ||
204 | static bool initialized = false; | 278 | static bool initialized = false; | ||
205 | if ( initialized ) | 279 | if ( initialized ) | ||
206 | return; | 280 | return; | ||
207 | initialized = true; | 281 | initialized = true; | ||
208 | 282 | | |||
209 | g_appProto = new KJSPrototype(); | 283 | g_appProto = new KJSPrototype(); | ||
210 | 284 | | |||
211 | g_appProto->defineProperty( ctx, QStringLiteral("formsVersion"), appGetFormsVersion ); | 285 | g_appProto->defineProperty( ctx, QStringLiteral("formsVersion"), appGetFormsVersion ); | ||
212 | g_appProto->defineProperty( ctx, QStringLiteral("language"), appGetLanguage ); | 286 | g_appProto->defineProperty( ctx, QStringLiteral("language"), appGetLanguage ); | ||
213 | g_appProto->defineProperty( ctx, QStringLiteral("numPlugIns"), appGetNumPlugins ); | 287 | g_appProto->defineProperty( ctx, QStringLiteral("numPlugIns"), appGetNumPlugins ); | ||
214 | g_appProto->defineProperty( ctx, QStringLiteral("platform"), appGetPlatform ); | 288 | g_appProto->defineProperty( ctx, QStringLiteral("platform"), appGetPlatform ); | ||
215 | g_appProto->defineProperty( ctx, QStringLiteral("plugIns"), appGetPlugIns ); | 289 | g_appProto->defineProperty( ctx, QStringLiteral("plugIns"), appGetPlugIns ); | ||
216 | g_appProto->defineProperty( ctx, QStringLiteral("printColorProfiles"), appGetPrintColorProfiles ); | 290 | g_appProto->defineProperty( ctx, QStringLiteral("printColorProfiles"), appGetPrintColorProfiles ); | ||
217 | g_appProto->defineProperty( ctx, QStringLiteral("printerNames"), appGetPrinterNames ); | 291 | g_appProto->defineProperty( ctx, QStringLiteral("printerNames"), appGetPrinterNames ); | ||
218 | g_appProto->defineProperty( ctx, QStringLiteral("viewerType"), appGetViewerType ); | 292 | g_appProto->defineProperty( ctx, QStringLiteral("viewerType"), appGetViewerType ); | ||
219 | g_appProto->defineProperty( ctx, QStringLiteral("viewerVariation"), appGetViewerVariation ); | 293 | g_appProto->defineProperty( ctx, QStringLiteral("viewerVariation"), appGetViewerVariation ); | ||
220 | g_appProto->defineProperty( ctx, QStringLiteral("viewerVersion"), appGetViewerVersion ); | 294 | g_appProto->defineProperty( ctx, QStringLiteral("viewerVersion"), appGetViewerVersion ); | ||
221 | 295 | | |||
222 | g_appProto->defineFunction( ctx, QStringLiteral("beep"), appBeep ); | 296 | g_appProto->defineFunction( ctx, QStringLiteral("beep"), appBeep ); | ||
223 | g_appProto->defineFunction( ctx, QStringLiteral("getNthPlugInName"), appGetNthPlugInName ); | 297 | g_appProto->defineFunction( ctx, QStringLiteral("getNthPlugInName"), appGetNthPlugInName ); | ||
224 | g_appProto->defineFunction( ctx, QStringLiteral("goBack"), appGoBack ); | 298 | g_appProto->defineFunction( ctx, QStringLiteral("goBack"), appGoBack ); | ||
225 | g_appProto->defineFunction( ctx, QStringLiteral("goForward"), appGoForward ); | 299 | g_appProto->defineFunction( ctx, QStringLiteral("goForward"), appGoForward ); | ||
300 | g_appProto->defineFunction( ctx, QStringLiteral("setInterval"), appSetInterval ); | ||||
301 | g_appProto->defineFunction( ctx, QStringLiteral("clearInterval"), appClearInterval ); | ||||
302 | g_appProto->defineFunction( ctx, QStringLiteral("setTimeOut"), appSetTimeOut ); | ||||
303 | g_appProto->defineFunction( ctx, QStringLiteral("clearTimeOut"), appClearTimeOut ); | ||||
226 | } | 304 | } | ||
227 | 305 | | |||
228 | KJSObject JSApp::object( KJSContext *ctx, DocumentPrivate *doc ) | 306 | KJSObject JSApp::object( KJSContext *ctx, DocumentPrivate *doc ) | ||
229 | { | 307 | { | ||
230 | return g_appProto->constructObject( ctx, doc ); | 308 | return g_appProto->constructObject( ctx, doc ); | ||
231 | } | 309 | } | ||
310 | | ||||
311 | KJSObject JSApp::wrapTimer( KJSContext *ctx, QTimer *timer) | ||||
312 | { | ||||
313 | KJSObject timerObject = g_appProto->constructObject( ctx, timer ); | ||||
314 | timerObject.setProperty( ctx, QStringLiteral("timerID").toLatin1().toBase64(), timer->timerId() ); | ||||
315 | | ||||
316 | g_timerCache->insert( timer->timerId(), timer ); | ||||
317 | | ||||
318 | return timerObject; | ||||
319 | } | ||||
320 | | ||||
321 | void JSApp::clearCachedFields() | ||||
322 | { | ||||
323 | if ( g_timerCache ) | ||||
324 | { | ||||
325 | for( auto it = g_timerCache->begin(); it != g_timerCache->end(); ++it ) | ||||
326 | { | ||||
327 | QTimer *timer = it.value(); | ||||
328 | | ||||
329 | if(timer == nullptr) | ||||
330 | continue; | ||||
331 | | ||||
332 | delete timer; | ||||
333 | } | ||||
334 | g_timerCache->clear(); | ||||
335 | } | ||||
336 | } | ||||
337 | No newline at end of file |