From 7f817241edbba286ad9c9313bfcee30d9f050e26 Mon Sep 17 00:00:00 2001 From: Alberto Miranda Date: Wed, 22 Mar 2017 23:21:07 -0300 Subject: [PATCH] added start / stop events; starting / stopping folder watcher. --- src/main/folder-watcher.js | 35 +++++++++++--------------- src/main/index.js | 6 ++++- src/main/ipc.js | 21 +++++++++++++++- src/renderer/main.js | 6 +++++ src/renderer/pages/preferences-page.js | 7 ++++++ 5 files changed, 53 insertions(+), 22 deletions(-) diff --git a/src/main/folder-watcher.js b/src/main/folder-watcher.js index 7bde2b7c..7692ec8b 100644 --- a/src/main/folder-watcher.js +++ b/src/main/folder-watcher.js @@ -6,9 +6,19 @@ class FolderWatcher { this.window = window this.state = state this.torrentsFolderPath + this.watching = false } - init () { + isEnabled () { + return this.state.saved.prefs.autoAddTorrents + } + + start () { + log('--- FOLDER WATCHER --> START') + // Stop watching previous folder before + // start watching a new one. + if (this.watching) this.stop() + const torrentsFolderPath = this.state.saved.prefs.torrentsFolderPath this.torrentsFolderPath = torrentsFolderPath if (!torrentsFolderPath) return @@ -23,30 +33,15 @@ class FolderWatcher { 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) - }) + this.watching = true } stop () { - if (!this.watcher) return + log('--- FOLDER WATCHER --> STOP') + if (!this.watching) return this.watcher.close() + this.watching = false } } diff --git a/src/main/index.js b/src/main/index.js index c99a4c31..ea38a842 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -136,7 +136,11 @@ function delayedInit (state) { announcement.init() dock.init() updater.init() - folderWatcher.init() + + ipc.setModule('folderWatcher', folderWatcher) + if (folderWatcher.isEnabled()) { + folderWatcher.start() + } if (process.platform === 'win32') { const userTasks = require('./user-tasks') diff --git a/src/main/ipc.js b/src/main/ipc.js index e6110626..432a480f 100644 --- a/src/main/ipc.js +++ b/src/main/ipc.js @@ -1,5 +1,6 @@ module.exports = { - init + init, + setModule } const electron = require('electron') @@ -13,6 +14,14 @@ const windows = require('./windows') // Messages from the main process, to be sent once the WebTorrent process starts const messageQueueMainToWebTorrent = [] +// Will hold modules injected from the app that will be used on fired +// IPC events. +const modules = {} + +function setModule (name, module) { + modules[name] = module +} + function init () { const ipc = electron.ipcMain @@ -106,6 +115,16 @@ function init () { thumbar.onPlayerPause() }) + ipc.on('startFolderWatcher', function () { + log('--- Torrent Watcher started') + modules['folderWatcher'].start() + }) + + ipc.on('stopFolderWatcher', function () { + log('--- Torrent Watcher stop') + modules['folderWatcher'].stop() + }) + /** * Shell */ diff --git a/src/renderer/main.js b/src/renderer/main.js index 7acb24f5..1fc431df 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -111,6 +111,10 @@ function onState (err, _state) { update: createGetter(() => { const UpdateController = require('./controllers/update-controller') return new UpdateController(state) + }), + folderWatcher: createGetter(() => { + const FolderWatcherController = require('./controllers/folder-watcher-controller') + return new FolderWatcherController() }) } @@ -296,6 +300,8 @@ const dispatchHandlers = { 'preferences': () => controllers.prefs().show(), 'updatePreferences': (key, value) => controllers.prefs().update(key, value), 'checkDownloadPath': checkDownloadPath, + 'startFolderWatcher': () => controllers.folderWatcher().start(), + 'stopFolderWatcher': () => controllers.folderWatcher().stop(), // Update (check for new versions on Linux, where there's no auto updater) 'updateAvailable': (version) => controllers.update().updateAvailable(version), diff --git a/src/renderer/pages/preferences-page.js b/src/renderer/pages/preferences-page.js index c6553f91..05c2ecfe 100644 --- a/src/renderer/pages/preferences-page.js +++ b/src/renderer/pages/preferences-page.js @@ -130,6 +130,13 @@ class PreferencesPage extends React.Component { } dispatch('updatePreferences', 'autoAddTorrents', isChecked) + + if (isChecked) { + dispatch('startFolderWatcher', null) + return + } + + dispatch('stopFolderWatcher', null) } torrentsFolderPathSelector () {