import { DesktopDeviceInterface, Environment, RawKeychainValue, FileBackupReadToken, FileBackupReadChunkResponse, FileBackupsMapping, PlaintextBackupsMapping, } from '@web/Application/Device/DesktopSnjsExports' import { WebOrDesktopDevice } from '@web/Application/Device/WebOrDesktopDevice' import { Component } from '../Main/Packages/PackageManagerInterface' import { CrossProcessBridge } from './CrossProcessBridge' const FallbackLocalStorageKey = 'keychain' export class DesktopDevice extends WebOrDesktopDevice implements DesktopDeviceInterface { public environment: Environment.Desktop = Environment.Desktop constructor( private remoteBridge: CrossProcessBridge, private useNativeKeychain: boolean, public extensionsServerHost: string, appVersion: string, ) { super(appVersion) } async getHomeServerUrl(): Promise { return this.remoteBridge.getHomeServerUrl() } async getHomeServerLastErrorMessage(): Promise { return this.remoteBridge.getHomeServerLastErrorMessage() } async isHomeServerRunning(): Promise { return this.remoteBridge.isHomeServerRunning() } async activatePremiumFeatures(username: string, subscriptionId: number): Promise { return this.remoteBridge.activatePremiumFeatures(username, subscriptionId) } async setHomeServerConfiguration(configurationJSONString: string): Promise { return this.remoteBridge.setHomeServerConfiguration(configurationJSONString) } async getHomeServerConfiguration(): Promise { return this.remoteBridge.getHomeServerConfiguration() } async setHomeServerDataLocation(location: string): Promise { return this.remoteBridge.setHomeServerDataLocation(location) } startHomeServer(): Promise { return this.remoteBridge.startHomeServer() } stopHomeServer(): Promise { return this.remoteBridge.stopHomeServer() } getHomeServerLogs(): Promise { return this.remoteBridge.getHomeServerLogs() } openLocation(path: string): Promise { return this.remoteBridge.openLocation(path) } presentDirectoryPickerForLocationChangeAndTransferOld( appendPath: string, oldLocation?: string | undefined, ): Promise { return this.remoteBridge.presentDirectoryPickerForLocationChangeAndTransferOld(appendPath, oldLocation) } getDirectoryManagerLastErrorMessage(): Promise { return this.remoteBridge.getDirectoryManagerLastErrorMessage() } getFilesBackupsMappingFile(location: string): Promise { return this.remoteBridge.getFilesBackupsMappingFile(location) } getPlaintextBackupsMappingFile(location: string): Promise { return this.remoteBridge.getPlaintextBackupsMappingFile(location) } persistPlaintextBackupsMappingFile(location: string): Promise { return this.remoteBridge.persistPlaintextBackupsMappingFile(location) } getTextBackupsCount(location: string): Promise { return this.remoteBridge.getTextBackupsCount(location) } async getKeychainValue() { if (this.useNativeKeychain) { const keychainValue = await this.remoteBridge.getKeychainValue() return keychainValue } else { const value = window.localStorage.getItem(FallbackLocalStorageKey) if (value) { return JSON.parse(value) } } } async setKeychainValue(value: RawKeychainValue) { if (this.useNativeKeychain) { await this.remoteBridge.setKeychainValue(value) } else { window.localStorage.setItem(FallbackLocalStorageKey, JSON.stringify(value)) } } async clearRawKeychainValue() { if (this.useNativeKeychain) { await this.remoteBridge.clearKeychainValue() } else { window.localStorage.removeItem(FallbackLocalStorageKey) } } syncComponents(components: Component[]) { this.remoteBridge.syncComponents(components) } onSearch(text: string) { this.remoteBridge.onSearch(text) } async clearAllDataFromDevice(workspaceIdentifiers: string[]): Promise<{ killsApplication: boolean }> { await super.clearAllDataFromDevice(workspaceIdentifiers) this.remoteBridge.destroyAllData() return { killsApplication: true } } public isLegacyFilesBackupsEnabled(): Promise { return this.remoteBridge.isLegacyFilesBackupsEnabled() } public getLegacyFilesBackupsLocation(): Promise { return this.remoteBridge.getLegacyFilesBackupsLocation() } wasLegacyTextBackupsExplicitlyDisabled(): Promise { return this.remoteBridge.wasLegacyTextBackupsExplicitlyDisabled() } getUserDocumentsDirectory(): Promise { return this.remoteBridge.getUserDocumentsDirectory() } getLegacyTextBackupsLocation(): Promise { return this.remoteBridge.getLegacyTextBackupsLocation() } saveTextBackupData(workspaceId: string, data: string): Promise { return this.remoteBridge.saveTextBackupData(workspaceId, data) } savePlaintextNoteBackup(location: string, uuid: string, name: string, tags: string[], data: string): Promise { return this.remoteBridge.savePlaintextNoteBackup(location, uuid, name, tags, data) } async saveFilesBackupsFile( location: string, uuid: string, metaFile: string, downloadRequest: { chunkSizes: number[] valetToken: string url: string }, ): Promise<'success' | 'failed'> { return this.remoteBridge.saveFilesBackupsFile(location, uuid, metaFile, downloadRequest) } getFileBackupReadToken(filePath: string): Promise { return this.remoteBridge.getFileBackupReadToken(filePath) } migrateLegacyFileBackupsToNewStructure(newPath: string): Promise { return this.remoteBridge.migrateLegacyFileBackupsToNewStructure(newPath) } readNextChunk(token: string): Promise { return this.remoteBridge.readNextChunk(token) } monitorPlaintextBackupsLocationForChanges(backupsDirectory: string): Promise { return this.remoteBridge.monitorPlaintextBackupsLocationForChanges(backupsDirectory) } joinPaths(...paths: string[]): Promise { return this.remoteBridge.joinPaths(...paths) } async performHardReset(): Promise { console.error('performHardReset is not yet implemented') } isDeviceDestroyed(): boolean { return false } askForMediaAccess(type: 'camera' | 'microphone'): Promise { return this.remoteBridge.askForMediaAccess(type) } }