Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

Commit

Permalink
Qtum path config (#286)
Browse files Browse the repository at this point in the history
* Update submod hash for import fi

* Catch error when quitting qtum process

* Point to side branch

* Remove param for exec file in getting qtum path

* Fix starting qtum wallet

* Change all exits to app.quit

* Route all quitting flows through will quit

* Remove commented code

* Run linter

* Point submodule to master branch

* Update submod hash

* Add localized strings

* Check if qtum process defined for quit flow

* Fix translation
  • Loading branch information
dwalintukan authored Jun 20, 2018
1 parent 0f10622 commit a7d3a92
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 42 deletions.
85 changes: 62 additions & 23 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const { version, testnetOnly, encryptOk } = require('./package.json');
const Tracking = require('./src/analytics/tracking');
const { getQtumExecPath } = require('./src/utils');
const { initDB, deleteBodhiData } = require('./server/src/db/nedb');
const { getQtumProcess, killQtumProcess, startServices, startServer, getServer } = require('./server/src/server');
const { getQtumProcess, killQtumProcess, startServices, startServer, getServer, startQtumWallet } = require('./server/src/server');
const EmitterHelper = require('./server/src/utils/emitterHelper');
const { Config, setQtumEnv, getQtumExplorerUrl } = require('./server/src/config');
const { getLogger } = require('./server/src/utils/logger');
const { blockchainEnv, ipcEvent, execFile } = require('./server/src/constants');
const { blockchainEnv, ipcEvent } = require('./server/src/constants');
const Wallet = require('./server/src/api/wallet');

/*
Expand All @@ -32,13 +32,26 @@ const Wallet = require('./server/src/api/wallet');
const UI_PORT = 3000;
const EXPLORER_URL_PLACEHOLDER = 'https://qtumhost';

/*
* Codes for type of event when killing the Qtum process
* NORMAL = regular shutdown
* QT_WALLET = shutdown qtumd before starting QT wallet
*/
const [NORMAL, QT_WALLET] = [0, 1];

// 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.
let uiWin;
let i18n;
let killType;

function killQtum(emitEvent) {
killQtumProcess(getQtumExecPath(execFile.QTUM_CLI), emitEvent);
function killQtum(type) {
try {
killType = type;
killQtumProcess(true);
} catch (err) {
app.quit();
}
}

function createWindow() {
Expand Down Expand Up @@ -92,7 +105,7 @@ function showLaunchQtumWalletDialog() {
}, (response) => {
if (response === LAUNCH) {
if (getQtumProcess()) {
killQtum(true);
killQtum(QT_WALLET);
} else {
// Show dialog to wait for initializing to finish
dialog.showMessageBox({
Expand All @@ -119,7 +132,6 @@ function showDeleteDataDialog() {
cancelId: CANCEL,
}, (response) => {
if (response === DELETE) {
killQtum(false);
deleteBodhiData();
app.quit();
}
Expand All @@ -146,7 +158,7 @@ function setupMenu() {
{ type: "separator" },
{ label: "About", click: () => showAboutDialog() },
{ type: "separator" },
{ label: "Quit", accelerator: "Command+Q", click: () => exit() },
{ label: "Quit", accelerator: "Command+Q", click: () => app.quit() },
]
},
{
Expand Down Expand Up @@ -205,7 +217,7 @@ async function startBackend(blockchainEnv) {
throw Error(`blockchainEnv cannot be empty.`);
}

await startServer(blockchainEnv, getQtumExecPath(execFile.QTUMD), encryptOk);
await startServer(blockchainEnv, getQtumExecPath(), encryptOk);
initBrowserWindow();
}

Expand All @@ -225,7 +237,7 @@ function showUpdateDialog() {
showSelectEnvDialog();
} else {
shell.openExternal('https://bodhi.network');
exit();
app.quit();
}
});
}
Expand Down Expand Up @@ -311,7 +323,7 @@ function showErrorDialog(errMessage) {
title: i18n.get('error'),
message: errMessage,
}, (response) => {
exit();
app.quit();
});
}

Expand Down Expand Up @@ -372,25 +384,23 @@ function showWalletUnlockPrompt() {
}

function startQtWallet() {
const qtumqtPath = getQtumExecPath(execFile.QTUM_QT);
setTimeout(() => require('./server/src/start_wallet').startQtumWallet(qtumqtPath), 4000);
setTimeout(() => startQtumWallet(), 4000);
}

function exit(signal) {
function handleExitSignal(signal) {
try {
getLogger().info(`Received ${signal}, exiting...`);
} catch (err) {
console.log(`Received ${signal}, exiting...`);
}

killQtum(false);
app.quit();
}

// Handle exit signals
process.on('SIGINT', exit);
process.on('SIGTERM', exit);
process.on('SIGHUP', exit);
process.on('SIGINT', handleExitSignal);
process.on('SIGTERM', handleExitSignal);
process.on('SIGHUP', handleExitSignal);

/* Electron Events */

Expand All @@ -404,7 +414,6 @@ app.on('ready', () => {

// Emitted when the application is activated.
app.on('activate', () => {
getLogger().debug('activate');
// 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.
if (uiWin === null) {
Expand All @@ -414,13 +423,31 @@ app.on('activate', () => {

// Emitted when all windows have been closed.
app.on('window-all-closed', () => {
getLogger().debug('window-all-closed');
console.log('window-all-closed');
});

// Emitted before the application starts closing its windows.
app.on('before-quit', () => {
getLogger().debug('before-quit');
killQtum(false);
app.on('before-quit', (event) => {
console.log('before-quit');
});

// Emitted when all windows have been closed and the application will quit.
app.on('will-quit', (event) => {
console.log('will-quit');
event.preventDefault();

if (getQtumProcess()) {
dialog.showMessageBox({
type: 'info',
title: i18n.get('shutdownDialogTitle'),
message: i18n.get('shutdownDialogMessage'),
buttons: [i18n.get('ok')],
}, () => {
killQtum(NORMAL);
});
} else {
app.exit();
}
});

/* Emitter Events */
Expand All @@ -436,7 +463,19 @@ EmitterHelper.emitter.on(ipcEvent.QTUMD_ERROR, (errMessage) => {

// Delay, then start qtum-qt
EmitterHelper.emitter.on(ipcEvent.QTUMD_KILLED, () => {
startQtWallet();
switch (killType) {
case NORMAL: {
app.exit();
break;
}
case QT_WALLET: {
startQtWallet();
break;
}
default: {
break;
}
}
});

// Load UI when services are running
Expand Down
8 changes: 8 additions & 0 deletions src/localization/locale.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
"en": "Error",
"zh": "错误"
},
"shutdownDialogTitle": {
"en": "Shutdown",
"zh": "关闭"
},
"shutdownDialogMessage": {
"en": "Bodhi will now shutdown. Please wait a little before shutting your computer off.",
"zh": "菩提即将关闭。请在关闭您的电脑前等待一段时间。"
},
"functionDisabledUntilInitialized": {
"en": "This function is disabled until initializing is finished.",
"zh": "初始化完成后方能使用次功能"
Expand Down
28 changes: 10 additions & 18 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ const { isDevEnv, getDevQtumExecPath } = require('../../server/src/utils');

/*
* Gets the prod env qtum exec path.
* @param execFile {String} The exec file type needed to be returned.
* return {String} The full prod path for the exec file.
*/
const getProdQtumExecPath = (exec) => {
if (exec !== execFile.QTUMD && exec !== execFile.QTUM_QT && exec !== execFile.QTUM_CLI) {
throw Error(`Invalid execFile type: ${exec}`);
}

const getProdQtumExecPath = () => {
const platform = process.platform;
const arch = process.arch;
let osFolder;
Expand Down Expand Up @@ -42,27 +37,24 @@ const getProdQtumExecPath = (exec) => {
}

const { app } = require('electron');
let path;
if (platform === 'win32') {
path = `${app.getAppPath()}/server/qtum/${osFolder}/bin/${exec}.exe`;
} else {
path = `${app.getAppPath()}/server/qtum/${osFolder}/bin/${exec}`;
}

const path = `${app.getAppPath()}/server/qtum/${osFolder}/bin`;
return path.replace('app.asar', 'app.asar.unpacked');
};

const getQtumExecPath = (exec) => {
/*
* Returns the path for the Qtum binaries folder.
*/
const getQtumExecPath = () => {
let qtumExecPath;
if (isDevEnv()) {
qtumExecPath = getDevQtumExecPath(exec);
qtumExecPath = getDevQtumExecPath();
} else {
qtumExecPath = getProdQtumExecPath(exec);
qtumExecPath = getProdQtumExecPath();
}

if (_.isEmpty(qtumExecPath)) {
throw Error(`qtumExecPath cannot be empty.`);
throw Error('qtumExecPath cannot be empty.');
}

return qtumExecPath;
};

Expand Down

0 comments on commit a7d3a92

Please sign in to comment.