home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 October / PCgo 2008-10 (DVD).iso / interface / contents / vollversionen_6617 / 21733 / files / xulrunner / components / nsURLFormatter.js < prev    next >
Encoding:
Text File  |  2008-08-20  |  3.0 KB  |  86 lines

  1. //@line 37 "e:\builds\tinderbox\XR-Trunk\WINNT_5.2_Depend\mozilla\toolkit\components\urlformatter\src\nsURLFormatter.js"
  2. /**
  3.  * @class nsURLFormatterService
  4.  *
  5.  * nsURLFormatterService exposes methods to substitute variables in URL formats.
  6.  *
  7.  * Mozilla Applications linking to Mozilla websites are strongly encouraged to use
  8.  * URLs of the following format:
  9.  *
  10.  *   http[s]://%LOCALE%.%SERVICE%.mozilla.[com|org]/%LOCALE%/
  11.  */
  12.  
  13. const Cc = Components.classes;
  14. const Ci = Components.interfaces;
  15. const Cu = Components.utils;
  16.  
  17. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  18.  
  19. function nsURLFormatterService() {}
  20. nsURLFormatterService.prototype = {
  21.   classDescription: "Application URL Formatter Service",
  22.   contractID: "@mozilla.org/toolkit/URLFormatterService;1",
  23.   classID: Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}"),
  24.   QueryInterface: XPCOMUtils.generateQI([Ci.nsIURLFormatter]),
  25.  
  26.   _defaults: {
  27.     get appInfo() {
  28.       if (!this._appInfo)
  29.         this._appInfo = Cc["@mozilla.org/xre/app-info;1"].
  30.                         getService(Ci.nsIXULAppInfo).
  31.                         QueryInterface(Ci.nsIXULRuntime);
  32.       return this._appInfo;
  33.     },
  34.  
  35.     LOCALE: function() Cc["@mozilla.org/chrome/chrome-registry;1"].
  36.                        getService(Ci.nsIXULChromeRegistry).getSelectedLocale('global'),
  37.     VENDOR:           function() this.appInfo.vendor,
  38.     NAME:             function() this.appInfo.name,
  39.     ID:               function() this.appInfo.ID,
  40.     VERSION:          function() this.appInfo.version,
  41.     APPBUILDID:       function() this.appInfo.appBuildID,
  42.     PLATFORMVERSION:  function() this.appInfo.platformVersion,
  43.     PLATFORMBUILDID:  function() this.appInfo.platformBuildID,
  44.     APP:              function() this.appInfo.name.toLowerCase().replace(/ /, ""),
  45.     OS:               function() this.appInfo.OS,
  46.     XPCOMABI:         function() this.appInfo.XPCOMABI
  47.   },
  48.  
  49.   formatURL: function uf_formatURL(aFormat) {
  50.     var _this = this;
  51.     var replacementCallback = function(aMatch, aKey) {
  52.       if (aKey in _this._defaults) // supported defaults
  53.         return _this._defaults[aKey]();
  54.       Cu.reportError("formatURL: Couldn't find value for key: " + aKey);
  55.       return aMatch;
  56.     }
  57.     return aFormat.replace(/%([A-Z]+)%/g, replacementCallback);
  58.   },
  59.  
  60.   formatURLPref: function uf_formatURLPref(aPref) {
  61.     var format = null;
  62.     var PS = Cc['@mozilla.org/preferences-service;1'].
  63.              getService(Ci.nsIPrefBranch);
  64.  
  65.     try {
  66.       format = PS.getComplexValue(aPref, Ci.nsISupportsString).data;
  67.     } catch(ex) {
  68.       Cu.reportError("formatURLPref: Couldn't get pref: " + aPref);
  69.       return "about:blank";
  70.     }
  71.  
  72.     if (!PS.prefHasUserValue(aPref) &&
  73.         /^chrome:\/\/.+\/locale\/.+\.properties$/.test(format)) {
  74.       // This looks as if it might be a localised preference
  75.       try {
  76.         format = PS.getComplexValue(aPref, Ci.nsIPrefLocalizedString).data;
  77.       } catch(ex) {}
  78.     }
  79.  
  80.     return this.formatURL(format);
  81.   }
  82. };
  83.  
  84. function NSGetModule(aCompMgr, aFileSpec)
  85.   XPCOMUtils.generateModule([nsURLFormatterService]);
  86.