import { copyFile } from 'fs/promises' ; import { app as rawApp , dialog , ipcMain } from 'electron' ; import { getLogger } from '../shared/logger' ; import { getLogsPath } from './application-constants' ; import { LogHandler } from './log-handler' ; import { ExtendedAppMainProcess } from './types' ; import * as mainWindow from './windows/main' ; import { openHelpWindow } from './windows/help' ; import { join } from 'path' ; const log = getLogger ( 'main/ipc' ) ; const DeltaChatController = ( ( ) => { try { return require ( './deltachat/controller' ) . default ; } catch ( error ) { log . critical ( "Fatal: The DeltaChat Module couldn't be loaded. Please check if all dependencies for deltachat-core are installed!" , error ) ; dialog . showErrorBox ( 'Fatal Error' , `The DeltaChat Module couldn't be loaded.\n Please check if all dependencies for deltachat-core are installed!\n The Log file is located in this folder: ${ getLogsPath ( ) }` ) ; } } ) ( ) ; const app = rawApp as ExtendedAppMainProcess ; export async function init ( cwd , logHandler ) { const main = mainWindow ; const dcController = new DeltaChatController ( cwd ) ; await dcController . init ( ) ; ipcMain . once ( 'ipcReady' , _e => { app . ipcReady = true ; app . emit ( 'ipcReady' ) ; } ) ; ipcMain . on ( 'all' , ( e , ... args ) => { log . debug ( 'Renderer event:' , e , ... args ) ; } ) ; ipcMain . on ( 'show' , ( ) => main . show ( ) ) ; ipcMain . once ( 'frontendReady' , ( ) => app . emit ( 'frontendReady' ) ) ; ipcMain . on ( 'EVENT_DC_DISPATCH' , ( e , _identifier , methodName , args ) => { if ( ! Array . isArray ( args ) ) args = [ args ] ; log . debug ( 'EVENT_DC_DISPATCH: ' , methodName , args ) ; dcController . callMethod ( e , methodName , args ) ; } ) ; ipcMain . on ( 'EVENT_DC_DISPATCH_CB' , async ( e , identifier , methodName , args ) => { if ( ! Array . isArray ( args ) ) args = [ args ] ; log . debug ( `EVENT_DC_DISPATCH_CB (${ identifier }) : ${ methodName } ${ args }` ) ; try { const returnValue = await dcController . callMethod ( e , methodName , args ) ; main . send ( `EVENT_DD_DISPATCH_RETURN_${ identifier }_${ methodName }` , returnValue ) ; } catch ( err ) { main . send ( `EVENT_DD_DISPATCH_RETURN_ERR_${ identifier }_${ methodName }` , err . toString ( ) ) ; } } ) ; ipcMain . on ( 'handleLogMessage' , ( _e , channel , level , stacktrace , ... args ) => logHandler . log ( channel , level , stacktrace , ... args ) ) ; ipcMain . on ( 'ondragstart' , ( event , filePath ) => { event . sender . startDrag ( { file : filePath , icon : join ( __dirname , '../../images/electron-file-drag-out.png' ) , } ) ; } ) ; ipcMain . on ( 'saveLastChatId' , ( _e , chatId ) => { const { lastChats } = app . state . saved ; lastChats [ dcController . selectedAccountId ] = chatId ; } ) ; ipcMain . on ( 'getLastSelectedChatId' , e => { const { lastChats } = app . state . saved ; e . returnValue = lastChats [ dcController . selectedAccountId ] ; } ) ; ipcMain . on ( 'help' , async ( _ev , locale ) => { await openHelpWindow ( locale ) ; } ) ; ipcMain . on ( 'reload-main-window' , ( ) => { mainWindow . window . webContents . reload ( ) ; } ) ; ipcMain . on ( 'get-log-path' , ev => { ev . returnValue = logHandler . logFilePath ( ) ; } ) ; ipcMain . on ( 'get-rc-config' , ev => { ev . returnValue = app . rc ; } ) ; ipcMain . on ( 'app-get-path' , ( ev , arg ) => { ev . returnValue = app . getPath ( arg ) ; } ) ; ipcMain . handle ( 'fileChooser' , ( _ev , options ) => { return dialog . showOpenDialog ( mainWindow . window , options ) ; } ) ; ipcMain . handle ( 'saveFile' , async ( _ev , source , options ) => { const { canceled , filePath } = await dialog . showSaveDialog ( mainWindow . window , options ) ; if ( ! canceled && filePath ) { try { await copyFile ( source , filePath ) ; } catch ( error ) { if ( error . code == 'EACCES' ) { dialog . showErrorBox ( 'Permission Error' , `Cannot write in this folder. You don't have write permission` ) ; } else { dialog . showErrorBox ( 'Unhandled Error' , `Cannot copy file. Error: ${ error }` ) ; } } } } ) ; }