compile
[kdegraphics.git] / okular / core / script / kjs_app.cpp
blobea793938f8ef3cb7407e6cd8eda470bba66c9378
1 /***************************************************************************
2 * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
3 * Copyright (C) 2008 by Harri Porten <porten@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "kjs_app_p.h"
13 #include <kjs/kjsarguments.h>
14 #include <kjs/kjsobject.h>
15 #include <kjs/kjsprototype.h>
17 #include <qapplication.h>
19 #include <kglobal.h>
20 #include <klocale.h>
22 #include "../document_p.h"
23 #include "kjs_fullscreen_p.h"
25 using namespace Okular;
27 static KJSPrototype *g_appProto;
29 // the acrobat version we fake
30 static const double fake_acroversion = 8.00;
32 static const struct FakePluginInfo {
33 const char *name;
34 bool certified;
35 bool loaded;
36 const char *path;
37 } s_fake_plugins[] = {
38 { "Annots", true, true, "" },
39 { "EFS", true, true, "" },
40 { "EScript", true, true, "" },
41 { "Forms", true, true, "" },
42 { "ReadOutLoud", true, true, "" },
43 { "WebLink", true, true, "" }
45 static const int s_num_fake_plugins = sizeof( s_fake_plugins ) / sizeof( s_fake_plugins[0] );
48 static KJSObject appGetFormsVersion( KJSContext *, void * )
50 // faking a bit...
51 return KJSNumber( fake_acroversion );
54 static KJSObject appGetLanguage( KJSContext *, void * )
56 QString lang;
57 QString country;
58 QString dummy;
59 KLocale::splitLocale( KGlobal::locale()->language(),
60 lang, country, dummy, dummy );
61 QString acroLang = QString::fromLatin1( "ENU" );
62 if ( lang == QLatin1String( "da" ) )
63 acroLang = QString::fromLatin1( "DAN" ); // Danish
64 else if ( lang == QLatin1String( "de" ) )
65 acroLang = QString::fromLatin1( "DEU" ); // German
66 else if ( lang == QLatin1String( "en" ) )
67 acroLang = QString::fromLatin1( "ENU" ); // English
68 else if ( lang == QLatin1String( "es" ) )
69 acroLang = QString::fromLatin1( "ESP" ); // Spanish
70 else if ( lang == QLatin1String( "fr" ) )
71 acroLang = QString::fromLatin1( "FRA" ); // French
72 else if ( lang == QLatin1String( "it" ) )
73 acroLang = QString::fromLatin1( "ITA" ); // Italian
74 else if ( lang == QLatin1String( "ko" ) )
75 acroLang = QString::fromLatin1( "KOR" ); // Korean
76 else if ( lang == QLatin1String( "ja" ) )
77 acroLang = QString::fromLatin1( "JPN" ); // Japanese
78 else if ( lang == QLatin1String( "nl" ) )
79 acroLang = QString::fromLatin1( "NLD" ); // Dutch
80 else if ( lang == QLatin1String( "pt" ) && country == QLatin1String( "BR" ) )
81 acroLang = QString::fromLatin1( "PTB" ); // Brazilian Portuguese
82 else if ( lang == QLatin1String( "fi" ) )
83 acroLang = QString::fromLatin1( "SUO" ); // Finnish
84 else if ( lang == QLatin1String( "sv" ) )
85 acroLang = QString::fromLatin1( "SVE" ); // Swedish
86 else if ( lang == QLatin1String( "zh" ) && country == QLatin1String( "CN" ) )
87 acroLang = QString::fromLatin1( "CHS" ); // Chinese Simplified
88 else if ( lang == QLatin1String( "zh" ) && country == QLatin1String( "TW" ) )
89 acroLang = QString::fromLatin1( "CHT" ); // Chinese Traditional
90 return KJSString( acroLang );
93 static KJSObject appGetNumPlugins( KJSContext *, void * )
95 return KJSNumber( s_num_fake_plugins );
98 static KJSObject appGetPlatform( KJSContext *, void * )
100 #if defined(Q_OS_WIN)
101 return KJSString( QString::fromLatin1( "WIN" ) );
102 #elif defined(Q_OS_MAC)
103 return KJSString( QString::fromLatin1( "MAC" ) );
104 #else
105 return KJSString( QString::fromLatin1( "UNIX" ) );
106 #endif
109 static KJSObject appGetPlugIns( KJSContext *context, void * )
111 KJSArray plugins( context, s_num_fake_plugins );
112 for ( int i = 0; i < s_num_fake_plugins; ++i )
114 const FakePluginInfo &info = s_fake_plugins[i];
115 KJSObject plugin;
116 plugin.setProperty( context, "certified", info.certified );
117 plugin.setProperty( context, "loaded", info.loaded );
118 plugin.setProperty( context, "name", info.name );
119 plugin.setProperty( context, "path", info.path );
120 plugin.setProperty( context, "version", fake_acroversion );
121 plugins.setProperty( context, QString::number( i ), plugin );
123 return plugins;
126 static KJSObject appGetPrintColorProfiles( KJSContext *context, void * )
128 return KJSArray( context, 0 );
131 static KJSObject appGetPrinterNames( KJSContext *context, void * )
133 return KJSArray( context, 0 );
136 static KJSObject appGetViewerType( KJSContext *, void * )
138 // faking a bit...
139 return KJSString( QString::fromLatin1( "Reader" ) );
142 static KJSObject appGetViewerVariation( KJSContext *, void * )
144 // faking a bit...
145 return KJSString( QString::fromLatin1( "Reader" ) );
148 static KJSObject appGetViewerVersion( KJSContext *, void * )
150 // faking a bit...
151 return KJSNumber( fake_acroversion );
154 static KJSObject appBeep( KJSContext *context, void *,
155 const KJSArguments &arguments )
157 if ( arguments.count() < 1 )
159 return context->throwException( "Missing beep type" );
161 QApplication::beep();
162 return KJSUndefined();
165 static KJSObject appGetNthPlugInName( KJSContext *context, void *,
166 const KJSArguments &arguments )
168 if ( arguments.count() < 1 )
170 return context->throwException( "Missing plugin index" );
172 const int nIndex = arguments.at( 0 ).toInt32( context );
174 if ( nIndex < 0 || nIndex >= s_num_fake_plugins )
175 return context->throwException( "PlugIn index out of bounds" );
177 const FakePluginInfo &info = s_fake_plugins[nIndex];
178 return KJSString( info.name );
181 static KJSObject appGoBack( KJSContext *, void *object,
182 const KJSArguments & )
184 const DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object );
185 if ( doc->m_parent->historyAtBegin() )
186 return KJSUndefined();
188 doc->m_parent->setPrevViewport();
189 return KJSUndefined();
192 static KJSObject appGoForward( KJSContext *, void *object,
193 const KJSArguments & )
195 const DocumentPrivate *doc = reinterpret_cast< DocumentPrivate * >( object );
196 if ( doc->m_parent->historyAtEnd() )
197 return KJSUndefined();
199 doc->m_parent->setNextViewport();
200 return KJSUndefined();
203 void JSApp::initType( KJSContext *ctx )
205 static bool initialized = false;
206 if ( initialized )
207 return;
208 initialized = true;
210 g_appProto = new KJSPrototype();
212 g_appProto->defineProperty( ctx, "formsVersion", appGetFormsVersion );
213 g_appProto->defineProperty( ctx, "language", appGetLanguage );
214 g_appProto->defineProperty( ctx, "numPlugIns", appGetNumPlugins );
215 g_appProto->defineProperty( ctx, "platform", appGetPlatform );
216 g_appProto->defineProperty( ctx, "plugIns", appGetPlugIns );
217 g_appProto->defineProperty( ctx, "printColorProfiles", appGetPrintColorProfiles );
218 g_appProto->defineProperty( ctx, "printerNames", appGetPrinterNames );
219 g_appProto->defineProperty( ctx, "viewerType", appGetViewerType );
220 g_appProto->defineProperty( ctx, "viewerVariation", appGetViewerVariation );
221 g_appProto->defineProperty( ctx, "viewerVersion", appGetViewerVersion );
223 g_appProto->defineFunction( ctx, "beep", appBeep );
224 g_appProto->defineFunction( ctx, "getNthPlugInName", appGetNthPlugInName );
225 g_appProto->defineFunction( ctx, "goBack", appGoBack );
226 g_appProto->defineFunction( ctx, "goForward", appGoForward );
229 KJSObject JSApp::object( KJSContext *ctx, DocumentPrivate *doc )
231 return g_appProto->constructObject( ctx, doc );