Working on watch-folder

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.
This commit is contained in:
Alberto Miranda
2017-03-22 09:42:17 -03:00
parent c2abb50e9e
commit a70c4d1bf2
5 changed files with 121 additions and 4 deletions

View File

@@ -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

View File

@@ -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')

View File

@@ -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: [],

View File

@@ -108,6 +108,56 @@ class PreferencesPage extends React.Component {
dispatch('updatePreferences', 'externalPlayerPath', filePath)
}
autoAddTorrentsCheckbox () {
return (
<Preference>
<Checkbox
className='control'
checked={this.props.state.unsaved.prefs.autoAddTorrents}
label={'Enable'}
onCheck={(e, value) => {this.handleAutoAddTorrentsChange(e, value)}}
/>
</Preference>
)
}
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 (
<Preference>
<p>{description}</p>
<PathSelector
dialog={{
title: 'Select torrents folder path',
properties: [ 'openDirectory' ]
}}
displayValue={value}
onChange={this.handletorrentsFolderPathChange}
title='Torrents folder'
value={torrentsFolderPath ? path.dirname(torrentsFolderPath) : null} />
</Preference>
)
}
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 {
<PreferencesSection title='Default torrent app'>
{this.setDefaultAppButton()}
</PreferencesSection>
<PreferencesSection title='Auto add torrents'>
{this.autoAddTorrentsCheckbox()}
{this.torrentsFolderPathSelector()}
</PreferencesSection>
{this.setStartupSection()}
</div>
)