multiCDLookupResultsDialog.js
Go to the documentation of this file.
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 if (typeof(Cc) == "undefined")
26  var Cc = Components.classes;
27 if (typeof(Ci) == "undefined")
28  var Ci = Components.interfaces;
29 if (typeof(Cr) == "undefined")
30  var Cr = Components.results;
31 if (typeof(Cu) == "undefined")
32  var Cu = Components.utils;
33 
34 Cu.import("resource://app/jsmodules/sbProperties.jsm");
35 Cu.import("resource://app/jsmodules/ArrayConverter.jsm");
36 Cu.import("resource://app/jsmodules/StringUtils.jsm");
37 
38 var multiCDDialog = {
39  library: null,
40  _infolist: null,
41  // Artist name to be set (for onEnumeratedItem)
42  _artistValue: null,
43  // Album title to be set (for onEnumeratedItem)
44  _albumValue: null,
45  // True if user cancels - only set artist/album if empty, don't overwrite
46  // existing data.
47  _setEmptyOnly: false,
48  _curTrackIndex: 1,
49 
53  onload: function onload()
54  {
55  this.library = window.arguments[0].QueryInterface(Ci.sbIDeviceLibrary);
56  this._device = this.library.device;
57  this._metadataResults = ArrayConverter.JSArray(
58  window.arguments[1].QueryInterface(
59  Ci.nsISimpleEnumerator));
60 
61  this._infolist = document.getElementById("infolist");
62  this._other = document.getElementById("other");
63 
64  this._jobTracksMap = [];
65  this._jobResultIndexMap =
66  Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
67  .createInstance(Ci.nsIMutableArray);
68 
69  for (i in this._metadataResults)
70  {
71  var result = this._metadataResults[i];
72  result.QueryInterface(Ci.sbIMetadataAlbumDetail);
73  var props = SBProperties.arrayToJSObject(result.properties);
74 
75  var radio =
76  this._infolist.insertItemAt(
77  this._infolist.getIndexOfItem(this._other),
78  SBFormattedString("cdrip.lookup.name.format",
79  [props[SBProperties.artistName],
80  props[SBProperties.albumName]]),
81  i);
82 
83  var tracks = document.createElementNS(
84  "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
85  "sb-cdtracks");
86  if (i == 0)
87  tracks.setAttribute("first-sb-cdtracks", "true");
88 
89  this._infolist.insertBefore(tracks, this._other);
90 
91  // Do the follow-up call to get album detail for each track
92  var mlm = Cc["@songbirdnest.com/Songbird/MetadataLookup/manager;1"]
93  .getService(Ci.sbIMetadataLookupManager);
94  var job = mlm.defaultProvider.getAlbumDetail(result);
95 
96  // Map the job to the results index so we can update it later
97  this._jobResultIndexMap.appendElement(job, false);
98  // Map the job to the sb-cdtracks xbl
99  this._jobTracksMap[i] = tracks;
100  if (job.status == Ci.sbIJobProgress.STATUS_RUNNING) {
101  job.addJobProgressListener(this);
102  } else {
103  this.onJobProgress(job);
104  }
105  }
106 
107  this._infolist.selectedIndex = 0;
108 
109  // Listen for device events.
110  var deviceManager = Cc["@songbirdnest.com/Songbird/DeviceManager;2"]
111  .getService(Ci.sbIDeviceManager2);
112  deviceManager.addEventListener(this);
113  },
114 
118  onunload: function onunload()
119  {
120  // Remove device event listener.
121  var deviceManager = Cc["@songbirdnest.com/Songbird/DeviceManager;2"]
122  .getService(Ci.sbIDeviceManager2);
123  deviceManager.removeEventListener(this);
124  },
125 
129  onJobProgress: function onJobProgress(job)
130  {
131  job.QueryInterface(Ci.sbIMetadataLookupJob);
132  if (job.status != Ci.sbIJobProgress.STATUS_SUCCEEDED)
133  return;
134 
135  if (job.mlJobType != Ci.sbIMetadataLookupJob.JOB_ALBUM_DETAIL_LOOKUP)
136  return;
137 
138  function getname(props) {
139  return SBProperties.arrayToJSObject(props)[SBProperties.trackName];
140  }
141 
142  var index = this._jobResultIndexMap.indexOf(0, job);
143  var tracks = this._jobTracksMap[index];
144  // album detail calls should only ever have one result
145  var enumDetails = job.getMetadataResults();
146  var result = enumDetails.getNext().QueryInterface(Ci.sbIMetadataAlbumDetail);
147  tracks.setTrackTitles(ArrayConverter.JSArray(result.tracks).map(getname));
148  this._metadataResults[index] = result;
149  },
150 
154  onSelectionChange: function onSelectionChange()
155  {
156  // This might get called during radiogroup construction, before load event.
157  if (!this._infolist)
158  return;
159 
160  document.getElementById("artist-textbox").disabled =
161  (this._infolist.value != "other");
162  document.getElementById("album-textbox").disabled =
163  (this._infolist.value != "other");
164  },
165 
169  onaccept: function onaccept()
170  {
171  if (this._infolist.value == "other")
172  {
173  this._artistValue = document.getElementById("artist-textbox").value ||
174  SBString("cdrip.lookup.default_artistname");
175  this._albumValue = document.getElementById("album-textbox").value ||
176  SBString("cdrip.lookup.default_albumname");
177 
178  // Update the device library album name if one was specified.
179  if (document.getElementById("album-textbox").value)
180  this.library.setProperty(SBProperties.albumName, this._albumValue);
181 
182  this._setEmptyOnly = false;
183  this.library.enumerateAllItems(this);
184  return;
185  }
186 
187  var result = this._metadataResults[parseInt(this._infolist.value)];
188  var tracks = ArrayConverter.JSArray(result.tracks);
189 
190  if (tracks.length > 0) {
191  // Update the device library album name.
192  this.library.setProperty
193  (SBProperties.albumName,
194  result.properties.getPropertyValue(SBProperties.albumName));
195  }
196 
197  for (var i=0; i < tracks.length; i++) {
198  // Get the media item in this device library that has the same track
199  // number (we add one due to the tracknumber being indexed from 1)
200  var trackArr = this.library.getItemsByProperty(SBProperties.trackNumber,
201  i+1);
202  var item = trackArr.queryElementAt(0, Ci.sbIMediaItem);
203  item.setProperties(tracks[i]);
204  }
205  },
206 
210  onDeviceEvent: function onDeviceEvent(aEvent) {
211  switch(aEvent.type) {
212  case Ci.sbIDeviceEvent.EVENT_DEVICE_REMOVED :
213  // Close dialog if device is removed.
214  if (aEvent.data == this._device)
215  window.close();
216  break;
217 
218  default :
219  break;
220  }
221  },
222 
226  oncancel: function oncancel()
227  {
228  // Populate all of the tracks w/ the default entries.
229  this._artistValue = SBString("cdrip.lookup.default_artistname");
230  this._albumValue = SBString("cdrip.lookup.default_albumname");
231 
232  this._setEmptyOnly = true;
233  this.library.enumerateAllItems(this);
234  },
235 
239  onEnumerationBegin: function (aList) {},
240  onEnumeratedItem: function (aList, aItem)
241  {
242  if (!this._setEmptyOnly || !aItem.getProperty(SBProperties.albumArtistName))
243  aItem.setProperty(SBProperties.albumArtistName, this._artistValue);
244  if (!this._setEmptyOnly || !aItem.getProperty(SBProperties.artistName))
245  aItem.setProperty(SBProperties.artistName, this._artistValue);
246  if (!this._setEmptyOnly || !aItem.getProperty(SBProperties.albumName))
247  aItem.setProperty(SBProperties.albumName, this._albumValue);
248 
249  if (!aItem.getProperty(SBProperties.trackName)) {
250  // Only pad the track count to two digits since a CD can only have
251  // up to 99 tracks on it.
252  var curTrackNum = this._curTrackIndex++;
253  if (curTrackNum < 10 && this.library.length >= 10) {
254  curTrackNum = "0" + curTrackNum;
255  }
256  aItem.setProperty(SBProperties.trackName,
257  SBFormattedString("cdrip.lookup.default_trackname",
258  [curTrackNum]));
259  }
260  },
261  onEnumerationEnd: function (aList, aStatus) {}
262 }
const Cu
const Cc
window onload
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
let window
return null
Definition: FeedWriter.js:1143
const Cr
const Ci
_getSelectedPageStyle s i