compile
[kdegraphics.git] / okular / core / script / kjs_document.cpp
blobd22e28a14dcd50d59c6e2118628d02c90f536421
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_document_p.h"
13 #include <qwidget.h>
15 #include <kjs/kjsobject.h>
16 #include <kjs/kjsprototype.h>
17 #include <kjs/kjsarguments.h>
19 #include <kdebug.h>
20 #include <assert.h>
22 #include "../document_p.h"
23 #include "../page.h"
24 #include "../form.h"
25 #include "kjs_data_p.h"
26 #include "kjs_field_p.h"
28 using namespace Okular;
30 static KJSPrototype *g_docProto;
32 // Document.numPages
33 static KJSObject docGetNumPages( KJSContext *, void *object )
35 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
37 return KJSNumber( doc->m_pagesVector.count() );
40 // Document.pageNum (getter)
41 static KJSObject docGetPageNum( KJSContext *, void *object )
43 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
45 return KJSNumber( doc->m_parent->currentPage() );
48 // Document.pageNum (setter)
49 static void docSetPageNum( KJSContext* ctx, void* object,
50 KJSObject value )
52 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
54 int page = value.toInt32( ctx );
56 if ( page == (int)doc->m_parent->currentPage() )
57 return;
59 doc->m_parent->setViewportPage( page );
62 // Document.documentFileName
63 static KJSObject docGetDocumentFileName( KJSContext *, void *object )
65 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
67 return KJSString( doc->m_url.fileName() );
70 // Document.filesize
71 static KJSObject docGetFilesize( KJSContext *, void *object )
73 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
75 return KJSNumber( doc->m_docSize );
78 // Document.path
79 static KJSObject docGetPath( KJSContext *, void *object )
81 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
83 return KJSString( doc->m_url.pathOrUrl() );
86 // Document.URL
87 static KJSObject docGetURL( KJSContext *, void *object )
89 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
91 return KJSString( doc->m_url.prettyUrl() );
94 // Document.permStatusReady
95 static KJSObject docGetPermStatusReady( KJSContext *, void * )
97 return KJSBoolean( true );
100 // Document.dataObjects
101 static KJSObject docGetDataObjects( KJSContext *ctx, void *object )
103 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
105 const QList< EmbeddedFile * > *files = doc->m_generator->embeddedFiles();
107 KJSArray dataObjects( ctx, files ? files->count() : 0 );
108 if ( files )
110 QList< EmbeddedFile * >::ConstIterator it = files->begin(), itEnd = files->end();
111 for ( int i = 0; it != itEnd; ++it, ++i )
113 KJSObject newdata = JSData::wrapFile( ctx, *it );
114 dataObjects.setProperty( ctx, QString::number( i ), newdata );
117 return dataObjects;
120 // Document.external
121 static KJSObject docGetExternal( KJSContext *, void *object )
123 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
124 QWidget *widget = doc->m_parent->widget();
126 const bool isShell = ( widget
127 && widget->parentWidget()
128 && widget->parentWidget()->objectName() == QLatin1String( "okular::Shell" ) );
129 return KJSBoolean( !isShell );
133 static KJSObject docGetInfo( KJSContext *ctx, void *object )
135 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
137 KJSObject obj;
138 const DocumentInfo *docinfo = doc->m_generator->generateDocumentInfo();
139 if ( docinfo )
141 #define KEY_GET( key, property ) \
142 do { \
143 const QString data = docinfo->get( key ); \
144 if ( !data.isEmpty() ) \
146 const KJSString newval( data ); \
147 obj.setProperty( ctx, property, newval ); \
148 obj.setProperty( ctx, QString( property ).toLower(), newval ); \
150 } while ( 0 );
151 KEY_GET( "title", "Title" );
152 KEY_GET( "author", "Author" );
153 KEY_GET( "subject", "Subject" );
154 KEY_GET( "keywords", "Keywords" );
155 KEY_GET( "creator", "Creator" );
156 KEY_GET( "producer", "Producer" );
157 #undef KEY_GET
159 return obj;
162 #define DOCINFO_GET_METHOD( key, name ) \
163 static KJSObject docGet ## name( KJSContext *, void *object ) \
165 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); \
166 const DocumentInfo *docinfo = doc->m_generator->generateDocumentInfo(); \
167 return KJSString( docinfo->get( key ) ); \
170 DOCINFO_GET_METHOD( "author", Author )
171 DOCINFO_GET_METHOD( "creator", Creator )
172 DOCINFO_GET_METHOD( "keywords", Keywords )
173 DOCINFO_GET_METHOD( "producer", Producer )
174 DOCINFO_GET_METHOD( "title", Title )
175 DOCINFO_GET_METHOD( "subject", Subject )
177 #undef DOCINFO_GET_METHOD
179 // Document.getField()
180 static KJSObject docGetField( KJSContext *context, void *object,
181 const KJSArguments &arguments )
183 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
185 QString cName = arguments.at( 0 ).toString( context );
187 QVector< Page * >::const_iterator pIt = doc->m_pagesVector.begin(), pEnd = doc->m_pagesVector.end();
188 for ( ; pIt != pEnd; ++pIt )
190 const QLinkedList< Okular::FormField * > pageFields = (*pIt)->formFields();
191 QLinkedList< Okular::FormField * >::const_iterator ffIt = pageFields.begin(), ffEnd = pageFields.end();
192 for ( ; ffIt != ffEnd; ++ffIt )
194 if ( (*ffIt)->name() == cName )
196 return JSField::wrapField( context, *ffIt, *pIt );
200 return KJSUndefined();
203 // Document.getPageLabel()
204 static KJSObject docGetPageLabel( KJSContext *ctx,void *object,
205 const KJSArguments &arguments )
207 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
208 int nPage = arguments.at( 0 ).toInt32( ctx );
209 Page *p = doc->m_pagesVector.value( nPage );
210 return KJSString( p ? p->label() : QString() );
213 // Document.getPageRotation()
214 static KJSObject docGetPageRotation( KJSContext *ctx, void *object,
215 const KJSArguments &arguments )
217 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
218 int nPage = arguments.at( 0 ).toInt32( ctx );
219 Page *p = doc->m_pagesVector.value( nPage );
220 return KJSNumber( p ? p->orientation() * 90 : 0 );
223 // Document.gotoNamedDest()
224 static KJSObject docGotoNamedDest( KJSContext *ctx, void *object,
225 const KJSArguments &arguments )
227 DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
229 QString dest = arguments.at( 0 ).toString( ctx );
231 DocumentViewport viewport( doc->m_generator->metaData( "NamedViewport", dest ).toString() );
232 if ( !viewport.isValid() )
233 return KJSUndefined();
235 doc->m_parent->setViewport( viewport );
237 return KJSUndefined();
240 // Document.syncAnnotScan()
241 static KJSObject docSyncAnnotScan( KJSContext *, void *,
242 const KJSArguments & )
244 return KJSUndefined();
247 void JSDocument::initType( KJSContext *ctx )
249 assert( g_docProto );
251 static bool initialized = false;
252 if ( initialized )
253 return;
254 initialized = true;
256 g_docProto->defineProperty( ctx, "numPages", docGetNumPages );
257 g_docProto->defineProperty( ctx, "pageNum", docGetPageNum, docSetPageNum );
258 g_docProto->defineProperty( ctx, "documentFileName", docGetDocumentFileName );
259 g_docProto->defineProperty( ctx, "filesize", docGetFilesize );
260 g_docProto->defineProperty( ctx, "path", docGetPath );
261 g_docProto->defineProperty( ctx, "URL", docGetURL );
262 g_docProto->defineProperty( ctx, "permStatusReady", docGetPermStatusReady );
263 g_docProto->defineProperty( ctx, "dataObjects", docGetDataObjects );
264 g_docProto->defineProperty( ctx, "external", docGetExternal );
266 // info properties
267 g_docProto->defineProperty( ctx, "info", docGetInfo );
268 g_docProto->defineProperty( ctx, "author", docGetAuthor );
269 g_docProto->defineProperty( ctx, "creator", docGetCreator );
270 g_docProto->defineProperty( ctx, "keywords", docGetKeywords );
271 g_docProto->defineProperty( ctx, "producer", docGetProducer );
272 g_docProto->defineProperty( ctx, "title", docGetTitle );
273 g_docProto->defineProperty( ctx, "subject", docGetSubject );
275 g_docProto->defineFunction( ctx, "getField", docGetField );
276 g_docProto->defineFunction( ctx, "getPageLabel", docGetPageLabel );
277 g_docProto->defineFunction( ctx, "getPageRotation", docGetPageRotation );
278 g_docProto->defineFunction( ctx, "gotoNamedDest", docGotoNamedDest );
279 g_docProto->defineFunction( ctx, "syncAnnotScan", docSyncAnnotScan );
282 KJSGlobalObject JSDocument::wrapDocument( DocumentPrivate *doc )
284 if ( !g_docProto )
285 g_docProto = new KJSPrototype();
286 return g_docProto->constructGlobalObject( doc );