diff --git a/lib/downloads.js b/lib/downloads.js index 3b564fc..f85c704 100644 --- a/lib/downloads.js +++ b/lib/downloads.js @@ -19,20 +19,75 @@ const privateBrowsing = require('sdk/private-browsing'); const { browserWindows } = require('sdk/windows'); -const { Cu } = require('chrome'); +const { Cu, Ci, Cc} = require('chrome'); const { uniqName } = require('uniq-name'); const { Downloads } = Cu.import('resource://gre/modules/Downloads.jsm'); const { Task } = Cu.import('resource://gre/modules/Task.jsm'); +const { notify } = require("sdk/notifications"); +const { Services } = Cu.import('resource://gre/modules/Services.jsm'); +const { FileUtils } = Cu.import('resource://gre/modules/FileUtils.jsm'); +const utils = require('sdk/window/utils'); +const pref = Services.prefs.getBranch('extensions.jid1-9tZMAIdeuiEjHg@jetpack.'); + const download = data => { Task.spawn(function () { + let dir = pref.getComplexValue('destDir', Ci.nsISupportsString).data; + if (!dir) //use browser's settings + { + if (Services.prefs.getBoolPref("browser.download.useDownloadDir")) //use prefered directory + { + dir = (yield Downloads.getPreferredDownloadsDirectory()); + } + else //ask where to save + { + let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); + fp.init(utils.getMostRecentBrowserWindow(), "Save .torrent", fp.modeSave); + fp.appendFilter("torrent", "*.torrent"); + try + { + fp.defaultString = data.name; + fp.defaultExtension = ".torrent"; + fp.displayDirectory = new FileUtils.File(Services.prefs.getComplexValue("browser.download.lastDir", Ci.nsISupportsString).data); + } + catch(e){} + let rv = fp.show(); + if (rv == fp.returnOK || rv == fp.returnReplace) + { + data.name = fp.file.leafName; + dir = fp.file.parent.path; + } + else + { + return; //user canceled save file dialog + } + } + } + if (!dir) //something went wrong + dir = (yield Downloads.getPreferredDownloadsDirectory()); + + let nsIFile = new FileUtils.File(dir); + if (!nsIFile.exists()) + try + { + nsIFile.create(nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY); + } + catch(e) + { + notify({ + title: "Problem saving file into", + text: dir + }); + return; + } + let dl = yield Downloads.createDownload({ source: { url: data.url, isPrivate: privateBrowsing.isPrivate(browserWindows.activeWindow) }, - target: uniqName(data.name, (yield Downloads.getPreferredDownloadsDirectory())) + target: uniqName(data.name, dir) }); let list = yield Downloads.getList(Downloads.ALL); yield list.add(dl); diff --git a/options.xul b/options.xul new file mode 100644 index 0000000..4f7e18a --- /dev/null +++ b/options.xul @@ -0,0 +1,37 @@ + + + + Will be used in 'Download torrent file' context menu item + + + + + + + + + + + +