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

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2008 POTI, Inc.
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the "GPL").
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. const Cc = Components.classes;
  28. const Ci = Components.interfaces;
  29. const Cr = Components.results;
  30. const Ce = Components.Exception;
  31. const Cu = Components.utils;
  32.  
  33. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  34.  
  35. const WINDOWTYPE_SONGBIRD_PLAYER      = "Songbird:Main";
  36. const WINDOWTYPE_SONGBIRD_CORE        = "Songbird:Core";
  37.  
  38. /**
  39.  * \class ApplicationController
  40.  * \brief Service representing the global instance of the Songbird application.
  41.  * \sa sbIApplicationController.idl
  42.  */
  43. function ApplicationController() {
  44. }
  45. ApplicationController.prototype = {
  46.   QueryInterface          : XPCOMUtils.generateQI(
  47.       [Ci.sbIApplicationController, Ci.nsIClassInfo]),
  48.   classDescription        : 'Songbird Root Application Service Implementation',
  49.   classID                 : Components.ID("{8492c5a0-ab8e-11dd-ad8b-0800200c9a66}"),
  50.   contractID              : "@songbirdnest.com/Songbird/ApplicationController;1",
  51.   flags                   : Ci.nsIClassInfo.MAIN_THREAD_ONLY,
  52.   implementationLanguage  : Ci.nsIProgrammingLanguage.JAVASCRIPT,
  53.   getHelperForLanguage    : function(aLanguage) { return null; },
  54.   getInterfaces : function(count) {
  55.     var interfaces = [Ci.sbIApplicationController,
  56.                       Ci.nsIClassInfo,
  57.                       Ci.nsISupports
  58.                      ];
  59.     count.value = interfaces.length;
  60.     return interfaces;
  61.   },
  62.  
  63.   /**
  64.    * Get the current active window, regardless of category.
  65.    */
  66.   get activeWindow() {
  67.     var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"]
  68.                            .getService(Ci.nsIWindowMediator);
  69.     return windowMediator.getMostRecentWindow(null);
  70.   },
  71.   
  72.   /**
  73.    * Get the current active window of type Songbird:Main.
  74.    */
  75.   get activeMainWindow() {
  76.     var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"]
  77.                            .getService(Ci.nsIWindowMediator);
  78.     return windowMediator.getMostRecentWindow(WINDOWTYPE_SONGBIRD_PLAYER);
  79.   },
  80.  
  81.   /**
  82.    * Play something using the UI for context
  83.    */
  84.   playDefault: function ApplicationController_playDefault() {
  85.     var mm = Cc["@songbirdnest.com/Songbird/Mediacore/Manager;1"]
  86.                .getService(Ci.sbIMediacoreManager);
  87.                  
  88.     // If paused, just continue
  89.     if (mm.status.state == Ci.sbIMediacoreStatus.STATUS_PAUSED) {
  90.       mm.playbackControl.play();
  91.       return;
  92.     }
  93.     
  94.     var handled = false;
  95.     
  96.     // Try to get the front-most window to start
  97.     // playback, based on whatever context it has
  98.     var window = this.activeWindow;
  99.     if (window) {
  100.       if (this._sendEventToWindow("Play", window))
  101.         return;
  102.     }
  103.         
  104.     // If that didn't work, then try the main songbird window
  105.     // (if different)
  106.     var mainWindow = this.activeMainWindow;
  107.     if (mainWindow && mainWindow != window) {
  108.       if (this._sendEventToWindow("Play", mainWindow)) 
  109.         return;
  110.     }
  111.     
  112.     // Fallback to a stashed view, or the main library
  113.     
  114.     // When the tabbrowser is unloaded, the currently active view is
  115.     // set on the sequencer, just in case it is of some use
  116.     // to the next window to load.
  117.     var view = mm.sequencer.view;
  118.  
  119.     // Still no view, fetch main library.
  120.     if(!view) {
  121.       if (typeof(LibraryUtils) == "undefined") {
  122.         Cu.import("resource://app/jsmodules/sbLibraryUtils.jsm");
  123.       }
  124.       view = LibraryUtils.createStandardMediaListView(LibraryUtils.mainLibrary);
  125.     }
  126.  
  127.     var index = view.selection.currentIndex;
  128.       
  129.     // If same view as current view on sequencer and nothing
  130.     // selected in the view, use sequencer view position.
  131.     if((index == -1) && (mm.sequencer.view == view)) {
  132.       index = mm.sequencer.viewPosition;
  133.     }
  134.  
  135.     mm.sequencer.playView(view,  
  136.                            Math.max(index, 
  137.                                     Ci.sbIMediacoreSequencer.AUTO_PICK_INDEX)); 
  138.   },
  139.   
  140.   /** 
  141.    * Helper to dispatch and event to a window. Returns true if
  142.    * the event was handled.
  143.    */
  144.   _sendEventToWindow: 
  145.   function ApplicationController__sendEventToWindow(aEventName, aWindow) {
  146.     var event = aWindow.document.createEvent("Events");
  147.     event.initEvent(aEventName, true, true);
  148.     return !aWindow.dispatchEvent(event);
  149.   }
  150. }
  151.  
  152.  
  153. function NSGetModule(compMgr, fileSpec) {
  154.   return XPCOMUtils.generateModule([ApplicationController]);
  155. }