diff --git a/src/renderer/controllers/playback-controller.js b/src/renderer/controllers/playback-controller.js index d1c38153..523d6f3c 100644 --- a/src/renderer/controllers/playback-controller.js +++ b/src/renderer/controllers/playback-controller.js @@ -26,6 +26,15 @@ module.exports = class PlaybackController { // * Stream, if not already fully downloaded // * If no file index is provided, pick the default file to play playFile (infoHash, index /* optional */) { + // playback priority: pause all active torrents + if (this.state.saved.prefs.highestPlaybackPriority) { + var params = { + filter: {status: /downloading|seeding/}, + excluded: [infoHash] + } + dispatch('pauseAllTorrents', params) + } + this.state.location.go({ url: 'player', setup: (cb) => { @@ -300,6 +309,11 @@ module.exports = class PlaybackController { ipcRenderer.send('onPlayerClose') + // playback priority: resume previously paused downloads + if (this.state.saved.prefs.highestPlaybackPriority) { + dispatch('resumePausedTorrents') + } + this.update() } } diff --git a/src/renderer/controllers/torrent-list-controller.js b/src/renderer/controllers/torrent-list-controller.js index c155fbcf..50012451 100644 --- a/src/renderer/controllers/torrent-list-controller.js +++ b/src/renderer/controllers/torrent-list-controller.js @@ -103,18 +103,75 @@ module.exports = class TorrentListController { } } + pauseAll ({filter, excluded}) { + console.log('--- pause all') + this.state.saved.torrents.map((torrent) => { + // "excluded" is an array of torrents that should not be paused + if (excluded) { + var isExcluded = excluded.some((excludeInfoHash) => { + if (excludeInfoHash === torrent.infoHash) return true + }) + if (isExcluded) return + } + + // don't play sounds when pausing all + var wasPaused = this.pauseTorrent(torrent, false, filter) + + // if torrent was paused add it to paused torrents collection + // we will use this collection to resume downloading when playback stops + if (wasPaused) this.state.saved.pausedTorrents.push(torrent.infoHash) + }) + } + + resumePausedTorrents () { + console.log('--- resume paused torrents') + this.state.saved.pausedTorrents.map((infoHash) => { + var torrentSummary = TorrentSummary.getByKey(this.state, infoHash) + this.startTorrent(torrentSummary) + }) + + // reset paused torrents + this.state.saved.pausedTorrents = [] + } + // TODO: use torrentKey, not infoHash toggleTorrent (infoHash) { var torrentSummary = TorrentSummary.getByKey(this.state, infoHash) + + // start if (torrentSummary.status === 'paused') { - torrentSummary.status = 'new' - this.startTorrentingSummary(torrentSummary.torrentKey) - sound.play('ENABLE') - } else { - torrentSummary.status = 'paused' - ipcRenderer.send('wt-stop-torrenting', torrentSummary.infoHash) - sound.play('DISABLE') + this.startTorrent(torrentSummary, true) + return } + + // pause + this.pauseTorrent(torrentSummary, true) + } + + startTorrent (torrentSummary, playSound) { + torrentSummary.status = 'new' + this.startTorrentingSummary(torrentSummary.torrentKey) + + if (playSound) sound.play('ENABLE') + } + + pauseTorrent (torrentSummary, playSound, filter) { + if (filter && !this.matchesFilter(torrentSummary, filter)) return false + + torrentSummary.status = 'paused' + ipcRenderer.send('wt-stop-torrenting', torrentSummary.infoHash) + + if (playSound) sound.play('DISABLE') + return true + } + + matchesFilter (torrentSummary, filter) { + var keys = Object.keys(filter) + var matches = keys.some((key) => { + if (!torrentSummary[key].match(filter[key])) return false + return true + }) + return matches } toggleTorrentFile (infoHash, index) { diff --git a/src/renderer/main.js b/src/renderer/main.js index e9ed6cee..ccab7261 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -184,6 +184,8 @@ const dispatchHandlers = { 'toggleSelectTorrent': (infoHash) => controllers.torrentList.toggleSelectTorrent(infoHash), 'openTorrentContextMenu': (infoHash) => controllers.torrentList.openTorrentContextMenu(infoHash), 'startTorrentingSummary': (torrentKey) => controllers.torrentList.startTorrentingSummary(torrentKey), + 'pauseAllTorrents': (params) => controllers.torrentList.pauseAll(params), + 'resumePausedTorrents': () => controllers.torrentList.resumePausedTorrents(), // Playback 'playFile': (infoHash, index) => controllers.playback.playFile(infoHash, index), diff --git a/src/renderer/pages/PreferencesPage.js b/src/renderer/pages/PreferencesPage.js index 79642979..f79a8f3b 100644 --- a/src/renderer/pages/PreferencesPage.js +++ b/src/renderer/pages/PreferencesPage.js @@ -60,6 +60,24 @@ class PreferencesPage extends React.Component { dispatch('updatePreferences', 'openExternalPlayer', !isChecked) } + highestPlaybackPriorityCheckbox () { + return ( + + +

Pauses all active torrents to allow playback to use all of the available bandwidth.

+
+ ) + } + + handleHighestPlaybackPriorityChange (e, isChecked) { + dispatch('updatePreferences', 'highestPlaybackPriority', isChecked) + } + externalPlayerPathSelector () { const playerName = path.basename( this.props.state.unsaved.prefs.externalPlayerPath || 'VLC' @@ -129,6 +147,7 @@ class PreferencesPage extends React.Component { {this.openExternalPlayerCheckbox()} {this.externalPlayerPathSelector()} + {this.highestPlaybackPriorityCheckbox()} {this.setDefaultAppButton()}