sound-menu.js
Go to the documentation of this file.
1 try {
2 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
3 Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
4 Components.utils.import("resource://app/jsmodules/sbCoverHelper.jsm");
5 }
6 catch (error) {alert("Unity Integration: Unexpected error - module import error\n\n" + error)}
7 
11 
12 if (typeof UnityIntegration == 'undefined') {
13  var UnityIntegration = {};
14 };
15 
16 UnityIntegration.soundMenu = {
17  xulAppInfo: null,
18  stringConverter: null,
19  gMM: null,
20  wm: null,
21  unityServiceProxy: null,
23  mainwindow: null,
24  prefs: null,
25  lastItem: null,
26 
27  onLoad: function () {
28  var mainwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIWebNavigation)
29  .QueryInterface(Components.interfaces.nsIDocShellTreeItem).rootTreeItem
30  .QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindow);
31 
32  this.xulAppInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
33  this.stringConverter = Components.classes['@mozilla.org/intl/scriptableunicodeconverter'].getService(Components.interfaces.nsIScriptableUnicodeConverter);
34  this.gMM = Components.classes["@songbirdnest.com/Songbird/Mediacore/Manager;1"].getService(Components.interfaces.sbIMediacoreManager);
35  this.wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
36  this.mainwindow = this.wm.getMostRecentWindow("Songbird:Main");
37  this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.unity-integration.");
38  this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
39 
40  UnityIntegration.soundMenu.preferencesObserver.register();
41 
42  this.unityServiceProxy = Components.classes["@LookingMan.org/Songbird-Nightingale/UnityProxy;1"].getService(Components.interfaces.IUnityProxy);
43 
44  var windowTitle = mainwindow.document.getElementById("mainplayer").getAttribute("title");
45 
46  if (this.xulAppInfo.name == "Songbird")
47  this.unityServiceProxy.InitializeFor("songbird.desktop", windowTitle);
48  else if (this.xulAppInfo.name == "Nightingale")
49  this.unityServiceProxy.InitializeFor("nightingale.desktop", windowTitle);
50  else {
51  alert("Unity Integration: Unexpected error - your application is not supported")
52  return;
53  }
54 
55  UnityIntegration.soundMenu.preferencesObserver.observe(null, "needInit", "enableNotifications");
56  UnityIntegration.soundMenu.preferencesObserver.observe(null, "needInit", "hideOnClose");
57 
58  this.stringConverter.charset = 'utf-8';
59  var mm = this.gMM;
60  var that = this;
61 
62  var soundMenuObserver = {
63  observe : function(subject, topic, data) {
64  switch (topic) {
65  case "sound-menu-play":
66  // If we are already playing something just pause/unpause playback
67  var sbIMediacoreStatus = Components.interfaces.sbIMediacoreStatus;
68  if (mm.status.state == sbIMediacoreStatus.STATUS_PAUSED) {
69  mm.playbackControl.play();
70  } else if(mm.status.state == sbIMediacoreStatus.STATUS_PLAYING ||
71  mm.status.state == sbIMediacoreStatus.STATUS_BUFFERING) {
72  that.unityServiceProxy.SoundMenuSetPlayingState(true);
73  // Otherwise dispatch a play event. Someone should catch this
74  // and intelligently initiate playback. If not, just have
75  // the playback service play the default.
76  } else {
77  var event = document.createEvent("Events");
78  event.initEvent("Play", true, true);
79  var notHandled = that.mainwindow.dispatchEvent(event);
80  if (notHandled) {
81  // If we have no context, initiate playback
82  // via the root application controller
83  var app = Components.classes["@songbirdnest.com/Songbird/ApplicationController;1"]
84  .getService(Components.interfaces.sbIApplicationController);
85  app.playDefault();
86  }
87  }
88  break;
89 
90  case "sound-menu-pause":
91  songbird.pause();
92  break;
93 
94  case "sound-menu-next":
95  songbird.next();
96  break;
97 
98  case "sound-menu-previous":
99  songbird.previous();
100  break;
101  }
102  }
103  };
104 
105  this.observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
106  this.observerService.addObserver(soundMenuObserver, "sound-menu-play", false);
107  this.observerService.addObserver(soundMenuObserver, "sound-menu-pause", false);
108  this.observerService.addObserver(soundMenuObserver, "sound-menu-next", false);
109  this.observerService.addObserver(soundMenuObserver, "sound-menu-previous", false);
110 
111  this.gMM.addListener({
112  onMediacoreEvent : function(event) {
113  var item = event.data;
114  if (that.gMM.sequencer.view == null) return;
115  var list = that.gMM.sequencer.view.mediaList;
116 
117  switch (event.type) {
118  case Components.interfaces.sbIMediacoreEvent.TRACK_CHANGE:
119  if (that.lastItem == that.gMM.sequencer.currentItem) break;
120  else that.lastItem = that.gMM.sequencer.currentItem;
121 
122  var artist = that.stringConverter.ConvertFromUnicode(that.gMM.sequencer.currentItem.getProperty(SBProperties.artistName));
123  var album = that.stringConverter.ConvertFromUnicode(that.gMM.sequencer.currentItem.getProperty(SBProperties.albumName));
124  var track = that.stringConverter.ConvertFromUnicode(that.gMM.sequencer.currentItem.getProperty(SBProperties.trackName));
125  var coverFilePath = null;
126 
127  var resourceURL = that.gMM.sequencer.currentItem.getProperty(SBProperties.primaryImageURL);
128  if (resourceURL != null) {
129  try {
130  var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
131  var fileURI = ios.newURI(decodeURI(resourceURL), null, null).QueryInterface(Components.interfaces.nsIFileURL);
132  var fileurl = fileURI.file.path; // code from FolderSync
133  if (fileURI.file.exists())
134  coverFilePath = fileurl;
135  } catch(e) {
136  Cu.reportError(e + " " + resourceURL);
137  }
138  }
139 
140  that.unityServiceProxy.SoundMenuSetTrackInfo(track, artist, album, coverFilePath);
141  break;
142 
143  case Components.interfaces.sbIMediacoreEvent.STREAM_START:
144  that.unityServiceProxy.SoundMenuSetPlayingState(UNITY_PLAYBACK_STATE_PLAYING);
145  break;
146 
147  case Components.interfaces.sbIMediacoreEvent.STREAM_PAUSE:
148  that.unityServiceProxy.SoundMenuSetPlayingState(UNITY_PLAYBACK_STATE_PAUSED);
149  break;
150 
151  case Components.interfaces.sbIMediacoreEvent.STREAM_STOP:
152  case Components.interfaces.sbIMediacoreEvent.STREAM_END:
153  that.unityServiceProxy.SoundMenuSetPlayingState(UNITY_PLAYBACK_STATE_STOPED);
154  break;
155 
156  default:
157  break;
158  }
159  }
160  });
161  },
162 
163  onUnLoad: function () {
164  UnityIntegration.soundMenu.preferencesObserver.unregister();
165  },
166 
167  registerOnClose: function (force) {
168  if (this.prefs.getBoolPref("hideOnClose")) {
169  var that = this;
170  this.mainwindow.onclose = function () {
171  that.unityServiceProxy.HideWindow();
172  return false;
173  }
174  } else if (force) {
175  this.mainwindow.onclose = "";
176  }
177  },
178 
179  preferencesObserver: {
180  register: function () {
181  UnityIntegration.soundMenu.prefs.addObserver("", this, false);
182  },
183 
184  unregister: function () {
185  UnityIntegration.soundMenu.prefs.removeObserver("", this);
186  },
187 
188  observe: function (aSubject, aTopic, aData) {
189  if(aTopic != "nsPref:changed" && aTopic != "needInit") return;
190 
191  if (aData == "hideOnClose")
192  UnityIntegration.soundMenu.registerOnClose(true);
193  }
194  }
195 };
196 
197 window.addEventListener("load", function(e) { UnityIntegration.soundMenu.onLoad(); }, false);
198 window.addEventListener("unload", function(e) { UnityIntegration.soundMenu.onUnLoad(); }, false);
const Cu
var gMM
Definition: windowUtils.js:62
static nsCOMPtr< nsIObserverService > observerService
Definition: UnityProxy.cpp:6
const UNITY_PLAYBACK_STATE_STOPED
Definition: sound-menu.js:10
var event
let window
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
const UNITY_PLAYBACK_STATE_PAUSED
Definition: sound-menu.js:8
Lastfm onLoad
Definition: mini.js:36
return null
Definition: FeedWriter.js:1143
var prefs
Definition: FeedWriter.js:1169
const UNITY_PLAYBACK_STATE_PLAYING
Definition: sound-menu.js:9
observe topic
Definition: FeedWriter.js:1326
var ios
Definition: head_feeds.js:5
observe data
Definition: FeedWriter.js:1329
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe