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

  1. /*
  2.  *=BEGIN SONGBIRD GPL
  3.  *
  4.  * This file is part of the Songbird web player.
  5.  *
  6.  * Copyright(c) 2005-2010 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. /**
  26.  * \file sbDeviceEventMonitor.js
  27.  * \brief This service monitors devices for events, we can then perform various
  28.  * tasks based on these events. This is a global monitor so it will get events
  29.  * for all devices and also detects devices being added or removed.
  30.  *
  31.  */
  32. const Cc = Components.classes;
  33. const Ci = Components.interfaces;
  34. const Cu = Components.utils;
  35.  
  36. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  37. Cu.import("resource://app/jsmodules/sbProperties.jsm");
  38. Cu.import("resource://app/jsmodules/sbLibraryUtils.jsm");
  39. Cu.import("resource://app/jsmodules/StringUtils.jsm");
  40. Cu.import("resource://app/jsmodules/sbSmartMediaListColumnSpecUpdater.jsm");
  41.  
  42. const VIDEO_TOGO_PLAYLIST_NAME = "video-togo";
  43.  
  44. var deviceEventMonitorConfig = {
  45.   className:      "Songbird Device Event Monitor Service",
  46.   cid:            Components.ID("{57b32ce8-a373-4d8d-babb-9d23afeeb409}"),
  47.   contractID:     "@songbirdnest.com/device/event-monitor-service;1",
  48.  
  49.   ifList: [ Ci.sbIDeviceEventListener,
  50.             Ci.nsISupportsWeakReference,
  51.             Ci.nsIObserver ],
  52.  
  53.   categoryList:
  54.   [
  55.     {
  56.       category: 'app-startup',
  57.       entry: 'service-device-event-monitor',
  58.       value: 'service,@songbirdnest.com/device/event-monitor-service;1'
  59.     }
  60.   ]
  61. };
  62.  
  63. function deviceEventMonitor () {
  64.   var obsSvc = Cc['@mozilla.org/observer-service;1']
  65.                  .getService(Ci.nsIObserverService);
  66.  
  67.   // We want to wait until profile-after-change to initialize
  68.   obsSvc.addObserver(this, 'profile-after-change', true);
  69.   obsSvc.addObserver(this, 'quit-application', true);
  70. }
  71.  
  72. deviceEventMonitor.prototype = {
  73.   // XPCOM stuff
  74.   classDescription: deviceEventMonitorConfig.className,
  75.   classID: deviceEventMonitorConfig.cid,
  76.   contractID: deviceEventMonitorConfig.contractID,
  77.   _xpcom_categories: deviceEventMonitorConfig.categoryList,
  78.  
  79.   /**
  80.    * \brief Initialize the deviceEventMonitor service.
  81.    */
  82.   _init: function deviceEventMonitor__init() {
  83.     var deviceManagerSvc = Cc["@songbirdnest.com/Songbird/DeviceManager;2"]
  84.                              .getService(Ci.sbIDeviceManager2);
  85.     deviceManagerSvc.addEventListener(this);
  86.   },
  87.  
  88.   /**
  89.    * \brief Shutdown (cleanup) the deviceEventMonitorService.
  90.    */
  91.   _shutdown: function deviceEventMonitor__shutdown() {
  92.     var deviceManagerSvc = Cc["@songbirdnest.com/Songbird/DeviceManager;2"]
  93.                              .getService(Ci.sbIDeviceManager2);
  94.     deviceManagerSvc.removeEventListener(this);
  95.   },
  96.  
  97.   /**
  98.    * \brief checks if a device supports video formats.
  99.    * \param aDevice device to check.
  100.    * \returns true if device supports video.
  101.    */
  102.   _deviceSupportsVideo:
  103.     function deviceEventMonitor__deviceSupportsVideo(aDevice) {
  104.     var capabilities = aDevice.capabilities;
  105.     var sbIDC = Ci.sbIDeviceCapabilities;
  106.     try {
  107.       if (capabilities.supportsContent(sbIDC.FUNCTION_VIDEO_PLAYBACK,
  108.                                        sbIDC.CONTENT_VIDEO)) {
  109.         return true;
  110.       }
  111.     }
  112.     catch (err) {
  113.       Cu.reportError("Unable to determine if device supports video:" + err);
  114.     }
  115.  
  116.     return false;
  117.   },
  118.  
  119.   /**
  120.    * \brief Checks to see if the Video playlist exists in the main library, if
  121.    * it does not then we will create the playlist if the device supports video.
  122.    * \param aDevice device to check for video support
  123.    */
  124.   _checkForVideoPlaylist:
  125.     function deviceEventMonitor__checkForVideoPlaylist(aDevice) {
  126.  
  127.     // Check if this device supports video and if not we are done.
  128.     if (!this._deviceSupportsVideo(aDevice))
  129.       return;
  130.  
  131.     var mainLibrary = LibraryUtils.mainLibrary;
  132.  
  133.     var listProperties =
  134.       Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
  135.         .createInstance(Ci.sbIMutablePropertyArray);
  136.     listProperties.appendProperty(SBProperties.isList, "1");
  137.     listProperties.appendProperty(SBProperties.hidden, "0");
  138.     listProperties.appendProperty(SBProperties.customType,
  139.                                   VIDEO_TOGO_PLAYLIST_NAME);
  140.  
  141.     var playlistFound = false;
  142.     var searchListener = {
  143.       onEnumerationBegin: function(aMediaList) {
  144.         return Ci.sbIMediaListEnumerationListener.CONTINUE;
  145.       },
  146.       onEnumeratedItem: function(aMediaList, aMediaItem) {
  147.         // Already created so we are done.
  148.         playlistFound = true;
  149.         return Ci.sbIMediaListEnumerationListener.CANCEL;
  150.       },
  151.       onEnumerationEnd: function(aMediaList, aStatusCode) {
  152.       }
  153.     };
  154.  
  155.     mainLibrary.enumerateItemsByProperties(listProperties,
  156.                                            searchListener,
  157.                                            Ci.sbIMediaList.ENUMERATIONTYPE_SNAPSHOT);
  158.  
  159.     if (!playlistFound) {
  160.       var propertyManager =
  161.         Cc["@songbirdnest.com/Songbird/Properties/PropertyManager;1"]
  162.           .getService(Ci.sbIPropertyManager);
  163.       var typePI = propertyManager.getPropertyInfo(SBProperties.contentType);
  164.       const sbILDSML = Components.interfaces.sbILocalDatabaseSmartMediaList;
  165.  
  166.       var videoSmartPlaylist = [
  167.         {
  168.           name: "&device.sync.video-togo.playlist",
  169.           conditions: [
  170.             {
  171.               property     : SBProperties.contentType,
  172.               operator     : typePI.getOperator(typePI.OPERATOR_EQUALS),
  173.               leftValue    : "video",
  174.               rightValue   : null,
  175.               displayUnit  : null
  176.             }
  177.           ],
  178.           matchType        : sbILDSML.MATCH_TYPE_ALL,
  179.           limitType        : sbILDSML.LIMIT_TYPE_NONE,
  180.           limit            : 0,
  181.           selectDirection  : false,
  182.           randomSelection  : true,
  183.           autoUpdate       : true
  184.         }
  185.       ];
  186.       // Create the smart playlist since it does not exist.
  187.       var list = mainLibrary.createMediaList("smart");
  188.       for each (var item in videoSmartPlaylist) {
  189.         for (var prop in item) {
  190.           if (prop == "conditions") {
  191.             for each (var condition in item.conditions) {
  192.               list.appendCondition(condition.property,
  193.                                    condition.operator,
  194.                                    condition.leftValue,
  195.                                    condition.rightValue,
  196.                                    condition.displayUnit);
  197.             }
  198.           } else {
  199.             list[prop] = item[prop];
  200.           }
  201.         }
  202.       }
  203.       list.setProperty(SBProperties.customType, VIDEO_TOGO_PLAYLIST_NAME);
  204.       list.setProperty(SBProperties.hidden, "0");
  205.       SmartMediaListColumnSpecUpdater.update(list);
  206.       list.rebuild();
  207.     }
  208.   },
  209.  
  210.   // sbIDeviceEventListener
  211.   onDeviceEvent: function deviceEventMonitor_onDeviceEvent(aDeviceEvent) {
  212.     switch(aDeviceEvent.type) {
  213.       case Ci.sbIDeviceEvent.EVENT_DEVICE_ADDED:
  214.         var device = aDeviceEvent.data.QueryInterface(Ci.sbIDevice);
  215.         if (device)
  216.           this._checkForVideoPlaylist(device);
  217.         break;
  218.     }
  219.   },
  220.  
  221.   // nsIObserver
  222.   observe: function deviceEventMonitor_observer(subject, topic, data) {
  223.     var obsSvc = Cc['@mozilla.org/observer-service;1']
  224.                    .getService(Ci.nsIObserverService);
  225.  
  226.     switch (topic) {
  227.       case 'quit-application':
  228.         obsSvc.removeObserver(this, 'quit-application');
  229.         this._shutdown();
  230.       break;
  231.       case 'profile-after-change':
  232.         obsSvc.removeObserver(this, 'profile-after-change');
  233.         this._init();
  234.       break;
  235.     }
  236.   },
  237.  
  238.   // nsISupports
  239.   QueryInterface: XPCOMUtils.generateQI(deviceEventMonitorConfig.ifList)
  240. };
  241.  
  242. /**
  243.  * /brief XPCOM initialization code
  244.  */
  245. function NSGetModule(compMgr, fileSpec) {
  246.   return XPCOMUtils.generateModule([deviceEventMonitor]);
  247. }
  248.