2016-11-02 19:49:28 +01:00
|
|
|
/*
|
2016-11-03 18:06:41 +01:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2017-06-04 16:30:44 +02:00
|
|
|
Copyright 2017 Michael Telatynski <7t3chguy@gmail.com>
|
2018-12-18 18:42:55 +01:00
|
|
|
Copyright 2018 New Vector Ltd
|
2016-11-02 19:49:28 +01:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-12-05 15:08:27 +01:00
|
|
|
// Squirrel on windows starts the app with various flags
|
|
|
|
// as hooks to tell us when we've been installed/uninstalled
|
|
|
|
// etc.
|
2017-05-22 20:21:52 +02:00
|
|
|
const checkSquirrelHooks = require('./squirrelhooks');
|
|
|
|
if (checkSquirrelHooks()) return;
|
2016-12-01 12:35:26 +01:00
|
|
|
|
2017-05-26 10:45:30 +02:00
|
|
|
const argv = require('minimist')(process.argv);
|
2018-12-18 18:42:55 +01:00
|
|
|
const {app, ipcMain, powerSaveBlocker, BrowserWindow, Menu, autoUpdater} = require('electron');
|
2017-05-24 16:52:08 +02:00
|
|
|
const AutoLaunch = require('auto-launch');
|
2018-12-18 18:42:55 +01:00
|
|
|
const path = require('path');
|
2016-11-02 19:49:28 +01:00
|
|
|
|
2017-01-19 15:29:07 +01:00
|
|
|
const tray = require('./tray');
|
2017-05-22 20:21:52 +02:00
|
|
|
const vectorMenu = require('./vectormenu');
|
2017-05-25 13:42:53 +02:00
|
|
|
const webContentsHandler = require('./webcontents-handler');
|
2017-06-11 17:43:20 +02:00
|
|
|
const updater = require('./updater');
|
2016-11-02 19:49:28 +01:00
|
|
|
|
2017-04-12 19:13:25 +02:00
|
|
|
const windowStateKeeper = require('electron-window-state');
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
if (argv['profile']) {
|
|
|
|
app.setPath('userData', `${app.getPath('userData')}-${argv['profile']}`);
|
2017-05-26 10:45:30 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 12:07:36 +01:00
|
|
|
let vectorConfig = {};
|
|
|
|
try {
|
2016-11-25 21:09:21 +01:00
|
|
|
vectorConfig = require('../../webapp/config.json');
|
2016-11-08 12:07:36 +01:00
|
|
|
} catch (e) {
|
|
|
|
// it would be nice to check the error code here and bail if the config
|
|
|
|
// is unparseable, but we get MODULE_NOT_FOUND in the case of a missing
|
|
|
|
// file or invalid json, so node is just very unhelpful.
|
|
|
|
// Continue with the defaults (ie. an empty config)
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:49:28 +01:00
|
|
|
let mainWindow = null;
|
2017-06-11 17:43:20 +02:00
|
|
|
global.appQuitting = false;
|
2016-11-02 19:49:28 +01:00
|
|
|
|
2016-11-08 12:07:36 +01:00
|
|
|
|
|
|
|
// handle uncaught errors otherwise it displays
|
|
|
|
// stack traces in popup dialogs, which is terrible (which
|
|
|
|
// it will do any time the auto update poke fails, and there's
|
|
|
|
// no other way to catch this error).
|
|
|
|
// Assuming we generally run from the console when developing,
|
|
|
|
// this is far preferable.
|
2017-05-17 11:39:43 +02:00
|
|
|
process.on('uncaughtException', function(error) {
|
2017-05-22 20:21:52 +02:00
|
|
|
console.log('Unhandled exception', error);
|
2016-11-08 12:07:36 +01:00
|
|
|
});
|
|
|
|
|
2017-05-17 11:39:43 +02:00
|
|
|
let focusHandlerAttached = false;
|
2018-05-26 15:36:25 +02:00
|
|
|
ipcMain.on('setBadgeCount', function(ev, count) {
|
|
|
|
app.setBadgeCount(count);
|
|
|
|
if (count === 0 && mainWindow) {
|
2017-06-01 01:00:00 +02:00
|
|
|
mainWindow.flashFrame(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
ipcMain.on('loudNotification', function() {
|
2017-06-01 01:00:00 +02:00
|
|
|
if (process.platform === 'win32' && mainWindow && !mainWindow.isFocused() && !focusHandlerAttached) {
|
|
|
|
mainWindow.flashFrame(true);
|
|
|
|
mainWindow.once('focus', () => {
|
2017-05-17 11:39:43 +02:00
|
|
|
mainWindow.flashFrame(false);
|
2017-06-01 01:00:00 +02:00
|
|
|
focusHandlerAttached = false;
|
|
|
|
});
|
|
|
|
focusHandlerAttached = true;
|
2017-05-17 11:39:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-22 19:32:39 +02:00
|
|
|
let powerSaveBlockerId;
|
2018-05-26 15:36:25 +02:00
|
|
|
ipcMain.on('app_onAction', function(ev, payload) {
|
2017-05-22 19:32:39 +02:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'call_state':
|
2018-05-26 15:36:25 +02:00
|
|
|
if (powerSaveBlockerId && powerSaveBlocker.isStarted(powerSaveBlockerId)) {
|
2017-05-22 19:32:39 +02:00
|
|
|
if (payload.state === 'ended') {
|
2018-05-26 15:36:25 +02:00
|
|
|
powerSaveBlocker.stop(powerSaveBlockerId);
|
2017-05-22 19:32:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (payload.state === 'connected') {
|
2018-05-26 15:36:25 +02:00
|
|
|
powerSaveBlockerId = powerSaveBlocker.start('prevent-display-sleep');
|
2017-05-22 19:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-12-18 18:42:55 +01:00
|
|
|
autoUpdater.on('update-downloaded', (ev, releaseNotes, releaseName, releaseDate, updateURL) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
// forward to renderer
|
|
|
|
mainWindow.webContents.send('update-downloaded', {
|
|
|
|
releaseNotes,
|
|
|
|
releaseName,
|
|
|
|
releaseDate,
|
|
|
|
updateURL,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.on('ipcCall', function(ev, payload) {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
|
|
|
|
const args = payload.args || [];
|
|
|
|
let ret;
|
|
|
|
|
|
|
|
switch (payload.name) {
|
|
|
|
case 'getUpdateFeedUrl':
|
|
|
|
ret = autoUpdater.getFeedURL();
|
|
|
|
break;
|
|
|
|
case 'getAutoLaunchEnabled':
|
|
|
|
ret = launcher.isEnabled;
|
|
|
|
break;
|
|
|
|
case 'setAutoLaunchEnabled':
|
|
|
|
if (args[0]) {
|
|
|
|
launcher.enable();
|
|
|
|
} else {
|
|
|
|
launcher.disable();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'getAppVersion':
|
|
|
|
ret = app.getVersion();
|
|
|
|
break;
|
|
|
|
case 'focusWindow':
|
|
|
|
if (mainWindow.isMinimized()) {
|
|
|
|
mainWindow.restore();
|
|
|
|
} else if (!mainWindow.isVisible()) {
|
|
|
|
mainWindow.show();
|
|
|
|
} else {
|
|
|
|
mainWindow.focus();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
mainWindow.webContents.send('ipcReply', {
|
|
|
|
id: payload.id,
|
|
|
|
error: new Error("Unknown IPC Call: "+payload.name),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mainWindow.webContents.send('ipcReply', {
|
|
|
|
id: payload.id,
|
|
|
|
reply: ret,
|
|
|
|
});
|
|
|
|
});
|
2017-05-22 19:32:39 +02:00
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
app.commandLine.appendSwitch('--enable-usermedia-screen-capturing');
|
2017-01-10 19:39:21 +01:00
|
|
|
|
2018-12-18 19:10:09 +01:00
|
|
|
const gotLock = app.requestSingleInstanceLock();
|
|
|
|
if (!gotLock) {
|
2017-05-22 20:21:52 +02:00
|
|
|
console.log('Other instance detected: exiting');
|
2018-05-26 15:36:25 +02:00
|
|
|
app.exit();
|
2017-01-19 17:01:37 +01:00
|
|
|
}
|
|
|
|
|
2017-05-24 16:52:08 +02:00
|
|
|
const launcher = new AutoLaunch({
|
|
|
|
name: vectorConfig.brand || 'Riot',
|
|
|
|
isHidden: true,
|
2017-05-30 11:53:01 +02:00
|
|
|
mac: {
|
|
|
|
useLaunchAgent: true,
|
|
|
|
},
|
2017-05-24 16:52:08 +02:00
|
|
|
});
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
app.on('ready', () => {
|
|
|
|
if (argv['devtools']) {
|
2017-06-04 16:30:44 +02:00
|
|
|
try {
|
2018-05-26 15:00:45 +02:00
|
|
|
const { default: installExt, REACT_DEVELOPER_TOOLS, REACT_PERF } = require('electron-devtools-installer');
|
|
|
|
installExt(REACT_DEVELOPER_TOOLS)
|
2017-06-04 16:30:44 +02:00
|
|
|
.then((name) => console.log(`Added Extension: ${name}`))
|
|
|
|
.catch((err) => console.log('An error occurred: ', err));
|
2018-05-26 15:00:45 +02:00
|
|
|
installExt(REACT_PERF)
|
2017-06-04 16:30:44 +02:00
|
|
|
.then((name) => console.log(`Added Extension: ${name}`))
|
|
|
|
.catch((err) => console.log('An error occurred: ', err));
|
2018-05-26 15:00:45 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2017-06-04 16:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
if (vectorConfig['update_base_url']) {
|
|
|
|
console.log(`Starting auto update with base URL: ${vectorConfig['update_base_url']}`);
|
|
|
|
updater.start(vectorConfig['update_base_url']);
|
2016-12-05 15:08:27 +01:00
|
|
|
} else {
|
2017-05-22 20:21:52 +02:00
|
|
|
console.log('No update_base_url is defined: auto update is disabled');
|
2016-12-05 15:08:27 +01:00
|
|
|
}
|
2016-11-02 19:49:28 +01:00
|
|
|
|
2017-05-22 20:21:52 +02:00
|
|
|
const iconPath = `${__dirname}/../img/riot.${process.platform === 'win32' ? 'ico' : 'png'}`;
|
2016-12-07 12:19:51 +01:00
|
|
|
|
2017-04-12 19:13:25 +02:00
|
|
|
// Load the previous window state with fallback to defaults
|
2017-05-22 20:21:52 +02:00
|
|
|
const mainWindowState = windowStateKeeper({
|
2017-04-12 19:13:25 +02:00
|
|
|
defaultWidth: 1024,
|
|
|
|
defaultHeight: 768,
|
|
|
|
});
|
|
|
|
|
2018-12-18 18:42:55 +01:00
|
|
|
const preloadScript = path.normalize(`${__dirname}/preload.js`);
|
2018-05-26 15:36:25 +02:00
|
|
|
mainWindow = global.mainWindow = new BrowserWindow({
|
2017-05-22 20:21:52 +02:00
|
|
|
icon: iconPath,
|
2016-12-06 10:45:31 +01:00
|
|
|
show: false,
|
2017-01-18 11:39:59 +01:00
|
|
|
autoHideMenuBar: true,
|
2017-04-12 19:13:25 +02:00
|
|
|
|
|
|
|
x: mainWindowState.x,
|
|
|
|
y: mainWindowState.y,
|
|
|
|
width: mainWindowState.width,
|
|
|
|
height: mainWindowState.height,
|
2018-12-18 18:42:55 +01:00
|
|
|
webPreferences: {
|
|
|
|
preload: preloadScript,
|
|
|
|
nodeIntegration: false,
|
|
|
|
sandbox: true,
|
|
|
|
enableRemoteModule: false,
|
|
|
|
// We don't use this: it's useful for the preload script to
|
|
|
|
// share a context with the main page so we can give select
|
|
|
|
// objects to the main page. The sandbox option isolates the
|
|
|
|
// main page from the background script.
|
|
|
|
contextIsolation: false,
|
|
|
|
webgl: false,
|
|
|
|
},
|
2016-11-02 19:49:28 +01:00
|
|
|
});
|
2016-11-08 16:46:21 +01:00
|
|
|
mainWindow.loadURL(`file://${__dirname}/../../webapp/index.html`);
|
2018-05-26 15:36:25 +02:00
|
|
|
Menu.setApplicationMenu(vectorMenu);
|
2016-11-02 19:49:28 +01:00
|
|
|
|
2017-05-30 13:33:32 +02:00
|
|
|
// explicitly hide because setApplicationMenu on Linux otherwise shows...
|
|
|
|
// https://github.com/electron/electron/issues/9621
|
|
|
|
mainWindow.hide();
|
|
|
|
|
2017-01-19 15:29:07 +01:00
|
|
|
// Create trayIcon icon
|
2017-06-12 14:47:29 +02:00
|
|
|
tray.create({
|
2017-05-22 20:21:52 +02:00
|
|
|
icon_path: iconPath,
|
|
|
|
brand: vectorConfig.brand || 'Riot',
|
2017-01-19 15:29:07 +01:00
|
|
|
});
|
|
|
|
|
2018-05-26 15:21:38 +02:00
|
|
|
mainWindow.once('ready-to-show', () => {
|
|
|
|
mainWindowState.manage(mainWindow);
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
if (!argv['hidden']) {
|
2017-04-17 23:10:56 +02:00
|
|
|
mainWindow.show();
|
2018-05-26 15:21:38 +02:00
|
|
|
} else {
|
|
|
|
// hide here explicitly because window manage above sometimes shows it
|
|
|
|
mainWindow.hide();
|
|
|
|
}
|
|
|
|
});
|
2017-04-17 23:10:56 +02:00
|
|
|
|
2016-11-02 19:49:28 +01:00
|
|
|
mainWindow.on('closed', () => {
|
2017-06-12 14:47:29 +02:00
|
|
|
mainWindow = global.mainWindow = null;
|
2016-11-02 19:49:28 +01:00
|
|
|
});
|
|
|
|
mainWindow.on('close', (e) => {
|
2017-06-11 17:43:20 +02:00
|
|
|
if (!global.appQuitting && (tray.hasTray() || process.platform === 'darwin')) {
|
2016-11-02 19:49:28 +01:00
|
|
|
// On Mac, closing the window just hides it
|
|
|
|
// (this is generally how single-window Mac apps
|
|
|
|
// behave, eg. Mail.app)
|
|
|
|
e.preventDefault();
|
|
|
|
mainWindow.hide();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-03 16:03:50 +02:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
// Handle forward/backward mouse buttons in Windows
|
|
|
|
mainWindow.on('app-command', (e, cmd) => {
|
|
|
|
if (cmd === 'browser-backward' && mainWindow.webContents.canGoBack()) {
|
|
|
|
mainWindow.webContents.goBack();
|
|
|
|
} else if (cmd === 'browser-forward' && mainWindow.webContents.canGoForward()) {
|
|
|
|
mainWindow.webContents.goForward();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-25 13:42:53 +02:00
|
|
|
webContentsHandler(mainWindow.webContents);
|
2016-11-02 19:49:28 +01:00
|
|
|
});
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
app.quit();
|
2016-11-02 19:49:28 +01:00
|
|
|
});
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
app.on('activate', () => {
|
2016-11-02 19:49:28 +01:00
|
|
|
mainWindow.show();
|
|
|
|
});
|
|
|
|
|
2018-05-26 15:36:25 +02:00
|
|
|
app.on('before-quit', () => {
|
2017-06-11 17:43:20 +02:00
|
|
|
global.appQuitting = true;
|
2017-06-22 19:25:19 +02:00
|
|
|
if (mainWindow) {
|
|
|
|
mainWindow.webContents.send('before-quit');
|
|
|
|
}
|
2016-11-02 19:49:28 +01:00
|
|
|
});
|
2016-12-06 14:28:59 +01:00
|
|
|
|
2018-12-18 19:10:09 +01:00
|
|
|
app.on('second-instance', (ev, commandLine, workingDirectory) => {
|
|
|
|
// If other instance launched with --hidden then skip showing window
|
|
|
|
if (commandLine.includes('--hidden')) return;
|
|
|
|
|
|
|
|
// Someone tried to run a second instance, we should focus our window.
|
|
|
|
if (mainWindow) {
|
|
|
|
if (!mainWindow.isVisible()) mainWindow.show();
|
|
|
|
if (mainWindow.isMinimized()) mainWindow.restore();
|
|
|
|
mainWindow.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-06 14:28:59 +01:00
|
|
|
// Set the App User Model ID to match what the squirrel
|
|
|
|
// installer uses for the shortcut icon.
|
|
|
|
// This makes notifications work on windows 8.1 (and is
|
|
|
|
// a noop on other platforms).
|
2018-05-26 15:36:25 +02:00
|
|
|
app.setAppUserModelId('com.squirrel.riot-web.Riot');
|