mainPaneOverlay.js
Go to the documentation of this file.
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 PREF_DOWNLOAD_MUSIC_FOLDER = "songbird.download.music.folder";
28 const PREF_DOWNLOAD_MUSIC_ALWAYS_ASK = "songbird.download.music.alwaysPrompt";
29 const PREF_DOWNLOAD_VIDEO_FOLDER = "songbird.download.video.folder";
30 const PREF_DOWNLOAD_VIDEO_ALWAYS_ASK = "songbird.download.video.alwaysPrompt";
31 
32 // Use var, not const, so that we won't conflict with other overlays that define
33 // the same variables
34 var Cc = Components.classes;
35 var Ci = Components.interfaces;
36 var Cr = Components.results;
37 var Cu = Components.utils;
38 
39 Cu.import("resource://app/jsmodules/PlatformUtils.jsm");
40 
41 function makeFile(path) {
42  var file = Cc["@mozilla.org/file/local;1"].
43  createInstance(Ci.nsILocalFile);
44  try {
45  file.initWithPath(path);
46  }
47  catch (e) {
48  return null;
49  }
50  return file;
51 }
52 
53 function folderIsValid(folder) {
54  try {
55  return folder && folder.isDirectory();
56  }
57  catch (e) {
58  }
59  return false;
60 }
61 
66  _defaultMusicFolder: null,
67  _defaultVideoFolder: null,
68 
72  _musicPrefBox: null,
73  _videoPrefBox: null,
74 
78  _musicFolderPref: null,
79  _musicFolderAskPref: null,
80  _videoFolderPref: null,
81  _videoFolderAskPref: null,
82 
86  _strings: null,
87 
91  get musicFolder() {
92  return this._musicPrefBox.folder;
93  },
94  set musicFolder(val) {
95  this._musicPrefBox.folder = val;
96  return this.musicFolder;
97  },
98 
102  get videoFolder() {
103  return this._videoPrefBox.folder;
104  },
105 
106  set videoFolder(val) {
107  this._videoPrefBox.folder = val;
108  return this.videoFolder;
109  },
110 
114  get musicFolderAsk() {
115  return this._musicPrefBox.ask;
116  },
117  set musicFolderAsk(val) {
118  this._musicPrefBox.ask = val;
119  return this.musicFolderAsk;
120  },
121 
125  get videoFolderAsk() {
126  return this._videoPrefBox.ask;
127  },
128  set videoFolderAsk(val) {
129  this._videoPrefBox.ask = val;
130  return this.videoFolderAsk;
131  },
132 
136  _createMusicUIElements: function() {
137  // Make some preference elements.
138  const preferencesElement = document.getElementById("mainPreferences");
139 
140  this._musicFolderAskPref = document.createElement("preference");
141  this._musicFolderAskPref.setAttribute("id", PREF_DOWNLOAD_MUSIC_ALWAYS_ASK);
142  this._musicFolderAskPref.setAttribute("name",
144  this._musicFolderAskPref.setAttribute("type", "bool");
145  this._musicFolderAskPref.setAttribute(
146  "onchange",
147  "SongbirdMainPaneOverlay.onMusicAskPrefChanged();");
148  preferencesElement.appendChild(this._musicFolderAskPref);
149 
150  this._musicFolderPref = document.createElement("preference");
151  this._musicFolderPref.setAttribute("id", PREF_DOWNLOAD_MUSIC_FOLDER);
152  this._musicFolderPref.setAttribute("name", PREF_DOWNLOAD_MUSIC_FOLDER);
153  this._musicFolderPref.setAttribute("type", "unichar");
154  this._musicFolderPref.setAttribute(
155  "onchange",
156  "SongbirdMainPaneOverlay.onMusicFolderPrefChanged();");
157  preferencesElement.appendChild(this._musicFolderPref);
158 
159  // Build the music downloads box
160  this._musicPrefBox = document.createElement("sb-savefolder-box");
161  this._musicPrefBox.setAttribute("id","musicDownloadsGroup");
162  this._musicPrefBox.setAttribute(
163  "oncommand",
164  "SongbirdMainPaneOverlay.onUserChangedMusicBox()");
165 
166  // Insert it into the DOM to apply the XBL binding before trying
167  // to access its properties:
168  const mainPrefPane = document.getElementById("paneMain");
169  const downloadsGroup = document.getElementById("downloadsGroup");
170  mainPrefPane.insertBefore(this._musicPrefBox, downloadsGroup);
171 
172  // Populate the labels. Control labels are borrowed from the
173  // corresponding elements in the existing moz file downloads
174  // groupbox. Other labels are taken from the strings bundle:
175  this._musicPrefBox.title =
176  this._strings.getString("prefs.main.downloads.audio.label");
177  this._musicPrefBox.folderLabel =
178  document.getElementById("saveTo").getAttribute("label"),
179  this._musicPrefBox.browseLabel =
180  document.getElementById("chooseFolder").getAttribute("label"),
181  this._musicPrefBox.askLabel =
182  document.getElementById("alwaysAsk").getAttribute("label");
183  this._musicPrefBox.browseWindowTitle =
184  this._strings.getString("prefs.main.downloads.audio.chooseTitle");
185  },
186 
187  _createVideoUIElements: function() {
188  // Make some preference elements.
189  const preferencesElement = document.getElementById("mainPreferences");
190 
191  this._videoFolderAskPref = document.createElement("preference");
192  this._videoFolderAskPref.setAttribute("id", PREF_DOWNLOAD_VIDEO_ALWAYS_ASK);
193  this._videoFolderAskPref.setAttribute("name",
195  this._videoFolderAskPref.setAttribute("type", "bool");
196  this._videoFolderAskPref.setAttribute(
197  "onchange",
198  "SongbirdMainPaneOverlay.onVideoAskPrefChanged();");
199  preferencesElement.appendChild(this._videoFolderAskPref);
200 
201  this._videoFolderPref = document.createElement("preference");
202  this._videoFolderPref.setAttribute("id", PREF_DOWNLOAD_VIDEO_FOLDER);
203  this._videoFolderPref.setAttribute("name", PREF_DOWNLOAD_VIDEO_FOLDER);
204  this._videoFolderPref.setAttribute("type", "unichar");
205  this._videoFolderPref.setAttribute(
206  "onchange",
207  "SongbirdMainPaneOverlay.onVideoFolderPrefChanged();");
208  preferencesElement.appendChild(this._videoFolderPref);
209 
210  // Build the video downloads box
211  this._videoPrefBox = document.createElement("sb-savefolder-box");
212  this._videoPrefBox.setAttribute("id","videoDownloadsGroup");
213  this._videoPrefBox.setAttribute(
214  "oncommand",
215  "SongbirdMainPaneOverlay.onUserChangedVideoBox()");
216 
217  // Insert it into the DOM to apply the XBL binding before trying
218  // to access its properties
219  const mainPrefPane = document.getElementById("paneMain");
220  const downloadsGroup = document.getElementById("downloadsGroup");
221  mainPrefPane.insertBefore(this._videoPrefBox, downloadsGroup);
222 
223  // Populate the labels. Control labels are borrowed from the
224  // corresponding elements in the existing moz file downloads
225  // groupbox. Other labels are taken from the strings bundle:
226  this._videoPrefBox.title =
227  this._strings.getString("prefs.main.downloads.video.label");
228  this._videoPrefBox.folderLabel =
229  document.getElementById("saveTo").getAttribute("label"),
230  this._videoPrefBox.browseLabel =
231  document.getElementById("chooseFolder").getAttribute("label"),
232  this._videoPrefBox.askLabel =
233  document.getElementById("alwaysAsk").getAttribute("label");
234  this._videoPrefBox.browseWindowTitle =
235  this._strings.getString("prefs.main.downloads.video.chooseTitle");
236  },
237 
238  _createUIElements: function() {
239  var tempString;
240 
241  // Relabel the original downloads caption.
242  const downloadsCaptions = document.getElementById("downloadsGroup")
243  .getElementsByTagName("caption");
244  if (downloadsCaptions.length) {
245  tempString = this._strings.getString("prefs.main.browserdownloads.label");
246  downloadsCaptions.item(0).setAttribute("label", tempString);
247  }
248 
249  // Hide the addons button
250  // actually, we'll remove the hbox containing to the addonsGroupbox to
251  // avoid bug17280
252  var addonsGroup = document.getElementById("addonsMgrGroup");
253  addonsGroup.parentNode.parentNode.removeChild(addonsGroup.parentNode);
254 
255  this._createMusicUIElements();
256  this._createVideoUIElements();
257  },
258 
262  onPaneLoad: function(event) {
263  // Don't actually load until the main pane has been loaded (see the comments
264  // near the matching addEventListener call at the end of this code).
265  if (event.target.getAttribute("id") != "paneMain") {
266  return;
267  }
268 
269  const self = SongbirdMainPaneOverlay;
270  window.removeEventListener('paneload', self.onPaneLoad, false);
271 
272  // Save this for later.
273  var downloadHelper =
274  Cc["@songbirdnest.com/Songbird/DownloadDeviceHelper;1"].
275  getService(Ci.sbIDownloadDeviceHelper)
276  self._defaultMusicFolder =
277  downloadHelper.getDefaultDownloadFolder("audio");
278  self._defaultVideoFolder =
279  downloadHelper.getDefaultDownloadFolder("video");
280 
281  self._strings = document.getElementById("bundleSongbirdPreferences");
282 
283  // Build the UI. This requires strings to be defined.
284  self._createUIElements();
285 
286  // Sync from preferences.
287  var musicFolder = makeFile(self._musicFolderPref.value);
288  if (!folderIsValid(musicFolder)) {
289  // Reset to default.
290  musicFolder = self._defaultMusicFolder;
291  self._musicFolderPref.value = (musicFolder ? musicFolder.path : "");
292  }
293  self.musicFolder = musicFolder;
294 
295  self.musicFolderAsk = self._musicFolderAskPref.value;
296 
297  var videoFolder = makeFile(self._videoFolderPref.value);
298  if (!folderIsValid(videoFolder)) {
299  // Reset to default.
300  videoFolder = self._defaultVideoFolder;
301  self._videoFolderPref.value = (videoFolder ? videoFolder.path : "");
302  }
303  self.videoFolder = videoFolder;
304 
305  self.videoFolderAsk = self._videoFolderAskPref.value;
306  },
307 
311  onUserChangedMusicBox: function() {
312  // The user changed a pref control. Reflect the new state to
313  // the preferences.
314  const self = SongbirdMainPaneOverlay;
315  var path = (self.musicFolder ? self.musicFolder.path : "");
316  self._musicFolderPref.value = path;
317  self._musicFolderAskPref.value = self.musicFolderAsk;
318  },
319 
323  onUserChangedVideoBox: function() {
324  // The user changed a pref control. Reflect the new state to
325  // the preferences.
326  const self = SongbirdMainPaneOverlay;
327  var path = (self.videoFolder ? self.videoFolder.path : "");
328  self._videoFolderPref.value = path;
329  self._videoFolderAskPref.value = self.videoFolderAsk;
330  },
331 
335  onMusicFolderPrefChanged: function() {
336  // The folder pref changed. Load the value and use
337  // it to update the pref box UI:
338  const self = SongbirdMainPaneOverlay;
339  var newFolder = makeFile(self._musicFolderPref.value);
340  if (!folderIsValid(newFolder)) {
341  // The pref is not a valid folder. Set it to the default
342  // folder. This function will get called again if, and only
343  // if, the pref value changes here:
344  newFolder = self._defaultMusicFolder;
345  self._musicFolderPref.value = (newFolder ? newFolder.path : "");
346  }
347 
348  // Update the pref box UI with the new folder setting:
349  self.musicFolder = newFolder;
350  },
351 
355  onVideoFolderPrefChanged: function() {
356  // The folder pref changed. Load the value and use
357  // it to update the pref box UI:
358  const self = SongbirdMainPaneOverlay;
359  var newFolder = makeFile(self._videoFolderPref.value);
360  if (!folderIsValid(newFolder)) {
361  // The pref is not a valid folder. Set it to the default
362  // folder. This function will get called again if, and only
363  // if, the pref value changes here:
364  newFolder = self._defaultVideoFolder;
365  self._videoFolderPref.value = (newFolder ? newFolder.path : "");
366  }
367 
368  // Update the pref box UI with the new folder setting:
369  self.videoFolder = newFolder;
370  },
371 
375  onMusicAskPrefChanged: function() {
376  // The ask pref changed. Load the value and use
377  // it to update the pref box UI:
378  const self = SongbirdMainPaneOverlay;
379  self.musicFolderAsk = self._musicFolderAskPref.value;
380  },
381 
385  onVideoAskPrefChanged: function() {
386  // The ask pref changed. Load the value and use
387  // it to update the pref box UI:
388  const self = SongbirdMainPaneOverlay;
389  self.videoFolderAsk = self._videoFolderAskPref.value;
390  }
391 };
392 
393 // Don't forget to load! Can't use the standard load event because the
394 // individual preference panes are loaded asynchronously via
395 // document.loadOverlay (see preferences.xml) and the load event may fire before
396 // our target pane has been integrated.
397 window.addEventListener('paneload', SongbirdMainPaneOverlay.onPaneLoad, false);
var Ci
const PREF_DOWNLOAD_MUSIC_FOLDER
var event
const PREF_DOWNLOAD_VIDEO_ALWAYS_ASK
sidebarFactory createInstance
Definition: nsSidebar.js:351
getService(Ci.sbIFaceplateManager)
let window
function folderIsValid(folder)
const PREF_DOWNLOAD_VIDEO_FOLDER
var Cc
this _dialogInput val(dateText)
function makeFile(path)
return null
Definition: FeedWriter.js:1143
var SongbirdMainPaneOverlay
var Cu
const PREF_DOWNLOAD_MUSIC_ALWAYS_ASK
var file
var Cr