2009-05-07 08:44:22 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is HelperApp Launcher Dialog.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Mark Finkle <mfinkle@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* 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 Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
2010-07-13 18:36:09 +04:00
|
|
|
const Cu = Components.utils;
|
2010-09-09 08:30:01 +04:00
|
|
|
const Cr = Components.results;
|
2009-05-07 08:44:22 +04:00
|
|
|
|
|
|
|
const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
|
2010-09-10 23:25:09 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alertdownloads";
|
|
|
|
#else
|
|
|
|
const URI_GENERIC_ICON_DOWNLOAD = "chrome://browser/skin/images/alert-downloads-30.png";
|
|
|
|
#endif
|
2009-05-07 08:44:22 +04:00
|
|
|
|
2010-07-13 18:36:09 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-08-10 22:43:20 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2009-05-07 08:44:22 +04:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// HelperApp Launcher Dialog
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
function HelperAppLauncherDialog() { }
|
|
|
|
|
|
|
|
HelperAppLauncherDialog.prototype = {
|
|
|
|
classID: Components.ID("{e9d277a0-268a-4ec2-bb8c-10fdf3e44611}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]),
|
|
|
|
|
|
|
|
show: function hald_show(aLauncher, aContext, aReason) {
|
2010-06-07 23:46:54 +04:00
|
|
|
// Check to see if we can open this file or not
|
|
|
|
if (aLauncher.MIMEInfo.hasDefaultHandler) {
|
2010-09-01 09:54:38 +04:00
|
|
|
aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useSystemDefault;
|
|
|
|
aLauncher.launchWithApplication(null, false);
|
2010-06-07 23:46:54 +04:00
|
|
|
} else {
|
2010-09-10 23:25:09 +04:00
|
|
|
let wasClicked = false;
|
|
|
|
let listener = {
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
if (aTopic == "alertclickcallback") {
|
|
|
|
wasClicked = true;
|
|
|
|
let win = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser");
|
|
|
|
if (win)
|
|
|
|
win.BrowserUI.showPanel("downloads-container");
|
|
|
|
|
|
|
|
aLauncher.saveToDisk(null, false);
|
|
|
|
} else {
|
|
|
|
if (!wasClicked)
|
|
|
|
aLauncher.cancel(Cr.NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._notify(aLauncher, listener);
|
2010-06-07 23:46:54 +04:00
|
|
|
}
|
2009-05-07 08:44:22 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) {
|
|
|
|
let file = null;
|
2010-07-13 18:36:09 +04:00
|
|
|
let prefs = Services.prefs;
|
2009-05-07 08:44:22 +04:00
|
|
|
|
|
|
|
if (!aForcePrompt) {
|
|
|
|
// Check to see if the user wishes to auto save to the default download
|
|
|
|
// folder without prompting. Note that preference might not be set.
|
|
|
|
let autodownload = true;
|
|
|
|
try {
|
|
|
|
autodownload = prefs.getBoolPref(PREF_BD_USEDOWNLOADDIR);
|
|
|
|
} catch (e) { }
|
|
|
|
|
|
|
|
if (autodownload) {
|
|
|
|
// Retrieve the user's default download directory
|
|
|
|
let dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
|
|
|
|
let defaultFolder = dnldMgr.userDownloadsDirectory;
|
|
|
|
|
|
|
|
try {
|
|
|
|
file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to make sure we have a valid directory, otherwise, prompt
|
|
|
|
if (file)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use file picker to show dialog.
|
2010-09-09 08:30:01 +04:00
|
|
|
let picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
|
2009-05-07 08:44:22 +04:00
|
|
|
let windowTitle = "";
|
2011-07-15 14:31:34 +04:00
|
|
|
let parent = aContext.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
|
2009-05-07 08:44:22 +04:00
|
|
|
picker.init(parent, windowTitle, Ci.nsIFilePicker.modeSave);
|
|
|
|
picker.defaultString = aDefaultFile;
|
|
|
|
|
|
|
|
if (aSuggestedFileExt) {
|
|
|
|
// aSuggestedFileExtension includes the period, so strip it
|
|
|
|
picker.defaultExtension = aSuggestedFileExt.substring(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
2009-11-30 18:40:06 +03:00
|
|
|
picker.defaultExtension = aLauncher.MIMEInfo.primaryExtension;
|
2009-05-07 08:44:22 +04:00
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:40:06 +03:00
|
|
|
var wildCardExtension = "*";
|
2009-05-07 08:44:22 +04:00
|
|
|
if (aSuggestedFileExt) {
|
|
|
|
wildCardExtension += aSuggestedFileExt;
|
2009-11-30 18:40:06 +03:00
|
|
|
picker.appendFilter(aLauncher.MIMEInfo.description, wildCardExtension);
|
2009-05-07 08:44:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
picker.appendFilters(Ci.nsIFilePicker.filterAll);
|
|
|
|
|
|
|
|
// Default to lastDir if it is valid, otherwise use the user's default
|
|
|
|
// downloads directory. userDownloadsDirectory should always return a
|
|
|
|
// valid directory, so we can safely default to it.
|
|
|
|
var dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
|
|
|
|
picker.displayDirectory = dnldMgr.userDownloadsDirectory;
|
|
|
|
|
|
|
|
// The last directory preference may not exist, which will throw.
|
|
|
|
try {
|
|
|
|
let lastDir = prefs.getComplexValue("browser.download.lastDir", Ci.nsILocalFile);
|
|
|
|
if (isUsableDirectory(lastDir))
|
|
|
|
picker.displayDirectory = lastDir;
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
|
|
|
|
if (picker.show() == Ci.nsIFilePicker.returnCancel) {
|
|
|
|
// null result means user cancelled.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be sure to save the directory the user chose through the Save As...
|
|
|
|
// dialog as the new browser.download.dir since the old one
|
|
|
|
// didn't exist.
|
|
|
|
file = picker.file;
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
try {
|
|
|
|
// Remove the file so that it's not there when we ensure non-existence later;
|
|
|
|
// this is safe because for the file to exist, the user would have had to
|
|
|
|
// confirm that he wanted the file overwritten.
|
|
|
|
if (file.exists())
|
|
|
|
file.remove(false);
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
var newDir = file.parent.QueryInterface(Ci.nsILocalFile);
|
|
|
|
prefs.setComplexValue("browser.download.lastDir", Ci.nsILocalFile, newDir);
|
|
|
|
file = this.validateLeafName(newDir, file.leafName, null);
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
},
|
|
|
|
|
|
|
|
validateLeafName: function hald_validateLeafName(aLocalFile, aLeafName, aFileExt) {
|
|
|
|
if (!(aLocalFile && this.isUsableDirectory(aLocalFile)))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// Remove any leading periods, since we don't want to save hidden files
|
|
|
|
// automatically.
|
|
|
|
aLeafName = aLeafName.replace(/^\.+/, "");
|
|
|
|
|
|
|
|
if (aLeafName == "")
|
|
|
|
aLeafName = "unnamed" + (aFileExt ? "." + aFileExt : "");
|
|
|
|
aLocalFile.append(aLeafName);
|
|
|
|
|
|
|
|
this.makeFileUnique(aLocalFile);
|
|
|
|
return aLocalFile;
|
|
|
|
},
|
|
|
|
|
|
|
|
makeFileUnique: function hald_makeFileUnique(aLocalFile) {
|
|
|
|
try {
|
|
|
|
// Note - this code is identical to that in
|
|
|
|
// toolkit/content/contentAreaUtils.js.
|
|
|
|
// If you are updating this code, update that code too! We can't share code
|
|
|
|
// here since this is called in a js component.
|
|
|
|
var collisionCount = 0;
|
|
|
|
while (aLocalFile.exists()) {
|
|
|
|
collisionCount++;
|
|
|
|
if (collisionCount == 1) {
|
|
|
|
// Append "(2)" before the last dot in (or at the end of) the filename
|
|
|
|
// special case .ext.gz etc files so we don't wind up with .tar(2).gz
|
|
|
|
if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i))
|
|
|
|
aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&");
|
|
|
|
else
|
|
|
|
aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// replace the last (n) in the filename with (n+1)
|
|
|
|
aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")");
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 08:30:01 +04:00
|
|
|
aLocalFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
|
2009-05-07 08:44:22 +04:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
dump("*** exception in validateLeafName: " + e + "\n");
|
|
|
|
|
2010-09-09 08:30:01 +04:00
|
|
|
if (e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED)
|
2009-05-07 08:44:22 +04:00
|
|
|
throw e;
|
|
|
|
|
|
|
|
if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) {
|
|
|
|
aLocalFile.append("unnamed");
|
|
|
|
if (aLocalFile.exists())
|
2010-09-09 08:30:01 +04:00
|
|
|
aLocalFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
|
2009-05-07 08:44:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
isUsableDirectory: function hald_isUsableDirectory(aDirectory) {
|
|
|
|
return aDirectory.exists() && aDirectory.isDirectory() && aDirectory.isWritable();
|
2010-09-10 23:25:09 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_notify: function hald_notify(aLauncher, aCallback) {
|
|
|
|
let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
|
|
|
|
|
2011-01-27 21:54:36 +03:00
|
|
|
let notifier = Cc[aCallback ? "@mozilla.org/alerts-service;1" : "@mozilla.org/toaster-alerts-service;1"].getService(Ci.nsIAlertsService);
|
2010-09-10 23:25:09 +04:00
|
|
|
notifier.showAlertNotification(URI_GENERIC_ICON_DOWNLOAD,
|
|
|
|
bundle.GetStringFromName("alertDownloads"),
|
|
|
|
bundle.GetStringFromName("alertCantOpenDownload"),
|
|
|
|
true, "", aCallback, "downloadopen-fail");
|
2009-05-07 08:44:22 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-06-30 01:02:25 +04:00
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]);
|