From 0b847d2926c9d78b9673e1e61a2931efa66730f0 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sun, 18 May 2008 19:33:46 +0000 Subject: [PATCH] start implementing properties and methods of the global JS 'app' object svn path=/trunk/KDE/kdegraphics/okular/; revision=809367 --- CMakeLists.txt | 1 + core/script/executor_kjs.cpp | 3 + core/script/kjs_app.cpp | 232 +++++++++++++++++++++++++++++++++++ core/script/kjs_app_p.h | 30 +++++ 4 files changed, 266 insertions(+) create mode 100644 core/script/kjs_app.cpp create mode 100644 core/script/kjs_app_p.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fe1724343..ce53d2bf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ set(okularcore_SRCS core/view.cpp core/fileprinter.cpp core/script/executor_kjs.cpp + core/script/kjs_app.cpp core/script/kjs_console.cpp core/script/kjs_data.cpp core/script/kjs_document.cpp diff --git a/core/script/executor_kjs.cpp b/core/script/executor_kjs.cpp index 61b2e63c6..0e93928b1 100644 --- a/core/script/executor_kjs.cpp +++ b/core/script/executor_kjs.cpp @@ -20,6 +20,7 @@ #include "../debug_p.h" #include "../document_p.h" +#include "kjs_app_p.h" #include "kjs_console_p.h" #include "kjs_data_p.h" #include "kjs_document_p.h" @@ -59,6 +60,7 @@ void ExecutorKJSPrivate::initTypes() KJSContext *ctx = m_interpreter->globalContext(); + JSApp::initType( ctx ); JSFullscreen::initType( ctx ); JSConsole::initType( ctx ); JSData::initType( ctx ); @@ -67,6 +69,7 @@ void ExecutorKJSPrivate::initTypes() JSSpell::initType( ctx ); JSUtil::initType( ctx ); + m_docObject.setProperty( ctx, "app", JSApp::object( ctx, m_doc ) ); m_docObject.setProperty( ctx, "console", JSConsole::object( ctx ) ); m_docObject.setProperty( ctx, "Doc", m_docObject ); m_docObject.setProperty( ctx, "spell", JSSpell::object( ctx ) ); diff --git a/core/script/kjs_app.cpp b/core/script/kjs_app.cpp new file mode 100644 index 000000000..ea793938f --- /dev/null +++ b/core/script/kjs_app.cpp @@ -0,0 +1,232 @@ +/*************************************************************************** + * Copyright (C) 2008 by Pino Toscano * + * Copyright (C) 2008 by Harri Porten * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#include "kjs_app_p.h" + +#include +#include +#include + +#include + +#include +#include + +#include "../document_p.h" +#include "kjs_fullscreen_p.h" + +using namespace Okular; + +static KJSPrototype *g_appProto; + +// the acrobat version we fake +static const double fake_acroversion = 8.00; + +static const struct FakePluginInfo { + const char *name; + bool certified; + bool loaded; + const char *path; +} s_fake_plugins[] = { + { "Annots", true, true, "" }, + { "EFS", true, true, "" }, + { "EScript", true, true, "" }, + { "Forms", true, true, "" }, + { "ReadOutLoud", true, true, "" }, + { "WebLink", true, true, "" } +}; +static const int s_num_fake_plugins = sizeof( s_fake_plugins ) / sizeof( s_fake_plugins[0] ); + + +static KJSObject appGetFormsVersion( KJSContext *, void * ) +{ + // faking a bit... + return KJSNumber( fake_acroversion ); +} + +static KJSObject appGetLanguage( KJSContext *, void * ) +{ + QString lang; + QString country; + QString dummy; + KLocale::splitLocale( KGlobal::locale()->language(), + lang, country, dummy, dummy ); + QString acroLang = QString::fromLatin1( "ENU" ); + if ( lang == QLatin1String( "da" ) ) + acroLang = QString::fromLatin1( "DAN" ); // Danish + else if ( lang == QLatin1String( "de" ) ) + acroLang = QString::fromLatin1( "DEU" ); // German + else if ( lang == QLatin1String( "en" ) ) + acroLang = QString::fromLatin1( "ENU" ); // English + else if ( lang == QLatin1String( "es" ) ) + acroLang = QString::fromLatin1( "ESP" ); // Spanish + else if ( lang == QLatin1String( "fr" ) ) + acroLang = QString::fromLatin1( "FRA" ); // French + else if ( lang == QLatin1String( "it" ) ) + acroLang = QString::fromLatin1( "ITA" ); // Italian + else if ( lang == QLatin1String( "ko" ) ) + acroLang = QString::fromLatin1( "KOR" ); // Korean + else if ( lang == QLatin1String( "ja" ) ) + acroLang = QString::fromLatin1( "JPN" ); // Japanese + else if ( lang == QLatin1String( "nl" ) ) + acroLang = QString::fromLatin1( "NLD" ); // Dutch + else if ( lang == QLatin1String( "pt" ) && country == QLatin1String( "BR" ) ) + acroLang = QString::fromLatin1( "PTB" ); // Brazilian Portuguese + else if ( lang == QLatin1String( "fi" ) ) + acroLang = QString::fromLatin1( "SUO" ); // Finnish + else if ( lang == QLatin1String( "sv" ) ) + acroLang = QString::fromLatin1( "SVE" ); // Swedish + else if ( lang == QLatin1String( "zh" ) && country == QLatin1String( "CN" ) ) + acroLang = QString::fromLatin1( "CHS" ); // Chinese Simplified + else if ( lang == QLatin1String( "zh" ) && country == QLatin1String( "TW" ) ) + acroLang = QString::fromLatin1( "CHT" ); // Chinese Traditional + return KJSString( acroLang ); +} + +static KJSObject appGetNumPlugins( KJSContext *, void * ) +{ + return KJSNumber( s_num_fake_plugins ); +} + +static KJSObject appGetPlatform( KJSContext *, void * ) +{ +#if defined(Q_OS_WIN) + return KJSString( QString::fromLatin1( "WIN" ) ); +#elif defined(Q_OS_MAC) + return KJSString( QString::fromLatin1( "MAC" ) ); +#else + return KJSString( QString::fromLatin1( "UNIX" ) ); +#endif +} + +static KJSObject appGetPlugIns( KJSContext *context, void * ) +{ + KJSArray plugins( context, s_num_fake_plugins ); + for ( int i = 0; i < s_num_fake_plugins; ++i ) + { + const FakePluginInfo &info = s_fake_plugins[i]; + KJSObject plugin; + plugin.setProperty( context, "certified", info.certified ); + plugin.setProperty( context, "loaded", info.loaded ); + plugin.setProperty( context, "name", info.name ); + plugin.setProperty( context, "path", info.path ); + plugin.setProperty( context, "version", fake_acroversion ); + plugins.setProperty( context, QString::number( i ), plugin ); + } + return plugins; +} + +static KJSObject appGetPrintColorProfiles( KJSContext *context, void * ) +{ + return KJSArray( context, 0 ); +} + +static KJSObject appGetPrinterNames( KJSContext *context, void * ) +{ + return KJSArray( context, 0 ); +} + +static KJSObject appGetViewerType( KJSContext *, void * ) +{ + // faking a bit... + return KJSString( QString::fromLatin1( "Reader" ) ); +} + +static KJSObject appGetViewerVariation( KJSContext *, void * ) +{ + // faking a bit... + return KJSString( QString::fromLatin1( "Reader" ) ); +} + +static KJSObject appGetViewerVersion( KJSContext *, void * ) +{ + // faking a bit... + return KJSNumber( fake_acroversion ); +} + +static KJSObject appBeep( KJSContext *context, void *, + const KJSArguments &arguments ) +{ + if ( arguments.count() < 1 ) + { + return context->throwException( "Missing beep type" ); + } + QApplication::beep(); + return KJSUndefined(); +} + +static KJSObject appGetNthPlugInName( KJSContext *context, void *, + const KJSArguments &arguments ) +{ + if ( arguments.count() < 1 ) + { + return context->throwException( "Missing plugin index" ); + } + const int nIndex = arguments.at( 0 ).toInt32( context ); + + if ( nIndex < 0 || nIndex >= s_num_fake_plugins ) + return context->throwException( "PlugIn index out of bounds" ); + + const FakePluginInfo &info = s_fake_plugins[nIndex]; + return KJSString( info.name ); +} + +static KJSObject appGoBack( KJSContext *, void *object, + const KJSArguments & ) +{ + const DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object ); + if ( doc->m_parent->historyAtBegin() ) + return KJSUndefined(); + + doc->m_parent->setPrevViewport(); + return KJSUndefined(); +} + +static KJSObject appGoForward( KJSContext *, void *object, + const KJSArguments & ) +{ + const DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object ); + if ( doc->m_parent->historyAtEnd() ) + return KJSUndefined(); + + doc->m_parent->setNextViewport(); + return KJSUndefined(); +} + +void JSApp::initType( KJSContext *ctx ) +{ + static bool initialized = false; + if ( initialized ) + return; + initialized = true; + + g_appProto = new KJSPrototype(); + + g_appProto->defineProperty( ctx, "formsVersion", appGetFormsVersion ); + g_appProto->defineProperty( ctx, "language", appGetLanguage ); + g_appProto->defineProperty( ctx, "numPlugIns", appGetNumPlugins ); + g_appProto->defineProperty( ctx, "platform", appGetPlatform ); + g_appProto->defineProperty( ctx, "plugIns", appGetPlugIns ); + g_appProto->defineProperty( ctx, "printColorProfiles", appGetPrintColorProfiles ); + g_appProto->defineProperty( ctx, "printerNames", appGetPrinterNames ); + g_appProto->defineProperty( ctx, "viewerType", appGetViewerType ); + g_appProto->defineProperty( ctx, "viewerVariation", appGetViewerVariation ); + g_appProto->defineProperty( ctx, "viewerVersion", appGetViewerVersion ); + + g_appProto->defineFunction( ctx, "beep", appBeep ); + g_appProto->defineFunction( ctx, "getNthPlugInName", appGetNthPlugInName ); + g_appProto->defineFunction( ctx, "goBack", appGoBack ); + g_appProto->defineFunction( ctx, "goForward", appGoForward ); +} + +KJSObject JSApp::object( KJSContext *ctx, DocumentPrivate *doc ) +{ + return g_appProto->constructObject( ctx, doc ); +} diff --git a/core/script/kjs_app_p.h b/core/script/kjs_app_p.h new file mode 100644 index 000000000..c0649df19 --- /dev/null +++ b/core/script/kjs_app_p.h @@ -0,0 +1,30 @@ +/*************************************************************************** + * Copyright (C) 2008 by Pino Toscano * + * Copyright (C) 2008 by Harri Porten * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#ifndef OKULAR_SCRIPT_KJS_APP_P_H +#define OKULAR_SCRIPT_KJS_APP_P_H + +class KJSContext; +class KJSObject; + +namespace Okular { + +class DocumentPrivate; + +class JSApp +{ + public: + static void initType( KJSContext *ctx ); + static KJSObject object( KJSContext *ctx, DocumentPrivate *doc ); +}; + +} + +#endif