WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZblApp.h
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2013, 2014 Bob Dinitto
4  *
5  * ZblApp.h
6  *
7  * Created on: Feb 3, 2013
8  * Author: Bob Dinitto bob@ninzo.com
9  *
10  * Zuble is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24 */
25 
26 #ifndef ZBLAPP_H
27 #define ZBLAPP_H
28 
29 #include "zblcore_global.h"
30 #include "zglobal.h"
31 #include "ZblPlugin.h"
32 #include <QObject>
33 #include <QQmlEngine>
34 #include <QJSEngine>
35 //#include <QFileDialog>
36 //#include <QMessageBox>
37 #include <QReadWriteLock>
38 #include <QThreadStorage>
39 
40 namespace Zbl
41 {
42 
43 // map of background script objects
45 typedef QMap<QString, ZScriptThread*> ZScriptMap;
46 
81 class ZBLCORESHARED_EXPORT ZblApp : public QObject
82 {
83  Q_OBJECT
84  Q_ENUMS(QStandardButton)
85 
86 
92  {
93  public:
95  TimerMethodCall(const QJSValue& func, const QJSValue& arg)
96  : timerFunc(func), timerArg(arg){}
97  TimerMethodCall(const TimerMethodCall& source)
98  {timerFunc = source.timerFunc;timerArg = source.timerArg;}
99 
100  QJSValue timerFunc;
101  QJSValue timerArg;
102 
103  };
104 
105  typedef QHash<int, TimerMethodCall> timerHash;
106  typedef QList<int> timerKeys;
107 
118  class AppPtr
119  {
120  public:
121  AppPtr(ZblApp* app){m_app = app;}
122 
124  };
125 
126 public:
127 
128 #if 0
129 
130  // This enum allows Zuble applications to display some Qt dialog boxes.
131 
132  enum QStandardButton {
133  // keep this in sync with QDialogButtonBox::StandardButton and QMessageDialogOptions::StandardButton
134  BtnNoButton = QMessageBox::NoButton,
135  BtnOk = QMessageBox::Ok,
136  BtnSave = QMessageBox::Save,
137  BtnSaveAll = QMessageBox::SaveAll,
138  BtnOpen = QMessageBox::Open,
139  BtnYes = QMessageBox::Yes,
140  BtnYesToAll = QMessageBox::YesToAll,
141  BtnNo = QMessageBox::No,
142  BtnNoToAll = QMessageBox::NoToAll,
143  BtnAbort = QMessageBox::Abort,
144  BtnRetry = QMessageBox::Retry,
145  BtnIgnore = QMessageBox::Ignore,
146  BtnClose = QMessageBox::Close,
147  BtnCancel = QMessageBox::Cancel,
148  BtnDiscard = QMessageBox::Discard,
149  BtnHelp = QMessageBox::Help,
150  BtnApply = QMessageBox::Apply,
151  BtnReset = QMessageBox::Reset,
152  BtnRestoreDefaults = QMessageBox::RestoreDefaults,
153 };
154  Q_DECLARE_FLAGS(QStandardButtons, QStandardButton)
155 
156 
157 #endif
158 
175  Q_PROPERTY(QVariant tags READ getAppTags CONSTANT)
176 
177 
182  Q_PROPERTY(QString errorMessage READ errorMessage)
183 
184 
193  Q_PROPERTY(QString rootPath READ getRootPath NOTIFY appPathsChanged)
194 
200  Q_PROPERTY(QString mainQmlPath READ getMainQmlPath NOTIFY appPathsChanged)
201 
202 #if 0
203 
208  Q_PROPERTY(QString codePath READ getCodePath)
209 
210 
215  Q_PROPERTY(QString dataPath READ getDataPath)
216 
217 #endif
218 
219 
220  public:
221 
222  explicit ZblApp(QJSEngine *scriptEngine,
223  ZScriptThread* container = nullptr,
224  QObject* parent = nullptr);
225 
226  virtual ~ZblApp();
227 
233  static ZblApp& zInstance();
234 
244  static QObject* getSingletonType(
245  QQmlEngine *engine, QJSEngine *scriptEngine);
246 
259  static int registerSingletonType();
260 
266  QJSEngine& engine();
267 
268 
274  QString errorMessage();
275 
276 
284  QVariant getAppTags();
285 
292  QString getRootPath();
293 
300  QString getMainQmlPath();
301 
302 
303 #if 0
304 
311  QString getCodePath();
312 
319  QString getDataPath();
320 #endif
321 
322 
332  Q_INVOKABLE bool isResourcePath(const QString& path);
333 
342  Q_INVOKABLE void addLibraryPath(const QString& path);
343 
344 
359  Q_INVOKABLE QString resolveAppPath(const QString& path, bool includeUrlScheme = true);
360 
375  Q_INVOKABLE QString resolveHomePath(const QString& path, bool includeUrlScheme = true);
376 
393  Q_INVOKABLE QString resolvePath(const QString& path, bool includeUrlScheme = true);
394 
395 
396 #if 0
397 
407  Q_INVOKABLE QVariant showInfo(QWidget * parent,
408  const QString & title,
409  const QString & text,
410  int buttons = ZblApp::BtnOk,
411  int defaultButton = ZblApp::BtnNoButton);
412 
413 #endif
414 
423  Q_INVOKABLE bool isInMainThread();
424 
425 #if 0
426 
437  Q_INVOKABLE void sync();
438 #endif
439 
447  Q_INVOKABLE void sleep(unsigned long milliseconds);
448 
449 
458  Q_INVOKABLE void sleepEvents(unsigned long milliseconds);
459 
460 
467  Q_INVOKABLE QVariant getAppData(int dataKey);
468 
474  Q_INVOKABLE QVariant getAppName();
475 
481  Q_INVOKABLE void status(const QString& text);
482 
517  Q_INVOKABLE QObject* newAppSettings(int scope,
518  const QString& application,
519  const QString& organization);
520 
526  Q_INVOKABLE QObject* newByteArray();
527 
533  Q_INVOKABLE QObject* newDataStream(QObject* byteArray);
534 
540  Q_INVOKABLE QObject* newDir();
541 
547  Q_INVOKABLE QObject* newFile();
548 
554  Q_INVOKABLE QObject* newFileSystemWatcher();
555 
561  Q_INVOKABLE QObject* newTableModel();
562 
568  Q_INVOKABLE QObject* newTextStream(const QString& text);
569 
577  Q_INVOKABLE QObject* newLogReader();
578 
593  Q_INVOKABLE QObject* newObject(const QString& sprocketUri, const QString& objectName);
594 
600  Q_INVOKABLE QObject* newProcess();
601 
607  Q_INVOKABLE QObject* newMailbox();
608 
614  Q_INVOKABLE QObject* newMailslot();
615 
623  Q_INVOKABLE QObject* newMap();
624 
630  Q_INVOKABLE QObject* newMessageQueue();
631 
637  Q_INVOKABLE QObject* newSchemaValidator();
638 
646  Q_INVOKABLE QObject* newScopedMap();
647 
662  Q_INVOKABLE QObject* newSettingsFile(const QString& filePath);
663 
673  Q_INVOKABLE QObject* newThread();
674 
685  Q_INVOKABLE QObject* newXmlQuery();
686 
703  Q_INVOKABLE QObject* bundleSettings(const QString& bundleID);
704 
717  Q_INVOKABLE QString bundleMetapath(const QString& bundleID);
718 
731  Q_INVOKABLE QString bundleMetadata(const QString& bundleID);
732 
751  Q_INVOKABLE bool import(
752  const QString& sprocketUri, int versionMajor, int versionMinor);
753 
779  Q_INVOKABLE bool mapResource(
780  const QString& uri,
781  int versionMajor,
782  int versionMinor,
783  const QString& fileName);
784 
785 
809  Q_INVOKABLE bool registerResource(
810  const QString& uri, int versionMajor, int versionMinor);
811 
812 
840  Q_INVOKABLE bool mapPluginResources(const QString& fileName, bool qmlregister = false);
841 
866  Q_INVOKABLE bool loadPlugin(const char* fileName, bool load = false);
867 
868 #if 0
869 
890  //Q_INVOKABLE bool loadResource(
891  // const QString& resourceUri, int versionMajor, int versionMinor);
892 
893 #endif
894 
900  Q_INVOKABLE QStringList getApplicationArguments();
901 
907  Q_INVOKABLE QStringList getSystemEnvironment();
908 
923  Q_INVOKABLE bool isInterruptRequested();
924 
925 
930  Q_INVOKABLE QString getCurrentThreadAddress();
931 
938  Q_INVOKABLE QString getObjectThreadAddress(QObject* object);
939 
946  Q_INVOKABLE QObject* getLogBuffer();
947 
957  Q_INVOKABLE QObject* getLogRules();
958 
959 
960 signals:
961 
968  void statusAvailable(const QString& text);
969 
976  void appPathsChanged();
977 
978 
985  void settingsBundleAvailable(QString bundleId);
986 
987 
995  void exit(int returnCode);
996 
997 
998 
999 
1000 public slots:
1001 
1002 
1003 
1004  // begin test methods
1005 
1010  void throwAnException();
1011 
1016  void throwDeeperException();
1017 
1022  void catchDeeperException();
1023 
1029  QVariant returnAnException();
1030 
1035  void throwException();
1036 
1037  // end test methods
1038 
1039 
1040 
1041 
1048  QJSValue error();
1049 
1058  void setError(QJSValue theObject);
1059 
1071  bool isError();
1072 
1079  void clearError();
1080 
1081 
1091  QJSValue convertEvaluateError(const QJSValue& evaluateError);
1092 
1099  QVariant include(const QString& fileUrl);
1100 
1101 
1112  //TBD: reverse timerArgument and milliseconds and give timerArgument a default value
1113 
1114  int startTimerInterval(
1115  QJSValue timerFunction,
1116  QJSValue timerArgument,
1117  int milliseconds);
1118 
1126  void stopTimerInterval(int timerID);
1127 
1133  void stopAllTimers();
1134 
1135 
1140  void collectGarbage();
1141 
1142 
1163  void alert(const QString signalID, QVariant payload, bool wait = false);
1164 
1165 
1166 
1175  void quit();
1176 
1177 
1178 
1187  QString readLine();
1188 
1198  void print(const QString& text, bool newLine=true);
1199 
1209  void printErr(const QString& text, bool newLine=true);
1210 
1211 
1217  void onDestroyed(QObject* obj); // Experimental
1218 
1219 
1220 
1221 private slots:
1222 
1233  void exitApp(int returnCode = 0);
1234 
1235 
1236 protected:
1237 
1238 
1240 
1249  virtual void timerEvent(QTimerEvent * event);
1250 
1255  void createTags();
1256 
1263  static QThreadStorage<AppPtr*> m_zApp;
1264 
1268  static QReadWriteLock m_lock;
1269 
1275  QJSEngine* m_engine;
1276 
1287 
1288 
1293  static QVariant m_tags;
1294 
1300  timerHash m_scriptTimers;
1301 
1302 
1308 
1309 
1310 };
1311 
1312 inline QJSEngine& ZblApp::engine()
1313 {
1314  if(!m_engine)
1315  throw ZblException("ZblApp::engine - ERROR: missing javascript engine");
1316 
1317  return *m_engine;
1318 }
1319 
1320 
1321 } // Zbl
1322 
1323 
1324 #endif // ZBLAPP_H
#define ZBLCORESHARED_EXPORT
ZblApp * m_app
Definition: ZblApp.h:123
static QThreadStorage< AppPtr * > m_zApp
ZblApp is a thread singleton - ie one ZblApp object exists per Zuble thread. This variable holds a po...
Definition: ZblApp.h:1263
#define ZBL_DECLARE_LOGGED_OBJECT
Definition: zglobal.h:94
A mechanism for safely holding a ZblApp object pointer in thread local storage.
Definition: ZblApp.h:118
QJSEngine * m_engine
The QJSEngine that owns the javascript context for this ZblApp object.
Definition: ZblApp.h:1275
QJSEngine & engine()
Returns the QJSEngine object associated with this object.
Definition: ZblApp.h:1312
TimerMethodCall(const TimerMethodCall &source)
Definition: ZblApp.h:97
AppPtr(ZblApp *app)
Definition: ZblApp.h:121
A thread class to support Zuble&#39;s background Javascript processing.
Definition: ZScriptThread.h:62
factoryMap m_factories
Maps sprocket plugin uri&#39;s to ZblFactory objects.
Definition: ZblApp.h:1307
static QVariant m_tags
A QVariantMap used to pass ZApplication enumeration values to Javascript programs.
Definition: ZblApp.h:1293
static QReadWriteLock m_lock
A lock for multi-threaded data access.
Definition: ZblApp.h:1268
timerHash m_scriptTimers
A hash of javascript function/argument pairs keyed by timer ID.
Definition: ZblApp.h:1300
Definition: ZAndGate.cpp:6
static int versionMinor
Definition: main.cpp:60
QMap< QString, ZScriptThread * > ZScriptMap
Definition: ZblApp.h:44
static int versionMajor
Definition: main.cpp:59
QHash< int, TimerMethodCall > timerHash
Definition: ZblApp.h:105
The primary QML API to the Zuble plugin library. Zuble applications access this object through the ja...
Definition: ZblApp.h:81
ZScriptThread * m_container
The ZScriptThread object that represents the thread in which ZblApp is running.
Definition: ZblApp.h:1286
bool loadPlugin()
Definition: main.cpp:164
This inner class allows ZblApp to store javascript function pointers that are used to provide timer s...
Definition: ZblApp.h:91
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
QList< int > timerKeys
Definition: ZblApp.h:106
QMap< QString, ZblFactory * > factoryMap
Map Zuble Sprocket uri&#39;s to Sprocket object factories.
Definition: ZblPlugin.h:56
TimerMethodCall(const QJSValue &func, const QJSValue &arg)
Definition: ZblApp.h:95