watchFolderPrefs.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 // Watch folder preferences imported services.
37 //
38 //------------------------------------------------------------------------------
39 
40 // Songbird imports.
41 Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
42 Components.utils.import("resource://app/jsmodules/SBJobUtils.jsm");
43 Components.utils.import("resource://app/jsmodules/StringUtils.jsm");
44 
45 //------------------------------------------------------------------------------
46 //
47 // Watch folder preferences services defs.
48 //
49 //------------------------------------------------------------------------------
50 
51 // Component manager defs.
52 if (typeof(Cc) == "undefined")
53  var Cc = Components.classes;
54 if (typeof(Ci) == "undefined")
55  var Ci = Components.interfaces;
56 if (typeof(Cr) == "undefined")
57  var Cr = Components.results;
58 if (typeof(Cu) == "undefined")
59  var Cu = Components.utils;
60 
61 
62 //------------------------------------------------------------------------------
63 //------------------------------------------------------------------------------
64 //
65 // Watch folder preference pane services.
66 //
67 //------------------------------------------------------------------------------
68 //------------------------------------------------------------------------------
69 
71 
72  // set to true when the folder path has changed
73  // will also be set if watch folders was toggled disabled -> enabled
74  folderPathChanged: false,
75 
76  // Used to determine if this script should assign the pref manually
77  shouldSetWFPathPref: false,
78 
79  //----------------------------------------------------------------------------
80  //
81  // Event handling services.
82  //
83  //----------------------------------------------------------------------------
84 
89  doPaneLoad: function watchFolderPrefsPane_doPaneLoad() {
90  // Instant-apply prefs (mac and linux) will notify the pref and update the
91  // watch folder component before this dialog can warn the user. If the
92  // current platform is not windows, disable instant-apply and handle the
93  // pref assignment in this script.
94  // See bug 15570.
95  try {
96  var sysInfo = Cc["@mozilla.org/system-info;1"]
97  .getService(Ci.nsIPropertyBag2);
98  if (sysInfo.getProperty("name") != "Windows_NT") {
99  this.shouldSetWFPathPref = true;
100  }
101  }
102  catch (e) {
103  }
104 
105  if (this.shouldSetWFPathPref) {
106  // Remove the preferences attribute.
107  document.getElementById("watch_folder_path_textbox")
108  .removeAttribute("preference");
109  }
110 
111  // Update the UI.
112  this._updateUIState();
113 
114  var self = this;
115  function forceCheck(event) {
116  if (event.type == "dialogcancel" &&
117  !Application.prefs.getValue("browser.preferences.instantApply", true))
118  {
119  return;
120  }
121  if (!self._checkForValidPref(false)) {
122  event.preventDefault();
123  event.stopPropagation();
124  return false;
125  }
126 
127  var watchFolderEnablePrefElem =
128  document.getElementById("watch_folder_enable_pref");
129  var watchFolderPathPrefElem =
130  document.getElementById("watch_folder_path_pref");
131  var watchFolderEnableCheckboxElem =
132  document.getElementById("watch_folder_enable_checkbox");
133  var watchFolderPathTextboxElem =
134  document.getElementById("watch_folder_path_textbox");
135 
136  // explicitly saving these prefs because they're not inside a prefpane
137  watchFolderEnablePrefElem.valueFromPreferences = watchFolderEnableCheckboxElem.checked;
138  watchFolderPathPrefElem.valueFromPreferences = watchFolderPathTextboxElem.value;
139 
140  var watchFolderEnableCheckbox =
141  document.getElementById("watch_folder_enable_checkbox");
142  var shouldPrompt = self.folderPathChanged;
143 
144  // should never prompt for watch folder rescan if it's not enabled
145  shouldPrompt &= watchFolderEnableCheckbox.checked;
146 
147  if (shouldPrompt) {
148  // offer to rescan with the _new_ watch dir, taking into account that
149  // instantApply may be off
150  var watchFolderDir = Cc["@mozilla.org/file/local;1"]
151  .createInstance(Ci.nsILocalFile);
152  var watchFolderPath =
153  watchFolderPathPrefElem.getElementValue(watchFolderPathTextboxElem);
154  watchFolderDir.initWithPath(watchFolderPath);
155  self._rescan(watchFolderDir);
156  }
157  }
158  window.addEventListener('dialogaccept', forceCheck, true);
159  window.addEventListener('dialogcancel', forceCheck, true);
160  },
161 
162 
169  doImportOptionsChange:
170  function watchFolderPrefsPane_doImportOptionsChange(aEvent) {
171  // Update the UI.
172  this._checkForValidPref(true);
173  },
174 
175 
182  doBrowseCommand: function watchFolderPrefsPane_doBrowseCommand(aEvent) {
183  // Get the currently selected watch folder directory.
184  var watchFolderPathPrefElem =
185  document.getElementById("watch_folder_path_pref");
186  var watchFolderPathTextboxElem =
187  document.getElementById("watch_folder_path_textbox");
188  var watchFolderDir = Cc["@mozilla.org/file/local;1"]
189  .createInstance(Ci.nsILocalFile);
190  try {
191  watchFolderDir.initWithPath(watchFolderPathPrefElem.value);
192  if (!watchFolderDir.exists() ||
193  !watchFolderDir.isDirectory() ||
194  watchFolderDir.isSymlink()) {
195  watchFolderDir = null;
196  }
197  } catch (ex) {
198  watchFolderDir = null;
199  }
200 
201  // Set up a file picker for browsing.
202  var filePicker = Cc["@mozilla.org/filepicker;1"]
203  .createInstance(Ci.nsIFilePicker);
204  var filePickerMsg = SBString("watch_folder.file_picker_msg");
205  filePicker.init(window, filePickerMsg, Ci.nsIFilePicker.modeGetFolder);
206 
207  // Set the file picker initial directory.
208  if (watchFolderDir)
209  filePicker.displayDirectory = watchFolderDir;
210 
211  // Show the file picker.
212  var result = filePicker.show();
213 
214  // Update the watch folder path.
215  if ((result == Ci.nsIFilePicker.returnOK) &&
216  (filePicker.file.isDirectory())) {
217  // Update the text box - the preference will be set in
218  // _checkForValidPref() if it is valid
219  watchFolderPathTextboxElem.value = filePicker.file.path;
220  this.onPathChanged(aEvent);
221  }
222 
223  this._checkForValidPref();
224  },
225 
229  onPathChanged: function watchFolderPrefsPane_onPathChanged(aEvent) {
230  this.folderPathChanged = true;
231  this._updateUIState();
232  return true;
233  },
234 
235 
236  //----------------------------------------------------------------------------
237  //
238  // Internal services.
239  //
240  //----------------------------------------------------------------------------
241 
242  _updateUIState: function()
243  {
244  this._checkManageMedia();
245  var checkbox = document.getElementById("watch_folder_enable_checkbox");
246  var enabled = checkbox.checked;
247  var broadcaster = document.getElementById("watch_folder_disabled_broadcaster");
248 
249  if (enabled) {
250  broadcaster.removeAttribute("disabled");
251  } else {
252  broadcaster.setAttribute("disabled", "true");
253  }
254 
255  this._checkForValidPref();
256  },
257 
258  _checkManageMedia: function()
259  {
260  var manageMediaValue = Application.prefs.getValue("songbird.media_management.library.enabled", false);
261  var watchFolderEnableWarning = document.getElementById("watch_folder_enable_warning");
262  if (!manageMediaValue) {
263  watchFolderEnableWarning.setAttribute("hidden", "true");
264  }
265  },
266 
267  _removeErrorNotifications: function()
268  {
269  var oldNotif, notifBox = document.getElementById("watch_folder_notification_box");
270 
271  while ((oldNotif = notifBox.getNotificationWithValue("watch_folder_error"))) {
272  notifBox.removeNotification(oldNotif);
273  }
274  },
275 
276  _showErrorNotification: function(aMsg)
277  {
278  var notifBox = document.getElementById("watch_folder_notification_box");
279 
280  // focus this pref pane and this tab
281  var pane = document.getElementById("paneImportMedia");
282  document.documentElement.showPane(pane);
283  document.getElementById("import_media_tabs").selectedItem = document.getElementById("watch_folder_tab");
284 
285  // show the notification, hiding any other ones of this class
286  this._removeErrorNotifications();
287  notifBox.appendNotification(aMsg, "watch_folder_error", null,
288  notifBox.PRIORITY_CRITICAL_LOW, []);
289  },
290 
296  _checkForValidPref: function watchFolderPrefsPane__checkForValidPref(aSilent)
297  {
298  this._removeErrorNotifications();
299 
300  var self = this;
301  function showErrorNotification(aMsg)
302  {
303  if (!aSilent) {
304  self._showErrorNotification(aMsg);
305  }
306  }
307 
308  // Set the watch folder hidden if the watch folder services are not
309  // available.
310  var watchFolderSupported = ("@songbirdnest.com/watch-folder-service;1" in Cc);
311  if (watchFolderSupported) {
312  watchFolderSupported = Cc["@songbirdnest.com/watch-folder-service;1"]
313  .getService(Ci.sbIWatchFolderService)
314  .isSupported;
315  }
316  var watchFolderHiddenElem =
317  document.getElementById("watch_folder_hidden_broadcaster");
318  if (watchFolderSupported) {
319  watchFolderHiddenElem.removeAttribute("hidden");
320  } else {
321  watchFolderHiddenElem.setAttribute("hidden", "true");
322  }
323 
324  var checkbox = document.getElementById("watch_folder_enable_checkbox");
325  var enabled = checkbox.checked;
326 
327  if (!enabled) {
328  // if it's not enabled, we allow whatever.
329  return true;
330  }
331 
332  var watchFolderPathPrefElem =
333  document.getElementById("watch_folder_path_pref");
334  var watchFolderPathTextboxElem =
335  document.getElementById("watch_folder_path_textbox");
336 
337  // only continue checking the folder path validity if the path changed
338  if (!this.folderPathChanged)
339  return true;
340 
341  var path = watchFolderPathPrefElem.getElementValue(watchFolderPathTextboxElem);
342  if (!path) {
343  // there is no pref set, but watch folder is enabled. Complain.
344  showErrorNotification(SBString("prefs.watch_folder.error.no_path"));
345  return false;
346  }
347 
348  var dir = null;
349  try {
350  dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
351  dir.initWithPath(path);
352  if (!dir.exists()) {
353  showErrorNotification(SBFormattedString("prefs.watch_folder.error.not_exist", [dir.path]));
354  return false;
355  }
356  if (!dir.isDirectory()) {
357  showErrorNotification(SBFormattedString("prefs.watch_folder.error.not_directory", [dir.path]));
358  return false;
359  }
360  if (!dir.isReadable()) {
361  showErrorNotification(SBFormattedString("prefs.watch_folder.error.not_readable", [dir.path]));
362  return false;
363  }
364  } catch (e) {
365  // failed to create the dir (e.g. invalid path?)
366  Components.utils.reportError(e);
367  showErrorNotification(SBFormattedString("prefs.watch_folder.error.generic", [path]));
368  return false;
369  }
370 
371  // The path seems legit, if this script is supposed to set the WF path pref
372  // do that now.
373  if (this.shouldSetWFPathPref) {
374  document.getElementById("watch_folder_path_pref")
375  .valueFromPreferences = path;
376  }
377 
378  return true;
379  },
380 
384  _rescan: function watchFolderPrefsPane__rescan(aFile) {
385  var shouldImportTitle =
386  SBString("watch_folder.new_folder.scan_title");
387  var shouldImportMsg =
388  SBFormattedString("watch_folder.new_folder.scan_text", [aFile.path]);
389 
390  var promptService = Cc['@mozilla.org/embedcomp/prompt-service;1']
391  .getService(Ci.nsIPromptService);
392  var promptButtons =
393  promptService.STD_YES_NO_BUTTONS + promptService.BUTTON_POS_1_DEFAULT;
394 
395  var checkState = {};
396  var result = promptService.confirmEx(window,
397  shouldImportTitle,
398  shouldImportMsg,
399  promptButtons,
400  null,
401  null,
402  null,
403  null,
404  checkState);
405  if(result == 0) {
406  var importer = Cc['@songbirdnest.com/Songbird/DirectoryImportService;1']
407  .getService(Ci.sbIDirectoryImportService);
408  var directoryArray = ArrayConverter.nsIArray([aFile]);
409 
410  var job = importer.import(directoryArray);
411  SBJobUtils.showProgressDialog(job, window, 0);
412  }
413  }
414 };
415 
const Cu
const Cc
var Application
Definition: sbAboutDRM.js:37
function SBFormattedString(aKey, aParams, aDefault, aStringBundle)
var event
function SBString(aKey, aDefault, aStringBundle)
Definition: StringUtils.jsm:93
let window
var watchFolderPrefsPane
return null
Definition: FeedWriter.js:1143
const Cr
let promptService
const Ci