aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron
diff options
context:
space:
mode:
Diffstat (limited to 'src/electron')
-rw-r--r--src/electron/Settings.js30
-rw-r--r--src/electron/ipc-api/appIndicator.js2
-rw-r--r--src/electron/ipc-api/autoUpdate.js20
-rw-r--r--src/electron/ipc-api/download.js43
-rw-r--r--src/electron/ipc-api/index.js2
-rw-r--r--src/electron/ipc-api/settings.js10
6 files changed, 85 insertions, 22 deletions
diff --git a/src/electron/Settings.js b/src/electron/Settings.js
index b3138e948..6ac3b9177 100644
--- a/src/electron/Settings.js
+++ b/src/electron/Settings.js
@@ -1,15 +1,21 @@
1import { observable, toJS } from 'mobx'; 1import { observable, toJS } from 'mobx';
2import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra'; 2import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra';
3import path from 'path';
3 4
4import { SETTINGS_PATH, DEFAULT_APP_SETTINGS } from '../config'; 5import { SETTINGS_PATH } from '../config';
5 6
6const debug = require('debug')('Settings'); 7const debug = require('debug')('Franz:Settings');
7 8
8export default class Settings { 9export default class Settings {
9 @observable store = DEFAULT_APP_SETTINGS; 10 type = '';
11 @observable store = {};
10 12
11 constructor() { 13 constructor(type, defaultState = {}) {
12 if (!pathExistsSync(SETTINGS_PATH)) { 14 this.type = type;
15 this.store = defaultState;
16 this.defaultState = defaultState;
17
18 if (!pathExistsSync(this.settingsFile)) {
13 this._writeFile(); 19 this._writeFile();
14 } else { 20 } else {
15 this._hydrate(); 21 this._hydrate();
@@ -17,7 +23,7 @@ export default class Settings {
17 } 23 }
18 24
19 set(settings) { 25 set(settings) {
20 this.store = Object.assign(this.store, settings); 26 this.store = this._merge(settings);
21 27
22 this._writeFile(); 28 this._writeFile();
23 } 29 }
@@ -30,13 +36,21 @@ export default class Settings {
30 return this.store[key]; 36 return this.store[key];
31 } 37 }
32 38
39 _merge(settings) {
40 return Object.assign(this.defaultState, this.store, settings);
41 }
42
33 _hydrate() { 43 _hydrate() {
34 this.store = readJsonSync(SETTINGS_PATH); 44 this.store = this._merge(readJsonSync(this.settingsFile));
35 debug('Hydrate store', toJS(this.store)); 45 debug('Hydrate store', toJS(this.store));
36 } 46 }
37 47
38 _writeFile() { 48 _writeFile() {
39 outputJsonSync(SETTINGS_PATH, this.store); 49 outputJsonSync(this.settingsFile, this.store);
40 debug('Write settings file', toJS(this.store)); 50 debug('Write settings file', toJS(this.store));
41 } 51 }
52
53 get settingsFile() {
54 return path.join(SETTINGS_PATH, `${this.type === 'app' ? 'settings' : this.type}.json`);
55 }
42} 56}
diff --git a/src/electron/ipc-api/appIndicator.js b/src/electron/ipc-api/appIndicator.js
index d31819068..e568bf35d 100644
--- a/src/electron/ipc-api/appIndicator.js
+++ b/src/electron/ipc-api/appIndicator.js
@@ -15,7 +15,7 @@ function getAsset(type, asset) {
15 15
16export default (params) => { 16export default (params) => {
17 autorun(() => { 17 autorun(() => {
18 isTrayIconEnabled = params.settings.get('enableSystemTray'); 18 isTrayIconEnabled = params.settings.app.get('enableSystemTray');
19 19
20 if (!isTrayIconEnabled) { 20 if (!isTrayIconEnabled) {
21 params.trayIcon.hide(); 21 params.trayIcon.hide();
diff --git a/src/electron/ipc-api/autoUpdate.js b/src/electron/ipc-api/autoUpdate.js
index ba49a2f97..9ccc89ea2 100644
--- a/src/electron/ipc-api/autoUpdate.js
+++ b/src/electron/ipc-api/autoUpdate.js
@@ -1,17 +1,17 @@
1import { app, ipcMain } from 'electron'; 1import { app, ipcMain } from 'electron';
2import { autoUpdater } from 'electron-updater'; 2import { autoUpdater } from 'electron-updater';
3import { isDevMode } from '../../environment.js'; 3
4const debug = require('debug')('Franz:ipcApi:autoUpdate');
4 5
5export default (params) => { 6export default (params) => {
6 if (!isDevMode && (process.platform === 'darwin' || process.platform === 'win32')) { 7 if (process.platform === 'darwin' || process.platform === 'win32') {
7 // autoUpdater.setFeedURL(updateUrl);
8 ipcMain.on('autoUpdate', (event, args) => { 8 ipcMain.on('autoUpdate', (event, args) => {
9 try { 9 try {
10 autoUpdater.allowPrerelease = Boolean(params.settings.get('beta')); 10 autoUpdater.allowPrerelease = Boolean(params.settings.app.get('beta'));
11 if (args.action === 'check') { 11 if (args.action === 'check') {
12 autoUpdater.checkForUpdates(); 12 autoUpdater.checkForUpdates();
13 } else if (args.action === 'install') { 13 } else if (args.action === 'install') {
14 console.log('install update'); 14 debug('install update');
15 autoUpdater.quitAndInstall(); 15 autoUpdater.quitAndInstall();
16 // we need to send a quit event 16 // we need to send a quit event
17 setTimeout(() => { 17 setTimeout(() => {
@@ -25,12 +25,12 @@ export default (params) => {
25 }); 25 });
26 26
27 autoUpdater.on('update-not-available', () => { 27 autoUpdater.on('update-not-available', () => {
28 console.log('update-not-available'); 28 debug('update-not-available');
29 params.mainWindow.webContents.send('autoUpdate', { available: false }); 29 params.mainWindow.webContents.send('autoUpdate', { available: false });
30 }); 30 });
31 31
32 autoUpdater.on('update-available', () => { 32 autoUpdater.on('update-available', () => {
33 console.log('update-available'); 33 debug('update-available');
34 params.mainWindow.webContents.send('autoUpdate', { available: true }); 34 params.mainWindow.webContents.send('autoUpdate', { available: true });
35 }); 35 });
36 36
@@ -39,16 +39,16 @@ export default (params) => {
39 logMessage = `${logMessage} - Downloaded ${progressObj.percent}%`; 39 logMessage = `${logMessage} - Downloaded ${progressObj.percent}%`;
40 logMessage = `${logMessage} (${progressObj.transferred}/${progressObj.total})`; 40 logMessage = `${logMessage} (${progressObj.transferred}/${progressObj.total})`;
41 41
42 console.log(logMessage); 42 debug(logMessage);
43 }); 43 });
44 44
45 autoUpdater.on('update-downloaded', () => { 45 autoUpdater.on('update-downloaded', () => {
46 console.log('update-downloaded'); 46 debug('update-downloaded');
47 params.mainWindow.webContents.send('autoUpdate', { downloaded: true }); 47 params.mainWindow.webContents.send('autoUpdate', { downloaded: true });
48 }); 48 });
49 49
50 autoUpdater.on('error', () => { 50 autoUpdater.on('error', () => {
51 console.log('update-error'); 51 debug('update-error');
52 params.mainWindow.webContents.send('autoUpdate', { error: true }); 52 params.mainWindow.webContents.send('autoUpdate', { error: true });
53 }); 53 });
54 } 54 }
diff --git a/src/electron/ipc-api/download.js b/src/electron/ipc-api/download.js
new file mode 100644
index 000000000..9e504834d
--- /dev/null
+++ b/src/electron/ipc-api/download.js
@@ -0,0 +1,43 @@
1import { ipcMain, dialog } from 'electron';
2import { download } from 'electron-dl';
3import mime from 'mime-types';
4import fs from 'fs-extra';
5
6const debug = require('debug')('Franz:ipcApi:download');
7
8function decodeBase64Image(dataString) {
9 const matches = dataString.match(/^data:([A-Za-z-+/]+);base64,(.+)$/);
10
11 if (matches.length !== 3) {
12 return new Error('Invalid input string');
13 }
14
15 return new Buffer(matches[2], 'base64');
16}
17
18export default (params) => {
19 ipcMain.on('download-file', async (event, { url, content, fileOptions = {} }) => {
20 try {
21 if (!content) {
22 const dl = await download(params.mainWindow, url, {
23 saveAs: true,
24 });
25 debug('File saved to', dl.getSavePath());
26 } else {
27 const extension = mime.extension(fileOptions.mime);
28 const filename = `${fileOptions.name}.${extension}`;
29
30 dialog.showSaveDialog(params.mainWindow, {
31 defaultPath: filename,
32 }, (name) => {
33 const binaryImage = decodeBase64Image(content);
34 fs.writeFileSync(name, binaryImage, 'binary');
35
36 debug('File blob saved to', name);
37 });
38 }
39 } catch (e) {
40 console.error(e);
41 }
42 });
43};
diff --git a/src/electron/ipc-api/index.js b/src/electron/ipc-api/index.js
index 4ea6d1475..be8e0815a 100644
--- a/src/electron/ipc-api/index.js
+++ b/src/electron/ipc-api/index.js
@@ -1,9 +1,11 @@
1import autoUpdate from './autoUpdate'; 1import autoUpdate from './autoUpdate';
2import settings from './settings'; 2import settings from './settings';
3import appIndicator from './appIndicator'; 3import appIndicator from './appIndicator';
4import download from './download';
4 5
5export default (params) => { 6export default (params) => {
6 settings(params); 7 settings(params);
7 autoUpdate(params); 8 autoUpdate(params);
8 appIndicator(params); 9 appIndicator(params);
10 download(params);
9}; 11};
diff --git a/src/electron/ipc-api/settings.js b/src/electron/ipc-api/settings.js
index 3eab68a91..ce006bb92 100644
--- a/src/electron/ipc-api/settings.js
+++ b/src/electron/ipc-api/settings.js
@@ -1,11 +1,15 @@
1import { ipcMain } from 'electron'; 1import { ipcMain } from 'electron';
2 2
3export default (params) => { 3export default (params) => {
4 ipcMain.on('getAppSettings', () => { 4 ipcMain.on('getAppSettings', (event, type) => {
5 params.mainWindow.webContents.send('appSettings', params.settings.all); 5 console.log('getAppSettings', type, params.settings[type].all);
6 params.mainWindow.webContents.send('appSettings', {
7 type,
8 data: params.settings[type].all,
9 });
6 }); 10 });
7 11
8 ipcMain.on('updateAppSettings', (event, args) => { 12 ipcMain.on('updateAppSettings', (event, args) => {
9 params.settings.set(args); 13 params.settings[args.type].set(args.data);
10 }); 14 });
11}; 15};