Commit 26d9cca6 authored by Alex Brazier's avatar Alex Brazier Committed by Gabriel Delavald

[NEW] Adds rocketchat:// protocol to add/open server (#466)

* Add rocketchat:// protocol to add/open server

* Update rocketchat protocol

* Adds desktop scheme for linux

* Add open-url listener before app is ready, set timeout workaround for dialog popup

* Refactor makesingleinstance call to antecipate app ready

* Refactor dialog to main function

* Adds arguments check on app ready

* Codacy lint fixes
parent 0d970fd5
......@@ -14,5 +14,18 @@
<true/>
<key>com.apple.security.device.microphone</key>
<true/>
<key>CFBundleIdentifier</key>
<string>chat.rocket.AppleScript.RocketChat</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>RocketChat</string>
<key>CFBundleURLSchemes</key>
<array>
<string>rocketchat</string>
</array>
</dict>
</array>
</dict>
</plist>
......@@ -44,7 +44,8 @@
"linux": {
"desktop": {
"Categories": "GNOME;GTK;Network;InstantMessaging",
"StartupWMClass": "Rocket.Chat+"
"StartupWMClass": "Rocket.Chat+",
"MimeType": "x-scheme-handler/rocketchat"
},
"target": [
"deb",
......
......@@ -21,23 +21,23 @@ process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c';
let screenshareEvent;
ipcMain.on('screenshare', (event, sources) => {
screenshareEvent = event;
let window = new BrowserWindow({
let mainWindow = new BrowserWindow({
width: 776,
height: 600,
show : false,
skipTaskbar: false
});
window.loadURL('file://'+__dirname+'/public/screenshare.html');
mainWindow.loadURL('file://'+__dirname+'/public/screenshare.html');
//window.openDevTools();
window.webContents.on('did-finish-load', () => {
window.webContents.send('sources', sources);
window.show();
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.webContents.send('sources', sources);
mainWindow.show();
});
window.on('closed', () => {
window = null;
mainWindow.on('closed', () => {
mainWindow = null;
if (screenshareEvent) {
screenshareEvent.sender.send('screenshare-result', 'PermissionDeniedError');
screenshareEvent = null;
......@@ -53,20 +53,9 @@ ipcMain.on('source-result', (e, sourceId) => {
});
export function afterMainWindow (mainWindow) {
if (process.platform !== 'darwin') {
const shouldQuit = app.makeSingleInstance(function () {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
}
});
if (shouldQuit) {
app.quit();
}
if (!app.isDefaultProtocolClient('rocketchat')) {
app.setAsDefaultProtocolClient('rocketchat');
}
// Preserver of the window size and position between app launches.
const mainWindowState = windowStateKeeper('main', {
width: 1000,
......
......@@ -35,10 +35,30 @@ if (env.name !== 'production') {
app.setPath('userData', userDataPath + ' (' + env.name + ')');
}
const processProtocolURI = (uri) => {
if (uri && uri.startsWith('rocketchat://')) {
const site = uri.split(/\/|\?/)[2];
if (site) {
let scheme = 'https://';
if (uri.includes('insecure=true')) {
scheme = 'http://';
}
return scheme + site;
}
}
};
const processProtocolArgv = (argv) => {
const protocolURI = argv.find(arg => arg.startsWith('rocketchat://'));
if (protocolURI) {
return processProtocolURI(protocolURI);
}
};
let mainWindow = null;
app.on('ready', function () {
setApplicationMenu();
const mainWindow = createWindow('main', {
mainWindow = createWindow('main', {
width: 1000,
titleBarStyle: 'hidden',
height: 600
......@@ -55,8 +75,77 @@ app.on('ready', function () {
if (env.name === 'development') {
mainWindow.openDevTools();
}
if (process.argv.length > 1) {
const site = processProtocolArgv(process.argv);
if (site) {
const dialog = require('electron').dialog;
dialog.showMessageBox({
type: 'question',
buttons: ['Add', 'Cancel'],
defaultId: 0,
title: 'Add Server',
message: `Do you want to add "${site}" to your list of servers?`
}, (response) => {
if (response === 0) {
mainWindow.send('add-host', site);
}
});
}
}
});
app.on('window-all-closed', function () {
app.quit();
});
const appIsReady = new Promise(resolve => {
if (app.isReady()) {
resolve();
} else {
app.on('ready', resolve);
}
});
if (process.platform === 'darwin') {
// Open protocol urls on mac as open-url is not yet implemented on other OS's
app.on('open-url', function (e, url) {
e.preventDefault();
const site = processProtocolURI(url);
if (site) {
appIsReady.then(() => {
mainWindow.send('add-host', site);
});
}
});
} else {
const shouldQuit = app.makeSingleInstance((argv) => {
// Someone tried to run a second instance, we should focus our window.
const site = processProtocolArgv(argv);
if (site) {
const dialog = require('electron').dialog;
dialog.showMessageBox({
type: 'question',
buttons: ['Add', 'Cancel'],
defaultId: 0,
title: 'Add Server',
message: `Do you want to add "${site}" to your list of servers?`
}, (response) => {
if (response === 0) {
mainWindow.send('add-host', site);
}
});
}
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
mainWindow.focus();
}
});
if (shouldQuit) {
app.quit();
}
}
......@@ -15,7 +15,7 @@ try {
const userUpdateFile = userDataDir.read(updateStoreFile, 'json');
updateFile = Object.assign({}, installUpdateFile, userUpdateFile);
} catch (err) {
console.log(err);
console.error(err);
}
function updateDownloaded () {
......@@ -52,7 +52,6 @@ function updateAvailable ({version}) {
checkForUpdatesEvent.sender.send('update-result', true);
checkForUpdatesEvent = null;
} else if (updateFile.skip === version) {
console.log(`Skipping version: ${version}`);
return;
}
......@@ -112,10 +111,6 @@ function checkForUpdates () {
autoUpdater.on('update-available', updateAvailable);
autoUpdater.on('update-not-available', updateNotAvailable);
autoUpdater.on('download-progress', ({percent}) => {
console.log(`Update progress: ${percent}`);
});
autoUpdater.on('update-downloaded', updateDownloaded);
// Event from about window
......
......@@ -103,7 +103,7 @@ class SpellCheck {
}
if (!this.setEnabled('en')) {
console.log('Unable to set a language for the spell checker - Spell checker is disabled');
console.info('Unable to set a language for the spell checker - Spell checker is disabled');
}
}
......
......@@ -2,13 +2,23 @@
import jetpack from 'fs-jetpack';
import { EventEmitter } from 'events';
import { remote } from 'electron';
import { remote, ipcRenderer } from 'electron';
const remoteServers = remote.require('./background').remoteServers;
class Servers extends EventEmitter {
constructor () {
super();
this.load();
ipcRenderer.on('add-host', (e, host) => {
if (this.hostExists(host)) {
this.setActive(host);
} else {
this.validateHost(host)
.then(() => this.addHost(host))
.then(() => this.setActive(host))
.catch(() => remote.dialog.showErrorBox('Invalid Host', `The host "${host}" could not be validated, so was not added.`));
}
});
}
get hosts () {
......@@ -85,7 +95,7 @@ class Servers extends EventEmitter {
}
} catch (e) {
console.log('Server file invalid');
console.error('Server file invalid');
}
}
......@@ -112,7 +122,6 @@ class Servers extends EventEmitter {
}
validateHost (hostUrl, timeout) {
console.log('Validating hostUrl', hostUrl);
timeout = timeout || 5000;
return new Promise(function (resolve, reject) {
let resolved = false;
......@@ -121,14 +130,12 @@ class Servers extends EventEmitter {
return;
}
resolved = true;
console.log('HostUrl valid', hostUrl);
resolve();
}, function (request) {
if (request.status === 401) {
const authHeader = request.getResponseHeader('www-authenticate');
if (authHeader && authHeader.toLowerCase().indexOf('basic ') === 0) {
resolved = true;
console.log('HostUrl needs basic auth', hostUrl);
reject('basic-auth');
}
}
......@@ -136,7 +143,6 @@ class Servers extends EventEmitter {
return;
}
resolved = true;
console.log('HostUrl invalid', hostUrl);
reject('invalid');
});
if (timeout) {
......@@ -145,7 +151,6 @@ class Servers extends EventEmitter {
return;
}
resolved = true;
console.log('Validating hostUrl TIMEOUT', hostUrl);
reject('timeout');
}, timeout);
}
......
......@@ -170,7 +170,6 @@ class WebView extends EventEmitter {
}
setActive (hostUrl) {
console.log('active setted', hostUrl);
if (this.isActive(hostUrl)) {
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment