home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / sbURLFormatter.js < prev    next >
Text File  |  2012-06-08  |  12KB  |  303 lines

  1. /*
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Original Code is the nsURLFormatterService.
  16. #
  17. # The Initial Developer of the Original Code is
  18. # Axel Hecht <axel@mozilla.com>
  19. # Portions created by the Initial Developer are Copyright (C) 2006
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. #   Dietrich Ayala <dietrich@mozilla.com>
  24. #
  25. # Alternatively, the contents of this file may be used under the terms of
  26. # either the GNU General Public License Version 2 or later (the "GPL"), or
  27. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. # in which case the provisions of the GPL or the LGPL are applicable instead
  29. # of those above. If you wish to allow use of your version of this file only
  30. # under the terms of either the GPL or the LGPL, and not to allow others to
  31. # use your version of this file under the terms of the MPL, indicate your
  32. # decision by deleting the provisions above and replace them with the notice
  33. # and other provisions required by the GPL or the LGPL. If you do not delete
  34. # the provisions above, a recipient may use your version of this file under
  35. # the terms of any one of the MPL, the GPL or the LGPL.
  36. #
  37. # ***** END LICENSE BLOCK *****
  38. */
  39.  
  40. /**
  41.  * Original code from Mozilla with Songbird modifications
  42.  *   John Gaunt <redfive@songbirdnest.com>
  43.  */
  44.  
  45. /**
  46.  * @class sbURLFormatterService
  47.  *
  48.  * sbURLFormatterService exposes methods to substitute variables in URL formats.
  49.  *
  50.  * Mozilla Applications linking to Mozilla websites are strongly encouraged to use
  51.  * URLs of the following format:
  52.  *
  53.  *   http[s]://%LOCALE%.%SERVICE%.mozilla.[com|org]/%LOCALE%/
  54.  */
  55.  
  56. var Cr = Components.results;
  57. var Cc = Components.classes;
  58. var Ci = Components.interfaces;
  59. var Cu = Components.utils;
  60.  
  61. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  62.  
  63. const PREF_APP_DISTRIBUTION               = "distribution.id";
  64. const PREF_APP_DISTRIBUTION_VERSION       = "distribution.version";
  65. const PREF_APP_UPDATE_CHANNEL             = "app.update.channel";
  66. const PREF_PARTNER_BRANCH                 = "app.partner.";
  67.  
  68.  
  69. function sbURLFormatterService() {
  70. }
  71.  
  72. sbURLFormatterService.prototype = {
  73.   classDescription: "Songbird URL Formatter Service",
  74.   contractID: "@songbirdnest.com/moz/sburlformatter;1",
  75.   classID: Components.ID("{4dc7da1b-89d1-48b5-8582-64c98bfb3410}"),
  76.   QueryInterface: XPCOMUtils.generateQI([Ci.sbIURLFormatter,
  77.                                          Ci.nsIPropertyBag2]),
  78.  
  79.   _xpcom_categories: [ { category: "extension-update-params", entry: "LOCALE" },
  80.                        { category: "extension-update-params", entry: "VENDOR" },
  81.                        { category: "extension-update-params", entry: "NAME" },
  82.                        { category: "extension-update-params", entry: "PRODUCT" },
  83.                        { category: "extension-update-params", entry: "ID" },
  84.                        { category: "extension-update-params", entry: "VERSION" },
  85.                        { category: "extension-update-params", entry: "APPBUILDID" },
  86.                        { category: "extension-update-params", entry: "BUILD_ID" },
  87.                        { category: "extension-update-params", entry: "BUILD_TARGET" },
  88.                        { category: "extension-update-params", entry: "PLATFORMVERSION" },
  89.                        { category: "extension-update-params", entry: "PLATFORM_VERSION" },
  90.                        { category: "extension-update-params", entry: "PLATFORMBUILDID" },
  91.                        { category: "extension-update-params", entry: "APP" },
  92.                        { category: "extension-update-params", entry: "OS" },
  93.                        { category: "extension-update-params", entry: "XPCOMABI" },
  94.                        { category: "extension-update-params", entry: "DISTRIBUTION" },
  95.                        { category: "extension-update-params", entry: "DISTRIBUTION_VERSION" },
  96.                        { category: "extension-update-params", entry: "CHANNEL" },
  97.                        { category: "extension-update-params", entry: "OS_VERSION" },
  98.                        { category: "extension-update-params", entry: "CUSTOM1" },
  99.                        { category: "extension-update-params", entry: "CUSTOM2" } ],
  100.  
  101.   _defaults: {
  102.     get appInfo() {
  103.       if (!this._appInfo)
  104.         this._appInfo = Cc["@mozilla.org/xre/runtime;1"].
  105.                         getService(Ci.nsIXULAppInfo).
  106.                         QueryInterface(Ci.nsIXULRuntime);
  107.       return this._appInfo;
  108.     },
  109.  
  110.     LOCALE: function() Cc["@mozilla.org/chrome/chrome-registry;1"].
  111.                        getService(Ci.nsIXULChromeRegistry).getSelectedLocale('global'),
  112.     VENDOR:           function() this.appInfo.vendor,
  113.     NAME:             function() this.appInfo.name,
  114.     PRODUCT:          function() this.appInfo.name,
  115.     ID:               function() this.appInfo.ID,
  116.     VERSION:          function() this.appInfo.version,
  117.     APPBUILDID:       function() this.appInfo.appBuildID,
  118.     BUILD_ID:         function() this.appInfo.appBuildID,
  119.     BUILD_TARGET:     function() this.appInfo.OS + "_" + this.getABI(),
  120.     PLATFORMVERSION:  function() this.appInfo.platformVersion,
  121.     PLATFORM_VERSION: function() this.appInfo.platformVersion,
  122.     PLATFORMBUILDID:  function() this.appInfo.platformBuildID,
  123.     APP:              function() this.appInfo.name.toLowerCase().replace(/ /, ""),
  124.     OS:               function() this.appInfo.OS,
  125.     XPCOMABI:         function() this.appInfo.XPCOMABI,
  126.     DISTRIBUTION:         function() this.getPrefValue(PREF_APP_DISTRIBUTION),
  127.     DISTRIBUTION_VERSION: function() this.getPrefValue(PREF_APP_DISTRIBUTION_VERSION),
  128.     CHANNEL:          function() this.getUpdateChannel(),
  129.     OS_VERSION:       function() this.getOSVersion(),
  130.     CUSTOM1:          function() "custom_parameter_1",
  131.     CUSTOM2:          function() "custom_parameter_2",
  132.  
  133.  
  134.     getABI: function () {
  135.       var abi = "default";
  136.       try {
  137.         abi = this.appInfo.XPCOMABI;
  138.  
  139.         // Mac universal build should report a different ABI than either macppc
  140.         // or mactel.
  141.         var macutils = null;
  142.         if ("@mozilla.org/xpcom/mac-utils;1" in Cc) {
  143.           macutils = Cc["@mozilla.org/xpcom/mac-utils;1"]
  144.                        .getService(Ci.nsIMacUtils);
  145.         }
  146.       
  147.         if (macutils && macutils.isUniversalBinary)
  148.           abi = "Universal-gcc3";
  149.       }
  150.       catch (e) { }
  151.  
  152.       return abi;
  153.     },
  154.  
  155.     getPrefValue: function (aPrefName) {
  156.       var prefSvc =  Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch2);
  157.       var prefValue = "default";
  158.       var defaults = prefSvc.QueryInterface(Ci.nsIPrefService)
  159.                                       .getDefaultBranch(null);
  160.       try {
  161.         prefValue = defaults.getCharPref(aPrefName);
  162.       } catch (e) {
  163.         // use default when pref not found
  164.       }
  165.       return prefValue;
  166.     },
  167.  
  168.     /**
  169.      * Read the update channel from defaults only.  We do this to ensure that
  170.      * the channel is tightly coupled with the application and does not apply
  171.      * to other instances of the application that may use the same profile.
  172.      */
  173.     getUpdateChannel: function uf_getUpdateChannel() {
  174.       var prefSvc =  Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch2);
  175.       var channel = "default";
  176.       var prefName;
  177.       var prefValue;
  178.  
  179.       var defaults = prefSvc.QueryInterface(Ci.nsIPrefService)
  180.                                       .getDefaultBranch(null);
  181.       try {
  182.         channel = defaults.getCharPref(PREF_APP_UPDATE_CHANNEL);
  183.       } catch (e) {
  184.         // use default when pref not found
  185.       }
  186.  
  187.       try {
  188.         var partners = prefSvc.getChildList(PREF_PARTNER_BRANCH, { });
  189.         if (partners.length) {
  190.           channel += "-cck";
  191.           partners.sort();
  192.     
  193.           for each (prefName in partners) {
  194.             prefValue = prefSvc.getCharPref(prefName);
  195.             channel += "-" + prefValue;
  196.           }
  197.         }
  198.       }
  199.       catch (e) {
  200.         Cu.reportError(e);
  201.       }
  202.  
  203.       return channel;
  204.     },
  205.  
  206.     getOSVersion: function uf_getOSVersion() {
  207.       var osVersion = "default";
  208.       var sysInfo = Components.classes["@mozilla.org/system-info;1"]
  209.                               .getService(Components.interfaces.nsIPropertyBag2);
  210.       try {
  211.         osVersion = sysInfo.getProperty("name") + " " + sysInfo.getProperty("version");
  212.       }
  213.       catch (e) {
  214.       }
  215.  
  216.       if (osVersion) {
  217.         try {
  218.           osVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
  219.         }
  220.         catch (e) {
  221.           // Not all platforms have a secondary widget library, so an error is nothing to worry about.
  222.         }
  223.       }
  224.       return encodeURIComponent(osVersion);
  225.     }
  226.   },
  227.  
  228.   // nsIPropertyBag
  229.   get enumerator() { return null; },
  230.   getProperty: function (aPropName) { return null; },
  231.  
  232.   // nsIPropertyBag2
  233.   getPropertyAsInt32 : function(aPropName) { return null; },
  234.   getPropertyAsUint32 : function(aPropName) { return null; },
  235.   getPropertyAsInt64 : function(aPropName) { return null; },
  236.   getPropertyAsUint64 : function(aPropName) { return null; },
  237.   getPropertyAsDouble : function(aPropName) { return null; },
  238.   getPropertyAsACString : function(aPropName) { return null; },
  239.   getPropertyAsAUTF8String : function(aPropName) { return null; },
  240.   getPropertyAsBool : function(aPropName) { return null; },
  241.   getPropertyAsInterface : function(aPropName, aIID, aRetVal) { aRetVal = null; return; },
  242.   get : function(aPropName) { return null; },
  243.   hasKey : function(aPropName) { return (aPropName in this._defaults); },
  244.  
  245.   getPropertyAsAString: function(aPropName) {
  246.     if (aPropName in this._defaults) // supported defaults
  247.       return this._defaults[aPropName]();
  248.     return "";
  249.   },
  250.  
  251.   formatURL: function uf_formatURL(aFormat, aMappings) {
  252.     var _this = this;
  253.     var _overrides = aMappings;
  254.  
  255.     var replacementCallback = function(aMatch, aKey) {
  256.       try {
  257.         if ( _overrides ) {
  258.           return _overrides.getProperty(aKey);
  259.         }
  260.       } catch (e) {
  261.         // excpetion meaans it's not in the override
  262.       }
  263.  
  264.       if (aKey in _this._defaults) // supported defaults
  265.         return _this._defaults[aKey]();
  266.  
  267.       Cu.reportError("formatURL: Couldn't find value for key: " + aKey);
  268.       return aMatch;
  269.     }
  270.  
  271.     // Ensure 3 characters to prevent matching things like %D0%CB
  272.     return aFormat.replace(/%(\w{3,})%/g, replacementCallback);
  273.   },
  274.  
  275.   formatURLPref: function uf_formatURLPref(aPref, aMappings) {
  276.     var format = null;
  277.     var PS = Cc['@mozilla.org/preferences-service;1']
  278.              .getService(Ci.nsIPrefBranch);
  279.  
  280.     try {
  281.       format = PS.getComplexValue(aPref, Ci.nsISupportsString).data;
  282.     } catch(ex) {
  283.       Cu.reportError("formatURLPref: Couldn't get pref: " + aPref);
  284.       return "about:blank";
  285.     }
  286.  
  287.     if (!PS.prefHasUserValue(aPref) &&
  288.         /^chrome:\/\/.+\/locale\/.+\.properties$/.test(format)) {
  289.       // This looks as if it might be a localised preference
  290.       try {
  291.         format = PS.getComplexValue(aPref, Ci.nsIPrefLocalizedString).data;
  292.       } catch(ex) {}
  293.     }
  294.  
  295.     return this.formatURL(format, aMappings);
  296.   }
  297. };
  298.  
  299. function NSGetModule(compMgr, fileSpec) {
  300.   return XPCOMUtils.generateModule([sbURLFormatterService]);
  301. } // NSGetModule
  302.  
  303.