home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / filD7099B7390EBAEBBF2848200060D196B < prev    next >
Text File  |  2010-07-12  |  8KB  |  240 lines

  1. XB.WidgetPackage = function XBPkg_constructor(rootDir, id) {
  2.     if ( !(rootDir instanceof XB._Ci.nsIFile) )
  3.         throw new TypeError(XB._base.consts.ERR_FILE_REQUIRED);
  4.     if ( !rootDir.isDirectory() )
  5.         throw new TypeError(this._consts.ERR_NOT_A_DIR);
  6.     if (!sysutils.isString(id))
  7.         throw new TypeError(XB._base.consts.ERR_STRING_EXPECTED);
  8.     
  9.     this._rootDir = rootDir.clone();
  10.     this._id = id;
  11.     this._idHash = misc.stringMD5(id + Math.random());
  12.     this._files = {__proto__: null};
  13.     
  14.     this._logRoot = XB._base.loggersRoot + ".Package_" + this._idHash;
  15.     this._logger = XB._base.application.core.Log4Moz.repository.getLogger(this._logRoot);
  16.     
  17.     this._xbProtocolHandler = Cc[PROTOCOL_XB_CONTRACT_ID].getService(Ci.nsIProtocolHandler).wrappedJSObject;
  18.     this._xbProtocolHandler.addDataProvider(this);
  19.     
  20.     this._units = {};
  21. };
  22.  
  23. XB.WidgetPackage.prototype = {
  24.     constructor: XB.WidgetPackage,
  25.     
  26.     get id() {
  27.         return this._id;
  28.     },
  29.     
  30.     getUnit: function XBPkg_getUnit(unitName) {
  31.         let unit = this._units[unitName];
  32.         if (!unit) {
  33.             let unitFile = this.findFile(unitName + ".xml");
  34.             if (!unitFile)
  35.                 throw new Error(this._consts.ERR_FILE_NOT_FOUND + " \"" + unitName + ".xml\"");
  36.             unit = this._loadUnit(unitFile);
  37.             this._units[unitName] = unit;
  38.         }
  39.         return unit;
  40.     },
  41.     
  42.     resolvePath: function XBPkg_resolvePath(path) {
  43.         if (!path)
  44.             return "";
  45.         
  46.         if (path.indexOf("xb://") == 0)
  47.             return path;
  48.         
  49.         
  50.         var spec = "xb://" + this.providerId + "/" + path;
  51.         return spec;
  52.     },
  53.     
  54.     findFile: function XBPkg_findFile(path) {
  55.         if (!sysutils.isString(path))
  56.             throw new TypeError(XB._base.consts.ERR_STRING_EXPECTED);
  57.         if (this._files[path])
  58.             return this._files[path];
  59.         
  60.         var root = this._rootDir.clone(),
  61.             locales = this._locales(),
  62.             components = path.split("/"),
  63.             file = null;
  64.         
  65.         if (components[components.length - 1][0] == ".")
  66.             return this._files[path] = null;
  67.         
  68.         var file = null;
  69.         for (let i = locales.length; i--;) {
  70.             let localeName = locales[i].name;
  71.             let candidate = root.clone();
  72.             
  73.             if (localeName != '') {
  74.                 candidate.append("locale");
  75.                 candidate.append(localeName);
  76.             }
  77.             
  78.             for (let j = 0; j < components.length; j++)
  79.                 candidate.append(components[j]);
  80.             
  81.             try {
  82.                 candidate.normalize();
  83.                 
  84.                 if (
  85.                     candidate.exists() &&
  86.                     candidate.isReadable() &&
  87.                     !candidate.isSymlink() &&
  88.                     !candidate.isDirectory() &&
  89.                     this._rootDir.contains(candidate, true)
  90.                 ) {
  91.                     file = candidate;
  92.                     break;
  93.                 }
  94.             } catch(e) {
  95.                 continue;
  96.             }
  97.         }
  98.         
  99.         return this._files[path] = file;
  100.     },
  101.     
  102.     finalize: function XBPkg_finalize() {
  103.         for (let unitName in this._units)
  104.             try {
  105.                 this._units[unitName].finalize();
  106.             }
  107.             catch (e) {
  108.                 this._logger.error("Couldn't clear loaded unit " + unitName + ". " + misc.formatError(e));
  109.             }
  110.         this._units = {};
  111.         
  112.         this._xbProtocolHandler.removeDataProvider(this);
  113.     },
  114.     
  115.     get providerId() {
  116.         return this._idHash + "." + XB._base.application.name;
  117.     },
  118.     
  119.     newChannel: function XBPkg_newChannel(aURI) {
  120.         var file = this.findFile(aURI.path);
  121.         if (!file)
  122.             throw new Error(this._consts.ERR_FILE_NOT_FOUND + " " + aURI.path);
  123.         
  124.         var filesStream = XB._Cc["@mozilla.org/network/file-input-stream;1"]
  125.             .createInstance(XB._Ci.nsIFileInputStream);
  126.         filesStream.init(file, 1, 0, 4);
  127.         
  128.         var channel = XB._Cc["@mozilla.org/network/input-stream-channel;1"]
  129.                         .createInstance(XB._Ci.nsIInputStreamChannel)
  130.                         .QueryInterface(XB._Ci.nsIChannel);
  131.         channel.setURI(aURI);
  132.         channel.originalURI = aURI;
  133.         channel.contentStream = filesStream;
  134.         
  135.         return channel;
  136.     },
  137.     
  138.     _consts: {
  139.         ERR_NOT_A_DIR: "Root must be a directory",
  140.         ERR_NO_UNIT: "No such unit",
  141.         ERR_NO_WIDGET: "Unit does not describe a widget",
  142.         ERR_FILE_NOT_FOUND: "File not found",
  143.         ERR_ACCESS_DENIED: "Attempt to access a file outside the package directory",
  144.         ERR_STRING_EXPECTED: "String expected"
  145.     },
  146.     _id: undefined,
  147.     _idHash: undefined,
  148.     _rootDir: null,
  149.     _units: null,
  150.     _logRoot: undefined,
  151.     _name: undefined,
  152.     _bestLocaleName: undefined,
  153.     _logger: null,
  154.     
  155.     _ioService: Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService),
  156.     _DOMParser: new Components.Constructor("@mozilla.org/xmlextras/domparser;1", "nsIDOMParser"),
  157.     
  158.     _loadUnit: function XBPkg_loadUnit(unitFile) {
  159.         var unitName = unitFile.leafName.substr(0, unitFile.leafName.length - 4);
  160.         var unitDoc = this._loadXMLDoc(this.resolvePath(unitFile.leafName));
  161.         var unit = new XB._Parser.Unit(unitDoc, this, unitName, this._logRoot);
  162.         return unit;
  163.     },
  164.     
  165.     _loadXMLDoc: function XBPkg_loadXMLDoc(docURL) {
  166.         var docURI = this._ioService.newURI(docURL, null, null);
  167.         var channel = this.newChannel(docURI).QueryInterface(XB._Ci.nsIInputStreamChannel);
  168.         return sysutils.xmlDocFromStream(channel.contentStream, docURI, docURI);
  169.     },
  170.     
  171.     _locales: function() {
  172.         if (this._localesCache)
  173.             return this._localesCache;
  174.         
  175.         const weights = {
  176.             language: 32,
  177.             root: 16,
  178.             ru: 8,
  179.             en: 4,
  180.             country: 2,
  181.             region: 1
  182.         };
  183.         
  184.         var locales = [];
  185.         
  186.         locales.push({
  187.             name: '',
  188.             weight: weights.root,
  189.             components: null
  190.         });
  191.         
  192.         var appLocale = misc.parseLocale(XB._base.application.localeString);
  193.         var localeDir = this._rootDir.clone();
  194.         localeDir.append("locale");
  195.         if (!localeDir.exists())
  196.             return locales;
  197.         
  198.         var entries = localeDir.directoryEntries;
  199.         while (entries.hasMoreElements()) {
  200.             var file = entries.getNext().QueryInterface(XB._Ci.nsIFile);
  201.             if (file.isDirectory()) {
  202.                 var name = file.leafName;
  203.                 var components = misc.parseLocale(name);
  204.                 
  205.                 if (!components)
  206.                     continue;
  207.                 
  208.                 var weight = 0;
  209.                 for (let space in weights) {
  210.                     var component = components[space];
  211.                     if (component === undefined)
  212.                         continue;
  213.                     
  214.                     if (space == "language")
  215.                         if (component in weights)
  216.                             weight += weights[component];
  217.                     
  218.                     if (component === appLocale[space])
  219.                         weight += weights[space];
  220.                 }
  221.                 
  222.                 locales.push({
  223.                     name: name,
  224.                     weight: weight,
  225.                     components: components
  226.                 });
  227.                 
  228.             }
  229.         }
  230.         
  231.         locales.sort(function rule(a, b) {
  232.             if (a.weight == b.weight)
  233.                 return 0;
  234.             return a.weight < b.weight ? -1 : +1;
  235.         });
  236.         
  237.         return this._localesCache = locales;
  238.     }
  239. };
  240.