Skip to content

Commit

Permalink
[NEW] Adds rocketchat:// protocol to add/open server (RocketChat#466)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexbrazier authored and gdelavald committed Aug 14, 2017
1 parent 0d970fd commit 26d9cca
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 37 deletions.
13 changes: 13 additions & 0 deletions build/entitlements.mas.plist
Original file line number Diff line number Diff line change
Expand Up @@ -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>
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"linux": {
"desktop": {
"Categories": "GNOME;GTK;Network;InstantMessaging",
"StartupWMClass": "Rocket.Chat+"
"StartupWMClass": "Rocket.Chat+",
"MimeType": "x-scheme-handler/rocketchat"
},
"target": [
"deb",
Expand Down
29 changes: 9 additions & 20 deletions src/background.custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
91 changes: 90 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
7 changes: 1 addition & 6 deletions src/background/autoUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/public/lib/SpellCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
Expand Down
19 changes: 12 additions & 7 deletions src/scripts/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -85,7 +95,7 @@ class Servers extends EventEmitter {
}

} catch (e) {
console.log('Server file invalid');
console.error('Server file invalid');
}
}

Expand All @@ -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;
Expand All @@ -121,22 +130,19 @@ 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');
}
}
if (resolved) {
return;
}
resolved = true;
console.log('HostUrl invalid', hostUrl);
reject('invalid');
});
if (timeout) {
Expand All @@ -145,7 +151,6 @@ class Servers extends EventEmitter {
return;
}
resolved = true;
console.log('Validating hostUrl TIMEOUT', hostUrl);
reject('timeout');
}, timeout);
}
Expand Down
1 change: 0 additions & 1 deletion src/scripts/webview.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ class WebView extends EventEmitter {
}

setActive (hostUrl) {
console.log('active setted', hostUrl);
if (this.isActive(hostUrl)) {
return;
}
Expand Down

0 comments on commit 26d9cca

Please sign in to comment.