package org.incido.desktop import androidx.datastore.core.DataStore import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler import androidx.datastore.preferences.core.PreferenceDataStoreFactory import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.emptyPreferences import java.io.File import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import org.incido.compose.data.settings.SettingsRepository import org.incido.data.ImageOrientationNormalizer import org.incido.data.ObjectBoxStore import org.incido.data.crypto.AesGcmFieldCipher import org.incido.data.crypto.CipherHolder import org.incido.data.crypto.EncryptionVerifier import org.incido.data.crypto.NoteCryptoService import org.incido.data.repository.NoteRepositoryImpl import org.incido.domain.repository.NoteRepository /** * Hand-rolled DI container for the desktop build — the JVM counterpart of * the Android app's `AppContainer` (Development Plan §6, Phase 1, M1b). * * Construction order mirrors Android exactly, for the reason the plan * names: settings first, then the cipher install from the settings * snapshot BEFORE the repository exists (so the very first snapshot * already reads encrypted rows correctly), then the repository over the * shared [CipherHolder], then the crypto service + startup verifier. * * Key-source difference (M3 task 3): Android's key lives in a * Keystore-wrapped DataStore blob; desktop's counterpart is * [DesktopKeyProvider]'s `0600` key file — see there for why the keyring * fallback was chosen. PIN / biometric / screenshot gating is * intentionally NOT ported (BiometricPrompt and FLAG_SECURE are * Android-only; the plan says hide, don't fake — the desktop security * story is theme + encryption + recovery key only). * * [dataDir] owns every desktop-side file this container wires: the * ObjectBox library, `incido_settings.preferences_pb`, the `0600` field * key, and (separately) the window-state store. * * EXIF rotation remains the one absent Android behaviour (M4 — needs a * JVM image decoder). */ class DesktopContainer(val dataDir: File) { /** Process-wide scope for the verifier, mirroring AppContainer.appScope. */ val appScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) val settingsRepository: SettingsRepository = SettingsRepository(preferencesStore(dataDir, "incido_settings")) val keyProvider = DesktopKeyProvider(dataDir) /** Shared cipher slot consulted by the repository on every I/O. */ val cipherHolder = CipherHolder() val boxStore = ObjectBoxStore.init(dataDir) /** * Install the field cipher BEFORE the repository is constructed * (AppContainer's rule). `snapshot()`'s runBlocking is startup-safe — * the desktop equivalent of Application.onCreate is main() before the * window opens. The key file is only trusted when BOTH signals agree: * `isEncryptionActive` (user intent) AND a key actually on disk. */ init { val s = settingsRepository.snapshot() if (s.isEncryptionActive && keyProvider.hasKey()) { cipherHolder.cipher = AesGcmFieldCipher(keyProvider) } } val repository: NoteRepository = NoteRepositoryImpl( boxStore = boxStore, cipherHolder = cipherHolder, orientationNormalizer = ImageOrientationNormalizer.NoOp, ) /** Migration sweep + startup integrity checker — the shared service. */ val noteCryptoService = NoteCryptoService(boxStore, cipherHolder) /** Runs on every launch — feeds the "Encrypted" badge state. */ val encryptionVerifier = EncryptionVerifier(noteCryptoService, appScope) init { encryptionVerifier.checkNow() } fun shutdown() { appScope.cancel() ObjectBoxStore.reset() } } /** * One DataStore Preferences file inside [dataDir]. Each call site owns a * DISTINCT file (settings, window state, …) — the JVM factory is not a * name-keyed singleton like Android's `preferencesDataStore` delegate, so * uniqueness of the produced file is the caller's job (two DataStore * instances on one file would fight over the lock). */ fun preferencesStore(dataDir: File, name: String): DataStore = PreferenceDataStoreFactory.create( corruptionHandler = ReplaceFileCorruptionHandler { emptyPreferences() }, produceFile = { File(dataDir, "$name.preferences_pb") }, ) /** * Where the desktop library lives: `$XDG_DATA_HOME/incido`, falling back to * `~/.local/share/incido` (Development Plan §6 — a *separate* library per * machine; the backup file is the bridge to the phone, never the DB itself, * because ObjectBox is single-writer). */ fun defaultDataDir(): File { val sep = File.separator val base = System.getenv("XDG_DATA_HOME")?.takeIf { it.isNotBlank() } ?: System.getProperty("user.home") + sep + ".local" + sep + "share" return File(base + sep + "incido").also { it.mkdirs() } }