Commit e36251e6 authored by Tasso Evangelista's avatar Tasso Evangelista

Update code style of source code

parent 086393e6
import './branding/branding.js';
import { start } from './scripts/start';
import { remote } from 'electron';
const app = remote.app;
const { app } = remote;
Bugsnag.metaData = {
// platformId: app.process.platform,
// platformArch: app.process.arch,
// electronVersion: app.process.versions.electron,
version: app.getVersion()
// platformVersion: cordova.platformVersion
// build: appInfo.build
// platformId: app.process.platform,
// platformArch: app.process.arch,
// electronVersion: app.process.versions.electron,
version: app.getVersion(),
// platformVersion: cordova.platformVersion
// build: appInfo.build
};
Bugsnag.appVersion = app.getVersion();
......
......@@ -21,90 +21,90 @@ process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c';
const isMacOS = process.platform === 'darwin';
const unsetDefaultApplicationMenu = () => {
if (!isMacOS) {
Menu.setApplicationMenu(null);
return;
}
const emptyMenuTemplate = [{
submenu: [
{
label: i18n.__('Quit_App', app.getName()),
accelerator: 'CommandOrControl+Q',
click () {
app.quit();
}
}
]
}];
Menu.setApplicationMenu(Menu.buildFromTemplate(emptyMenuTemplate));
if (!isMacOS) {
Menu.setApplicationMenu(null);
return;
}
const emptyMenuTemplate = [{
submenu: [
{
label: i18n.__('Quit_App', app.getName()),
accelerator: 'CommandOrControl+Q',
click() {
app.quit();
},
},
],
}];
Menu.setApplicationMenu(Menu.buildFromTemplate(emptyMenuTemplate));
};
const setUserDataPath = () => {
const appName = app.getName();
const dirName = env.name === 'production' ? appName : `${ appName } (${ env.name })`;
const appName = app.getName();
const dirName = env.name === 'production' ? appName : `${ appName } (${ env.name })`;
app.setPath('userData', path.join(app.getPath('appData'), dirName));
app.setPath('userData', path.join(app.getPath('appData'), dirName));
};
const migrateOlderVersionUserData = () => {
const olderAppName = 'Rocket.Chat+';
const dirName = env.name === 'production' ? olderAppName : `${ olderAppName } (${ env.name })`;
const olderUserDataPath = path.join(app.getPath('appData'), dirName);
try {
jetpack.copy(olderUserDataPath, app.getPath('userData'), { overwrite: true });
jetpack.remove(olderUserDataPath);
} catch (e) {
return;
}
const olderAppName = 'Rocket.Chat+';
const dirName = env.name === 'production' ? olderAppName : `${ olderAppName } (${ env.name })`;
const olderUserDataPath = path.join(app.getPath('appData'), dirName);
try {
jetpack.copy(olderUserDataPath, app.getPath('userData'), { overwrite: true });
jetpack.remove(olderUserDataPath);
} catch (e) {
return;
}
};
const parseProtocolUrls = (args) =>
args.filter(arg => /^rocketchat:\/\/./.test(arg))
.map(uri => url.parse(uri))
.map(({ hostname, pathname, query }) => {
const { insecure } = querystring.parse(query);
return `${ insecure === 'true' ? 'http' : 'https' }://${ hostname }${ pathname || '' }`;
});
args.filter((arg) => /^rocketchat:\/\/./.test(arg))
.map((uri) => url.parse(uri))
.map(({ hostname, pathname, query }) => {
const { insecure } = querystring.parse(query);
return `${ insecure === 'true' ? 'http' : 'https' }://${ hostname }${ pathname || '' }`;
});
const addServers = (protocolUrls) => parseProtocolUrls(protocolUrls)
.forEach(serverUrl => addServer(serverUrl));
.forEach((serverUrl) => addServer(serverUrl));
const isSecondInstance = app.makeSingleInstance((argv) => {
addServers(argv.slice(2));
addServers(argv.slice(2));
});
if (isSecondInstance && !process.mas) {
app.quit();
app.quit();
}
// macOS only
app.on('open-url', (event, url) => {
event.preventDefault();
addServers([ url ]);
event.preventDefault();
addServers([url]);
});
app.on('ready', () => {
unsetDefaultApplicationMenu();
setUserDataPath();
migrateOlderVersionUserData();
unsetDefaultApplicationMenu();
setUserDataPath();
migrateOlderVersionUserData();
if (!app.isDefaultProtocolClient('rocketchat')) {
app.setAsDefaultProtocolClient('rocketchat');
}
if (!app.isDefaultProtocolClient('rocketchat')) {
app.setAsDefaultProtocolClient('rocketchat');
}
createMainWindow();
createMainWindow();
getMainWindow().then(mainWindow => certificate.initWindow(mainWindow));
getMainWindow().then((mainWindow) => certificate.initWindow(mainWindow));
autoUpdate();
autoUpdate();
});
app.on('window-all-closed', () => {
app.quit();
app.quit();
});
ipcMain.on('getSystemIdleTime', (event) => {
event.returnValue = idle.getIdleTime();
event.returnValue = idle.getIdleTime();
});
......@@ -8,127 +8,127 @@ const userDataDir = jetpack.cwd(app.getPath('userData'));
const updateSettingsFileName = 'update.json';
const loadUpdateSettings = (dir) => {
try {
return dir.read(updateSettingsFileName, 'json') || {};
} catch (error) {
console.error(error);
return {};
}
try {
return dir.read(updateSettingsFileName, 'json') || {};
} catch (error) {
console.error(error);
return {};
}
};
const appUpdateSettings = loadUpdateSettings(appDir);
const userUpdateSettings = loadUpdateSettings(userDataDir);
const updateSettings = (() => {
const defaultUpdateSettings = { autoUpdate: true };
const defaultUpdateSettings = { autoUpdate: true };
if (appUpdateSettings.forced) {
return Object.assign({}, defaultUpdateSettings, appUpdateSettings);
} else {
return Object.assign({}, defaultUpdateSettings, appUpdateSettings, userUpdateSettings);
}
if (appUpdateSettings.forced) {
return Object.assign({}, defaultUpdateSettings, appUpdateSettings);
} else {
return Object.assign({}, defaultUpdateSettings, appUpdateSettings, userUpdateSettings);
}
})();
delete updateSettings.forced;
const saveUpdateSettings = () => {
if (appUpdateSettings.forced) {
return;
}
if (appUpdateSettings.forced) {
return;
}
userDataDir.write(updateSettingsFileName, userUpdateSettings, { atomic: true });
userDataDir.write(updateSettingsFileName, userUpdateSettings, { atomic: true });
};
let checkForUpdatesEvent;
function updateDownloaded () {
dialog.showMessageBox({
title: i18n.__('Update_ready'),
message: i18n.__('Update_ready_message'),
buttons: [
i18n.__('Update_Install_Later'),
i18n.__('Update_Install_Now')
],
defaultId: 1
}, (response) => {
if (response === 0) {
dialog.showMessageBox({
title: i18n.__('Update_installing_later'),
message: i18n.__('Update_installing_later_message')
});
} else {
autoUpdater.quitAndInstall();
setTimeout(() => app.quit(), 1000);
}
});
function updateDownloaded() {
dialog.showMessageBox({
title: i18n.__('Update_ready'),
message: i18n.__('Update_ready_message'),
buttons: [
i18n.__('Update_Install_Later'),
i18n.__('Update_Install_Now'),
],
defaultId: 1,
}, (response) => {
if (response === 0) {
dialog.showMessageBox({
title: i18n.__('Update_installing_later'),
message: i18n.__('Update_installing_later_message'),
});
} else {
autoUpdater.quitAndInstall();
setTimeout(() => app.quit(), 1000);
}
});
}
function updateNotAvailable () {
if (checkForUpdatesEvent) {
checkForUpdatesEvent.sender.send('update-result', false);
checkForUpdatesEvent = null;
}
function updateNotAvailable() {
if (checkForUpdatesEvent) {
checkForUpdatesEvent.sender.send('update-result', false);
checkForUpdatesEvent = null;
}
}
function updateAvailable ({version}) {
if (checkForUpdatesEvent) {
checkForUpdatesEvent.sender.send('update-result', true);
checkForUpdatesEvent = null;
} else if (updateSettings.skip === version) {
return;
}
let window = new BrowserWindow({
title: i18n.__('Update_Available'),
width: 600,
height: 330,
show : false,
center: true,
resizable: false,
maximizable: false,
minimizable: false
});
window.loadURL(`file://${__dirname}/public/update.html`);
window.setMenuBarVisibility(false);
window.webContents.on('did-finish-load', () => {
window.webContents.send('new-version', version);
window.show();
});
ipcMain.once('update-response', (e, type) => {
switch (type) {
case 'skip':
userUpdateSettings.skip = version;
saveUpdateSettings();
dialog.showMessageBox({
title: i18n.__('Update_skip'),
message: i18n.__('Update_skip_message')
}, () => window.close());
break;
case 'remind':
dialog.showMessageBox({
title: i18n.__('Update_remind'),
message: i18n.__('Update_remind_message')
}, () => window.close());
break;
case 'update':
dialog.showMessageBox({
title: i18n.__('Update_downloading'),
message: i18n.__('Update_downloading_message')
}, () => window.close());
autoUpdater.downloadUpdate();
break;
}
});
window.on('closed', () => {
window = null;
ipcMain.removeAllListeners('update-response');
});
function updateAvailable({ version }) {
if (checkForUpdatesEvent) {
checkForUpdatesEvent.sender.send('update-result', true);
checkForUpdatesEvent = null;
} else if (updateSettings.skip === version) {
return;
}
let window = new BrowserWindow({
title: i18n.__('Update_Available'),
width: 600,
height: 330,
show : false,
center: true,
resizable: false,
maximizable: false,
minimizable: false,
});
window.loadURL(`file://${ __dirname }/public/update.html`);
window.setMenuBarVisibility(false);
window.webContents.on('did-finish-load', () => {
window.webContents.send('new-version', version);
window.show();
});
ipcMain.once('update-response', (e, type) => {
switch (type) {
case 'skip':
userUpdateSettings.skip = version;
saveUpdateSettings();
dialog.showMessageBox({
title: i18n.__('Update_skip'),
message: i18n.__('Update_skip_message'),
}, () => window.close());
break;
case 'remind':
dialog.showMessageBox({
title: i18n.__('Update_remind'),
message: i18n.__('Update_remind_message'),
}, () => window.close());
break;
case 'update':
dialog.showMessageBox({
title: i18n.__('Update_downloading'),
message: i18n.__('Update_downloading_message'),
}, () => window.close());
autoUpdater.downloadUpdate();
break;
}
});
window.on('closed', () => {
window = null;
ipcMain.removeAllListeners('update-response');
});
}
export const canUpdate = () =>
(process.platform === 'linux' && Boolean(process.env.APPIMAGE)) ||
(process.platform === 'linux' && Boolean(process.env.APPIMAGE)) ||
(process.platform === 'win32' && !process.windowsStore) ||
(process.platform === 'darwin' && !process.mas);
......@@ -137,28 +137,28 @@ export const canAutoUpdate = () => updateSettings.autoUpdate !== false;
export const canSetAutoUpdate = () => !appUpdateSettings.forced || appUpdateSettings.autoUpdate !== false;
export const setAutoUpdate = (canAutoUpdate) => {
if (!canSetAutoUpdate()) {
return;
}
if (!canSetAutoUpdate()) {
return;
}
updateSettings.autoUpdate = userUpdateSettings.autoUpdate = Boolean(canAutoUpdate);
saveUpdateSettings();
updateSettings.autoUpdate = userUpdateSettings.autoUpdate = Boolean(canAutoUpdate);
saveUpdateSettings();
};
ipcMain.on('can-update', (event) => {
event.returnValue = canUpdate();
event.returnValue = canUpdate();
});
ipcMain.on('can-auto-update', (event) => {
event.returnValue = canAutoUpdate();
event.returnValue = canAutoUpdate();
});
ipcMain.on('can-set-auto-update', (event) => {
event.returnValue = canSetAutoUpdate();
event.returnValue = canSetAutoUpdate();
});
ipcMain.on('set-auto-update', (event, canAutoUpdate) => {
setAutoUpdate(canAutoUpdate);
setAutoUpdate(canAutoUpdate);
});
autoUpdater.autoDownload = false;
......@@ -167,14 +167,14 @@ autoUpdater.on('update-not-available', updateNotAvailable);
autoUpdater.on('update-downloaded', updateDownloaded);
ipcMain.on('check-for-updates', (event) => {
if (canAutoUpdate() && canUpdate()) {
checkForUpdatesEvent = event;
autoUpdater.checkForUpdates();
}
if (canAutoUpdate() && canUpdate()) {
checkForUpdatesEvent = event;
autoUpdater.checkForUpdates();
}
});
export default () => {
if (canAutoUpdate() && canUpdate()) {
autoUpdater.checkForUpdates();
}
if (canAutoUpdate() && canUpdate()) {
autoUpdater.checkForUpdates();
}
};
......@@ -4,108 +4,108 @@ import url from 'url';
import i18n from '../i18n/index.js';
class CertificateStore {
initWindow (win) {
this.storeFileName = 'certificate.json';
this.userDataDir = jetpack.cwd(app.getPath('userData'));
this.load();
// Don't ask twice for same cert if loading multiple urls
this.queued = {};
this.window = win;
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
event.preventDefault();
if (this.isTrusted(url, certificate)) {
callback(true);
return;
}
if (this.queued[certificate.fingerprint]) {
this.queued[certificate.fingerprint].push(callback);
// Call the callback after approved/rejected
return;
} else {
this.queued[certificate.fingerprint] = [callback];
}
let detail = `URL: ${url}\nError: ${error}`;
if (this.isExisting(url)) {
detail = i18n.__('Certificate_error_different', detail);
}
dialog.showMessageBox(this.window, {
title: i18n.__('Certificate_error'),
message: i18n.__('Certificate_error_message', certificate.issuerName),
detail: detail,
type: 'warning',
buttons: [
i18n.__('Yes'),
i18n.__('No')
],
cancelId: 1
}, (response) => {
if (response === 0) {
this.add(url, certificate);
this.save();
if (webContents.getURL().indexOf('file://') === 0) {
webContents.send('certificate-reload', url);
}
}
//Call all queued callbacks with result
this.queued[certificate.fingerprint].forEach(cb => cb(response === 0));
delete this.queued[certificate.fingerprint];
});
});
}
load () {
try {
this.data = this.userDataDir.read(this.storeFileName, 'json');
} catch (e) {
console.error(e);
this.data = {};
}
if (this.data === undefined) {
this.clear();
}
}
clear () {
this.data = {};
this.save();
}
save () {
this.userDataDir.write(this.storeFileName, this.data, { atomic: true });
}
parseCertificate (certificate) {
return certificate.issuerName + '\n' + certificate.data.toString();
}
getHost (certUrl) {
return url.parse(certUrl).host;
}
add (certUrl, certificate) {
const host = this.getHost(certUrl);
this.data[host] = this.parseCertificate(certificate);
}
isExisting (certUrl) {
const host = this.getHost(certUrl);
return this.data.hasOwnProperty(host);
}
isTrusted (certUrl, certificate) {
const host = this.getHost(certUrl);
if (!this.isExisting(certUrl)) {
return false;
}
return this.data[host] === this.parseCertificate(certificate);
}
initWindow(win) {
this.storeFileName = 'certificate.json';
this.userDataDir = jetpack.cwd(app.getPath('userData'));
this.load();
// Don't ask twice for same cert if loading multiple urls
this.queued = {};
this.window = win;
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
event.preventDefault();
if (this.isTrusted(url, certificate)) {
callback(true);
return;
}
if (this.queued[certificate.fingerprint]) {
this.queued[certificate.fingerprint].push(callback);
// Call the callback after approved/rejected
return;
} else {
this.queued[certificate.fingerprint] = [callback];
}
let detail = `URL: ${ url }\nError: ${ error }`;
if (this.isExisting(url)) {
detail = i18n.__('Certificate_error_different', detail);
}
dialog.showMessageBox(this.window, {
title: i18n.__('Certificate_error'),
message: i18n.__('Certificate_error_message', certificate.issuerName),
detail,
type: 'warning',
buttons: [
i18n.__('Yes'),
i18n.__('No'),
],
cancelId: 1,
}, (response) => {
if (response === 0) {
this.add(url, certificate);
this.save();
if (webContents.getURL().indexOf('file://') === 0) {
webContents.send('certificate-reload', url);
}
}
// Call all queued callbacks with result
this.queued[certificate.fingerprint].forEach((cb) => cb(response === 0));