2017-12-21 18:32:51 +01:00
|
|
|
/* eslint-disable no-console */
|
2017-11-17 21:35:37 +01:00
|
|
|
// Module imports
|
2017-12-21 18:32:51 +01:00
|
|
|
import Path from 'path';
|
|
|
|
import Url from 'url';
|
|
|
|
import Jayson from 'jayson';
|
|
|
|
import Semver from 'semver';
|
|
|
|
import Https from 'https';
|
|
|
|
import Keytar from 'keytar';
|
|
|
|
import ChildProcess from 'child_process';
|
|
|
|
import Assert from 'assert';
|
|
|
|
import { app, BrowserWindow, globalShortcut, ipcMain, Menu, Tray } from 'electron';
|
2017-11-18 06:08:44 +01:00
|
|
|
import { autoUpdater } from 'electron-updater';
|
2017-11-27 09:13:33 +01:00
|
|
|
import log from 'electron-log';
|
2017-12-21 18:32:51 +01:00
|
|
|
import mainMenu from './menu/mainMenu';
|
2018-01-05 23:25:33 +01:00
|
|
|
import contextMenu from './menu/contextMenu';
|
2017-12-13 22:36:30 +01:00
|
|
|
|
2017-12-04 21:46:51 +01:00
|
|
|
const localVersion = app.getVersion();
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-11-17 21:35:37 +01:00
|
|
|
// Debug configs
|
2017-12-21 18:32:51 +01:00
|
|
|
const isDevelopment = process.env.NODE_ENV === 'development';
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-11-27 09:13:33 +01:00
|
|
|
// For now, log info messages in production for easier debugging of built apps
|
|
|
|
log.transports.file.level = 'info';
|
|
|
|
|
2017-11-17 21:35:37 +01:00
|
|
|
// Misc constants
|
2017-12-21 18:32:51 +01:00
|
|
|
const LATEST_RELEASE_API_URL = 'https://api.github.com/repos/lbryio/lbry-app/releases/latest';
|
|
|
|
const DAEMON_PATH = process.env.LBRY_DAEMON || Path.join(__static, 'daemon/lbrynet-daemon');
|
2017-12-06 17:32:21 +01:00
|
|
|
const rendererUrl = isDevelopment
|
2017-12-05 02:08:08 +01:00
|
|
|
? `http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`
|
2017-12-06 17:32:21 +01:00
|
|
|
: `file://${__dirname}/index.html`;
|
2017-01-16 20:06:53 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
const client = Jayson.client.http({
|
|
|
|
host: 'localhost',
|
2017-06-03 10:04:58 +02:00
|
|
|
port: 5279,
|
2017-12-21 18:32:51 +01:00
|
|
|
path: '/',
|
2017-12-13 22:36:30 +01:00
|
|
|
timeout: 1000,
|
2017-06-03 10:04:58 +02:00
|
|
|
});
|
|
|
|
|
2017-01-16 20:06:53 +01:00
|
|
|
// Keep a global reference of the window object, if you don't, the window will
|
|
|
|
// be closed automatically when the JavaScript object is garbage collected.
|
2017-12-21 18:32:51 +01:00
|
|
|
let rendererWindow;
|
2017-01-16 20:06:53 +01:00
|
|
|
// Also keep the daemon subprocess alive
|
2017-03-24 00:07:08 +01:00
|
|
|
let daemonSubprocess;
|
2017-01-16 20:06:53 +01:00
|
|
|
|
2017-03-24 08:04:30 +01:00
|
|
|
// This is set to true right before we try to shut the daemon subprocess --
|
|
|
|
// if it dies when we didn't ask it to shut down, we want to alert the user.
|
2017-04-13 23:30:53 +02:00
|
|
|
let daemonStopRequested = false;
|
2017-03-24 00:07:08 +01:00
|
|
|
|
|
|
|
// When a quit is attempted, we cancel the quit, do some preparations, then
|
|
|
|
// this is set to true and app.quit() is called again to quit for real.
|
|
|
|
let readyToQuit = false;
|
2017-01-16 20:06:53 +01:00
|
|
|
|
2017-05-08 11:04:11 +02:00
|
|
|
// If we receive a URI to open from an external app but there's no window to
|
2017-08-17 22:19:29 +02:00
|
|
|
// sendCredits it to, it's cached in this variable.
|
2017-05-08 11:04:11 +02:00
|
|
|
let openUri = null;
|
|
|
|
|
2017-09-14 09:36:41 +02:00
|
|
|
// Set this to true to minimize on clicking close
|
|
|
|
// false for normal action
|
|
|
|
let minimize = true;
|
|
|
|
|
|
|
|
// Keep the tray also, it is getting GC'd if put in createTray()
|
|
|
|
let tray = null;
|
|
|
|
|
2017-06-28 09:25:02 +02:00
|
|
|
function processRequestedUri(uri) {
|
|
|
|
// Windows normalizes URIs when they're passed in from other apps. On Windows,
|
|
|
|
// this function tries to restore the original URI that was typed.
|
2017-05-11 05:40:07 +02:00
|
|
|
// - If the URI has no path, Windows adds a trailing slash. LBRY URIs
|
|
|
|
// can't have a slash with no path, so we just strip it off.
|
|
|
|
// - In a URI with a claim ID, like lbry://channel#claimid, Windows
|
|
|
|
// interprets the hash mark as an anchor and converts it to
|
|
|
|
// lbry://channel/#claimid. We remove the slash here as well.
|
2017-06-28 09:25:02 +02:00
|
|
|
// On Linux and Mac, we just return the URI as given.
|
2017-05-11 05:40:07 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
return uri.replace(/\/$/, '').replace('/#', '#');
|
2017-05-11 05:40:07 +02:00
|
|
|
}
|
2017-12-13 22:36:30 +01:00
|
|
|
return uri;
|
2017-05-11 05:40:07 +02:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:08:14 +01:00
|
|
|
/*
|
|
|
|
* Replacement for Electron's shell.openItem. The Electron version doesn't
|
|
|
|
* reliably work from the main process, and we need to be able to run it
|
|
|
|
* when no windows are open.
|
|
|
|
*/
|
|
|
|
function openItem(fullPath) {
|
2017-12-13 22:36:30 +01:00
|
|
|
const subprocOptions = {
|
|
|
|
detached: true,
|
2017-12-21 18:32:51 +01:00
|
|
|
stdio: 'ignore',
|
2017-12-13 22:36:30 +01:00
|
|
|
};
|
2017-03-23 19:08:14 +01:00
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
let child;
|
2017-12-21 18:32:51 +01:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
child = ChildProcess.spawn('open', [fullPath], subprocOptions);
|
|
|
|
} else if (process.platform === 'linux') {
|
|
|
|
child = ChildProcess.spawn('xdg-open', [fullPath], subprocOptions);
|
|
|
|
} else if (process.platform === 'win32') {
|
|
|
|
child = ChildProcess.spawn(fullPath, Object.assign({}, subprocOptions, { shell: true }));
|
2017-12-13 22:36:30 +01:00
|
|
|
}
|
2017-03-23 19:08:14 +01:00
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
// Causes child process reference to be garbage collected, allowing main process to exit
|
|
|
|
child.unref();
|
2017-03-23 19:08:14 +01:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
/*
|
|
|
|
* Quits by first killing the daemon, the calling quitting for real.
|
|
|
|
*/
|
|
|
|
export function safeQuit() {
|
|
|
|
minimize = false;
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMenuTemplate() {
|
|
|
|
function getToggleItem() {
|
|
|
|
if (rendererWindow.isVisible() && rendererWindow.isFocused()) {
|
|
|
|
return {
|
|
|
|
label: 'Hide LBRY App',
|
|
|
|
click: () => rendererWindow.hide(),
|
|
|
|
};
|
2017-03-26 13:02:28 +02:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
return {
|
|
|
|
label: 'Show LBRY App',
|
|
|
|
click: () => rendererWindow.show(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
getToggleItem(),
|
|
|
|
{
|
|
|
|
label: 'Quit',
|
|
|
|
click: () => safeQuit(),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// This needs to be done as for linux the context menu doesn't update automatically(docs)
|
|
|
|
function updateTray() {
|
|
|
|
const trayContextMenu = Menu.buildFromTemplate(getMenuTemplate());
|
|
|
|
if (tray) {
|
|
|
|
tray.setContextMenu(trayContextMenu);
|
|
|
|
} else {
|
|
|
|
console.log('How did update tray get called without a tray?');
|
2017-03-24 08:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-20 19:59:03 +01:00
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
function createWindow() {
|
2017-12-06 17:32:21 +01:00
|
|
|
// Disable renderer process's webSecurity on development to enable CORS.
|
2017-12-21 18:32:51 +01:00
|
|
|
let windowConfiguration = {
|
|
|
|
backgroundColor: '#155B4A',
|
|
|
|
minWidth: 800,
|
|
|
|
minHeight: 600,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
windowConfiguration = isDevelopment
|
|
|
|
? {
|
|
|
|
...windowConfiguration,
|
|
|
|
webPreferences: {
|
|
|
|
webSecurity: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
: windowConfiguration;
|
|
|
|
|
|
|
|
let window = new BrowserWindow(windowConfiguration);
|
2017-05-08 11:22:38 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.webContents.session.setUserAgent(`LBRY/${localVersion}`);
|
2017-12-07 21:04:08 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.maximize();
|
2017-12-06 17:32:21 +01:00
|
|
|
if (isDevelopment) {
|
2017-12-21 18:32:51 +01:00
|
|
|
window.webContents.openDevTools();
|
2017-05-21 20:07:02 +02:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
window.loadURL(rendererUrl);
|
2017-12-13 22:36:30 +01:00
|
|
|
if (openUri) {
|
|
|
|
// We stored and received a URI that an external app requested before we had a window object
|
2017-12-21 18:32:51 +01:00
|
|
|
window.webContents.on('did-finish-load', () => {
|
2017-12-26 14:25:26 +01:00
|
|
|
window.webContents.send('open-uri-requested', openUri, true);
|
2017-05-08 11:04:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.removeAllListeners();
|
2017-09-14 09:36:41 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.on('close', event => {
|
2017-09-14 09:36:41 +02:00
|
|
|
if (minimize) {
|
|
|
|
event.preventDefault();
|
2017-12-21 18:32:51 +01:00
|
|
|
window.hide();
|
2017-09-14 09:36:41 +02:00
|
|
|
}
|
2017-12-13 22:36:30 +01:00
|
|
|
});
|
2017-09-14 09:36:41 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.on('closed', () => {
|
|
|
|
window = null;
|
2017-12-13 22:36:30 +01:00
|
|
|
});
|
2017-10-19 07:58:19 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.on('hide', () => {
|
2017-09-14 09:36:41 +02:00
|
|
|
// Checks what to show in the tray icon menu
|
|
|
|
if (minimize) updateTray();
|
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.on('show', () => {
|
2017-09-14 09:36:41 +02:00
|
|
|
// Checks what to show in the tray icon menu
|
|
|
|
if (minimize) updateTray();
|
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.on('blur', () => {
|
2017-09-14 09:36:41 +02:00
|
|
|
// Checks what to show in the tray icon menu
|
|
|
|
if (minimize) updateTray();
|
|
|
|
|
|
|
|
// Unregisters Alt+F4 shortcut
|
2017-12-21 18:32:51 +01:00
|
|
|
globalShortcut.unregister('Alt+F4');
|
2017-09-14 09:36:41 +02:00
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.on('focus', () => {
|
2017-09-14 09:36:41 +02:00
|
|
|
// Checks what to show in the tray icon menu
|
|
|
|
if (minimize) updateTray();
|
|
|
|
|
|
|
|
// Registers shortcut for closing(quitting) the app
|
2017-12-21 18:32:51 +01:00
|
|
|
globalShortcut.register('Alt+F4', () => safeQuit());
|
2017-09-14 09:36:41 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
window.webContents.send('window-is-focused', null);
|
2017-09-14 09:36:41 +02:00
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
mainMenu();
|
|
|
|
|
|
|
|
return window;
|
2017-12-13 22:36:30 +01:00
|
|
|
}
|
2017-10-19 07:58:19 +02:00
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
function createTray() {
|
2017-09-14 09:36:41 +02:00
|
|
|
// Minimize to tray logic follows:
|
|
|
|
// Set the tray icon
|
2017-11-21 07:12:09 +01:00
|
|
|
let iconPath;
|
2017-12-21 18:32:51 +01:00
|
|
|
if (process.platform === 'darwin') {
|
2017-11-21 07:12:09 +01:00
|
|
|
// Using @2x for mac retina screens so the icon isn't blurry
|
|
|
|
// file name needs to include "Template" at the end for dark menu bar
|
2017-12-21 18:32:51 +01:00
|
|
|
iconPath = Path.join(__static, '/img/fav/macTemplate@2x.png');
|
2017-11-21 07:12:09 +01:00
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
iconPath = Path.join(__static, '/img/fav/32x32.png');
|
2017-11-21 07:12:09 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 09:36:41 +02:00
|
|
|
tray = new Tray(iconPath);
|
2017-12-21 18:32:51 +01:00
|
|
|
tray.setToolTip('LBRY App');
|
|
|
|
tray.setTitle('LBRY');
|
|
|
|
tray.on('double-click', () => {
|
|
|
|
rendererWindow.show();
|
2017-12-13 22:36:30 +01:00
|
|
|
});
|
2017-09-14 09:36:41 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 09:25:02 +02:00
|
|
|
function handleOpenUriRequested(uri) {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (!rendererWindow) {
|
2017-06-28 09:25:02 +02:00
|
|
|
// Window not created yet, so store up requested URI for when it is
|
2017-06-29 06:56:59 +02:00
|
|
|
openUri = processRequestedUri(uri);
|
2017-06-28 09:25:02 +02:00
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (rendererWindow.isMinimized()) {
|
|
|
|
rendererWindow.restore();
|
|
|
|
} else if (!rendererWindow.isVisible()) {
|
|
|
|
rendererWindow.show();
|
2017-06-28 09:25:02 +02:00
|
|
|
}
|
2017-09-14 09:36:41 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
rendererWindow.focus();
|
|
|
|
rendererWindow.webContents.send('open-uri-requested', processRequestedUri(uri));
|
2017-06-28 09:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
/*
|
|
|
|
* Quits without any preparation. When a quit is requested (either through the
|
|
|
|
* interface or through app.quit()), we abort the quit, try to shut down the daemon,
|
|
|
|
* and then call this to quit for real.
|
|
|
|
*/
|
|
|
|
function quitNow() {
|
|
|
|
readyToQuit = true;
|
|
|
|
safeQuit();
|
|
|
|
}
|
|
|
|
|
2017-03-24 00:07:08 +01:00
|
|
|
function handleDaemonSubprocessExited() {
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('The daemon has exited.');
|
2017-03-24 00:07:08 +01:00
|
|
|
daemonSubprocess = null;
|
2017-04-13 23:30:53 +02:00
|
|
|
if (!daemonStopRequested) {
|
|
|
|
// We didn't request to stop the daemon, so display a
|
2017-03-24 00:07:08 +01:00
|
|
|
// warning and schedule a quit.
|
|
|
|
//
|
|
|
|
// TODO: maybe it would be better to restart the daemon?
|
2017-12-21 18:32:51 +01:00
|
|
|
if (rendererWindow) {
|
|
|
|
console.log('Did not request daemon stop, so quitting in 5 seconds.');
|
|
|
|
rendererWindow.loadURL(`file://${__static}/warning.html`);
|
2017-03-24 08:04:30 +01:00
|
|
|
setTimeout(quitNow, 5000);
|
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('Did not request daemon stop, so quitting.');
|
2017-03-24 08:04:30 +01:00
|
|
|
quitNow();
|
2017-03-24 00:07:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 10:06:02 +01:00
|
|
|
function launchDaemon() {
|
2017-12-21 18:32:51 +01:00
|
|
|
Assert(!daemonSubprocess, 'Tried to launch daemon twice');
|
2017-03-24 00:07:08 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('Launching daemon:', DAEMON_PATH);
|
|
|
|
daemonSubprocess = ChildProcess.spawn(DAEMON_PATH);
|
2017-02-10 20:11:26 +01:00
|
|
|
// Need to handle the data event instead of attaching to
|
|
|
|
// process.stdout because the latter doesn't work. I believe on
|
|
|
|
// windows it buffers stdout and we don't get any meaningful output
|
2017-12-21 18:32:51 +01:00
|
|
|
daemonSubprocess.stdout.on('data', buf => {
|
2017-12-13 22:36:30 +01:00
|
|
|
console.log(String(buf).trim());
|
|
|
|
});
|
2017-12-21 18:32:51 +01:00
|
|
|
daemonSubprocess.stderr.on('data', buf => {
|
2017-12-13 22:36:30 +01:00
|
|
|
console.log(String(buf).trim());
|
|
|
|
});
|
2017-12-21 18:32:51 +01:00
|
|
|
daemonSubprocess.on('exit', handleDaemonSubprocessExited);
|
2017-03-24 00:07:08 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
const isSecondaryInstance = app.makeSingleInstance(argv => {
|
2017-10-13 00:04:31 +02:00
|
|
|
if (argv.length >= 2) {
|
|
|
|
handleOpenUriRequested(argv[1]); // This will handle restoring and focusing the window
|
2017-12-21 18:32:51 +01:00
|
|
|
} else if (rendererWindow) {
|
|
|
|
if (rendererWindow.isMinimized()) {
|
|
|
|
rendererWindow.restore();
|
|
|
|
} else if (!rendererWindow.isVisible()) {
|
|
|
|
rendererWindow.show();
|
2017-05-11 06:03:39 +02:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
rendererWindow.focus();
|
2017-05-11 09:30:23 +02:00
|
|
|
}
|
2017-10-13 00:04:31 +02:00
|
|
|
});
|
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
if (isSecondaryInstance) {
|
|
|
|
// We're not in the original process, so quit
|
2017-10-13 00:04:31 +02:00
|
|
|
quitNow();
|
2017-05-10 10:44:06 +02:00
|
|
|
}
|
2017-02-23 19:46:25 +01:00
|
|
|
|
|
|
|
function launchDaemonIfNotRunning() {
|
2017-02-23 20:01:23 +01:00
|
|
|
// Check if the daemon is already running. If we get
|
|
|
|
// an error its because its not running
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('Checking for lbrynet daemon');
|
|
|
|
client.request('status', [], err => {
|
2017-12-13 22:36:30 +01:00
|
|
|
if (err) {
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('lbrynet daemon needs to be launched');
|
2017-12-13 22:36:30 +01:00
|
|
|
launchDaemon();
|
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('lbrynet daemon is already running');
|
2017-02-23 20:01:23 +01:00
|
|
|
}
|
2017-12-13 22:36:30 +01:00
|
|
|
});
|
2017-02-23 19:46:25 +01:00
|
|
|
}
|
2017-01-16 20:06:53 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
// Taken from webtorrent-desktop
|
|
|
|
function checkLinuxTraySupport(cb) {
|
|
|
|
// Check that we're on Ubuntu (or another debian system) and that we have
|
|
|
|
// libappindicator1.
|
|
|
|
ChildProcess.exec('dpkg --get-selections libappindicator1', (err, stdout) => {
|
|
|
|
if (err) return cb(err);
|
|
|
|
// Unfortunately there's no cleaner way, as far as I can tell, to check
|
|
|
|
// whether a debian package is installed:
|
|
|
|
if (stdout.endsWith('\tinstall\n')) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
return cb(new Error('debian package not installed'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a quit is attempted, this is called. It attempts to shutdown the daemon,
|
|
|
|
// then calls quitNow() to quit for real.
|
|
|
|
function shutdownDaemonAndQuit(evenIfNotStartedByApp = false) {
|
|
|
|
function doShutdown() {
|
|
|
|
console.log('Shutting down daemon');
|
|
|
|
daemonStopRequested = true;
|
|
|
|
client.request('daemon_stop', [], err => {
|
|
|
|
if (err) {
|
|
|
|
console.log(`received error when stopping lbrynet-daemon. Error message: ${err.message}\n`);
|
|
|
|
console.log('You will need to manually kill the daemon.');
|
|
|
|
} else {
|
|
|
|
console.log('Successfully stopped daemon via RPC call.');
|
|
|
|
quitNow();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-22 11:47:44 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
if (daemonSubprocess) {
|
|
|
|
doShutdown();
|
|
|
|
} else if (!evenIfNotStartedByApp) {
|
|
|
|
console.log('Not killing lbrynet-daemon because app did not start it');
|
2017-03-24 00:07:08 +01:00
|
|
|
quitNow();
|
2017-03-22 11:47:44 +01:00
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
doShutdown();
|
2017-03-22 11:47:44 +01:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
|
|
|
|
// Is it safe to start the installer before the daemon finishes running?
|
|
|
|
// If not, we should wait until the daemon is closed before we start the install.
|
2017-03-22 11:47:44 +01:00
|
|
|
}
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
if (isDevelopment) {
|
|
|
|
import('devtron')
|
|
|
|
.then(({ install }) => {
|
|
|
|
install();
|
|
|
|
console.log('Added Extension: Devtron');
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
import('electron-devtools-installer')
|
|
|
|
.then(({ default: installExtension, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS }) => {
|
|
|
|
app.on('ready', () => {
|
|
|
|
[REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS].forEach(extension => {
|
|
|
|
installExtension(extension)
|
|
|
|
.then(name => console.log(`Added Extension: ${name}`))
|
|
|
|
.catch(err => console.log('An error occurred: ', err));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
}
|
2017-12-04 21:46:51 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
app.setAsDefaultProtocolClient('lbry');
|
|
|
|
|
|
|
|
app.on('ready', () => {
|
2017-12-04 21:46:51 +01:00
|
|
|
launchDaemonIfNotRunning();
|
2017-12-21 18:32:51 +01:00
|
|
|
if (process.platform === 'linux') {
|
2017-12-13 22:36:30 +01:00
|
|
|
checkLinuxTraySupport(err => {
|
2017-12-04 21:46:51 +01:00
|
|
|
if (!err) createTray();
|
|
|
|
else minimize = false;
|
2017-12-13 22:36:30 +01:00
|
|
|
});
|
2017-12-04 21:46:51 +01:00
|
|
|
} else {
|
|
|
|
createTray();
|
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
rendererWindow = createWindow();
|
2017-12-04 21:46:51 +01:00
|
|
|
});
|
2017-01-16 20:06:53 +01:00
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
2017-12-21 18:32:51 +01:00
|
|
|
app.on('window-all-closed', () => {
|
2017-01-16 20:06:53 +01:00
|
|
|
// On macOS it is common for applications and their menu bar
|
|
|
|
// to stay active until the user quits explicitly with Cmd + Q
|
2017-12-21 18:32:51 +01:00
|
|
|
if (process.platform !== 'darwin') {
|
2017-12-13 22:36:30 +01:00
|
|
|
app.quit();
|
2017-01-16 20:06:53 +01:00
|
|
|
}
|
2017-12-13 22:36:30 +01:00
|
|
|
});
|
2017-02-23 19:46:25 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
app.on('before-quit', event => {
|
2017-03-24 00:07:08 +01:00
|
|
|
if (!readyToQuit) {
|
2017-03-24 08:04:30 +01:00
|
|
|
// We need to shutdown the daemon before we're ready to actually quit. This
|
2017-03-24 00:07:08 +01:00
|
|
|
// event will be triggered re-entrantly once preparation is done.
|
|
|
|
event.preventDefault();
|
|
|
|
shutdownDaemonAndQuit();
|
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('Quitting.');
|
2017-01-18 17:32:01 +01:00
|
|
|
}
|
2017-03-24 00:07:08 +01:00
|
|
|
});
|
2017-01-18 17:32:01 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
app.on('activate', () => {
|
2017-01-16 20:06:53 +01:00
|
|
|
// On macOS it's common to re-create a window in the app when the
|
|
|
|
// dock icon is clicked and there are no other windows open.
|
2017-12-21 18:32:51 +01:00
|
|
|
if (rendererWindow === null) {
|
2017-12-13 22:36:30 +01:00
|
|
|
createWindow();
|
2017-01-16 20:06:53 +01:00
|
|
|
}
|
2017-03-24 00:07:08 +01:00
|
|
|
});
|
2017-02-20 19:59:03 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.on('open-url', (event, uri) => {
|
2017-12-04 21:46:51 +01:00
|
|
|
handleOpenUriRequested(uri);
|
|
|
|
});
|
|
|
|
} else if (process.argv.length >= 2) {
|
|
|
|
handleOpenUriRequested(process.argv[1]);
|
|
|
|
}
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
ipcMain.on('upgrade', (event, installerPath) => {
|
|
|
|
app.on('quit', () => {
|
|
|
|
console.log('Launching upgrade installer at', installerPath);
|
2017-03-24 00:07:08 +01:00
|
|
|
// This gets triggered called after *all* other quit-related events, so
|
|
|
|
// we'll only get here if we're fully prepared and quitting for real.
|
2017-03-23 19:08:14 +01:00
|
|
|
openItem(installerPath);
|
2017-02-20 19:59:03 +01:00
|
|
|
});
|
2017-03-23 19:08:14 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
if (rendererWindow) {
|
|
|
|
rendererWindow.loadURL(`file://${__static}/upgrade.html`);
|
2017-03-17 10:06:02 +01:00
|
|
|
}
|
2017-03-24 00:07:08 +01:00
|
|
|
|
2017-03-26 13:02:54 +02:00
|
|
|
shutdownDaemonAndQuit(true);
|
2017-03-17 23:05:25 +01:00
|
|
|
// wait for daemon to shut down before upgrading
|
|
|
|
// what to do if no shutdown in a long time?
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('Update downloaded to', installerPath);
|
2017-12-13 22:36:30 +01:00
|
|
|
console.log(
|
2017-12-21 18:32:51 +01:00
|
|
|
'The app will close, and you will be prompted to install the latest version of LBRY.'
|
2017-12-13 22:36:30 +01:00
|
|
|
);
|
2017-12-21 18:32:51 +01:00
|
|
|
console.log('After the install is complete, please reopen the app.');
|
2017-12-04 21:46:51 +01:00
|
|
|
});
|
2017-02-20 19:59:03 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
ipcMain.on('version-info-requested', () => {
|
2017-12-04 21:46:51 +01:00
|
|
|
function formatRc(ver) {
|
|
|
|
// Adds dash if needed to make RC suffix semver friendly
|
2017-12-21 18:32:51 +01:00
|
|
|
return ver.replace(/([^-])rc/, '$1-rc');
|
2017-12-04 21:46:51 +01:00
|
|
|
}
|
2017-09-14 09:36:41 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
let result = '';
|
2017-12-04 21:46:51 +01:00
|
|
|
const opts = {
|
|
|
|
headers: {
|
2017-12-21 18:32:51 +01:00
|
|
|
'User-Agent': `LBRY/${localVersion}`,
|
2017-12-13 22:36:30 +01:00
|
|
|
},
|
2017-12-04 21:46:51 +01:00
|
|
|
};
|
2017-05-05 11:15:58 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
const req = Https.get(Object.assign(opts, Url.parse(LATEST_RELEASE_API_URL)), res => {
|
|
|
|
res.on('data', data => {
|
|
|
|
result += data;
|
|
|
|
});
|
|
|
|
res.on('end', () => {
|
|
|
|
const tagName = JSON.parse(result).tag_name;
|
|
|
|
const [, remoteVersion] = tagName.match(/^v([\d.]+(?:-?rc\d+)?)$/);
|
|
|
|
if (!remoteVersion) {
|
|
|
|
if (rendererWindow) {
|
|
|
|
rendererWindow.webContents.send('version-info-received', null);
|
2017-12-04 21:46:51 +01:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
} else {
|
|
|
|
const upgradeAvailable = Semver.gt(formatRc(remoteVersion), formatRc(localVersion));
|
|
|
|
if (rendererWindow) {
|
|
|
|
rendererWindow.webContents.send('version-info-received', {
|
|
|
|
remoteVersion,
|
|
|
|
localVersion,
|
|
|
|
upgradeAvailable,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-05-10 10:44:06 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
req.on('error', err => {
|
|
|
|
console.log('Failed to get current version from GitHub. Error:', err);
|
|
|
|
if (rendererWindow) {
|
|
|
|
rendererWindow.webContents.send('version-info-received', null);
|
2017-12-04 21:46:51 +01:00
|
|
|
}
|
2017-05-05 11:15:58 +02:00
|
|
|
});
|
2017-12-04 21:46:51 +01:00
|
|
|
});
|
2017-06-26 16:47:49 +02:00
|
|
|
|
2017-12-10 09:01:24 +01:00
|
|
|
ipcMain.on('autoUpdate', () => {
|
|
|
|
minimize = false;
|
|
|
|
autoUpdater.quitAndInstall();
|
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
ipcMain.on('get-auth-token', event => {
|
|
|
|
Keytar.getPassword('LBRY', 'auth_token').then(token => {
|
|
|
|
event.sender.send('auth-token-response', token ? token.toString().trim() : null);
|
2017-06-26 16:47:49 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
ipcMain.on('set-auth-token', (event, token) => {
|
|
|
|
Keytar.setPassword('LBRY', 'auth_token', token ? token.toString().trim() : null);
|
2017-11-29 01:29:59 +01:00
|
|
|
});
|
2018-01-05 23:25:33 +01:00
|
|
|
|
|
|
|
export { contextMenu };
|