ArnLib  4.0.x
Active Registry Network
ArnScript.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnInc/ArnScript.hpp"
33 #include "ArnInc/ArnDepend.hpp"
34 #include "ArnInc/ArnMonitor.hpp"
35 #include "ArnInc/Arn.hpp"
36 #include <QFile>
37 #include <QDebug>
38 
39 #ifdef ARNUSE_SCRIPTJS
40 #include <QJSValue>
41 #include <QJSEngine>
42 #include <QObject>
43 
44 
45 ArnJsGlobal::ArnJsGlobal( ArnScript& arnScript, QObject* parent)
46  : QObject( parent)
47  , _arnScript( arnScript)
48 {
49 }
50 
51 
52 void ArnJsGlobal::print( const QString &txt)
53 {
54  _arnScript.errorLog( txt, ArnError::Info);
55 }
56 
57 
59 
60 ArnScript::ArnScript( QObject* parent) :
61  QObject( parent)
62 {
63  init( arnNullptr);
64 }
65 
66 
67 ArnScript::ArnScript( ARN_JSENGINE* engine, QObject* parent) :
68  QObject( parent)
69 {
70  init( engine);
71 }
72 
73 
75 {
76  return *_engine;
77 }
78 
79 
80 void ArnScript::addObject( const QString& id, QObject* obj)
81 {
82  if ((id.isEmpty()) || (obj == arnNullptr)) return;
83 
84  if ((obj != this) && (!obj->parent()))
85  obj->setParent( this); // Reparent to this script handler
86 
87  ARN_JSVALUE jsObj = _engine->newQObject( obj);
88  _engine->globalObject().setProperty( id, jsObj);
89 }
90 
91 
92 bool ArnScript::evaluate( const QByteArray& script, const QString& idName, const QString& typeName)
93 {
94  _idName = idName;
95  ARN_JSVALUE result = _engine->evaluate( QString::fromUtf8( script.constData()));
96  bool isOk = doJsResult( result, typeName);
97  return isOk;
98 }
99 
100 
101 bool ArnScript::evaluateFile( const QString& fileName)
102 {
103  QFile file( fileName);
104  file.open( QIODevice::ReadOnly);
105 
106  return evaluate( file.readAll(), fileName);
107 }
108 
109 
110 QJSValue ArnScript::globalProperty( const QString& id)
111 {
112  return _engine->globalObject().property( id);;
113 }
114 
115 
116 QJSValue ArnScript::callFunc( QJSValue& func, const QJSValue& thisObj, const QJSValueList& args)
117 {
118  QJSValue result = func.callWithInstance( thisObj, args);
119  doJsResult( result);
120 
121  return result;
122 }
123 
124 
125 QString ArnScript::idName() const
126 {
127  return _idName;
128 }
129 
130 
131 void ArnScript::setInterruptedText( const QString& interruptedText)
132 {
133  _interruptedText = interruptedText;
134 }
135 
136 
137 bool ArnScript::doJsResult( const ARN_JSVALUE& jsResult, const QString& typeName)
138 {
139  bool isAborted = _engine->isInterrupted();
140  if (isAborted) {
141  _engine->setInterrupted( false);
142  }
143  bool isError = jsResult.isError();
144  if (!isError && !isAborted) return true;
145 
146  // Exception properties: name, message, fileName, lineNumber, stack
147  int lineNo = jsResult.property("lineNumber").toInt();
148  QString lineTxt = lineNo > 0 ? " @line:" + QString::number( lineNo) : QString();
149  QString errTxt;
150  if (!typeName.isEmpty()) errTxt += "From type " + typeName + ": ";
151  errTxt += isAborted ? _interruptedText : jsResult.toString();
152  errTxt += lineTxt;
154 
155  return false;
156 }
157 
158 
159 void ArnScript::init( ARN_JSENGINE* engine)
160 {
161  _interruptedText = "JS-Engine interrupted";
162 
163  if (engine)
164  _engine = engine;
165  else
166  _engine = new ARN_JSENGINE( this);
167 
169  // TODO: vararg print
170  // QJSValue print = fEngine->evaluate("function() { printCallback(Array.prototype.slice.apply(arguments));}");
171  // fEngine->globalObject().setProperty("print", print);
172  ArnJsGlobal* arnJsGlobal = new ArnJsGlobal( *this, this);
173  QJSValue jsGlobalAdd = _engine->newQObject( arnJsGlobal);
174  QJSValue jsPrintFunc = jsGlobalAdd.property("print");
175  _engine->globalObject().setProperty( "print", jsPrintFunc);
176 
178  QJSValue jsMetaObject;
179  jsMetaObject = _engine->newQMetaObject( &ArnItemJs::staticMetaObject);
180  _engine->globalObject().setProperty( "ArnItem", jsMetaObject);
181 
182  jsMetaObject = _engine->newQMetaObject( &ArnMonitorJs::staticMetaObject);
183  _engine->globalObject().setProperty( "ArnMonitor", jsMetaObject);
184 
185  jsMetaObject = _engine->newQMetaObject( &ArnDepOfferJs::staticMetaObject);
186  _engine->globalObject().setProperty( "ArnDependOffer", jsMetaObject);
187 
188  jsMetaObject = _engine->newQMetaObject( &ArnDepJs::staticMetaObject);
189  _engine->globalObject().setProperty( "ArnDepend", jsMetaObject);
191 
192  _engine->globalObject().setProperty( "arn", _engine->newQObject( new ArnInterface( this)));
193 }
194 
195 
196 void ArnScript::errorLog( const QString& errText, ArnError err, void* reference)
197 {
198  QString scriptText = " Script:" + _idName;
199 
200  ArnM::errorLog( errText + scriptText, err, reference);
201  emit errorText( errText);
202 }
203 
204 
206 
207 void ArnItemJs::init()
208 {
209  QObject::connect( this, static_cast<void(ArnItem::*)(void)>(&ArnItem::changed),
210  this, static_cast<void(ArnItemJs::*)(void)>(&ArnItemJs::changedVoid));
211  QObject::connect( this, static_cast<void(ArnItem::*)(ARNREAL)>(&ArnItem::changed),
212  this, static_cast<void(ArnItemJs::*)(ARNREAL)>(&ArnItemJs::changedNum));
213  QObject::connect( this, static_cast<void(ArnItem::*)(const QString&)>(&ArnItem::changed),
214  this, static_cast<void(ArnItemJs::*)(const QString&)>(&ArnItemJs::changedString));
215 }
216 
217 
218 ArnItemJs::ArnItemJs( QObject* parent)
219  : ArnItem( parent)
220 {
221  init();
222 }
223 
224 
225 ArnItemJs::ArnItemJs( const QString& path, QObject* parent)
226  : ArnItem( path, parent)
227 {
228  _path = path;
229  init();
230 }
231 
232 
233 ArnItemJs::ArnItemJs( const QJSValue& itemTemplate, const QString& path, QObject* parent)
234  : ArnItem( arnItemFromJsValue( itemTemplate, *this), path, parent)
235 {
236  _path = path;
237  init();
238 }
239 
240 
241 QString ArnItemJs::variantType() const
242 {
243  if (!_variantType) return QString();
244 
245  const char* typeName = QMetaType::typeName(_variantType);
246  if (!typeName) return QString();
247 
248  return typeName;
249 }
250 
251 
252 void ArnItemJs::setVariantType( const QString& typeName)
253 {
254  if (typeName.isEmpty()) {
255  _variantType = 0;
256  }
257  else {
258  int type = QMetaType::type( typeName.toLatin1().constData());
259  if (!type) {
260  qWarning() << "ItemJS setVariantType, Unknown: type=" + typeName + " path=" + path();
261  return;
262  }
263 
264  _variantType = type;
265  }
266 }
267 
268 
269 QString ArnItemJs::path() const
270 {
271  return _path;
272 }
273 
274 
275 void ArnItemJs::setPath( const QString& path)
276 {
277  _path = path;
278  if (_useUuid)
279  openUuid( path);
280  else
281  open( path);
282 }
283 
284 
285 void ArnItemJs::setVariant( const QVariant& value)
286 {
287  if (!_variantType) // No variantType, no conversion
288  ArnItem::setValue( value);
289  else { // Use variantType
290  QVariant val = value;
291  if (val.convert( QVariant::Type( _variantType))) {
292  ArnItem::setValue( val);
293  }
294  else {
295  qWarning() << "ItemJS setVariant, Can't convert: type="
296  << _variantType << " path=" + path();
297  }
298  }
299 }
300 
301 
302 void ArnItemJs::setBiDirMode( bool isBiDirMode)
303 {
304  if (isBiDirMode)
306 }
307 
308 
309 void ArnItemJs::setPipeMode( bool isPipeMode)
310 {
311  if (isPipeMode)
313 }
314 
315 
316 void ArnItemJs::setMaster( bool isMaster)
317 {
318  if (isMaster)
320 }
321 
322 
323 void ArnItemJs::setAutoDestroy( bool isAutoDestroy)
324 {
325  if (isAutoDestroy)
327 }
328 
329 
330 void ArnItemJs::setSaveMode( bool isSaveMode)
331 {
332  if (isSaveMode)
334 }
335 
336 
337 void ArnItemJs::setAtomicOpProvider( bool isAtomicOpPv)
338 {
339  if (isAtomicOpPv)
341 }
342 
343 
344 bool ArnItemJs::useUuid() const
345 {
346  return _useUuid;
347 }
348 
349 
350 void ArnItemJs::setUseUuid( bool useUuid)
351 {
352  _useUuid = useUuid;
353 }
354 
355 
357 
358 ArnMonitorJs::ArnMonitorJs( QObject* parent)
359  : ArnMonitor( parent)
360 {
361 }
362 
363 
364 void ArnMonitorJs::reStart()
365 {
367 }
368 
369 
371 
372 ArnDepOfferJs::ArnDepOfferJs( QObject *parent)
373  : ArnDependOffer( parent)
374 {
375 }
376 
377 
378 void ArnDepOfferJs::advertise( const QString &serviceName)
379 {
380  ArnDependOffer::advertise( serviceName);
381 }
382 
383 
385 
386 ArnDepJs::ArnDepJs( QObject *parent)
387  : ArnDepend( parent)
388 {
389 }
390 
391 
392 void ArnDepJs::add( const QString &serviceName, int stateId)
393 {
394  ArnDepend::add( serviceName, stateId);
395 }
396 
397 
398 void ArnDepJs::add( const QString &serviceName, const QString &stateName)
399 {
400  ArnDepend::add( serviceName, stateName);
401 }
402 
403 
404 void ArnDepJs::setMonitorName( const QString &name)
405 {
407 }
408 
409 
410 void ArnDepJs::startMonitor()
411 {
413 }
414 
415 
416 const ArnItem& ArnItemJs::arnItemFromJsValue( const QJSValue& jsValue, QObject& defParent)
417 {
418  // When jsValue not contains an ArnItem, return a fresh ArnItem owned by defParent
419  const ArnItem* arnItem = qobject_cast<const ArnItem*>( jsValue.toQObject());
420  return arnItem ? *arnItem : *new ArnItem( &defParent);
421 }
422 
423 #else
424 #include <QtScript>
425 #include <QScriptValue>
426 #include <QScriptEngine>
427 
428 Q_DECLARE_METATYPE(ArnItemScr*)
429 Q_DECLARE_METATYPE(ArnMonitor*)
430 Q_DECLARE_METATYPE(ArnDependOffer*)
431 Q_DECLARE_METATYPE(ArnDepend*)
432 
433 
434 void ArnItemScr::init()
435 {
436  _defaultType = 0;
437 #if QT_VERSION >= 0x050200
438  QObject::connect( this, static_cast<void(ArnItem::*)(void)>(&ArnItem::changed),
439  this, static_cast<void(ArnItemScr::*)(void)>(&ArnItemScr::changedVoid));
440  QObject::connect( this, static_cast<void(ArnItem::*)(ARNREAL)>(&ArnItem::changed),
441  this, static_cast<void(ArnItemScr::*)(ARNREAL)>(&ArnItemScr::changedNum));
442  QObject::connect( this, static_cast<void(ArnItem::*)(const QString&)>(&ArnItem::changed),
443  this, static_cast<void(ArnItemScr::*)(const QString&)>(&ArnItemScr::changedString));
444 #else
445  connect( this, SIGNAL(changed()), this, SIGNAL(changedVoid()));
446  #ifdef ARNREAL_FLOAT
447  connect( this, SIGNAL(changed(float)), this, SIGNAL(changedNum(float)));
448  #else
449  connect( this, SIGNAL(changed(double)), this, SIGNAL(changedNum(double)));
450  #endif
451  connect( this, SIGNAL(changed(QString)), this, SIGNAL(changedString(QString)));
452 #endif
453 }
454 
455 
456 ArnItemScr::ArnItemScr( QObject* parent) :
457  ArnItem( parent)
458 {
459  init();
460 }
461 
462 
463 ArnItemScr::ArnItemScr( const QString& path, QObject* parent) :
464  ArnItem( path, parent)
465 {
466  init();
467 }
468 
469 
470 ArnItemScr::ArnItemScr( const ArnItem& itemTemplate, const QString& path, QObject* parent) :
471  ArnItem( itemTemplate, path, parent)
472 {
473  init();
474 }
475 
476 
477 ArnItemScr::~ArnItemScr()
478 {
479 }
480 
481 
482 ArnScript::ArnScript( QObject* parent) :
483  QObject( parent)
484 {
485  init( arnNullptr);
486 }
487 
488 
489 ArnScript::ArnScript( QScriptEngine* engine, QObject* parent) :
490  QObject( parent)
491 {
492  init( engine);
493 }
494 
495 
496 QScriptEngine& ArnScript::engine() const
497 {
498  return *_engine;
499 }
500 
501 
502 void ArnScript::addObject( const QString& id, QObject* obj)
503 {
504  if ((id.isEmpty()) || (obj == arnNullptr)) return;
505 
506  if ((obj != this) && (!obj->parent()))
507  obj->setParent( this); // Reparent to this script handler
508 
509  ARN_JSVALUE objScr = _engine->newQObject( obj, QScriptEngine::QtOwnership,
510  QScriptEngine::ExcludeSuperClassContents);
511  _engine->globalObject().setProperty( id, objScr);
512 }
513 
514 
515 bool ArnScript::evaluate( const QByteArray& script, const QString& idName, const QString& typeName)
516 {
517  _idName = idName;
518  QScriptValue result = _engine->evaluate( QString::fromUtf8( script.constData()));
519  if (logUncaughtError( result, typeName)) {
520  return false;
521  }
522  return true;
523 }
524 
525 
526 bool ArnScript::evaluateFile( const QString& fileName)
527 {
528  QFile file( fileName);
529  file.open( QIODevice::ReadOnly);
530 
531  return evaluate( file.readAll(), fileName);
532 }
533 
534 
535 QScriptValue ArnScript::globalProperty( const QString& id)
536 {
537  return _engine->globalObject().property( id);;
538 }
539 
540 
541 QScriptValue ArnScript::callFunc( QScriptValue& func, const QScriptValue& thisObj, const QScriptValueList& args)
542 {
543  QScriptValue result = func.call( thisObj, args);
544  logUncaughtError( result);
545 
546  return result;
547 }
548 
549 
550 bool ArnScript::logUncaughtError( QScriptValue& scriptValue, const QString& typeName)
551 {
552  // qDebug() << "logUncaughtError: has=" << _engine->hasUncaughtException();
553  if (_engine->hasUncaughtException()) {
554  QString errDesc = scriptValue.toString();
555  // qDebug() << "logUncaughtError: errDesc=" << errDesc;
556  if (!errDesc.isEmpty()) {
557  QString preTxt = typeName.isEmpty() ? QString() : ("From type " + typeName + ": ");
558  int lineNo = _engine->uncaughtExceptionLineNumber();
559  errorLog( preTxt + errDesc + " @line:" + QString::number( lineNo),
561  }
562  return true;
563  }
564  return false;
565 }
566 
567 
568 QString ArnScript::idName() const
569 {
570  return _idName;
571 }
572 
573 
574 void ArnScript::doSignalException(const QScriptValue& exception)
575 {
576  int lineNo = _engine->uncaughtExceptionLineNumber();
577  errorLog( exception.toString() + " sig@line:" + QString::number( lineNo),
579 }
580 
581 
582 QScriptValue ArnScript::printFunction( QScriptContext* context, QScriptEngine* engine)
583 {
584  QString result;
585  for (int i = 0; i < context->argumentCount(); ++i) {
586  if (i > 0)
587  result.append(" ");
588  result.append( context->argument(i).toString());
589  }
590 
591  QScriptValue calleeData = context->callee().data();
592  ArnScript* arnScript = qobject_cast<ArnScript*>( calleeData.toQObject());
593  Q_ASSERT( arnScript);
594  arnScript->errorLog( result, ArnError::Info);
595 
596  return engine->undefinedValue();
597 }
598 
599 
600 void ArnScript::init( QScriptEngine* engine)
601 {
602  if (engine)
603  _engine = engine;
604  else
605  _engine = new QScriptEngine( this);
606  connect( _engine, SIGNAL(signalHandlerException(QScriptValue)), this, SLOT(doSignalException(QScriptValue)));
607 
609  QScriptValue printFunc = _engine->newFunction( ArnScript::printFunction);
610  printFunc.setData( _engine->newQObject( this)); // Save internal pointer to this ArnScript
611  _engine->globalObject().setProperty("print", printFunc);
612 
613 
615  _itemProto = new ArnItemProto( this);
616  _monitorProto = new ArnMonitorProto( this);
617  _depOfferProto = new ArnDepOfferProto( this);
618  _depProto = new ArnDepProto( this);
619 
620  QScriptValue itemProtoScr = _engine->newQObject( _itemProto);
621  QScriptValue monitorProtoScr = _engine->newQObject( _monitorProto);
622  QScriptValue depOfferProtoScr = _engine->newQObject( _depOfferProto);
623  QScriptValue depProtoScr = _engine->newQObject( _depProto);
624 
625  _engine->setDefaultPrototype( qMetaTypeId<ArnItemScr*>(), itemProtoScr);
626  _engine->setDefaultPrototype( qMetaTypeId<ArnMonitor*>(), monitorProtoScr);
627  _engine->setDefaultPrototype( qMetaTypeId<ArnDependOffer*>(), depOfferProtoScr);
628  _engine->setDefaultPrototype( qMetaTypeId<ArnDepend*>(), depProtoScr);
629 
630  QScriptValue itemConstrScr = _engine->newFunction( ArnItemProto::constructor, itemProtoScr);
631  QScriptValue monitorConstrScr = _engine->newFunction( ArnMonitorProto::constructor, monitorProtoScr);
632  QScriptValue depOfferConstrScr = _engine->newFunction( ArnDepOfferProto::constructor, depOfferProtoScr);
633  QScriptValue depConstrScr = _engine->newFunction( ArnDepProto::constructor, depProtoScr);
634 
635  _engine->globalObject().setProperty("ArnItem", itemConstrScr);
636  _engine->globalObject().setProperty("ArnMonitor", monitorConstrScr);
637  _engine->globalObject().setProperty("ArnDependOffer", depOfferConstrScr);
638  _engine->globalObject().setProperty("ArnDepend", depConstrScr);
639 
640  // Add properties to prototypes (manually)
641  itemProtoScr.setProperty("num", _engine->newFunction( ArnItemProto::getSetNum),
642  QScriptValue::PropertyGetter|QScriptValue::PropertySetter);
644 
645  _engine->globalObject().setProperty( "arn", _engine->newQObject( new ArnInterface( this),
646  QScriptEngine::QtOwnership, QScriptEngine::ExcludeSuperClassContents));
647 }
648 
649 
650 void ArnScript::errorLog( const QString& errText, ArnError err, void* reference)
651 {
652  QString scriptText = " Script:" + _idName;
653 
654  ArnM::errorLog( errText + scriptText, err, reference);
655  emit errorText( errText);
656 }
657 
658 
660 
661 ArnItemProto::ArnItemProto( ArnScript* parent)
662 : QObject( parent)
663 {
664 }
665 
666 
667 QString ArnItemProto::defaultType() const
668 {
669  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
670  if (!item) return QString();
671  if (!item->_defaultType) return QString();
672 
673  const char* typeName = QMetaType::typeName( item->_defaultType);
674  if (!typeName) return QString();
675 
676  return typeName;
677 }
678 
679 
680 void ArnItemProto::setDefaultType( const QString &typeName)
681 {
682  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
683  if (!item) return;
684 
685  if (typeName.isEmpty()) {
686  item->_defaultType = 0;
687  return;
688  }
689 
690  int type = QMetaType::type( typeName.toLatin1().constData());
691  if (!type) {
692  context()->throwError( QScriptContext::TypeError,
693  "Setting unknown defaultType=" + typeName);
694  return;
695  }
696 
697 
698  item->_defaultType = type;
699 }
700 
701 
702 QString ArnItemProto::path() const
703 {
704  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
705  if (item) return item->path();
706  return QString();
707 }
708 
709 
710 void ArnItemProto::setPath( const QString &path)
711 {
712  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
713  if (item) item->open( path);
714 }
715 
716 
717 QVariant ArnItemProto::value() const
718 {
719  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
720  if (!item) return QVariant();
721 
722  QVariant val = item->toVariant();
723  int type = val.type();
724 
725  switch (type) {
726  case QMetaType::QTime:
727  val = QVariant( QDateTime( QDate( 1, 1, 1), val.toTime()));
728  break;
729  default:
730  break;
731  }
732 
733  return val;
734 }
735 
736 
737 void ArnItemProto::setValue( const QVariant &value)
738 {
739  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
740  if (!item) return;
741 
742  if (!item->_defaultType) // No defaultType, no conversion
743  item->setValue( value);
744  else { // Use defaultType
745  QVariant val = value;
746  if (val.convert( QVariant::Type( item->_defaultType))) {
747  item->setValue( val);
748  }
749  else {
750  context()->throwError( QScriptContext::TypeError,
751  "Can't convert to defaultType=" + defaultType());
752  }
753  }
754 }
755 
756 
757 QString ArnItemProto::string() const
758 {
759  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
760  if (item) return item->toString();
761  return QString();
762 }
763 
764 
765 void ArnItemProto::setString( const QString &value)
766 {
767  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
768  if (item) item->setValue( value);
769 }
770 
771 
772 bool ArnItemProto::isPipeMode() const
773 {
774  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
775  if (item) return item->isPipeMode();
776  return false;
777 }
778 
779 
780 void ArnItemProto::setPipeMode( bool /*isPipeMode*/)
781 {
782  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
783  if (item) item->setPipeMode();
784 }
785 
786 
787 bool ArnItemProto::isMaster() const
788 {
789  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
790  if (item) return item->isMaster();
791  return false;
792 }
793 
794 
795 void ArnItemProto::setMaster( bool /*isMaster*/)
796 {
797  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
798  if (item)
799  item->setMaster();
800 }
801 
802 
803 bool ArnItemProto::isAutoDestroy() const
804 {
805  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
806  if (item) return item->isAutoDestroy();
807  return false;
808 }
809 
810 
811 void ArnItemProto::setAutoDestroy( bool /*isAutoDestroy*/)
812 {
813  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
814  if (item) item->setAutoDestroy();
815 }
816 
817 
818 bool ArnItemProto::isSaveMode() const
819 {
820  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
821  if (item) return item->isSaveMode();
822  return false;
823 }
824 
825 
826 void ArnItemProto::setSaveMode( bool /*isSaveMode*/)
827 {
828  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
829  if (item) item->setSaveMode();
830 }
831 
832 
833 bool ArnItemProto::isTemplate() const
834 {
835  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
836  if (item) return item->isTemplate();
837  return false;
838 }
839 
840 
841 void ArnItemProto::setTemplate( bool isTemplate)
842 {
843  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( thisObject());
844  if (item) item->setTemplate( isTemplate);
845 }
846 
847 
848 QScriptValue ArnItemProto::getSetNum(QScriptContext* context, QScriptEngine* engine)
849 {
850  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( context->thisObject());
851  QScriptValue result;
852  if (context->argumentCount() == 1) { // Set property
853  result = context->argument(0);
854  // Bugg in qt ScriptValue::toNumber (only gives float), using toString as workaround
855  item->setValue( result.toString());
856  //qDebug() << "ArnScr set path=" << item->path() << " value=" << result.toString();
857  }
858  else { // Get property
859  result = QScriptValue( engine, qsreal( item->toDouble()));
860  }
861  return result;
862 }
863 
864 
865 QScriptValue ArnItemProto::constructor( QScriptContext* context, QScriptEngine* engine)
866 {
867  if (!context->isCalledAsConstructor()) {
868  return context->throwError( QScriptContext::SyntaxError,
869  "use the 'new' operator");
870  }
871 
872  ArnItemScr* arnItem;
873  if (context->argumentCount() >= 2) { // (Item, Path) as arguments
874  ArnItemScr* item = qscriptvalue_cast<ArnItemScr*>( context->argument(0));
875  if (!item) {
876  return context->throwError(QScriptContext::TypeError,
877  "is not ArnItem as first argument");
878  }
879  QString path = qscriptvalue_cast<QString>( context->argument(1));
880  if (path.isNull()) {
881  return context->throwError(QScriptContext::TypeError,
882  "is not String (path) as second argument");
883  }
884  arnItem = new ArnItemScr( *item, path);
885  }
886  else if (context->argumentCount() >= 1) { // Path as argument
887  QString path = qscriptvalue_cast<QString>( context->argument(0));
888  if (path.isNull()) {
889  return context->throwError(QScriptContext::TypeError,
890  "is not String (path) as first argument");
891  }
892  arnItem = new ArnItemScr( path);
893  }
894  else { // No argument
895  arnItem = new ArnItemScr;
896  }
897 
898  // let the engine manage the new object's lifetime.
899  return engine->newQObject( arnItem, QScriptEngine::ScriptOwnership);
900 }
901 
902 
904 
905 ArnMonitorProto::ArnMonitorProto( ArnScript* parent)
906 : QObject( parent)
907 {
908 }
909 
910 
911 void ArnMonitorProto::reStart()
912 {
913  ArnMonitor* arnMon = qscriptvalue_cast<ArnMonitor*>( thisObject());
914  if (arnMon) arnMon->reStart();
915 }
916 
917 
918 void ArnMonitorProto::setClientId( const QString& id)
919 {
920  ArnMonitor* arnMon = qscriptvalue_cast<ArnMonitor*>( thisObject());
921  ArnScript* arnScr = qobject_cast<ArnScript*>(parent());
922  if (arnMon && arnScr) {
923  arnMon->setClient( id);
924  }
925 }
926 
927 
928 QString ArnMonitorProto::clientId() const
929 {
930  ArnMonitor* arnMon = qscriptvalue_cast<ArnMonitor*>( thisObject());
931  if (arnMon) return arnMon->clientId();
932  return QString();
933 }
934 
935 
936 void ArnMonitorProto::setMonitorPath( const QString& name)
937 {
938  ArnMonitor* arnMon = qscriptvalue_cast<ArnMonitor*>( thisObject());
939  if (arnMon) arnMon->start( name, arnMon->client());
940 }
941 
942 
943 QString ArnMonitorProto::monitorPath() const
944 {
945  ArnMonitor* arnMon = qscriptvalue_cast<ArnMonitor*>( thisObject());
946  if (arnMon) return arnMon->monitorPath();
947  return QString();
948 }
949 
950 
951 QScriptValue ArnMonitorProto::constructor( QScriptContext* context, QScriptEngine* engine)
952 {
953  if (!context->isCalledAsConstructor()) {
954  return context->throwError( QScriptContext::SyntaxError,
955  "use the 'new' operator");
956  }
957  ArnMonitor* arnMon = new ArnMonitor;
958  // let the engine manage the new object's lifetime.
959  return engine->newQObject( arnMon, QScriptEngine::ScriptOwnership);
960 }
961 
962 
964 
965 ArnDepOfferProto::ArnDepOfferProto( ArnScript* parent)
966 : QObject( parent)
967 {
968 }
969 
970 
971 void ArnDepOfferProto::advertise( const QString& serviceName)
972 {
973  ArnDependOffer* depOffer = qscriptvalue_cast<ArnDependOffer*>( thisObject());
974  if (depOffer) depOffer->advertise( serviceName);
975 }
976 
977 
978 void ArnDepOfferProto::setStateName( const QString& name)
979 {
980  ArnDependOffer* depOffer = qscriptvalue_cast<ArnDependOffer*>( thisObject());
981  if (depOffer) depOffer->setStateName( name);
982 }
983 
984 
985 QString ArnDepOfferProto::stateName() const
986 {
987  ArnDependOffer* depOffer = qscriptvalue_cast<ArnDependOffer*>( thisObject());
988  if (depOffer) return depOffer->stateName();
989  return QString();
990 }
991 
992 
993 void ArnDepOfferProto::setStateId(int id)
994 {
995  ArnDependOffer* depOffer = qscriptvalue_cast<ArnDependOffer*>( thisObject());
996  if (depOffer) depOffer->setStateId( id);
997 }
998 
999 
1000 int ArnDepOfferProto::stateId() const
1001 {
1002  ArnDependOffer* depOffer = qscriptvalue_cast<ArnDependOffer*>( thisObject());
1003  if (depOffer) return depOffer->stateId();
1004  return 0;
1005 }
1006 
1007 
1008 QScriptValue ArnDepOfferProto::constructor( QScriptContext* context, QScriptEngine* engine)
1009 {
1010  if (!context->isCalledAsConstructor()) {
1011  return context->throwError( QScriptContext::SyntaxError,
1012  "use the 'new' operator");
1013  }
1014  ArnDependOffer* depOffer = new ArnDependOffer;
1015  // let the engine manage the new object's lifetime.
1016  return engine->newQObject( depOffer, QScriptEngine::ScriptOwnership);
1017 }
1018 
1019 
1021 
1022 ArnDepProto::ArnDepProto( ArnScript* parent)
1023 : QObject( parent)
1024 {
1025 }
1026 
1027 
1028 void ArnDepProto::add( const QString& serviceName, const QString& stateName)
1029 {
1030  ArnDepend* dep = qscriptvalue_cast<ArnDepend*>( thisObject());
1031  // qDebug() << "DepProto add serv=" << serviceName << " stateName=" << stateName;
1032  if (dep) dep->add( serviceName, stateName);
1033 }
1034 
1035 
1036 void ArnDepProto::add( const QString& serviceName, int stateId)
1037 {
1038  ArnDepend* dep = qscriptvalue_cast<ArnDepend*>( thisObject());
1039  // qDebug() << "DepProto add serv=" << serviceName << " stateId=" << stateId;
1040  if (dep) dep->add( serviceName, stateId);
1041 }
1042 
1043 
1044 void ArnDepProto::setMonitorName( const QString& name)
1045 {
1046  ArnDepend* dep = qscriptvalue_cast<ArnDepend*>( thisObject());
1047  // qDebug() << "DepProto set monitorName=" << name;
1048  if (dep) dep->setMonitorName( name);;
1049 }
1050 
1051 
1052 void ArnDepProto::startMonitor()
1053 {
1054  ArnDepend* dep = qscriptvalue_cast<ArnDepend*>( thisObject());
1055  if (dep) dep->startMonitor();
1056 }
1057 
1058 
1059 QScriptValue ArnDepProto::constructor( QScriptContext* context, QScriptEngine* engine)
1060 {
1061  if (!context->isCalledAsConstructor()) {
1062  return context->throwError( QScriptContext::SyntaxError,
1063  "use the 'new' operator");
1064  }
1065  ArnDepend* dep = new ArnDepend;
1066  // let the engine manage the new object's lifetime.
1067  return engine->newQObject( dep, QScriptEngine::ScriptOwnership);
1068 }
1069 #endif
QString stateName() const
Definition: ArnDepend.cpp:106
void reStart()
The monitor is restarted.
Definition: ArnMonitor.cpp:222
ARN_JSVALUE globalProperty(const QString &id)
Definition: ArnScript.cpp:535
void setValue(const ArnItem &other, int ignoreSame=Arn::SameValue::DefaultAction)
Assign the value of an other ArnItem to an Arn Data Object
Definition: ArnItem.hpp:440
void add(const QString &serviceName, int stateId=-1)
Add a dependency for a service
Definition: ArnDepend.cpp:221
ArnItem & setAutoDestroy()
Set client session sync mode as AutoDestroy for this ArnItem.
Definition: ArnItem.hpp:258
QScriptEngine * _engine
Definition: ArnScript.hpp:436
QString idName() const
Definition: ArnScript.cpp:568
bool evaluate(const QByteArray &script, const QString &idName, const QString &typeName=QString())
Definition: ArnScript.cpp:515
void setStateId(int id)
Set the state of the service by an id number.
Definition: ArnDepend.cpp:114
Class for advertising that a service is available.
Definition: ArnDepend.hpp:59
ArnItem & setPipeMode()
Set general mode as Pipe for this Arn Data Object
Definition: ArnItem.hpp:211
void addObject(const QString &id, QObject *obj)
Definition: ArnScript.cpp:502
void setClient(ArnClient *client)
Set the client to be used.
Definition: ArnMonitor.cpp:101
static QScriptValue printFunction(QScriptContext *context, QScriptEngine *engine)
Definition: ArnScript.cpp:582
void setInterruptedText(const QString &interruptedText)
ArnItem & setMaster()
Set client session sync mode as Master for this ArnItem.
Definition: ArnItem.hpp:244
ArnItem & setBiDirMode()
Set general mode as Bidirectional for this Arn Data Object
Definition: ArnItem.hpp:195
QString monitorPath() const
Get the monitored path
Definition: ArnMonitor.cpp:214
void setAtomicOpProvider()
Set this Arn Data Object as Atomic Operator Provider
#define ARN_JSVALUE
Definition: ArnScript.hpp:55
bool start(const QString &path, ArnClient *client)
Starts the monitoring.
Definition: ArnMonitor.cpp:142
void changed()
Signals emitted when data in Arn Data Object is changed.
#define ARN_JSENGINE
Definition: ArnScript.hpp:54
ArnClient * client() const
Get the used client
Definition: ArnMonitor.cpp:126
void setMonitorName(const QString &name)
Set an optional monitor name for debugging.
Definition: ArnDepend.cpp:229
Class for setting up dependencis to needed services.
Definition: ArnDepend.hpp:132
ArnDepProto * _depProto
Definition: ArnScript.hpp:440
ArnItem & setSaveMode()
Set general mode as Save for this Arn Data Object
Definition: ArnItem.hpp:228
ArnDepOfferProto * _depOfferProto
Definition: ArnScript.hpp:439
void setStateName(const QString &name)
Set the state of the service by a logic name.
Definition: ArnDepend.cpp:98
#define ARNREAL
Definition: Arn.hpp:44
void advertise(const QString &serviceName)
Advertise an available service
Definition: ArnDepend.cpp:76
QScriptEngine & engine() const
Definition: ArnScript.cpp:496
ArnMonitorProto * _monitorProto
Definition: ArnScript.hpp:438
void errorLog(const QString &errText, ArnError err=ArnError::Undef, void *reference=arnNullptr)
Definition: ArnScript.cpp:650
ArnScript(QObject *parent=arnNullptr)
Definition: ArnScript.cpp:482
bool evaluateFile(const QString &fileName)
Definition: ArnScript.cpp:526
bool logUncaughtError(QScriptValue &scriptValue, const QString &typeName=QString())
Definition: ArnScript.cpp:550
static void errorLog(QString errText, ArnError err=ArnError::Undef, void *reference=arnNullptr)
Definition: ArnM.cpp:1025
int stateId() const
Definition: ArnDepend.cpp:122
void errorText(QString txt)
ArnItemProto * _itemProto
Definition: ArnScript.hpp:437
ARN_JSVALUE callFunc(ARN_JSVALUE &func, const ARN_JSVALUE &thisObj, const ARN_JSVALUE_LIST &args)
Definition: ArnScript.cpp:541
A client remote monitor to detect changes at server.
Definition: ArnMonitor.hpp:65
QString clientId() const
Get the id name of the used client
Definition: ArnMonitor.cpp:117
Handle for an Arn Data Object.
Definition: ArnItem.hpp:72
void startMonitor()
Starting the dependency monitor.
Definition: ArnDepend.cpp:237