1 /*
  2  * QMLLoader.js - loader implementation for Qt/QML apps.
  3  *
  4  * Copyright © 2015, JEDLSoft
  5  *
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  *
 10  *     http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *
 16  * See the License for the specific language governing permissions and
 17  * limitations under the License.
 18  */
 19 
 20 var ilib = require("./ilib.js");
 21 var path = require("./Path.js");
 22 var Loader = require("./Loader.js");
 23 
 24 /**
 25  * @class
 26  * QML implemenation of a Loader class.
 27  *
 28  * @private
 29  * @extends Loader
 30  * @param {Object} fr the Qt FileReader instance
 31  */
 32 var QMLLoader = function(fr) {
 33     //console.log("new QMLLoader instance called with " + fr);
 34     this.fr = fr;
 35 
 36     this.parent.call(this, ilib);
 37 
 38     this.root = module.filename ? path.dirname(path.join(module.filename, "..")) : Qt.resolvedUrl("..").toString();
 39     this.root = this.root.replace("file://", "");
 40     //console.log("QMLLoader using root: " + root);
 41 
 42     if (this.root[this.root.length-1] === '/') {
 43         this.root = this.root.substring(0, this.root.length-1);
 44     }
 45 
 46 
 47     this.includePath.push(path.join(this.root, "resources"));     // always check the application's resources dir first
 48 
 49     // then a standard locale dir of a built version of ilib from npm
 50     this._exists(path.join(this.root, "locale"), "localeinfo.json");
 51 
 52     // try the standard install directories
 53     this._exists("/usr/share/javascript/ilib/locale", "localeinfo.json");
 54 
 55     // ... else fall back to see if we're in a check-out dir of ilib
 56     // this._exists(path.join(this.root, "data", "locale"), "localeinfo.json");
 57 
 58     //console.log("QMLLoader: include path is now " + JSON.stringify(this.includePath));
 59 };
 60 
 61 QMLLoader.prototype = new Loader();
 62 QMLLoader.prototype.parent = Loader;
 63 QMLLoader.prototype.constructor = QMLLoader;
 64 
 65 QMLLoader.prototype._loadFile = function (pathname, sync, cb) {
 66     //console.log("_loadFile: attempting to load " + pathname);
 67     // use the FileReader plugin to access the local disk synchronously
 68     if (this.fr.exists(pathname)) {
 69         var text = this.fr.read(pathname);
 70         cb && typeof(cb) === 'function' && cb(text);
 71         return text;
 72     } else {
 73         cb && typeof(cb) === 'function' && cb();
 74         return undefined;
 75     }
 76 };
 77 
 78 QMLLoader.prototype._exists = function(dir, file) {
 79     var fullpath = path.normalize(path.join(dir, file));
 80     //console.log("QMLLoader._exists: checking for the existence of " + dir);
 81     if (this.fr.exists(fullpath)) {
 82         //console.log("QMLLoader._exists: found");
 83         this.includePath.push(dir);
 84     }
 85 };
 86 
 87 module.exports = QMLLoader;