Bug 1500990 - make download folder picker in Prefs work again after removal of some special folders in M-C. r=jorgk
authoraceman <acelists@atlas.sk>
Sun, 02 Dec 2018 15:55:00 +0100
changeset 33841 78ed6c7be87defefdbce82e1bd1e8dc7b59e9ef3
parent 33840 440dbc342936fc1a2eea7b63d76c6e9ae95a5cf7
child 33842 3fb7643137ce3a07b25e3ad130deb1c6bf6ee93a
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersjorgk
bugs1500990
Bug 1500990 - make download folder picker in Prefs work again after removal of some special folders in M-C. r=jorgk
mail/components/preferences/downloads.js
--- a/mail/components/preferences/downloads.js
+++ b/mail/components/preferences/downloads.js
@@ -1,116 +1,102 @@
 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+ChromeUtils.import("resource://gre/modules/Downloads.jsm");
+
 var gDownloadDirSection = {
-  chooseFolder() {
-    const nsIFilePicker = Ci.nsIFilePicker;
+  async chooseFolder() {
     var fp = Cc["@mozilla.org/filepicker;1"]
-               .createInstance(nsIFilePicker);
+               .createInstance(Ci.nsIFilePicker);
     var bundlePreferences = document.getElementById("bundlePreferences");
     var title = bundlePreferences.getString("chooseAttachmentsFolderTitle");
-    fp.init(window, title, nsIFilePicker.modeGetFolder);
+    fp.init(window, title, Ci.nsIFilePicker.modeGetFolder);
 
-    const nsIFile = Ci.nsIFile;
     var customDirPref = document.getElementById("browser.download.dir");
     if (customDirPref.value)
       fp.displayDirectory = customDirPref.value;
-    fp.appendFilters(nsIFilePicker.filterAll);
-    fp.open(rv => {
-      if (rv != nsIFilePicker.returnOK || !fp.file) {
-        return;
-      }
+    fp.appendFilters(Ci.nsIFilePicker.filterAll);
 
-      let file = fp.file.QueryInterface(nsIFile);
-      let currentDirPref = document.getElementById("browser.download.downloadDir");
-      customDirPref.value = currentDirPref.value = file;
-      let folderListPref = document.getElementById("browser.download.folderList");
-      folderListPref.value = this._fileToIndex(file);
-    });
+    let rv = await new Promise(resolve => fp.open(resolve));
+    if (rv != Ci.nsIFilePicker.returnOK || !fp.file) {
+      return;
+    }
+
+    let file = fp.file.QueryInterface(Ci.nsIFile);
+    let currentDirPref = document.getElementById("browser.download.downloadDir");
+    customDirPref.value = currentDirPref.value = file;
+    let folderListPref = document.getElementById("browser.download.folderList");
+    folderListPref.value = await this._fileToIndex(file);
   },
 
   onReadUseDownloadDir() {
     this.readDownloadDirPref();
     var downloadFolder = document.getElementById("downloadFolder");
     var chooseFolder = document.getElementById("chooseFolder");
     var preference = document.getElementById("browser.download.useDownloadDir");
     downloadFolder.disabled = !preference.value;
     chooseFolder.disabled = !preference.value;
     return undefined;
   },
 
-  _fileToIndex(aFile) {
-    if (!aFile || aFile.equals(this._getDownloadsFolder("Desktop")))
+  async _fileToIndex(aFile) {
+    if (!aFile || aFile.equals(await this._getDownloadsFolder("Desktop")))
       return 0;
-    else if (aFile.equals(this._getDownloadsFolder("Downloads")))
+    else if (aFile.equals(await this._getDownloadsFolder("Downloads")))
       return 1;
     return 2;
   },
 
-  _indexToFile(aIndex) {
+  async _indexToFile(aIndex) {
     switch (aIndex) {
     case 0:
       return this._getDownloadsFolder("Desktop");
     case 1:
       return this._getDownloadsFolder("Downloads");
     }
     var customDirPref = document.getElementById("browser.download.dir");
     return customDirPref.value;
   },
 
-  _getSpecialFolderKey(aFolderType) {
-    if (aFolderType == "Desktop")
-      return "Desk";
-
-    if (aFolderType != "Downloads")
-      throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
-
-    if (AppConstants.platform == "win")
-      return "Pers";
-
-    if (AppConstants.platform == "macosx")
-      return "UsrDocs";
-
-    return "Home";
+  async _getDownloadsFolder(aFolder) {
+    switch (aFolder) {
+      case "Desktop":
+        return Services.dirsvc.get("Desk", Ci.nsIFile);
+      case "Downloads":
+        let downloadsDir = await Downloads.getSystemDownloadsDirectory();
+        return new FileUtils.File(downloadsDir);
+    }
+    throw new Error("ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'");
   },
 
-  _getDownloadsFolder(aFolder) {
-    let dir = Services.dirsvc.get(this._getSpecialFolderKey(aFolder),
-                                  Ci.nsIFile);
-    if (aFolder != "Desktop")
-      dir.append("My Downloads");
-
-    return dir;
-  },
-
-  readDownloadDirPref() {
+  async readDownloadDirPref() {
     var folderListPref = document.getElementById("browser.download.folderList");
     var bundlePreferences = document.getElementById("bundlePreferences");
     var downloadFolder = document.getElementById("downloadFolder");
 
     var customDirPref = document.getElementById("browser.download.dir");
-    var customIndex = customDirPref.value ? this._fileToIndex(customDirPref.value) : 0;
+    var customIndex = customDirPref.value ? await this._fileToIndex(customDirPref.value) : 0;
     if (customIndex == 0)
       downloadFolder.value = bundlePreferences.getString("desktopFolderName");
     else if (customIndex == 1)
       downloadFolder.value = bundlePreferences.getString("myDownloadsFolderName");
     else
       downloadFolder.value = customDirPref.value ? customDirPref.value.path : "";
 
     var currentDirPref = document.getElementById("browser.download.downloadDir");
-    var downloadDir = currentDirPref.value || this._indexToFile(folderListPref.value);
+    var downloadDir = currentDirPref.value || await this._indexToFile(folderListPref.value);
     let urlSpec = Services.io.getProtocolHandler("file")
       .QueryInterface(Ci.nsIFileProtocolHandler)
       .getURLSpecFromFile(downloadDir);
 
     downloadFolder.style.backgroundImage = "url(moz-icon://" + urlSpec + "?size=16)";
 
     return undefined;
   },
 
-  writeFolderList() {
+  async writeFolderList() {
     var currentDirPref = document.getElementById("browser.download.downloadDir");
-    return this._fileToIndex(currentDirPref.value);
+    return await this._fileToIndex(currentDirPref.value);
   },
 };