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
main.cpp
Go to the documentation of this file.
1 /*
2 * Zuble - A run-time system for QML/Javascript applications
3 * Copyright (C) 2019 Bob Dinitto
4 *
5 * Filename: main.cpp
6 * Created on: 20-Aug-2019
7 * Author: Bob Dinitto
8 *
9 * Zuble is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
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, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24 #include <QCoreApplication>
25 #include <QQmlApplicationEngine>
26 #include <QPluginLoader>
27 #include <QDir>
28 #include <QtDebug>
29 #include "ZblApp.h"
30 #include "ZResourceEdifyIF.h"
31 #include <iostream>
32 
33 using namespace Zbl;
34 using namespace std;
35 
36 bool loadPlugin();
37 void printUsage();
38 void addImportPath(const QString& prefix, const QString& path, QQmlEngine* engine);
39 void addPath(const QString& prefix, const QString& path, QStringList& pathList);
40 void dumpPathList(const QStringList& list);
41 QString findScriptPath(const QString& name);
42 QStringList getPathList();
43 void listScripts();
44 QString getScriptPath();
45 void printScriptPath();
46 
47 static QString extension("qml");
48 static QString envPathName("ZUB_PATH");
49 static QChar pathSeparator(':');
50 static QString zubName;
51 
52 int main(int argc, char *argv[])
53 {
54  zubName = argv[0];
55 
56  zubName = zubName.right(zubName.length() - zubName.lastIndexOf('/')-1);
57 
58  if(argc < 2 || !strcmp(argv[1], "-h"))
59  {
60  printUsage();
61  return -1;
62  }
63  else if(!strcmp(argv[1], "-l"))
64  {
65  listScripts();
66  return 0;
67  }
68  else if(!strcmp(argv[1], "-p"))
69  {
71  return 0;
72  }
73 
74  QString scriptName(argv[1]);
75 
76  QString filePath = findScriptPath(scriptName);
77 
78  QCoreApplication a(argc, argv);
79 
80  QQmlApplicationEngine* engine = new QQmlApplicationEngine(&a);
81 
82  engine->installExtensions(QJSEngine::AllExtensions);
83 
84  QString installPrefix;
85 
86  installPrefix = QCoreApplication::applicationDirPath();
87  installPrefix += "/../";
88  installPrefix = QDir::cleanPath(installPrefix);
89  QString zAppDirPath = installPrefix + "/apps";
90 
91  qWarning("Install prefix: %s", installPrefix.toUtf8().constData());
92  qWarning("Zuble app directory: %s", zAppDirPath.toUtf8().constData());
93 
94  QStringList libPaths;
95 
96  //if(snapApp)
97  // libPaths = QCoreApplication::libraryPaths();
98 
99  addPath(installPrefix, "/lib", libPaths);
100  addPath("",".", libPaths);
101 
102  QCoreApplication::setLibraryPaths(libPaths);
103  engine->setPluginPathList(libPaths);
104 
105  engine->addImportPath("qrc:/"); // exposes /org/zuble/qml/qmldir to QML
106 
107  addImportPath(installPrefix, "/lib", engine); // exposes Zuble plugins to QML
108 
109  qDebug("Dumping library paths...");
110  libPaths = QCoreApplication::libraryPaths();
111  dumpPathList(libPaths);
112 
113  qDebug("Dumping plugin paths...");
114  libPaths = engine->pluginPathList();
115  dumpPathList(libPaths);
116 
117  loadPlugin();
118 
119  engine->load(filePath);
120 
121  qDebug() << "Returned from engine->load()";
122 
123  int status = a.exec();
124 
125  qDebug("Returned from exec() with value: %d", status);
126 
127  return status;
128 }
129 
130 void addImportPath(const QString& prefix, const QString& path, QQmlEngine* engine)
131 {
132  QString tempPath("%1%2");
133  tempPath = tempPath.arg(prefix).arg(path);
134 
135  qDebug("Adding import path: %s", tempPath.toUtf8().constData());
136 
137  engine->addImportPath(tempPath);
138 }
139 
140 void addPath(const QString& prefix, const QString& path, QStringList& pathList)
141 {
142  QString tempPath("%1%2");
143  tempPath = tempPath.arg(prefix).arg(path);
144 
145  qDebug("Adding path: %s", tempPath.toUtf8().constData());
146 
147  pathList.append(tempPath);
148 }
149 
150 void dumpPathList(const QStringList& list)
151 {
152  const int len = list.length();
153 
154  for(int i=0; i < len; i++)
155  {
156  qDebug("path: %s", list.at(i).toUtf8().constData() );
157  }
158 
159 }
160 
161 
162 
163 
165 {
166 
167  // if(hasArg("-zl"))
168  // logging = "on";
169  //
170  // registerConfigSettingsBundles
171  // if(logprofile)
172  // use it;
173  // else
174  // use default log profile;
175 
176  qDebug("Loading Zuble core plugin resources...");
177 
178  bool status = false;
179 
180  static const QString corePluginName = "libzblcore";
181 
182  QPluginLoader loader;
183 
184  loader.setFileName(corePluginName);
185 
186  if(!loader.load())
187  {
188  qWarning() << "zub failed to load core plugin: " << loader.errorString();
189  return status;
190  }
191 
192  Zbl::ZResourceEdifyIF* plugin =
193  qobject_cast<Zbl::ZResourceEdifyIF*>(loader.instance());
194 
195 
196  if(!plugin)
197  {
198  qWarning() << "Zuble core ERROR, core plugin doesn't implement ZResourceEdifyIF";
199 
200  return status;
201  }
202 
203  status = plugin->mapPluginResources(corePluginName.toUtf8(), true);
204 
205  if(!status)
206  {
207  qWarning() << "Zuble FAILED to locate and register core binary "
208  "resource files.";
209 
210  return status;
211  }
212 
213  return status;
214 
215 }
216 
217 // walks the ZUB_PATH looking for a script file named <name>.qml
218 
219 QString findScriptPath(const QString& scriptName)
220 {
221  QString path(getenv(envPathName.toUtf8().constData()));
222 
223  if(path.isEmpty())
224  {
225  qWarning() << "ERROR: No ZUB_PATH environment variable specified. "
226  "This should contain a list of directories for "
227  << zubName << " to search for QML files.";
228 
229  exit(-1);
230 
231  }
232  QString fullName(scriptName);
233 
234  if(fullName.lastIndexOf(extension) != fullName.size() - 4)
235  {
236  fullName += ".";
237  fullName += extension;
238  }
239 
240  QStringList folders = getPathList();
241 
242  qDebug() << "resolveProgramName, name: " << fullName;
243  qDebug() << "resolveProgramName, path: " << path;
244 
245  QDir dir;
246  QString fullPath;
247 
248  for(int i=0; i<folders.size(); i++)
249  {
250  qDebug() << "folder: " << folders.at(i) << endl;
251 
252  dir.setPath(folders.at(i));
253 
254  if(!dir.exists())
255  continue;
256 
257  if(dir.exists(fullName))
258  {
259  QFileInfo info = QFileInfo(dir, fullName);
260  fullPath = info.absoluteFilePath();
261  if(!info.isExecutable())
262  {
263  qWarning() << zubName << ": ERROR - File is not executable: "
264  << fullPath;
265  qWarning() << zubName << ": You must set file permissions to \"executable\" for file: "
266  << fullPath;
267  exit(-1);
268  }
269  return fullPath;
270  }
271  }
272 
273  qWarning() << zubName << ": ERROR - Script file not found: "
274  << fullName;
275 
276  exit(-1);
277 
278 }
279 
281 {
282  cout << zubName.toUtf8().constData()
283  << ": Listing Zuble scripts..." << endl;
284 
285  cout << envPathName.toUtf8().constData() << "="
286  << getScriptPath().toUtf8().constData() << endl << endl;
287 
288  QStringList folders = getPathList();
289 
290  QDir dir;
291  QStringList filters;
292  filters << "*.qml";
293  dir.setNameFilters(filters);
294  dir.setFilter(QDir::Executable | QDir::Files);
295  QString fullPath;
296  QString ext(".%1");
297  ext = ext.arg(extension);
298 
299  for(int i=0; i<folders.size(); i++)
300  {
301  cout << folders.at(i).toUtf8().constData() << ":" << endl;
302 
303  dir.setPath(folders.at(i));
304 
305  if(!dir.exists())
306  continue;
307 
308 
309  QStringList files = dir.entryList();
310 
311  for(int i=0; i<files.count(); i++)
312  {
313  QString name = files.at(i);
314 
315  int extIndex = name.lastIndexOf(ext);
316 
317  name = name.left(extIndex);
318 
319  cout << name.toUtf8().constData() << endl;
320  }
321  }
322 }
323 
324 QStringList getPathList()
325 {
326  QStringList folders = getScriptPath().split(pathSeparator);
327 
328  if(folders.isEmpty())
329  {
330  qWarning() << zubName << "WARNING: ZUB_PATH environment variable contains no directories"
331  " to search for QML files";
332 
333  exit(-1);
334  }
335 
336  return folders;
337 }
338 
339 QString getScriptPath()
340 {
341  QString path(getenv(envPathName.toUtf8().constData()));
342 
343  if(path.isEmpty())
344  {
345  qWarning() << "ERROR: No ZUB_PATH environment variable specified. "
346  "This should contain a list of directories for "
347  << zubName << " to search for QML files.";
348 
349  exit(-1);
350 
351  }
352 
353  return path;
354 }
355 
357 {
358  cout << zubName.toUtf8().constData() << " script path: "
359  << getScriptPath().toUtf8().constData() << endl;
360 }
361 
362 
364 {
365  cout << zubName.toUtf8().constData() << " usage: " << endl;
366  cout << " " << zubName.toUtf8().constData() << " <script-file>" << endl;
367  cout << " Loads and runs the non-gui QML script named <script-file>.qml" << endl;
368  cout << " on the path defined by environment variable ZUB_PATH." << endl;
369  cout << " " << zubName.toUtf8().constData() << " -l" << endl;
370  cout << " Lists QML scripts found on paths defined by ZUB_PATH environment variable." << endl;
371  cout << " Script files must have \".qml\" extension and executable permission." << endl;
372  cout << " " << zubName.toUtf8().constData() << " -h" << endl;
373  cout << " Prints this help message." << endl << endl;
374 }
static QString envPathName("ZUB_PATH")
QString findScriptPath(const QString &name)
Definition: main.cpp:219
void printScriptPath()
Definition: main.cpp:356
void addImportPath(const QString &prefix, const QString &path, QQmlEngine *engine)
Definition: main.cpp:576
STL namespace.
static QString zubName
Definition: main.cpp:50
virtual bool mapPluginResources(const char *fileName, bool qmlRegister)=0
static QString extension("qml")
Definition: ZAndGate.cpp:6
void addPath(const QString &prefix, const QString &path, QStringList &pathList)
Definition: main.cpp:566
QStringList getPathList()
Definition: main.cpp:324
bool loadPlugin()
Definition: main.cpp:164
void dumpPathList(const QStringList &list)
Definition: main.cpp:516
static QChar pathSeparator(':')
QString getScriptPath()
Definition: main.cpp:339
void listScripts()
Definition: main.cpp:280
int main(int argc, char *argv[])
Definition: main.cpp:65
void printUsage()
Definition: main.cpp:363
This interface allows Qt applications to access Zuble&#39;s platform-independent binary resource manageme...