Merge pull request #846 from yuya-oc/startup-via-config-json
Startup via config json
This commit is contained in:
@@ -10,7 +10,6 @@ import ReactDOM from 'react-dom';
|
|||||||
import {Button, Checkbox, Col, FormGroup, Grid, HelpBlock, Navbar, Radio, Row} from 'react-bootstrap';
|
import {Button, Checkbox, Col, FormGroup, Grid, HelpBlock, Navbar, Radio, Row} from 'react-bootstrap';
|
||||||
|
|
||||||
import {ipcRenderer, remote} from 'electron';
|
import {ipcRenderer, remote} from 'electron';
|
||||||
import AutoLaunch from 'auto-launch';
|
|
||||||
import {debounce} from 'underscore';
|
import {debounce} from 'underscore';
|
||||||
|
|
||||||
import buildConfig from '../../common/config/buildConfig';
|
import buildConfig from '../../common/config/buildConfig';
|
||||||
@@ -19,11 +18,6 @@ import settings from '../../common/settings';
|
|||||||
import TeamList from './TeamList.jsx';
|
import TeamList from './TeamList.jsx';
|
||||||
import AutoSaveIndicator from './AutoSaveIndicator.jsx';
|
import AutoSaveIndicator from './AutoSaveIndicator.jsx';
|
||||||
|
|
||||||
const appLauncher = new AutoLaunch({
|
|
||||||
name: remote.app.getName(),
|
|
||||||
isHidden: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
function backToIndex(index) {
|
function backToIndex(index) {
|
||||||
const target = typeof index === 'undefined' ? 0 : index;
|
const target = typeof index === 'undefined' ? 0 : index;
|
||||||
const indexURL = remote.getGlobal('isDev') ? 'http://localhost:8080/browser/index.html' : `file://${remote.app.getAppPath()}/browser/index.html`;
|
const indexURL = remote.getGlobal('isDev') ? 'http://localhost:8080/browser/index.html' : `file://${remote.app.getAppPath()}/browser/index.html`;
|
||||||
@@ -58,7 +52,6 @@ export default class SettingsPage extends React.Component {
|
|||||||
this.didSaveConfig = this.didSaveConfig.bind(this);
|
this.didSaveConfig = this.didSaveConfig.bind(this);
|
||||||
this.handleTeamsChange = this.handleTeamsChange.bind(this);
|
this.handleTeamsChange = this.handleTeamsChange.bind(this);
|
||||||
this.saveConfig = this.saveConfig.bind(this);
|
this.saveConfig = this.saveConfig.bind(this);
|
||||||
this.saveAutoStart = this.saveAutoStart.bind(this);
|
|
||||||
this.handleCancel = this.handleCancel.bind(this);
|
this.handleCancel = this.handleCancel.bind(this);
|
||||||
this.handleChangeShowTrayIcon = this.handleChangeShowTrayIcon.bind(this);
|
this.handleChangeShowTrayIcon = this.handleChangeShowTrayIcon.bind(this);
|
||||||
this.handleChangeTrayIconTheme = this.handleChangeTrayIconTheme.bind(this);
|
this.handleChangeTrayIconTheme = this.handleChangeTrayIconTheme.bind(this);
|
||||||
@@ -77,14 +70,6 @@ export default class SettingsPage extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
if (process.platform === 'win32' || process.platform === 'linux') {
|
|
||||||
const self = this;
|
|
||||||
appLauncher.isEnabled().then((enabled) => {
|
|
||||||
self.setState({
|
|
||||||
autostart: enabled,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
ipcRenderer.on('add-server', () => {
|
ipcRenderer.on('add-server', () => {
|
||||||
this.setState({
|
this.setState({
|
||||||
showAddTeamForm: true,
|
showAddTeamForm: true,
|
||||||
@@ -159,6 +144,7 @@ export default class SettingsPage extends React.Component {
|
|||||||
useSpellChecker: this.state.useSpellChecker,
|
useSpellChecker: this.state.useSpellChecker,
|
||||||
spellCheckerLocale: this.state.spellCheckerLocale,
|
spellCheckerLocale: this.state.spellCheckerLocale,
|
||||||
enableHardwareAcceleration: this.state.enableHardwareAcceleration,
|
enableHardwareAcceleration: this.state.enableHardwareAcceleration,
|
||||||
|
autostart: this.state.autostart,
|
||||||
};
|
};
|
||||||
|
|
||||||
settings.writeFile(this.props.configFile, config, (err) => {
|
settings.writeFile(this.props.configFile, config, (err) => {
|
||||||
@@ -168,31 +154,10 @@ export default class SettingsPage extends React.Component {
|
|||||||
}
|
}
|
||||||
ipcRenderer.send('update-menu', config);
|
ipcRenderer.send('update-menu', config);
|
||||||
ipcRenderer.send('update-config');
|
ipcRenderer.send('update-config');
|
||||||
if (process.platform === 'win32' || process.platform === 'linux') {
|
callback();
|
||||||
const autostart = this.state.autostart;
|
|
||||||
this.saveAutoStart(autostart, callback);
|
|
||||||
} else {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
saveAutoStart(autostart, callback) {
|
|
||||||
appLauncher.isEnabled().then((enabled) => {
|
|
||||||
if (enabled && !autostart) {
|
|
||||||
appLauncher.disable().then(() => {
|
|
||||||
callback();
|
|
||||||
}).catch(callback);
|
|
||||||
} else if (!enabled && autostart) {
|
|
||||||
appLauncher.enable().then(() => {
|
|
||||||
callback();
|
|
||||||
}).catch(callback);
|
|
||||||
} else {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}).catch(callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
handleCancel() {
|
handleCancel() {
|
||||||
backToIndex();
|
backToIndex();
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,7 @@ const defaultPreferences = {
|
|||||||
showUnreadBadge: true,
|
showUnreadBadge: true,
|
||||||
useSpellChecker: true,
|
useSpellChecker: true,
|
||||||
enableHardwareAcceleration: true,
|
enableHardwareAcceleration: true,
|
||||||
|
autostart: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default defaultPreferences;
|
export default defaultPreferences;
|
||||||
|
10
src/main.js
10
src/main.js
@@ -24,6 +24,7 @@ import {parse as parseArgv} from 'yargs';
|
|||||||
import {protocols} from '../electron-builder.json';
|
import {protocols} from '../electron-builder.json';
|
||||||
|
|
||||||
import squirrelStartup from './main/squirrelStartup';
|
import squirrelStartup from './main/squirrelStartup';
|
||||||
|
import AutoLauncher from './main/AutoLauncher';
|
||||||
import CriticalErrorHandler from './main/CriticalErrorHandler';
|
import CriticalErrorHandler from './main/CriticalErrorHandler';
|
||||||
|
|
||||||
const criticalErrorHandler = new CriticalErrorHandler();
|
const criticalErrorHandler = new CriticalErrorHandler();
|
||||||
@@ -99,6 +100,15 @@ if (config.enableHardwareAcceleration === false) {
|
|||||||
ipcMain.on('update-config', () => {
|
ipcMain.on('update-config', () => {
|
||||||
const configFile = app.getPath('userData') + '/config.json';
|
const configFile = app.getPath('userData') + '/config.json';
|
||||||
config = settings.readFileSync(configFile);
|
config = settings.readFileSync(configFile);
|
||||||
|
if (process.platform === 'win32' || process.platform === 'linux') {
|
||||||
|
const appLauncher = new AutoLauncher();
|
||||||
|
const autoStartTask = config.autostart ? appLauncher.enable() : appLauncher.disable();
|
||||||
|
autoStartTask.then(() => {
|
||||||
|
console.log('config.autostart has been configured:', config.autostart);
|
||||||
|
}).catch((err) => {
|
||||||
|
console.log('error:', err);
|
||||||
|
});
|
||||||
|
}
|
||||||
const trustedURLs = settings.mergeDefaultTeams(config.teams).map((team) => team.url);
|
const trustedURLs = settings.mergeDefaultTeams(config.teams).map((team) => team.url);
|
||||||
permissionManager.setTrustedURLs(trustedURLs);
|
permissionManager.setTrustedURLs(trustedURLs);
|
||||||
ipcMain.emit('update-dict', true, config.spellCheckerLocale);
|
ipcMain.emit('update-dict', true, config.spellCheckerLocale);
|
||||||
|
48
src/main/AutoLauncher.js
Normal file
48
src/main/AutoLauncher.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
// Copyright (c) 2015-2016 Yuya Ochiai
|
||||||
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See LICENSE.txt for license information.
|
||||||
|
|
||||||
|
import AutoLaunch from 'auto-launch';
|
||||||
|
import {app} from 'electron';
|
||||||
|
import isDev from 'electron-is-dev';
|
||||||
|
|
||||||
|
export default class AutoLauncher {
|
||||||
|
constructor() {
|
||||||
|
this.appLauncher = new AutoLaunch({
|
||||||
|
name: app.getName(),
|
||||||
|
isHidden: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
isEnabled() {
|
||||||
|
return this.appLauncher.isEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
async blankPromise() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async enable() {
|
||||||
|
if (isDev) {
|
||||||
|
console.log('In development mode, autostart config never effects');
|
||||||
|
return this.blankPromise();
|
||||||
|
}
|
||||||
|
const enabled = await this.isEnabled();
|
||||||
|
if (!enabled) {
|
||||||
|
return this.appLauncher.enable();
|
||||||
|
}
|
||||||
|
return this.blankPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
async disable() {
|
||||||
|
if (isDev) {
|
||||||
|
console.log('In development mode, autostart config never effects');
|
||||||
|
return this.blankPromise();
|
||||||
|
}
|
||||||
|
const enabled = await this.isEnabled();
|
||||||
|
if (enabled) {
|
||||||
|
return this.appLauncher.disable();
|
||||||
|
}
|
||||||
|
return this.blankPromise();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user