2013-10-17 23:30:47 +00:00
|
|
|
// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2009-05-07 04:44:22 +00:00
|
|
|
|
2013-10-17 23:30:47 +00:00
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
2009-05-07 04:44:22 +00:00
|
|
|
|
|
|
|
const PREF_BD_USEDOWNLOADDIR = "browser.download.useDownloadDir";
|
2012-05-25 03:41:57 +00:00
|
|
|
const URI_GENERIC_ICON_DOWNLOAD = "drawable://alert_download";
|
2009-05-07 04:44:22 +00:00
|
|
|
|
2010-07-13 14:36:09 +00:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-08-10 18:43:20 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-10-17 23:30:47 +00:00
|
|
|
Cu.import("resource://gre/modules/Prompt.jsm");
|
|
|
|
Cu.import("resource://gre/modules/HelperApps.jsm");
|
2009-05-07 04:44:22 +00: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) {
|
2013-10-17 23:30:47 +00:00
|
|
|
let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
|
|
|
|
|
|
|
|
let defaultHandler = new Object();
|
|
|
|
let apps = HelperApps.getAppsForUri(aLauncher.source, {
|
|
|
|
mimeType: aLauncher.MIMEInfo.MIMEType,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add a fake intent for save to disk at the top of the list
|
|
|
|
apps.unshift({
|
|
|
|
name: bundle.GetStringFromName("helperapps.saveToDisk"),
|
|
|
|
iconUri: "drawable://icon",
|
|
|
|
launch: function() {
|
|
|
|
// Reset the preferredAction here
|
|
|
|
aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk;
|
|
|
|
aLauncher.saveToDisk(null, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let app = apps[0];
|
|
|
|
if (apps.length > 1) {
|
|
|
|
app = HelperApps.prompt(apps, {
|
|
|
|
title: bundle.GetStringFromName("helperapps.pick")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app) {
|
|
|
|
aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.useHelperApp;
|
|
|
|
if (!app.launch(aLauncher.source)) {
|
|
|
|
aLauncher.cancel();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Something weird happened. Log an error
|
|
|
|
Services.console.logStringMessage("Unexpected selection from grid: " + app);
|
|
|
|
}
|
|
|
|
|
2009-05-07 04:44:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
promptForSaveToFile: function hald_promptForSaveToFile(aLauncher, aContext, aDefaultFile, aSuggestedFileExt, aForcePrompt) {
|
2013-09-12 22:46:35 +00:00
|
|
|
// Retrieve the user's default download directory
|
|
|
|
let dnldMgr = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
|
|
|
|
let defaultFolder = dnldMgr.userDownloadsDirectory;
|
2009-05-07 04:44:22 +00:00
|
|
|
|
|
|
|
try {
|
2013-09-12 22:46:35 +00:00
|
|
|
file = this.validateLeafName(defaultFolder, aDefaultFile, aSuggestedFileExt);
|
|
|
|
} catch (e) { }
|
2009-05-07 04:44:22 +00:00
|
|
|
|
|
|
|
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.
|
2013-10-17 23:30:47 +00:00
|
|
|
let collisionCount = 0;
|
2009-05-07 04:44:22 +00:00
|
|
|
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 04:30:01 +00:00
|
|
|
aLocalFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
|
2009-05-07 04:44:22 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
dump("*** exception in validateLeafName: " + e + "\n");
|
|
|
|
|
2010-09-09 04:30:01 +00:00
|
|
|
if (e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED)
|
2009-05-07 04:44:22 +00:00
|
|
|
throw e;
|
|
|
|
|
|
|
|
if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) {
|
|
|
|
aLocalFile.append("unnamed");
|
|
|
|
if (aLocalFile.exists())
|
2010-09-09 04:30:01 +00:00
|
|
|
aLocalFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600);
|
2009-05-07 04:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
isUsableDirectory: function hald_isUsableDirectory(aDirectory) {
|
|
|
|
return aDirectory.exists() && aDirectory.isDirectory() && aDirectory.isWritable();
|
2010-09-10 19:25:09 +00:00
|
|
|
},
|
2009-05-07 04:44:22 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]);
|