favourites.js
Go to the documentation of this file.
1 if (typeof(Cc) == "undefined")
2  var Cc = Components.classes;
3 if (typeof(Ci) == "undefined")
4  var Ci = Components.interfaces;
5 
6 #ifdef METRICS_ENABLED
7 if (typeof(gMetrics) == "undefined")
8  var gMetrics = Cc["@songbirdnest.com/Songbird/Metrics;1"]
9  .createInstance(Ci.sbIMetrics);
10 #endif
11 
12 const shoutcastTempLibGuid = "extensions.shoutcast-radio.templib.guid";
13 
14 if (typeof(songbirdMainWindow) == "undefined")
15  var songbirdMainWindow = Cc["@mozilla.org/appshell/window-mediator;1"]
16  .getService(Ci.nsIWindowMediator)
17  .getMostRecentWindow("Songbird:Main").window;
18 
20  list : null,
21  genres : null,
22 
23  listListener : {
24  onEnumerationBegin: function(list) {
25  FavouriteStreams.genres = new Array();
26  return Ci.sbIMediaListEnumerationListener.CONTINUE;
27  },
28  onEnumeratedItem: function(list, item) {
29  item.setProperty(SC_bookmark,
30  "chrome://shoutcast-radio/skin/invis-16x16.png");
31  var genre = item.getProperty(SBProperties.genre);
32  if (typeof(FavouriteStreams.genres[genre]) == "undefined") {
33  // pull genres and update this feed
34  FavouriteStreams.updateGenre(genre);
35  }
36  return Ci.sbIMediaListEnumerationListener.CONTINUE;
37  },
38  onEnumerationEnd: function(list, status) {
39  }
40  },
41 
42  updateGenre : function(genre) {
43  // Get our updated station list for this genre
44  var stationList = ShoutcastRadio.getStationList(genre);
45 
46  // Grab all stations in our list with this genre
47  var ourFavourites = this.list.getItemsByProperty(SBProperties.genre,
48  genre);
49 
50  for (var i=0; i<ourFavourites.length; i++) {
51  var item = ourFavourites.queryElementAt(i, Ci.sbIMediaItem);
52  for (var j=0; j<stationList.length; j++) {
53  if (stationList[j].getAttribute("id") ==
54  item.getProperty(SC_id))
55  {
56  var comment = stationList[j].getAttribute("ct");
57  item.setProperty(SC_comment, comment);
58  }
59  }
60  }
61  },
62 
63  updateComments : function() {
64  this.list = document.getElementById("playlist").mediaListView.mediaList;
65  this.list.enumerateAllItems(this.listListener);
66  },
67 
68  onLoad : function() {
69  var pls = document.getElementById("playlist");
70  var list = pls.mediaListView.mediaList;
71  if (list.getProperty(SBProperties.customType) == "radio_favouritesList")
72  {
73  pls.addEventListener("PlaylistCellClick", this.myCellClick, false);
74 
75  // OMG. I'm sorry. Forgive me.
76  FavouriteStreams.oldOnPlay = songbirdMainWindow
77  .gSongbirdPlayerWindow.onPlay;
78  window.addEventListener("unload", function() {
79  songbirdMainWindow.gSongbirdPlayerWindow.onPlay =
80  FavouriteStreams.oldOnPlay;
81  }, false);
82  songbirdMainWindow.gSongbirdPlayerWindow.onPlay = function() {};
83 
84  pls.addEventListener("Play", this.onPlay, false);
85  this.updateComments();
86  }
87  },
88 
89  myCellClick : function(e) {
90  var prop = e.getData("property");
91  var item = e.getData("item");
92  if (prop == SC_bookmark) {
93 #ifdef METRICS_ENABLED
94  // # of times a station is unfavourited
95  gMetrics.metricsInc("shoutcast", "favourites", "removed");
96 #endif
97 
98  var list =
99  document.getElementById("playlist").mediaListView.mediaList;
100  list.remove(item);
101  }
102  },
103 
104  onPlay : function(e) {
105  var item = document.getElementById("playlist").mediaListView
106  .selection.currentMediaItem;
107  var id = item.getProperty(SC_id);
108  var plsURL = ShoutcastRadio.getListenURL(id);
109  var plsMgr = Cc["@songbirdnest.com/Songbird/PlaylistReaderManager;1"]
110  .getService(Ci.sbIPlaylistReaderManager);
111  var listener = Cc["@songbirdnest.com/Songbird/PlaylistReaderListener;1"]
112  .createInstance(Ci.sbIPlaylistReaderListener);
113  var ioService = Cc["@mozilla.org/network/io-service;1"]
114  .getService(Ci.nsIIOService);
115 
116  // Get our temporary stream list
117  var libGuid = Application.prefs.get(shoutcastTempLibGuid);
118  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
119  .getService(Ci.sbILibraryManager);
120  this.tempLib = libraryManager.getLibrary(libGuid.value);
121  a = this.tempLib.getItemsByProperty(
122  SBProperties.customType, "radio_tempStreamList");
123  this.streamList = a.queryElementAt(0, Ci.sbIMediaList);
124 
125  // clear the current list of any existing streams, etc.
126  this.streamList.clear();
127 
128  listener.playWhenLoaded = true;
129  listener.observer = {
130  observe: function(aSubject, aTopic, aData) {
131  if (aTopic == "success") {
132  dump("Success\n");
133  var list = aSubject;
134  var name = item.getProperty(SC_streamName);
135  for (var i=0; i<list.length; i++) {
136  listItem = list.getItemByIndex(i);
137  listItem.setProperty(SC_streamName, name);
138  listItem.setProperty(SC_id, id);
139  listItem.setProperty(SBProperties.outerGUID, item.guid);
140  }
141  } else {
142  alert("Failed to load " + item.getProperty(SC_streamName) +
143  "\n");
144  }
145  }
146  }
147 
148 #ifdef METRICS_ENABLED
149  // # of times a station is played
150  gMetrics.metricsInc("shoutcast", "station", "total.played");
151 
152  // # of times this station (ID) is played
153  gMetrics.metricsInc("shoutcast", "station", "played." + id.toString());
154 
155  // # of times this genre is played
156  var genre = item.getProperty(SBProperties.genre);
157  gMetrics.metricsInc("shoutcast", "genre", "played." + genre);
158 #endif
159 
160  if (id == -1) {
161  plsURL = item.getProperty(SBProperties.contentURL);
162  }
163  var uri = ioService.newURI(plsURL, null, null);
164  plsMgr.loadPlaylist(uri, this.streamList, null, false, listener);
165 
166  e.stopPropagation();
167  e.preventDefault();
168  }
169 }
170 
171 FavouriteStreams.onLoad();
const Cc
var Application
Definition: sbAboutDRM.js:37
var ioService
const SC_bookmark
let window
var FavouriteStreams
Definition: favourites.js:19
const shoutcastTempLibGuid
Definition: favourites.js:12
Lastfm onLoad
Definition: mini.js:36
var libraryManager
return null
Definition: FeedWriter.js:1143
const SC_id
var uri
Definition: FeedWriter.js:1135
return aWindow document documentElement getAttribute(aAttribute)||dimension
const SC_comment
const Ci
_getSelectedPageStyle s i
const SC_streamName
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe