Bug 665603 - Centralise sound preferences r=mnyromyr
authorIan Neal <iann_cvs@blueyonder.co.uk>
Tue, 05 Jul 2011 00:51:46 +0100
changeset 8266 ae84f8b74c90e52908360b1fd5cc7077b3b0ad57
parent 8265 e3f147cb26aae09acd4909e62a06d1d33e6097fb
child 8267 2ffa1a8f346375e9f23d798f7707023748e8e585
push id84
push userbugzilla@standard8.plus.com
push dateTue, 16 Aug 2011 21:25:04 +0000
treeherdercomm-beta@6970c86be3cd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmnyromyr
bugs665603
Bug 665603 - Centralise sound preferences r=mnyromyr
suite/common/pref/pref-download.js
suite/common/pref/pref-download.xul
suite/common/pref/pref-popups.js
suite/common/pref/pref-popups.xul
suite/common/pref/preferences.js
suite/locales/en-US/chrome/common/pref/pref-popups.dtd
suite/mailnews/prefs/pref-notifications.js
suite/mailnews/prefs/pref-notifications.xul
--- a/suite/common/pref/pref-download.js
+++ b/suite/common/pref/pref-download.js
@@ -37,23 +37,24 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 const kDesktop = 0;
 const kDownloads = 1;
 const kUserDir = 2;
 var gFPHandler;
+var gSoundUrlPref;
 
 function Startup()
 {
   // Define globals
   gFPHandler = Services.io.getProtocolHandler("file")
                           .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
-
+  gSoundUrlPref = document.getElementById("browser.download.finished_sound_url");
   SetSoundEnabled(document.getElementById("browser.download.finished_download_sound").value);
 
   // if we don't have the alert service, hide the pref UI for using alerts to
   // notify on download completion
   // see bug #158711
   var downloadDoneNotificationAlertUI = document.getElementById("finishedNotificationAlert");
   downloadDoneNotificationAlertUI.hidden = !("@mozilla.org/alerts-service;1" in Components.classes);
 }
@@ -184,59 +185,8 @@ function IndexToFolder(aIndex)
   }
 }
 
 function SetSoundEnabled(aEnable)
 {
   EnableElementById("downloadSndURL", aEnable, false);
   document.getElementById("downloadSndPlay").disabled = !aEnable;
 }
-
-function BrowseSound()
-{
-  var pref = document.getElementById("browser.download.finished_sound_url");
-
-  const nsIFilePicker = Components.interfaces.nsIFilePicker;
-  const nsILocalFile = Components.interfaces.nsILocalFile;
-  var fp = Components.classes["@mozilla.org/filepicker;1"]
-                     .createInstance(nsIFilePicker);
-  var prefutilitiesBundle = document.getElementById("bundle_prefutilities");
-  var title = prefutilitiesBundle.getString("choosesound");
-  fp.init(window, title, nsIFilePicker.modeOpen);
-
-  if (pref.value)
-    fp.displayDirectory = gFPHandler.getFileFromURLSpec(pref.value)
-                                    .parent.QueryInterface(nsILocalFile);
-
-  var ftype = prefutilitiesBundle.getString("SoundFiles");
-  fp.appendFilter(ftype, "*.wav; *.wave");
-  fp.appendFilters(nsIFilePicker.filterAll);
-
-  if (fp.show() == nsIFilePicker.returnOK)
-    pref.value = fp.fileURL.spec;
-}
-
-function PlaySound()
-{
-  var pref = document.getElementById("browser.download.finished_sound_url");
-  var sound = Components.classes["@mozilla.org/sound;1"]
-                        .createInstance(Components.interfaces.nsISound);
-
-  if (pref.value)
-  {
-    var ioservice = Components.classes["@mozilla.org/network/io-service;1"]
-                              .getService(Components.interfaces.nsIIOService);
-    sound.play(ioservice.newURI(pref.value, null, null));
-  }
-  else
-    sound.beep();
-}
-
-function ReadSndFile(aField)
-{
-  var pref = document.getElementById("browser.download.finished_sound_url");
-  if (pref.value)
-  {
-    var file = gFPHandler.getFileFromURLSpec(pref.value);
-    aField.file = file;
-    aField.label = (/Mac/.test(navigator.platform)) ? file.leafName : file.path;
-  }
-}
--- a/suite/common/pref/pref-download.xul
+++ b/suite/common/pref/pref-download.xul
@@ -159,25 +159,25 @@
                   accesskey="&showAlert.accesskey;"/>
       </hbox>
 
       <hbox align="center" class="indent">
         <filefield id="downloadSndURL"
                    flex="1"
                    preference="browser.download.finished_sound_url"
                    preference-editable="true"
-                   onsyncfrompreference="return document.getElementById('download_pane').ReadSndFile(this);"/>
+                   onsyncfrompreference="return WriteSoundField(this, document.getElementById('download_pane').gSoundUrlPref.value);"/>
         <hbox align="center">
           <button id="downloadSndBrowse"
                   label="&browse.label;"
                   accesskey="&browse.accesskey;"
-                  oncommand="BrowseSound();">
+                  oncommand="SelectSound(gSoundUrlPref);">
             <observes element="downloadSndURL" attribute="disabled"/>
           </button>
           <button id="downloadSndPlay"
                   label="&playButton.label;"
                   accesskey="&playButton.accesskey;"
-                  oncommand="PlaySound();"/>
+                  oncommand="PlaySound(gSoundUrlPref.value, false);"/>
         </hbox>
       </hbox>
     </groupbox>
   </prefpane>
 </overlay>
--- a/suite/common/pref/pref-popups.js
+++ b/suite/common/pref/pref-popups.js
@@ -30,33 +30,21 @@
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
 
-const nsIFileProtocolHandler = Components.interfaces.nsIFileProtocolHandler;
-
 var gSoundUrlPref;
-var gSelectSound;
-
-var gIOService;
-var gFileHandler;
 
 function Startup()
 {
   gSoundUrlPref = document.getElementById("privacy.popups.sound_url");
-  gSelectSound = document.getElementById("selectSound");
-
-  gIOService = Components.classes["@mozilla.org/network/io-service;1"]
-                         .getService(Components.interfaces.nsIIOService);
-  gFileHandler = gIOService.getProtocolHandler("file")
-                           .QueryInterface(nsIFileProtocolHandler);
 
   SetLists();
 
   SetButtons();
 }
 
 function SetLists()
 {
@@ -136,50 +124,11 @@ function EnableSoundRadio(aSoundChecked)
   EnableElement(element, aSoundChecked, false);
   var pref = document.getElementById(element.getAttribute("preference"));
   EnableSoundUrl(aSoundChecked && (pref.value == kCustomSound));
 }
 
 function EnableSoundUrl(aCustomSelected)
 {
   EnableElementById("playSoundUrl", aCustomSelected, false);
-  EnableElement(gSelectSound, aCustomSelected, false);
+  EnableElementById("selectSound", aCustomSelected, false);
   EnableElementById("playSoundButton", aCustomSelected, false);
 }
-
-function ReadSoundLocation(aElement)
-{
-  aElement.value = gSoundUrlPref.value;
-  if (aElement.value)
-    aElement.file = gFileHandler.getFileFromURLSpec(aElement.value);
-}
-
-function SelectSound()
-{
-  const nsILocalFile = Components.interfaces.nsILocalFile;
-  const nsIFilePicker = Components.interfaces.nsIFilePicker;
-  var filepicker = Components.classes["@mozilla.org/filepicker;1"]
-                             .createInstance(nsIFilePicker);
-
-  filepicker.init(window,
-                  gSelectSound.getAttribute("filepickertitle"),
-                  nsIFilePicker.modeOpen);
-  if (gSoundUrlPref.value)
-    filepicker.displayDirectory = gFileHandler.getFileFromURLSpec(gSoundUrlPref.value).parent;
-
-  filepicker.appendFilter(gSelectSound.getAttribute("filepickerfilter"),
-                          "*.wav; *.wave");
-  filepicker.appendFilters(nsIFilePicker.filterAll);
-
-  if (filepicker.show() == nsIFilePicker.returnOK)
-    gSoundUrlPref.value = filepicker.fileURL.spec;
-}
-
-function PlaySound()
-{
-  var soundUrl = gSoundUrlPref.value;
-  var sound = Components.classes["@mozilla.org/sound;1"]
-                        .createInstance(Components.interfaces.nsISound);
-  if (soundUrl)
-    sound.play(gIOService.newURI(soundUrl, null, null));
-  else
-    sound.beep();
-}
--- a/suite/common/pref/pref-popups.xul
+++ b/suite/common/pref/pref-popups.xul
@@ -130,30 +130,28 @@
                  accesskey="&customSound.accesskey;"/>
         </radiogroup>
       </hbox>
       <hbox class="indent">
         <filefield id="playSoundUrl"
                    flex="1"
                    preference="privacy.popups.sound_url"
                    preference-editable="true"
-                   onsyncfrompreference="return document.getElementById('popups_pane').ReadSoundLocation(this);"
+                   onsyncfrompreference="return WriteSoundField(this, document.getElementById('popups_pane').gSoundUrlPref.value);"
                    aria-labelledby="popupCustomSound"/>
         <button id="selectSound"
                 label="&selectSound.label;"
                 accesskey="&selectSound.accesskey;"
                 preference="pref.advanced.popups.disable_button.select_sound"
-                filepickertitle="&selectSound.title;"
-                filepickerfilter="&selectSound.filter;"
-                oncommand="SelectSound();"/>
+                oncommand="SelectSound(gSoundUrlPref);"/>
         <button id="playSoundButton"
                 label="&playSoundButton.label;"
                 accesskey="&playSoundButton.accesskey;"
                 preference="pref.advanced.popups.disable_button.preview_sound"
-                oncommand="PlaySound();"/>
+                oncommand="PlaySound(gSoundUrlPref.value, false);"/>
       </hbox>
       <hbox>
         <checkbox id="displayIcon"
                   label="&displayIcon.label;"
                   accesskey="&displayIcon.accesskey;"
                   preference="privacy.popups.statusbar_icon_enabled"/>
       </hbox>
       <hbox>
--- a/suite/common/pref/preferences.js
+++ b/suite/common/pref/preferences.js
@@ -49,8 +49,56 @@ function EnableElement(aElement, aEnable
   let pref = document.getElementById(aElement.getAttribute("preference"));
   let enabled = aEnable && !pref.locked;
 
   aElement.disabled = !enabled;
 
   if (enabled && aFocus)
     aElement.focus();
 }
+
+function WriteSoundField(aField, aValue)
+{
+  var file = GetFileFromString(aValue);
+  if (file)
+  {
+    aField.file = file;
+    aField.label = (/Mac/.test(navigator.platform)) ? file.leafName : file.path;
+  }
+}
+
+function SelectSound(aSoundUrlPref)
+{
+  const nsIFilePicker = Components.interfaces.nsIFilePicker;
+  var fp = Components.classes["@mozilla.org/filepicker;1"]
+                     .createInstance(nsIFilePicker);
+  var prefutilitiesBundle = document.getElementById("bundle_prefutilities");
+  fp.init(window, prefutilitiesBundle.getString("choosesound"),
+          nsIFilePicker.modeOpen);
+
+  var file = GetFileFromString(aSoundUrlPref.value);
+  if (file && file.parent && file.parent.exists())
+    fp.displayDirectory = file.parent;
+
+  var filterExts = "*.wav; *.wave";
+  // On Mac, allow AIFF files too.
+  if (/Mac/.test(navigator.platform))
+    filterExts += "; *.aif; *.aiff";
+  fp.appendFilter(prefutilitiesBundle.getString("SoundFiles"), filterExts);
+  fp.appendFilters(nsIFilePicker.filterAll);
+
+  if (fp.show() == nsIFilePicker.returnOK)
+    aSoundUrlPref.value = fp.fileURL.spec;
+}
+
+function PlaySound(aValue, aMail)
+{
+  const nsISound = Components.interfaces.nsISound;
+  var sound = Components.classes["@mozilla.org/sound;1"]
+                        .createInstance(nsISound);
+
+  if (aValue)
+    sound.play(Services.io.newURI(aValue, null, null));
+  else if (aMail && !/Mac/.test(navigator.platform))
+    sound.playEventSound(nsISound.EVENT_NEW_MAIL_RECEIVED);
+  else
+    sound.beep();
+}
--- a/suite/locales/en-US/chrome/common/pref/pref-popups.dtd
+++ b/suite/locales/en-US/chrome/common/pref/pref-popups.dtd
@@ -15,18 +15,16 @@
 <!ENTITY systemSound.accesskey          "S">
 <!ENTITY customSound.label              "Custom sound file">
 <!ENTITY customSound.accesskey          "C">
 
 <!ENTITY selectSound.label              "Browse…">
 <!ENTITY selectSound.accesskey          "o">
 <!ENTITY playSoundButton.label          "Play">
 <!ENTITY playSoundButton.accesskey      "l">
-<!ENTITY selectSound.title              "Select Popup Block Sound">
-<!ENTITY selectSound.filter             "Sounds">
 
 <!ENTITY displayIcon.label              "Display an icon in the browser status bar">
 <!ENTITY displayIcon.accesskey          "D">
 
 <!ENTITY displayNotification.label      "Display a notification bar at the top of the content area">
 <!ENTITY displayNotification.accesskey  "N">
 
 <!ENTITY popupNote.description          "Note: Blocking all popups may prevent important features of some websites from working, such as login windows for banks and shopping websites. For details of how to allow specific websites to use popups while blocking all others, click Help. Even if blocked, websites may use other methods to show popups.">
--- a/suite/mailnews/prefs/pref-notifications.js
+++ b/suite/mailnews/prefs/pref-notifications.js
@@ -33,36 +33,30 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 // The contents of this file will be loaded into the scope of the object
 // <prefpane id="notifications_pane">!
 
-const nsIFileProtocolHandler = Components.interfaces.nsIFileProtocolHandler;
-
-var gSound = null;
 var gSoundUrlPref = null;
-var gFileHandler = null;
 
 function Startup()
 {
   // if we don't have the alert service, hide the pref UI for using alerts to notify on new mail
   // see bug #158711
   var newMailNotificationAlertUI = document.getElementById("newMailNotificationAlert");
   newMailNotificationAlertUI.hidden = !("@mozilla.org/alerts-service;1" in Components.classes);
   if (!/Mac/.test(navigator.platform))
     document.getElementById('newMailNotificationBounce').setAttribute("hidden", true);
   // show tray icon option currently available for Windows only
   var newMailNotificationTrayIconPref = document.getElementById("newMailNotificationTrayIcon");
   newMailNotificationTrayIconPref.hidden = !/^Win/.test(navigator.platform);
 
-  gFileHandler = Services.io.getProtocolHandler("file")
-                            .QueryInterface(nsIFileProtocolHandler);
   gSoundUrlPref = document.getElementById("mail.biff.play_sound.url");
 
   PlaySoundCheck(document.getElementById("mail.biff.play_sound").value);
 }
 
 function PlaySoundCheck(aPlaySound)
 {
   let playSoundType = document.getElementById("mail.biff.play_sound.type").value;
@@ -70,50 +64,8 @@ function PlaySoundCheck(aPlaySound)
   EnableElementById("newMailNotificationType", aPlaySound, false);
   EnableSoundURL(aPlaySound && (playSoundType == 1));
 }
 
 function EnableSoundURL(aEnable)
 {
   EnableElementById("mailnewsSoundFileUrl", aEnable, false);
 }
-
-function SelectSound()
-{
-  var prefBundle = document.getElementById("bundle_prefutilities");
-
-  var nsIFilePicker = Components.interfaces.nsIFilePicker;
-  var fp = Components.classes["@mozilla.org/filepicker;1"]
-                     .createInstance(nsIFilePicker);
-
-  fp.init(window, prefBundle.getString("choosesound"), nsIFilePicker.modeOpen);
-
-  if (gSoundUrlPref.value != "")
-    fp.displayDirectory = gFileHandler.getFileFromURLSpec(gSoundUrlPref.value).parent;
-
-  fp.appendFilter(prefBundle.getString("SoundFiles"), "*.wav; *.wave");
-  fp.appendFilters(nsIFilePicker.filterAll);
-
-  if (fp.show() == nsIFilePicker.returnOK)
-    gSoundUrlPref.value = fp.fileURL.spec;
-}
-
-function ReadSoundLocation(aElement)
-{
-  aElement.value = gSoundUrlPref.value;
-  if (aElement.value)
-    aElement.file = gFileHandler.getFileFromURLSpec(aElement.value);
-}
-
-function PlaySound()
-{
-  const nsISound = Components.interfaces.nsISound;
-  if (!gSound)
-    gSound = Components.classes["@mozilla.org/sound;1"]
-                       .createInstance(nsISound);
-  var soundURL = gSoundUrlPref.value;
-  if (soundURL)
-    gSound.play(Services.io.newURI(soundURL, null, null));
-  else if (/Mac/.test(navigator.platform))
-    gSound.beep();
-  else
-    gSound.playEventSound(nsISound.EVENT_NEW_MAIL_RECEIVED);
-}
--- a/suite/mailnews/prefs/pref-notifications.xul
+++ b/suite/mailnews/prefs/pref-notifications.xul
@@ -115,29 +115,29 @@
         </radiogroup>
       </hbox>
 
       <hbox align="center" class="indent">
         <filefield id="mailnewsSoundFileUrl"
                    flex="1"
                    preference="mail.biff.play_sound.url"
                    preference-editable="true"
-                   onsyncfrompreference="return document.getElementById('notifications_pane').ReadSoundLocation(this);"
+                   onsyncfrompreference="return WriteSoundField(this, document.getElementById('notifications_pane').gSoundUrlPref.value);"
                    aria-labelledby="custom"/>
         <hbox align="center">
           <button id="browse"
                   label="&browse.label;"
                   filepickertitle="&browse.title;"
                   accesskey="&browse.accesskey;"
-                  oncommand="SelectSound();">
+                  oncommand="SelectSound(gSoundUrlPref);">
             <observes element="mailnewsSoundFileUrl" attribute="disabled"/>
           </button>
           <button id="playButton"
                   label="&playButton.label;"
                   accesskey="&playButton.accesskey;"
-                  oncommand="PlaySound();">
+                  oncommand="PlaySound(gSoundUrlPref.value, true);">
             <observes element="mailnewsSoundFileUrl" attribute="disabled"/>
           </button>
         </hbox>
       </hbox>
     </groupbox>
   </prefpane>
 </overlay>