import { C } from 'deltachat-node' ; import { getLogger } from '../../shared/logger' ; import { app as rawApp } from 'electron' ; const log = getLogger ( 'main/deltachat/settings' ) ; import SplitOut from './splitout' ; import { ExtendedAppMainProcess } from '../types' ; import { DesktopSettings } from '../../shared/shared-types' ; import { copyFile , mkdir , rm } from 'fs/promises' ; import { join , extname } from 'path' ; import { getConfigPath } from '../application-constants' ; import { updateTrayIcon } from '../tray' ; const app = rawApp as ExtendedAppMainProcess ; export default class DCSettings extends SplitOut { setConfig ( key , value ) { log . info ( `Setting config ${ key }:${ value }` ) ; const result = this . selectedAccountContext . setConfig ( key , String ( value ) ) === 1 ; if ( key === 'inbox_watch' || key === 'sentbox_watch' || key === 'mvbox_watch' ) { log . info ( `It's a watch config, restarting IO...` ) ; this . selectedAccountContext . stopIO ( ) ; this . selectedAccountContext . startIO ( ) ; } return Boolean ( result ) ; } setConfigFromQr ( qrCodeContent ) { if ( ! this . selectedAccountContext . setConfigFromQr ( qrCodeContent ) ) { throw new Error ( 'setConfigFromQr failed, look into error events for details' ) ; } } getConfig ( key ) { return this . selectedAccountContext . getConfig ( key ) ; } getConfigFor ( keys ) { const config = { } ; for ( const key of keys ) { if ( key . indexOf ( '_port' ) > - 1 ) { config [ key ] = this . getConfig ( key ) === '0' ? '' : this . getConfig ( key ) ; } else { config [ key ] = this . getConfig ( key ) ; } } return config ; } setDesktopSetting ( key , value ) { const { saved } = app . state ; ( saved as any ) [ key ] = value ; app . saveState ( { saved } ) ; if ( key === 'minimizeToTray' ) updateTrayIcon ( ) ; if ( key === 'syncAllAccounts' ) { if ( value ) { this . accounts . startIO ( ) ; } else { this . accounts . stopIO ( ) ; } } return true ; } getDesktopSettings ( ) { return app . state . saved ; } keysImport ( directory ) { this . selectedAccountContext . importExport ( C . DC_IMEX_IMPORT_SELF_KEYS , directory , undefined ) ; } keysExport ( directory ) { this . selectedAccountContext . importExport ( C . DC_IMEX_EXPORT_SELF_KEYS , directory , undefined ) ; } async saveBackgroundImage ( file , isDefaultPicture ) { const originalFilePath = ! isDefaultPicture ? file : join ( __dirname , '../../../images/backgrounds/' , file ) ; const bgDir = join ( getConfigPath ( ) , 'background' ) ; await rm ( bgDir , { recursive : true , force : true } ) ; await mkdir ( bgDir , { recursive : true } ) ; const newPath = join ( getConfigPath ( ) , 'background' , `background_${ Date . now ( ) }` + extname ( originalFilePath ) ) ; try { await copyFile ( originalFilePath , newPath ) ; } catch ( error ) { log . error ( 'BG-IMG Copy Failed' , error ) ; throw error ; } return `url("${ newPath . replace ( /\\/g , '/' ) }")` ; } estimateAutodeleteCount ( fromServer , seconds ) { return this . selectedAccountContext . estimateDeletionCount ( fromServer , seconds ) ; } }