2016-11-02 18:49:28 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright 2016 Aviral Dasgupta
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-12-18 17:42:55 +00:00
|
|
|
Copyright 2018 New Vector Ltd
|
2016-11-02 18:49:28 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-06-11 19:19:17 +01:00
|
|
|
import VectorBasePlatform, {updateCheckStatusEnum} from './VectorBasePlatform';
|
2016-11-02 18:49:28 +00:00
|
|
|
import dis from 'matrix-react-sdk/lib/dispatcher';
|
2017-05-31 17:53:32 +01:00
|
|
|
import { _t } from 'matrix-react-sdk/lib/languageHandler';
|
2017-07-13 00:48:31 +01:00
|
|
|
import Promise from 'bluebird';
|
2018-04-13 01:35:55 +01:00
|
|
|
import rageshake from 'matrix-react-sdk/lib/rageshake/rageshake';
|
2016-11-03 15:45:12 +00:00
|
|
|
|
2018-12-18 18:03:47 +00:00
|
|
|
const ipcRenderer = window.ipcRenderer;
|
|
|
|
|
2017-05-02 21:29:19 +01:00
|
|
|
function platformFriendlyName(): string {
|
2018-12-18 17:42:55 +00:00
|
|
|
// used to use window.process but the same info is available here
|
2019-01-23 16:53:50 +00:00
|
|
|
if (navigator.userAgent.includes('Macintosh')) {
|
2018-12-18 17:42:55 +00:00
|
|
|
return 'macOS';
|
2019-01-23 16:53:50 +00:00
|
|
|
} else if (navigator.userAgent.includes('FreeBSD')) {
|
2018-12-18 17:42:55 +00:00
|
|
|
return 'FreeBSD';
|
2019-01-23 16:53:50 +00:00
|
|
|
} else if (navigator.userAgent.includes('OpenBSD')) {
|
2018-12-18 17:42:55 +00:00
|
|
|
return 'OpenBSD';
|
2019-01-23 16:53:50 +00:00
|
|
|
} else if (navigator.userAgent.includes('SunOS')) {
|
2018-12-18 17:42:55 +00:00
|
|
|
return 'SunOS';
|
2019-01-23 16:53:50 +00:00
|
|
|
} else if (navigator.userAgent.includes('Windows')) {
|
2018-12-18 17:42:55 +00:00
|
|
|
return 'Windows';
|
2019-01-23 16:53:50 +00:00
|
|
|
} else if (navigator.userAgent.includes('Linux')) {
|
2018-12-18 17:42:55 +00:00
|
|
|
return 'Linux';
|
|
|
|
} else {
|
|
|
|
return 'Unknown';
|
2016-11-24 16:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 18:30:45 +01:00
|
|
|
function _onAction(payload: Object) {
|
|
|
|
// Whitelist payload actions, no point sending most across
|
|
|
|
if (['call_state'].includes(payload.action)) {
|
|
|
|
ipcRenderer.send('app_onAction', payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 19:19:17 +01:00
|
|
|
function getUpdateCheckStatus(status) {
|
|
|
|
if (status === true) {
|
|
|
|
return { status: updateCheckStatusEnum.DOWNLOADING };
|
|
|
|
} else if (status === false) {
|
|
|
|
return { status: updateCheckStatusEnum.NOTAVAILABLE };
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
status: updateCheckStatusEnum.ERROR,
|
|
|
|
detail: status,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:49:28 +00:00
|
|
|
export default class ElectronPlatform extends VectorBasePlatform {
|
2017-05-22 18:30:45 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
2017-06-11 19:19:17 +01:00
|
|
|
|
2018-12-18 17:42:55 +00:00
|
|
|
this._pendingIpcCalls = {};
|
|
|
|
this._nextIpcCallId = 0;
|
|
|
|
|
|
|
|
dis.register(_onAction);
|
2017-06-11 19:19:17 +01:00
|
|
|
/*
|
|
|
|
IPC Call `check_updates` returns:
|
|
|
|
true if there is an update available
|
|
|
|
false if there is not
|
|
|
|
or the error if one is encountered
|
|
|
|
*/
|
|
|
|
ipcRenderer.on('check_updates', (event, status) => {
|
|
|
|
if (!this.showUpdateCheck) return;
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'check_updates',
|
|
|
|
value: getUpdateCheckStatus(status),
|
|
|
|
});
|
|
|
|
this.showUpdateCheck = false;
|
|
|
|
});
|
|
|
|
|
2018-12-18 17:42:55 +00:00
|
|
|
// try to flush the rageshake logs to indexeddb before quit.
|
|
|
|
ipcRenderer.on('before-quit', function() {
|
|
|
|
console.log('riot-desktop closing');
|
|
|
|
rageshake.flush();
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcRenderer.on('ipcReply', this._onIpcReply.bind(this));
|
|
|
|
ipcRenderer.on('update-downloaded', this.onUpdateDownloaded.bind(this));
|
|
|
|
|
2017-06-11 19:19:17 +01:00
|
|
|
this.startUpdateCheck = this.startUpdateCheck.bind(this);
|
|
|
|
this.stopUpdateCheck = this.stopUpdateCheck.bind(this);
|
2017-05-22 18:30:45 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 17:42:55 +00:00
|
|
|
async onUpdateDownloaded(ev, updateInfo) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'new_version',
|
|
|
|
currentVersion: await this.getAppVersion(),
|
|
|
|
newVersion: updateInfo,
|
|
|
|
releaseNotes: updateInfo.releaseNotes,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-29 20:03:21 +01:00
|
|
|
getHumanReadableName(): string {
|
2017-05-31 14:51:08 +01:00
|
|
|
return 'Electron Platform'; // no translation required: only used for analytics
|
2017-05-29 19:51:28 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:49:28 +00:00
|
|
|
setNotificationCount(count: number) {
|
2017-01-26 12:58:29 +00:00
|
|
|
if (this.notificationCount === count) return;
|
2016-11-02 18:49:28 +00:00
|
|
|
super.setNotificationCount(count);
|
2017-05-17 10:39:43 +01:00
|
|
|
|
|
|
|
ipcRenderer.send('setBadgeCount', count);
|
2016-11-02 18:49:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 21:29:19 +01:00
|
|
|
supportsNotifications(): boolean {
|
2016-11-02 18:49:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:29:19 +01:00
|
|
|
maySendNotifications(): boolean {
|
2016-11-02 18:49:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-06 13:28:59 +00:00
|
|
|
displayNotification(title: string, msg: string, avatarUrl: string, room: Object): Notification {
|
2017-04-03 20:30:05 +01:00
|
|
|
// GNOME notification spec parses HTML tags for styling...
|
|
|
|
// Electron Docs state all supported linux notification systems follow this markup spec
|
|
|
|
// https://github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md#linux
|
|
|
|
// maybe we should pass basic styling (italics, bold, underline) through from MD
|
2017-04-22 16:05:08 +01:00
|
|
|
// we only have to strip out < and > as the spec doesn't include anything about things like &
|
|
|
|
// so we shouldn't assume that all implementations will treat those properly. Very basic tag parsing is done.
|
2019-01-23 14:47:31 +00:00
|
|
|
if (navigator.userAgent.includes('Linux')) {
|
2017-04-23 09:59:00 +01:00
|
|
|
msg = msg.replace(/</g, '<').replace(/>/g, '>');
|
2017-04-03 20:30:05 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:49:28 +00:00
|
|
|
// Notifications in Electron use the HTML5 notification API
|
|
|
|
const notification = new global.Notification(
|
|
|
|
title,
|
|
|
|
{
|
|
|
|
body: msg,
|
|
|
|
icon: avatarUrl,
|
|
|
|
silent: true, // we play our own sounds
|
2017-05-02 21:29:19 +01:00
|
|
|
},
|
2016-11-02 18:49:28 +00:00
|
|
|
);
|
|
|
|
|
2018-12-18 17:42:55 +00:00
|
|
|
notification.onclick = () => {
|
2016-11-02 18:49:28 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
2017-04-23 09:56:43 +01:00
|
|
|
room_id: room.roomId,
|
2016-11-02 18:49:28 +00:00
|
|
|
});
|
|
|
|
global.focus();
|
2018-12-18 17:42:55 +00:00
|
|
|
this._ipcCall('focusWindow');
|
2016-11-02 18:49:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
2017-06-01 00:00:00 +01:00
|
|
|
loudNotification(ev: Event, room: Object) {
|
|
|
|
ipcRenderer.send('loudNotification');
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:49:28 +00:00
|
|
|
clearNotification(notif: Notification) {
|
|
|
|
notif.close();
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:42:55 +00:00
|
|
|
async getAppVersion(): Promise<string> {
|
|
|
|
return await this._ipcCall('getAppVersion');
|
|
|
|
}
|
|
|
|
|
|
|
|
supportsAutoLaunch() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAutoLaunchEnabled() {
|
|
|
|
return await this._ipcCall('getAutoLaunchEnabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
async setAutoLaunchEnabled(enabled) {
|
|
|
|
return await this._ipcCall('setAutoLaunchEnabled', enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
async canSelfUpdate(): boolean {
|
|
|
|
const feedUrl = await this._ipcCall('getUpdateFeedUrl');
|
|
|
|
return Boolean(feedUrl);
|
2016-11-08 10:47:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 19:19:17 +01:00
|
|
|
startUpdateCheck() {
|
|
|
|
if (this.showUpdateCheck) return;
|
|
|
|
super.startUpdateCheck();
|
2017-06-03 15:12:46 +01:00
|
|
|
|
2017-06-11 19:19:17 +01:00
|
|
|
ipcRenderer.send('check_updates');
|
2016-11-02 18:49:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
installUpdate() {
|
|
|
|
// IPC to the main process to install the update, since quitAndInstall
|
|
|
|
// doesn't fire the before-quit event so the main process needs to know
|
|
|
|
// it should exit.
|
2017-06-11 19:19:17 +01:00
|
|
|
ipcRenderer.send('install_update');
|
2016-11-02 18:49:28 +00:00
|
|
|
}
|
2016-11-24 16:46:15 +00:00
|
|
|
|
2017-05-02 21:29:19 +01:00
|
|
|
getDefaultDeviceDisplayName(): string {
|
2017-05-31 14:51:08 +01:00
|
|
|
return _t('Riot Desktop on %(platformName)s', { platformName: platformFriendlyName() });
|
2016-11-24 16:46:15 +00:00
|
|
|
}
|
2017-01-10 18:39:21 +00:00
|
|
|
|
2017-05-02 21:29:19 +01:00
|
|
|
screenCaptureErrorString(): ?string {
|
2017-01-10 18:39:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
2017-01-19 13:25:56 +00:00
|
|
|
|
2017-05-02 21:29:19 +01:00
|
|
|
requestNotificationPermission(): Promise<string> {
|
2017-07-13 00:32:07 +01:00
|
|
|
return Promise.resolve('granted');
|
2017-01-19 13:25:56 +00:00
|
|
|
}
|
2017-04-10 17:40:09 +01:00
|
|
|
|
|
|
|
reload() {
|
2018-12-18 17:42:55 +00:00
|
|
|
// we used to remote to the main process to get it to
|
|
|
|
// reload the webcontents, but in practice this is unnecessary:
|
|
|
|
// the normal way works fine.
|
|
|
|
window.location.reload(false);
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:14:25 +00:00
|
|
|
async migrateFromOldOrigin() {
|
|
|
|
return this._ipcCall('origin_migrate');
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:42:55 +00:00
|
|
|
async _ipcCall(name, ...args) {
|
|
|
|
const ipcCallId = ++this._nextIpcCallId;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._pendingIpcCalls[ipcCallId] = {resolve, reject};
|
|
|
|
window.ipcRenderer.send('ipcCall', {id: ipcCallId, name, args});
|
|
|
|
// Maybe add a timeout to these? Probably not necessary.
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_onIpcReply(ev, payload) {
|
|
|
|
if (payload.id === undefined) {
|
|
|
|
console.warn("Ignoring IPC reply with no ID");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._pendingIpcCalls[payload.id] === undefined) {
|
|
|
|
console.warn("Unknown IPC payload ID: " + payload.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const callbacks = this._pendingIpcCalls[payload.id];
|
|
|
|
delete this._pendingIpcCalls[payload.id];
|
|
|
|
if (payload.error) {
|
|
|
|
callbacks.reject(payload.error);
|
|
|
|
} else {
|
|
|
|
callbacks.resolve(payload.reply);
|
|
|
|
}
|
2017-09-04 09:33:08 +02:00
|
|
|
}
|
2016-11-02 18:49:28 +00:00
|
|
|
}
|