ArnLib  4.0.x
Active Registry Network
ArnQml.hpp
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 #ifndef ARNQML_HPP
33 #define ARNQML_HPP
34 
35 #include "ArnLib_global.hpp"
36 #include "ArnInterface.hpp"
37 #include "ArnItem.hpp"
38 #include "ArnMonitor.hpp"
39 #include "ArnRpc.hpp"
40 #include "XStringMap.hpp"
41 #include <QNetworkReply>
42 #include <QNetworkAccessManager>
43 #include <QVariantMap>
44 
45 #if QT_VERSION >= 0x050000
46 # include <QtQml>
47 # include <QQmlParserStatus>
48 # include <QQmlNetworkAccessManagerFactory>
49 # include <QQmlEngine>
50 # define QML_QUICK_TYPE 2
51 # define QML_ENGINE QQmlEngine
52 # define QML_PARSER_STATUS QQmlParserStatus
53 # define QML_NETACC_FACTORY QQmlNetworkAccessManagerFactory
54 # define QML_LIST_PROPERTY QQmlListProperty
55 #else
56 # include <QtDeclarative>
57 # include <QDeclarativeParserStatus>
58 # include <QDeclarativeNetworkAccessManagerFactory>
59 # include <QDeclarativeEngine>
60 # define QML_Qt4
61 # define QML_QUICK_TYPE 1
62 # define QML_ENGINE QDeclarativeEngine
63 # define QML_PARSER_STATUS QDeclarativeParserStatus
64 # define QML_NETACC_FACTORY QDeclarativeNetworkAccessManagerFactory
65 # define QML_LIST_PROPERTY QDeclarativeListProperty
66 #endif
67 
68 class QJSEngine;
69 class ArnNetworkAccessManagerFactory;
70 
71 
73 
183 class ARNLIBSHARED_EXPORT ArnQml : public QObject
184 {
185  Q_OBJECT
186 public:
187  struct UseFlags {
188  enum E {
190  ArnLib = 0x01,
192  MSystem = 0x02,
194  MQt = 0x04,
196  All = 0xff
197  };
199  };
200 
202 
206  static void setup( QML_ENGINE* qmlEngine, UseFlags flags = UseFlags::ArnLib);
207 
208  static ArnQml& instance();
209 
211 
215  static QString arnRootPath();
216 
218 
228  static void setArnRootPath( const QString& path);
229 
231  static QObject *constructorArnInterface( QML_ENGINE* engine, QJSEngine* scriptEngine);
233 
234 private:
236  ArnQml();
237  ArnQml( const ArnQml&);
238  ~ArnQml();
239  ArnQml& operator=( const ArnQml&);
240 
241  QString _arnRootPath;
242  ArnNetworkAccessManagerFactory* _arnNetworkAccessManagerFactory;
243  UseFlags _regedUse;
244 };
245 
247 
248 
249 
286 class ArnItemQml : public ArnItem, public QML_PARSER_STATUS
287 {
288  Q_OBJECT
289 
290 #ifdef QML_Qt4
291  Q_INTERFACES( QDeclarativeParserStatus)
292 #else
293  Q_INTERFACES( QQmlParserStatus)
294 #endif
295 
297  Q_PROPERTY( QString variantType READ variantType WRITE setVariantType NOTIFY variantTypeChanged)
299  Q_PROPERTY( bool useUuid READ useUuid WRITE setUseUuid NOTIFY dummyNotifier)
301  Q_PROPERTY( QString path READ path WRITE setPath NOTIFY pathChanged)
303  Q_PROPERTY( ArnInterface::DataType type
304  READ type NOTIFY valueChanged)
306  Q_PROPERTY( QVariant variant READ toVariant WRITE setVariant NOTIFY valueChanged)
308  Q_PROPERTY( QString string READ toString WRITE setValue NOTIFY valueChanged)
310  Q_PROPERTY( QByteArray bytes READ toByteArray WRITE setValue NOTIFY valueChanged)
312 #ifdef ARNREAL_FLOAT
313  Q_PROPERTY( float num READ toReal WRITE setValue NOTIFY valueChanged)
314 #else
315  Q_PROPERTY( double num READ toReal WRITE setValue NOTIFY valueChanged)
316 #endif
317  Q_PROPERTY( int intNum READ toInt WRITE setValue NOTIFY valueChanged)
320  Q_PROPERTY( bool biDirMode READ isBiDirMode WRITE setBiDirMode NOTIFY dummyNotifier)
322  Q_PROPERTY( bool pipeMode READ isPipeMode WRITE setPipeMode NOTIFY dummyNotifier)
324  Q_PROPERTY( bool saveMode READ isSaveMode WRITE setSaveMode NOTIFY dummyNotifier)
326  Q_PROPERTY( bool masterMode READ isMaster WRITE setMaster NOTIFY dummyNotifier)
328  Q_PROPERTY( bool autoDestroyMode READ isAutoDestroy WRITE setAutoDestroy NOTIFY dummyNotifier)
330  Q_PROPERTY( bool atomicOpProvider
331  READ isAtomicOpProvider WRITE setAtomicOpProvider NOTIFY dummyNotifier)
333  Q_PROPERTY( bool ignoreSameValue READ isIgnoreSameValue WRITE setIgnoreSameValue NOTIFY dummyNotifier)
335  Q_PROPERTY( int delay READ delay WRITE setDelay NOTIFY dummyNotifier)
336  // Q_PROPERTY( bool smTemplate READ isTemplate WRITE setTemplate)
337 
338 public slots:
340 
342  void setBits( int mask, int value)
343  {ArnItemB::setBits( mask, value);}
344 
346 
348  void addIntNum( int value)
349  {ArnItemB::addValue( value);}
350 
352 
354 #ifdef ARNREAL_FLOAT
355  void addNum( float value)
356 #else
357  void addNum( double value)
358 #endif
359  {ArnItemB::addValue( value);}
360 
362 
365  {ArnItemB::addMode( Arn::ObjectMode::fromInt( mode));}
366 
371  {return ArnInterface::ObjectMode( ArnItemB::getMode().toInt());}
372 
374 public:
375  explicit ArnItemQml( QObject* parent = arnNullptr);
376 
377  QString variantType() const;
378  void setVariantType( const QString& typeName);
379 
380  QString path() const;
381 
382  ArnInterface::DataType type() const
383  {return ArnInterface::DataType( ArnItem::type().toInt());}
384 
385  void setPath( const QString& path);
386 
387  void setVariant( const QVariant& value);
388 
389  void setBiDirMode( bool isBiDirMode);
390  void setPipeMode( bool isPipeMode);
391  void setMaster( bool isMaster);
392  void setAutoDestroy( bool isAutoDestroy);
393  void setSaveMode( bool isSaveMode);
394  void setAtomicOpProvider( bool isAtomicOpPv);
395  // bool isTemplate() const;
396  // void setTemplate( bool isTemplate);
397 
398  bool useUuid() const;
399  void setUseUuid( bool useUuid);
400 
401  virtual void classBegin();
402  virtual void componentComplete();
403 
404 signals:
405  void valueChanged();
406  void pathChanged();
407  void variantTypeChanged();
408  void dummyNotifier();
409 
410 protected:
411  virtual void itemUpdated( const ArnLinkHandle& handleData, const QByteArray* value = arnNullptr);
412  virtual void itemCreatedBelow( const QString& path);
413  virtual void itemModeChangedBelow( const QString& path, uint linkId, Arn::ObjectMode mode);
414  virtual void timerEvent( QTimerEvent* ev);
416 
417 private:
418  bool _isCompleted;
419  QString _path;
420  int _variantType;
421  bool _useUuid;
422 };
423 
424 
426 
449 {
450  Q_OBJECT
451 
452 #ifdef QML_Qt4
453  Q_INTERFACES( QDeclarativeParserStatus)
454 #else
455  Q_INTERFACES( QQmlParserStatus)
456 #endif
457 
459  Q_PROPERTY( QString clientId READ clientId WRITE setClientId NOTIFY dummyNotifier)
461  Q_PROPERTY( QString monitorPath READ monitorPath WRITE setMonitorPath NOTIFY pathChanged)
462 public slots:
464 
466  void reStart();
467 
469 public:
470  explicit ArnMonitorQml( QObject* parent = arnNullptr);
471 
472  void setClientId( const QString& id);
473  QString clientId() const;
474  void setMonitorPath( const QString& path);
475  QString monitorPath() const;
476 
477  virtual void classBegin();
478  virtual void componentComplete();
479 
480 signals:
481  void pathChanged();
482  void dummyNotifier();
483 
484 protected:
485  virtual QString outPathConvert( const QString& path);
486  virtual QString inPathConvert( const QString& path);
488 
489 private:
490  bool _isCompleted;
491  QString _path;
492  QString _clientId;
493 };
494 
495 
497 
551 class ArnSapiQml : public ArnRpc, public QML_PARSER_STATUS
552 {
553  Q_OBJECT
554 
555 #ifdef QML_Qt4
556  Q_INTERFACES( QDeclarativeParserStatus)
557 #else
558  Q_INTERFACES( QQmlParserStatus)
559 #endif
560 
562  Q_PROPERTY( QString pipePath READ pipePath WRITE setPipePath NOTIFY pathChanged)
564  Q_PROPERTY( Mode mode READ mode WRITE setMode NOTIFY dummyNotifier)
566  Q_PROPERTY( QObject* receiver READ receiver WRITE setReceiver NOTIFY dummyNotifier)
567 
569 
571  Q_PROPERTY( int heartBeatSend READ getHeartBeatSend WRITE setHeartBeatSend NOTIFY dummyNotifier)
572 
574 
576  Q_PROPERTY( int heartBeatCheck READ getHeartBeatCheck WRITE setHeartBeatCheck NOTIFY dummyNotifier)
577 public:
578  enum Mode {
598  UuidAutoDestroy = int(UuidPipe) | int(AutoDestroy)
599  };
600  Q_ENUMS( Mode)
601 
602 public slots:
604  {return ArnRpc::isHeartBeatOk();}
605 
607 public:
608  explicit ArnSapiQml( QObject* parent = arnNullptr);
609 
610  void setPipePath( const QString& path);
611  QString pipePath() const;
612 
613  Mode mode() const
614  {return Mode( ArnRpc::mode().toInt());}
615  void setMode( Mode m)
616  {ArnRpc::setMode( ArnRpc::Mode::fromInt(m));}
617 
618  virtual void classBegin();
619  virtual void componentComplete();
620 
621 signals:
622  void pathChanged();
623  void dummyNotifier();
625 
626 private:
627  bool _isCompleted;
628  QString _path;
629  QString _sendPrefix;
630  QString _providerPrefix;
631  QString _requesterPrefix;
632 };
633 
634 
635 namespace Arn {
636 
637 class XStringMapQml : public QObject, public QML_PARSER_STATUS, public XStringMap
638 {
639  Q_OBJECT
640 
641 #ifdef QML_Qt4
642  Q_INTERFACES( QDeclarativeParserStatus)
643 #else
644  Q_INTERFACES( QQmlParserStatus)
645 #endif
646 
648  Q_PROPERTY( QString xstring READ toXStringString WRITE fromXString NOTIFY dummyNotifier)
650  Q_PROPERTY( int size READ size NOTIFY dummyNotifier)
651 
652  // XStringMap& addNum( const QString& key, int val);
653  // XStringMap& addNum( const QString& key, uint val);
654  // XStringMap& addNum( const QString& key, double val, int precision = -1);
655  // XStringMap& operator+=( const XStringMap& other);
656  // XStringMap& operator+=( const QVariantMap& other);
657  // QByteArray info();
658 
659 public slots:
661 
663  void clear()
664  {XStringMap::clear( true);}
665 
666  int indexOf( const QString& key, int from = 0) const
667  {return XStringMap::indexOf( key, from);}
668 
669  int indexOfValue( const QString& value, int from = 0) const
670  {return XStringMap::indexOfValue( value, from);}
671 
672  QObject* add( const QString& key, const QString& val)
673  {XStringMap::add( key, val); return this;}
674 
675  QObject* add( QObject* other);
676 
677  QObject* set( int i, const QString& val)
678  {XStringMap::set( i, val); return this;}
679 
680  QObject* set( const QString& key, const QString& val)
681  {XStringMap::set( key, val); return this;}
682 
683  QString key( int i, const QString& def = QString()) const
684  {return XStringMap::keyString( i, def);}
685 
686  QString key( const QString& value, const QString& def = QString()) const
687  {return XStringMap::keyString( value, def);}
688 
689  QString value( int i, const QString& def = QString()) const
690  {return XStringMap::valueString( i, def);}
691 
692  QString value( const QString& key, const QString& def = QString()) const
693  {return XStringMap::valueString( key, def);}
694 
695  QObject* remove( int index)
696  {XStringMap::remove( index); return this;}
697 
698  QObject* remove( const QString& key)
699  {XStringMap::remove( key); return this;}
700 
701  QObject* removeValue( const QString& val)
702  {XStringMap::removeValue( val); return this;}
703 
705  {XStringMap::setEmptyKeysToValue();}
706 
707  QStringList keys() const
708  {return XStringMap::keys();}
709 
710  QStringList values() const
711  {return XStringMap::values();}
712 
714  {return XStringMap::toVariantMap( true);}
715 
717 public:
718  explicit XStringMapQml( QObject* parent = arnNullptr);
719 
720  virtual void classBegin();
721  virtual void componentComplete();
722 
723 signals:
724  void dummyNotifier();
725 
726 protected:
728 
729 private:
730  bool _isCompleted;
731 };
732 
733 
734 class QmlMSys : public QObject
735 {
736  Q_OBJECT
737 
738 public:
739  Q_PROPERTY( int quickTypeRun READ quickTypeRun)
740 
741 public slots:
742  QVariantMap xstringToEnum( const QString& xstring);
743 
745 public:
746  explicit QmlMSys( QObject* parent = arnNullptr);
747 
748  int quickTypeRun();
750 };
751 }
752 
753 
755 class ArnNetworkReply : public QNetworkReply
756 {
757  Q_OBJECT
758 public:
759  explicit ArnNetworkReply( QObject* parent = arnNullptr);
760 
761  void setOperation( QNetworkAccessManager::Operation operation)
762  { QNetworkReply::setOperation( operation);}
763 
764  void setRequest( const QNetworkRequest& request)
765  { QNetworkReply::setRequest( request);}
766 
767  void setUrl( const QUrl& url)
768  { QNetworkReply::setUrl( url);}
769 
770  bool isFinished() const;
771  qint64 bytesAvailable() const;
772  bool isSequential() const;
773  qint64 size() const;
774 
775  void setup( const QString& path);
776  QByteArray data() const;
777  void setData( const QByteArray& data);
778 
779 protected:
780  virtual void abort();
781  virtual qint64 readData( char *data, qint64 maxlen);
782 
783 private slots:
784  void postSetup();
785  void dataArived();
786 
787 private:
788  QString _arnPath;
789  QByteArray _data;
790  int _readPos;
791  bool _isFinished;
792  ArnItem _arnItem;
793 };
794 
795 
796 class ArnNetworkAccessManager : public QNetworkAccessManager
797 {
798  Q_OBJECT
799 public:
800  explicit ArnNetworkAccessManager( QObject* parent = arnNullptr);
801 
802 protected:
803  virtual QNetworkReply* createRequest( Operation op, const QNetworkRequest &request, QIODevice *outgoingData);
804 
805 private slots:
806 
807 private:
808 };
809 
810 
811 class ArnNetworkAccessManagerFactory : public QML_NETACC_FACTORY
812 {
813 public:
814  virtual QNetworkAccessManager* create( QObject *parent);
815 };
817 
818 #endif // ARNQML_HPP
Mode mode() const
Get the mode.
Definition: ArnRpc.cpp:386
bool isHeartBeatOk() const
Get the state of heart beat.
Definition: ArnRpc.cpp:432
QString value(const QString &key, const QString &def=QString()) const
Definition: ArnQml.hpp:692
void addMode(ArnInterface::ObjectMode mode)
Add general mode settings for this Arn Data Object
Definition: ArnQml.hpp:364
ARN Sapi QML.
Definition: ArnQml.hpp:551
When calling out, uses named argument e.g "myFunc count=123".
Definition: ArnRpc.hpp:105
QObject * removeValue(const QString &val)
Definition: ArnQml.hpp:701
QMultiMap< QString, QVariant > MQVariantMap
Definition: XStringMap.hpp:54
MQVariantMap toMap() const
Definition: ArnQml.hpp:713
Container class with string representation for serialized data.
Definition: XStringMap.hpp:107
void setMonitorPath(const QString &path, ArnClient *client=arnNullptr)
Set the path to be monitored.
Definition: ArnMonitor.cpp:134
Arn::DataType type() const
The type stored in the Arn Data Object
Definition: ArnItem.hpp:135
Arn::ObjectMode getMode() const
Use with care, link must be "referenced" before use, otherwise it might have been deleted...
#define MQ_DECLARE_FLAGS(FEStruct)
Flags.
Definition: MQFlagsBase.hpp:49
int indexOf(const QString &key, int from=0) const
Definition: ArnQml.hpp:666
int toInt(bool *isOk=arnNullptr) const
QString value(int i, const QString &def=QString()) const
Definition: ArnQml.hpp:689
ARN Item QML.
Definition: ArnQml.hpp:286
QString key(int i, const QString &def=QString()) const
Definition: ArnQml.hpp:683
QStringList keys() const
Definition: ArnQml.hpp:707
When receiver method missing, send defaultCall() signal instead of error.
Definition: ArnRpc.hpp:109
DataType
Data type of an Arn Data Object
QObject * add(const QString &key, const QString &val)
Definition: ArnQml.hpp:672
Remote Procedure Call.
Definition: ArnRpc.hpp:156
QString monitorPath() const
Get the monitored path
Definition: ArnMonitor.cpp:214
#define QML_PARSER_STATUS
Definition: ArnQml.hpp:63
int indexOfValue(const QString &value, int from=0) const
Definition: ArnQml.hpp:669
Check sequence order information from pipe. Can generate signal outOfSequence().
Definition: ArnRpc.hpp:101
void addValue(int value)
AtomicOp adds an integer to an Arn Data Object
#define QML_NETACC_FACTORY
Definition: ArnQml.hpp:64
void setBits(int mask, int value, int ignoreSame=Arn::SameValue::DefaultAction)
AtomicOp assign an integer to specified bits in an Arn Data Object
void setEmptyKeysToValue()
Definition: ArnQml.hpp:704
ObjectMode
General global mode of an Arn Data Object
ARN Monitor QML.
Definition: ArnQml.hpp:448
Provider side (opposed to requester)
Definition: ArnRpc.hpp:91
#define MQ_DECLARE_OPERATORS_FOR_FLAGS(FEStruct)
Definition: MQFlagsBase.hpp:65
Definition: Arn.cpp:43
QString path(Arn::NameF nameF=Arn::NameF::EmptyOk) const
Path of the Arn Data Object
void addMode(Arn::ObjectMode mode)
Add general mode settings for this Arn Data Object
When calling out, uses named argument with type e.g "myFunc count:int=123".
Definition: ArnRpc.hpp:107
Send sequence order information to pipe.
Definition: ArnRpc.hpp:99
Use AutoDestroy for the pipe, i.e. it is closed when tcp/ip is broken.
Definition: ArnRpc.hpp:93
QString key(const QString &value, const QString &def=QString()) const
Definition: ArnQml.hpp:686
Use an unique uuid in the pipe name.
Definition: ArnRpc.hpp:95
ARN QML.
Definition: ArnQml.hpp:183
#define QML_ENGINE
Definition: ArnQml.hpp:62
ArnInterface::ObjectMode getMode() const
Definition: ArnQml.hpp:370
If guarantied no default arguments, full member name overload is ok.
Definition: ArnRpc.hpp:97
bool isHeartBeatOk()
Definition: ArnQml.hpp:603
#define ARNLIBSHARED_EXPORT
void reStart()
Restart the monitor.
Definition: ArnQml.cpp:320
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
void addNum(double value)
AtomicOp adds an ARNREAL to an Arn Data Object
Definition: ArnQml.hpp:357
void addIntNum(int value)
AtomicOp adds an integer to an Arn Data Object
Definition: ArnQml.hpp:348
Handle for an Arn Data Object.
Definition: ArnItem.hpp:72
QStringList values() const
Definition: ArnQml.hpp:710
void setMode(Mode mode)
Definition: ArnRpc.cpp:378