From a70c4d1bf217f4a651757ddccbc4dbf9d13de093 Mon Sep 17 00:00:00 2001 From: Alberto Miranda Date: Wed, 22 Mar 2017 09:42:17 -0300 Subject: [PATCH] Working on watch-folder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added chokidar to watch for folder changes; added folder-watcher; passing state to delayedInit on main; added default values for new preferences; added “Auto add torrents” preference with its checkbox and path selector; TODO: start/stop watching on preference change, start watching on init, add dialog when trying to enable preference without a torrents folder. --- package.json | 1 + src/main/folder-watcher.js | 54 ++++++++++++++++++++++++++ src/main/index.js | 12 ++++-- src/renderer/lib/state.js | 4 +- src/renderer/pages/preferences-page.js | 54 ++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 src/main/folder-watcher.js diff --git a/package.json b/package.json index 90be9826..fc30abe0 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "auto-launch": "^4.0.1", "bitfield": "^1.0.2", "capture-frame": "^1.0.0", + "chokidar": "^1.6.1", "chromecasts": "^1.8.0", "cp-file": "^4.0.1", "create-torrent": "^3.24.5", diff --git a/src/main/folder-watcher.js b/src/main/folder-watcher.js new file mode 100644 index 00000000..9e0a4ad7 --- /dev/null +++ b/src/main/folder-watcher.js @@ -0,0 +1,54 @@ +const chokidar = require('chokidar') +const log = require('./log') + +class FolderWatcher { + constructor ({window, state}) { + this.window = window + this.state = state + this.torrentsFolderPath + } + + init () { + const torrentsFolderPath = this.state.saved.prefs.torrentsFolderPath + this.torrentsFolderPath = torrentsFolderPath + if (!torrentsFolderPath) return + + const glob = `${torrentsFolderPath}/**/*.torrent` + log('Folder Watcher: watching: ', glob) + + const options = {ignoreInitial: true} + this.watcher = chokidar.watch(glob, options) + this.watcher + .on('add', (path) => { + log('-- torrent added: ', path) + this.window.dispatch('addTorrent', path) + }) + } + + start (torrentsFolderPath) { + // Stop watching previous folder before + // start watching a new one. + if (this.torrentsFolderPath) { + this.stop() + } + + const glob = `${torrentsFolderPath}/**/*.torrent` + log('Folder Watcher: watching: ', glob) + + const options = {ignoreInitial: true} + this.watcher = chokidar.watch(glob, options) + this.watcher + .on('add', (path) => { + log('-- torrent added: ', path) + this.window.dispatch('addTorrent', path) + }) + } + + stop () { + if (!this.watcher) return + this.watcher.close() + } +} + + +module.exports = FolderWatcher diff --git a/src/main/index.js b/src/main/index.js index 38286977..c99a4c31 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -72,13 +72,16 @@ function init () { if (err) throw err isReady = true + const state = results.state - windows.main.init(results.state, {hidden: hidden}) + windows.main.init(state, {hidden: hidden}) windows.webtorrent.init() menu.init() // To keep app startup fast, some code is delayed. - setTimeout(delayedInit, config.DELAYED_INIT) + setTimeout(() => { + delayedInit(state) + }, config.DELAYED_INIT) // Report uncaught exceptions process.on('uncaughtException', (err) => { @@ -121,16 +124,19 @@ function init () { }) } -function delayedInit () { +function delayedInit (state) { if (app.isQuitting) return const announcement = require('./announcement') const dock = require('./dock') const updater = require('./updater') + const FolderWatcher = require('./folder-watcher') + const folderWatcher = new FolderWatcher({window: windows.main, state}) announcement.init() dock.init() updater.init() + folderWatcher.init() if (process.platform === 'win32') { const userTasks = require('./user-tasks') diff --git a/src/renderer/lib/state.js b/src/renderer/lib/state.js index 6a909d96..68c85bb0 100644 --- a/src/renderer/lib/state.js +++ b/src/renderer/lib/state.js @@ -121,7 +121,9 @@ function setupStateSaved (cb) { isFileHandler: false, openExternalPlayer: false, externalPlayerPath: null, - startup: false + startup: false, + autoAddTorrents: false, + torrentsFolderPath: '' }, torrents: config.DEFAULT_TORRENTS.map(createTorrentObject), torrentsToResume: [], diff --git a/src/renderer/pages/preferences-page.js b/src/renderer/pages/preferences-page.js index bf729141..f5c4b8d1 100644 --- a/src/renderer/pages/preferences-page.js +++ b/src/renderer/pages/preferences-page.js @@ -108,6 +108,56 @@ class PreferencesPage extends React.Component { dispatch('updatePreferences', 'externalPlayerPath', filePath) } + autoAddTorrentsCheckbox () { + return ( + + {this.handleAutoAddTorrentsChange(e, value)}} + /> + + ) + } + + handleAutoAddTorrentsChange (e, isChecked) { + const torrentsFolderPath = this.props.state.unsaved.prefs.torrentsFolderPath + if (isChecked && !torrentsFolderPath) { + alert('Select a torrents folder first.') + e.preventDefault() + return + } + + dispatch('updatePreferences', 'autoAddTorrents', isChecked) + } + + torrentsFolderPathSelector () { + const torrentsFolderPath = this.props.state.unsaved.prefs.torrentsFolderPath + + const value = torrentsFolderPath || 'Path to be watched.' + const description = 'Torrent files saved to this folder will be automatically added to the list.' + + return ( + +

{description}

+ +
+ ) + } + + handletorrentsFolderPathChange (filePath) { + dispatch('updatePreferences', 'torrentsFolderPath', filePath) + } + setDefaultAppButton () { const isFileHandler = this.props.state.unsaved.prefs.isFileHandler if (isFileHandler) { @@ -174,6 +224,10 @@ class PreferencesPage extends React.Component { {this.setDefaultAppButton()} + + {this.autoAddTorrentsCheckbox()} + {this.torrentsFolderPathSelector()} + {this.setStartupSection()} )