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
|
2019-06-26 21:08:04 +01:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
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';
|
2019-12-19 18:47:40 -07:00
|
|
|
import BaseEventIndexManager from 'matrix-react-sdk/src/indexing/BaseEventIndexManager';
|
|
|
|
import dis from 'matrix-react-sdk/src/dispatcher';
|
|
|
|
import { _t } from 'matrix-react-sdk/src/languageHandler';
|
|
|
|
import * as rageshake from 'matrix-react-sdk/src/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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 12:43:01 +01:00
|
|
|
class SeshatIndexManager extends BaseEventIndexManager {
|
2019-11-13 12:15:26 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this._pendingIpcCalls = {};
|
|
|
|
this._nextIpcCallId = 0;
|
|
|
|
ipcRenderer.on('seshatReply', this._onIpcReply.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
async _ipcCall(name: string, ...args: []): Promise<{}> {
|
|
|
|
// TODO this should be moved into the preload.js file.
|
|
|
|
const ipcCallId = ++this._nextIpcCallId;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._pendingIpcCalls[ipcCallId] = {resolve, reject};
|
|
|
|
window.ipcRenderer.send('seshat', {id: ipcCallId, name, args});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async supportsEventIndexing(): Promise<boolean> {
|
|
|
|
return this._ipcCall('supportsEventIndexing');
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:14:59 +01:00
|
|
|
async initEventIndex(): Promise<> {
|
|
|
|
return this._ipcCall('initEventIndex');
|
2019-11-13 12:15:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async addEventToIndex(ev: MatrixEvent, profile: MatrixProfile): Promise<> {
|
|
|
|
return this._ipcCall('addEventToIndex', ev, profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
async isEventIndexEmpty(): Promise<boolean> {
|
|
|
|
return this._ipcCall('isEventIndexEmpty');
|
|
|
|
}
|
|
|
|
|
|
|
|
async commitLiveEvents(): Promise<> {
|
|
|
|
return this._ipcCall('commitLiveEvents');
|
|
|
|
}
|
|
|
|
|
|
|
|
async searchEventIndex(searchConfig: SearchConfig): Promise<SearchResult> {
|
|
|
|
return this._ipcCall('searchEventIndex', searchConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
async addHistoricEvents(
|
|
|
|
events: [HistoricEvent],
|
2019-11-19 12:43:43 +01:00
|
|
|
checkpoint: CrawlerCheckpoint | null,
|
|
|
|
oldCheckpoint: CrawlerCheckpoint | null,
|
2019-11-13 12:15:26 +01:00
|
|
|
): Promise<> {
|
|
|
|
return this._ipcCall('addHistoricEvents', events, checkpoint, oldCheckpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
async addCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> {
|
|
|
|
return this._ipcCall('addCrawlerCheckpoint', checkpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeCrawlerCheckpoint(checkpoint: CrawlerCheckpoint): Promise<> {
|
|
|
|
return this._ipcCall('removeCrawlerCheckpoint', checkpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadCheckpoints(): Promise<[CrawlerCheckpoint]> {
|
|
|
|
return this._ipcCall('loadCheckpoints');
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:14:48 +01:00
|
|
|
async closeEventIndex(): Promise<> {
|
|
|
|
return this._ipcCall('closeEventIndex');
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:15:26 +01:00
|
|
|
async deleteEventIndex(): Promise<> {
|
|
|
|
return this._ipcCall('deleteEventIndex');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2019-11-19 12:43:01 +01:00
|
|
|
this.eventIndexManager = new SeshatIndexManager();
|
2018-12-18 17:42:55 +00:00
|
|
|
|
|
|
|
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);
|
2019-10-04 12:17:01 +01:00
|
|
|
|
|
|
|
this._tryPersistStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _tryPersistStorage() {
|
|
|
|
if (navigator.storage && navigator.storage.persist) {
|
|
|
|
const granted = await navigator.storage.persist();
|
|
|
|
const persisted = await navigator.storage.persisted();
|
|
|
|
console.log("Storage persist request granted: " + granted + " persisted: " + persisted);
|
|
|
|
}
|
2017-05-22 18:30:45 +01:00
|
|
|
}
|
|
|
|
|
2019-06-26 21:08:04 +01:00
|
|
|
async getConfig(): Promise<{}> {
|
|
|
|
return this._ipcCall('getConfig');
|
|
|
|
}
|
|
|
|
|
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
|
2019-05-30 20:04:16 -06:00
|
|
|
const notifBody = {
|
|
|
|
body: msg,
|
|
|
|
silent: true, // we play our own sounds
|
|
|
|
};
|
|
|
|
if (avatarUrl) notifBody['icon'] = avatarUrl;
|
|
|
|
const notification = new global.Notification(title, notifBody);
|
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> {
|
2019-08-05 11:58:16 +01:00
|
|
|
return this._ipcCall('getAppVersion');
|
2018-12-18 17:42:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 01:21:18 +00:00
|
|
|
supportsAutoLaunch(): boolean {
|
2018-12-18 17:42:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-24 01:21:18 +00:00
|
|
|
async getAutoLaunchEnabled(): boolean {
|
2019-08-05 11:58:16 +01:00
|
|
|
return this._ipcCall('getAutoLaunchEnabled');
|
2018-12-18 17:42:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 01:38:21 +00:00
|
|
|
async setAutoLaunchEnabled(enabled: boolean): void {
|
2019-08-05 11:58:16 +01:00
|
|
|
return this._ipcCall('setAutoLaunchEnabled', enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
supportsAutoHideMenuBar(): boolean {
|
2019-11-05 20:31:08 +00:00
|
|
|
// This is irelevant on Mac as Menu bars don't live in the app window
|
|
|
|
return !navigator.platform.toUpperCase().includes('MAC');
|
2019-08-05 11:58:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAutoHideMenuBarEnabled(): boolean {
|
|
|
|
return this._ipcCall('getAutoHideMenuBarEnabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
async setAutoHideMenuBarEnabled(enabled: boolean): void {
|
|
|
|
return this._ipcCall('setAutoHideMenuBarEnabled', enabled);
|
2018-12-18 17:42:55 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 14:32:28 +00:00
|
|
|
supportsMinimizeToTray(): boolean {
|
2019-10-29 11:37:42 +00:00
|
|
|
// Things other than Mac support tray icons
|
|
|
|
return !navigator.platform.toUpperCase().includes('MAC');
|
|
|
|
}
|
|
|
|
|
2019-02-24 01:21:18 +00:00
|
|
|
async getMinimizeToTrayEnabled(): boolean {
|
2019-08-05 11:58:16 +01:00
|
|
|
return this._ipcCall('getMinimizeToTrayEnabled');
|
2019-02-24 01:08:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 01:38:21 +00:00
|
|
|
async setMinimizeToTrayEnabled(enabled: boolean): void {
|
2019-08-05 11:58:16 +01:00
|
|
|
return this._ipcCall('setMinimizeToTrayEnabled', enabled);
|
2019-02-24 01:08:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 17:42:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2019-10-11 16:05:14 +02:00
|
|
|
|
2019-11-13 12:15:26 +01:00
|
|
|
getEventIndexingManager(): BaseEventIndexManager | null {
|
|
|
|
return this.eventIndexManager;
|
2019-10-11 16:05:14 +02:00
|
|
|
}
|
2016-11-02 18:49:28 +00:00
|
|
|
}
|