import Logger from '../tools/env.logger' ; import { dialog , SaveDialogReturnValue , OpenDialogReturnValue } from 'electron' ; import * as fs from 'fs' ; import { IService } from '../interfaces/interface.service' ; import ServiceElectron , { IPCMessages , Subscription } from './service.electron' ; import ServiceFileRecent from './files/service.file.recent' ; import ServiceFileWriter from './files/service.file.writer' ; import ServiceStorage from './service.storage' ; import { app } from 'electron' ; class description class ServiceFilters implements IService { private _logger : Logger = new Logger ( 'ServiceFilters' ) ; private _subscriptions : { [ key ] : Subscription | undefined ; } = { } ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { ServiceElectron . IPC . subscribe ( IPCMessages . FiltersLoadRequest , this . _ipc_onFiltersLoadRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . FiltersLoadRequest = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "FiltersLoadRequest" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) ; ServiceElectron . IPC . subscribe ( IPCMessages . FiltersSaveRequest , this . _ipc_onFiltersSaveRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . FiltersSaveRequest = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "FiltersSaveRequest" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) ; resolve ( ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceFilters' ; } public openFilters ( session , file ? ) { ServiceElectron . IPC . send ( new ServiceElectron . IPCMessages . FiltersOpen ( { session : session , file : file } ) ) . catch ( ( error ) => { this . _logger . warn ( `Fail to send IPC message "FiltersOpen" to render due: ${ error . message }` ) ; } ) ; } public clear ( ) { ServiceStorage . get ( ) . set ( { recentFiltersFiles : [ ] , } ) . catch ( ( err ) => { this . _logger . error ( err . message ) ; } ) ; app . clearRecentDocuments ( ) ; ServiceElectron . updateMenu ( ) ; } private _ipc_onFiltersLoadRequest ( message , response ) { const request = message as IPCMessages . FiltersLoadRequest ; if ( request . file === undefined ) { const win = ServiceElectron . getBrowserWindow ( ) ; if ( win === undefined ) { return ; } dialog . showOpenDialog ( win , { properties : [ 'openFile' , 'showHiddenFiles' ] , filters : [ { name : 'Text Files' , extensions : [ 'txt' ] } ] , } ) . then ( ( returnValue ) => { if ( ! ( returnValue . filePaths instanceof Array ) || returnValue . filePaths . length !== 1 ) { return ; } const file = returnValue . filePaths [ 0 ] ; this . _load ( file ) . then ( ( content ) => { ServiceFileRecent . saveFilters ( file , 0 ) ; response ( new IPCMessages . FiltersLoadResponse ( { store : content , file : file , } ) ) ; } ) . catch ( ( error ) => { return response ( new IPCMessages . FiltersLoadResponse ( { file : file , error : this . _logger . warn ( `Fail to open file "${ file }" due error: ${ error . message }` ) , } ) ) ; } ) ; } ) ; } else { this . _load ( request . file ) . then ( ( content ) => { ServiceFileRecent . saveFilters ( request . file as string , 0 ) ; response ( new IPCMessages . FiltersLoadResponse ( { store : content , file : request . file as string , } ) ) ; } ) . catch ( ( error ) => { return response ( new IPCMessages . FiltersLoadResponse ( { file : request . file as string , error : this . _logger . warn ( `Fail to open file "${ request . file }" due error: ${ error . message }` ) , } ) ) ; } ) ; } } private _ipc_onFiltersSaveRequest ( message , response ) { const request = message as IPCMessages . FiltersSaveRequest ; if ( typeof request . file === 'string' ) { if ( ! fs . existsSync ( request . file ) ) { request . file = undefined ; } } if ( typeof request . file === 'string' && request . file . trim ( ) !== '' ) { ServiceFileWriter . write ( request . file , request . store ) ; ServiceFileRecent . saveFilters ( request . file as string , request . count ) ; response ( new IPCMessages . FiltersSaveResponse ( { filename : request . file as string , } ) ) ; } else { dialog . showSaveDialog ( { title : 'Saving filters' , filters : [ { name : 'Text Files' , extensions : [ 'txt' ] } ] , } ) . then ( ( results ) => { if ( typeof results . filePath === 'string' && results . filePath . trim ( ) !== '' ) { ServiceFileWriter . write ( results . filePath , request . store ) ; ServiceFileRecent . saveFilters ( results . filePath , request . count ) ; response ( new IPCMessages . FiltersSaveResponse ( { filename : results . filePath , } ) ) ; } else { response ( new IPCMessages . FiltersSaveResponse ( { filename : '' , } ) ) ; } } ) ; } } private _load ( file ) { return new Promise ( ( resolve , reject ) => { fs . stat ( file , ( error , stats ) => { if ( error ) { return reject ( error ) ; } fs . readFile ( file , ( readingError , data ) => { if ( readingError ) { return reject ( readingError ) ; } data = data . toString ( ) ; try { const content = JSON . parse ( data ) ; if ( typeof content !== 'object' || content === null ) { return reject ( new Error ( `Fail to parse file "${ file }" because content isn't an object.` ) ) ; } resolve ( data ) ; } catch ( e ) { return reject ( new Error ( `Fail to parse file "${ file }" due error: ${ e . message }` ) ) ; } } ) ; } ) ; } ) ; } } export default ( new ServiceFilters ( ) ) ;