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 / filECAECAE22D7745FB271EBA3046A19D4E < prev    next >
Text File  |  2010-07-12  |  17KB  |  445 lines

  1. var misc = {
  2.     dump: function misc_dump(what, depth) {
  3.         function _dump(what, depth, stack) {
  4.             stack.push(what);
  5.             
  6.             var res = "";
  7.             for (let prop in what) {
  8.                 let val = what[prop];
  9.                 let valStr;
  10.                 if ((depth > stack.length) &&
  11.                     (val instanceof Object || typeof val == "object") &&
  12.                     (stack.indexOf(val) == -1))
  13.                     valStr = _dump(val, depth, stack);
  14.                 else
  15.                     valStr = String(val);
  16.                 
  17.                 res += (prop + ": " + valStr + "\r\n");
  18.             }
  19.             
  20.             stack.pop();
  21.             
  22.             return res;
  23.         }
  24.         
  25.         return _dump(what, depth, []);
  26.     },
  27.     
  28.     formatError: function misc_formatError(e) {
  29.         if (!(e instanceof this._Ci.nsIException))
  30.             return ("" + e);
  31.         
  32.         let text = e.name + ": " + e.message;
  33.         if (e.fileName)
  34.             text += "\nin " + e.fileName + "\nat line " + e.lineNumber;
  35.         
  36.         return  text;
  37.     },
  38.     
  39.     getNavigatorWindows: function misc_getNavigatorWindows() {
  40.         let windows = [],
  41.             wndEnum = this._Cc["@mozilla.org/appshell/window-mediator;1"]
  42.                 .getService(this._Ci.nsIWindowMediator)
  43.                 .getEnumerator("navigator:browser");
  44.         while (wndEnum.hasMoreElements())
  45.             windows.push(wndEnum.getNext());
  46.         
  47.         return windows;
  48.     },
  49.     
  50.     getTopNavigatorWindow: function() {
  51.         return this.getTopWindowOfType("navigator:browser");
  52.     },
  53.     
  54.     getTopWindowOfType: function(windowType) {
  55.         let mediator = this._Cc["@mozilla.org/appshell/window-mediator;1"].getService(this._Ci.nsIWindowMediator);
  56.         return mediator.getMostRecentWindow(windowType);
  57.     },
  58.     
  59.     openWindow: function(parameters) {
  60.         let window;
  61.         
  62.         if ("name" in parameters && parameters.name) {
  63.             const WM = this._Cc["@mozilla.org/appshell/window-mediator;1"].getService(this._Ci.nsIWindowMediator);
  64.             if ((window = WM.getMostRecentWindow(parameters.name))) {
  65.                 window.focus();
  66.                 return window;
  67.             }
  68.         }
  69.         
  70.         let parent;
  71.         let features = parameters.features || "";
  72.         
  73.         if (features.indexOf("__popup__") != -1) {
  74.             let featuresHash = { __proto__: null };
  75.             
  76.             features.replace(/(^|,)__popup__($|,)/, "").split(",")
  77.             .forEach(function(aFeatureString) {
  78.                 if (aFeatureString) {
  79.                     let [name, value] = aFeatureString.split("=");
  80.                     if (name)
  81.                         featuresHash[name] = value;
  82.                 }
  83.             });
  84.             
  85.             function addFeature(aFeatureString) {
  86.                 let [name, value] = aFeatureString.split("=");
  87.                 if (!(name in featuresHash))
  88.                     featuresHash[name] = value;
  89.             }
  90.             
  91.             addFeature("chrome");
  92.             addFeature("dependent=yes");
  93.             
  94.             if (!Ci.nsIDOMGeoPositionAddress)
  95.                 addFeature("titlebar=no");
  96.             
  97.             const AppInfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo).QueryInterface(Ci.nsIXULRuntime);
  98.             if (!/^win/i.test(AppInfo.OS))
  99.                 addFeature("popup=yes");
  100.             
  101.             let featuresMod = [];
  102.             for (let [name, value] in Iterator(featuresHash))
  103.                 featuresMod.push(name + (value ? "=" + value : ""));
  104.             
  105.             features = featuresMod.join(",");
  106.             
  107.             if (!("parent" in parameters))
  108.                 parent = this.getTopNavigatorWindow();
  109.         }
  110.         
  111.         parent = parent || parameters.parent || null;
  112.         
  113.         const WW = this._Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(this._Ci.nsIWindowWatcher);
  114.         window = WW.openWindow(
  115.             parent,
  116.             parameters.url,
  117.             parameters.name || "_blank",
  118.             features,
  119.             parameters.arguments
  120.         );
  121.         
  122.         window.parameters = parameters;
  123.         
  124.         return window;
  125.     },
  126.     
  127.     stringMD5: function misc_stringMD5(str) {
  128.         var converter = this._Cc["@mozilla.org/intl/scriptableunicodeconverter"]
  129.             .createInstance(this._Ci.nsIScriptableUnicodeConverter);
  130.         converter.charset = "UTF-8";
  131.         
  132.         var result = {};
  133.         var data = converter.convertToByteArray(str, result);
  134.         var ch = this._Cc["@mozilla.org/security/hash;1"].createInstance(this._Ci.nsICryptoHash);
  135.         ch.init(ch.MD5);
  136.         ch.update(data, data.length);
  137.         var hash = ch.finish(false);
  138.         
  139.         function toHexString(charCode)
  140.         {
  141.           return ("0" + charCode.toString(16)).slice(-2);
  142.         }
  143.         
  144.         return [toHexString(hash.charCodeAt(i)) for (i in hash)].join("");
  145.     },
  146.     
  147.     camelize: function(string) {
  148.         function camelizeFunction(item, i) {
  149.             if (i != 0)
  150.                 return item.charAt(0).toUpperCase() + item.substr(1).toLowerCase();
  151.             return item;
  152.         }
  153.         return string.split("-").map(camelizeFunction).join("");
  154.     },
  155.     
  156.     parseLocale: function misc_parseLocale(localeString) {
  157.         var components = localeString.match(this._localePattern);
  158.         if (components)
  159.             return {
  160.                 language: components[1],
  161.                 country: components[3],
  162.                 region: components[5]
  163.             };
  164.         return null;
  165.     },
  166.     
  167.     get StringBundle() {
  168.         function StringBundle(aURL) {
  169.             this._url = this._createURL(aURL);
  170.         }
  171.         
  172.         StringBundle.prototype = {
  173.             get: function StringBundle_get(key, args) {
  174.                 if (args)
  175.                     return this._stringBundle.formatStringFromName(key, args, args.length);
  176.                 
  177.                 return this._stringBundle.GetStringFromName(key);
  178.             },
  179.             
  180.             getPlural: function StringBundle_getPlural(key, pluralData, args) {
  181.                 if (typeof pluralData == "number")
  182.                     return this._getPluralString(key, pluralData);
  183.                 
  184.                 let str = this.get(key, args);
  185.                 
  186.                 pluralData.forEach(function(aData, aIndex) {
  187.                     let purIndex = aIndex + 1;
  188.                     
  189.                     let plurStringKey = (typeof aData == "number" || !("key" in aData))
  190.                                             ? [key, purIndex, "Plur"].join("")
  191.                                             : aData.key;
  192.                     
  193.                     let plurNumber = (typeof aData == "number" ? aData : aData.number) || 0;
  194.                     let plurString = this._getPluralString(plurStringKey, plurNumber);
  195.                     
  196.                     str = str.replace("#" + purIndex, plurString);
  197.                 }, this);
  198.                 
  199.                 return str;
  200.             },
  201.             
  202.             tryGet: function StringBundle_tryGet(key, args, default_) {
  203.                 try {
  204.                     return this.get(key, args);
  205.                 }
  206.                 catch (e) {
  207.                     return default_;
  208.                 }
  209.             },
  210.             
  211.             _defaultPrefixForURL: "chrome://" + APP_NAME + "/locale/",
  212.             
  213.             _createURL: function StringBundle__createURL(aURL) {
  214.                 return (/^chrome:\/\//.test(aURL) ? "" : this._defaultPrefixForURL) + aURL;
  215.             },
  216.             
  217.             get _stringBundle() {
  218.                 let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"]
  219.                                        .getService(Ci.nsIStringBundleService)
  220.                                        .createBundle(this._url);
  221.                 
  222.                 this.__defineGetter__("_stringBundle", function() stringBundle);
  223.                 return this._stringBundle;
  224.             },
  225.             
  226.             _getPluralString: function StringBundle__getPluralString(aStringKey, aNumber) {
  227.                 let plurStrings = this.get(aStringKey).split(";");
  228.                 let plurStringNone = pluralForm.numForms() < plurStrings.length ? plurStrings.shift() : null;
  229.                 
  230.                 return (aNumber === 0 && plurStringNone !== null)
  231.                            ? plurStringNone
  232.                            : pluralForm.get(aNumber, plurStrings.join(";"));
  233.             }
  234.         };
  235.         
  236.         const pluralRule = parseInt(new StringBundle("global.properties").get("pluralRule"), 10);
  237.         const pluralForm = {};
  238.         
  239.         Cu.import("resource://gre/modules/PluralForm.jsm", pluralForm);
  240.         [pluralForm.get, pluralForm.numForms] = pluralForm.PluralForm.makeGetter(pluralRule);
  241.         
  242.         delete this.StringBundle;
  243.         return this.StringBundle = StringBundle;
  244.     },
  245.     
  246.     removeFileSafe: function(file) {
  247.         if (!file.exists())
  248.             return;
  249.         file = file.clone();
  250.         
  251.         try {
  252.             file.remove(true);
  253.             if (!file.exists())
  254.                 return;
  255.         }
  256.         catch(e) {
  257.         }
  258.         
  259.         var trash = Components.classes["@mozilla.org/file/directory_service;1"].
  260.             getService(Components.interfaces.nsIProperties).
  261.             get("TmpD", Components.interfaces.nsIFile);
  262.         
  263.         trash.append("trash");
  264.         trash.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  265.         try {
  266.             file.moveTo(trash, file.leafName);
  267.         }
  268.         catch (e) {
  269.             try {
  270.                 file.remove(true);
  271.             }
  272.             catch (e) {
  273.             }
  274.         }
  275.         try {
  276.             trash.remove(true);
  277.         }
  278.         catch (e) {
  279.         }
  280.     },
  281.     
  282.     get DownloadQueue() {
  283.         function DownloadQueue(queue, callback, progressmeter) {
  284.             this.queue = queue;
  285.             this.callback = callback;
  286.             this.progressmeter = progressmeter;
  287.             if (this.progressmeter)
  288.                 this.progressmeter.value = 0;
  289.             
  290.             if (this.queue.length == 0) {
  291.                 this.checkDefer();
  292.                 return;
  293.             }
  294.             
  295.             this.start();
  296.         }
  297.         
  298.         DownloadQueue.prototype = {
  299.             start: function() {
  300.                 var failed = false;
  301.                 for each (let item in this.queue) {
  302.                     try {
  303.                         item.uri = item.uri || item.url;
  304.                         let uri = Components.classes["@mozilla.org/network/io-service;1"]
  305.                             .getService(Components.interfaces.nsIIOService)
  306.                             .newURI(item.uri, null, null);
  307.                         
  308.                         if (!item.file)
  309.                             item.file = this.tempFile(item.uri);
  310.                         
  311.                         let persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);
  312.                         persist.persistFlags = persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES | persist.PERSIST_FLAGS_BYPASS_CACHE | persist.PERSIST_FLAGS_IGNORE_REDIRECTED_DATA;
  313.                         persist.progressListener = {
  314.                             item: item,
  315.                             owner: this,
  316.                             onLocationChange: this.onLocationChange,
  317.                             onSecurityChange: this.onSecurityChange,
  318.                             onStatusChange: this.onStatusChange,
  319.                             onStateChange: this.onStateChange,
  320.                             onProgressChange: this.onProgressChange
  321.                         };
  322.                         persist.saveURI(uri, null, null, null, "", item.file);
  323.                         item.persist = persist;
  324.                     }
  325.                     catch(e) {
  326.                         item.done = true;
  327.                         item.status = Components.results.NS_ERROR_UNEXPECTED;
  328.                         failed = true;
  329.                     }
  330.                 }
  331.                 
  332.                 if (failed)
  333.                     this.checkDefer();
  334.             },
  335.             
  336.             tempFile: function(uri) {
  337.                 if (!this.tempDirectory) {
  338.                     this.tempDirectory = Components.classes["@mozilla.org/file/directory_service;1"].
  339.                         getService(Components.interfaces.nsIProperties).
  340.                         get("TmpD", Components.interfaces.nsIFile);
  341.                 }
  342.                 
  343.                 let file = this.tempDirectory.clone();
  344.                 file.append(misc.stringMD5(uri));
  345.                 file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
  346.                 return file;
  347.             },
  348.             
  349.             destroy: function() {
  350.                 this.clean();
  351.             },
  352.             
  353.             clean: function() {
  354.                 for each (let item in this.queue) {
  355.                     try {
  356.                         if (item.persist)
  357.                             item.persist.cancelSave();
  358.                     }
  359.                     catch(e) {}
  360.                     try {
  361.                         if (item.file)
  362.                             misc.removeFileSafe(item.file);
  363.                     }
  364.                     catch(e) {}
  365.                 }
  366.             },
  367.             
  368.             checkDefer: function() {
  369.                 var context = this;
  370.                 (this._timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer))
  371.                     .initWithCallback({notify: function() { context.check(); }}, 1, Ci.nsITimer.TYPE_ONE_SHOT);
  372.             },
  373.             
  374.             check: function() {
  375.                 var done = true;
  376.                 for each (item in this.queue)
  377.                     if (!item.done) {
  378.                         done = false;
  379.                         break;
  380.                     }
  381.                 
  382.                 if (done)
  383.                     this.callback(this.queue);
  384.             },
  385.             
  386.             onLocationChange: function(webProgress, request, location) {
  387.             },
  388.             
  389.             onSecurityChange: function(webProgress, request, state) {
  390.             },
  391.             
  392.             onStatusChange: function(webProgress, request, status, message) {
  393.             },
  394.             
  395.             onStateChange: function(webProgress, request, state, message) {
  396.                 if (state & Components.interfaces.nsIWebProgressListener.STATE_STOP) {
  397.                     var item = this.item,
  398.                         owner = this.owner,
  399.                         queue = owner.queue;
  400.                     
  401.                     var httpStatus = 400; // 400: Bad Request
  402.                     try {
  403.                         httpStatus = request.QueryInterface(Components.interfaces.nsIHttpChannel).responseStatus;
  404.                     }
  405.                     catch (e) {
  406.                         // no problem
  407.                     }
  408.                     item.httpStatus = httpStatus;
  409.                     item.status = request.status;
  410.                     if (item.httpStatus >= 400)
  411.                         item.status = Components.results.NS_ERROR_UNEXPECTED;
  412.                     item.done = true;
  413.                     
  414.                     owner.check();
  415.                 }
  416.             },
  417.             
  418.             onProgressChange: function(webProgress, request, currentSelfProgress, maxSelfProgress, currentTotalProgress, maxTotalProgress) {
  419.                 var item = this.item,
  420.                     owner = this.owner,
  421.                     queue = owner.queue;
  422.                 if (!item || !owner || queue.length <= 0)
  423.                     return;
  424.                 item.percent = (maxTotalProgress <= 0 ? 1 : (currentTotalProgress / maxTotalProgress)) * 100;
  425.                 var total = 0;
  426.                 for each (item in queue)
  427.                     total += item.percent;
  428.                 
  429.                 if (this.owner.progressmeter)
  430.                     this.owner.progressmeter.value = total / queue.length;
  431.             }
  432.         };
  433.         
  434.         delete this.DownloadQueue;
  435.         return this.DownloadQueue = DownloadQueue;
  436.     },
  437.     
  438.     _Cc: Components.classes,
  439.     _Ci: Components.interfaces,
  440.     _Cu: Components.utils,
  441.     _Cr: Components.results,
  442.     
  443.     _localePattern: /^([a-z]{2})(-([A-Z]{2})(-(\w{2,5}))?)?$/
  444. };
  445.