33 #include <QXmlResultItems> 36 #include <QPluginLoader> 37 #include <QJsonDocument> 38 #include <QJsonObject> 69 "Zbl::ZblCog::Zblcore programming error: " 70 "Only one instance of ZblCog class should exist per linux process.");
77 QWriteLocker lock(&
m_lock);
79 QList<ZblSprocket*> sprockets(
m_sprockets.values());
81 for(
int i=0; i < sprockets.size(); i++)
82 delete sprockets.at(i);
84 QList<ZblResource*> resources(
m_resources.values());
86 for(
int i=0; i < resources.size(); i++)
87 delete resources.at(i);
96 zDebug() <<
"ZblCog::registerSprocket:" 103 QWriteLocker lock(&
m_lock);
109 zWarning() <<
"ZblCog::registerSprocket FAILED: sprocket already" 115 sv =
new ZblSprocket(uri, versionMajor, versionMinor);
119 zWarning() <<
"ZblCog::registerSprocket FAILED: can't create " 146 QReadLocker lock(&
m_lock);
152 zWarning() <<
"ZblCog::registerSprocketObject FAILED: " 153 "no sprocket plugin is registered for uri: " 154 << uri <<
"; Call Zbl::ZblCog::registerSprocket " 155 "before calling this method.";
160 uri, versionMajor, versionMinor, qmlName, createFunc);
164 const char* sprocketUri,
169 QReadLocker lock(&
m_lock);
175 zWarning() <<
"ZblCog::createFactory FAILED: no sprocket plugin " 176 "exists for uri: " << sprocketUri;
184 zWarning() <<
"ZblCog::createFactory FAILED: can't construct " 190 QList<QString> foundaryKeys(s->
m_objects.keys());
192 for(
int i=0; i < foundaryKeys.size(); i++)
194 const QString qmlName(foundaryKeys.at(i));
197 versionMajor, versionMinor,
cStr(qmlName));
206 "ZblCog::createFactory WARNING: Sprocket contains no objects";
221 uri, versionMajor, versionMinor));
223 zDebug() <<
"ZblCog::mapResource - mapping resource: " 228 QWriteLocker lock(&
m_lock);
234 zWarning() <<
"ZblCog::mapResource FAILED: resource already" 235 "registered as: " << key;
240 rs =
new ZblResource(uri, versionMajor, versionMinor, fileName);
244 zWarning() <<
"ZblCog::registerResource FAILED: can't create " 245 "resource object for resource: " << key;
252 zWarning() <<
"ZblCog::mapResource FAILED: can't locate " 253 "resource file: " << fileName <<
254 " for resource: " << key;
272 zDebug() <<
"ZblCog::mapResource - mapped resource: " 281 static const QString label(
"ZblCog::mapPluginResources");
287 zWarning() << label <<
" - FAILED - " <<
288 "invalid configuration file path: NULL";
292 zDebug() <<
"Mapping plugin file resources: " 295 QPluginLoader loader;
297 loader.setFileName(fileName);
299 QJsonObject metaObject = loader.metaData();
303 QJsonDocument doc(metaObject);
305 QByteArray ba = doc.toJson();
307 zDebug() <<
"plugin metadata = " << ba.constData();
308 zDebug() <<
"plugin file name = " << loader.fileName();
313 QJsonObject metaData = metaObject.value(
"MetaData").toObject();
315 QJsonArray resourceBundle = metaData.value(
"Resources").toArray();
317 if(resourceBundle.isEmpty())
320 <<
" - WARNING: plugin file %s has no resource metadata: " 326 zDebug() <<
"got resource metadata!";
328 const int bundleSize = resourceBundle.size();
330 for(
int i=0; i < bundleSize; i++)
332 QJsonValue nextResource;
334 nextResource = resourceBundle.at(i);
343 zDebug() << label <<
"processing resource metadata: " << uri;
347 int numMajor = vMajor.toInt(&numOK);
352 <<
" - WARNING - can't load resource, metadata has invalid value - " 353 "major-version should be an integer string, ie \"1\"," 363 int numMinor = vMinor.toInt(&numOK);
368 <<
" - WARNING - can't load resource, metadata has invalid value - " 369 "minor-version should be an integer string, ie \"0\"," 380 <<
" - WARNING - Zuble failed to map resource: " 391 <<
" - WARNING - Qt failed to register resource: " 401 <<
" - WARNING - resource metadata was invalid for plugin file: " 418 static const QString uriName(
"uri");
419 static const QString filenameName(
"fileName");
420 static const QString vMajorName(
"version-major");
421 static const QString vMinorName(
"version-minor");
423 if(resourceValue.isObject())
425 QJsonObject resObject(resourceValue.toObject());
441 const QString& paramKey,
446 value = resource.value(paramKey);
450 outValue = value.toString();
460 static const QString label(
"ZblCog::mapConfigResources");
466 zWarning() <<
"ZblCog::mapAppResources - FAILED - " <<
467 "invalid configuration file path: NULL";
471 zDebug() <<
"Mapping configuration file resources: " 475 QFile cfg(configFilePath);
480 QString(
"Nonexistent configuration file: %1").arg(configFilePath));
489 QUrl url(QUrl::fromLocalFile(configFilePath));
491 zDebug() << label <<
"setting XQuery focus to config file: " << url.path();
493 QString query =
"declare default element namespace 'http://zuble.org/schema/zuble/zblconfig'; \n" 494 "declare function local:pluginElement($action as xs:string, $pe as element()?) as xs:string* \n" 495 "{ (string-join(($action,\"plugin\",\"1\"),\"::\"),xs:string($pe/filename)) }; \n" 496 "declare function local:fileElement($action as xs:string, $fe as element()?) as xs:string* \n" 497 "{ (string-join(($action,\"file\",\"4\"), \"::\"), \n" 498 " xs:string($fe/uri), xs:string($fe/version-major), \n" 499 " xs:string($fe/version-minor),xs:string($fe/filename)) }; \n" 500 "for $map in /zblconfig/resources/resource-map \n" 501 " let $plugin := $map/plugin let $file := $map/file \n" 502 " return (if($plugin) then local:pluginElement($map/@action,$plugin) \n" 503 " else if($file) then local:fileElement($map/@action,$file) \n" 504 " else string-join((\"ignore\",\"invalid\",\"0\"), \"::\"))";
507 bool status = xq.setFocus(url);
512 QString(
"QXmlQuery can't load configuration file: %1").arg(url.path().toUtf8().constData()));
517 zDebug() <<
"Query=" << query;
524 QString(
"Programming error: invalid XQuery: %1").arg(query.toUtf8().constData()));
531 QStringList xmlResults;
533 xq.evaluateTo(&xmlResults);
535 const int count = xmlResults.count();
537 zDebug() <<
"Dumping XQuery evaluation results ++++++++++++++++++++++++++";
539 for(
int x=0; x<count; x++)
540 zDebug() << xmlResults.at(x).toUtf8().constData();
544 zDebug() <<
"End dumping XQuery evaluation results ++++++++++++++++++++++++++";
549 for(
int j=0; j<count; j++)
564 const QString& strAction(command.at(
cmdAction));
569 zDebug() <<
"Resource command - Action: " << strAction
570 <<
"Type: " << strResourceType
571 <<
"Size: " << strSize;
575 const int size = strSize.toInt(&intOK);
580 "ZblCog::mapConfigResources - WARNING: invalid command format, " 581 "third field should contain an integer specifying the number " 582 "of command parameter strings that follow this command string " 583 "in the string array: " 593 zWarning() <<
"ZblCog::mapConfigResources - " 594 "WARNING: invalid XML configuration file format - " 595 "an invalid XML node or text was encountered and ignored " 605 zWarning() <<
"ZblCog::mapConfigResources - " 606 "WARNING: invalid command format - skipping this command -" 607 "first field should contain a valid action string - " 608 "instead it contained: " << strAction;
631 xmlResults.at(j+
pluginName).toUtf8().constData(),
635 "ZblCog::mapConfigResources - " 636 "WARNING: failed to map plugin resources for plugin: " 660 "ZblCog::mapConfigResources - WARNING: invalid file resource parameter - " 661 "version-major should contain an integer specifying the major " 662 "version number of the resource file to be mapped - " 663 "instead it contains: " 676 "ZblCog::mapConfigResources - WARNING: invalid file resource parameter - " 677 "version-mainor should contain an integer specifying the minor " 678 "version number of the resource file to be mapped - " 679 "instead it contains: %s" 688 xmlResults.at(j+
fileUri).toUtf8().constData(),
691 xmlResults.at(j+
fileName).toUtf8().constData()))
694 "ZblCog::mapConfigResources - " 695 "WARNING: failed to map file resources for file: " 698 else if(strAction ==
"load")
702 xmlResults.at(j+
fileUri).toUtf8().constData(),
706 zWarning() <<
"ZblCog::mapConfigResources - " 707 "WARNING: failed to register resources for uri: " 717 zWarning() <<
"ZblCog::mapConfigResources - " 718 "WARNING: invalid command format - " 719 "second field should contain a valid resource type string.";
732 zDebug() <<
"Done mapping configuration file resources";
740 if(actual == required)
743 zWarning() <<
"ZblCog::validateParamCount - " 744 "WARNING: invalid parameter count - command required " 745 << required <<
" paramaters but " << actual <<
" were passed.";
754 " - WARNING: invalid resource command format - " 755 "command strings should have exactly three fields as follows: " 756 "[action]::[recordType]::[parameterCount] where: " 757 "action=\"map\"|\"load\" resourceType=\"file\"|\"plugin\" " 758 "parameterCount=[integer number of parameters following this command.]" 759 " INVALID COMMAND STRING=" 765 const QString& label,
const QString& reason)
768 <<
" - WARNING - ABORTING configuration resource load operation! " 782 QXmlNodeModelIndex index(item.toNodeModelIndex());
784 const QAbstractXmlNodeModel* model = index.model();
786 QXmlName name(model->name(index));
788 retval = name.localName();
804 uri, versionMajor, versionMinor));
806 zDebug() <<
"ZblCog::registerResource - registering resource: " 812 QReadLocker lock(&
m_lock);
818 zWarning() <<
"ZblCog::registerResource FAILED: resource not mapped: " 826 zWarning() <<
"ZblCog::loadResource FAILED: can't access " 827 "resource file: " << rs->
fileName() <<
828 " for resource: " << key;
843 zDebug() <<
"ZblCog::loadResource - loadResource resource: " 856 QReadLocker lock(&
m_lock);
858 if(versionMajor != -1 && versionMinor != -1)
863 uri, versionMajor, versionMinor));
873 if(versionMajor != -1)
877 prefix += QString::number(versionMajor);
883 keys = keys.filter(prefix);
885 return !keys.isEmpty();
889 throw ZblException(
"ZblCog::isMappedResource - unknown exception");
898 QReadLocker lock(&
m_lock);
910 "Zbl::ZblCog::zInstance programming error: " 911 "Singleton ZblCog object already initialized!");
923 "Zbl::ZblCog::zInstance programming error: " 924 "Singleton ZblCog object not initialized! " 925 "Call ZblCog::zInit before calling this method.");
const QString & fileName() const
returns the Resource file's file name
bool registerResource(const char *uri, int versionMajor, int versionMinor)
Makes a Zuble resource file available to QML and background javascript programs.
This class holds the state for managing a Zuble sprocket.
ZblFactory * createFactory(const char *sprocketUri, int versionMajor, int versionMinor, QObject *parent=NULL)
Constructs a ZblFactory object and populates it with a set of version-specific Sprocket object constr...
static ZblCog * m_zCog
The one and only ZblCog object allowed to exist in this process.
static const int m_paramcountPlugin
sprocketFoundryMap m_objects
Maps object QML names to maps of sprocket object foundaries. Each foundary contains a separate object...
ZblSprockOb * findSprock(int versionMajor, int versionMinor, const char *qmlName)
Locates a version-specific Sprocket object constructor in the Sprocket's object foundry based on the ...
bool mapPluginResources(const char *fileName, bool qmlRegister=false)
Make a plugin's binary resource files known to Zuble so they can be loaded into QML by calling Zbl...
bool getPluginStringParam(const QJsonObject &resource, const QString ¶mKey, QString &outValue)
bool isMappedResource(const char *uri, int versionMajor, int versionMinor)
Determine whether a resource is mapped.
#define ZBL_REGISTER_LOGGED_OBJECT
sprocketObjectMap m_objectFunctions
Maps QML type names to appropriate version-specific Sprocket object constructors. ...
bool getPluginResParams(const QJsonValue &resourceValue, QString &uri, QString &fileName, QString &versionMajor, QString &versionMinor)
bool mapConfigResources(const char *configFilePath)
Reads specified zblconfig.xml file and maps/loads resources accordingly.
sprocketPluginMap m_sprockets
Maps Sprocket plugin uri's to sprocket objects.
static ZblCog & zInstance()
zInstance
void warnAbortingResourceLoad(const QString &label, const QString &reason)
bool registerSprocketObject(const char *uri, int versionMajor, int versionMinor, const char *qmlName, sprocketObjectConstructor createFunc)
Creates a new ZblSprockOb Sprocket object constructor and adds it to the Sprocket's object foundary...
static const QString m_restypeInvalid
static const QString m_commandSeparator
resourceMap m_resources
Maps Resource plugin uri's to resource objects.
static const QString m_actionMap
Represents a binary resource file created by Qt's resource complier.
#define ZBL_DEFINE_LOGGED_OBJECT(class_name)
QString getNodeName(const QXmlItem &item, const QXmlQuery &query)
static const QString m_actionIgnore
bool validateParamCount(int actual, int required)
bool registerSprocketObject(const char *uri, int versionMajor, int versionMinor, const char *qmlName, sprocketObjectConstructor createFunc)
Called by Zuble sprocket extension plugins to register their object constructors with Zuble so their ...
const QString sprocketTag() const
returns a string in the form of "<uri>:<majorVersion>:<minorVersion>"
static const QString m_restypePlugin
static const QString m_restypeFile
void warnInvalidResourceCommand(const QString &label, const QString &command)
Zuble's Qt Exception Object.
Maintains a map of Zuble Sprockets and creates Sprocket object factories for javascript background th...
bool mapResource(const char *uri, int versionMajor, int versionMinor, const char *fileName)
Binds a resource uri with it's associated binary resource file. The resource can subsequently be refe...
An object factory that creates new instances of objects defined by a Zuble sprocket. Used primarily to construct sprocket objects in Zuble background threads.
QObject *(* sprocketObjectConstructor)(QObject *parent)
A pointer to a function that creates a Zuble Sprocket object. Sprocket object constructor functions s...
This class acts as an object construction wrapper for objects defined in Zuble Sprocket plugins...
QReadWriteLock m_lock
ZblCog's multithreaded lock.
static const QString m_actionQmlregister
ZblCog(QObject *parent=0)
QStringList getMappedResources()
Returns the list of mapped resource keys in the form of uri:versionMajor:versionMinor, ie: 'org.zuble.qml:1:0'.
bool registerSprocket(const char *uri, int versionMajor, int versionMinor)
Called by Zuble sprocket extension plugins to register themselves with Zuble so their objects can be ...
static const int m_paramcountFile
static ZblCog & zInit(QObject *parent)