importLibraryPrefs.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2009 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27  */
28 
34 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 //
37 // Import library preferences UI services.
38 //
39 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 
42 //------------------------------------------------------------------------------
43 //
44 // Import library preferences UI imported services.
45 //
46 //------------------------------------------------------------------------------
47 
48 // Songbird imports.
49 Components.utils.import("resource://app/jsmodules/DOMUtils.jsm");
50 Components.utils.import("resource://app/jsmodules/SBJobUtils.jsm");
51 Components.utils.import("resource://app/jsmodules/StringUtils.jsm");
52 
53 
54 //------------------------------------------------------------------------------
55 //
56 // Import library preferences UI services defs.
57 //
58 //------------------------------------------------------------------------------
59 
60 // Component manager defs.
61 if (typeof(Cc) == "undefined")
62  var Cc = Components.classes;
63 if (typeof(Ci) == "undefined")
64  var Ci = Components.interfaces;
65 if (typeof(Cr) == "undefined")
66  var Cr = Components.results;
67 if (typeof(Cu) == "undefined")
68  var Cu = Components.utils;
69 
70 
71 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 //
74 // Import library preferences UI services.
75 //
76 // These services provide support for implementing library importer preference
77 // UI inside and outside of a preference window.
78 // Elements such as textboxes and checkboxes can be bound to preferences
79 // without using real preference elements. In these cases, the preference name
80 // and type are specified with the "prefname" and "preftype" attributes.
81 // All preference or preference bound elements should specify the appropriate
82 // preference ID from the following list using the "prefid" attribute:
83 //
84 // library_file_path_pref Library import file path preference.
85 // auto_import_pref Startup auto-import preference.
86 // dont_import_playlists_pref Don't import playlists preference.
87 // unsupported_media_alert_pref
88 // Alert user on unsupported media preference.
89 //
90 //------------------------------------------------------------------------------
91 //------------------------------------------------------------------------------
92 
93 var importLibraryPrefsUI = {
94  //
95  // Import library preferences UI services fields.
96  //
97  // _userReadableLibraryType Type of library in user readable format.
98  // _defaultLibraryFileName Default file name of library.
99  // _libraryFileExtFilter Filter of library file extensions.
100  // _prefBranch Root preference branch object.
101  //
102 
103  _userReadableLibraryType: null,
104  _defaultLibraryFileName: null,
105  _libraryFileExtFilter: null,
106  _prefBranch: null,
107 
108 
109  //----------------------------------------------------------------------------
110  //
111  // Public services.
112  //
113  //----------------------------------------------------------------------------
114 
119  initialize: function importLibraryPrefsUI_initialize() {
120  // Get the library importer and its attributes.
121  //XXXeps don't hard code.
122  this._userReadableLibraryType = "iTunes";
123  this._defaultLibraryFileName = "iTunes Music Library.xml";
124  this._libraryFileExtFilter = "*.xml";
125 
126  // Get the preferences branch.
127  this._prefBranch = Cc["@mozilla.org/preferences-service;1"]
128  .getService(Ci.nsIPrefBranch);
129 
130  // Update the UI.
131  this._update();
132  },
133 
134 
139  readPrefs: function importLibraryPrefsUI_readPrefs() {
140  // Read the preferences into all preference bound elements.
141  var prefElemList = DOMUtils.getElementsByAttribute(document, "prefname");
142  for (var i = 0; i < prefElemList.length; i++) {
143  // Get the preference bound element.
144  var prefElem = prefElemList[i];
145 
146  // Don't read into real preference elements.
147  if (prefElem.localName == "preference")
148  continue;
149 
150  // Read the preference value and set the preference bound element value.
151  var prefValue = this._readPrefValue(prefElem);
152  this._setPrefElemValue(prefElem, prefValue);
153  }
154 
155  // Update the UI.
156  this._update();
157  },
158 
159 
164  writePrefs: function importLibraryPrefsUI_writePrefs() {
165  // Write the preferences from all preference bound elements.
166  var prefElemList = DOMUtils.getElementsByAttribute(document, "prefname");
167  for (var i = 0; i < prefElemList.length; i++) {
168  // Get the preference bound element.
169  var prefElem = prefElemList[i];
170 
171  // Don't write from real preference elements.
172  if (prefElem.localName == "preference")
173  continue;
174 
175  // Write the preference value from the preference bound element.
176  var prefValue = this._getPrefElemValue(prefElem);
177  this._writePrefValue(prefElem, prefValue);
178  }
179  },
180 
181 
182  //----------------------------------------------------------------------------
183  //
184  // Event handling services.
185  //
186  //----------------------------------------------------------------------------
187 
194  doBrowseCommand: function importLibraryPrefsUI_doBrowseCommand(aEvent) {
195  // Get the currently selected import library file and its directory.
196  var importLibraryPathPrefElem = this._getPrefElem("library_file_path_pref");
197  var importLibraryFile = Cc["@mozilla.org/file/local;1"]
198  .createInstance(Ci.nsILocalFile);
199  var importLibraryDir = null;
200  try {
201  importLibraryFile.initWithPath(importLibraryPathPrefElem.value);
202  importLibraryDir = importLibraryFile.parent;
203  } catch (ex) {
204  importLibraryFile = null;
205  importLibraryDir = null;
206  }
207 
208  // Set up a file picker for browsing.
209  var filePicker = Cc["@mozilla.org/filepicker;1"]
210  .createInstance(Ci.nsIFilePicker);
211  var filePickerMsg = SBFormattedString("import_library.file_picker_msg",
212  [ this._userReadableLibraryType ]);
213  filePicker.init(window, filePickerMsg, Ci.nsIFilePicker.modeOpen);
214 
215  // Set the file picker default file name.
216  filePicker.defaultString = this._defaultLibraryFileName;
217 
218  // Set the file picker initial directory.
219  if (importLibraryDir && importLibraryDir.exists())
220  filePicker.displayDirectory = importLibraryDir;
221 
222  // Set the list of file picker file extension filters.
223  var filePickerFilterMsg =
224  SBFormattedString("import_library.file_picker_filter_msg",
225  [ this._userReadableLibraryType ]);
226  filePicker.appendFilter (filePickerFilterMsg, this._libraryFileExtFilter);
227 
228  // Show the file picker.
229  var result = filePicker.show();
230 
231  // Update the scan directory path.
232  if (result == Ci.nsIFilePicker.returnOK)
233  importLibraryPathPrefElem.value = filePicker.file.path;
234  },
235 
242  doImportCommand: function importLibraryPrefsUI_doIMportCommand(aEvent) {
243  document.getElementById("import_playlists_itunes_pref")
244  .valueFromPreferences =
245  document.getElementById("import_playlists_itunes_checkbox").checked;
246 
247  // Get the default library importer. Do nothing if none available.
248  var libraryImporterManager =
249  Cc["@songbirdnest.com/Songbird/LibraryImporterManager;1"]
250  .getService(Ci.sbILibraryImporterManager);
251  var libraryImporter = libraryImporterManager.defaultLibraryImporter;
252 
253  // Import the library as user directs.
254  var libraryFilePath = this._getPrefElem("library_file_path_pref").value;
255 
256  var job = libraryImporter.import(libraryFilePath, "songbird", false);
257 
258  // Pass a timeout of 0 so the dialog opens immediately, and don't make
259  // it modal so it won't block the dirty playlist dialog
260  SBJobUtils.showProgressDialog(job, window, 0, true);
261  },
262 
269  doOptionsChange: function libraryPrefsUI_doOptionsChange(aEvent, aElement) {
270  // Turn off all child prefs that are parent to the root impor/export
271  // checkboxes.
272  var prefid = aElement.getAttribute("id");
273  var prefValue = aElement.value;
274  if (!prefValue && prefid == "import_tracks_itunes_pref") {
275  document.getElementById("import_playlists_itunes_pref").value = false;
276  }
277  else if (!prefValue && prefid == "export_tracks_itunes_pref") {
278  document.getElementById("export_playlists_itunes_pref").value = false;
279  document.getElementById("export_smartplaylists_itunes_pref").value = false;
280  }
281 
282  // Update the UI.
283  this._update();
284  },
285 
286  //----------------------------------------------------------------------------
287  //
288  // Internal services.
289  //
290  //----------------------------------------------------------------------------
291 
296  _update: function importLibrary__update() {
297  // Disable the library import command when there is no file selected.
298  var importLibraryButton = document.getElementById
299  ("import_library_button");
300  var libraryPath = this._getPrefElem("library_file_path_pref").value;
301  var choseLibrary = (libraryPath != "");
302  importLibraryButton.setAttribute("disabled", choseLibrary ? "false" : "true");
303 
304  // Update the broadcasters
305  this._updateCheckboxBroadcaster("import_tracks_itunes");
306  this._updateCheckboxBroadcaster("export_tracks_itunes");
307  },
308 
309 
319  _updateCheckboxBroadcaster: function(aIdBaseString) {
320  var broadcasterElem =
321  document.getElementById(aIdBaseString + "_broadcaster");
322  var checkboxElem =
323  document.getElementById(aIdBaseString + "_checkbox");
324 
325  // Update the broadcaster based on the checkbox value
326  if (checkboxElem.checked) {
327  broadcasterElem.removeAttribute("disabled");
328  }
329  else {
330  broadcasterElem.setAttribute("disabled", "true");
331  }
332  },
333 
334 
343  _getPrefElem: function importLibrary__getPrefElem(aPrefID) {
344  // Get the requested element.
345  var prefElemList = DOMUtils.getElementsByAttribute(document,
346  "prefid",
347  aPrefID);
348  if (!prefElemList || (prefElemList.length == 0))
349  return null;
350 
351  return prefElemList[0];
352  },
353 
354 
363  _getPrefElemValue: function importLibrary__getPrefElemValue(aElement) {
364  // Return the checked value for checkbox preferences.
365  if (aElement.localName == "checkbox")
366  return aElement.checked;
367 
368  return aElement.value;
369  },
370 
371 
380  _setPrefElemValue: function importLibrary__setPrefElemValue(aElement,
381  aValue) {
382  // Set the checked value for checkbox preferences.
383  if (aElement.localName == "checkbox") {
384  aElement.checked = aValue;
385  return;
386  }
387 
388  aElement.value = aValue;
389  },
390 
391 
401  _readPrefValue: function importLibraryPrefsUI__readPrefValue(aElement) {
402  // Get the preference name and type.
403  var prefName = aElement.getAttribute("prefname");
404  var prefType = aElement.getAttribute("preftype");
405 
406  // Read the preference value.
407  var prefValue = null;
408  try {
409  switch (prefType) {
410  case "int" :
411  prefValue = this._prefBranch.getIntPref(prefName);
412  break;
413 
414  case "bool" :
415  prefValue = this._prefBranch.getBoolPref(prefName);
416  if (aElement.getAttribute("prefinverted") == "true")
417  prefValue = !prefValue;
418  break;
419 
420  case "string" :
421  case "unichar" :
422  prefValue = this._prefBranch.getComplexValue(prefName,
423  Ci.nsISupportsString);
424  break;
425 
426  default :
427  break;
428  }
429  } catch (ex) {}
430 
431  return prefValue;
432  },
433 
434 
444  _writePrefValue: function importLibraryPrefsUI__writePrefValue(aElement,
445  aValue) {
446  // Get the preference name and type.
447  var prefName = aElement.getAttribute("prefname");
448  var prefType = aElement.getAttribute("preftype");
449 
450  // Write the preference value.
451  switch(prefType) {
452  case "int" :
453  this._prefBranch.setIntPref(prefName, aValue);
454  break;
455 
456  case "bool" :
457  if (aElement.getAttribute("prefinverted") == "true")
458  this._prefBranch.setBoolPref(prefName, !aValue);
459  else
460  this._prefBranch.setBoolPref(prefName, aValue);
461  break;
462 
463  case "string" :
464  case "unichar" :
465  var value = Cc["@mozilla.org/supports-string;1"]
466  .createInstance(Ci.nsISupportsString);
467  value.data = aValue;
468  this._prefBranch.setComplexValue(prefName, Ci.nsISupportsString, value);
469  break;
470 
471  default :
472  break;
473  }
474  }
475 }
476 
477 
478 //------------------------------------------------------------------------------
479 //------------------------------------------------------------------------------
480 //
481 // Import library preference pane services.
482 //
483 //------------------------------------------------------------------------------
484 //------------------------------------------------------------------------------
485 
486 var importLibraryPrefsPane = {
487  //----------------------------------------------------------------------------
488  //
489  // Event handling services.
490  //
491  //----------------------------------------------------------------------------
492 
497  doPaneLoad: function importLibraryPrefs_doPaneLoad() {
498  // Initialize the import library preferences UI.
499  importLibraryPrefsUI.initialize();
500  }
501 }
502 
const Cu
function Fx prototype initialize
const Cc
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
onPageChanged aValue
Definition: FeedWriter.js:1395
systray _prefBranch
Definition: mainwin.js:150
let window
return null
Definition: FeedWriter.js:1143
countRef value
Definition: FeedWriter.js:1423
const Cr
const Ci
_getSelectedPageStyle s i