firstRunImportMedia.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-2008 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 
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 //
38 // First-run wizard import media widget services.
39 //
40 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 
43 //------------------------------------------------------------------------------
44 //
45 // First-run wizard import media widget imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 // Songbird imports.
50 Components.utils.import("resource://app/jsmodules/DOMUtils.jsm");
51 Components.utils.import("resource://app/jsmodules/FolderUtils.jsm");
52 
53 
54 //------------------------------------------------------------------------------
55 //
56 // First-run wizard import media widget 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 // First-run wizard import media widget services.
74 //
75 //------------------------------------------------------------------------------
76 
84 function firstRunImportMediaSvc(aWidget) {
85  this._widget = aWidget;
86 }
87 
88 // Define the object.
90  // Set the constructor.
92 
93  //
94  // Widget services fields.
95  //
96  // _widget First-run wizard import media widget.
97  // _watchFolderAvailable True if watch folder services are available.
98  //
99 
100  _widget: null,
101  _watchFolderAvailable: false,
102 
103 
104  //----------------------------------------------------------------------------
105  //
106  // Widget services.
107  //
108  //----------------------------------------------------------------------------
109 
114  initialize: function firstRunImportMediaSvc_initialize() {
115  // Select the default scan directory.
116  this._selectDefaultScanDirectory();
117 
118  // Determine whether the watch folder services are available.
119  var watchFolderService = Cc["@songbirdnest.com/watch-folder-service;1"]
120  .getService(Ci.sbIWatchFolderService);
121  this._watchFolderAvailable = watchFolderService.isSupported;
122 
123  // Update the UI.
124  this._update();
125  },
126 
127 
132  finalize: function firstRunImportMediaSvc_finalize() {
133  // Clear object fields.
134  this._widget = null;
135  },
136 
137 
142  saveSettings: function firstRunImportMediaSvc_saveSettings() {
143  // Dispatch processing of the import settings radio group.
144  var importRadioGroupElem = this._getElement("import_radiogroup");
145  var metricsImportType;
146  switch (importRadioGroupElem.value) {
147  case "scan_directories" :
148  this._saveScanDirectoriesSettings();
149  metricsImportType = "filescan";
150  break;
151 
152  default :
153  metricsImportType = "none";
154  break;
155  }
156 
157 #ifdef METRICS_ENABLED
158  var metrics = Cc["@songbirdnest.com/Songbird/Metrics;1"]
159  .createInstance(Ci.sbIMetrics);
160  metrics.metricsInc("firstrun", "mediaimport", metricsImportType);
161 #endif
162  },
163 
164  //----------------------------------------------------------------------------
165  //
166  // Widget event handler services.
167  //
168  //----------------------------------------------------------------------------
169 
176  doCommand: function firstRunImportMediaSvc_doCommand(aEvent) {
177  // Dispatch processing of command.
178  var action = aEvent.target.getAttribute("action");
179  switch (action) {
180  case "browse_scan_directory" :
181  this._doBrowseScanDirectory();
182  break;
183 
184  default :
185  break;
186  }
187 
188  // Update the UI.
189  this._update();
190  },
191 
192 
197  _doBrowseScanDirectory: function
198  firstRunImportMediaSvc__browseScanDirectory() {
199  // Get the currently selected scan directory.
200  var scanDirectoryTextBox = this._getElement("scan_directory_textbox");
201  var scanPath = scanDirectoryTextBox.value;
202  var scanDir = null;
203  if (scanPath) {
204  scanDir = Cc["@mozilla.org/file/local;1"]
205  .createInstance(Ci.nsILocalFile);
206  scanDir.initWithPath(scanPath);
207  }
208 
209  // Set up a file picker for browsing.
210  var filePicker = Cc["@mozilla.org/filepicker;1"]
211  .createInstance(Ci.nsIFilePicker);
212  filePicker.init(window,
213  SBString("first_run.media_scan_browser.title"),
214  Ci.nsIFilePicker.modeGetFolder);
215  if (scanDir && scanDir.exists())
216  filePicker.displayDirectory = scanDir;
217 
218  // Show the file picker.
219  var result = filePicker.show();
220 
221  // Update the scan directory path.
222  if (result == Ci.nsIFilePicker.returnOK)
223  scanDirectoryTextBox.value = filePicker.file.path;
224  },
225 
226 
227  //----------------------------------------------------------------------------
228  //
229  // Internal widget services.
230  //
231  //----------------------------------------------------------------------------
232 
237  _update: function firstRunImportMediaSvc__update() {
238  // Get the import radio group element.
239  var importRadioGroupElem = this._getElement("import_radiogroup");
240 
241  // Check which import options should be enabled.
242  this._checkImportEnabled();
243 
244  // Select the default radio if none selected.
245  if (importRadioGroupElem.selectedIndex < 0)
246  this._selectDefaultImport();
247 
248  // Get the import radio group element and the selected radio.
249  var selectedRadioElem = importRadioGroupElem.selectedItem;
250  var selectedRadioElemID = selectedRadioElem.getAttribute("anonid");
251 
252  // Get all radio group child elements with a "parentradio" attribute.
253  var radioChildList = DOMUtils.getElementsByAttribute(importRadioGroupElem,
254  "parentradio");
255 
256  // Disable all elements that are children of unselected radio elements and
257  // enable all other radio children.
258  for (var i = 0; i < radioChildList.length; i++) {
259  // If child's radio is selected, enable the child; otherwise, disable it.
260  var child = radioChildList[i];
261  if (child.getAttribute("parentradio") == selectedRadioElemID)
262  child.removeAttribute("disabled");
263  else
264  child.setAttribute("disabled", "true");
265  }
266  },
267 
268 
273  _checkImportEnabled: function firstRunImportMediaSvc__checkImportEnabled() {
274  // If the watch folder services are not available, hide the watch folder
275  // options.
276  if (!this._watchFolderAvailable) {
277  var scanDirectoriesWatchCheckBox =
278  this._getElement("scan_directories_watch_checkbox");
279  scanDirectoriesWatchCheckBox.hidden = true;
280  }
281  },
282 
283 
288  _saveScanDirectoriesSettings:
289  function firstRunImportMediaSvc__savScanDirectoriesSettings() {
290  // Get the scan directories settings.
291  var scanDirectoryTextBox = this._getElement("scan_directory_textbox");
292  var scanDirectoryPath = scanDirectoryTextBox.value;
293 
294  // Save the scan directories settings.
295  Application.prefs.setValue("songbird.firstrun.scan_directory_path",
296  scanDirectoryPath);
297  Application.prefs.setValue("songbird.firstrun.do_scan_directory", true);
298 
299  // If the watch folder services are available, save the watch folder
300  // settings.
301  if (this._watchFolderAvailable) {
302  // Get the watch folder settings.
303  var scanDirectoriesWatchCheckBox =
304  this._getElement("scan_directories_watch_checkbox");
305  var enableWatchFolder = scanDirectoriesWatchCheckBox.checked;
306 
307  // Save the watch folder settings.
308  Application.prefs.setValue("songbird.watch_folder.enable",
309  enableWatchFolder);
310  if (enableWatchFolder) {
311  Application.prefs.setValue("songbird.watch_folder.path",
312  scanDirectoryPath);
313  }
314  }
315  },
316 
317 
322  _selectDefaultImport: function firstRunImportMediaSvc__selectDefaultImport() {
323  // Get the import radio group element.
324  var importRadioGroupElem = this._getElement("import_radiogroup");
325 
326  // Select the scan directories import option.
327  var scanDirectoriesElem = this._getElement("scan_directories_radio");
328  importRadioGroupElem.selectedItem = scanDirectoriesElem;
329  },
330 
331 
336  _selectDefaultScanDirectory:
337  function firstRunImportMediaSvc__selectDefaultScanDirectory() {
338  // Set the scan directory textbox value.
339  var scanDirectoryTextBox = this._getElement("scan_directory_textbox");
340  scanDirectoryTextBox.value = FolderUtils.musicFolder.path;
341  },
342 
343 
353  _getElement: function firstRunImportMediaSvc__getElement(aElementID) {
354  return document.getAnonymousElementByAttribute(this._widget,
355  "anonid",
356  aElementID);
357  }
358 };
359 
const Cu
function Fx prototype initialize
const Cc
var Application
Definition: sbAboutDRM.js:37
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
let window
DataRemote prototype constructor
function firstRunImportMediaSvc(aWidget)
return null
Definition: FeedWriter.js:1143
const Cr
const Ci
_getSelectedPageStyle s i