Bug 875648 - Use Downloads.jsm functions to get download directories in Firefox for Desktop. r=jaws

This commit is contained in:
Paolo Amadini 2013-11-01 18:24:00 +00:00
Родитель d4c5c8ca0b
Коммит 468dfdbf31
4 изменённых файлов: 102 добавлений и 153 удалений

Просмотреть файл

@ -2,8 +2,9 @@
* 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/. */
XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
"resource:///modules/DownloadsCommon.jsm");
Components.utils.import("resource://gre/modules/Downloads.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
var gMainPane = {
/**
@ -455,34 +456,19 @@ var gMainPane = {
* downloads are automatically saved, updating preferences and UI in
* response to the choice, if one is made.
*/
chooseFolder: function ()
chooseFolder() this.chooseFolderTask().catch(Components.utils.reportError),
chooseFolderTask: Task.async(function* ()
{
const nsIFilePicker = Components.interfaces.nsIFilePicker;
const nsILocalFile = Components.interfaces.nsILocalFile;
let bundlePreferences = document.getElementById("bundlePreferences");
let title = bundlePreferences.getString("chooseDownloadFolderTitle");
let folderListPref = document.getElementById("browser.download.folderList");
let currentDirPref = this._indexToFolder(folderListPref.value); // file
let defDownloads = this._indexToFolder(1); // file
let currentDirPref = yield this._indexToFolder(folderListPref.value);
let defDownloads = yield this._indexToFolder(1);
let fp = Components.classes["@mozilla.org/filepicker;1"].
createInstance(nsIFilePicker);
let fpCallback = function fpCallback_done(aResult) {
if (aResult == nsIFilePicker.returnOK) {
let file = fp.file.QueryInterface(nsILocalFile);
let downloadDirPref = document.getElementById("browser.download.dir");
createInstance(Components.interfaces.nsIFilePicker);
downloadDirPref.value = file;
folderListPref.value = this._folderToIndex(file);
// Note, the real prefs will not be updated yet, so dnld manager's
// userDownloadsDirectory may not return the right folder after
// this code executes. displayDownloadDirPref will be called on
// the assignment above to update the UI.
}
}.bind(this);
fp.init(window, title, nsIFilePicker.modeGetFolder);
fp.appendFilters(nsIFilePicker.filterAll);
fp.init(window, title, Components.interfaces.nsIFilePicker.modeGetFolder);
fp.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
// First try to open what's currently configured
if (currentDirPref && currentDirPref.exists()) {
fp.displayDirectory = currentDirPref;
@ -491,16 +477,36 @@ var gMainPane = {
fp.displayDirectory = defDownloads;
} // Fall back to Desktop
else {
fp.displayDirectory = this._indexToFolder(0);
fp.displayDirectory = yield this._indexToFolder(0);
}
fp.open(fpCallback);
},
let result = yield new Promise(resolve => fp.open(resolve));
if (result != Components.interfaces.nsIFilePicker.returnOK) {
return;
}
let downloadDirPref = document.getElementById("browser.download.dir");
downloadDirPref.value = fp.file;
folderListPref.value = yield this._folderToIndex(fp.file);
// Note, the real prefs will not be updated yet, so dnld manager's
// userDownloadsDirectory may not return the right folder after
// this code executes. displayDownloadDirPref will be called on
// the assignment above to update the UI.
}),
/**
* Initializes the download folder display settings based on the user's
* preferences.
*/
displayDownloadDirPref: function ()
displayDownloadDirPref()
{
this.displayDownloadDirPrefTask().catch(Components.utils.reportError);
// don't override the preference's value in UI
return undefined;
},
displayDownloadDirPrefTask: Task.async(function* ()
{
var folderListPref = document.getElementById("browser.download.folderList");
var bundlePreferences = document.getElementById("bundlePreferences");
@ -531,17 +537,14 @@ var gMainPane = {
// platforms and versions that don't support a default system downloads
// folder. See nsDownloadManager for details.
downloadFolder.label = bundlePreferences.getString("downloadsFolderName");
iconUrlSpec = fph.getURLSpecFromFile(this._indexToFolder(1));
iconUrlSpec = fph.getURLSpecFromFile(yield this._indexToFolder(1));
} else {
// 'Desktop'
downloadFolder.label = bundlePreferences.getString("desktopFolderName");
iconUrlSpec = fph.getURLSpecFromFile(desk);
iconUrlSpec = fph.getURLSpecFromFile(yield this._getDownloadsFolder("Desktop"));
}
downloadFolder.image = "moz-icon://" + iconUrlSpec + "?size=16";
// don't override the preference's value in UI
return undefined;
},
}),
/**
* Returns the textual path of a folder in readable form.
@ -562,22 +565,19 @@ var gMainPane = {
*
* @throws if aFolder is not "Desktop" or "Downloads"
*/
_getDownloadsFolder: function (aFolder)
_getDownloadsFolder: Task.async(function* (aFolder)
{
switch (aFolder) {
case "Desktop":
var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties);
return fileLoc.get("Desk", Components.interfaces.nsILocalFile);
break;
case "Downloads":
var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
.getService(Components.interfaces.nsIDownloadManager);
return dnldMgr.defaultDownloadsDirectory;
break;
let downloadsDir = yield Downloads.getSystemDownloadsDirectory();
return new FileUtils.File(downloadsDir);
}
throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
},
}),
/**
* Determines the type of the given folder.
@ -589,14 +589,14 @@ var gMainPane = {
* 1 if aFolder is the Downloads folder,
* 2 otherwise
*/
_folderToIndex: function (aFolder)
_folderToIndex: Task.async(function* (aFolder)
{
if (!aFolder || aFolder.equals(this._getDownloadsFolder("Desktop")))
if (!aFolder || aFolder.equals(yield this._getDownloadsFolder("Desktop")))
return 0;
else if (aFolder.equals(this._getDownloadsFolder("Downloads")))
else if (aFolder.equals(yield this._getDownloadsFolder("Downloads")))
return 1;
return 2;
},
}),
/**
* Converts an integer into the corresponding folder.
@ -607,40 +607,17 @@ var gMainPane = {
* the Downloads folder if aIndex == 1,
* the folder stored in browser.download.dir
*/
_indexToFolder: function (aIndex)
_indexToFolder: Task.async(function* (aIndex)
{
switch (aIndex) {
case 0:
return this._getDownloadsFolder("Desktop");
return yield this._getDownloadsFolder("Desktop");
case 1:
return this._getDownloadsFolder("Downloads");
return yield this._getDownloadsFolder("Downloads");
}
var currentDirPref = document.getElementById("browser.download.dir");
return currentDirPref.value;
},
/**
* Returns the value for the browser.download.folderList preference.
*/
getFolderListPref: function ()
{
var folderListPref = document.getElementById("browser.download.folderList");
switch (folderListPref.value) {
case 0: // Desktop
case 1: // Downloads
return folderListPref.value;
break;
case 2: // Custom
var currentDirPref = document.getElementById("browser.download.dir");
if (currentDirPref.value) {
// Resolve to a known location if possible. We are writing out
// to prefs on this call, so now would be a good time to do it.
return this._folderToIndex(currentDirPref.value);
}
return 0;
break;
}
},
}),
/**
* Hide/show the "Show my windows and tabs from last time" option based

Просмотреть файл

@ -223,8 +223,7 @@
preference="browser.download.folderList"
preference-editable="true"
aria-labelledby="saveTo"
onsyncfrompreference="return gMainPane.displayDownloadDirPref();"
onsynctopreference="return gMainPane.getFolderListPref()"/>
onsyncfrompreference="return gMainPane.displayDownloadDirPref();"/>
<button id="chooseFolder"
#ifdef XP_MACOSX
accesskey="&chooseFolderMac.accesskey;"
@ -233,8 +232,7 @@
accesskey="&chooseFolderWin.accesskey;"
label="&chooseFolderWin.label;"
#endif
preference="browser.download.folderList"
onsynctopreference="return gMainPane.getFolderListPref();"/>
/>
</hbox>
<hbox>
<radio id="alwaysAsk"

Просмотреть файл

@ -3,9 +3,9 @@
* 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/. */
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
"resource:///modules/DownloadsCommon.jsm");
Components.utils.import("resource://gre/modules/Downloads.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");
var gMainPane = {
_pane: null,
@ -332,34 +332,19 @@ var gMainPane = {
* downloads are automatically saved, updating preferences and UI in
* response to the choice, if one is made.
*/
chooseFolder: function ()
chooseFolder() this.chooseFolderTask().catch(Components.utils.reportError),
chooseFolderTask: Task.async(function* ()
{
const nsIFilePicker = Components.interfaces.nsIFilePicker;
const nsILocalFile = Components.interfaces.nsILocalFile;
let bundlePreferences = document.getElementById("bundlePreferences");
let title = bundlePreferences.getString("chooseDownloadFolderTitle");
let folderListPref = document.getElementById("browser.download.folderList");
let currentDirPref = this._indexToFolder(folderListPref.value); // file
let defDownloads = this._indexToFolder(1); // file
let currentDirPref = yield this._indexToFolder(folderListPref.value);
let defDownloads = yield this._indexToFolder(1);
let fp = Components.classes["@mozilla.org/filepicker;1"].
createInstance(nsIFilePicker);
let fpCallback = function fpCallback_done(aResult) {
if (aResult == nsIFilePicker.returnOK) {
let file = fp.file.QueryInterface(nsILocalFile);
let downloadDirPref = document.getElementById("browser.download.dir");
createInstance(Components.interfaces.nsIFilePicker);
downloadDirPref.value = file;
folderListPref.value = this._folderToIndex(file);
// Note, the real prefs will not be updated yet, so dnld manager's
// userDownloadsDirectory may not return the right folder after
// this code executes. displayDownloadDirPref will be called on
// the assignment above to update the UI.
}
}.bind(this);
fp.init(window, title, nsIFilePicker.modeGetFolder);
fp.appendFilters(nsIFilePicker.filterAll);
fp.init(window, title, Components.interfaces.nsIFilePicker.modeGetFolder);
fp.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
// First try to open what's currently configured
if (currentDirPref && currentDirPref.exists()) {
fp.displayDirectory = currentDirPref;
@ -368,16 +353,36 @@ var gMainPane = {
fp.displayDirectory = defDownloads;
} // Fall back to Desktop
else {
fp.displayDirectory = this._indexToFolder(0);
fp.displayDirectory = yield this._indexToFolder(0);
}
fp.open(fpCallback);
},
let result = yield new Promise(resolve => fp.open(resolve));
if (result != Components.interfaces.nsIFilePicker.returnOK) {
return;
}
let downloadDirPref = document.getElementById("browser.download.dir");
downloadDirPref.value = fp.file;
folderListPref.value = yield this._folderToIndex(fp.file);
// Note, the real prefs will not be updated yet, so dnld manager's
// userDownloadsDirectory may not return the right folder after
// this code executes. displayDownloadDirPref will be called on
// the assignment above to update the UI.
}),
/**
* Initializes the download folder display settings based on the user's
* preferences.
*/
displayDownloadDirPref: function ()
displayDownloadDirPref()
{
this.displayDownloadDirPrefTask().catch(Components.utils.reportError);
// don't override the preference's value in UI
return undefined;
},
displayDownloadDirPrefTask: Task.async(function* ()
{
var folderListPref = document.getElementById("browser.download.folderList");
var bundlePreferences = document.getElementById("bundlePreferences");
@ -408,17 +413,14 @@ var gMainPane = {
// platforms and versions that don't support a default system downloads
// folder. See nsDownloadManager for details.
downloadFolder.label = bundlePreferences.getString("downloadsFolderName");
iconUrlSpec = fph.getURLSpecFromFile(this._indexToFolder(1));
iconUrlSpec = fph.getURLSpecFromFile(yield this._indexToFolder(1));
} else {
// 'Desktop'
downloadFolder.label = bundlePreferences.getString("desktopFolderName");
iconUrlSpec = fph.getURLSpecFromFile(this._getDownloadsFolder("Desktop"));
iconUrlSpec = fph.getURLSpecFromFile(yield this._getDownloadsFolder("Desktop"));
}
downloadFolder.image = "moz-icon://" + iconUrlSpec + "?size=16";
// don't override the preference's value in UI
return undefined;
},
}),
/**
* Returns the textual path of a folder in readable form.
@ -439,22 +441,19 @@ var gMainPane = {
*
* @throws if aFolder is not "Desktop" or "Downloads"
*/
_getDownloadsFolder: function (aFolder)
_getDownloadsFolder: Task.async(function* (aFolder)
{
switch (aFolder) {
case "Desktop":
var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties);
return fileLoc.get("Desk", Components.interfaces.nsILocalFile);
break;
case "Downloads":
var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
.getService(Components.interfaces.nsIDownloadManager);
return dnldMgr.defaultDownloadsDirectory;
break;
let downloadsDir = yield Downloads.getSystemDownloadsDirectory();
return new FileUtils.File(downloadsDir);
}
throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
},
}),
/**
* Determines the type of the given folder.
@ -466,14 +465,14 @@ var gMainPane = {
* 1 if aFolder is the Downloads folder,
* 2 otherwise
*/
_folderToIndex: function (aFolder)
_folderToIndex: Task.async(function* (aFolder)
{
if (!aFolder || aFolder.equals(this._getDownloadsFolder("Desktop")))
if (!aFolder || aFolder.equals(yield this._getDownloadsFolder("Desktop")))
return 0;
else if (aFolder.equals(this._getDownloadsFolder("Downloads")))
else if (aFolder.equals(yield this._getDownloadsFolder("Downloads")))
return 1;
return 2;
},
}),
/**
* Converts an integer into the corresponding folder.
@ -484,40 +483,17 @@ var gMainPane = {
* the Downloads folder if aIndex == 1,
* the folder stored in browser.download.dir
*/
_indexToFolder: function (aIndex)
_indexToFolder: Task.async(function* (aIndex)
{
switch (aIndex) {
case 0:
return this._getDownloadsFolder("Desktop");
return yield this._getDownloadsFolder("Desktop");
case 1:
return this._getDownloadsFolder("Downloads");
return yield this._getDownloadsFolder("Downloads");
}
var currentDirPref = document.getElementById("browser.download.dir");
return currentDirPref.value;
},
/**
* Returns the value for the browser.download.folderList preference.
*/
getFolderListPref: function ()
{
var folderListPref = document.getElementById("browser.download.folderList");
switch (folderListPref.value) {
case 0: // Desktop
case 1: // Downloads
return folderListPref.value;
break;
case 2: // Custom
var currentDirPref = document.getElementById("browser.download.dir");
if (currentDirPref.value) {
// Resolve to a known location if possible. We are writing out
// to prefs on this call, so now would be a good time to do it.
return this._folderToIndex(currentDirPref.value);
}
return 0;
break;
}
},
}),
/**
* Hide/show the "Show my windows and tabs from last time" option based

Просмотреть файл

@ -164,8 +164,7 @@
preference="browser.download.folderList"
preference-editable="true"
aria-labelledby="saveTo"
onsyncfrompreference="return gMainPane.displayDownloadDirPref();"
onsynctopreference="return gMainPane.getFolderListPref()"/>
onsyncfrompreference="return gMainPane.displayDownloadDirPref();"/>
<button id="chooseFolder" oncommand="gMainPane.chooseFolder();"
#ifdef XP_MACOSX
accesskey="&chooseFolderMac.accesskey;"
@ -174,8 +173,7 @@
accesskey="&chooseFolderWin.accesskey;"
label="&chooseFolderWin.label;"
#endif
preference="browser.download.folderList"
onsynctopreference="return gMainPane.getFolderListPref();"/>
/>
</hbox>
<radio id="alwaysAsk" value="false"
label="&alwaysAsk.label;"