Skip to content
This repository has been archived by the owner on Sep 22, 2021. It is now read-only.

Commit

Permalink
Updates to the downloading #63
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas101 committed Feb 3, 2016
1 parent f5279b1 commit 6eccfbd
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 16 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"electron-google-oauth": "^1.3.1",
"electron-localshortcut": "^0.6.0",
"flexboxgrid": "^6.3.0",
"fs-extra": "^0.26.5",
"googleapis": "^2.1.7",
"https-proxy-agent": "^1.0.0",
"material-ui": "^0.14.2",
Expand All @@ -60,6 +61,7 @@
"react-dom": "^0.14.6",
"react-tap-event-plugin": "^0.2.1",
"react-timer-mixin": "^0.13.3",
"typo-js": "^1.0.1"
"typo-js": "^1.0.1",
"uuid": "^2.0.1"
}
}
18 changes: 18 additions & 0 deletions src/mailbox/stores/settings/settingsActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ class SettingsActions {
return this.mergeUpdates({ spellcheckerEnabled: enabled })
}

/* **************************************************************************/
// Downloads
/* **************************************************************************/

/**
* @param ask: true to always ask, false otherwise
*/
setAlwaysAskDownloadLocation (ask) {
return this.mergeUpdates({ alwaysAskDownloadLocation: ask })
}

/**
* @param path: the path to download files to automatically
*/
setDefaultDownloadLocation (path) {
return this.mergeUpdates({ defaultDownloadLocation: path })
}

}

module.exports = alt.createActions(SettingsActions)
11 changes: 11 additions & 0 deletions src/mailbox/stores/settings/settingsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ class SettingsStore {
return this.__valuediff__('spellcheckerEnabled', true)
}

/* ****************************************/
// Downloads
/* ****************************************/

this.alwaysAskDownloadLocation = () => {
return this.__value__('alwaysAskDownloadLocation', true)
}
this.defaultDownloadLocation = () => {
return this.__value__('defaultDownloadLocation', undefined)
}

/* ****************************************/
// Higher order
/* ****************************************/
Expand Down
35 changes: 23 additions & 12 deletions src/mailbox/ui/Mailbox/GoogleMailboxWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const session = remote.require('session')
const ipc = window.nativeRequire('electron').ipcRenderer
const mailboxDispatch = require('../Dispatch/mailboxDispatch')
const TimerMixin = require('react-timer-mixin')
const uuid = require('uuid')

/* eslint-disable react/prop-types */

Expand Down Expand Up @@ -141,6 +142,26 @@ module.exports = React.createClass({
}
},

/* **************************************************************************/
// Webview events
/* **************************************************************************/

/**
* Handles the download
* @param evt: the event that fired
* @param item: the item that's downloading
*/
handleDownload: function (evt, item) {
const totalBytes = item.getTotalBytes()
const id = uuid.v4()
item.on('updated', () => {
ipc.send('download-progress', { id: id, received: item.getReceivedBytes(), total: totalBytes })
})
item.on('done', (e, state) => {
ipc.send('download-complete', { id: id })
})
},

/* **************************************************************************/
// Rendering
/* **************************************************************************/
Expand All @@ -153,19 +174,9 @@ module.exports = React.createClass({
renderWebviewDOMNode: function () {
// Setup the session that will be used
const partition = 'persist:' + this.state.mailbox.id
var ses = session.fromPartition(partition)
const ses = session.fromPartition(partition)
ses.setDownloadPath(app.getPath('downloads'))
/* ses.on('will-download', (evt, item) => {
const totalBytes = item.getTotalBytes()
item.setSavePath(path.join(app.getPath('downloads'), item.getFilename())
item.on('updated', () => {
win.setProgressBar(item.getReceivedBytes() / totalBytes);
})
item.on('done', (e, state) => {
win.setProgressBar(-1)
})
})*/
ses.on('will-download', this.handleDownload)

// Build the dom
const webview = document.createElement('webview')
Expand Down
3 changes: 1 addition & 2 deletions src/mailbox/ui/Settings/GeneralSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ module.exports = React.createClass({
label='Show tray icon'
onToggle={this.handleToggleShowTrayIcon} />
</Paper>
<Paper zDepth={1} style={{ padding: 15, marginTop: 5 }}>
<Paper zDepth={1} style={{ padding: 15, marginTop: 5, marginBottom: 5 }}>
<Toggle
toggled={this.state.spellcheckerEnabled}
label={(<span><span>Spell-checker</span> <small>(Experimental, requires restart)</small></span>)}
onToggle={this.handleToggleSpellchecker} />

</Paper>
</div>
)
Expand Down
62 changes: 62 additions & 0 deletions src/main/MailboxesDownloadManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

class MailboxesDownloadManager {

/* ****************************************************************************/
// Lifecycle
/* ****************************************************************************/

constructor (mailboxWindow) {
this.inProgress = { }
this.mailboxWindow = mailboxWindow
}

/* ****************************************************************************/
// App
/* ****************************************************************************/

/**
* Updates the progress bar in the dock
*/
updateProgressBar () {
const all = Object.keys(this.inProgress).reduce((acc, id) => {
acc.received += this.inProgress[id].received
acc.total += this.inProgress[id].total
return acc
}, { received: 0, total: 0 })

if (all.received === 0 && all.total === 0) {
this.mailboxWindow.setProgressBar(-1)
} else {
this.mailboxWindow.setProgressBar(all.received / all.total)
}
}

/* ****************************************************************************/
// Updates
/* ****************************************************************************/

/**
* Updates the progress on a download
* @param id: the download id
* @param received: the bytes received
* @param total: the total bytes to download
*/
updateProgress (id, received, total) {
this.inProgress[id] = this.inProgress[id] || {}
this.inProgress[id].received = received
this.inProgress[id].total = total
this.updateProgressBar()
}

/**
* Indicates that a download has finished
* @param id: the download id
*/
downloadFinished (id) {
delete this.inProgress[id]
this.updateProgressBar()
}
}

module.exports = MailboxesDownloadManager
8 changes: 8 additions & 0 deletions src/main/WMailWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ class WMailWindow extends EventEmitter {
isFocused () {
return this.window.isFocused()
}

/**
* Sets the download progress
* @param v: the download progress to set
*/
setProgressBar (v) {
this.window.setProgressBar(v)
}
}

module.exports = WMailWindow
13 changes: 12 additions & 1 deletion src/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const WindowManager = require('./WindowManager')
const constants = require('../shared/constants')
const exec = require('child_process').exec
const AppSettings = require('./AppSettings')
const MailboxesDownloadManager = require('./MailboxesDownloadManager')

/* ****************************************************************************/
// Global objects
Expand All @@ -24,7 +25,9 @@ const appDirectory = new AppDirectory(pkg.name)
const localStorage = new LocalStorage(appDirectory.userData())
const appSettings = new AppSettings(localStorage)
const analytics = new AppAnalytics(localStorage, appSettings)
const windowManager = new WindowManager(new MailboxesWindow(analytics, localStorage, appSettings))
const mailboxesWindow = new MailboxesWindow(analytics, localStorage, appSettings)
const mailboxesDownloadManager = new MailboxesDownloadManager(mailboxesWindow)
const windowManager = new WindowManager(mailboxesWindow)

const appMenuSelectors = {
fullQuit: () => { windowManager.quit() },
Expand Down Expand Up @@ -80,6 +83,14 @@ ipcMain.on('settings-update', (evt, settings) => {
appSettings.update(settings)
})

ipcMain.on('download-progress', (evt, data) => {
mailboxesDownloadManager.updateProgress(data.id, data.received, data.total)
})

ipcMain.on('download-complete', (evt, data) => {
mailboxesDownloadManager.downloadFinished(data.id)
})

/* ****************************************************************************/
// App Events
/* ****************************************************************************/
Expand Down

0 comments on commit 6eccfbd

Please sign in to comment.