2019-11-12 18:54:21 +09:00
|
|
|
import { ipcRenderer, remote } from 'electron';
|
2019-09-18 15:02:21 +09:00
|
|
|
|
2019-10-24 15:37:33 +09:00
|
|
|
import { Observable, Subject } from 'rxjs';
|
2019-09-19 10:40:16 +09:00
|
|
|
|
2019-11-01 11:25:54 +09:00
|
|
|
import {
|
|
|
|
NativeService,
|
|
|
|
WindowState,
|
2019-11-09 17:29:02 +09:00
|
|
|
NotificationRequest,
|
2019-11-12 18:54:21 +09:00
|
|
|
WindowIdle,
|
2019-11-01 11:25:54 +09:00
|
|
|
} from '@ucap-webmessenger/native';
|
2019-10-24 15:37:33 +09:00
|
|
|
import { share } from 'rxjs/operators';
|
2019-11-09 17:29:02 +09:00
|
|
|
import {
|
|
|
|
NotificationChannel,
|
|
|
|
UpdaterChannel,
|
|
|
|
FileChannel,
|
|
|
|
WindowStateChannel,
|
2019-11-12 10:38:51 +09:00
|
|
|
IdleStateChannel,
|
2019-11-12 18:54:21 +09:00
|
|
|
ChatChannel,
|
2019-11-09 17:29:02 +09:00
|
|
|
} from '../types/channel.type';
|
2019-11-11 15:53:39 +09:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-11-11 18:09:47 +09:00
|
|
|
import { TranslateLoaderService } from '../translate/electron-loader';
|
|
|
|
import { TranslateLoader } from '@ngx-translate/core';
|
2019-09-18 15:02:21 +09:00
|
|
|
|
2019-11-11 15:53:39 +09:00
|
|
|
@Injectable({
|
2019-11-12 18:54:21 +09:00
|
|
|
providedIn: 'root',
|
2019-11-11 15:53:39 +09:00
|
|
|
})
|
2019-09-18 15:02:21 +09:00
|
|
|
export class ElectronNativeService implements NativeService {
|
2019-11-12 18:54:21 +09:00
|
|
|
private ipcRenderer: typeof ipcRenderer;
|
|
|
|
private remote: typeof remote;
|
|
|
|
|
2019-10-24 15:37:33 +09:00
|
|
|
private windowStateChangedSubject: Subject<WindowState> | null = null;
|
|
|
|
private windowStateChanged$: Observable<WindowState> | null = null;
|
|
|
|
|
2019-11-07 16:56:14 +09:00
|
|
|
private idleStateChangedSubject: Subject<WindowIdle> | null = null;
|
|
|
|
private idleStateChanged$: Observable<WindowIdle> | null = null;
|
|
|
|
|
2019-11-12 10:38:51 +09:00
|
|
|
private chatOpenRoomSubject: Subject<string> | null = null;
|
|
|
|
private chatOpenRoom$: Observable<string> | null = null;
|
|
|
|
|
2019-11-11 18:09:47 +09:00
|
|
|
postAppInit(): void {}
|
|
|
|
|
2019-11-09 17:29:02 +09:00
|
|
|
notify(noti: NotificationRequest): void {
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.send(NotificationChannel.Notify, noti);
|
2019-11-09 17:29:02 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
closeAllNotify(): void {
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.send(NotificationChannel.CloseAllNotify);
|
2019-09-18 15:02:21 +09:00
|
|
|
}
|
|
|
|
|
2019-09-19 10:40:16 +09:00
|
|
|
checkForUpdates(): Observable<boolean> {
|
|
|
|
return new Observable<boolean>(subscriber => {
|
2019-09-18 15:02:21 +09:00
|
|
|
try {
|
2019-11-12 18:54:21 +09:00
|
|
|
subscriber.next(this.ipcRenderer.sendSync(UpdaterChannel.Check));
|
2019-09-18 15:02:21 +09:00
|
|
|
} catch (error) {
|
2019-09-19 10:40:16 +09:00
|
|
|
subscriber.error(error);
|
|
|
|
} finally {
|
|
|
|
subscriber.complete();
|
2019-09-18 15:02:21 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
showImageViewer(): void {
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.send(FileChannel.ShowImageViewer);
|
2019-09-18 15:02:21 +09:00
|
|
|
}
|
|
|
|
|
2019-11-07 11:37:33 +09:00
|
|
|
readFile(path: string): Observable<Buffer> {
|
|
|
|
return new Observable<Buffer>(subscriber => {
|
2019-09-24 09:03:36 +09:00
|
|
|
try {
|
2019-11-12 18:54:21 +09:00
|
|
|
subscriber.next(this.ipcRenderer.sendSync(FileChannel.ReadFile, path));
|
2019-09-24 09:03:36 +09:00
|
|
|
} catch (error) {
|
|
|
|
subscriber.error(error);
|
|
|
|
} finally {
|
|
|
|
subscriber.complete();
|
|
|
|
}
|
|
|
|
});
|
2019-09-18 15:02:21 +09:00
|
|
|
}
|
2019-09-24 09:03:36 +09:00
|
|
|
|
2019-11-07 11:37:33 +09:00
|
|
|
saveFile(
|
|
|
|
buffer: Buffer,
|
|
|
|
fileName: string,
|
|
|
|
path?: string
|
|
|
|
): Observable<string> {
|
|
|
|
return new Observable<string>(subscriber => {
|
2019-09-24 09:03:36 +09:00
|
|
|
try {
|
2019-11-07 11:37:33 +09:00
|
|
|
subscriber.next(
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.sendSync(
|
|
|
|
FileChannel.SaveFile,
|
|
|
|
buffer,
|
|
|
|
fileName,
|
|
|
|
path
|
|
|
|
)
|
2019-11-07 11:37:33 +09:00
|
|
|
);
|
2019-09-24 09:03:36 +09:00
|
|
|
} catch (error) {
|
|
|
|
subscriber.error(error);
|
|
|
|
} finally {
|
|
|
|
subscriber.complete();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-24 15:37:33 +09:00
|
|
|
windowStateChanged(): Observable<WindowState> {
|
|
|
|
if (!this.windowStateChangedSubject) {
|
|
|
|
this.windowStateChangedSubject = new Subject<WindowState>();
|
|
|
|
this.windowStateChanged$ = this.windowStateChangedSubject
|
|
|
|
.asObservable()
|
|
|
|
.pipe(share());
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.on(
|
2019-11-09 17:29:02 +09:00
|
|
|
WindowStateChannel.Changed,
|
2019-11-12 18:54:21 +09:00
|
|
|
(event: any, windowState: WindowState) => {
|
2019-10-24 15:37:33 +09:00
|
|
|
this.windowStateChangedSubject.next(windowState);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return this.windowStateChanged$;
|
|
|
|
}
|
|
|
|
|
|
|
|
windowClose(): void {
|
2019-11-12 18:54:21 +09:00
|
|
|
const currentWindow = this.remote.getCurrentWindow();
|
2019-10-24 15:37:33 +09:00
|
|
|
if (!currentWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentWindow.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
windowMinimize(): void {
|
2019-11-12 18:54:21 +09:00
|
|
|
const currentWindow = this.remote.getCurrentWindow();
|
2019-10-24 15:37:33 +09:00
|
|
|
if (!currentWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentWindow.minimize();
|
|
|
|
}
|
|
|
|
|
|
|
|
windowMaximize(): void {
|
2019-11-12 18:54:21 +09:00
|
|
|
const currentWindow = this.remote.getCurrentWindow();
|
2019-10-24 15:37:33 +09:00
|
|
|
if (!currentWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentWindow.isMaximized()) {
|
|
|
|
currentWindow.unmaximize();
|
|
|
|
} else {
|
|
|
|
currentWindow.maximize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 16:56:14 +09:00
|
|
|
idleStateChanged(): Observable<WindowIdle> {
|
|
|
|
if (!this.idleStateChangedSubject) {
|
|
|
|
this.idleStateChangedSubject = new Subject<WindowIdle>();
|
|
|
|
this.idleStateChanged$ = this.idleStateChangedSubject
|
|
|
|
.asObservable()
|
|
|
|
.pipe(share());
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.send(IdleStateChannel.StartCheck);
|
2019-11-07 16:56:14 +09:00
|
|
|
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.on(
|
2019-11-09 17:29:02 +09:00
|
|
|
IdleStateChannel.Changed,
|
2019-11-12 18:54:21 +09:00
|
|
|
(event: any, idleState: WindowIdle) => {
|
2019-11-07 16:56:14 +09:00
|
|
|
this.idleStateChangedSubject.next(idleState);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return this.idleStateChanged$;
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:38:51 +09:00
|
|
|
chatOpenRoom(): Observable<string> {
|
|
|
|
if (!this.chatOpenRoomSubject) {
|
|
|
|
this.chatOpenRoomSubject = new Subject<WindowIdle>();
|
|
|
|
this.chatOpenRoom$ = this.chatOpenRoomSubject
|
|
|
|
.asObservable()
|
|
|
|
.pipe(share());
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:54:21 +09:00
|
|
|
this.ipcRenderer.on(ChatChannel.OpenRoom, (event: any, roomSeq: string) => {
|
|
|
|
this.chatOpenRoomSubject.next(roomSeq);
|
|
|
|
});
|
2019-11-12 10:38:51 +09:00
|
|
|
return this.chatOpenRoom$;
|
|
|
|
}
|
|
|
|
|
2019-11-11 18:09:47 +09:00
|
|
|
getTranslateLoader(prefix?: string, suffix?: string): TranslateLoader {
|
|
|
|
return new TranslateLoaderService(this, prefix, suffix);
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:54:21 +09:00
|
|
|
get isElectron() {
|
|
|
|
return window && (window as any).process && (window as any).process.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
if (this.isElectron) {
|
|
|
|
this.ipcRenderer = (window as any).require('electron').ipcRenderer;
|
|
|
|
this.remote = (window as any).require('electron').remote;
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 15:02:21 +09:00
|
|
|
}
|