diff --git a/src/renderer/components/delete-all-torrents-modal.js b/src/renderer/components/delete-all-torrents-modal.js index 6e34a41d..91993275 100644 --- a/src/renderer/components/delete-all-torrents-modal.js +++ b/src/renderer/components/delete-all-torrents-modal.js @@ -1,11 +1,11 @@ const React = require('react') const ModalOKCancel = require('./modal-ok-cancel') -const {dispatch, dispatcher} = require('../lib/dispatcher') +const { dispatch, dispatcher } = require('../lib/dispatcher') module.exports = class DeleteAllTorrentsModal extends React.Component { render () { - const {state: {modal: {deleteData}}} = this.props + const { state: { modal: { deleteData } } } = this.props const message = deleteData ? 'Are you sure you want to remove all the torrents from the list and delete the data files?' : 'Are you sure you want to remove all the torrents from the list?' diff --git a/src/renderer/controllers/torrent-list-controller.js b/src/renderer/controllers/torrent-list-controller.js index 699749a2..aff42c8e 100644 --- a/src/renderer/controllers/torrent-list-controller.js +++ b/src/renderer/controllers/torrent-list-controller.js @@ -210,44 +210,29 @@ module.exports = class TorrentListController { // TODO: use torrentKey, not infoHash deleteTorrent (infoHash, deleteData) { - ipcRenderer.send('wt-stop-torrenting', infoHash) - const index = this.state.saved.torrents.findIndex((x) => x.infoHash === infoHash) if (index > -1) { const summary = this.state.saved.torrents[index] - - // remove torrent and poster file - deleteFile(TorrentSummary.getTorrentPath(summary)) - deleteFile(TorrentSummary.getPosterPath(summary)) - - // optionally delete the torrent data - if (deleteData) moveItemToTrash(summary) + deleteTorrentFile(summary, deleteData) // remove torrent from saved list this.state.saved.torrents.splice(index, 1) dispatch('stateSave') - } - // prevent user from going forward to a deleted torrent - this.state.location.clearForward('player') - sound.play('DELETE') + // prevent user from going forward to a deleted torrent + this.state.location.clearForward('player') + sound.play('DELETE') + } else { + throw new TorrentKeyNotFoundError(infoHash) + } } deleteAllTorrents (deleteData) { - this.state.saved.torrents.forEach((summary) => { - ipcRenderer.send('wt-stop-torrenting', summary.infoHash) - - // remove torrent and poster file - deleteFile(TorrentSummary.getTorrentPath(summary)) - deleteFile(TorrentSummary.getPosterPath(summary)) - - if (deleteData) moveItemToTrash(summary) - - dispatch('stateSave') - }) + this.state.saved.torrents.forEach((summary) => deleteTorrentFile(summary, deleteData)) this.state.saved.torrents = [] + dispatch('stateSave') // prevent user from going forward to a deleted torrent this.state.location.clearForward('player') @@ -313,7 +298,7 @@ module.exports = class TorrentListController { // Shows a Save File dialog, then saves the .torrent file wherever the user requests saveTorrentFileAs (torrentKey) { const torrentSummary = TorrentSummary.getByKey(this.state, torrentKey) - if (!torrentSummary) throw new Error('Missing torrentKey: ' + torrentKey) + if (!torrentSummary) throw new TorrentKeyNotFoundError(torrentKey) const downloadPath = this.state.saved.prefs.downloadPath const newFileName = path.parse(torrentSummary.name).name + '.torrent' const win = electron.remote.getCurrentWindow() @@ -398,3 +383,14 @@ function moveItemToTrash (torrentSummary) { function showItemInFolder (torrentSummary) { ipcRenderer.send('showItemInFolder', TorrentSummary.getFileOrFolder(torrentSummary)) } + +function deleteTorrentFile (torrentSummary, deleteData) { + ipcRenderer.send('wt-stop-torrenting', torrentSummary.infoHash) + + // remove torrent and poster file + deleteFile(TorrentSummary.getTorrentPath(torrentSummary)) + deleteFile(TorrentSummary.getPosterPath(torrentSummary)) + + // optionally delete the torrent data + if (deleteData) moveItemToTrash(torrentSummary) +}