• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegraphics API Reference
  • KDE Home
  • Contact Us
 

okular

  • sources
  • kde-4.12
  • kdegraphics
  • okular
  • core
  • script
kjs_document.cpp
Go to the documentation of this file.
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  ***************************************************************************/
10 
11 #include "kjs_document_p.h"
12 
13 #include <qwidget.h>
14 
15 #include <kjs/kjsobject.h>
16 #include <kjs/kjsprototype.h>
17 #include <kjs/kjsarguments.h>
18 
19 #include <kdebug.h>
20 #include <assert.h>
21 
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"
27 
28 using namespace Okular;
29 
30 static KJSPrototype *g_docProto;
31 
32 // Document.numPages
33 static KJSObject docGetNumPages( KJSContext *, void *object )
34 {
35  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
36 
37  return KJSNumber( doc->m_pagesVector.count() );
38 }
39 
40 // Document.pageNum (getter)
41 static KJSObject docGetPageNum( KJSContext *, void *object )
42 {
43  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
44 
45  return KJSNumber( doc->m_parent->currentPage() );
46 }
47 
48 // Document.pageNum (setter)
49 static void docSetPageNum( KJSContext* ctx, void* object,
50  KJSObject value )
51 {
52  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
53 
54  int page = value.toInt32( ctx );
55 
56  if ( page == (int)doc->m_parent->currentPage() )
57  return;
58 
59  doc->m_parent->setViewportPage( page );
60 }
61 
62 // Document.documentFileName
63 static KJSObject docGetDocumentFileName( KJSContext *, void *object )
64 {
65  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
66 
67  return KJSString( doc->m_url.fileName() );
68 }
69 
70 // Document.filesize
71 static KJSObject docGetFilesize( KJSContext *, void *object )
72 {
73  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
74 
75  return KJSNumber( doc->m_docSize );
76 }
77 
78 // Document.path
79 static KJSObject docGetPath( KJSContext *, void *object )
80 {
81  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
82 
83  return KJSString( doc->m_url.pathOrUrl() );
84 }
85 
86 // Document.URL
87 static KJSObject docGetURL( KJSContext *, void *object )
88 {
89  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
90 
91  return KJSString( doc->m_url.prettyUrl() );
92 }
93 
94 // Document.permStatusReady
95 static KJSObject docGetPermStatusReady( KJSContext *, void * )
96 {
97  return KJSBoolean( true );
98 }
99 
100 // Document.dataObjects
101 static KJSObject docGetDataObjects( KJSContext *ctx, void *object )
102 {
103  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
104 
105  const QList< EmbeddedFile * > *files = doc->m_generator->embeddedFiles();
106 
107  KJSArray dataObjects( ctx, files ? files->count() : 0 );
108  if ( files )
109  {
110  QList< EmbeddedFile * >::ConstIterator it = files->begin(), itEnd = files->end();
111  for ( int i = 0; it != itEnd; ++it, ++i )
112  {
113  KJSObject newdata = JSData::wrapFile( ctx, *it );
114  dataObjects.setProperty( ctx, QString::number( i ), newdata );
115  }
116  }
117  return dataObjects;
118 }
119 
120 // Document.external
121 static KJSObject docGetExternal( KJSContext *, void *object )
122 {
123  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
124  QWidget *widget = doc->m_parent->widget();
125 
126  const bool isShell = ( widget
127  && widget->parentWidget()
128  && widget->parentWidget()->objectName() == QLatin1String( "okular::Shell" ) );
129  return KJSBoolean( !isShell );
130 }
131 
132 
133 static KJSObject docGetInfo( KJSContext *ctx, void *object )
134 {
135  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
136 
137  KJSObject obj;
138  const DocumentInfo *docinfo = doc->m_generator->generateDocumentInfo();
139  if ( docinfo )
140  {
141 #define KEY_GET( key, property ) \
142 do { \
143  const QString data = docinfo->get( key ); \
144  if ( !data.isEmpty() ) \
145  { \
146  const KJSString newval( data ); \
147  obj.setProperty( ctx, property, newval ); \
148  obj.setProperty( ctx, QString( property ).toLower(), newval ); \
149  } \
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
158  }
159  return obj;
160 }
161 
162 #define DOCINFO_GET_METHOD( key, name ) \
163 static KJSObject docGet ## name( KJSContext *, void *object ) \
164 { \
165  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object ); \
166  const DocumentInfo *docinfo = doc->m_generator->generateDocumentInfo(); \
167  return KJSString( docinfo->get( key ) ); \
168 }
169 
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 )
176 
177 #undef DOCINFO_GET_METHOD
178 
179 // Document.getField()
180 static KJSObject docGetField( KJSContext *context, void *object,
181  const KJSArguments &arguments )
182 {
183  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
184 
185  QString cName = arguments.at( 0 ).toString( context );
186 
187  QVector< Page * >::const_iterator pIt = doc->m_pagesVector.constBegin(), pEnd = doc->m_pagesVector.constEnd();
188  for ( ; pIt != pEnd; ++pIt )
189  {
190  const QLinkedList< Okular::FormField * > pageFields = (*pIt)->formFields();
191  QLinkedList< Okular::FormField * >::const_iterator ffIt = pageFields.constBegin(), ffEnd = pageFields.constEnd();
192  for ( ; ffIt != ffEnd; ++ffIt )
193  {
194  if ( (*ffIt)->name() == cName )
195  {
196  return JSField::wrapField( context, *ffIt, *pIt );
197  }
198  }
199  }
200  return KJSUndefined();
201 }
202 
203 // Document.getPageLabel()
204 static KJSObject docGetPageLabel( KJSContext *ctx,void *object,
205  const KJSArguments &arguments )
206 {
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() );
211 }
212 
213 // Document.getPageRotation()
214 static KJSObject docGetPageRotation( KJSContext *ctx, void *object,
215  const KJSArguments &arguments )
216 {
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 );
221 }
222 
223 // Document.gotoNamedDest()
224 static KJSObject docGotoNamedDest( KJSContext *ctx, void *object,
225  const KJSArguments &arguments )
226 {
227  DocumentPrivate *doc = reinterpret_cast< DocumentPrivate* >( object );
228 
229  QString dest = arguments.at( 0 ).toString( ctx );
230 
231  DocumentViewport viewport( doc->m_generator->metaData( "NamedViewport", dest ).toString() );
232  if ( !viewport.isValid() )
233  return KJSUndefined();
234 
235  doc->m_parent->setViewport( viewport );
236 
237  return KJSUndefined();
238 }
239 
240 // Document.syncAnnotScan()
241 static KJSObject docSyncAnnotScan( KJSContext *, void *,
242  const KJSArguments & )
243 {
244  return KJSUndefined();
245 }
246 
247 void JSDocument::initType( KJSContext *ctx )
248 {
249  assert( g_docProto );
250 
251  static bool initialized = false;
252  if ( initialized )
253  return;
254  initialized = true;
255 
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 );
265 
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 );
274 
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 );
280 }
281 
282 KJSGlobalObject JSDocument::wrapDocument( DocumentPrivate *doc )
283 {
284  if ( !g_docProto )
285  g_docProto = new KJSPrototype();
286  return g_docProto->constructGlobalObject( doc );
287 }
docGetPageRotation
static KJSObject docGetPageRotation(KJSContext *ctx, void *object, const KJSArguments &arguments)
Definition: kjs_document.cpp:214
Okular::DocumentPrivate::m_url
KUrl m_url
Definition: document_p.h:197
docGetDataObjects
static KJSObject docGetDataObjects(KJSContext *ctx, void *object)
Definition: kjs_document.cpp:101
Okular::Document::setViewport
void setViewport(const DocumentViewport &viewport, DocumentObserver *excludeObserver=0, bool smoothMove=false)
Sets the current document viewport to the given viewport.
Definition: document.cpp:3128
Okular::Generator::embeddedFiles
virtual const QList< EmbeddedFile * > * embeddedFiles() const
Returns the 'list of embedded files' object of the document or 0 if no list of embedded files is avai...
Definition: generator.cpp:290
docGetPath
static KJSObject docGetPath(KJSContext *, void *object)
Definition: kjs_document.cpp:79
Okular::JSDocument::initType
static void initType(KJSContext *ctx)
Definition: kjs_document.cpp:247
QWidget
docGetField
static KJSObject docGetField(KJSContext *context, void *object, const KJSArguments &arguments)
Definition: kjs_document.cpp:180
docGetPermStatusReady
static KJSObject docGetPermStatusReady(KJSContext *, void *)
Definition: kjs_document.cpp:95
Okular::Generator::metaData
virtual QVariant metaData(const QString &key, const QVariant &option) const
This method returns the meta data of the given key with the given option of the document.
Definition: generator.cpp:328
docGetFilesize
static KJSObject docGetFilesize(KJSContext *, void *object)
Definition: kjs_document.cpp:71
docGetDocumentFileName
static KJSObject docGetDocumentFileName(KJSContext *, void *object)
Definition: kjs_document.cpp:63
docGetURL
static KJSObject docGetURL(KJSContext *, void *object)
Definition: kjs_document.cpp:87
kjs_field_p.h
Okular::DocumentPrivate::m_pagesVector
QVector< Page * > m_pagesVector
Definition: document_p.h:249
kjs_document_p.h
docSetPageNum
static void docSetPageNum(KJSContext *ctx, void *object, KJSObject value)
Definition: kjs_document.cpp:49
KEY_GET
#define KEY_GET(key, property)
Okular::Page
Collector for all the data belonging to a page.
Definition: page.h:49
Okular::Document::widget
QWidget * widget() const
Returns the widget to be used for relaying GUI things (messageboxes, ...)
Definition: document.cpp:2508
Okular::Generator::generateDocumentInfo
virtual const DocumentInfo * generateDocumentInfo()
Returns the general information object of the document or 0 if no information are available...
Definition: generator.cpp:275
Okular::DocumentPrivate::m_docSize
qint64 m_docSize
Definition: document_p.h:203
Okular::JSDocument::wrapDocument
static KJSGlobalObject wrapDocument(DocumentPrivate *doc)
Definition: kjs_document.cpp:282
Okular::DocumentPrivate::m_generator
Generator * m_generator
Definition: document_p.h:246
Okular::JSField::wrapField
static KJSObject wrapField(KJSContext *ctx, FormField *field, Page *page)
Definition: kjs_field.cpp:216
docGetExternal
static KJSObject docGetExternal(KJSContext *, void *object)
Definition: kjs_document.cpp:121
docGetNumPages
static KJSObject docGetNumPages(KJSContext *, void *object)
Definition: kjs_document.cpp:33
Okular::DocumentPrivate
Definition: document_p.h:83
Okular::DocumentInfo
A DOM tree containing information about the document.
Definition: document.h:1073
docGetPageLabel
static KJSObject docGetPageLabel(KJSContext *ctx, void *object, const KJSArguments &arguments)
Definition: kjs_document.cpp:204
DOCINFO_GET_METHOD
#define DOCINFO_GET_METHOD(key, name)
Definition: kjs_document.cpp:162
docGotoNamedDest
static KJSObject docGotoNamedDest(KJSContext *ctx, void *object, const KJSArguments &arguments)
Definition: kjs_document.cpp:224
g_docProto
static KJSPrototype * g_docProto
Definition: kjs_document.cpp:30
docSyncAnnotScan
static KJSObject docSyncAnnotScan(KJSContext *, void *, const KJSArguments &)
Definition: kjs_document.cpp:241
docGetPageNum
static KJSObject docGetPageNum(KJSContext *, void *object)
Definition: kjs_document.cpp:41
Okular::DocumentViewport
A view on the document.
Definition: document.h:1003
Okular::Page::label
QString label() const
Returns the label of the page, or a null string if not set.
Definition: page.cpp:626
Okular::Page::orientation
Rotation orientation() const
Returns the orientation of the page as defined by the document.
Definition: page.cpp:165
docGetInfo
static KJSObject docGetInfo(KJSContext *ctx, void *object)
Definition: kjs_document.cpp:133
Okular::DocumentPrivate::m_parent
Document * m_parent
Definition: document_p.h:187
kjs_data_p.h
Okular::Document::setViewportPage
void setViewportPage(int page, DocumentObserver *excludeObserver=0, bool smoothMove=false)
Sets the current document viewport to the given page.
Definition: document.cpp:3116
Okular::JSData::wrapFile
static KJSObject wrapFile(KJSContext *ctx, EmbeddedFile *f)
Definition: kjs_data.cpp:87
Okular::Document::currentPage
uint currentPage() const
Returns the number of the current page.
Definition: document.cpp:2647
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:45:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okular

Skip menu "okular"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegraphics API Reference

Skip menu "kdegraphics API Reference"
  •     libkdcraw
  •     libkexiv2
  •     libkipi
  •     libksane
  • okular

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal