track-metadata.js
Go to the documentation of this file.
1 /*
2  * BEGIN NIGHTINGALE GPL
3  *
4  * This file is part of the Nightingale Media Player.
5  *
6  * Copyright(c) 2013
7  * http://getnightingale.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 NIGHTINGALE GPL
23  */
24 
25 if (typeof(Ci) == "undefined")
26  var Ci = Components.interfaces;
27 if (typeof(Cc) == "undefined")
28  var Cc = Components.classes;
29 if (typeof(Cr) == "undefined")
30  var Cr = Components.results;
31 if (typeof(Cu) == "undefined")
32  var Cu = Components.utils;
33 
34 try {
35  Cu.import("resource://gre/modules/XPCOMUtils.jsm");
36  Cu.import("resource://app/jsmodules/sbProperties.jsm");
37  Cu.import("resource://app/jsmodules/sbCoverHelper.jsm");
38 } catch (error) {
39  alert("lnNotifs: Unexpected error - module import error\n\n" + error)
40 }
41 
43  mmService: null,
44  strConv: null,
45  lastItem: null,
46  wm: null,
47  mainwindow: null,
48  prefs: null,
49  lnNotifsService: null,
50  ios: null,
51 
52  onLoad: function() {
53  var mainwindow = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation)
54  .QueryInterface(Ci.nsIDocShellTreeItem).rootTreeItem
55  .QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
56  this.mmService = Cc["@songbirdnest.com/Songbird/Mediacore/Manager;1"].getService(Ci.sbIMediacoreManager);
57  this.strConv = Cc["@mozilla.org/intl/scriptableunicodeconverter"].getService(Ci.nsIScriptableUnicodeConverter);
58  this.lnNotifsService = Cc["@getnightingale.com/Nightingale/lnNotifs;1"].getService(Ci.ILNNotifs);
59  this.wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
60  this.ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
61 
62  this.mainwindow = this.wm.getMostRecentWindow("Songbird:Main");
63  var windowTitle = this.mainwindow.document.getElementById("mainplayer").getAttribute("title");
64 
65  this.prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.libnotify-notifs.");
66 
67  this.lnNotifsService.InitNotifs(windowTitle);
68 
69  this.strConv.charset = 'utf-8';
70  var mm = this.mmService;
71  var that = this;
72 
73  this.mmService.addListener({
74  onMediacoreEvent: function(event) {
75  // Get mediacore event
76  if (that.mmService.sequencer.view == null) return;
77 
78  if (event.type == Ci.sbIMediacoreEvent.TRACK_CHANGE) {
79  // Check if notifications are enabled
80  var notifsEnabled = lnNotifs.prefs.getBoolPref("enableNotifications");
81  lnNotifs.lnNotifsService.EnableNotifications(notifsEnabled);
82 
83  var curItem = that.mmService.sequencer.currentItem;
84  // Don't notify for mediacore sequencing oddities
85  if (that.lastItem == curItem) return;
86  else that.lastItem = curItem;
87 
88  var resourceURL = curItem.getProperty(SBProperties.primaryImageURL);
89  var fileURI = null;
90  if(resourceURL) {
91  try{
92  fileURI = that.ios.newURI(decodeURI(resourceURL), null, null).QueryInterface(Ci.nsIFileURL).file.path;
93  }
94  catch(e) {
95  Cu.reportError(e);
96  }
97  }
98 
99  var artist = that.strConv.ConvertFromUnicode(curItem.getProperty(SBProperties.artistName));
100  var album = that.strConv.ConvertFromUnicode(curItem.getProperty(SBProperties.albumName));
101  var track = that.strConv.ConvertFromUnicode(curItem.getProperty(SBProperties.trackName));
102  var timeout = that.prefs.getIntPref("notificationTimeout");
103 
104  that.lnNotifsService.TrackChangeNotify(track, artist, album, fileURI, timeout);
105  }
106  }
107  });
108  },
109 
110  onUnload: function() {
111 
112  }
113 };
114 
115 window.addEventListener("load", function(e) { lnNotifs.onLoad(); }, false);
116 window.addEventListener("unload", function(e) { lnNotifs.onUnload(); }, false);
const Cu
const Cc
var event
let window
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
Lastfm onLoad
Definition: mini.js:36
void EnableNotifications(in PRBool enable)
return null
Definition: FeedWriter.js:1143
var prefs
Definition: FeedWriter.js:1169
const Cr
const Ci
var ios
Definition: head_feeds.js:5
function onUnload()
onUnload - called when the cover preview window unloads.
Definition: coverPreview.js:36