2017-02-05 11:08:26 +01:00
|
|
|
const Backbone = require('backbone');
|
|
|
|
const Locale = require('../util/locale');
|
|
|
|
const Logger = require('../util/logger');
|
2015-10-22 20:03:44 +02:00
|
|
|
|
2017-02-05 11:08:26 +01:00
|
|
|
const logger = new Logger('launcher');
|
|
|
|
|
|
|
|
const Launcher = {
|
|
|
|
name: 'electron',
|
|
|
|
version: window.process.versions.electron,
|
|
|
|
autoTypeSupported: true,
|
2017-04-12 00:06:44 +02:00
|
|
|
thirdPartyStoragesSupported: true,
|
2017-02-05 11:08:26 +01:00
|
|
|
req: window.require,
|
|
|
|
platform: function() {
|
|
|
|
return process.platform;
|
|
|
|
},
|
|
|
|
electron: function() {
|
|
|
|
return this.req('electron');
|
|
|
|
},
|
|
|
|
remoteApp: function() {
|
|
|
|
return this.electron().remote.app;
|
|
|
|
},
|
|
|
|
remReq: function(mod) {
|
|
|
|
return this.electron().remote.require(mod);
|
|
|
|
},
|
|
|
|
openLink: function(href) {
|
|
|
|
this.electron().shell.openExternal(href);
|
|
|
|
},
|
|
|
|
devTools: true,
|
|
|
|
openDevTools: function() {
|
|
|
|
this.electron().remote.getCurrentWindow().openDevTools();
|
|
|
|
},
|
2017-02-22 01:12:42 +01:00
|
|
|
getSaveFileName: function(defaultPath, callback) {
|
2017-02-05 11:08:26 +01:00
|
|
|
if (defaultPath) {
|
|
|
|
const homePath = this.remReq('electron').app.getPath('userDesktop');
|
2017-04-14 23:22:33 +02:00
|
|
|
defaultPath = this.joinPath(homePath, defaultPath);
|
2017-02-05 11:08:26 +01:00
|
|
|
}
|
|
|
|
this.remReq('electron').dialog.showSaveDialog({
|
|
|
|
title: Locale.launcherSave,
|
|
|
|
defaultPath: defaultPath,
|
|
|
|
filters: [{ name: Locale.launcherFileFilter, extensions: ['kdbx'] }]
|
2017-02-22 01:12:42 +01:00
|
|
|
}, callback);
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
getUserDataPath: function(fileName) {
|
2017-04-14 23:22:33 +02:00
|
|
|
return this.joinPath(this.remoteApp().getPath('userData'), fileName || '');
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
getTempPath: function(fileName) {
|
2017-04-14 23:22:33 +02:00
|
|
|
return this.joinPath(this.remoteApp().getPath('temp'), fileName || '');
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
getDocumentsPath: function(fileName) {
|
2017-04-14 23:22:33 +02:00
|
|
|
return this.joinPath(this.remoteApp().getPath('documents'), fileName || '');
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
getAppPath: function(fileName) {
|
2017-04-14 23:22:33 +02:00
|
|
|
return this.joinPath(this.remoteApp().getAppPath(), fileName || '');
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
getWorkDirPath: function(fileName) {
|
2017-04-14 23:22:33 +02:00
|
|
|
return this.joinPath(process.cwd(), fileName || '');
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
2017-04-08 17:34:27 +02:00
|
|
|
joinPath: function(...parts) {
|
|
|
|
return this.req('path').join(...parts);
|
|
|
|
},
|
2017-02-05 11:08:26 +01:00
|
|
|
writeFile: function(path, data, callback) {
|
2017-02-05 11:49:22 +01:00
|
|
|
this.req('fs').writeFile(path, new window.Buffer(data), callback);
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
readFile: function(path, encoding, callback) {
|
2017-02-05 11:49:22 +01:00
|
|
|
this.req('fs').readFile(path, encoding, (err, contents) => {
|
2017-02-05 11:08:26 +01:00
|
|
|
const data = typeof contents === 'string' ? contents : new Uint8Array(contents);
|
2017-02-05 11:49:22 +01:00
|
|
|
callback(data, err);
|
|
|
|
});
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
fileExists: function(path, callback) {
|
2017-02-05 11:49:22 +01:00
|
|
|
this.req('fs').exists(path, callback);
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
2017-02-05 11:49:22 +01:00
|
|
|
deleteFile: function(path, callback) {
|
|
|
|
this.req('fs').unlink(path, callback);
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
2017-02-05 11:49:22 +01:00
|
|
|
statFile: function(path, callback) {
|
|
|
|
this.req('fs').stat(path, (err, stats) => callback(stats, err));
|
|
|
|
},
|
|
|
|
statFileSync: function(path) {
|
2017-04-09 01:10:53 +02:00
|
|
|
return this.req('fs').statSync(path);
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
2017-02-05 23:34:37 +01:00
|
|
|
mkdir: function(dir, callback) {
|
2017-02-05 11:08:26 +01:00
|
|
|
const fs = this.req('fs');
|
|
|
|
const path = this.req('path');
|
|
|
|
const stack = [];
|
2017-02-05 23:34:37 +01:00
|
|
|
|
|
|
|
const collect = function(dir, stack, callback) {
|
|
|
|
fs.exists(dir, exists => {
|
|
|
|
if (exists) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.unshift(dir);
|
|
|
|
const newDir = path.dirname(dir);
|
|
|
|
if (newDir === dir || !newDir || newDir === '.' || newDir === '/') {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
collect(newDir, stack, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const create = function(stack, callback) {
|
|
|
|
if (!stack.length) {
|
2017-02-21 19:44:45 +01:00
|
|
|
return callback();
|
2017-02-05 11:08:26 +01:00
|
|
|
}
|
2017-02-05 23:34:37 +01:00
|
|
|
|
2017-02-11 23:59:26 +01:00
|
|
|
fs.mkdir(stack.shift(), err =>
|
|
|
|
err ? callback(err) : create(stack, callback)
|
|
|
|
);
|
2017-02-05 23:34:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
collect(dir, stack, () => create(stack, callback));
|
2017-02-05 11:08:26 +01:00
|
|
|
},
|
|
|
|
parsePath: function(fileName) {
|
|
|
|
const path = this.req('path');
|
|
|
|
return {
|
|
|
|
path: fileName,
|
|
|
|
dir: path.dirname(fileName),
|
|
|
|
file: path.basename(fileName)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
createFsWatcher: function(path) {
|
|
|
|
return this.req('fs').watch(path, { persistent: false });
|
|
|
|
},
|
|
|
|
ensureRunnable: function(path) {
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
const fs = this.req('fs');
|
|
|
|
const stat = fs.statSync(path);
|
|
|
|
if ((stat.mode & 0o0111) === 0) {
|
|
|
|
const mode = stat.mode | 0o0100;
|
|
|
|
logger.info(`chmod 0${mode.toString(8)} ${path}`);
|
|
|
|
fs.chmodSync(path, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
preventExit: function(e) {
|
|
|
|
e.returnValue = false;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
exit: function() {
|
|
|
|
this.exitRequested = true;
|
|
|
|
this.requestExit();
|
|
|
|
},
|
|
|
|
requestExit: function() {
|
|
|
|
const app = this.remoteApp();
|
|
|
|
if (this.restartPending) {
|
|
|
|
app.restartApp();
|
|
|
|
} else {
|
|
|
|
app.quit();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
requestRestart: function() {
|
|
|
|
this.restartPending = true;
|
|
|
|
this.requestExit();
|
|
|
|
},
|
|
|
|
cancelRestart: function() {
|
|
|
|
this.restartPending = false;
|
|
|
|
},
|
|
|
|
setClipboardText: function(text) {
|
|
|
|
return this.electron().clipboard.writeText(text);
|
|
|
|
},
|
|
|
|
getClipboardText: function() {
|
|
|
|
return this.electron().clipboard.readText();
|
|
|
|
},
|
|
|
|
clearClipboardText: function() {
|
|
|
|
return this.electron().clipboard.clear();
|
|
|
|
},
|
|
|
|
minimizeApp: function() {
|
|
|
|
this.remoteApp().minimizeApp();
|
|
|
|
},
|
|
|
|
canMinimize: function() {
|
|
|
|
return process.platform !== 'darwin';
|
|
|
|
},
|
|
|
|
updaterEnabled: function() {
|
|
|
|
return this.electron().remote.process.argv.indexOf('--disable-updater') === -1;
|
|
|
|
},
|
|
|
|
getMainWindow: function() {
|
|
|
|
return this.remoteApp().getMainWindow();
|
|
|
|
},
|
|
|
|
resolveProxy: function(url, callback) {
|
|
|
|
const window = this.getMainWindow();
|
|
|
|
const session = window.webContents.session;
|
|
|
|
session.resolveProxy(url, proxy => {
|
|
|
|
const match = /^proxy\s+([\w\.]+):(\d+)+\s*/i.exec(proxy);
|
|
|
|
proxy = match && match[1] ? { host: match[1], port: +match[2] } : null;
|
|
|
|
callback(proxy);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
openWindow: function(opts) {
|
|
|
|
return this.remoteApp().openWindow(opts);
|
|
|
|
},
|
|
|
|
hideApp: function() {
|
|
|
|
const app = this.remoteApp();
|
|
|
|
if (this.canMinimize()) {
|
|
|
|
app.getMainWindow().minimize();
|
|
|
|
} else {
|
|
|
|
app.hide();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isAppFocused: function() {
|
|
|
|
return !!this.electron().remote.BrowserWindow.getFocusedWindow();
|
|
|
|
},
|
|
|
|
showMainWindow: function() {
|
|
|
|
const win = this.getMainWindow();
|
|
|
|
win.show();
|
|
|
|
win.restore();
|
|
|
|
},
|
|
|
|
spawn: function(config) {
|
|
|
|
const ts = logger.ts();
|
|
|
|
let complete = config.complete;
|
|
|
|
const ps = this.req('child_process').spawn(config.cmd, config.args);
|
|
|
|
[ps.stdin, ps.stdout, ps.stderr].forEach(s => s.setEncoding('utf-8'));
|
|
|
|
let stderr = '';
|
|
|
|
let stdout = '';
|
|
|
|
ps.stderr.on('data', d => { stderr += d.toString('utf-8'); });
|
|
|
|
ps.stdout.on('data', d => { stdout += d.toString('utf-8'); });
|
|
|
|
ps.on('close', code => {
|
|
|
|
stdout = stdout.trim();
|
|
|
|
stderr = stderr.trim();
|
|
|
|
const msg = 'spawn ' + config.cmd + ': ' + code + ', ' + logger.ts(ts);
|
|
|
|
if (code) {
|
|
|
|
logger.error(msg + '\n' + stdout + '\n' + stderr);
|
|
|
|
} else {
|
|
|
|
logger.info(msg + (stdout ? '\n' + stdout : ''));
|
|
|
|
}
|
|
|
|
if (complete) {
|
|
|
|
complete(code ? 'Exit code ' + code : null, stdout, code);
|
|
|
|
complete = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ps.on('error', err => {
|
|
|
|
logger.error('spawn error: ' + config.cmd + ', ' + logger.ts(ts), err);
|
|
|
|
if (complete) {
|
|
|
|
complete(err);
|
|
|
|
complete = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (config.data) {
|
|
|
|
try {
|
|
|
|
ps.stdin.write(config.data);
|
|
|
|
ps.stdin.end();
|
|
|
|
} catch (e) {
|
|
|
|
logger.error('spawn write error', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Backbone.on('launcher-exit-request', () => {
|
|
|
|
setTimeout(() => Launcher.exit(), 0);
|
|
|
|
});
|
|
|
|
Backbone.on('launcher-minimize', () => setTimeout(() => Backbone.trigger('app-minimized'), 0));
|
|
|
|
window.launcherOpen = function(path) {
|
|
|
|
Backbone.trigger('launcher-open-file', path);
|
|
|
|
};
|
|
|
|
if (window.launcherOpenedFile) {
|
|
|
|
logger.info('Open file request', window.launcherOpenedFile);
|
|
|
|
Backbone.trigger('launcher-open-file', window.launcherOpenedFile);
|
|
|
|
delete window.launcherOpenedFile;
|
2015-10-22 20:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Launcher;
|