Skip to content

Commit

Permalink
Misc cleanup (#588)
Browse files Browse the repository at this point in the history
* Group dc events together

* Use icpMain to be more explicit

* windows.main -> main

* Refactor and simplify login error events

* Add event listener for DC_EVENT_CONFIGURE_PROGRESS instead of checking for 2041 magic number
* emit 'DC_EVENT_LOGIN_FAILED' so ipc can attach event listener to this instead of checking magic number 0
  • Loading branch information
ralphtheninja authored and Jikstra committed Jan 15, 2019
1 parent b6203db commit b9f1532
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 62 deletions.
13 changes: 7 additions & 6 deletions src/main/deltachat.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ class DeltaChatController extends events.EventEmitter {

dc.on('ALL', (event, data1, data2) => {
log.debug('ALL event', { event, data1, data2 }, event)
if (event === 2041) {
this.logCoreEvent('DC_EVENT_CONFIGURE_PROGRESS', data1)
this.emit('DC_EVENT_CONFIGURE_PROGRESS', data1, data2)
if (Number(data1) === 0) { // login failed
this.logout()
}
})

dc.on('DC_EVENT_CONFIGURE_PROGRESS', progress => {
this.logCoreEvent('DC_EVENT_CONFIGURE_PROGRESS', progress)
if (Number(progress) === 0) { // login failed
this.emit('DC_EVENT_LOGIN_FAILED')
this.logout()
}
})

Expand Down
100 changes: 44 additions & 56 deletions src/main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,122 +16,110 @@ const setupNotifications = require('./notifications')
const logHandler = require('./developerTools/logHandler')

function init (cwd, state) {
const ipc = ipcMain
const main = windows.main
const dc = new DeltaChat(cwd, state.saved)

dc.on('ready', function () {
dc.on('ready', () => {
if (!state.logins.includes(dc.credentials.addr)) {
state.logins.push(dc.credentials.addr)
}
})

ipc.once('ipcReady', function (e) {
dc.on('DC_EVENT_IMEX_FILE_WRITTEN', filename => {
main.send('DC_EVENT_IMEX_FILE_WRITTEN', filename)
})

dc.on('DC_EVENT_IMEX_PROGRESS', progress => {
main.send('DC_EVENT_IMEX_PROGRESS', progress)
})

dc.on('error', error => main.send('error', error))

dc.on('DC_EVENT_LOGIN_FAILED', () => main.send('error', 'Login failed!'))

ipcMain.once('ipcReady', e => {
app.ipcReady = true
app.emit('ipcReady')
})

ipc.on('setAspectRatio', (e, ...args) => main.setAspectRatio(...args))
ipc.on('setBounds', (e, ...args) => main.setBounds(...args))
ipc.on('setProgress', (e, ...args) => main.setProgress(...args))
ipc.on('show', () => main.show())
ipc.on('setAllowNav', (e, ...args) => menu.setAllowNav(...args))
ipc.on('chooseLanguage', (e, locale) => {
ipcMain.on('setAspectRatio', (e, ...args) => main.setAspectRatio(...args))
ipcMain.on('setBounds', (e, ...args) => main.setBounds(...args))
ipcMain.on('setProgress', (e, ...args) => main.setProgress(...args))
ipcMain.on('show', () => main.show())
ipcMain.on('setAllowNav', (e, ...args) => menu.setAllowNav(...args))
ipcMain.on('chooseLanguage', (e, locale) => {
localize.setup(app, locale)
dc.setCoreStrings(txCoreStrings())
menu.init()
})

ipc.on('handleLogMessage', (e, ...args) => logHandler.log(...args))
ipcMain.on('handleLogMessage', (e, ...args) => logHandler.log(...args))

setupNotifications(dc, state.saved)

// Create a new instance
ipc.on('login', (e, ...args) => {
ipcMain.on('login', (e, ...args) => {
dc.login(...args, render, txCoreStrings())
})

ipc.on('forgetLogin', (e, addr) => {
ipcMain.on('forgetLogin', (e, addr) => {
var targetDir = dc.getPath(addr)
rimraf.sync(targetDir)
state.logins.splice(state.logins.indexOf(addr), 1)
render()
})

dc.on('DC_EVENT_IMEX_FILE_WRITTEN', (filename) => {
windows.main.send('DC_EVENT_IMEX_FILE_WRITTEN', filename)
})

dc.on('DC_EVENT_IMEX_PROGRESS', (progress) => {
windows.main.send('DC_EVENT_IMEX_PROGRESS', progress)
})

dc.on('error', (error) => {
windows.main.send('error', error)
})

dc.on('DC_EVENT_CONFIGURE_PROGRESS', function (data1) {
if (Number(data1) === 0) { // login failed
windows.main.send('error', 'Login failed!')
}
})

// Calls a function directly in the deltachat-node instance and returns the
// value (sync)
ipc.on('dispatchSync', (e, ...args) => {
ipcMain.on('dispatchSync', (e, ...args) => {
e.returnValue = dispatch(...args)
})

// Calls the function without returning the value (async)
ipc.on('dispatch', (e, ...args) => {
ipcMain.on('dispatch', (e, ...args) => {
dispatch(...args)
})

ipc.on('initiateKeyTransfer', (e, ...args) => {
ipcMain.on('initiateKeyTransfer', (e, ...args) => {
dc.initiateKeyTransfer((err, resp) => {
windows.main.send('initiateKeyTransferResp', err, resp)
main.send('initiateKeyTransferResp', err, resp)
})
})

ipc.on('continueKeyTransfer', (e, messageId, setupCode) => {
dc.continueKeyTransfer(messageId, setupCode, function (err) {
windows.main.send('continueKeyTransferResp', err)
ipcMain.on('continueKeyTransfer', (e, messageId, setupCode) => {
dc.continueKeyTransfer(messageId, setupCode, err => {
main.send('continueKeyTransferResp', err)
})
})

ipc.on('saveFile', (e, source, target) => {
fs.copyFile(source, target, function (err) {
if (err) windows.main.send('error', err.message)
ipcMain.on('saveFile', (e, source, target) => {
fs.copyFile(source, target, err => {
if (err) main.send('error', err.message)
})
})

ipc.on('ondragstart', (event, filePath) => {
ipcMain.on('ondragstart', (event, filePath) => {
event.sender.startDrag({ file: filePath, icon: '' })
})

ipc.on('render', render)
ipcMain.on('render', render)

ipc.on('locale-data', (e, locale) => {
ipcMain.on('locale-data', (e, locale) => {
if (locale) app.localeData = localize.setup(app, locale)
e.returnValue = app.localeData
})

ipc.on('updateSettings', (e, saved) => {
ipcMain.on('updateSettings', (e, saved) => {
dc.updateSettings(saved)
app.saveState()
})

ipc.on('updateCredentials', (e, credentials) => {
var dir = path.join(os.tmpdir(), Date.now().toString())
ipcMain.on('updateCredentials', (e, credentials) => {
const dir = path.join(os.tmpdir(), Date.now().toString())
if (!credentials.mailPw) credentials.mailPw = dc.getConfig('mail_pw')
var tmp = new DeltaChat(dir, state.saved)
const tmp = new DeltaChat(dir, state.saved)

tmp.on('DC_EVENT_CONFIGURE_PROGRESS', function (data1) {
if (Number(data1) === 0) { // login failed
windows.main.send('error', 'Login failed')
tmp.close()
}
})
tmp.on('DC_EVENT_LOGIN_FAILED', () => main.send('error', 'Login failed!'))

function fakeRender () {
const deltachat = dc.render()
Expand All @@ -140,7 +128,7 @@ function init (cwd, state) {
sendState(deltachat)
if (tmpDeltachat.ready) {
dc.login(credentials, render, txCoreStrings())
windows.main.send('success', 'Configuration success!')
main.send('success', 'Configuration success!')
tmp.close()
}
}
Expand All @@ -157,13 +145,13 @@ function init (cwd, state) {
function render () {
log.debug('RENDER')
const deltachat = dc.render()
windows.main.setTitle(deltachat.credentials.addr)
main.setTitle(deltachat.credentials.addr)
sendState(deltachat)
}

function sendState (deltachat) {
Object.assign(state, { deltachat })
windows.main.send('render', state)
main.send('render', state)
}
}

Expand Down

0 comments on commit b9f1532

Please sign in to comment.