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

  1. /*
  2.  *=BEGIN SONGBIRD GPL
  3.  *
  4.  * This file is part of the Songbird web player.
  5.  *
  6.  * Copyright(c) 2005-2009 POTI, Inc.
  7.  * http://www.songbirdnest.com
  8.  *
  9.  * This file may be licensed under the terms of of the
  10.  * GNU General Public License Version 2 (the "GPL").
  11.  *
  12.  * Software distributed under the License is distributed
  13.  * on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  14.  * express or implied. See the GPL for the specific language
  15.  * governing rights and limitations.
  16.  *
  17.  * You should have received a copy of the GPL along with this
  18.  * program. If not, go to http://www.gnu.org/licenses/gpl.html
  19.  * or write to the Free Software Foundation, Inc.,
  20.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21.  *
  22.  *=END SONGBIRD GPL
  23.  */
  24.  
  25. const Cc = Components.classes;
  26. const CC = Components.Constructor;
  27. const Ci = Components.interfaces;
  28. const Cr = Components.results;
  29. const Cu = Components.utils;
  30.  
  31. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  32. Cu.import("resource://app/jsmodules/ArrayConverter.jsm");
  33.  
  34. function sbMLM() {
  35.   // initialise private variables
  36.   this._providers = {};
  37.   this._numProviders = 0;
  38.  
  39.   this._defaultProvider = null;
  40.   // enumerate the catman for all the metadata-lookup managers
  41.   var catMgr = Cc["@mozilla.org/categorymanager;1"]
  42.                  .getService(Ci.nsICategoryManager);
  43.   var e = catMgr.enumerateCategory('metadata-lookup');
  44.   while (e.hasMoreElements()) {
  45.     var key = e.getNext().QueryInterface(Ci.nsISupportsCString);
  46.     var contract = catMgr.getCategoryEntry('metadata-lookup', key);
  47.     try {
  48.       var provider = Cc[contract].getService(Ci.sbIMetadataLookupProvider);
  49.       this._providers[provider.name] = provider;
  50.       this._numProviders++;
  51.  
  52.       // always make the first one we find the default... by default
  53.       if (!this._defaultProvider)
  54.         this._defaultProvider = provider;
  55.       else {
  56.         // multiple providers
  57.         // if the current provider has a lower weight than the currently
  58.         // set default provider, then change the default to be this one
  59.         if (provider.weight < this._defaultProvider.weight)
  60.           this._defaultProvider = provider;
  61.       }
  62.     } catch (e) {
  63.       dump("mlm // failed to register: " + contract + "\n" +
  64.            "mlm //             reason: " + e + "\n");
  65.     }
  66.   }
  67.  
  68.   Cc["@mozilla.org/observer-service;1"]
  69.     .getService(Ci.nsIObserverService)
  70.     .addObserver(this, "songbird-library-manager-before-shutdown", false);
  71. }
  72.  
  73. sbMLM.prototype = {
  74.   classDescription : 'Songbird Metadata Lookup Manager',
  75.   classID : Components.ID('46733000-1dd2-11b2-8022-ba2da8a6a950'),
  76.   contractID : '@songbirdnest.com/Songbird/MetadataLookup/manager;1',
  77.   QueryInterface : XPCOMUtils.generateQI([Ci.sbIMetadataLookupManager,
  78.                                           Ci.nsIObserver]),
  79.  
  80.   _providers: null,
  81.   _numProviders: 0,
  82.   _defaultProvider: null,
  83.  
  84.   // Return an nsISimpleEnumerator of all the providers registered with the
  85.   // metadata lookup manager
  86.   getProviders: function() {
  87.     return ArrayConverter.enumerator([p for each (p in this._providers)]);
  88.   },
  89.  
  90.   // Return the metadata provider of the passed in name
  91.   getProvider: function(aProviderName) {
  92.     if (this._providers[aProviderName])
  93.       return this._providers[aProviderName];
  94.     else
  95.       throw Cr.NS_ERROR_NOT_AVAILABLE;
  96.   },
  97.  
  98.   get numProviders() this._numProviders,
  99.  
  100.   get defaultProvider() {
  101.     if (this._numProviders == 0)
  102.       throw Cr.NS_ERROR_NOT_AVAILABLE;
  103.     else {
  104.       // check to see if the user has specified a default provider in their
  105.       // preferences.  if so, return that one.
  106.       var Application = Cc["@mozilla.org/fuel/application;1"]
  107.                           .getService(Ci.fuelIApplication);
  108.       var userDefault = Application.prefs.getValue(
  109.                               "metadata.lookup.default_provider", "");
  110.       if (userDefault && this._providers[userDefault]) {
  111.         return this._providers[userDefault];
  112.       } else {
  113.         // if the specified default doesn't exist, or no preference has been
  114.         // set, then return our default one as we scanned it
  115.         return this._defaultProvider;
  116.       }
  117.     }
  118.   },
  119.  
  120.   observe: function sbMLM_observe(aSubject, aTopic, aData) {
  121.     if (aTopic == "songbird-library-manager-before-shutdown") {
  122.       // clean up providers list; this is necessary because the service shuts
  123.       // down way too late
  124.       Cc["@mozilla.org/observer-service;1"]
  125.         .getService(Ci.nsIObserverService)
  126.         .removeObserver(this, "songbird-library-manager-before-shutdown");
  127.       this._providers = {};
  128.       this._numProviders = 0;
  129.       this._defaultProvider = null;
  130.     }
  131.   },
  132. }
  133.  
  134. function NSGetModule(compMgr, fileSpec) {
  135.   return XPCOMUtils.generateModule([sbMLM]);
  136. }
  137.