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

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
  • script
scripthandler.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2010, 2011, 2012 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "scripthandler.h"
24 #include "scriptengineinitializer.h"
25 #include "scriptutils.h"
26 #include "scriptlogger.h"
27 #include "classes/defaultscriptclass.h"
28 #include "../datatypes/datainformation.h"
29 #include "../datatypes/topleveldatainformation.h"
30 #include "../datatypes/array/arraydatainformation.h"
31 #include "../parsers/parserutils.h"
32 
33 #include <QStringList>
34 #include <QScriptValue>
35 #include <QScriptValueIterator>
36 #include <QScriptEngine>
37 #include <QScriptEngineDebugger>
38 
39 ScriptHandler::ScriptHandler(QScriptEngine* engine, TopLevelDataInformation* topLevel)
40  : mEngine(engine), mTopLevel(topLevel), mHandlerInfo(engine, topLevel->logger())
41 #ifdef OKTETA_DEBUG_SCRIPT
42 , mDebugger(new QScriptEngineDebugger())
43 #endif
44 {
45 }
46 
47 ScriptHandler::~ScriptHandler()
48 {
49 }
50 
51 void ScriptHandler::validateData(DataInformation* data)
52 {
53  Q_CHECK_PTR(data);
54 
55  if (data->hasBeenValidated())
56  return;
57  //first validate the children
58  for (uint i = 0; i < data->childCount(); ++i)
59  validateData(data->childAt(i));
60 
61  //check if has a validation function:
62  QScriptValue validationFunc = data->validationFunc();
63  if (validationFunc.isValid())
64  {
65 #ifdef OKTETA_DEBUG_SCRIPT
66  mDebugger->attachTo(mEngine.data());
67  mDebugger->action(QScriptEngineDebugger::InterruptAction)->trigger();
68  kDebug() << "validating element: " << data->name();
69 #endif
70  QScriptValue result = callFunction(validationFunc, data, ScriptHandlerInfo::Validating);
71  if (result.isError())
72  {
73  mTopLevel->logger()->error(data) << "Error occurred while validating element: "
74  << result.toString();
75  data->setValidationError(QLatin1String("Error occurred in validation: ")
76  + result.toString());
77  }
78  else if (mEngine->hasUncaughtException())
79  {
80  mTopLevel->logger()->error(data) << "Error occurred while validating element:"
81  << result.toString() << "\nBacktrace:" << mEngine->uncaughtExceptionBacktrace();
82  data->setValidationError(QLatin1String("Error occurred in validation: ")
83  + result.toString());
84  mEngine->clearExceptions();
85  }
86  if (result.isBool() || result.isBoolean())
87  {
88  data->mValidationSuccessful = result.toBool();
89  }
90  if (result.isString())
91  {
92  //error string
93  QString str = result.toString();
94  if (!str.isEmpty())
95  data->setValidationError(str);
96  }
97  data->mHasBeenValidated = true;
98  }
99 }
100 
101 void ScriptHandler::updateDataInformation(DataInformation* data)
102 {
103  Q_CHECK_PTR(data);
104  //check if has an update function:
105  Q_ASSERT(!data->hasBeenUpdated());
106  QScriptValue updateFunc = data->updateFunc();
107  data->mHasBeenUpdated = true;
108  if (updateFunc.isValid())
109  {
110  QString context = data->fullObjectPath(); //we mustn't use data after updateFunc.call(), save context
111  QScriptValue result = callFunction(updateFunc, data, ScriptHandlerInfo::Updating);
112  if (result.isError())
113  {
114  mTopLevel->logger()->error(context) << "Error occurred while updating element: "
115  << result.toString();
116  }
117  if (mEngine->hasUncaughtException())
118  {
119  mTopLevel->logger()->error(context) << "Error occurred while updating element:"
120  << result.toString() << "\nBacktrace:" << mEngine->uncaughtExceptionBacktrace();
121  mEngine->clearExceptions();
122  }
123  }
124 }
125 
126 void ScriptHandler::updateLength(ArrayDataInformation* array)
127 {
128  QScriptValue lengthFunc = array->lengthFunction();
129  if (lengthFunc.isValid())
130  {
131  Q_ASSERT(lengthFunc.isFunction());
132 
133  QScriptValue result = callFunction(lengthFunc, array, ScriptHandlerInfo::DeterminingLength);
134  if (mEngine->hasUncaughtException())
135  {
136  mTopLevel->logger()->error(array) << "Error occurred while calculating length:"
137  << result.toString() << "\nBacktrace:" << mEngine->uncaughtExceptionBacktrace();
138  mEngine->clearExceptions();
139  }
140  ParsedNumber<uint> value = ParserUtils::uintFromScriptValue(result);
141  if (value.isValid)
142  array->setArrayLength(value.value);
143  else
144  array->logError() << "Length function did not return a valid number! Result was: " << result.toString();
145  }
146 }
147 
148 QString ScriptHandler::customToString(const DataInformation* data, const QScriptValue& func)
149 {
150  Q_ASSERT(func.isValid());
151  Q_ASSERT(func.isFunction());
152  Q_ASSERT(data->wasAbleToRead()); //this should never be called if EOF was reached
153  //it is effectively const, since nothing may be modified while mode is CustomToString
154  //const_cast is okay in this case
155  QScriptValue result = callFunction(func, const_cast<DataInformation*>(data), ScriptHandlerInfo::CustomToString);
156  if (result.isError())
157  data->logError() << "toStringFunc caused an error:" << result.toString();
158  return result.toString();
159 }
160 
161 
162 QScriptValue ScriptHandler::callFunction(QScriptValue func, DataInformation* data,
163  ScriptHandlerInfo::Mode mode)
164 {
165  Q_ASSERT(func.isFunction());
166  //value exists, we assume it has been checked to be a function
167  QScriptValue thisObject = data->toScriptValue(mEngine.data(), &mHandlerInfo);
168  QScriptValue mainStruct = data->mainStructure()->toScriptValue(mEngine.data(), &mHandlerInfo);
169  QScriptValueList args;
170  args << mainStruct;
171  //ensure we get the right properties
172  mHandlerInfo.setMode(mode);
173  QScriptValue result = func.call(thisObject, args);
174  mHandlerInfo.setMode(ScriptHandlerInfo::None);
175  return result;
176 }
177 
DataInformation
Interface that must be implemented by all datatypes.
Definition: datainformation.h:67
DataInformation::name
QString name() const
Definition: datainformation.h:258
ArrayDataInformation::setArrayLength
bool setArrayLength(uint newLength)
Definition: arraydatainformation.cpp:68
DataInformation::fullObjectPath
QString fullObjectPath() const
Definition: datainformation.cpp:258
DataInformation::mValidationSuccessful
bool mValidationSuccessful
Definition: datainformation.h:240
scriptengineinitializer.h
DataInformation::wasAbleToRead
bool wasAbleToRead() const
Definition: datainformation.h:283
DataInformation::childCount
virtual unsigned int childCount() const =0
Definition: datainformation.h:278
ParsedNumber::value
T value
Definition: parserutils.h:88
scripthandler.h
ParsedNumber
Holds a number that was converted either from a QScriptValue or a QString.
Definition: parserutils.h:84
defaultscriptclass.h
ScriptHandlerInfo::DeterminingLength
Definition: scripthandlerinfo.h:46
ArrayDataInformation::lengthFunction
QScriptValue lengthFunction() const
Definition: arraydatainformation.h:173
TopLevelDataInformation::logger
ScriptLogger * logger() const
Definition: topleveldatainformation.h:174
ScriptHandler::~ScriptHandler
virtual ~ScriptHandler()
Definition: scripthandler.cpp:47
DataInformation::hasBeenUpdated
bool hasBeenUpdated() const
Definition: datainformation.h:339
ArrayDataInformation
Definition: arraydatainformation.h:36
DataInformation::mainStructure
DataInformation * mainStructure()
Definition: datainformation.cpp:85
ScriptHandler::ScriptHandler
ScriptHandler(QScriptEngine *engine, TopLevelDataInformation *topLevel)
Definition: scripthandler.cpp:39
ScriptHandlerInfo::Validating
Definition: scripthandlerinfo.h:46
ParserUtils::uintFromScriptValue
ParsedNumber< uint > uintFromScriptValue(const QScriptValue &val)
Definition: parserutils.cpp:103
ScriptHandler::updateLength
void updateLength(ArrayDataInformation *array)
Definition: scripthandler.cpp:126
DataInformation::childAt
virtual DataInformation * childAt(unsigned int) const =0
Definition: datainformation.h:268
DataInformation::validationFunc
QScriptValue validationFunc() const
Definition: datainformation.h:359
TopLevelDataInformation
Definition: topleveldatainformation.h:46
ScriptHandler::callFunction
QScriptValue callFunction(QScriptValue func, DataInformation *data, ScriptHandlerInfo::Mode mode)
Definition: scripthandler.cpp:162
ScriptHandler::updateDataInformation
void updateDataInformation(DataInformation *data)
The pointer may be changed while updating, CHECK AS SOON AS FUNCTION RETURNS!
Definition: scripthandler.cpp:101
scriptlogger.h
DataInformation::updateFunc
QScriptValue updateFunc() const
Definition: datainformation.h:354
DataInformation::mHasBeenUpdated
bool mHasBeenUpdated
Definition: datainformation.h:242
DataInformation::toScriptValue
virtual QScriptValue toScriptValue(QScriptEngine *engine, ScriptHandlerInfo *handlerInfo)
This method is virtual since DummyDataInformation has to override it.
Definition: datainformation.cpp:275
ScriptHandlerInfo::Mode
Mode
The type of function that is being evaluated (most writing is only allowed when updating) ...
Definition: scripthandlerinfo.h:45
ScriptHandler::customToString
QString customToString(const DataInformation *data, const QScriptValue &func)
Definition: scripthandler.cpp:148
DataInformation::hasBeenValidated
bool hasBeenValidated() const
Definition: datainformation.h:349
ScriptHandlerInfo::setMode
void setMode(Mode m)
Definition: scripthandlerinfo.h:59
ScriptLogger::error
QDebug error(const DataInformation *origin)
Definition: scriptlogger.h:68
ScriptHandler::validateData
void validateData(DataInformation *data)
Definition: scripthandler.cpp:51
ScriptHandlerInfo::None
Definition: scripthandlerinfo.h:46
DataInformation::mHasBeenValidated
bool mHasBeenValidated
Definition: datainformation.h:241
ParsedNumber::isValid
bool isValid
Definition: parserutils.h:89
scriptutils.h
ScriptHandlerInfo::CustomToString
Definition: scripthandlerinfo.h:47
ScriptHandlerInfo::Updating
Definition: scripthandlerinfo.h:46
DataInformation::logError
QDebug logError() const
just a shorthand for logger->error(this)
Definition: datainformation.h:324
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

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

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

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