From 12c98ea0d3729b36ffb5c66767391f96eb784fb1 Mon Sep 17 00:00:00 2001 From: "V@no" Date: Sat, 25 Apr 2015 19:06:22 -0400 Subject: [PATCH] v1.0.3.1 + setting for save file location + obey browser's settings if save file location not set --- lib/downloads.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++-- options.xul | 37 ++++++++++++++++++++++++++++++ package.json | 2 +- prefs.js | 2 ++ 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 options.xul create mode 100644 prefs.js 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 + + + + + + + + + + + +