michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Downloads", michael@0: "resource://gre/modules/Downloads.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Task", michael@0: "resource://gre/modules/Task.jsm"); michael@0: michael@0: // ----------------------------------------------------------------------- michael@0: // HelperApp Launcher Dialog michael@0: // michael@0: // For now on b2g we never prompt and just download to the default michael@0: // location. michael@0: // michael@0: // ----------------------------------------------------------------------- michael@0: michael@0: function HelperAppLauncherDialog() { } michael@0: michael@0: HelperAppLauncherDialog.prototype = { michael@0: classID: Components.ID("{710322af-e6ae-4b0c-b2c9-1474a87b077e}"), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIHelperAppLauncherDialog]), michael@0: michael@0: show: function(aLauncher, aContext, aReason) { michael@0: aLauncher.MIMEInfo.preferredAction = Ci.nsIMIMEInfo.saveToDisk; michael@0: aLauncher.saveToDisk(null, false); michael@0: }, michael@0: michael@0: promptForSaveToFile: function(aLauncher, michael@0: aContext, michael@0: aDefaultFile, michael@0: aSuggestedFileExt, michael@0: aForcePrompt) { michael@0: throw Cr.NS_ERROR_NOT_AVAILABLE; michael@0: }, michael@0: michael@0: promptForSaveToFileAsync: function(aLauncher, michael@0: aContext, michael@0: aDefaultFile, michael@0: aSuggestedFileExt, michael@0: aForcePrompt) { michael@0: // Retrieve the user's default download directory. michael@0: Task.spawn(function() { michael@0: let file = null; michael@0: try { michael@0: let defaultFolder = yield Downloads.getPreferredDownloadsDirectory(); michael@0: let dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); michael@0: dir.initWithPath(defaultFolder); michael@0: file = this.validateLeafName(dir, aDefaultFile, aSuggestedFileExt); michael@0: } catch(e) { } michael@0: aLauncher.saveDestinationAvailable(file); michael@0: }.bind(this)).then(null, Cu.reportError); michael@0: }, michael@0: michael@0: validateLeafName: function(aLocalFile, aLeafName, aFileExt) { michael@0: if (!(aLocalFile && this.isUsableDirectory(aLocalFile))) michael@0: return null; michael@0: michael@0: // Remove any leading periods, since we don't want to save hidden files michael@0: // automatically. michael@0: aLeafName = aLeafName.replace(/^\.+/, ""); michael@0: michael@0: if (aLeafName == "") michael@0: aLeafName = "unnamed" + (aFileExt ? "." + aFileExt : ""); michael@0: aLocalFile.append(aLeafName); michael@0: michael@0: this.makeFileUnique(aLocalFile); michael@0: return aLocalFile; michael@0: }, michael@0: michael@0: makeFileUnique: function(aLocalFile) { michael@0: try { michael@0: // Note - this code is identical to that in michael@0: // toolkit/content/contentAreaUtils.js. michael@0: // If you are updating this code, update that code too! We can't share code michael@0: // here since this is called in a js component. michael@0: let collisionCount = 0; michael@0: while (aLocalFile.exists()) { michael@0: collisionCount++; michael@0: if (collisionCount == 1) { michael@0: // Append "(2)" before the last dot in (or at the end of) the filename michael@0: // special case .ext.gz etc files so we don't wind up with .tar(2).gz michael@0: if (aLocalFile.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i)) michael@0: aLocalFile.leafName = aLocalFile.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&"); michael@0: else michael@0: aLocalFile.leafName = aLocalFile.leafName.replace(/(\.[^\.]*)?$/, "(2)$&"); michael@0: } michael@0: else { michael@0: // replace the last (n) in the filename with (n+1) michael@0: aLocalFile.leafName = aLocalFile.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")"); michael@0: } michael@0: } michael@0: aLocalFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); michael@0: } michael@0: catch (e) { michael@0: dump("*** exception in makeFileUnique: " + e + "\n"); michael@0: michael@0: if (e.result == Cr.NS_ERROR_FILE_ACCESS_DENIED) michael@0: throw e; michael@0: michael@0: if (aLocalFile.leafName == "" || aLocalFile.isDirectory()) { michael@0: aLocalFile.append("unnamed"); michael@0: if (aLocalFile.exists()) michael@0: aLocalFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0600); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: isUsableDirectory: function(aDirectory) { michael@0: return aDirectory.exists() && michael@0: aDirectory.isDirectory() && michael@0: aDirectory.isWritable(); michael@0: }, michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([HelperAppLauncherDialog]);