Skip to content
This repository was archived by the owner on May 23, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions lib/downloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions options.xul
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" ?>
<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<setting data-jetpack-id="jid1-9tZMAIdeuiEjHg@jetpack" pref="extensions.jid1-9tZMAIdeuiEjHg@jetpack.torrentSource" pref-name="torrentSource" title="Default torrent source" type="menulist">
Will be used in 'Download torrent file' context menu item
<menulist>
<menupopup>
<menuitem label="Torrage.com" value="torrage.com"/>
<menuitem label="Zoink.ch" value="zoink.ch"/>
<menuitem label="Torcache.net" value="torcache.net"/>
<menuitem label="TheTorrent.org" value="thetorrent.org"/>
</menupopup>
</menulist>
</setting>
<setting data-jetpack-id="jid1-9tZMAIdeuiEjHg@jetpack" title="Save to" type="control">
<setting id="magrentDestDir" data-jetpack-id="jid1-9tZMAIdeuiEjHg@jetpack" pref="extensions.jid1-9tZMAIdeuiEjHg@jetpack.destDir" pref-name="destDir" type="string" style="border: 0 solid transparent !important;"/>
<button data-jetpack-id="jid1-9tZMAIdeuiEjHg@jetpack" label="Browse" oncommand="
var fp = Components.classes['@mozilla.org/filepicker;1'].createInstance(Components.interfaces.nsIFilePicker),
destDir = document.getElementById('magrentDestDir'),
pref = Services.prefs.getBranch('extensions.jid1-9tZMAIdeuiEjHg@jetpack.');
fp.init(window, '', fp.modeGetFolder);
try
{
fp.displayDirectory = FileUtils.File(destDir.value);
}
catch(e){}
var rv = fp.show();
if (rv == fp.returnOK || rv == fp.returnReplace)
{
var str = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
str.data = fp.file.path;
pref.setComplexValue('destDir', Ci.nsISupportsString, str);

}
"/>
Leave blank to use browser's settings
</setting>
</vbox>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "magrent",
"license": "GPL 3.0",
"author": "Siavash Askari Nasr",
"version": "1.0.3",
"version": "1.0.3.1",
"fullName": "Magrent",
"id": "jid1-9tZMAIdeuiEjHg",
"permissions": {
Expand Down
2 changes: 2 additions & 0 deletions prefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pref("extensions.jid1-9tZMAIdeuiEjHg@jetpack.torrentSource", "torcache.net");
pref("extensions.jid1-9tZMAIdeuiEjHg@jetpack.destDir", "");