diff --git a/package.json b/package.json index 344a6ab2..b1933174 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "dependencies": { "airplay-js": "guerrerocarlos/node-airplay-js", + "application-config": "^0.2.0", "chromecasts": "^1.8.0", "create-torrent": "^3.22.1", "debug": "^2.2.0", diff --git a/renderer/index.js b/renderer/index.js index 6d0e074b..e424e8eb 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -13,6 +13,7 @@ var state = require('./state') var throttle = require('throttleit') var torrentPoster = require('./lib/torrent-poster') var WebTorrent = require('webtorrent') +var cfg = require('application-config')('WebTorrent') var createElement = require('virtual-dom/create-element') var diff = require('virtual-dom/diff') @@ -35,10 +36,7 @@ function init () { state.client = new WebTorrent() state.client.on('warning', onWarning) state.client.on('error', onError) - - state.saved.torrents.forEach(function (torrent) { - state.client.add(torrent.torrentFile) - }) + state.client.on('torrent', saveTorrents) // For easy debugging in Developer Tools global.state = state @@ -54,6 +52,8 @@ function init () { dragDrop('body', onFiles) + restoreSession() + chromecasts.on('update', function (player) { state.devices.chromecast = player update() @@ -251,6 +251,35 @@ function addTorrentEvents (torrent) { } } +function restoreSession () { + cfg.read(function (err, data) { + if (err) console.error(err) + state.saved = data + if (!state.saved.torrents) state.saved.torrents = [] + state.saved.torrents.forEach(function (torrent) { + dispatch('addTorrent', torrent.magnetURI) + }) + update() + }) +} + +function saveTorrents () { + state.saved.torrents = state.client.torrents.map(function (torrent) { + return { + name: torrent.name, + magnetURI: torrent.magnetURI, + path: torrent.path + } + }) + + cfg.write({ + torrents: state.saved.torrents + }, function (err) { + if (err) console.error(err) + update() + }) +} + function startServer (torrent, cb) { if (state.server) return cb() diff --git a/renderer/state.js b/renderer/state.js index 566e9453..6542b794 100644 --- a/renderer/state.js +++ b/renderer/state.js @@ -27,27 +27,20 @@ module.exports = { mouseStationarySince: 0 /* Unix time in ms */ }, - /* Saved state is read from and written to ~/.webtorrent/state.json - * It should be simple and minimal and must be JSONifiable + /* Saved state is read from and written to application config. + * It should be simple and minimal and must be JSON stringifiable. + * + * Config path: + * + * OS XDG ~/Library/Application Support/WebTorrent/config.json + * Linux (XDG) $XDG_CONFIG_HOME/WebTorrent/config.json + * Linux (Legacy) ~/.config/WebTorrent/config.json + * Windows (> Vista) %LOCALAPPDATA%/WebTorrent/config.json + * Windows (XP, 2000) %USERPROFILE%/Local Settings/Application Data/WebTorrent/config.json + * + * Also accessible via `require('application-config')('WebTorrent').filePath` */ saved: { - torrents: [ - { - name: 'Sintel', - torrentFile: 'resources/sintel.torrent' - } - // { - // name: 'Elephants Dream', - // torrentFile: 'resources/ElephantsDream_archive.torrent' - // }, - // { - // name: 'Big Buck Bunny', - // torrentFile: 'resources/BigBuckBunny_archive.torrent' - // }, - // { - // name: 'Tears of Steel', - // torrentFile: 'resources/TearsOfSteel_archive.torrent' - // } - ] + torrents: [] } }