Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Chats from every app, Continuity Camera, a command palette, and more
Bee70da2e83e96b
diff --git a/Cargo.lock b/Cargo.lockindex d793c9f..0c3d13d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2340,20 +2340,28 @@ name = "nectarlink-core"version = "0.0.1"dependencies = ["bytes",+ "data-encoding","iroh","iroh-mdns-address-lookup","n0-future","nectarlink-protocol","rand",+ "ring","rusqlite",+ "rustls",+ "rustls-pki-types","serde","serde_json",+ "sha2",+ "socket2","tempfile","thiserror 2.0.21","tokio",+ "tokio-rustls","tokio-util","tracing","tracing-subscriber",+ "url","windows",]diff --git a/README.md b/README.mdindex b8bddfb..a4cd7f3 100644--- a/README.md+++ b/README.md@@ -129,10 +129,15 @@ tests; there are no releases yet.window and the app re-lays out to fit, its size and position are rememberedfor next time, and if it closes on the phone the window offers to open itagain.-- **Texts** on the PC: a Messages page with the phone's conversations- (pictures included), to reply or start a new one (searching contacts as- you type); texts go out through the phone. Search them, copy a one-time- code with one click, and open links.+- **Messages (SMS, MMS and chat apps)** on the PC: one calm Messages inbox+ combining the phone's SMS/MMS conversations (without duplicating texts the+ SMS provider already gives) and **Unified Conversations** from RCS, WhatsApp,+ Telegram, Signal and other `MessagingStyle` chat apps that arrived while+ connected, marked by app with a filter and inline reply via `RemoteInput`.+ View pictures in MMS threads, drag an image into a thread or paste one from+ the PC clipboard (`Ctrl+V`) to preview and send it as MMS, start a new+ conversation (searching contacts as you type), copy one-time codes with one+ click, and open links.- **Calls and contacts**: a Calls page with the phone's recent calls(grouped by day, missed calls marked, tap to call back or text), itscontacts (favorites first, with photos and search), and a keypad to calldiff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xmlindex ac0715e..f8cda8e 100644--- a/android/app/src/main/AndroidManifest.xml+++ b/android/app/src/main/AndroidManifest.xml@@ -261,6 +261,14 @@android:exported="false"android:foregroundServiceType="camera" />+ <!-- Continuity Camera: takes a photo or scans a document for a paired PC. -->+ <activity+ android:name=".camera.ContinuityCameraActivity"+ android:exported="false"+ android:showWhenLocked="true"+ android:turnScreenOn="true"+ android:theme="@style/Theme.Nectarlink" />+<serviceandroid:name=".service.ConnectionService"android:exported="false"diff --git a/android/app/src/main/kotlin/app/nectarlink/android/camera/ContinuityCameraActivity.kt b/android/app/src/main/kotlin/app/nectarlink/android/camera/ContinuityCameraActivity.ktnew file mode 100644index 0000000..1e0fb49--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/camera/ContinuityCameraActivity.kt@@ -0,0 +1,932 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.camera++import android.Manifest+import android.content.Intent+import android.content.pm.PackageManager+import android.graphics.Bitmap+import android.graphics.BitmapFactory+import android.graphics.ImageFormat+import android.graphics.Matrix+import android.os.Bundle+import androidx.activity.ComponentActivity+import androidx.activity.compose.BackHandler+import androidx.activity.compose.setContent+import androidx.activity.enableEdgeToEdge+import androidx.activity.result.contract.ActivityResultContracts+import androidx.camera.core.Camera+import androidx.camera.core.CameraSelector+import androidx.camera.core.ImageAnalysis+import androidx.camera.core.ImageCapture+import androidx.camera.core.ImageCaptureException+import androidx.camera.core.ImageProxy+import androidx.camera.core.Preview+import androidx.camera.lifecycle.ProcessCameraProvider+import androidx.camera.view.PreviewView+import androidx.compose.foundation.Canvas+import androidx.compose.foundation.Image+import androidx.compose.foundation.background+import androidx.compose.foundation.border+import androidx.compose.foundation.clickable+import androidx.compose.foundation.gestures.detectDragGestures+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.navigationBarsPadding+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.layout.statusBarsPadding+import androidx.compose.foundation.layout.width+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.material3.Button+import androidx.compose.material3.CircularProgressIndicator+import androidx.compose.material3.FilterChip+import androidx.compose.material3.MaterialTheme+import androidx.compose.material3.OutlinedButton+import androidx.compose.material3.Surface+import androidx.compose.material3.Text+import androidx.compose.material3.TextButton+import androidx.compose.runtime.Composable+import androidx.compose.runtime.DisposableEffect+import androidx.compose.runtime.LaunchedEffect+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberCoroutineScope+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.Path+import androidx.compose.ui.graphics.asImageBitmap+import androidx.compose.ui.graphics.drawscope.Stroke+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.layout.ContentScale+import androidx.compose.ui.layout.onSizeChanged+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.IntSize+import androidx.compose.ui.unit.dp+import androidx.compose.ui.viewinterop.AndroidView+import androidx.core.content.ContextCompat+import androidx.core.graphics.scale+import androidx.lifecycle.compose.LocalLifecycleOwner+import androidx.lifecycle.compose.collectAsStateWithLifecycle+import app.nectarlink.android.NectarlinkApplication+import app.nectarlink.android.R+import app.nectarlink.android.ui.Preferences+import app.nectarlink.android.ui.theme.NectarlinkTheme+import java.io.ByteArrayOutputStream+import java.util.concurrent.Executors+import kotlin.math.hypot+import kotlin.math.max+import kotlin.math.min+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.launch+import kotlinx.coroutines.withContext++/**+ * Full-screen Continuity Camera capture activity launched when a paired PC+ * requests a photo (`"photo"`) or document scan (`"scan"`).+ */+class ContinuityCameraActivity : ComponentActivity() {+ private var pcId: String = ""+ private var requestId: String = ""+ private var initialMode: String = "photo"+ private var pcName: String = ""+ private var completedOrCancelled = false++ private val cameraPermissionGranted = mutableStateOf(false)++ private val permissionLauncher = registerForActivityResult(+ ActivityResultContracts.RequestPermission(),+ ) { granted ->+ cameraPermissionGranted.value = granted+ if (!granted) {+ cancelAndFinish("Camera permission denied on phone")+ }+ }++ override fun onCreate(savedInstanceState: Bundle?) {+ super.onCreate(savedInstanceState)+ enableEdgeToEdge()+ if (!readIntent(intent)) {+ finish()+ return+ }+ ContinuityCameraRequests.dismissNotification(this, pcId)++ val hasPerm = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==+ PackageManager.PERMISSION_GRANTED+ cameraPermissionGranted.value = hasPerm+ if (!hasPerm) {+ permissionLauncher.launch(Manifest.permission.CAMERA)+ }++ val app = application as NectarlinkApplication+ val preferences = Preferences(this)++ setContent {+ val appearance by preferences.appearance.collectAsStateWithLifecycle()+ val activeRequest by ContinuityCameraRequests.active.collectAsStateWithLifecycle()+ val hasCamera by cameraPermissionGranted++ // Close automatically if the PC cancelled this capture request.+ LaunchedEffect(activeRequest) {+ if (activeRequest == null && !completedOrCancelled) {+ completedOrCancelled = true+ finish()+ }+ }++ NectarlinkTheme(appearance) {+ Surface(+ modifier = Modifier.fillMaxSize(),+ color = MaterialTheme.colorScheme.background,+ ) {+ if (hasCamera) {+ ContinuityCameraScreen(+ pcName = pcName,+ initialMode = initialMode,+ onSendResult = { mode, fileName, width, height, jpegBytes ->+ completedOrCancelled = true+ ContinuityCameraRequests.clearActive(pcId, requestId)+ ContinuityCameraRequests.dismissNotification(this@ContinuityCameraActivity, pcId)+ app.core.sendCameraCaptureResult(+ pcId = pcId,+ requestId = requestId,+ mode = mode,+ fileName = fileName,+ mime = "image/jpeg",+ width = width,+ height = height,+ data = jpegBytes,+ )+ finish()+ },+ onCancel = { reason ->+ cancelAndFinish(reason)+ },+ )+ } else {+ Box(+ modifier = Modifier+ .fillMaxSize()+ .padding(24.dp),+ contentAlignment = Alignment.Center,+ ) {+ Column(horizontalAlignment = Alignment.CenterHorizontally) {+ Text(+ text = stringResource(R.string.webcam_permission_title),+ style = MaterialTheme.typography.titleMedium,+ )+ Spacer(Modifier.height(12.dp))+ Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {+ OutlinedButton(onClick = { cancelAndFinish("Cancelled on phone") }) {+ Text(stringResource(R.string.action_cancel))+ }+ Button(onClick = { permissionLauncher.launch(Manifest.permission.CAMERA) }) {+ Text(stringResource(R.string.action_allow_camera))+ }+ }+ }+ }+ }+ }+ }+ }+ }++ override fun onNewIntent(intent: Intent) {+ super.onNewIntent(intent)+ setIntent(intent)+ if (readIntent(intent)) {+ completedOrCancelled = false+ ContinuityCameraRequests.dismissNotification(this, pcId)+ }+ }++ override fun onDestroy() {+ if (isFinishing && !completedOrCancelled && pcId.isNotEmpty() && requestId.isNotEmpty()) {+ completedOrCancelled = true+ ContinuityCameraRequests.clearActive(pcId, requestId)+ (application as? NectarlinkApplication)?.core?.cancelCameraCapture(+ pcId = pcId,+ requestId = requestId,+ reason = "Cancelled on phone",+ )+ }+ super.onDestroy()+ }++ private fun readIntent(intent: Intent?): Boolean {+ val i = intent ?: return false+ val id = i.getStringExtra(ContinuityCameraRequests.EXTRA_PC_ID).orEmpty()+ val req = i.getStringExtra(ContinuityCameraRequests.EXTRA_REQUEST_ID).orEmpty()+ if (id.isEmpty() || req.isEmpty()) return false+ pcId = id+ requestId = req+ initialMode = if (i.getStringExtra(ContinuityCameraRequests.EXTRA_MODE) == "scan") "scan" else "photo"+ pcName = i.getStringExtra(ContinuityCameraRequests.EXTRA_PC_NAME).orEmpty()+ .ifEmpty { getString(R.string.your_pc) }+ return true+ }++ private fun cancelAndFinish(reason: String) {+ if (!completedOrCancelled && pcId.isNotEmpty() && requestId.isNotEmpty()) {+ completedOrCancelled = true+ ContinuityCameraRequests.clearActive(pcId, requestId)+ ContinuityCameraRequests.dismissNotification(this, pcId)+ (application as? NectarlinkApplication)?.core?.cancelCameraCapture(+ pcId = pcId,+ requestId = requestId,+ reason = reason,+ )+ }+ finish()+ }+}++@Composable+private fun ContinuityCameraScreen(+ pcName: String,+ initialMode: String,+ onSendResult: (mode: String, fileName: String, width: Int, height: Int, jpegBytes: ByteArray) -> Unit,+ onCancel: (reason: String) -> Unit,+) {+ val context = LocalContext.current+ val lifecycleOwner = LocalLifecycleOwner.current+ val scope = rememberCoroutineScope()++ var mode by remember(initialMode) { mutableStateOf(initialMode) }+ var useFrontCamera by remember { mutableStateOf(false) }+ var flashOn by remember { mutableStateOf(false) }+ var liveQuad by remember { mutableStateOf(DocumentScannerProcessor.DEFAULT_QUAD) }++ var capturedBitmap by remember { mutableStateOf<Bitmap?>(null) }+ var editQuad by remember { mutableStateOf(DocumentScannerProcessor.DEFAULT_QUAD) }+ var enhanceScan by remember { mutableStateOf(true) }+ var showRectifiedPreview by remember { mutableStateOf(false) }+ var rectifiedBitmap by remember { mutableStateOf<Bitmap?>(null) }+ var busy by remember { mutableStateOf(false) }++ val previewView = remember {+ PreviewView(context).apply {+ scaleType = PreviewView.ScaleType.FIT_CENTER+ }+ }+ val cameraExecutor = remember { Executors.newSingleThreadExecutor() }+ var imageCapture by remember { mutableStateOf<ImageCapture?>(null) }+ var boundCamera by remember { mutableStateOf<Camera?>(null) }++ BackHandler {+ if (capturedBitmap != null && !busy) {+ capturedBitmap = null+ rectifiedBitmap = null+ showRectifiedPreview = false+ } else if (!busy) {+ onCancel("Cancelled on phone")+ }+ }++ // Recompute rectified preview whenever the user toggles preview in scan mode+ LaunchedEffect(capturedBitmap, editQuad, enhanceScan, showRectifiedPreview) {+ val src = capturedBitmap+ if (src != null && mode == "scan" && showRectifiedPreview) {+ rectifiedBitmap = withContext(Dispatchers.Default) {+ DocumentScannerProcessor.rectifyDocument(src, editQuad, enhanceScan)+ }+ } else {+ rectifiedBitmap = null+ }+ }++ DisposableEffect(lifecycleOwner, mode, useFrontCamera, capturedBitmap == null) {+ val providerFuture = ProcessCameraProvider.getInstance(context)+ if (capturedBitmap == null) {+ providerFuture.addListener({+ runCatching {+ val provider = providerFuture.get()+ val preview = Preview.Builder().build().also {+ it.surfaceProvider = previewView.surfaceProvider+ }+ val capture = ImageCapture.Builder()+ .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)+ .setFlashMode(+ if (flashOn) ImageCapture.FLASH_MODE_ON else ImageCapture.FLASH_MODE_OFF,+ )+ .build()+ imageCapture = capture++ val selector = if (useFrontCamera && mode == "photo") {+ CameraSelector.DEFAULT_FRONT_CAMERA+ } else {+ CameraSelector.DEFAULT_BACK_CAMERA+ }++ provider.unbindAll()+ val cam = if (mode == "scan") {+ val analysis = ImageAnalysis.Builder()+ .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)+ .build()+ var lastDetectMs = 0L+ analysis.setAnalyzer(cameraExecutor) { proxy ->+ proxy.use { frame ->+ val now = System.currentTimeMillis()+ if (now - lastDetectMs >= 120L) {+ lastDetectMs = now+ val lumaData = extractUprightLuma(frame)+ if (lumaData != null) {+ val (luma, w, h) = lumaData+ val quad = DocumentScannerProcessor.detectDocumentQuad(luma, w, h)+ ContextCompat.getMainExecutor(context).execute {+ liveQuad = quad+ }+ }+ }+ }+ }+ provider.bindToLifecycle(lifecycleOwner, selector, preview, capture, analysis)+ } else {+ provider.bindToLifecycle(lifecycleOwner, selector, preview, capture)+ }+ boundCamera = cam+ cam.cameraControl.enableTorch(flashOn && (!useFrontCamera || mode == "scan"))+ }+ }, ContextCompat.getMainExecutor(context))+ }++ onDispose {+ if (capturedBitmap != null && providerFuture.isDone) {+ runCatching { providerFuture.get().unbindAll() }+ }+ }+ }++ DisposableEffect(Unit) {+ onDispose {+ cameraExecutor.shutdown()+ }+ }++ Column(+ modifier = Modifier+ .fillMaxSize()+ .statusBarsPadding()+ .navigationBarsPadding(),+ ) {+ // Top header bar+ Row(+ modifier = Modifier+ .fillMaxWidth()+ .padding(horizontal = 16.dp, vertical = 10.dp),+ verticalAlignment = Alignment.CenterVertically,+ horizontalArrangement = Arrangement.SpaceBetween,+ ) {+ Column(modifier = Modifier.weight(1f)) {+ Text(+ text = if (mode == "scan") "Scan document" else "Take photo",+ style = MaterialTheme.typography.titleMedium,+ fontWeight = FontWeight.SemiBold,+ )+ Text(+ text = "For $pcName",+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }++ if (capturedBitmap == null) {+ Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {+ FilterChip(+ selected = mode == "photo",+ onClick = { mode = "photo" },+ label = { Text("Photo") },+ )+ FilterChip(+ selected = mode == "scan",+ onClick = {+ mode = "scan"+ useFrontCamera = false+ },+ label = { Text("Scan") },+ )+ }+ }++ Spacer(Modifier.width(8.dp))+ TextButton(+ onClick = { onCancel("Cancelled on phone") },+ enabled = !busy,+ ) {+ Text(stringResource(R.string.action_cancel))+ }+ }++ // Main camera or review area+ Box(+ modifier = Modifier+ .weight(1f)+ .fillMaxWidth()+ .background(Color.Black),+ contentAlignment = Alignment.Center,+ ) {+ val currentCaptured = capturedBitmap+ if (currentCaptured == null) {+ AndroidView(+ factory = { previewView },+ modifier = Modifier.fillMaxSize(),+ )+ if (mode == "scan") {+ val primaryColor = MaterialTheme.colorScheme.primary+ Canvas(modifier = Modifier.fillMaxSize()) {+ val q = liveQuad.ordered()+ val p1 = Offset(q.topLeft.x * size.width, q.topLeft.y * size.height)+ val p2 = Offset(q.topRight.x * size.width, q.topRight.y * size.height)+ val p3 = Offset(q.bottomRight.x * size.width, q.bottomRight.y * size.height)+ val p4 = Offset(q.bottomLeft.x * size.width, q.bottomLeft.y * size.height)+ val path = Path().apply {+ moveTo(p1.x, p1.y)+ lineTo(p2.x, p2.y)+ lineTo(p3.x, p3.y)+ lineTo(p4.x, p4.y)+ close()+ }+ drawPath(path, color = primaryColor.copy(alpha = 0.18f))+ drawPath(path, color = primaryColor, style = Stroke(width = 3.dp.toPx()))+ }+ }+ } else {+ val displayBitmap = if (mode == "scan" && showRectifiedPreview && rectifiedBitmap != null) {+ rectifiedBitmap!!+ } else {+ currentCaptured+ }+ var boxSize by remember { mutableStateOf(IntSize.Zero) }+ Box(+ modifier = Modifier+ .fillMaxSize()+ .padding(16.dp)+ .onSizeChanged { boxSize = it },+ contentAlignment = Alignment.Center,+ ) {+ Image(+ bitmap = displayBitmap.asImageBitmap(),+ contentDescription = null,+ contentScale = ContentScale.Fit,+ modifier = Modifier.fillMaxSize(),+ )+ if (mode == "scan" && !showRectifiedPreview && boxSize.width > 0 && boxSize.height > 0) {+ QuadCornerEditorOverlay(+ bitmapWidth = currentCaptured.width,+ bitmapHeight = currentCaptured.height,+ containerSize = boxSize,+ quad = editQuad,+ onQuadChanged = { editQuad = it },+ )+ }+ }+ }++ if (busy) {+ Box(+ modifier = Modifier+ .fillMaxSize()+ .background(Color.Black.copy(alpha = 0.45f)),+ contentAlignment = Alignment.Center,+ ) {+ CircularProgressIndicator()+ }+ }+ }++ // Bottom controls+ val currentCaptured = capturedBitmap+ if (currentCaptured == null) {+ Row(+ modifier = Modifier+ .fillMaxWidth()+ .padding(horizontal = 24.dp, vertical = 20.dp),+ verticalAlignment = Alignment.CenterVertically,+ horizontalArrangement = Arrangement.SpaceBetween,+ ) {+ OutlinedButton(+ onClick = {+ flashOn = !flashOn+ boundCamera?.cameraControl?.enableTorch(flashOn && (!useFrontCamera || mode == "scan"))+ imageCapture?.flashMode = if (flashOn) {+ ImageCapture.FLASH_MODE_ON+ } else {+ ImageCapture.FLASH_MODE_OFF+ }+ },+ enabled = !busy,+ ) {+ Text(if (flashOn) stringResource(R.string.webcam_torch_on) else stringResource(R.string.webcam_torch_off))+ }++ // Shutter button+ Box(+ modifier = Modifier+ .size(72.dp)+ .clip(CircleShape)+ .border(4.dp, MaterialTheme.colorScheme.primary, CircleShape)+ .padding(6.dp)+ .clip(CircleShape)+ .background(MaterialTheme.colorScheme.primary)+ .clickable(enabled = !busy) {+ val cap = imageCapture+ if (cap == null) {+ val fallback = previewView.bitmap ?: return@clickable+ onPhotoCaptured(+ bmp = fallback,+ mode = mode,+ fallbackQuad = liveQuad,+ onReady = { bmp, q ->+ capturedBitmap = bmp+ editQuad = q+ },+ )+ return@clickable+ }+ busy = true+ cap.takePicture(+ cameraExecutor,+ object : ImageCapture.OnImageCapturedCallback() {+ override fun onCaptureSuccess(image: ImageProxy) {+ val decoded = imageProxyToBitmap(image, mirrorHorizontally = useFrontCamera && mode == "photo")+ image.close()+ val detectedQuad = if (decoded != null && mode == "scan") {+ DocumentScannerProcessor.detectDocumentQuad(decoded)+ } else {+ liveQuad+ }+ ContextCompat.getMainExecutor(context).execute {+ busy = false+ val finalBmp = decoded ?: previewView.bitmap+ if (finalBmp != null) {+ capturedBitmap = finalBmp+ editQuad = detectedQuad+ showRectifiedPreview = false+ }+ }+ }++ override fun onError(exception: ImageCaptureException) {+ ContextCompat.getMainExecutor(context).execute {+ busy = false+ val fallback = previewView.bitmap+ if (fallback != null) {+ onPhotoCaptured(+ bmp = fallback,+ mode = mode,+ fallbackQuad = liveQuad,+ onReady = { bmp, q ->+ capturedBitmap = bmp+ editQuad = q+ },+ )+ }+ }+ }+ },+ )+ },+ )++ if (mode == "photo") {+ OutlinedButton(+ onClick = { useFrontCamera = !useFrontCamera },+ enabled = !busy,+ ) {+ Text(stringResource(R.string.webcam_switch_camera))+ }+ } else {+ Spacer(Modifier.width(88.dp))+ }+ }+ } else {+ Column(+ modifier = Modifier+ .fillMaxWidth()+ .padding(horizontal = 20.dp, vertical = 14.dp),+ verticalArrangement = Arrangement.spacedBy(10.dp),+ ) {+ if (mode == "scan") {+ Row(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterHorizontally),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ FilterChip(+ selected = enhanceScan,+ onClick = { enhanceScan = !enhanceScan },+ label = { Text("Enhance document") },+ )+ FilterChip(+ selected = showRectifiedPreview,+ onClick = { showRectifiedPreview = !showRectifiedPreview },+ label = { Text(if (showRectifiedPreview) "Adjust corners" else "Preview scan") },+ )+ }+ }++ Row(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.spacedBy(12.dp),+ ) {+ OutlinedButton(+ onClick = {+ capturedBitmap = null+ rectifiedBitmap = null+ showRectifiedPreview = false+ },+ enabled = !busy,+ modifier = Modifier.weight(1f),+ ) {+ Text("Retake")+ }+ Button(+ onClick = {+ if (busy) return@Button+ busy = true+ scope.launch {+ val result = withContext(Dispatchers.Default) {+ val finalBmp = if (mode == "scan") {+ DocumentScannerProcessor.rectifyDocument(currentCaptured, editQuad, enhanceScan)+ } else {+ scaleDownIfNeeded(currentCaptured, MAX_OUTPUT_PX)+ }+ val jpeg = encodeJpegUnderLimit(finalBmp)+ val prefix = if (mode == "scan") "scan" else "photo"+ val name = "$prefix-${System.currentTimeMillis()}.jpg"+ Triple(name, finalBmp.width to finalBmp.height, jpeg)+ }+ val (fileName, dims, jpegBytes) = result+ onSendResult(mode, fileName, dims.first, dims.second, jpegBytes)+ }+ },+ enabled = !busy,+ modifier = Modifier.weight(1f),+ ) {+ Text(if (mode == "scan") "Send scan" else "Use photo")+ }+ }+ }+ }+ }+}++private const val MAX_OUTPUT_PX = 2400+private const val MAX_JPEG_BYTES = 16 * 1024 * 1024++private fun onPhotoCaptured(+ bmp: Bitmap,+ mode: String,+ fallbackQuad: QuadCorners,+ onReady: (Bitmap, QuadCorners) -> Unit,+) {+ val q = if (mode == "scan") {+ runCatching { DocumentScannerProcessor.detectDocumentQuad(bmp) }.getOrDefault(fallbackQuad)+ } else {+ fallbackQuad+ }+ onReady(bmp, q)+}++@Composable+private fun QuadCornerEditorOverlay(+ bitmapWidth: Int,+ bitmapHeight: Int,+ containerSize: IntSize,+ quad: QuadCorners,+ onQuadChanged: (QuadCorners) -> Unit,+) {+ val primaryColor = MaterialTheme.colorScheme.primary+ val surfaceColor = MaterialTheme.colorScheme.surface++ val containerW = containerSize.width.toFloat()+ val containerH = containerSize.height.toFloat()+ val bmpAspect = bitmapWidth.toFloat() / bitmapHeight.coerceAtLeast(1).toFloat()+ val containerAspect = containerW / containerH.coerceAtLeast(1f)++ val (drawW, drawH) = if (bmpAspect > containerAspect) {+ containerW to (containerW / bmpAspect)+ } else {+ (containerH * bmpAspect) to containerH+ }+ val offsetX = (containerW - drawW) * 0.5f+ val offsetY = (containerH - drawH) * 0.5f++ fun toScreen(pt: PointF2D): Offset = Offset(+ x = offsetX + pt.x * drawW,+ y = offsetY + pt.y * drawH,+ )++ fun toNormalized(screen: Offset): PointF2D = PointF2D(+ x = ((screen.x - offsetX) / drawW.coerceAtLeast(1f)).coerceIn(0.01f, 0.99f),+ y = ((screen.y - offsetY) / drawH.coerceAtLeast(1f)).coerceIn(0.01f, 0.99f),+ )++ var activeCorner by remember { mutableStateOf(-1) }+ var currentQuad by remember(quad) { mutableStateOf(quad) }++ Canvas(+ modifier = Modifier+ .fillMaxSize()+ .pointerInput(drawW, drawH, offsetX, offsetY) {+ detectDragGestures(+ onDragStart = { startOffset ->+ val pts = listOf(+ toScreen(currentQuad.topLeft),+ toScreen(currentQuad.topRight),+ toScreen(currentQuad.bottomRight),+ toScreen(currentQuad.bottomLeft),+ )+ var bestIdx = -1+ var bestDist = Float.MAX_VALUE+ for (i in pts.indices) {+ val d = hypot(pts[i].x - startOffset.x, pts[i].y - startOffset.y)+ if (d < bestDist) {+ bestDist = d+ bestIdx = i+ }+ }+ activeCorner = bestIdx+ },+ onDragEnd = {+ activeCorner = -1+ onQuadChanged(currentQuad.ordered())+ },+ onDragCancel = {+ activeCorner = -1+ },+ onDrag = { change, _ ->+ change.consume()+ val norm = toNormalized(change.position)+ currentQuad = when (activeCorner) {+ 0 -> currentQuad.copy(topLeft = norm)+ 1 -> currentQuad.copy(topRight = norm)+ 2 -> currentQuad.copy(bottomRight = norm)+ 3 -> currentQuad.copy(bottomLeft = norm)+ else -> currentQuad+ }+ onQuadChanged(currentQuad)+ },+ )+ },+ ) {+ val p1 = toScreen(currentQuad.topLeft)+ val p2 = toScreen(currentQuad.topRight)+ val p3 = toScreen(currentQuad.bottomRight)+ val p4 = toScreen(currentQuad.bottomLeft)++ val path = Path().apply {+ moveTo(p1.x, p1.y)+ lineTo(p2.x, p2.y)+ lineTo(p3.x, p3.y)+ lineTo(p4.x, p4.y)+ close()+ }+ drawPath(path, color = primaryColor.copy(alpha = 0.20f))+ drawPath(path, color = primaryColor, style = Stroke(width = 3.dp.toPx()))++ for (pt in listOf(p1, p2, p3, p4)) {+ drawCircle(color = primaryColor, radius = 12.dp.toPx(), center = pt)+ drawCircle(color = surfaceColor, radius = 6.dp.toPx(), center = pt)+ }+ }+}++private fun extractUprightLuma(image: ImageProxy): Triple<ByteArray, Int, Int>? = runCatching {+ val plane = image.planes.firstOrNull() ?: return null+ val buf = plane.buffer+ val rowStride = plane.rowStride+ val pixelStride = plane.pixelStride+ val srcW = image.width+ val srcH = image.height+ val rot = ((image.imageInfo.rotationDegrees % 360) + 360) % 360+ val uprightW = if (rot == 90 || rot == 270) srcH else srcW+ val uprightH = if (rot == 90 || rot == 270) srcW else srcH+ val scale = min(1f, 180f / max(uprightW, uprightH).toFloat())+ val dstW = (uprightW * scale).toInt().coerceAtLeast(32)+ val dstH = (uprightH * scale).toInt().coerceAtLeast(32)+ val out = ByteArray(dstW * dstH)+ val limit = buf.limit()++ for (dy in 0 until dstH) {+ val uy = dy * uprightH / dstH+ for (dx in 0 until dstW) {+ val ux = dx * uprightW / dstW+ val sx: Int+ val sy: Int+ when (rot) {+ 90 -> {+ sx = uy+ sy = srcH - 1 - ux+ }+ 180 -> {+ sx = srcW - 1 - ux+ sy = srcH - 1 - uy+ }+ 270 -> {+ sx = srcW - 1 - uy+ sy = ux+ }+ else -> {+ sx = ux+ sy = uy+ }+ }+ val idx = sy * rowStride + sx * pixelStride+ out[dy * dstW + dx] = if (idx in 0 until limit) buf.get(idx) else 0+ }+ }+ Triple(out, dstW, dstH)+}.getOrNull()++private fun imageProxyToBitmap(image: ImageProxy, mirrorHorizontally: Boolean): Bitmap? = runCatching {+ val rawBitmap = if (image.format == ImageFormat.JPEG || image.planes.size == 1) {+ val buf = image.planes[0].buffer+ val bytes = ByteArray(buf.remaining()).also { buf.get(it) }+ BitmapFactory.decodeByteArray(bytes, 0, bytes.size)+ } else {+ yuv420ToBitmap(image)+ } ?: return null++ val rot = ((image.imageInfo.rotationDegrees % 360) + 360) % 360+ val rotated = if (rot != 0 || mirrorHorizontally) {+ val matrix = Matrix().apply {+ if (rot != 0) postRotate(rot.toFloat())+ if (mirrorHorizontally) postScale(-1f, 1f)+ }+ Bitmap.createBitmap(rawBitmap, 0, 0, rawBitmap.width, rawBitmap.height, matrix, true)+ } else {+ rawBitmap+ }+ scaleDownIfNeeded(rotated, MAX_OUTPUT_PX)+}.getOrNull()++private fun yuv420ToBitmap(image: ImageProxy): Bitmap? {+ val w = image.width+ val h = image.height+ val yPlane = image.planes[0]+ val uPlane = image.planes[1]+ val vPlane = image.planes[2]+ val yBuf = yPlane.buffer+ val uBuf = uPlane.buffer+ val vBuf = vPlane.buffer+ val yRowStride = yPlane.rowStride+ val yPixStride = yPlane.pixelStride+ val uvRowStride = uPlane.rowStride+ val uvPixStride = uPlane.pixelStride+ val pixels = IntArray(w * h)++ for (y in 0 until h) {+ val yRow = y * yRowStride+ val uvRow = (y ushr 1) * uvRowStride+ for (x in 0 until w) {+ val yVal = (yBuf.get(yRow + x * yPixStride).toInt() and 0xFF)+ val uvIdx = uvRow + (x ushr 1) * uvPixStride+ val uVal = (uBuf.get(uvIdx).toInt() and 0xFF) - 128+ val vVal = (vBuf.get(uvIdx).toInt() and 0xFF) - 128+ val r = (yVal + (1.370705f * vVal).toInt()).coerceIn(0, 255)+ val g = (yVal - (0.698001f * vVal).toInt() - (0.337633f * uVal).toInt()).coerceIn(0, 255)+ val b = (yVal + (1.732446f * uVal).toInt()).coerceIn(0, 255)+ pixels[y * w + x] = (0xFF shl 24) or (r shl 16) or (g shl 8) or b+ }+ }+ return Bitmap.createBitmap(pixels, w, h, Bitmap.Config.ARGB_8888)+}++private fun scaleDownIfNeeded(bitmap: Bitmap, maxSide: Int): Bitmap {+ val longest = max(bitmap.width, bitmap.height)+ if (longest <= maxSide) return bitmap+ val scale = maxSide.toFloat() / longest.toFloat()+ val targetW = (bitmap.width * scale).toInt().coerceAtLeast(1)+ val targetH = (bitmap.height * scale).toInt().coerceAtLeast(1)+ return bitmap.scale(targetW, targetH)+}++private fun encodeJpegUnderLimit(bitmap: Bitmap): ByteArray {+ for (q in intArrayOf(90, 82, 72)) {+ val bytes = ByteArrayOutputStream().use { out ->+ bitmap.compress(Bitmap.CompressFormat.JPEG, q, out)+ out.toByteArray()+ }+ if (bytes.size <= MAX_JPEG_BYTES) return bytes+ }+ val smaller = scaleDownIfNeeded(bitmap, 1600)+ return ByteArrayOutputStream().use { out ->+ smaller.compress(Bitmap.CompressFormat.JPEG, 80, out)+ out.toByteArray()+ }+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/camera/ContinuityCameraRequest.kt b/android/app/src/main/kotlin/app/nectarlink/android/camera/ContinuityCameraRequest.ktnew file mode 100644index 0000000..5b264fa--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/camera/ContinuityCameraRequest.kt@@ -0,0 +1,154 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.camera++import android.Manifest+import android.app.NotificationChannel+import android.app.NotificationManager+import android.app.PendingIntent+import android.content.Context+import android.content.Intent+import android.content.pm.PackageManager+import androidx.core.app.NotificationCompat+import androidx.core.app.NotificationManagerCompat+import androidx.core.content.ContextCompat+import app.nectarlink.android.R+import kotlinx.coroutines.flow.MutableStateFlow+import kotlinx.coroutines.flow.StateFlow+import kotlinx.coroutines.flow.asStateFlow+import kotlinx.coroutines.flow.update++/** A paired PC asking this phone to capture a photo or scan a document (`camera.capture`). */+data class ContinuityCameraRequest(+ val pcId: String,+ val requestId: String,+ /** `"photo"` or `"scan"`. */+ val mode: String,+ val pcName: String,+)++/**+ * Coordinates Continuity Camera requests from paired PCs: launches+ * [ContinuityCameraActivity] directly when allowed, posts a high-priority+ * heads-up notification with full-screen intent, and propagates cancellations+ * from the PC so any open capture screen closes cleanly.+ */+object ContinuityCameraRequests {+ private const val CHANNEL = "continuity_camera_requests"+ private const val TAG = "continuity_camera_req"+ private const val REQUEST_TIMEOUT_MS = 2 * 60 * 1000L++ const val EXTRA_PC_ID = "continuity_pc_id"+ const val EXTRA_REQUEST_ID = "continuity_request_id"+ const val EXTRA_MODE = "continuity_mode"+ const val EXTRA_PC_NAME = "continuity_pc_name"++ private val _active = MutableStateFlow<ContinuityCameraRequest?>(null)+ val active: StateFlow<ContinuityCameraRequest?> = _active.asStateFlow()++ fun createChannel(context: Context) {+ context.getSystemService(NotificationManager::class.java)?.createNotificationChannel(+ NotificationChannel(+ CHANNEL,+ "Camera capture requests",+ NotificationManager.IMPORTANCE_HIGH,+ ),+ )+ }++ /**+ * Handles an incoming `camera.capture` request from `pcId`: updates active+ * state, starts [ContinuityCameraActivity] when foregrounded, and posts a+ * heads-up notification with full-screen intent.+ */+ fun show(+ context: Context,+ pcId: String,+ requestId: String,+ mode: String,+ pcName: String,+ ): Boolean {+ val cleanMode = if (mode == "scan") "scan" else "photo"+ val fromName = pcName.ifEmpty { context.getString(R.string.your_pc) }+ val req = ContinuityCameraRequest(+ pcId = pcId,+ requestId = requestId,+ mode = cleanMode,+ pcName = fromName,+ )+ _active.value = req++ val openIntent = Intent(context, ContinuityCameraActivity::class.java).apply {+ putExtra(EXTRA_PC_ID, pcId)+ putExtra(EXTRA_REQUEST_ID, requestId)+ putExtra(EXTRA_MODE, cleanMode)+ putExtra(EXTRA_PC_NAME, fromName)+ flags = Intent.FLAG_ACTIVITY_NEW_TASK or+ Intent.FLAG_ACTIVITY_SINGLE_TOP or+ Intent.FLAG_ACTIVITY_CLEAR_TOP+ }++ // Launch directly if the app is currently foregrounded or allowed to start activities.+ runCatching { context.startActivity(openIntent) }++ if (ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) ==+ PackageManager.PERMISSION_GRANTED+ ) {+ createChannel(context)+ val openPending = PendingIntent.getActivity(+ context,+ requestId.hashCode(),+ openIntent,+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,+ )+ val title = if (cleanMode == "scan") {+ "$fromName wants to scan a document"+ } else {+ "$fromName wants to take a photo"+ }+ val text = if (cleanMode == "scan") {+ "Tap to scan a document with your camera."+ } else {+ "Tap to take a photo with your camera."+ }+ val notification = NotificationCompat.Builder(context, CHANNEL)+ .setSmallIcon(R.drawable.ic_notification)+ .setContentTitle(title)+ .setContentText(text)+ .setContentIntent(openPending)+ .setFullScreenIntent(openPending, true)+ .setAutoCancel(true)+ .setTimeoutAfter(REQUEST_TIMEOUT_MS)+ .setCategory(NotificationCompat.CATEGORY_CALL)+ .setPriority(NotificationCompat.PRIORITY_HIGH)+ .build()+ runCatching {+ NotificationManagerCompat.from(context).notify(TAG, pcId.hashCode(), notification)+ }+ }+ return true+ }++ /** Called when the PC cancels an active `camera.capture` request. */+ fun cancelFromPc(context: Context, pcId: String, requestId: String) {+ NotificationManagerCompat.from(context).cancel(TAG, pcId.hashCode())+ _active.update { current ->+ if (current != null && current.pcId == pcId && (requestId.isEmpty() || current.requestId == requestId)) {+ null+ } else {+ current+ }+ }+ }++ /** Dismisses the heads-up notification once the activity opens or completes. */+ fun dismissNotification(context: Context, pcId: String) {+ NotificationManagerCompat.from(context).cancel(TAG, pcId.hashCode())+ }++ /** Clears active request tracking when the phone finishes or cancels capture. */+ fun clearActive(pcId: String, requestId: String) {+ _active.update { current ->+ if (current != null && current.pcId == pcId && current.requestId == requestId) null else current+ }+ }+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/camera/DocumentScannerProcessor.kt b/android/app/src/main/kotlin/app/nectarlink/android/camera/DocumentScannerProcessor.ktnew file mode 100644index 0000000..5e540a7--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/camera/DocumentScannerProcessor.kt@@ -0,0 +1,637 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.camera++import android.graphics.Bitmap+import kotlin.math.PI+import kotlin.math.abs+import kotlin.math.acos+import kotlin.math.atan2+import kotlin.math.cos+import kotlin.math.hypot+import kotlin.math.max+import kotlin.math.min+import kotlin.math.roundToInt+import kotlin.math.sin+import kotlin.math.sqrt++/** A 2D point in either normalized (`0f..1f`) or pixel coordinates. */+data class PointF2D(val x: Float, val y: Float)++/** Four corners of a document quadrilateral in clockwise order. */+data class QuadCorners(+ val topLeft: PointF2D,+ val topRight: PointF2D,+ val bottomRight: PointF2D,+ val bottomLeft: PointF2D,+) {+ fun toList(): List<PointF2D> = listOf(topLeft, topRight, bottomRight, bottomLeft)++ /**+ * Orders any 4 points clockwise (`topLeft`, `topRight`, `bottomRight`, `bottomLeft`)+ * using coordinate sum `(x + y)` and difference `(y - x)`.+ */+ fun ordered(): QuadCorners {+ val pts = toList()+ val tl = pts.minBy { it.x + it.y }+ val br = pts.maxBy { it.x + it.y }+ val remaining = pts.filter { it !== tl && it !== br }.let { rem ->+ if (rem.size == 2) rem else pts.sortedBy { it.x + it.y }.subList(1, 3)+ }+ val tr = remaining.minBy { it.y - it.x }+ val bl = remaining.maxBy { it.y - it.x }+ return QuadCorners(+ topLeft = tl,+ topRight = tr,+ bottomRight = br,+ bottomLeft = bl,+ )+ }++ /** Scales normalized (`0f..1f`) corners into pixel coordinates. */+ fun toPixelSpace(width: Int, height: Int): QuadCorners {+ val w = (width - 1).coerceAtLeast(1).toFloat()+ val h = (height - 1).coerceAtLeast(1).toFloat()+ return QuadCorners(+ topLeft = PointF2D(topLeft.x * w, topLeft.y * h),+ topRight = PointF2D(topRight.x * w, topRight.y * h),+ bottomRight = PointF2D(bottomRight.x * w, bottomRight.y * h),+ bottomLeft = PointF2D(bottomLeft.x * w, bottomLeft.y * h),+ )+ }+}++/**+ * Pure-Kotlin document quad detector and 4-corner perspective rectifier for+ * Continuity Camera's document scanner.+ */+object DocumentScannerProcessor {+ private const val MAX_WORK_SIDE = 180+ private const val MIN_QUAD_AREA_FRACTION = 0.15f+ private const val MIN_INTERIOR_ANGLE_DEG = 45f+ private const val MAX_INTERIOR_ANGLE_DEG = 135f++ val DEFAULT_QUAD = QuadCorners(+ topLeft = PointF2D(0.08f, 0.08f),+ topRight = PointF2D(0.92f, 0.08f),+ bottomRight = PointF2D(0.92f, 0.92f),+ bottomLeft = PointF2D(0.08f, 0.92f),+ )++ /**+ * Detects the dominant document quadrilateral in an 8-bit luminance (`Y`) buffer,+ * returning normalized (`0f..1f`) clockwise corners. Falls back to [DEFAULT_QUAD]+ * if no valid convex document quad is found.+ */+ fun detectDocumentQuad(luma: ByteArray, width: Int, height: Int): QuadCorners {+ if (width < 16 || height < 16 || luma.size < width * height) return DEFAULT_QUAD++ val maxSide = max(width, height)+ val scale = if (maxSide > MAX_WORK_SIDE) MAX_WORK_SIDE.toFloat() / maxSide.toFloat() else 1f+ val gw = (width * scale).roundToInt().coerceAtLeast(16)+ val gh = (height * scale).roundToInt().coerceAtLeast(16)++ val raw = IntArray(gw * gh)+ for (y in 0 until gh) {+ val sy = (y * height / gh).coerceIn(0, height - 1)+ val rowOffset = sy * width+ for (x in 0 until gw) {+ val sx = (x * width / gw).coerceIn(0, width - 1)+ raw[y * gw + x] = luma[rowOffset + sx].toInt() and 0xFF+ }+ }++ // 3x3 Gaussian blur: [1, 2, 1; 2, 4, 2; 1, 2, 1] / 16+ val blurred = IntArray(gw * gh)+ for (y in 0 until gh) {+ val ym = max(0, y - 1)+ val yp = min(gh - 1, y + 1)+ for (x in 0 until gw) {+ val xm = max(0, x - 1)+ val xp = min(gw - 1, x + 1)+ val sum = raw[ym * gw + xm] + 2 * raw[ym * gw + x] + raw[ym * gw + xp] ++ 2 * raw[y * gw + xm] + 4 * raw[y * gw + x] + 2 * raw[y * gw + xp] ++ raw[yp * gw + xm] + 2 * raw[yp * gw + x] + raw[yp * gw + xp]+ blurred[y * gw + x] = sum ushr 4+ }+ }++ // Sobel gradient magnitudes+ val mag = FloatArray(gw * gh)+ var maxMag = 0f+ for (y in 1 until gh - 1) {+ for (x in 1 until gw - 1) {+ val gx = -blurred[(y - 1) * gw + (x - 1)] + blurred[(y - 1) * gw + (x + 1)] -+ 2 * blurred[y * gw + (x - 1)] + 2 * blurred[y * gw + (x + 1)] -+ blurred[(y + 1) * gw + (x - 1)] + blurred[(y + 1) * gw + (x + 1)]+ val gy = -blurred[(y - 1) * gw + (x - 1)] - 2 * blurred[(y - 1) * gw + x] -+ blurred[(y - 1) * gw + (x + 1)] + blurred[(y + 1) * gw + (x - 1)] ++ 2 * blurred[(y + 1) * gw + x] + blurred[(y + 1) * gw + (x + 1)]+ val m = hypot(gx.toFloat(), gy.toFloat())+ mag[y * gw + x] = m+ if (m > maxMag) maxMag = m+ }+ }++ val otsu = otsuThreshold(blurred)+ val rawFg = BooleanArray(gw * gh) { blurred[it] > otsu }++ // 3x3 majority smoothing to remove isolated specks+ val fg = BooleanArray(gw * gh)+ var fgCount = 0+ for (y in 1 until gh - 1) {+ for (x in 1 until gw - 1) {+ var count = 0+ for (dy in -1..1) {+ for (dx in -1..1) {+ if (rawFg[(y + dy) * gw + (x + dx)]) count+++ }+ }+ if (count >= 5) {+ fg[y * gw + x] = true+ fgCount+++ }+ }+ }++ val totalInterior = (gw - 2) * (gh - 2)+ val fgFraction = fgCount.toFloat() / totalInterior.coerceAtLeast(1).toFloat()++ // Extract candidate boundary points and foreground moments+ val boundaryX = IntArray(gw * gh)+ val boundaryY = IntArray(gw * gh)+ var boundaryCount = 0++ var sumX = 0.0+ var sumY = 0.0+ var momentCount = 0++ if (fgFraction in 0.12f..0.92f) {+ for (y in 1 until gh - 1) {+ for (x in 1 until gw - 1) {+ val idx = y * gw + x+ if (!fg[idx]) continue+ sumX += x+ sumY += y+ momentCount+++ val isBoundary = !fg[(y - 1) * gw + x] ||+ !fg[(y + 1) * gw + x] ||+ !fg[y * gw + (x - 1)] ||+ !fg[y * gw + (x + 1)]+ if (isBoundary) {+ boundaryX[boundaryCount] = x+ boundaryY[boundaryCount] = y+ boundaryCount+++ }+ }+ }+ }++ // Fallback to strong Sobel edge pixels if bright-region segmentation wasn't decisive+ if (boundaryCount < 16 && maxMag > 1f) {+ val edgeThresh = maxMag * 0.30f+ boundaryCount = 0+ sumX = 0.0+ sumY = 0.0+ momentCount = 0+ for (y in 1 until gh - 1) {+ for (x in 1 until gw - 1) {+ if (mag[y * gw + x] >= edgeThresh) {+ boundaryX[boundaryCount] = x+ boundaryY[boundaryCount] = y+ boundaryCount+++ sumX += x+ sumY += y+ momentCount+++ }+ }+ }+ }++ if (boundaryCount < 4 || momentCount == 0) return DEFAULT_QUAD++ val cx = sumX / momentCount+ val cy = sumY / momentCount++ // Compute second central moments to align with the document's principal axes+ var mu20 = 0.0+ var mu02 = 0.0+ var mu11 = 0.0+ if (fgFraction in 0.12f..0.92f) {+ for (y in 1 until gh - 1) {+ for (x in 1 until gw - 1) {+ if (!fg[y * gw + x]) continue+ val dx = x - cx+ val dy = y - cy+ mu20 += dx * dx+ mu02 += dy * dy+ mu11 += dx * dy+ }+ }+ } else {+ for (i in 0 until boundaryCount) {+ val dx = boundaryX[i] - cx+ val dy = boundaryY[i] - cy+ mu20 += dx * dx+ mu02 += dy * dy+ mu11 += dx * dy+ }+ }++ val theta = 0.5 * atan2(2.0 * mu11, mu20 - mu02)+ val cosT = cos(theta)+ val sinT = sin(theta)++ var varXi = 0.0+ var varEta = 0.0+ for (i in 0 until boundaryCount) {+ val dx = boundaryX[i] - cx+ val dy = boundaryY[i] - cy+ val xi = dx * cosT + dy * sinT+ val eta = -dx * sinT + dy * cosT+ varXi += xi * xi+ varEta += eta * eta+ }+ val sigmaXi = sqrt(varXi / boundaryCount).coerceAtLeast(1.0)+ val sigmaEta = sqrt(varEta / boundaryCount).coerceAtLeast(1.0)++ var minSum = Double.POSITIVE_INFINITY+ var maxSum = Double.NEGATIVE_INFINITY+ var minDiff = Double.POSITIVE_INFINITY+ var maxDiff = Double.NEGATIVE_INFINITY+ var idx1 = 0+ var idx2 = 0+ var idx3 = 0+ var idx4 = 0++ for (i in 0 until boundaryCount) {+ val dx = boundaryX[i] - cx+ val dy = boundaryY[i] - cy+ val xi = (dx * cosT + dy * sinT) / sigmaXi+ val eta = (-dx * sinT + dy * cosT) / sigmaEta+ val s = xi + eta+ val d = xi - eta+ if (s < minSum) {+ minSum = s+ idx1 = i+ }+ if (d > maxDiff) {+ maxDiff = d+ idx2 = i+ }+ if (s > maxSum) {+ maxSum = s+ idx3 = i+ }+ if (d < minDiff) {+ minDiff = d+ idx4 = i+ }+ }++ val invW = 1f / (gw - 1).coerceAtLeast(1).toFloat()+ val invH = 1f / (gh - 1).coerceAtLeast(1).toFloat()+ val candidate = QuadCorners(+ topLeft = PointF2D(boundaryX[idx1] * invW, boundaryY[idx1] * invH),+ topRight = PointF2D(boundaryX[idx2] * invW, boundaryY[idx2] * invH),+ bottomRight = PointF2D(boundaryX[idx3] * invW, boundaryY[idx3] * invH),+ bottomLeft = PointF2D(boundaryX[idx4] * invW, boundaryY[idx4] * invH),+ ).ordered()++ return if (isValidDocumentQuad(candidate)) candidate else DEFAULT_QUAD+ }++ /**+ * Checks that a normalized quad is convex, covers at least 15% of the frame,+ * and has interior angles between 45° and 135°.+ */+ fun isValidDocumentQuad(quad: QuadCorners): Boolean {+ val pts = quad.ordered().toList()+ if (pts.any { it.x !in 0f..1f || it.y !in 0f..1f }) return false++ // Convexity and interior angle check in clockwise order (screen Y-down)+ for (i in 0 until 4) {+ val prev = pts[(i + 3) % 4]+ val curr = pts[i]+ val next = pts[(i + 1) % 4]++ val ux = prev.x - curr.x+ val uy = prev.y - curr.y+ val vx = next.x - curr.x+ val vy = next.y - curr.y++ val lenU = hypot(ux, uy)+ val lenV = hypot(vx, vy)+ if (lenU < 0.05f || lenV < 0.05f) return false++ // For clockwise points in Y-down coordinates, edge1 x edge2 > 0+ val edge1x = curr.x - prev.x+ val edge1y = curr.y - prev.y+ val edge2x = next.x - curr.x+ val edge2y = next.y - curr.y+ val cross = edge1x * edge2y - edge1y * edge2x+ if (cross <= 1e-4f) return false++ val cosAngle = ((ux * vx + uy * vy) / (lenU * lenV)).coerceIn(-1f, 1f)+ val angleDeg = (acos(cosAngle) * (180.0 / PI)).toFloat()+ if (angleDeg !in MIN_INTERIOR_ANGLE_DEG..MAX_INTERIOR_ANGLE_DEG) return false+ }++ // Shoelace area+ var doubleArea = 0f+ for (i in 0 until 4) {+ val p1 = pts[i]+ val p2 = pts[(i + 1) % 4]+ doubleArea += p1.x * p2.y - p2.x * p1.y+ }+ val area = abs(doubleArea) * 0.5f+ return area >= MIN_QUAD_AREA_FRACTION+ }++ /**+ * Solves the exact 8x8 linear system (Gaussian elimination with partial pivoting)+ * for the 3x3 projective homography matrix mapping target rectangle+ * `(0, 0), (dstWidth, 0), (dstWidth, dstHeight), (0, dstHeight)` to `src`.+ */+ fun computeHomography(src: QuadCorners, dstWidth: Float, dstHeight: Float): DoubleArray {+ val ordered = src.ordered()+ val u = doubleArrayOf(0.0, dstWidth.toDouble(), dstWidth.toDouble(), 0.0)+ val v = doubleArrayOf(0.0, 0.0, dstHeight.toDouble(), dstHeight.toDouble())+ val x = doubleArrayOf(+ ordered.topLeft.x.toDouble(),+ ordered.topRight.x.toDouble(),+ ordered.bottomRight.x.toDouble(),+ ordered.bottomLeft.x.toDouble(),+ )+ val y = doubleArrayOf(+ ordered.topLeft.y.toDouble(),+ ordered.topRight.y.toDouble(),+ ordered.bottomRight.y.toDouble(),+ ordered.bottomLeft.y.toDouble(),+ )++ val a = Array(8) { DoubleArray(9) }+ for (i in 0 until 4) {+ val r0 = i * 2+ val r1 = r0 + 1+ a[r0][0] = u[i]+ a[r0][1] = v[i]+ a[r0][2] = 1.0+ a[r0][6] = -u[i] * x[i]+ a[r0][7] = -v[i] * x[i]+ a[r0][8] = x[i]++ a[r1][3] = u[i]+ a[r1][4] = v[i]+ a[r1][5] = 1.0+ a[r1][6] = -u[i] * y[i]+ a[r1][7] = -v[i] * y[i]+ a[r1][8] = y[i]+ }++ // Gaussian elimination with partial pivoting+ for (col in 0 until 8) {+ var pivot = col+ var maxAbs = abs(a[col][col])+ for (row in col + 1 until 8) {+ val candidate = abs(a[row][col])+ if (candidate > maxAbs) {+ maxAbs = candidate+ pivot = row+ }+ }+ if (pivot != col) {+ val tmp = a[col]+ a[col] = a[pivot]+ a[pivot] = tmp+ }+ val diag = a[col][col]+ if (abs(diag) < 1e-12) continue+ for (j in col..8) {+ a[col][j] /= diag+ }+ for (row in 0 until 8) {+ if (row == col) continue+ val factor = a[row][col]+ if (abs(factor) < 1e-15) continue+ for (j in col..8) {+ a[row][j] -= factor * a[col][j]+ }+ }+ }++ return doubleArrayOf(+ a[0][8], a[1][8], a[2][8],+ a[3][8], a[4][8], a[5][8],+ a[6][8], a[7][8], 1.0,+ )+ }++ /**+ * Rectifies the quadrilateral `quadPixels` in `srcPixels` (`ARGB_8888`) into a+ * flat `dstWidth x dstHeight` image using bilinear interpolation and optional+ * whiteboard/document contrast enhancement.+ */+ fun warpPerspectiveArgb(+ srcPixels: IntArray,+ srcWidth: Int,+ srcHeight: Int,+ quadPixels: QuadCorners,+ dstWidth: Int,+ dstHeight: Int,+ enhanceDocument: Boolean = true,+ ): IntArray {+ val out = IntArray(dstWidth * dstHeight)+ if (srcWidth <= 0 || srcHeight <= 0 || dstWidth <= 0 || dstHeight <= 0) return out++ val h = computeHomography(+ src = quadPixels.ordered(),+ dstWidth = (dstWidth - 1).coerceAtLeast(1).toFloat(),+ dstHeight = (dstHeight - 1).coerceAtLeast(1).toFloat(),+ )++ val maxX = (srcWidth - 1).coerceAtLeast(0)+ val maxY = (srcHeight - 1).coerceAtLeast(0)++ for (dy in 0 until dstHeight) {+ val rowOff = dy * dstWidth+ val h1y = h[1] * dy + h[2]+ val h4y = h[4] * dy + h[5]+ val h7y = h[7] * dy + h[8]+ for (dx in 0 until dstWidth) {+ val denom = h[6] * dx + h7y+ val invDenom = if (abs(denom) > 1e-9) 1.0 / denom else 1.0+ val sx = ((h[0] * dx + h1y) * invDenom).toFloat().coerceIn(0f, maxX.toFloat())+ val sy = ((h[3] * dx + h4y) * invDenom).toFloat().coerceIn(0f, maxY.toFloat())++ val x0 = sx.toInt().coerceIn(0, maxX)+ val y0 = sy.toInt().coerceIn(0, maxY)+ val x1 = min(x0 + 1, maxX)+ val y1 = min(y0 + 1, maxY)+ val fx = sx - x0+ val fy = sy - y0+ val w00 = (1f - fx) * (1f - fy)+ val w10 = fx * (1f - fy)+ val w01 = (1f - fx) * fy+ val w11 = fx * fy++ val c00 = srcPixels[y0 * srcWidth + x0]+ val c10 = srcPixels[y0 * srcWidth + x1]+ val c01 = srcPixels[y1 * srcWidth + x0]+ val c11 = srcPixels[y1 * srcWidth + x1]++ val r = ((c00 ushr 16 and 0xFF) * w00 ++ (c10 ushr 16 and 0xFF) * w10 ++ (c01 ushr 16 and 0xFF) * w01 ++ (c11 ushr 16 and 0xFF) * w11).roundToInt().coerceIn(0, 255)+ val g = ((c00 ushr 8 and 0xFF) * w00 ++ (c10 ushr 8 and 0xFF) * w10 ++ (c01 ushr 8 and 0xFF) * w01 ++ (c11 ushr 8 and 0xFF) * w11).roundToInt().coerceIn(0, 255)+ val b = ((c00 and 0xFF) * w00 ++ (c10 and 0xFF) * w10 ++ (c01 and 0xFF) * w01 ++ (c11 and 0xFF) * w11).roundToInt().coerceIn(0, 255)++ out[rowOff + dx] = (0xFF shl 24) or (r shl 16) or (g shl 8) or b+ }+ }++ if (enhanceDocument) {+ enhanceDocumentInPlace(out)+ }+ return out+ }++ private fun enhanceDocumentInPlace(pixels: IntArray) {+ if (pixels.isEmpty()) return+ val hist = IntArray(256)+ for (c in pixels) {+ val r = c ushr 16 and 0xFF+ val g = c ushr 8 and 0xFF+ val b = c and 0xFF+ val y = (77 * r + 150 * g + 29 * b) ushr 8+ hist[y]+++ }++ val lowTarget = (pixels.size * 0.05f).toInt()+ val highTarget = (pixels.size * 0.85f).toInt()+ var acc = 0+ var low = 0+ var high = 255+ for (i in 0..255) {+ acc += hist[i]+ if (acc <= lowTarget) low = i+ if (acc <= highTarget) high = i+ }+ if (high <= low + 12) return++ val span = (high - low).toFloat()+ val lut = IntArray(256) { v ->+ val norm = ((v - low).toFloat() / span).coerceIn(0f, 1f)+ // S-curve that whitens paper highlights and deepens dark ink+ val curved = if (norm > 0.72f) {+ val t = (norm - 0.72f) / 0.28f+ 0.78f + 0.22f * (1f - (1f - t) * (1f - t))+ } else {+ norm * (0.78f / 0.72f)+ }+ (curved * 255f).roundToInt().coerceIn(0, 255)+ }++ for (i in pixels.indices) {+ val c = pixels[i]+ val r = lut[c ushr 16 and 0xFF]+ val g = lut[c ushr 8 and 0xFF]+ val b = lut[c and 0xFF]+ pixels[i] = (0xFF shl 24) or (r shl 16) or (g shl 8) or b+ }+ }++ /** Detects a document quad directly from an Android [Bitmap]. */+ fun detectDocumentQuad(bitmap: Bitmap): QuadCorners {+ val w = bitmap.width+ val h = bitmap.height+ if (w < 16 || h < 16) return DEFAULT_QUAD+ val pixels = IntArray(w * h)+ bitmap.getPixels(pixels, 0, w, 0, 0, w, h)+ val luma = ByteArray(w * h) { i ->+ val c = pixels[i]+ val r = c ushr 16 and 0xFF+ val g = c ushr 8 and 0xFF+ val b = c and 0xFF+ ((77 * r + 150 * g + 29 * b) ushr 8).toByte()+ }+ return detectDocumentQuad(luma, w, h)+ }++ /**+ * Rectifies `normalizedQuad` (`0f..1f`) from `bitmap` into a perspective-corrected+ * rectangular [Bitmap].+ */+ fun rectifyDocument(+ bitmap: Bitmap,+ normalizedQuad: QuadCorners,+ enhance: Boolean = true,+ ): Bitmap {+ val w = bitmap.width+ val h = bitmap.height+ val quadPx = normalizedQuad.ordered().toPixelSpace(w, h)++ val topW = hypot(quadPx.topRight.x - quadPx.topLeft.x, quadPx.topRight.y - quadPx.topLeft.y)+ val bottomW = hypot(quadPx.bottomRight.x - quadPx.bottomLeft.x, quadPx.bottomRight.y - quadPx.bottomLeft.y)+ val leftH = hypot(quadPx.bottomLeft.x - quadPx.topLeft.x, quadPx.bottomLeft.y - quadPx.topLeft.y)+ val rightH = hypot(quadPx.bottomRight.x - quadPx.topRight.x, quadPx.bottomRight.y - quadPx.topRight.y)++ var dstW = max(topW, bottomW).roundToInt().coerceAtLeast(64)+ var dstH = max(leftH, rightH).roundToInt().coerceAtLeast(64)+ val longest = max(dstW, dstH)+ if (longest > 2200) {+ val scale = 2200f / longest.toFloat()+ dstW = (dstW * scale).roundToInt().coerceAtLeast(64)+ dstH = (dstH * scale).roundToInt().coerceAtLeast(64)+ }++ val srcPixels = IntArray(w * h)+ bitmap.getPixels(srcPixels, 0, w, 0, 0, w, h)+ val dstPixels = warpPerspectiveArgb(+ srcPixels = srcPixels,+ srcWidth = w,+ srcHeight = h,+ quadPixels = quadPx,+ dstWidth = dstW,+ dstHeight = dstH,+ enhanceDocument = enhance,+ )+ return Bitmap.createBitmap(dstPixels, dstW, dstH, Bitmap.Config.ARGB_8888)+ }++ private fun otsuThreshold(values: IntArray): Int {+ val hist = IntArray(256)+ for (v in values) hist[v.coerceIn(0, 255)]+++ val total = values.size+ var sumAll = 0.0+ for (i in 0..255) sumAll += i.toDouble() * hist[i]++ var sumB = 0.0+ var wB = 0+ var maxVar = -1.0+ var threshold = 128++ for (t in 0..255) {+ wB += hist[t]+ if (wB == 0) continue+ val wF = total - wB+ if (wF == 0) break+ sumB += t.toDouble() * hist[t]+ val mB = sumB / wB+ val mF = (sumAll - sumB) / wF+ val diff = mB - mF+ val between = wB.toDouble() * wF.toDouble() * diff * diff+ if (between > maxVar) {+ maxVar = between+ threshold = t+ }+ }+ return threshold+ }+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.ktindex d7a4f44..bf006cd 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt@@ -2,6 +2,7 @@package app.nectarlink.android.coreimport android.content.Context+import android.content.pm.PackageManagerimport android.net.wifi.WifiManagerimport android.os.Buildimport android.service.notification.NotificationListenerService@@ -114,6 +115,7 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener}},appWindows = AppWindows(this.context, scope, open = { pc -> mirrorOpen(pc) }, nameOf = { pc -> _state.value.nameOf(pc).orEmpty() }),+ nameOf = { pc -> _state.value.nameOf(pc).orEmpty() },)private val storage = PhoneStorage(this.context) { path ->notificationOps.trySend { it.storageChanged(path) }@@ -204,6 +206,12 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener?.createMulticastLock("nectarlink-pairing")?.apply { setReferenceCounted(false) }+ // Holds a multicast lock while LocalSend LAN interop (UDP 224.0.0.167:53317) is enabled.+ private val localsendMulticast: WifiManager.MulticastLock? =+ this.context.getSystemService(WifiManager::class.java)+ ?.createMulticastLock("nectarlink-localsend")+ ?.apply { setReferenceCounted(false) }+val state: StateFlow<CoreState> = _state.asStateFlow()/** Short messages for the user (a command failed). */@@ -252,6 +260,14 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener}val retention = runCatching { started.timelineRetention() }.getOrNull()val tlPage = runCatching { started.timelinePage(null, null, null, 0u, 100u) }.getOrNull()+ val lsEnabled = runCatching { started.localsendEnabled() }.getOrDefault(false)+ val lsPeers = if (lsEnabled) {+ runCatching { started.localsendPeers() }.getOrDefault(emptyList())+ } else {+ emptyList()+ }+ localsendMulticast?.runCatching { if (lsEnabled) acquire() else release() }+ pruneOldTempCaches()_state.update {it.withDevices(devices, storageAllowed).copy(status = CoreStatus.Ready(started.deviceId()),@@ -261,8 +277,11 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenertimelineTotal = tlPage?.total ?: 0u,timelineHasMore = tlPage?.hasMore ?: false,timelineRetentionDays = retention?.maxDays ?: 90u,+ localsendEnabled = lsEnabled,+ localsendPeers = lsPeers,)}+ refreshDataRetention()if (devices.isNotEmpty()) ConnectionService.start(context)scope.launch(Dispatchers.Main) {battery.start()@@ -402,6 +421,7 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenerit.updatePower(powerLevel(), capabilities(_state.value.notificationAccess))if (photoAccess && (!prevPhotos || photoPartialAccess)) it.photosChanged()}+ scope.launch(Dispatchers.IO) { refreshDataRetention() }}}@@ -436,6 +456,7 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenerrefreshTimeline()}is Event.TimelineChanged -> refreshTimeline()+ is Event.LocalSendChanged -> syncLocalSendState()else -> {}}}@@ -464,7 +485,10 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenerrecordings.onTransferEvent(transfer, pcOnline)TransferNotifications.update(context, transfer, pc)val done = transfer.status as? TransferStatus.Done ?: return- if (transfer.direction != TransferDirection.INCOMING) return+ if (transfer.direction != TransferDirection.INCOMING) {+ scope.launch(Dispatchers.IO) { refreshDataRetention() }+ return+ }// Out of the app's cache, into Downloads, then tell the user.scope.launch(Dispatchers.IO) {val published = done.saved.flatMap { ReceivedFiles.publish(context, java.io.File(it)) }@@ -484,6 +508,8 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenertransfer.total,)refreshTimeline()+ } else {+ refreshDataRetention()}TransferNotifications.received(context, transfer, published, pc)}@@ -613,6 +639,45 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener}}+ /** Sends a captured photo or scanned document back to the requesting PC (`camera.result`). */+ fun sendCameraCaptureResult(+ pcId: String,+ requestId: String,+ mode: String,+ fileName: String,+ mime: String,+ width: Int,+ height: Int,+ data: ByteArray,+ ) {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ val currentNode = node ?: return@launch+ try {+ currentNode.sendCameraCaptureResult(+ pcId = pcId,+ requestId = requestId,+ mode = mode,+ fileName = fileName,+ mime = mime,+ width = width.coerceAtLeast(1).toUInt(),+ height = height.coerceAtLeast(1).toUInt(),+ data = data,+ )+ } catch (e: NectarlinkException) {+ _messages.tryEmit(describe(e))+ }+ }+ }++ /** Notifies the requesting PC that a Continuity Camera capture was cancelled (`camera.cancel`). */+ fun cancelCameraCapture(pcId: String, requestId: String, reason: String? = null) {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ runCatching { node?.cancelCameraCapture(pcId, requestId, reason) }+ }+ }+/*** Sends picked or shared files to a PC; problems arrive as messages.* The files are opened right away: Android's permission to read a@@ -655,10 +720,51 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener}}+ fun acceptTransfer(id: String) {+ node?.acceptTransfer(id)+ }+fun cancelTransfer(id: String) {node?.cancelTransfer(id)}+ // ---- LocalSend interop ----++ fun setLocalSendEnabled(enabled: Boolean) {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ val currentNode = node ?: return@launch+ try {+ currentNode.setLocalsendEnabled(enabled)+ } catch (e: NectarlinkException) {+ _messages.tryEmit(describe(e))+ }+ localsendMulticast?.runCatching { if (enabled) acquire() else release() }+ syncLocalSendState()+ }+ }++ fun refreshLocalSend() {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ val currentNode = node ?: return@launch+ runCatching { currentNode.refreshLocalsend() }+ syncLocalSendState()+ }+ }++ private fun syncLocalSendState() {+ val currentNode = node ?: return+ val enabled = runCatching { currentNode.localsendEnabled() }.getOrDefault(false)+ val peers = if (enabled) {+ runCatching { currentNode.localsendPeers() }.getOrDefault(emptyList())+ } else {+ emptyList()+ }+ localsendMulticast?.runCatching { if (enabled) acquire() else release() }+ _state.update { it.copy(localsendEnabled = enabled, localsendPeers = peers) }+ }+// ---- Clipboard ----/**@@ -715,6 +821,7 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListeneremptyList()}_state.update { it.copy(clipboardHistoryEnabled = enabled, clipboardHistory = items) }+ refreshDataRetention()}fun setClipboardHistoryEnabled(enabled: Boolean) {@@ -785,6 +892,7 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenertimelineRetentionDays = retention?.maxDays ?: it.timelineRetentionDays,)}+ refreshDataRetention()}suspend fun queryTimelinePage(@@ -822,6 +930,91 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener}}+ // ---- Data retention & Clear everything ----++ private fun pruneOldTempCaches() {+ val cutoff = System.currentTimeMillis() - 24L * 60 * 60 * 1000+ listOf("continuity_camera", "clipboard").forEach { dirName ->+ java.io.File(context.cacheDir, dirName).listFiles()?.forEach { file ->+ if (file.isFile && file.lastModified() in 1 until cutoff) {+ runCatching { file.delete() }+ }+ }+ }+ }++ private fun refreshDataRetention() {+ val counts = runCatching { node?.dataRetentionCounts() }.getOrNull()+ val photoFiles = listOf("continuity_camera", "clipboard").sumOf { dirName ->+ java.io.File(context.cacheDir, dirName).listFiles()?.count { it.isFile } ?: 0+ }+ val mmsFiles = java.io.File(context.cacheDir, "mms")+ .listFiles()+ ?.count { it.isFile && it.name.endsWith(".mms", ignoreCase = true) } ?: 0+ _state.update {+ it.copy(+ receivedFileRecords = counts?.receivedFileRecords?.toInt() ?: it.receivedFileRecords,+ cachedPhotoFiles = photoFiles,+ cachedMmsFiles = mmsFiles,+ )+ }+ }++ fun clearMessageCache() {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ java.io.File(context.cacheDir, "mms").listFiles()?.forEach { file ->+ if (file.isFile && file.name.endsWith(".mms", ignoreCase = true)) {+ runCatching { file.delete() }+ }+ }+ runCatching { node?.clearMessageCache() }+ refreshDataRetention()+ }+ }++ fun clearPhotoThumbnailsCache() {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ listOf("continuity_camera", "clipboard").forEach { dirName ->+ java.io.File(context.cacheDir, dirName).listFiles()?.forEach { file ->+ runCatching { file.deleteRecursively() }+ }+ }+ refreshDataRetention()+ }+ }++ fun clearReceivedFileHistory() {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ java.io.File(context.cacheDir, "received").listFiles()?.forEach { file ->+ runCatching { file.deleteRecursively() }+ }+ _state.update { s -> s.copy(transfers = s.transfers.filterNot { it.isFinished() }) }+ runCatching { node?.clearReceivedFileHistory() }+ refreshTimeline()+ refreshDataRetention()+ }+ }++ fun clearEverything() {+ scope.launch(Dispatchers.IO) {+ startJob?.join()+ runCatching { node?.clearAllLocalData() }+ listOf("mms", "continuity_camera", "clipboard", "received").forEach { dirName ->+ java.io.File(context.cacheDir, dirName).listFiles()?.forEach { file ->+ runCatching { file.deleteRecursively() }+ }+ }+ _state.update { s -> s.copy(transfers = s.transfers.filterNot { it.isFinished() }) }+ refreshClipboardHistory()+ refreshTimeline()+ refreshDataRetention()+ _messages.tryEmit(context.getString(R.string.data_cleared_everything_toast))+ }+ }+fun resendTimelineEntry(entry: TimelineEntry) {scope.launch(Dispatchers.IO) {startJob?.join()@@ -895,7 +1088,8 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener(if (PhoneSms.canRead(context)) listOf("sms.read") else emptyList()) +(if (PhoneSms.canSend(context)) listOf("sms.send") else emptyList()) +PhoneStorage.capabilities(context) +- (if (WebcamService.hasPermission(context)) listOf("webcam.h264") else emptyList()) ++ (if (WebcamService.hasPermission(context)) listOf("camera.stream") else emptyList()) ++ (if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) listOf("camera.capture") else emptyList()) +(if (InputService.running || Elevated.running) listOf("mirror.input") else emptyList()) +// Apps in windows of their own run on displays the Elevated helper makes.(if (Elevated.running && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) listOf("mirror.virtual_display") else emptyList()) +diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.ktindex 8e55d19..17ebfbf 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/CoreState.kt@@ -7,6 +7,7 @@ import app.nectarlink.core.DiscoveredDeviceimport app.nectarlink.core.Eventimport app.nectarlink.core.Featureimport app.nectarlink.core.Link+import app.nectarlink.core.LocalSendPeerimport app.nectarlink.core.PairedDeviceimport app.nectarlink.core.PairingFailureimport app.nectarlink.core.PowerLevel@@ -125,11 +126,23 @@ data class CoreState(val timelineHasMore: Boolean = false,/** Timeline auto-purge retention in days (`0` = keep up to the entry cap regardless of age). */val timelineRetentionDays: UInt = 90u,+ /** Number of received-file transfer records kept in local history. */+ val receivedFileRecords: Int = 0,+ /** Number of temporary Continuity Camera / clipboard image files in the phone's cache. */+ val cachedPhotoFiles: Int = 0,+ /** Number of temporary MMS staging files in the phone's cache. */+ val cachedMmsFiles: Int = 0,+ /** Whether LocalSend LAN interop is enabled (off by default). */+ val localsendEnabled: Boolean = false,+ /** Nearby LocalSend devices discovered on the LAN. */+ val localsendPeers: List<LocalSendPeer> = emptyList(),) {fun device(id: String): Device? = devices.firstOrNull { it.id == id }fun nameOf(id: String): String? =- device(id)?.name ?: discovered.firstOrNull { it.id == id }?.name+ device(id)?.name+ ?: discovered.firstOrNull { it.id == id }?.name+ ?: localsendPeers.firstOrNull { it.id == id }?.aliasfun withDevices(paired: List<PairedDevice>, storageAllowed: Set<String> = emptySet()): CoreState {val prevById = devices.associateBy { it.id }@@ -201,8 +214,12 @@ data class CoreState(if (pairing == PairingState.Idle) this else copy(pairing = PairingState.Failed(event.failure))// PCs don't send notifications; nothing for the phone to show.is Event.NotificationsReset, is Event.NotificationPosted, is Event.NotificationRemoved -> this- // Android shows its own "copied" confirmation; Core refreshes clipboardHistory/timeline on change.- is Event.ClipboardReceived, is Event.ClipboardHistoryChanged, is Event.TimelineChanged -> this+ // Android shows its own "copied" confirmation; Core refreshes clipboardHistory/timeline/LocalSend on change.+ is Event.ClipboardReceived,+ is Event.ClipboardHistoryChanged,+ is Event.TimelineChanged,+ is Event.LocalSendChanged,+ -> thisis Event.Transfer -> copy(transfers = withTransfer(event.transfer))// Shown in Android's media controls (see media/PcMedia).is Event.MediaChanged -> this@@ -240,7 +257,9 @@ data class CoreState(const val MAX_FINISHED_TRANSFERS = 5fun Transfer.isFinished(): Boolean =- status !is app.nectarlink.core.TransferStatus.Running && status !is app.nectarlink.core.TransferStatus.Waiting+ status !is app.nectarlink.core.TransferStatus.Requested &&+ status !is app.nectarlink.core.TransferStatus.Running &&+ status !is app.nectarlink.core.TransferStatus.Waitinginternal fun PairedDevice.toDevice() = Device(id = id,diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.ktindex e9f161b..3cc8379 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt@@ -9,6 +9,7 @@ import android.content.Intentimport android.os.Buildimport android.os.Bundleimport app.nectarlink.android.calls.PhoneCalls+import app.nectarlink.android.camera.ContinuityCameraRequestsimport app.nectarlink.android.clipboard.PhoneClipboardimport app.nectarlink.android.contacts.PhoneContactsimport app.nectarlink.android.notifications.NotificationListener@@ -29,6 +30,7 @@ import app.nectarlink.android.webcam.WebcamServiceimport app.nectarlink.core.CallCommandimport app.nectarlink.core.CallLogEntryimport app.nectarlink.core.Contact+import app.nectarlink.core.SmsAttachmentimport app.nectarlink.core.SmsMessageimport app.nectarlink.core.SmsPartDataimport app.nectarlink.core.SmsThread@@ -77,6 +79,8 @@ internal class PhonePlatform(private val onWebcam: (String, WebcamRequest) -> Boolean,/** Apps in windows of their own on PCs (Elevated). */private val appWindows: AppWindows,+ /** Resolves a paired PC's display name by ID. */+ private val nameOf: (String) -> String = { "" },) : Platform {private val context = context.applicationContext@@ -162,6 +166,13 @@ internal class PhonePlatform(override fun webcamKeyframeRequested(pcId: String) =WebcamService.keyframe(pcId)+ override fun cameraCaptureRequested(pcId: String, requestId: String, mode: String): Boolean =+ ContinuityCameraRequests.show(context, pcId, requestId, mode, nameOf(pcId))++ override fun cameraCaptureCancelled(pcId: String, requestId: String) {+ ContinuityCameraRequests.cancelFromPc(context, pcId, requestId)+ }+override fun phoneApps(): List<PhoneApp> = AppWindows.list(context)override fun smsThreads(limit: UInt): List<SmsThread> = sms().threads(limit.toInt())@@ -171,6 +182,9 @@ internal class PhonePlatform(override fun smsSend(to: List<String>, body: String): Boolean = sms().send(to, body)+ override fun sendSmsWithAttachments(to: List<String>, body: String, attachments: List<SmsAttachment>): Boolean =+ sms().send(to, body, attachments)+override fun smsPart(id: String): SmsPartData? = sms().part(id)override fun setPhoneToggle(id: String, value: PhoneToggleValue): Boolean =diff --git a/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.kt b/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.ktindex 9abe96c..e6f7cd6 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/notifications/NotificationReader.kt@@ -10,6 +10,7 @@ import android.graphics.BitmapFactoryimport android.graphics.drawable.Iconimport android.net.Uriimport android.os.Build+import android.provider.Telephonyimport android.service.notification.NotificationListenerService.Rankingimport android.service.notification.NotificationListenerService.RankingMapimport android.service.notification.StatusBarNotification@@ -18,10 +19,13 @@ import androidx.core.app.NotificationCompatimport androidx.core.graphics.drawable.toBitmapimport androidx.core.graphics.scaleimport java.io.ByteArrayOutputStream+import app.nectarlink.android.sms.PhoneSmsimport app.nectarlink.core.LivePointimport app.nectarlink.core.LiveSegmentimport app.nectarlink.core.Notification as Mirroredimport app.nectarlink.core.NotificationAction as MirroredAction+import app.nectarlink.core.NotificationChatMessage+import app.nectarlink.core.NotificationConversationimport app.nectarlink.core.NotificationLive/**@@ -44,6 +48,7 @@ internal class NotificationReader(private val context: Context) {val silent = isSilent(sbn, ranking) ||(update && (n.flags and Notification.FLAG_ONLY_ALERT_ONCE != 0 || live != null))val whenMs = if (live?.chronometer == true && n.`when` > 0L) n.`when` else sbn.postTime+ val conversation = extractConversation(sbn, n, content.title)return Mirrored(key = sbn.key,app = sbn.packageName,@@ -57,6 +62,7 @@ internal class NotificationReader(private val context: Context) {icon = icon(sbn.packageName),image = image(sbn.key, n),live = live,+ conversation = conversation,)}@@ -273,9 +279,108 @@ internal class NotificationReader(private val context: Context) {}}.getOrNull()?.takeIf { it.size <= MAX_ICON_BYTES }?.also { icons.put(pkg, it) }+ /**+ * Extracts `Notification.MessagingStyle` conversation metadata so chat apps+ * (RCS, WhatsApp, Telegram, Signal, etc.) can be shown in the unified+ * Messages inbox alongside SMS. Omits `conversation` when the notification+ * comes from the default SMS app and the message is already in the system+ * SMS provider.+ */+ private fun extractConversation(+ sbn: StatusBarNotification,+ n: Notification,+ fallbackTitle: String?,+ ): NotificationConversation? = runCatching {+ val style = NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(n) ?: return null+ val isGroup = style.isGroupConversation+ val userName = style.user.name?.toString()?.trim()+ val userKey = style.user.key+ val personAvatars = HashMap<String, ByteArray?>()++ val rawMessages = style.messages.mapNotNull { m ->+ val text = m.text?.toString()?.trim().orEmpty()+ if (text.isEmpty()) return@mapNotNull null+ val person = m.person+ val selfSent = person == null ||+ (userKey != null && person.key == userKey) ||+ (!userName.isNullOrEmpty() && person.name?.toString()?.trim() == userName)+ val sender = if (selfSent) {+ null+ } else {+ person.name?.toString()?.trim()?.takeIf { it.isNotEmpty() }?.take(MAX_SENDER_CHARS)+ }+ val time = if (m.timestamp > 0L) m.timestamp else sbn.postTime+ val avatar = if (selfSent || person.icon == null) {+ null+ } else {+ val cacheKey = person.key ?: person.name?.toString().orEmpty()+ personAvatars.getOrPut(cacheKey) {+ runCatching { avatarFromIcon(person.icon?.toIcon(context)) }.getOrNull()+ }+ }+ NotificationChatMessage(+ sender = sender,+ text = text,+ time = time,+ selfSent = selfSent,+ avatar = avatar,+ )+ }.takeLast(MAX_CONVERSATION_MESSAGES)++ if (rawMessages.isEmpty()) return null++ val defaultSmsPkg = runCatching { Telephony.Sms.getDefaultSmsPackage(context) }.getOrNull()+ if (defaultSmsPkg != null && sbn.packageName == defaultSmsPkg && PhoneSms.canRead(context)) {+ val latest = rawMessages.last()+ if (PhoneSms.isRecentSmsInProvider(context, latest.text, latest.time)) {+ return null+ }+ }++ val convAvatar = avatarFromIcon(n.getLargeIcon())+ ?: rawMessages.lastOrNull { !it.selfSent && it.avatar != null }?.avatar++ buildConversation(+ styleTitle = style.conversationTitle?.toString(),+ fallbackTitle = fallbackTitle,+ isGroup = isGroup,+ convAvatar = convAvatar,+ messages = rawMessages,+ )+ }.getOrNull()++ /** Scales and compresses a notification or person `Icon` to a `<= 16 KiB` JPEG. */+ private fun avatarFromIcon(icon: Icon?): ByteArray? = runCatching {+ if (icon == null) return null+ val drawable = icon.loadDrawable(context) ?: return null+ val w = drawable.intrinsicWidth.takeIf { it > 0 } ?: AVATAR_PX+ val h = drawable.intrinsicHeight.takeIf { it > 0 } ?: AVATAR_PX+ val scale = minOf(1f, AVATAR_PX.toFloat() / maxOf(w, h))+ val targetW = (w * scale).toInt().coerceAtLeast(1)+ val targetH = (h * scale).toInt().coerceAtLeast(1)+ val bitmap = drawable.toBitmap(targetW, targetH, Bitmap.Config.ARGB_8888)+ try {+ for (quality in intArrayOf(80, 65, 50)) {+ val bytes = ByteArrayOutputStream().use { out ->+ bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)+ out.toByteArray()+ }+ if (bytes.isNotEmpty() && bytes.size <= MAX_AVATAR_BYTES) return bytes+ }+ null+ } finally {+ bitmap.recycle()+ }+ }.getOrNull()+internal companion object {const val ICON_PX = 96const val MAX_ICON_BYTES = 64 * 1024+ const val AVATAR_PX = 96+ const val MAX_AVATAR_BYTES = 16 * 1024+ const val MAX_CONVERSATION_MESSAGES = 25+ const val MAX_SENDER_CHARS = 128+ const val MAX_TITLE_CHARS = 256/** Pictures' longest side, and the fallback for large ones. */const val IMAGE_PX = 512const val SMALL_IMAGE_PX = 360@@ -284,6 +389,45 @@ internal class NotificationReader(private val context: Context) {const val MAX_SEGMENTS = 16const val MAX_POINTS = 16+ internal fun buildConversation(+ styleTitle: String?,+ fallbackTitle: String?,+ isGroup: Boolean,+ convAvatar: ByteArray?,+ messages: List<NotificationChatMessage>,+ ): NotificationConversation? {+ val cleanMessages = messages+ .filter { it.text.isNotBlank() }+ .takeLast(MAX_CONVERSATION_MESSAGES)+ .map { msg ->+ val cleanSender = if (msg.selfSent) {+ null+ } else {+ msg.sender?.trim()?.takeIf { it.isNotEmpty() }?.take(MAX_SENDER_CHARS)+ }+ val cleanAvatar = msg.avatar?.takeIf { it.isNotEmpty() && it.size <= MAX_AVATAR_BYTES }+ msg.copy(+ sender = cleanSender,+ text = msg.text.trim(),+ avatar = cleanAvatar,+ )+ }+ if (cleanMessages.isEmpty()) return null+ val trimmedStyleTitle = styleTitle?.trim()?.takeIf { it.isNotEmpty() }+ val latestNonSelfSender = cleanMessages.lastOrNull { !it.selfSent && !it.sender.isNullOrEmpty() }?.sender+ val trimmedFallback = fallbackTitle?.trim()?.takeIf { it.isNotEmpty() }+ val title = (if (isGroup) trimmedStyleTitle ?: latestNonSelfSender else latestNonSelfSender ?: trimmedStyleTitle)+ ?: trimmedFallback+ ?: return null+ return NotificationConversation(+ v = 1u,+ title = title.take(MAX_TITLE_CHARS),+ group = isGroup,+ avatar = convAvatar?.takeIf { it.isNotEmpty() && it.size <= MAX_AVATAR_BYTES },+ messages = cleanMessages,+ )+ }+internal fun buildLive(progress: Int,progressMax: Int,diff --git a/android/app/src/main/kotlin/app/nectarlink/android/recorder/RecordingsStore.kt b/android/app/src/main/kotlin/app/nectarlink/android/recorder/RecordingsStore.ktindex 4009fce..1824485 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/recorder/RecordingsStore.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/recorder/RecordingsStore.kt@@ -145,7 +145,7 @@ class RecordingsStore(context: Context) {val updated = current.map { rec ->if (rec.transferId != transfer.id) return@map recval nextDelivery = when (val status = transfer.status) {- is TransferStatus.Waiting -> DeliveryState.Waiting+ is TransferStatus.Waiting, is TransferStatus.Requested -> DeliveryState.Waitingis TransferStatus.Running -> DeliveryState.Sendingis TransferStatus.Done -> DeliveryState.Sentis TransferStatus.Cancelled -> DeliveryState.Idlediff --git a/android/app/src/main/kotlin/app/nectarlink/android/sms/PhoneSms.kt b/android/app/src/main/kotlin/app/nectarlink/android/sms/PhoneSms.ktindex 620bd42..d6f51fc 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/sms/PhoneSms.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/sms/PhoneSms.kt@@ -17,11 +17,14 @@ import android.provider.Telephonyimport android.telephony.SmsManagerimport android.util.Logimport androidx.core.content.ContextCompat+import androidx.core.content.FileProvider+import app.nectarlink.core.SmsAttachmentimport app.nectarlink.core.SmsMessageimport app.nectarlink.core.SmsPartimport app.nectarlink.core.SmsPartDataimport app.nectarlink.core.SmsThreadimport java.io.ByteArrayOutputStream+import java.io.File/*** The phone's text messages, for PCs (docs/protocol/sms.md): lists@@ -246,21 +249,9 @@ internal class PhoneSms(context: Context, private val onChange: () -> Unit) {// ---- Sending ----- /** Sends a text to each recipient; Android keeps it in Sent. */- fun send(to: List<String>, body: String): Boolean = guarded(false) {- if (!granted(context, Manifest.permission.SEND_SMS)) return@guarded false- val manager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {- context.getSystemService(SmsManager::class.java)- } else {- @Suppress("DEPRECATION")- SmsManager.getDefault()- }- val parts = manager.divideMessage(body)- for (address in to) {- manager.sendMultipartTextMessage(address.trim(), null, parts, null, null)- }- true- }+ /** Sends a text or MMS to each recipient; Android keeps it in Sent. */+ fun send(to: List<String>, body: String, attachments: List<SmsAttachment> = emptyList()): Boolean =+ send(context, to, body, attachments)private inline fun <T> guarded(fallback: T, block: () -> T): T = try {block()@@ -310,6 +301,9 @@ internal class PhoneSms(context: Context, private val onChange: () -> Unit) {private const val SETTLE_MS = 1000Lprivate const val MAX_PART_BYTES = 900 * 1024private const val MAX_PHOTO_BYTES = 16 * 1024+ private const val MMS_CACHE_DIR = "mms"+ private const val MMS_CACHE_TTL_MS = 5 * 60 * 1000L+ private const val RECENT_SMS_WINDOW_MS = 60_000L/** What a conversation whose latest message is a picture says. */private const val PICTURE_SNIPPET = "Picture"// MMS address types (PduHeaders.FROM and TO).@@ -330,5 +324,168 @@ internal class PhoneSms(context: Context, private val onChange: () -> Unit) {fun canSend(context: Context) = granted(context, Manifest.permission.SEND_SMS)fun hasAll(context: Context) = permissions.all { granted(context, it) }++ /**+ * Checks whether `Telephony.Sms` already contains a message with `body`+ * within 60 seconds of `aroundTimeMs` (so notifications from the default+ * SMS app aren't duplicated as `NotificationConversation`s when the SMS+ * provider already supplies them).+ */+ fun isRecentSmsInProvider(context: Context, body: String, aroundTimeMs: Long): Boolean {+ if (!canRead(context)) return false+ val trimmed = body.trim()+ if (trimmed.isEmpty()) return false+ val now = System.currentTimeMillis()+ val minDate = minOf(aroundTimeMs, now) - RECENT_SMS_WINDOW_MS+ val maxDate = maxOf(aroundTimeMs, now) + RECENT_SMS_WINDOW_MS+ return runCatching {+ context.contentResolver.query(+ Telephony.Sms.CONTENT_URI,+ arrayOf(Telephony.Sms.BODY),+ "${Telephony.Sms.DATE} >= ? AND ${Telephony.Sms.DATE} <= ?",+ arrayOf(minDate.toString(), maxDate.toString()),+ "${Telephony.Sms.DATE} DESC",+ )?.use { c ->+ while (c.moveToNext()) {+ if (c.getString(0)?.trim() == trimmed) return@use true+ }+ false+ } ?: false+ }.getOrDefault(false)+ }++ /** Sends an SMS or MMS (when `attachments` is non-empty) to `to`. */+ fun send(+ context: Context,+ to: List<String>,+ body: String,+ attachments: List<SmsAttachment> = emptyList(),+ ): Boolean = try {+ if (!granted(context, Manifest.permission.SEND_SMS)) {+ false+ } else {+ val recipients = to.map { it.trim() }.filter { it.isNotEmpty() }+ if (recipients.isEmpty()) {+ false+ } else {+ val manager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {+ context.getSystemService(SmsManager::class.java)+ } else {+ @Suppress("DEPRECATION")+ SmsManager.getDefault()+ }+ if (attachments.isEmpty()) {+ if (body.isBlank()) {+ false+ } else {+ val parts = manager.divideMessage(body)+ for (address in recipients) {+ manager.sendMultipartTextMessage(address, null, parts, null, null)+ }+ true+ }+ } else {+ val now = System.currentTimeMillis()+ val mmsDir = File(context.cacheDir, MMS_CACHE_DIR).apply { mkdirs() }+ mmsDir.listFiles()?.forEach { f ->+ if (f.name.endsWith(".mms") && now - f.lastModified() > MMS_CACHE_TTL_MS) {+ f.delete()+ }+ }+ val pduBytes = buildMmsSendReqPdu(recipients, body, attachments, "nl-$now")+ val pduFile = File(mmsDir, "send-$now.mms")+ pduFile.writeBytes(pduBytes)+ val contentUri = FileProvider.getUriForFile(context, "${context.packageName}.files", pduFile)+ manager.sendMultimediaMessage(context, contentUri, null, null, null)+ true+ }+ }+ }+ } catch (e: Exception) {+ Log.w(TAG, "can't send message", e)+ false+ }++ /**+ * Builds an OMA MMS `M-Send.req` binary PDU (`application/vnd.wap.mms-message`)+ * with `application/vnd.wap.multipart.mixed` parts for optional UTF-8 text and+ * image attachments.+ */+ internal fun buildMmsSendReqPdu(+ to: List<String>,+ body: String,+ attachments: List<SmsAttachment>,+ transactionId: String = "nl-${System.currentTimeMillis()}",+ ): ByteArray {+ val out = ByteArrayOutputStream()+ // X-Mms-Message-Type (0x8C): m-send-req (0x80)+ out.write(0x8C)+ out.write(0x80)+ // X-Mms-Transaction-Id (0x98): text-string + NUL+ out.write(0x98)+ out.write(transactionId.toByteArray(Charsets.US_ASCII))+ out.write(0x00)+ // X-Mms-MMS-Version (0x8D): v1.2 (0x92)+ out.write(0x8D)+ out.write(0x92)+ // From (0x89): value-length 0x01, Insert-address-token (0x81)+ out.write(0x89)+ out.write(0x01)+ out.write(0x81)+ // To (0x97): one per recipient+ for (raw in to) {+ val addr = raw.trim()+ if (addr.isEmpty()) continue+ val formatted = if ('@' in addr || addr.endsWith("/TYPE=PLMN")) addr else "$addr/TYPE=PLMN"+ out.write(0x97)+ out.write(formatted.toByteArray(Charsets.US_ASCII))+ out.write(0x00)+ }+ // Content-Type (0x84): application/vnd.wap.multipart.mixed (0xA3)+ out.write(0x84)+ out.write(0xA3)++ // Multipart body: uintvar count of parts+ val hasText = body.isNotBlank()+ val partCount = (if (hasText) 1 else 0) + attachments.size+ writeUintvar(out, partCount)++ if (hasText) {+ // Content-Type well-known text/plain (0x83) + UTF-8 data bytes+ val textHeader = byteArrayOf(0x83.toByte())+ val textBytes = body.toByteArray(Charsets.UTF_8)+ writeUintvar(out, textHeader.size)+ writeUintvar(out, textBytes.size)+ out.write(textHeader)+ out.write(textBytes)+ }++ for (att in attachments) {+ val mime = att.mime.trim().ifEmpty { "image/jpeg" }+ val mimeHeader = mime.toByteArray(Charsets.US_ASCII) + byteArrayOf(0x00)+ writeUintvar(out, mimeHeader.size)+ writeUintvar(out, att.data.size)+ out.write(mimeHeader)+ out.write(att.data)+ }++ return out.toByteArray()+ }++ /** Encodes a non-negative integer as a WAP WSP `uintvar` (7 bits per byte, MSB continuation). */+ internal fun writeUintvar(out: ByteArrayOutputStream, value: Int) {+ require(value >= 0) { "uintvar must be non-negative" }+ val chunks = IntArray(5)+ var count = 0+ var v = value+ do {+ chunks[count++] = v and 0x7F+ v = v ushr 7+ } while (v > 0)+ for (i in count - 1 downTo 1) {+ out.write(chunks[i] or 0x80)+ }+ out.write(chunks[0])+ }}}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.ktindex fb3d27a..9d72a68 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/MainActivity.kt@@ -473,6 +473,8 @@ private fun App(onSendFolder = core::sendFolder,onAccessChanged = core::refreshNotificationAccess,onCancelTransfer = core::cancelTransfer,+ onAcceptTransfer = core::acceptTransfer,+ onRefreshLocalSend = core::refreshLocalSend,onAllowStorage = { pcId -> core.setStorageEnabled(pcId, true) },onDismissStorageRequest = core::dismissStorageRequest,onAcceptWebcamRequest = { req ->@@ -516,6 +518,11 @@ private fun App(onSetSuggestClipboardActions = preferences::updateSuggestClipboardActions,onSetTimelineRetentionDays = core::setTimelineRetentionDays,onClearTimeline = core::clearTimeline,+ onClearMessageCache = core::clearMessageCache,+ onClearPhotoCache = core::clearPhotoThumbnailsCache,+ onClearReceivedFileHistory = core::clearReceivedFileHistory,+ onClearEverything = core::clearEverything,+ onToggleLocalSend = core::setLocalSendEnabled,modifier = modifier,)}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.ktindex 9f59c84..2236e2d 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/home/HomeScreen.kt@@ -91,6 +91,7 @@ import app.nectarlink.core.ClipboardHistoryEntryimport app.nectarlink.core.ClipboardItemKindimport app.nectarlink.core.DeckStateimport app.nectarlink.core.Link+import app.nectarlink.core.LocalSendPeerimport app.nectarlink.core.TimelineEntryimport app.nectarlink.core.TimelineKindimport app.nectarlink.core.TimelinePage@@ -128,6 +129,8 @@ fun HomeScreen(onSendFolder: (pcId: String, tree: Uri) -> Unit,onAccessChanged: () -> Unit,onCancelTransfer: (id: String) -> Unit,+ onAcceptTransfer: (id: String) -> Unit = {},+ onRefreshLocalSend: () -> Unit = {},onAllowStorage: (pcId: String) -> Unit = {},onDismissStorageRequest: () -> Unit = {},onAcceptWebcamRequest: (app.nectarlink.android.webcam.WebcamRequest) -> Unit = {},@@ -333,6 +336,15 @@ fun HomeScreen(onSetPcAudio = onSetPcAudio,)}+ if (state.localsendEnabled) {+ item {+ LocalSendCard(+ peers = state.localsendPeers,+ onRefresh = onRefreshLocalSend,+ onSendFiles = onSendFiles,+ )+ }+ }if (state.clipboardHistoryEnabled && state.clipboardHistory.isNotEmpty()) {item {ClipboardHistoryCard(@@ -351,7 +363,7 @@ fun HomeScreen(}}if (state.transfers.isNotEmpty()) {- item { TransfersCard(state, onCancelTransfer) }+ item { TransfersCard(state, onAcceptTransfer, onCancelTransfer) }}}@@ -1103,51 +1115,174 @@ private fun previewBitmap(bytes: ByteArray): android.graphics.Bitmap? {private const val PREVIEW_PX = 720+@Composable+private fun LocalSendCard(+ peers: List<LocalSendPeer>,+ onRefresh: () -> Unit,+ onSendFiles: (pcId: String, uris: List<Uri>) -> Unit,+) {+ Surface(+ shape = MaterialTheme.shapes.extraLarge,+ color = MaterialTheme.colorScheme.surfaceContainer,+ modifier = Modifier.fillMaxWidth(),+ ) {+ Column(+ modifier = Modifier.fillMaxWidth().padding(20.dp),+ verticalArrangement = Arrangement.spacedBy(12.dp),+ ) {+ Row(+ modifier = Modifier.fillMaxWidth(),+ verticalAlignment = Alignment.CenterVertically,+ ) {+ Text(+ stringResource(R.string.localsend_card_title),+ style = MaterialTheme.typography.titleMedium,+ modifier = Modifier.weight(1f),+ )+ IconButton(+ onClick = onRefresh,+ modifier = Modifier.size(32.dp),+ ) {+ Icon(+ painterResource(R.drawable.ic_refresh),+ contentDescription = stringResource(R.string.action_refresh),+ modifier = Modifier.size(20.dp),+ )+ }+ }+ if (peers.isEmpty()) {+ Text(+ stringResource(R.string.localsend_card_empty),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ } else {+ peers.forEach { peer ->+ LocalSendPeerRow(peer = peer, onSendFiles = onSendFiles)+ }+ }+ }+ }+}++@Composable+private fun LocalSendPeerRow(+ peer: LocalSendPeer,+ onSendFiles: (pcId: String, uris: List<Uri>) -> Unit,+) {+ val pickFiles = rememberLauncherForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) { uris ->+ if (uris.isNotEmpty()) onSendFiles(peer.id, uris)+ }+ Surface(+ shape = MaterialTheme.shapes.large,+ color = MaterialTheme.colorScheme.surfaceContainerHigh,+ modifier = Modifier.fillMaxWidth(),+ ) {+ Row(+ modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 12.dp),+ verticalAlignment = Alignment.CenterVertically,+ horizontalArrangement = Arrangement.SpaceBetween,+ ) {+ Column(modifier = Modifier.weight(1f).padding(end = 12.dp)) {+ Text(+ peer.alias,+ style = MaterialTheme.typography.titleSmall,+ maxLines = 1,+ overflow = TextOverflow.Ellipsis,+ )+ val subtitle = peer.deviceModel?.takeIf { it.isNotBlank() } ?: peer.ip+ Text(+ subtitle,+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ maxLines = 1,+ overflow = TextOverflow.Ellipsis,+ )+ }+ FilledTonalButton(onClick = { pickFiles.launch(arrayOf("*/*")) }) {+ Text(stringResource(R.string.action_send_files))+ }+ }+ }+}+/** Running transfers and the latest finished ones. */@Composable-private fun TransfersCard(state: CoreState, onCancel: (String) -> Unit) {+private fun TransfersCard(+ state: CoreState,+ onAccept: (String) -> Unit,+ onCancel: (String) -> Unit,+) {Surface(shape = MaterialTheme.shapes.extraLarge, color = MaterialTheme.colorScheme.surfaceContainer) {Column(Modifier.fillMaxWidth().padding(20.dp), verticalArrangement = Arrangement.spacedBy(14.dp)) {Text(stringResource(R.string.transfers_title), style = MaterialTheme.typography.titleMedium)- state.transfers.forEach { transfer -> TransferRow(transfer, state.nameOf(transfer.deviceId).orEmpty(), onCancel) }+ state.transfers.forEach { transfer ->+ TransferRow(+ transfer = transfer,+ pc = state.nameOf(transfer.deviceId).orEmpty(),+ onAccept = onAccept,+ onCancel = onCancel,+ )+ }}}}@Composable-private fun TransferRow(transfer: Transfer, pc: String, onCancel: (String) -> Unit) {+private fun TransferRow(+ transfer: Transfer,+ pc: String,+ onAccept: (String) -> Unit,+ onCancel: (String) -> Unit,+) {val incoming = transfer.direction == TransferDirection.INCOMINGval status = transfer.statusval title = transferTitle(LocalContext.current.resources, transfer)+ val peerName = pc.ifEmpty { stringResource(R.string.localsend_nearby_device) }val detail = when (status) {- is TransferStatus.Waiting -> stringResource(R.string.transfer_waiting, pc)+ is TransferStatus.Requested ->+ stringResource(R.string.transfer_requested_from, peerName) ++ " · " + Formatter.formatShortFileSize(LocalContext.current, transfer.total.toLong())+ is TransferStatus.Waiting -> stringResource(R.string.transfer_waiting, peerName)is TransferStatus.Running ->- stringResource(if (incoming) R.string.transfer_receiving else R.string.transfer_sending, pc) ++ stringResource(if (incoming) R.string.transfer_receiving else R.string.transfer_sending, peerName) +" · " + Formatter.formatShortFileSize(LocalContext.current, transfer.done.toLong()) +" / " + Formatter.formatShortFileSize(LocalContext.current, transfer.total.toLong())is TransferStatus.Done ->- if (incoming) stringResource(R.string.transfer_received_from, pc) else stringResource(R.string.transfer_sent_to, pc)+ if (incoming) stringResource(R.string.transfer_received_from, peerName) else stringResource(R.string.transfer_sent_to, peerName)is TransferStatus.Cancelled -> stringResource(R.string.transfer_cancelled)is TransferStatus.Failed -> when (status.reason) {- "denied" -> stringResource(R.string.transfer_denied, pc)- "unreachable" -> stringResource(R.string.transfer_unreachable, pc)+ "denied" -> stringResource(R.string.transfer_denied, peerName)+ "unreachable" -> stringResource(R.string.transfer_unreachable, peerName)"noSpace" -> stringResource(R.string.transfer_no_space)else -> stringResource(R.string.transfer_failed)}}- Row(verticalAlignment = Alignment.CenterVertically) {- Column(Modifier.weight(1f)) {- Text(title, style = MaterialTheme.typography.titleSmall, maxLines = 1, overflow = TextOverflow.Ellipsis)- if (!transfer.isFinished()) {- Spacer(Modifier.height(6.dp))- val progress = if (transfer.total == 0uL) 1f else (transfer.done.toDouble() / transfer.total.toDouble()).toFloat()- LinearProgressIndicator(progress = { progress }, modifier = Modifier.fillMaxWidth())+ Column(modifier = Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) {+ Row(verticalAlignment = Alignment.CenterVertically) {+ Column(Modifier.weight(1f)) {+ Text(title, style = MaterialTheme.typography.titleSmall, maxLines = 1, overflow = TextOverflow.Ellipsis)+ if (status is TransferStatus.Running || status is TransferStatus.Waiting) {+ Spacer(Modifier.height(6.dp))+ val progress = if (transfer.total == 0uL) 1f else (transfer.done.toDouble() / transfer.total.toDouble()).toFloat()+ LinearProgressIndicator(progress = { progress }, modifier = Modifier.fillMaxWidth())+ }+ Spacer(Modifier.height(4.dp))+ Text(detail, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)+ }+ if (status is TransferStatus.Running || status is TransferStatus.Waiting) {+ TextButton(onClick = { onCancel(transfer.id) }) { Text(stringResource(R.string.action_cancel)) }}- Spacer(Modifier.height(4.dp))- Text(detail, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)}- if (!transfer.isFinished()) {- TextButton(onClick = { onCancel(transfer.id) }) { Text(stringResource(R.string.action_cancel)) }+ if (status is TransferStatus.Requested) {+ Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {+ Button(onClick = { onAccept(transfer.id) }) {+ Text(stringResource(R.string.action_accept))+ }+ OutlinedButton(onClick = { onCancel(transfer.id) }) {+ Text(stringResource(R.string.action_decline))+ }+ }}}}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.ktindex 62fd14b..bbf7e81 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt@@ -42,6 +42,7 @@ import androidx.compose.runtime.setValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.pluralStringResourceimport androidx.compose.ui.res.stringResourceimport androidx.compose.ui.semantics.contentDescriptionimport androidx.compose.ui.semantics.semantics@@ -87,9 +88,15 @@ fun SettingsScreen(onSetSuggestClipboardActions: (Boolean) -> Unit = {},onSetTimelineRetentionDays: (UInt) -> Unit = {},onClearTimeline: () -> Unit = {},+ onClearMessageCache: () -> Unit = {},+ onClearPhotoCache: () -> Unit = {},+ onClearReceivedFileHistory: () -> Unit = {},+ onClearEverything: () -> Unit = {},+ onToggleLocalSend: (Boolean) -> Unit = {},modifier: Modifier = Modifier,) {var confirmUnpair by remember { mutableStateOf<Device?>(null) }+ var confirmClearEverything by remember { mutableStateOf(false) }Column(modifier.verticalScroll(rememberScrollState()).padding(16.dp),@@ -120,32 +127,75 @@ fun SettingsScreen(onCheckedChange = onSetSuggestClipboardActions,)}+ }++ Section(stringResource(R.string.settings_localsend_title)) {+ Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {+ Column(Modifier.weight(1f).padding(end = 12.dp)) {+ Text(stringResource(R.string.settings_localsend_title), style = MaterialTheme.typography.titleSmall)+ Text(+ stringResource(R.string.settings_localsend_subtitle),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }+ Switch(+ checked = state.localsendEnabled,+ onCheckedChange = onToggleLocalSend,+ )+ }+ }++ Section(stringResource(R.string.settings_data_storage_title)) {+ // 1. Notification history+ Column(Modifier.fillMaxWidth()) {+ Text(stringResource(R.string.data_notifications_title), style = MaterialTheme.typography.titleSmall)+ Text(+ stringResource(R.string.data_notifications_subtitle),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }SettingRowDivider()++ // 2. Clipboard historyRow(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {Column(Modifier.weight(1f).padding(end = 12.dp)) {- Text(stringResource(R.string.settings_clipboard_history), style = MaterialTheme.typography.titleSmall)+ Text(stringResource(R.string.data_clipboard_title), style = MaterialTheme.typography.titleSmall)Text(- stringResource(R.string.settings_clipboard_history_hint),+ stringResource(R.string.data_clipboard_subtitle),style = MaterialTheme.typography.bodySmall,color = MaterialTheme.colorScheme.onSurfaceVariant,)+ if (state.clipboardHistory.isNotEmpty()) {+ Text(+ pluralStringResource(+ R.plurals.data_clipboard_count,+ state.clipboardHistory.size,+ state.clipboardHistory.size,+ ),+ style = MaterialTheme.typography.labelMedium,+ color = MaterialTheme.colorScheme.primary,+ modifier = Modifier.padding(top = 2.dp),+ )+ }}Switch(checked = state.clipboardHistoryEnabled,onCheckedChange = onSetClipboardHistoryEnabled,)}- if (state.clipboardHistoryEnabled && state.clipboardHistory.isNotEmpty()) {+ if (state.clipboardHistory.isNotEmpty()) {OutlinedButton(onClick = onClearClipboardHistory) {- Text(stringResource(R.string.clipboard_history_clear))+ Text(stringResource(R.string.data_action_clear))}}- }+ SettingRowDivider()- Section(stringResource(R.string.settings_timeline_title)) {- Text(stringResource(R.string.settings_timeline_retention), style = MaterialTheme.typography.titleSmall)+ // 3. Timeline+ Text(stringResource(R.string.data_timeline_title), style = MaterialTheme.typography.titleSmall)Text(- stringResource(R.string.settings_timeline_retention_hint),+ stringResource(R.string.data_timeline_subtitle),style = MaterialTheme.typography.bodySmall,color = MaterialTheme.colorScheme.onSurfaceVariant,)@@ -158,11 +208,76 @@ fun SettingsScreen(),state.timelineRetentionDays.toString(),) { value -> value.toUIntOrNull()?.let(onSetTimelineRetentionDays) }- if (state.timeline.isNotEmpty()) {+ if (state.timeline.isNotEmpty() || state.timelineTotal > 0u) {OutlinedButton(onClick = onClearTimeline) {- Text(stringResource(R.string.timeline_action_clear_all))+ Text(stringResource(R.string.data_action_clear))+ }+ }+ SettingRowDivider()++ // 4. Message cache+ Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {+ Column(Modifier.weight(1f).padding(end = 12.dp)) {+ Text(stringResource(R.string.data_messages_title), style = MaterialTheme.typography.titleSmall)+ Text(+ stringResource(R.string.data_messages_subtitle),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }+ if (state.cachedMmsFiles > 0) {+ OutlinedButton(onClick = onClearMessageCache) {+ Text(stringResource(R.string.data_action_clear))+ }+ }+ }+ SettingRowDivider()++ // 5. Photo thumbnails & camera cache+ Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {+ Column(Modifier.weight(1f).padding(end = 12.dp)) {+ Text(stringResource(R.string.data_photos_title), style = MaterialTheme.typography.titleSmall)+ Text(+ stringResource(R.string.data_photos_subtitle),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }+ if (state.cachedPhotoFiles > 0) {+ OutlinedButton(onClick = onClearPhotoCache) {+ Text(stringResource(R.string.data_action_clear))+ }}}+ SettingRowDivider()++ // 6. Received-file history+ Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {+ Column(Modifier.weight(1f).padding(end = 12.dp)) {+ Text(stringResource(R.string.data_received_files_title), style = MaterialTheme.typography.titleSmall)+ Text(+ stringResource(R.string.data_received_files_subtitle),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ }+ if (state.receivedFileRecords > 0 || state.transfers.isNotEmpty()) {+ OutlinedButton(onClick = onClearReceivedFileHistory) {+ Text(stringResource(R.string.data_action_clear))+ }+ }+ }+ SettingRowDivider()++ // 7. Clear everything+ Text(+ stringResource(R.string.data_clear_everything_hint),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ )+ OutlinedButton(onClick = { confirmClearEverything = true }) {+ Text(stringResource(R.string.data_clear_everything_button))+ }}Section(stringResource(R.string.settings_calls_and_contacts)) {@@ -370,6 +485,27 @@ fun SettingsScreen(},)}++ if (confirmClearEverything) {+ AlertDialog(+ onDismissRequest = { confirmClearEverything = false },+ title = { Text(stringResource(R.string.data_clear_everything_dialog_title)) },+ text = { Text(stringResource(R.string.data_clear_everything_dialog_body)) },+ confirmButton = {+ TextButton(onClick = {+ onClearEverything()+ confirmClearEverything = false+ }) {+ Text(stringResource(R.string.data_clear_everything_confirm))+ }+ },+ dismissButton = {+ TextButton(onClick = { confirmClearEverything = false }) {+ Text(stringResource(R.string.action_cancel))+ }+ },+ )+ }}/**diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xmlindex 25d4389..749be7e 100644--- a/android/app/src/main/res/values/strings.xml+++ b/android/app/src/main/res/values/strings.xml@@ -428,5 +428,41 @@<string name="timeline_retention_90d">90d</string><string name="timeline_retention_1y">1y</string><string name="timeline_retention_all">All</string>++ <!-- Data & storage -->+ <string name="settings_data_storage_title">Data & storage</string>+ <string name="data_action_clear">Clear</string>+ <string name="data_notifications_title">Notification history</string>+ <string name="data_notifications_subtitle">Mirrored live to paired PCs · not stored on this phone (PCs keep dismissed notifications for up to 24 hours / 300 items)</string>+ <string name="data_clipboard_title">Clipboard history</string>+ <string name="data_clipboard_subtitle">Encrypted on device (passwords and OTPs excluded) · kept for 7 days, up to 50 clips / 128 MB</string>+ <plurals name="data_clipboard_count">+ <item quantity="one">%d clip stored</item>+ <item quantity="other">%d clips stored</item>+ </plurals>+ <string name="data_timeline_title">Timeline</string>+ <string name="data_timeline_subtitle">Files, clips, links, photos, recordings and sessions · up to 5,000 items</string>+ <string name="data_messages_title">Message cache</string>+ <string name="data_messages_subtitle">SMS and chat notifications are read live from Android; temporary MMS staging files are kept for up to 5 minutes (PCs cache chat threads for 90 days)</string>+ <string name="data_photos_title">Photo thumbnails & camera cache</string>+ <string name="data_photos_subtitle">Photos are read directly from MediaStore on demand; temporary Continuity Camera captures are kept for up to 24 hours</string>+ <string name="data_received_files_title">Received-file history</string>+ <string name="data_received_files_subtitle">Transfer log kept with Timeline (saved files in Downloads are never deleted)</string>+ <string name="data_clear_everything_hint">Deletes all clipboard history, Timeline, transfer records and temporary caches on this phone. Paired PCs and settings are kept.</string>+ <string name="data_clear_everything_button">Clear everything…</string>+ <string name="data_clear_everything_dialog_title">Clear all local data?</string>+ <string name="data_clear_everything_dialog_body">This deletes clipboard history, Timeline activity, received-file records, and temporary caches on this phone. Your paired PCs and settings will not be touched.</string>+ <string name="data_clear_everything_confirm">Clear everything</string>+ <string name="data_cleared_everything_toast">Cleared all local data on this phone.</string>++ <!-- LocalSend -->+ <string name="settings_localsend_title">LocalSend</string>+ <string name="settings_localsend_subtitle">Send files to and receive files from nearby LocalSend devices on your Wi-Fi network. Off by default; incoming files always ask before saving.</string>+ <string name="localsend_card_title">LocalSend nearby</string>+ <string name="localsend_card_empty">No LocalSend devices found on this Wi-Fi network yet.</string>+ <string name="localsend_nearby_device">Nearby device</string>+ <string name="transfer_requested_from">%1$s wants to send files</string>+ <string name="action_accept">Accept</string>+ <string name="action_decline">Decline</string></resources>diff --git a/android/app/src/main/res/xml/file_paths.xml b/android/app/src/main/res/xml/file_paths.xmlindex 905e132..e4ec9a8 100644--- a/android/app/src/main/res/xml/file_paths.xml+++ b/android/app/src/main/res/xml/file_paths.xml@@ -1,8 +1,10 @@<?xml version="1.0" encoding="utf-8"?><!-- SPDX-License-Identifier: GPL-3.0-or-later -->-<!-- Received files on Android 8–9 (see files/ReceivedFiles.kt), and images- a PC put on the clipboard (see clipboard/PhoneClipboard.kt). -->+<!-- Received files on Android 8–9 (see files/ReceivedFiles.kt), images+ a PC put on the clipboard (see clipboard/PhoneClipboard.kt), and+ outgoing MMS PDUs (see sms/PhoneSms.kt). --><paths><external-files-path name="received" path="Download/Nectarlink/" /><cache-path name="clipboard" path="clipboard/" />+ <cache-path name="mms" path="mms/" /></paths>diff --git a/android/app/src/test/kotlin/app/nectarlink/android/camera/DocumentScannerProcessorTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/camera/DocumentScannerProcessorTest.ktnew file mode 100644index 0000000..3d85917--- /dev/null+++ b/android/app/src/test/kotlin/app/nectarlink/android/camera/DocumentScannerProcessorTest.kt@@ -0,0 +1,154 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.camera++import kotlin.math.PI+import kotlin.math.abs+import kotlin.math.cos+import kotlin.math.hypot+import kotlin.math.sin+import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test++class DocumentScannerProcessorTest {++ @Test+ fun `QuadCorners ordered sorts shuffled corners clockwise`() {+ val tl = PointF2D(0.12f, 0.15f)+ val tr = PointF2D(0.88f, 0.18f)+ val br = PointF2D(0.84f, 0.89f)+ val bl = PointF2D(0.14f, 0.85f)++ val shuffled = QuadCorners(+ topLeft = br,+ topRight = bl,+ bottomRight = tl,+ bottomLeft = tr,+ )+ val ordered = shuffled.ordered()+ assertEquals(tl, ordered.topLeft)+ assertEquals(tr, ordered.topRight)+ assertEquals(br, ordered.bottomRight)+ assertEquals(bl, ordered.bottomLeft)+ }++ @Test+ fun `detectDocumentQuad locates tilted bright rectangle on dark background`() {+ val width = 160+ val height = 160+ val angle = 12.0 * PI / 180.0+ val cosA = cos(angle).toFloat()+ val sinA = sin(angle).toFloat()+ val halfW = 0.30f+ val halfH = 0.24f++ fun rot(dx: Float, dy: Float): PointF2D = PointF2D(+ x = 0.5f + dx * cosA - dy * sinA,+ y = 0.5f + dx * sinA + dy * cosA,+ )++ val expectedTl = rot(-halfW, -halfH)+ val expectedTr = rot(halfW, -halfH)+ val expectedBr = rot(halfW, halfH)+ val expectedBl = rot(-halfW, halfH)++ val luma = ByteArray(width * height)+ for (y in 0 until height) {+ val ny = y.toFloat() / (height - 1).toFloat() - 0.5f+ for (x in 0 until width) {+ val nx = x.toFloat() / (width - 1).toFloat() - 0.5f+ val ux = nx * cosA + ny * sinA+ val uy = -nx * sinA + ny * cosA+ val inside = abs(ux) <= halfW && abs(uy) <= halfH+ luma[y * width + x] = (if (inside) 230 else 25).toByte()+ }+ }++ val detected = DocumentScannerProcessor.detectDocumentQuad(luma, width, height)+ assertTrue(DocumentScannerProcessor.isValidDocumentQuad(detected))+ assertTrue(dist(expectedTl, detected.topLeft) < 0.06f)+ assertTrue(dist(expectedTr, detected.topRight) < 0.06f)+ assertTrue(dist(expectedBr, detected.bottomRight) < 0.06f)+ assertTrue(dist(expectedBl, detected.bottomLeft) < 0.06f)+ }++ @Test+ fun `computeHomography and warpPerspectiveArgb rectify skewed quad and enhance contrast`() {+ val srcW = 100+ val srcH = 100+ val quad = QuadCorners(+ topLeft = PointF2D(20f, 15f),+ topRight = PointF2D(80f, 20f),+ bottomRight = PointF2D(90f, 85f),+ bottomLeft = PointF2D(10f, 80f),+ )++ val h = DocumentScannerProcessor.computeHomography(quad, 59f, 59f)+ // Verify (0, 0) maps to topLeft (20, 15) and (59, 59) maps to bottomRight (90, 85)+ assertEquals(20.0, h[2] / h[8], 1e-4)+ assertEquals(15.0, h[5] / h[8], 1e-4)+ val denomBr = h[6] * 59.0 + h[7] * 59.0 + h[8]+ assertEquals(90.0, (h[0] * 59.0 + h[1] * 59.0 + h[2]) / denomBr, 1e-4)+ assertEquals(85.0, (h[3] * 59.0 + h[4] * 59.0 + h[5]) / denomBr, 1e-4)++ val srcPixels = IntArray(srcW * srcH)+ val poly = quad.toList()+ for (y in 0 until srcH) {+ for (x in 0 until srcW) {+ val inQuad = pointInConvexQuad(x.toFloat(), y.toFloat(), poly)+ val inInk = x in 42..58 && y in 42..58+ val gray = when {+ !inQuad -> 15+ inInk -> 60+ else -> 205+ }+ srcPixels[y * srcW + x] = (0xFF shl 24) or (gray shl 16) or (gray shl 8) or gray+ }+ }++ val dstW = 60+ val dstH = 60+ val unenhanced = DocumentScannerProcessor.warpPerspectiveArgb(+ srcPixels = srcPixels,+ srcWidth = srcW,+ srcHeight = srcH,+ quadPixels = quad,+ dstWidth = dstW,+ dstHeight = dstH,+ enhanceDocument = false,+ )+ val enhanced = DocumentScannerProcessor.warpPerspectiveArgb(+ srcPixels = srcPixels,+ srcWidth = srcW,+ srcHeight = srcH,+ quadPixels = quad,+ dstWidth = dstW,+ dstHeight = dstH,+ enhanceDocument = true,+ )++ // Check that warped corners (near the edge of the destination rectangle) contain bright paper, not the dark 15 background+ val cornerPaperUnenhanced = unenhanced[2 * dstW + 2] and 0xFF+ assertTrue("Expected bright paper at warped corner, got $cornerPaperUnenhanced", cornerPaperUnenhanced >= 190)++ val paperUnenhanced = unenhanced[10 * dstW + 10] and 0xFF+ val inkUnenhanced = unenhanced[30 * dstW + 30] and 0xFF+ val paperEnhanced = enhanced[10 * dstW + 10] and 0xFF+ val inkEnhanced = enhanced[30 * dstW + 30] and 0xFF++ assertTrue(paperEnhanced > paperUnenhanced)+ assertTrue((paperEnhanced - inkEnhanced) > (paperUnenhanced - inkUnenhanced))+ }++ private fun dist(a: PointF2D, b: PointF2D): Float = hypot(a.x - b.x, a.y - b.y)++ private fun pointInConvexQuad(px: Float, py: Float, pts: List<PointF2D>): Boolean {+ for (i in 0 until 4) {+ val a = pts[i]+ val b = pts[(i + 1) % 4]+ val cross = (b.x - a.x) * (py - a.y) - (b.y - a.y) * (px - a.x)+ if (cross < 0f) return false+ }+ return true+ }+}diff --git a/android/app/src/test/kotlin/app/nectarlink/android/sms/MmsPduTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/sms/MmsPduTest.ktnew file mode 100644index 0000000..7e095d2--- /dev/null+++ b/android/app/src/test/kotlin/app/nectarlink/android/sms/MmsPduTest.kt@@ -0,0 +1,182 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.sms++import app.nectarlink.android.notifications.NotificationReader+import app.nectarlink.core.NotificationChatMessage+import app.nectarlink.core.SmsAttachment+import java.io.ByteArrayOutputStream+import org.junit.Assert.assertArrayEquals+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertNotNull+import org.junit.Assert.assertNull+import org.junit.Assert.assertTrue+import org.junit.Test++class MmsPduTest {++ @Test+ fun `writeUintvar encodes single and multi-byte values per WAP-230-WSP`() {+ fun encode(v: Int): ByteArray = ByteArrayOutputStream().also {+ PhoneSms.writeUintvar(it, v)+ }.toByteArray()++ assertArrayEquals(byteArrayOf(0x00), encode(0))+ assertArrayEquals(byteArrayOf(0x02), encode(2))+ assertArrayEquals(byteArrayOf(0x7F), encode(127))+ assertArrayEquals(byteArrayOf(0x81.toByte(), 0x00), encode(128))+ assertArrayEquals(byteArrayOf(0x82.toByte(), 0x2C), encode(300))+ assertArrayEquals(byteArrayOf(0x81.toByte(), 0x80.toByte(), 0x00), encode(16384))+ }++ @Test+ fun `buildMmsSendReqPdu encodes headers, text part, and image attachment`() {+ val imageBytes = ByteArray(300) { (it and 0xFF).toByte() }+ val pdu = PhoneSms.buildMmsSendReqPdu(+ to = listOf(" +15550100 ", "[email protected]"),+ body = "Look at this!",+ attachments = listOf(SmsAttachment(mime = "image/jpeg", data = imageBytes)),+ transactionId = "nl-1700000000",+ )++ var pos = 0+ // X-Mms-Message-Type (0x8C): m-send-req (0x80)+ assertEquals(0x8C, pdu[pos++].toInt() and 0xFF)+ assertEquals(0x80, pdu[pos++].toInt() and 0xFF)++ // X-Mms-Transaction-Id (0x98): "nl-1700000000" + NUL+ assertEquals(0x98, pdu[pos++].toInt() and 0xFF)+ val txEnd = pdu.indexOf(0, pos)+ assertEquals("nl-1700000000", String(pdu, pos, txEnd - pos, Charsets.US_ASCII))+ pos = txEnd + 1++ // X-Mms-MMS-Version (0x8D): v1.2 (0x92)+ assertEquals(0x8D, pdu[pos++].toInt() and 0xFF)+ assertEquals(0x92, pdu[pos++].toInt() and 0xFF)++ // From (0x89): length 1, Insert-address-token (0x81)+ assertEquals(0x89, pdu[pos++].toInt() and 0xFF)+ assertEquals(0x01, pdu[pos++].toInt() and 0xFF)+ assertEquals(0x81, pdu[pos++].toInt() and 0xFF)++ // First To (0x97): "+15550100/TYPE=PLMN" + NUL+ assertEquals(0x97, pdu[pos++].toInt() and 0xFF)+ val to1End = pdu.indexOf(0, pos)+ assertEquals("+15550100/TYPE=PLMN", String(pdu, pos, to1End - pos, Charsets.US_ASCII))+ pos = to1End + 1++ // Second To (0x97): "[email protected]" + NUL+ assertEquals(0x97, pdu[pos++].toInt() and 0xFF)+ val to2End = pdu.indexOf(0, pos)+ assertEquals("[email protected]", String(pdu, pos, to2End - pos, Charsets.US_ASCII))+ pos = to2End + 1++ // Content-Type (0x84): application/vnd.wap.multipart.mixed (0xA3)+ assertEquals(0x84, pdu[pos++].toInt() and 0xFF)+ assertEquals(0xA3, pdu[pos++].toInt() and 0xFF)++ // Multipart count: 2 (text + image)+ assertEquals(2, pdu[pos++].toInt() and 0xFF)++ // Part 1: text/plain (0x83)+ val textExpected = "Look at this!".toByteArray(Charsets.UTF_8)+ assertEquals(1, pdu[pos++].toInt() and 0xFF) // headersLen+ assertEquals(textExpected.size, pdu[pos++].toInt() and 0xFF) // dataLen+ assertEquals(0x83, pdu[pos++].toInt() and 0xFF) // Content-Type: text/plain+ assertArrayEquals(textExpected, pdu.copyOfRange(pos, pos + textExpected.size))+ pos += textExpected.size++ // Part 2: image/jpeg + NUL (11 bytes) and 300-byte payload (uintvar 0x82, 0x2C)+ assertEquals(11, pdu[pos++].toInt() and 0xFF) // headersLen+ assertEquals(0x82, pdu[pos++].toInt() and 0xFF) // dataLen high byte (300 = 2*128 + 44)+ assertEquals(0x2C, pdu[pos++].toInt() and 0xFF) // dataLen low byte+ val mimeEnd = pdu.indexOf(0, pos)+ assertEquals("image/jpeg", String(pdu, pos, mimeEnd - pos, Charsets.US_ASCII))+ pos = mimeEnd + 1++ assertArrayEquals(imageBytes, pdu.copyOfRange(pos, pos + imageBytes.size))+ pos += imageBytes.size+ assertEquals(pdu.size, pos)+ }++ @Test+ fun `buildMmsSendReqPdu omits text part when body is blank`() {+ val pngBytes = byteArrayOf(0x89.toByte(), 0x50, 0x4E, 0x47)+ val pdu = PhoneSms.buildMmsSendReqPdu(+ to = listOf("5550199"),+ body = " ",+ attachments = listOf(SmsAttachment(mime = "image/png", data = pngBytes)),+ transactionId = "nl-42",+ )++ // Find Content-Type (0x84, 0xA3) at the end of the headers+ var idx = 0+ while (idx < pdu.size - 1 && !((pdu[idx].toInt() and 0xFF) == 0x84 && (pdu[idx + 1].toInt() and 0xFF) == 0xA3)) {+ idx+++ }+ assertTrue(idx < pdu.size - 2)+ var pos = idx + 2++ // Multipart count: 1 (image only)+ assertEquals(1, pdu[pos++].toInt() and 0xFF)+ assertEquals(10, pdu[pos++].toInt() and 0xFF) // "image/png\0" length+ assertEquals(pngBytes.size, pdu[pos++].toInt() and 0xFF)+ val mimeEnd = pdu.indexOf(0, pos)+ assertEquals("image/png", String(pdu, pos, mimeEnd - pos, Charsets.US_ASCII))+ pos = mimeEnd + 1+ assertArrayEquals(pngBytes, pdu.copyOfRange(pos, pdu.size))+ }++ @Test+ fun `buildConversation extracts 1-to-1 and group MessagingStyle metadata with bounds`() {+ val smallAvatar = ByteArray(1024) { 1 }+ val oversizedAvatar = ByteArray(20 * 1024) { 2 }++ val msgs = (1..30).map { i ->+ NotificationChatMessage(+ sender = if (i % 2 == 0) "Me" else "Alice",+ text = "Message $i",+ time = 1_700_000_000_000L + i * 1000L,+ selfSent = i % 2 == 0,+ avatar = if (i % 2 == 0) oversizedAvatar else smallAvatar,+ )+ }++ val conv = NotificationReader.buildConversation(+ styleTitle = null,+ fallbackTitle = "Fallback",+ isGroup = false,+ convAvatar = smallAvatar,+ messages = msgs,+ )+ assertNotNull(conv)+ assertEquals("Alice", conv!!.title)+ assertFalse(conv.group)+ assertEquals(25, conv.messages.size)+ assertEquals("Message 6", conv.messages.first().text)+ assertEquals("Message 30", conv.messages.last().text)+ // Self-sent messages drop sender and oversized avatars+ assertTrue(conv.messages.last().selfSent)+ assertNull(conv.messages.last().sender)+ assertNull(conv.messages.last().avatar)++ val groupConv = NotificationReader.buildConversation(+ styleTitle = "Family Chat",+ fallbackTitle = "Alice",+ isGroup = true,+ convAvatar = oversizedAvatar,+ messages = msgs.take(2),+ )+ assertNotNull(groupConv)+ assertEquals("Family Chat", groupConv!!.title)+ assertTrue(groupConv.group)+ assertNull(groupConv.avatar)+ }++ private fun ByteArray.indexOf(byte: Byte, startIndex: Int): Int {+ for (i in startIndex until size) {+ if (this[i] == byte) return i+ }+ return -1+ }+}diff --git a/core/nectarlink-cli/src/main.rs b/core/nectarlink-cli/src/main.rsindex 5636cac..a384bf8 100644--- a/core/nectarlink-cli/src/main.rs+++ b/core/nectarlink-cli/src/main.rs@@ -11,8 +11,9 @@ use clap::{Parser, Subcommand, ValueEnum};use nectarlink_core::{Battery, ConnectionPath, DeviceId, DeviceInfo, DeviceKind, Direction, FeatureState, LinkState, LivePoint,LiveSegment, MediaAction, MediaError, MediaPlayer, Node, NodeConfig, NodeEvent, Notification,- NotificationAction, NotificationError, NotificationLive, PairedDevice, PairingEvent, Platform,- PowerAction, PowerLevel, ScreenCorners, ScreenRect, ScreenShape, TaskNotify, TransferState,+ NotificationAction, NotificationChatMessage, NotificationConversation, NotificationError,+ NotificationLive, PairedDevice, PairingEvent, Platform, PowerAction, PowerLevel, ScreenCorners,+ ScreenRect, ScreenShape, SmsAttachment, TaskNotify, TransferState,features::{Effort, FEATURES, Role, UnsupportedReason, Upgrade, UpgradeAction},notify_limits,};@@ -67,6 +68,12 @@ struct Cli {/// generated sample folder tree in the data directory).#[arg(long, global = true)]storage_root: Option<PathBuf>,+ /// Enable LocalSend v2.1 compatibility (UDP multicast 224.0.0.167:53317 and HTTP/HTTPS server).+ #[arg(long, global = true)]+ localsend: bool,+ /// TCP/UDP port for LocalSend v2.1 (default: 53317).+ #[arg(long, global = true, default_value_t = nectarlink_core::LOCALSEND_DEFAULT_PORT)]+ localsend_port: u16,/// Log verbosity (error, warn, info, debug, trace).#[arg(long, global = true, default_value = "warn")]log: String,@@ -679,8 +686,15 @@ enum SmsArg {#[arg(long, default_value_t = 20)]limit: u32,},- /// Send a text.- Send { to: String, body: String },+ /// Send a text or MMS (with --image).+ Send {+ to: String,+ #[arg(default_value = "")]+ body: String,+ /// JPEG or PNG image attachment to send as MMS (repeatable, up to 3).+ #[arg(long = "image", value_name = "PATH")]+ images: Vec<PathBuf>,+ },}#[derive(Debug, Subcommand)]@@ -1067,6 +1081,104 @@ fn sample_texts(main_thread_count: usize) {*TEXTS.lock().unwrap() = rows;}+static CHAT_NOTES: std::sync::Mutex<Vec<Notification>> = std::sync::Mutex::new(Vec::new());++fn sample_chat_notifications() -> Vec<Notification> {+ let now = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map_or(0, |d| d.as_millis() as i64);+ let min = 60_000i64;+ let reply_actions = || {+ vec![+ NotificationAction { id: "reply".into(), title: "Reply".into(), reply: true },+ NotificationAction { id: "read".into(), title: "Mark as read".into(), reply: false },+ ]+ };+ let notes = vec![+ Notification {+ key: "chat|whatsapp|maya".into(),+ app: "com.whatsapp".into(),+ app_name: "WhatsApp".into(),+ title: Some("Maya Lin".into()),+ text: Some("Sent over the deck too".into()),+ sub: None,+ when: now - 12 * min,+ actions: reply_actions(),+ silent: true,+ icon: Some(sample_icon(2)),+ image: None,+ live: None,+ conversation: Some(NotificationConversation {+ v: 1,+ title: "Maya Lin".into(),+ group: false,+ avatar: None,+ messages: vec![+ NotificationChatMessage {+ sender: Some("Maya Lin".into()),+ text: "Hey! Are you free for a quick call after lunch?".into(),+ time: now - 14 * min,+ self_sent: false,+ avatar: None,+ },+ NotificationChatMessage {+ sender: Some("Maya Lin".into()),+ text: "Sent over the deck too".into(),+ time: now - 12 * min,+ self_sent: false,+ avatar: None,+ },+ ],+ }),+ },+ Notification {+ key: "chat|signal|weekend".into(),+ app: "org.thoughtcrime.securesms".into(),+ app_name: "Signal".into(),+ title: Some("Weekend Hike 🥾".into()),+ text: Some("Priya: Meet at the trailhead by 8:30?".into()),+ sub: None,+ when: now - 22 * min,+ actions: reply_actions(),+ silent: true,+ icon: Some(sample_icon(1)),+ image: None,+ live: None,+ conversation: Some(NotificationConversation {+ v: 1,+ title: "Weekend Hike 🥾".into(),+ group: true,+ avatar: None,+ messages: vec![+ NotificationChatMessage {+ sender: Some("Liam".into()),+ text: "Weather looks clear all Saturday morning!".into(),+ time: now - 28 * min,+ self_sent: false,+ avatar: None,+ },+ NotificationChatMessage {+ sender: None,+ text: "I'll bring trail mix and the thermos".into(),+ time: now - 25 * min,+ self_sent: true,+ avatar: None,+ },+ NotificationChatMessage {+ sender: Some("Priya".into()),+ text: "Meet at the trailhead by 8:30?".into(),+ time: now - 22 * min,+ self_sent: false,+ avatar: None,+ },+ ],+ }),+ },+ ];+ *CHAT_NOTES.lock().unwrap() = notes.clone();+ notes+}+static CONTACTS: std::sync::Mutex<Vec<nectarlink_core::Contact>> = std::sync::Mutex::new(Vec::new());static CALL_LOG: std::sync::Mutex<Vec<nectarlink_core::CallLogEntry>> = std::sync::Mutex::new(Vec::new());@@ -1790,6 +1902,34 @@ impl Platform for TerminalPlatform {}Ok(())}+ fn send_sms_with_attachments(+ &self,+ to: &[String],+ body: &str,+ attachments: &[SmsAttachment],+ ) -> std::result::Result<(), String> {+ if attachments.is_empty() {+ return self.sms_send(to, body);+ }+ println!("A PC sent a message to {} ({} attachment(s)): {body}", to.join(", "), attachments.len());+ let summary = if body.is_empty() {+ format!("[{} image attachment(s)]", attachments.len())+ } else {+ format!("{body} [{} image attachment(s)]", attachments.len())+ };+ let now = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map_or(0, |d| d.as_millis() as i64);+ let name = contact_name_for(&to[0]).unwrap_or_default();+ let mut texts = TEXTS.lock().unwrap();+ let thread = texts.iter().find(|(_, n, ..)| *n == to[0]).map(|(t, ..)| *t);+ let thread = thread.unwrap_or_else(|| texts.iter().map(|(t, ..)| *t).max().unwrap_or(0) + 1);+ texts.push((thread, to[0].clone(), name, false, summary, now));+ if let Some(node) = TEXTS_NODE.get().cloned() {+ tokio::runtime::Handle::current().spawn(async move { node.sms_changed(None).await });+ }+ Ok(())+ }fn call_command(&self,id: &str,@@ -2023,7 +2163,35 @@ impl Platform for TerminalPlatform {reply: Option<&str>,) -> Result<(), NotificationError> {match reply {- Some(text) => println!("The PC replied to {key}: {text}"),+ Some(text) => {+ println!("The PC replied to {key}: {text}");+ let now = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map_or(0, |d| d.as_millis() as i64);+ let mut updated = None;+ {+ let mut notes = CHAT_NOTES.lock().unwrap();+ if let Some(n) = notes.iter_mut().find(|n| n.key == key) {+ n.text = Some(text.to_owned());+ n.when = now;+ if let Some(conv) = n.conversation.as_mut() {+ conv.messages.push(NotificationChatMessage {+ sender: None,+ text: text.to_owned(),+ time: now,+ self_sent: true,+ avatar: None,+ });+ }+ updated = Some(n.clone());+ }+ }+ if let (Some(node), Some(note)) = (TEXTS_NODE.get().cloned(), updated) {+ tokio::runtime::Handle::current().spawn(async move {+ node.notification_posted(note).await;+ });+ }+ }None => println!("The PC ran action {action} of {key}"),}Ok(())@@ -2151,6 +2319,48 @@ impl Platform for TerminalPlatform {}Ok(())}+ fn camera_capture_requested(+ &self,+ peer: &DeviceId,+ request: &nectarlink_core::CameraCaptureRequest,+ ) -> Result<(), String> {+ let node = TOGGLES_NODE.get().cloned().ok_or("camera capture is only available with --as-phone")?;+ let peer_id = *peer;+ let req_id = request.request_id.clone();+ let mode = request.mode;+ println!("A PC asked for Continuity Camera ({}, request {}).", mode.as_str(), req_id);+ let delay_ms = std::env::var("NECTARLINK_CLI_CAMERA_DELAY_MS")+ .ok()+ .and_then(|s| s.parse::<u64>().ok())+ .unwrap_or(1200);+ tokio::runtime::Handle::current().spawn(async move {+ tokio::time::sleep(Duration::from_millis(delay_ms)).await;+ let (file_name, jpeg) = match mode {+ nectarlink_core::CameraCaptureMode::Photo => (+ "IMG_continuity.jpg".to_owned(),+ sample_jpeg(800, 600, (255, 140, 82), (180, 62, 92), (255, 232, 160)),+ ),+ nectarlink_core::CameraCaptureMode::Scan => (+ "Scan_continuity.jpg".to_owned(),+ sample_jpeg(800, 600, (246, 244, 238), (232, 228, 220), (52, 108, 176)),+ ),+ };+ let meta = nectarlink_core::CameraCaptureResultMeta {+ request_id: req_id,+ mode,+ mime: "image/jpeg".into(),+ file_name,+ size: jpeg.len() as u64,+ width: 800,+ height: 600,+ };+ let _ = node.send_camera_capture_result(peer_id, meta, jpeg).await;+ });+ Ok(())+ }+ fn camera_capture_cancelled(&self, _peer: &DeviceId, request_id: &str) {+ println!("Continuity Camera request {request_id} cancelled.");+ }}struct CliWebcamSink {@@ -2250,6 +2460,7 @@ async fn start_node(cli: &Cli) -> Result<Node> {let mut config = NodeConfig::new(data_dir.clone(), device, env!("CARGO_PKG_VERSION"));config.downloads_dir = cli.downloads_dir.clone();config.port = cli.port;+ config.localsend_port = cli.localsend_port;config.lan_discovery = !cli.no_lan;config.away_mode = cli.away;config.power = power;@@ -2262,6 +2473,7 @@ async fn start_node(cli: &Cli) -> Result<Node> {PHONE_STORAGE.set(nectarlink_core::FolderStorage::new(storage_root).with_trash_dir(trash_dir));config.capabilities.push(nectarlink_core::STORAGE_READ.into());config.capabilities.push(nectarlink_core::STORAGE_WRITE.into());+ config.capabilities.push(nectarlink_core::CAMERA_CAPTURE.into());} else {config.capabilities.push(nectarlink_core::RECORDER.into());config.capabilities.push(nectarlink_core::TOGGLES_SHOW.into());@@ -2274,6 +2486,9 @@ async fn start_node(cli: &Cli) -> Result<Node> {}let extra_caps = config.capabilities.clone();let node = Node::start(config, Arc::new(TerminalPlatform)).await.context("failed to start")?;+ if cli.localsend {+ node.set_localsend_enabled(true).context("failed to enable LocalSend")?;+ }if cli.as_phone {let _ = TOGGLES_NODE.set(node.clone());let _ = node.toggles_changed(sample_toggles()).await;@@ -2357,12 +2572,19 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {Command::Devices { wait } => {tokio::time::sleep(Duration::from_secs(*wait)).await;let devices = node.paired_devices()?;- if devices.is_empty() {+ let localsend = node.localsend_peers();+ if devices.is_empty() && localsend.is_empty() {println!("No paired devices. Run `nectarlink pair` to pair one.");}for d in devices {print_device(&d);}+ for p in localsend {+ println!(+ "{:<24} {:<10} LocalSend ({}://{}:{}) {}",+ p.alias, p.device_type, p.protocol, p.ip, p.port, p.id+ );+ }}Command::Ring { device, off } => {let id = resolve(node, device)?;@@ -2371,8 +2593,10 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {println!("{}", if *off { "Stopped ringing." } else { "Ringing…" });}Command::Send { device, files } => {- let id = resolve(node, device)?;- wait_until_online(node, id).await?;+ let (id, is_localsend) = resolve_send_target(node, device, cli.localsend_port).await?;+ if !is_localsend {+ wait_until_online(node, id).await?;+ }send_files(node, id, files).await?;}Command::Record { device, duration, file, markers } => {@@ -3077,6 +3301,8 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {let mut offers = cli.offers.clone();offers.extend([+ "notify.mirror",+ "notify.reply",nectarlink_core::SMS_READ,nectarlink_core::SMS_SEND,nectarlink_core::CALLS_STATE,@@ -3089,6 +3315,9 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {.map(str::to_owned),);node.update_power(node_power(cli), offers).await;+ for n in sample_chat_notifications() {+ node.notification_posted(n).await;+ }println!("Sharing sample conversations, contacts, and calls with paired PCs.");watch(node, false).await?;}@@ -3153,6 +3382,7 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {nectarlink_core::CALLS_DIAL,nectarlink_core::CONTACTS_READ,nectarlink_core::PHOTOS_READ,+ nectarlink_core::CAMERA_CAPTURE,nectarlink_core::TOGGLES_READ,nectarlink_core::TOGGLES_RINGER,nectarlink_core::TOGGLES_VOLUME,@@ -3207,9 +3437,13 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {icon: None,image: None,live: None,+ conversation: None,}).await;}+ for n in sample_chat_notifications() {+ node.notification_posted(n).await;+ }if *call {let call = nectarlink_core::CallState {id: now.to_string(),@@ -3271,8 +3505,26 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {);}}- SmsArg::Send { to, body } => {- node.sms_send(id, vec![to.clone()], body.clone()).await.context("not sent")?;+ SmsArg::Send { to, body, images } => {+ if images.is_empty() {+ node.sms_send(id, vec![to.clone()], body.clone()).await.context("not sent")?;+ } else {+ let mut attachments = Vec::with_capacity(images.len());+ for path in images {+ let data = std::fs::read(path)+ .with_context(|| format!("can't read {}", path.display()))?;+ let is_png = data.starts_with(b"\x89PNG\r\n\x1a\n")+ || path+ .extension()+ .and_then(|e| e.to_str())+ .is_some_and(|e| e.eq_ignore_ascii_case("png"));+ let mime = if is_png { "image/png" } else { "image/jpeg" }.to_owned();+ attachments.push(SmsAttachment { mime, data });+ }+ node.send_sms_with_attachments(id, vec![to.clone()], body.clone(), attachments)+ .await+ .context("not sent")?;+ }println!("Sent.");}}@@ -3575,6 +3827,7 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {icon: None,image,live: live.clone(),+ conversation: None,};node.notification_posted(base_note.clone()).await;for (i, extra) in also.iter().enumerate() {@@ -3595,6 +3848,7 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {icon: None,image: None,live: None,+ conversation: None,}).await;}@@ -3774,7 +4028,9 @@ async fn wait_for_outgoing_transfer(}TransferState::Failed(why) => bail!("transfer failed: {why:?}"),TransferState::Cancelled => bail!("transfer cancelled"),- TransferState::Waiting => print!("\rWaiting for the device… "),+ TransferState::Requested | TransferState::Waiting => {+ print!("\rWaiting for the device… ")+ }TransferState::Running => {let percent = (t.done * 100).checked_div(t.total).unwrap_or(100);print!("\rSending… {percent}% ");@@ -4093,6 +4349,7 @@ fn print_event(node: &Node, event: &NodeEvent) {node.paired_devices().ok().and_then(|ds| ds.into_iter().find(|d| d.id == *id).map(|d| d.info.name))+ .or_else(|| node.localsend_peers().into_iter().find(|p| p.id == *id).map(|p| p.alias)).unwrap_or_else(|| id.short())};match event {@@ -4244,6 +4501,25 @@ fn print_event(node: &Node, event: &NodeEvent) {photo.size,photo.thumb.len()),+ NodeEvent::CameraCaptureRequested { device, request_id, mode } => {+ println!("{}: requested Continuity Camera {:?} ({request_id})", name(device), mode)+ }+ NodeEvent::CameraCaptureReceived {+ device, request_id, mode, file_name, data, saved_path, ..+ } => {+ let where_str = saved_path.as_ref().map(|p| format!(" -> {}", p.display())).unwrap_or_default();+ println!(+ "{}: Continuity Camera {:?} {file_name} ({} bytes, {request_id}){where_str}",+ name(device),+ mode,+ data.len()+ );+ }+ NodeEvent::CameraCaptureCancelled { device, request_id, reason } => println!(+ "{}: Continuity Camera cancelled ({request_id}{})",+ name(device),+ reason.as_deref().map(|r| format!(": {r}")).unwrap_or_default()+ ),NodeEvent::MediaChanged { device, players } if players.is_empty() => {println!("{}: nothing playing", name(device));}@@ -4264,6 +4540,16 @@ fn print_event(node: &Node, event: &NodeEvent) {}}NodeEvent::Transfer(t) if t.direction == Direction::Incoming => match &t.state {+ TransferState::Requested => {+ println!(+ "{}: incoming LocalSend transfer ({} file(s), {} bytes): {}; accepting…",+ name(&t.device),+ t.files,+ t.total,+ t.names.join(", ")+ );+ node.accept_transfer(&t.id);+ }TransferState::Done { saved } => {for path in saved {if t.recording {@@ -4529,6 +4815,55 @@ fn resolve(node: &Node, query: &str) -> Result<DeviceId> {}}+/// Resolves a target for `send`: a paired Nectarlink device, a discovered+/// LocalSend peer, or a `host[:port]` LocalSend address.+async fn resolve_send_target(node: &Node, query: &str, default_port: u16) -> Result<(DeviceId, bool)> {+ if let Ok(id) = resolve(node, query)+ && node.paired_devices()?.iter().any(|d| d.id == id)+ {+ return Ok((id, false));+ }+ if let Some((host, port_str)) = query.rsplit_once(':')+ && !host.is_empty()+ && let Ok(port) = port_str.parse::<u16>()+ {+ if !node.localsend_enabled() {+ let _ = node.set_localsend_enabled(true);+ }+ let peer = node+ .probe_localsend_peer(host, port)+ .await+ .with_context(|| format!("no LocalSend peer responded at {host}:{port}"))?;+ return Ok((peer.id, true));+ }+ if query.parse::<std::net::IpAddr>().is_ok() {+ if !node.localsend_enabled() {+ let _ = node.set_localsend_enabled(true);+ }+ let peer = node+ .probe_localsend_peer(query, default_port)+ .await+ .with_context(|| format!("no LocalSend peer responded at {query}:{default_port}"))?;+ return Ok((peer.id, true));+ }+ if node.localsend_enabled() {+ let q = query.to_ascii_lowercase();+ let ls_matches: Vec<_> = node+ .localsend_peers()+ .into_iter()+ .filter(|p| {+ p.id.to_string().starts_with(&q)+ || p.alias.to_ascii_lowercase() == q+ || p.ip.eq_ignore_ascii_case(&q)+ })+ .collect();+ if let [one] = ls_matches.as_slice() {+ return Ok((one.id, true));+ }+ }+ resolve(node, query).map(|id| (id, false))+}+fn parse_id(s: &str) -> Result<DeviceId> {s.parse().with_context(|| format!("{s:?} is not a device ID"))}diff --git a/core/nectarlink-core/Cargo.toml b/core/nectarlink-core/Cargo.tomlindex f7f58bc..3e32c86 100644--- a/core/nectarlink-core/Cargo.toml+++ b/core/nectarlink-core/Cargo.toml@@ -19,9 +19,17 @@ serde.workspace = trueserde_json.workspace = truethiserror.workspace = truebytes.workspace = true+data-encoding.workspace = true+ring = "0.17"+rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }+rustls-pki-types = "1.15"+sha2.workspace = true+socket2 = "0.6"tokio.workspace = true+tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] }tokio-util.workspace = truetracing.workspace = true+url.workspace = true[target.'cfg(windows)'.dependencies]windows = { workspace = true, features = ["Win32_Foundation", "Win32_Security_Cryptography"] }diff --git a/core/nectarlink-core/src/clipboard_history.rs b/core/nectarlink-core/src/clipboard_history.rsindex 1626271..df91265 100644--- a/core/nectarlink-core/src/clipboard_history.rs+++ b/core/nectarlink-core/src/clipboard_history.rs@@ -20,6 +20,9 @@ use crate::{Error, identity::KeyProtector, otp};/// Maximum number of clips retained in history.pub const MAX_CLIPBOARD_HISTORY: usize = 50;+/// Maximum age in seconds for unpinned clips in history (7 days).+pub const MAX_CLIPBOARD_AGE_SECS: i64 = 7 * 86_400;+/// Larger images (a big photo) aren't kept, and the oldest unpinned images/// go once together they'd take more than this, so history stays small on disk.const MAX_IMAGE_BYTES: usize = 16 * 1024 * 1024;@@ -101,7 +104,7 @@ impl std::fmt::Debug for ClipboardHistoryStore {impl ClipboardHistoryStore {/// Opens the clipboard history store in `data_dir`, decrypting any existing- /// history with `protector`.+ /// history with `protector` and purging unpinned clips older than 7 days.pub fn open(data_dir: &Path, protector: Arc<dyn KeyProtector>) -> Self {let index_path = data_dir.join(INDEX_FILE);let state = match fs::read(&index_path) {@@ -112,7 +115,22 @@ impl ClipboardHistoryStore {.unwrap_or_default(),Err(_) => StoredState::default(),};- Self { data_dir: data_dir.to_path_buf(), protector, state: Mutex::new(state) }+ let store = Self { data_dir: data_dir.to_path_buf(), protector, state: Mutex::new(state) };+ {+ let mut s = store.state.lock().unwrap_or_else(|e| e.into_inner());+ let evicted = store.evict_locked(&mut s.items);+ if !evicted.is_empty() {+ let _ = store.save_locked(&s);+ }+ }+ store+ }++ /// Returns `(item_count, total_bytes)` currently stored in clipboard history.+ pub fn count_and_bytes(&self) -> (usize, u64) {+ let s = self.state.lock().unwrap_or_else(|e| e.into_inner());+ let bytes = s.items.iter().map(|i| i.size.saturating_add(i.entry.text.len() as u64)).sum();+ (s.items.len(), bytes)}/// Whether clipboard history recording is enabled.@@ -156,8 +174,9 @@ impl ClipboardHistoryStore {existing.entry.incoming = incoming;existing.entry.timestamp = now;sort_items(&mut s.items);+ let evicted = self.evict_locked(&mut s.items);let _ = self.save_locked(&s);- return Some((id, Vec::new()));+ return Some((id, evicted));}let id = alloc_id(&mut s.next_id);@@ -220,8 +239,9 @@ impl ClipboardHistoryStore {return None;}sort_items(&mut s.items);+ let evicted = self.evict_locked(&mut s.items);let _ = self.save_locked(&s);- return Some((existing_id, Vec::new()));+ return Some((existing_id, evicted));}let id = alloc_id(&mut s.next_id);@@ -358,6 +378,23 @@ impl ClipboardHistoryStore {fn evict_locked(&self, items: &mut Vec<StoredEntry>) -> Vec<String> {let mut evicted_ids = Vec::new();+ let now = now_unix();+ // First evict unpinned entries older than MAX_CLIPBOARD_AGE_SECS (7 days).+ let mut idx = 0;+ while idx < items.len() {+ let expired = !items[idx].entry.pinned+ && items[idx].entry.timestamp > 0+ && now.saturating_sub(items[idx].entry.timestamp) > MAX_CLIPBOARD_AGE_SECS;+ if expired {+ let evicted = items.remove(idx);+ if evicted.entry.kind == ClipboardItemKind::Image {+ let _ = fs::remove_file(self.image_path(&evicted.entry.id));+ }+ evicted_ids.push(evicted.entry.id);+ } else {+ idx += 1;+ }+ }while items.len() > MAX_CLIPBOARD_HISTORY {// Evict the oldest unpinned item (at the end of `items` after `sort_items`),// or the last item if all are pinned.@@ -528,4 +565,41 @@ mod tests {assert!(!store.record_text("should not record", "Pixel", false));assert!(store.list(None).is_empty());}++ #[test]+ fn evicts_unpinned_entries_older_than_7_days_and_keeps_pinned() {+ let dir = tempfile::tempdir().unwrap();+ let store = ClipboardHistoryStore::open(dir.path(), Arc::new(PlainKeyProtector));++ let (old_text_id, _) = store.record_text_with_id("old unpinned text", "Pixel", true).unwrap();+ let (pinned_old_id, _) = store.record_text_with_id("old pinned text", "Pixel", true).unwrap();+ assert!(store.set_pinned(&pinned_old_id, true));+ let png = b"\x89PNG\r\n\x1a\nold-image";+ let (old_img_id, _) = store.record_image_with_id("image/png", png, "Pixel", true).unwrap();++ // Backdate all three entries to 8 days ago and save to disk.+ let eight_days_ago = now_unix() - MAX_CLIPBOARD_AGE_SECS - 86_400;+ {+ let mut s = store.state.lock().unwrap();+ for item in &mut s.items {+ item.entry.timestamp = eight_days_ago;+ }+ store.save_locked(&s).unwrap();+ }+ assert!(store.image_path(&old_img_id).exists());++ // Re-opening purges the expired unpinned text and image, keeping the pinned entry.+ let reopened = ClipboardHistoryStore::open(dir.path(), Arc::new(PlainKeyProtector));+ let remaining = reopened.list(None);+ assert_eq!(remaining.len(), 1);+ assert_eq!(remaining[0].id, pinned_old_id);+ assert!(remaining[0].pinned);+ assert!(reopened.entry(&old_text_id).is_none());+ assert!(reopened.entry(&old_img_id).is_none());+ assert!(!reopened.image_path(&old_img_id).exists());++ let (count, bytes) = reopened.count_and_bytes();+ assert_eq!(count, 1);+ assert_eq!(bytes, "old pinned text".len() as u64);+ }}diff --git a/core/nectarlink-core/src/config.rs b/core/nectarlink-core/src/config.rsindex 91c300f..de6e433 100644--- a/core/nectarlink-core/src/config.rs+++ b/core/nectarlink-core/src/config.rs@@ -34,10 +34,17 @@ pub struct NodeConfig {/// Capabilities this device offers beyond the ones every build does/// (docs/protocol/capabilities.md), e.g. `media.control`.pub capabilities: Vec<String>,+ /// TCP/UDP port for LocalSend v2.1 interop (defaults to 53317, or+ /// `NECTARLINK_LOCALSEND_PORT` when set).+ pub localsend_port: u16,}impl NodeConfig {pub fn new(data_dir: impl Into<PathBuf>, device: DeviceInfo, app_version: impl Into<String>) -> Self {+ let localsend_port = std::env::var("NECTARLINK_LOCALSEND_PORT")+ .ok()+ .and_then(|v| v.parse::<u16>().ok())+ .unwrap_or(crate::LOCALSEND_DEFAULT_PORT);NodeConfig {data_dir: data_dir.into(),device,@@ -48,7 +55,8 @@ impl NodeConfig {port: 0,key_protector: None,downloads_dir: None,- capabilities: Vec::new(),+ capabilities: vec![nectarlink_protocol::messages::continuity_camera::RECEIVE.into()],+ localsend_port,}}}@@ -63,6 +71,7 @@ impl fmt::Debug for NodeConfig {.field("away_mode", &self.away_mode).field("lan_discovery", &self.lan_discovery).field("port", &self.port)+ .field("localsend_port", &self.localsend_port).field("downloads_dir", &self.downloads_dir).finish_non_exhaustive()}@@ -233,6 +242,22 @@ pub trait Platform: Send + Sync + 'static {Err("this device doesn't send texts".into())}+ /// Send a text or MMS with optional image attachments (checked: 1–20+ /// recipients, body or attachments non-empty, attachment MIME and sizes+ /// within limits).+ fn send_sms_with_attachments(+ &self,+ to: &[String],+ body: &str,+ attachments: &[crate::SmsAttachment],+ ) -> Result<(), String> {+ if attachments.is_empty() {+ self.sms_send(to, body)+ } else {+ Err("MMS attachments are not supported on this device".into())+ }+ }+/// A picture in a message: its type and bytes.fn sms_part(&self, _id: &str) -> Result<(String, Vec<u8>), String> {Err("this device has no messages".into())@@ -380,6 +405,19 @@ pub trait Platform: Send + Sync + 'static {/// The PC's webcam decoder needs a fresh keyframe (`webcam.keyframe`).fn webcam_keyframe_requested(&self, _peer: &nectarlink_protocol::DeviceId) {}++ /// A paired PC requested a Continuity Camera capture (`camera.capture.request`).+ /// Return `Ok(())` if the phone launched or queued the capture UI, or `Err(reason)` if unavailable.+ fn camera_capture_requested(+ &self,+ _peer: &nectarlink_protocol::DeviceId,+ _request: &crate::CameraCaptureRequest,+ ) -> Result<(), String> {+ Err("camera capture not supported on this device".into())+ }++ /// The peer cancelled an in-flight Continuity Camera capture (`camera.capture.cancel`).+ fn camera_capture_cancelled(&self, _peer: &nectarlink_protocol::DeviceId, _request_id: &str) {}}/// A platform that does nothing; useful for tests and headless tools.diff --git a/core/nectarlink-core/src/continuity_camera.rs b/core/nectarlink-core/src/continuity_camera.rsnew file mode 100644index 0000000..f8b678b--- /dev/null+++ b/core/nectarlink-core/src/continuity_camera.rs@@ -0,0 +1,343 @@+// SPDX-License-Identifier: MPL-2.0+//! Continuity Camera: a paired PC asks a phone to take a photo or scan a+//! document (`camera.capture.request`), and the phone streams the captured+//! image back on a dedicated `camera` / `result` stream.++use std::{sync::Arc, time::Duration};++use iroh::endpoint::{RecvStream, SendStream};+use nectarlink_protocol::{+ DeviceId, Envelope, ErrorCode,+ messages::{+ CameraCaptureCancel, CameraCaptureMode, CameraCaptureOk, CameraCaptureRequest,+ CameraCaptureResultMeta, StreamHeader, continuity_camera, types,+ },+ read_frame, write_frame,+};++use crate::{Error, NodeEvent, Result, error::net, node::Shared, session::Session};++/// Per-device toggle that gates Continuity Camera along with photo sharing.+pub const TOGGLE: &str = "photos";++const RESULT_TIMEOUT: Duration = Duration::from_secs(60);+const CHUNK_BYTES: usize = 64 * 1024;++/// Whether a Continuity Camera `request_id` meets the protocol bounds.+pub fn valid_request_id(id: &str) -> bool {+ !id.is_empty() && id.len() <= continuity_camera::MAX_ID_BYTES && !id.chars().any(char::is_control)+}++fn detail_for_mode(mode: CameraCaptureMode) -> &'static str {+ match mode {+ CameraCaptureMode::Photo => "Continuity Camera · Photo",+ CameraCaptureMode::Scan => "Continuity Camera · Document scan",+ }+}++/// Asks a paired phone to open its camera UI and capture a photo or scan a document.+pub(crate) async fn request(+ shared: &Shared,+ peer: DeviceId,+ request_id: String,+ mode: CameraCaptureMode,+) -> Result<()> {+ if !valid_request_id(&request_id) {+ return Err(Error::Protocol("invalid continuity camera request_id".into()));+ }+ if !shared.store.is_paired(&peer)? {+ return Err(Error::NotPaired);+ }+ if !shared.toggle_on(&peer, TOGGLE) {+ return Err(Error::Denied);+ }+ let session = shared.session(&peer).ok_or(Error::Offline)?;+ let env = Envelope::new(types::CAMERA_CAPTURE_REQUEST, &CameraCaptureRequest { request_id, mode })?;+ session.send(env).await+}++/// Cancels an in-flight Continuity Camera capture request on either side.+pub(crate) async fn cancel(+ shared: &Shared,+ peer: DeviceId,+ request_id: String,+ reason: Option<String>,+) -> Result<()> {+ if !valid_request_id(&request_id) {+ return Err(Error::Protocol("invalid continuity camera request_id".into()));+ }+ if !shared.store.is_paired(&peer)? {+ return Err(Error::NotPaired);+ }+ let session = shared.session(&peer).ok_or(Error::Offline)?;+ let env = Envelope::new(types::CAMERA_CAPTURE_CANCEL, &CameraCaptureCancel { request_id, reason })?;+ session.send(env).await+}++/// Handles `camera.capture.*` messages on the control stream.+pub(crate) async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: &Envelope) -> Result<bool> {+ match env.t.as_str() {+ types::CAMERA_CAPTURE_REQUEST => {+ let req: CameraCaptureRequest = env.body()?;+ if !valid_request_id(&req.request_id) {+ return Err(Error::Protocol("invalid continuity camera request_id".into()));+ }+ if !shared.toggle_on(&session.peer, TOGGLE) {+ let cancel = Envelope::new(+ types::CAMERA_CAPTURE_CANCEL,+ &CameraCaptureCancel {+ request_id: req.request_id,+ reason: Some("photos sharing is turned off".into()),+ },+ )?;+ let _ = session.send(cancel).await;+ return Ok(true);+ }+ match shared.platform.camera_capture_requested(&session.peer, &req) {+ Ok(()) => {+ let ok = Envelope::new(+ types::CAMERA_CAPTURE_OK,+ &CameraCaptureOk { request_id: req.request_id.clone() },+ )?;+ let _ = session.send(ok).await;+ shared.emit(NodeEvent::CameraCaptureRequested {+ device: session.peer,+ request_id: req.request_id,+ mode: req.mode,+ });+ }+ Err(reason) => {+ let cancel = Envelope::new(+ types::CAMERA_CAPTURE_CANCEL,+ &CameraCaptureCancel { request_id: req.request_id, reason: Some(reason) },+ )?;+ let _ = session.send(cancel).await;+ }+ }+ Ok(true)+ }+ types::CAMERA_CAPTURE_OK => {+ let ok: CameraCaptureOk = env.body()?;+ if !valid_request_id(&ok.request_id) {+ return Err(Error::Protocol("invalid continuity camera request_id".into()));+ }+ Ok(true)+ }+ types::CAMERA_CAPTURE_CANCEL => {+ let cancel: CameraCaptureCancel = env.body()?;+ if !valid_request_id(&cancel.request_id) {+ return Err(Error::Protocol("invalid continuity camera request_id".into()));+ }+ shared.platform.camera_capture_cancelled(&session.peer, &cancel.request_id);+ shared.emit(NodeEvent::CameraCaptureCancelled {+ device: session.peer,+ request_id: cancel.request_id,+ reason: cancel.reason,+ });+ Ok(true)+ }+ _ => Ok(false),+ }+}++/// Sends a captured photo or scanned document from the phone to the requesting PC.+pub(crate) async fn send_result(+ shared: &Arc<Shared>,+ session: &Arc<Session>,+ meta: CameraCaptureResultMeta,+ bytes: Vec<u8>,+) -> Result<()> {+ if !valid_request_id(&meta.request_id) {+ return Err(Error::Protocol("invalid continuity camera request_id".into()));+ }+ if meta.file_name.trim().is_empty() || meta.file_name.len() > continuity_camera::MAX_NAME_BYTES {+ return Err(Error::Protocol("invalid continuity camera file_name".into()));+ }+ if !matches!(meta.mime.as_str(), "image/jpeg" | "image/png") {+ return Err(Error::Protocol("unsupported continuity camera mime".into()));+ }+ if meta.size > continuity_camera::MAX_IMAGE_BYTES+ || bytes.len() as u64 > continuity_camera::MAX_IMAGE_BYTES+ {+ return Err(Error::TooLarge);+ }+ if meta.size == 0 || bytes.len() as u64 != meta.size {+ return Err(Error::Protocol("continuity camera size mismatch".into()));+ }+ if !shared.toggle_on(&session.peer, TOGGLE) {+ return Err(Error::Denied);+ }++ let safe_name = crate::transfer::safe_file_name(&meta.file_name);+ let clean_meta = CameraCaptureResultMeta { file_name: safe_name.clone(), ..meta };++ let exchange = async {+ let (mut send, mut recv) = session.conn.open_bi().await.map_err(net)?;+ let header = StreamHeader {+ svc: continuity_camera::SERVICE.into(),+ op: continuity_camera::OP_RESULT.into(),+ v: continuity_camera::VERSION,+ };+ write_frame(&mut send, &Envelope::new(types::STREAM, &header)?.to_cbor()).await?;+ write_frame(&mut send, &Envelope::new(continuity_camera::RESULT_META, &clean_meta)?.to_cbor())+ .await?;+ for chunk in bytes.chunks(CHUNK_BYTES) {+ if let Err(e) = send.write_all(chunk).await {+ let _ = send.finish();+ if let Ok(Some(reply_bytes)) = read_frame(&mut recv).await+ && let Ok(env) = Envelope::from_cbor(&reply_bytes)+ {+ env.expect(types::OK)?;+ }+ return Err(net(e));+ }+ }+ let _ = send.finish();+ let reply = read_frame(&mut recv).await?.ok_or(Error::Offline)?;+ Envelope::from_cbor(&reply)?.expect(types::OK)?;+ Ok::<(), Error>(())+ };++ tokio::time::timeout(RESULT_TIMEOUT, exchange).await.map_err(|_| Error::Timeout)??;++ let _ = shared.record_timeline(crate::timeline::NewTimelineEntry {+ kind: crate::TimelineKind::Photo,+ device_id: session.peer,+ device_name: shared.peer_name(&session.peer),+ incoming: false,+ timestamp: crate::now_unix(),+ title: safe_name,+ detail: detail_for_mode(clean_meta.mode).into(),+ target: String::new(),+ size_bytes: clean_meta.size,+ duration_secs: 0,+ ref_id: None,+ });++ Ok(())+}++/// Receives a Continuity Camera capture result stream on the PC.+pub(crate) async fn receive_result(+ shared: Arc<Shared>,+ peer: DeviceId,+ mut send: SendStream,+ mut recv: RecvStream,+) {+ let reply = match tokio::time::timeout(RESULT_TIMEOUT, take_result(&shared, peer, &mut recv)).await {+ Ok(Ok(())) => Envelope::empty(types::OK),+ Ok(Err((code, msg))) => Envelope::error(code, msg),+ Err(_) => Envelope::error(ErrorCode::Internal, "continuity camera transfer timed out"),+ };+ if reply.t != types::OK {+ let _ = recv.stop(0u32.into());+ }+ let _ = write_frame(&mut send, &reply.to_cbor()).await;+ let _ = send.finish();+ let _ = tokio::time::timeout(Duration::from_secs(2), send.stopped()).await;+}++async fn take_result(+ shared: &Arc<Shared>,+ peer: DeviceId,+ recv: &mut RecvStream,+) -> std::result::Result<(), (ErrorCode, &'static str)> {+ let meta = read_frame(recv)+ .await+ .ok()+ .flatten()+ .and_then(|bytes| Envelope::from_cbor(&bytes).ok())+ .and_then(|env| env.expect_body::<CameraCaptureResultMeta>(continuity_camera::RESULT_META).ok())+ .ok_or((ErrorCode::BadMessage, "invalid continuity camera metadata"))?;++ if !valid_request_id(&meta.request_id) {+ return Err((ErrorCode::BadMessage, "invalid request_id"));+ }+ if !matches!(meta.mime.as_str(), "image/jpeg" | "image/png") {+ return Err((ErrorCode::BadMessage, "unsupported continuity camera format"));+ }+ if meta.size == 0 || meta.size > continuity_camera::MAX_IMAGE_BYTES {+ return Err((ErrorCode::BadMessage, "invalid continuity camera size"));+ }+ if !shared.toggle_on(&peer, TOGGLE) {+ return Err((ErrorCode::Denied, "photos sharing is turned off"));+ }++ let size = usize::try_from(meta.size).map_err(|_| (ErrorCode::BadMessage, "image too large"))?;+ let mut data = Vec::with_capacity(size.min(CHUNK_BYTES));+ let mut buf = vec![0u8; CHUNK_BYTES];+ while data.len() < size {+ let want = (size - data.len()).min(buf.len());+ match recv.read(&mut buf[..want]).await {+ Ok(Some(n)) if n > 0 => data.extend_from_slice(&buf[..n]),+ _ => return Err((ErrorCode::BadMessage, "continuity camera stream ended early")),+ }+ }++ let default_ext = if meta.mime == "image/png" { "png" } else { "jpg" };+ let fallback_name = match meta.mode {+ CameraCaptureMode::Photo => format!("Photo.{default_ext}"),+ CameraCaptureMode::Scan => format!("Scan.{default_ext}"),+ };+ let raw_name = if meta.file_name.trim().is_empty() { &fallback_name } else { &meta.file_name };+ let safe_name = crate::transfer::safe_file_name(raw_name);++ let saved_path = match tokio::fs::create_dir_all(&shared.downloads_dir).await {+ Ok(()) => {+ let dest = crate::transfer::unique_path(&shared.downloads_dir, &safe_name);+ match tokio::fs::write(&dest, &data).await {+ Ok(()) => Some(dest),+ Err(e) => {+ tracing::warn!(error = %e, "can't save continuity camera capture");+ None+ }+ }+ }+ Err(e) => {+ tracing::warn!(error = %e, "can't create downloads dir for continuity camera capture");+ None+ }+ };++ let target = saved_path.as_ref().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default();+ let _ = shared.record_timeline(crate::timeline::NewTimelineEntry {+ kind: crate::TimelineKind::Photo,+ device_id: peer,+ device_name: shared.peer_name(&peer),+ incoming: true,+ timestamp: crate::now_unix(),+ title: safe_name.clone(),+ detail: detail_for_mode(meta.mode).into(),+ target,+ size_bytes: meta.size,+ duration_secs: 0,+ ref_id: None,+ });++ shared.emit(NodeEvent::CameraCaptureReceived {+ device: peer,+ request_id: meta.request_id,+ mode: meta.mode,+ file_name: safe_name,+ mime: meta.mime,+ width: meta.width,+ height: meta.height,+ data,+ saved_path,+ });++ Ok(())+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn request_id_validation() {+ assert!(valid_request_id("req-1234"));+ assert!(!valid_request_id(""));+ assert!(!valid_request_id("bad\nid"));+ assert!(!valid_request_id(&"x".repeat(continuity_camera::MAX_ID_BYTES + 1)));+ }+}diff --git a/core/nectarlink-core/src/events.rs b/core/nectarlink-core/src/events.rsindex de48c65..ee9d1a4 100644--- a/core/nectarlink-core/src/events.rs+++ b/core/nectarlink-core/src/events.rs@@ -1,9 +1,11 @@// SPDX-License-Identifier: MPL-2.0//! Events the core pushes to the UI. UIs render from these; they never poll.+use std::path::PathBuf;+use nectarlink_protocol::{DeviceId,- messages::{Battery, DeviceInfo, MediaPlayer, Notification, PowerLevel},+ messages::{Battery, CameraCaptureMode, DeviceInfo, MediaPlayer, Notification, PowerLevel},};use crate::{features::CapabilityMatrix, transfer::Transfer};@@ -209,6 +211,32 @@ pub enum NodeEvent {device: DeviceId,on: bool,},+ /// A paired PC requested a Continuity Camera capture on this phone.+ CameraCaptureRequested {+ device: DeviceId,+ request_id: String,+ mode: CameraCaptureMode,+ },+ /// A paired phone completed a Continuity Camera capture and sent the image to this PC.+ CameraCaptureReceived {+ device: DeviceId,+ request_id: String,+ mode: CameraCaptureMode,+ file_name: String,+ mime: String,+ width: u32,+ height: u32,+ data: Vec<u8>,+ saved_path: Option<PathBuf>,+ },+ /// A Continuity Camera capture was cancelled by the peer.+ CameraCaptureCancelled {+ device: DeviceId,+ request_id: String,+ reason: Option<String>,+ },/// The local timeline of items shared with paired devices changed.TimelineChanged,+ /// LocalSend interop state or discovered LocalSend peers changed.+ LocalSendChanged,}diff --git a/core/nectarlink-core/src/features.rs b/core/nectarlink-core/src/features.rsindex f1a03b1..5ac7e54 100644--- a/core/nectarlink-core/src/features.rs+++ b/core/nectarlink-core/src/features.rs@@ -863,6 +863,16 @@ pub const FEATURES: &[FeatureDef] = &[],partial: None,},+ FeatureDef {+ id: "camera.continuity",+ group: FeatureGroup::Camera,+ requires: &[+ phone("camera.capture", Unlock::Permission(Permission::Camera)),+ desktop("camera.continuity", UPDATE),+ DeviceToggle("photos"),+ ],+ partial: None,+ },// InputFeatureDef {id: "input.remote",@@ -924,6 +934,7 @@ mod tests {"media.control","media.remote","camera.stream",+ "camera.capture","toggles.read","toggles.ringer","toggles.volume",@@ -945,6 +956,7 @@ mod tests {"mirror.view","mirror.listen","camera.virtual",+ "camera.continuity",];fn pc() -> DeviceFacts {@@ -972,6 +984,7 @@ mod tests {fn basic_phone_matches_the_documented_examples() {let m = matrix(&facts(DeviceKind::Phone, "android", "16", PowerLevel::Basic, BASIC_PHONE));assert_eq!(m.state("clipboard.pc_to_phone"), Some(FeatureState::Available));+ assert_eq!(m.state("camera.continuity"), Some(FeatureState::Available));assert_eq!(m.state("clipboard.auto_phone_to_pc"),Some(FeatureState::Partial {diff --git a/core/nectarlink-core/src/lib.rs b/core/nectarlink-core/src/lib.rsindex 0c8c33b..561f24c 100644--- a/core/nectarlink-core/src/lib.rs+++ b/core/nectarlink-core/src/lib.rs@@ -13,11 +13,13 @@ mod clipboard;pub mod clipboard_history;mod config;mod contacts;+pub mod continuity_camera;pub mod deck;mod error;mod events;pub mod features;mod identity;+pub mod localsend;mod media;mod mirror;mod node;@@ -40,7 +42,9 @@ pub use calls::CallCommand;pub use clip_kind::{ClipKind, ClipSuggestion, classify_clip, maps_web_url, percent_encode, tracking_search_url,};-pub use clipboard_history::{ClipboardHistoryEntry, ClipboardItemKind, MAX_CLIPBOARD_HISTORY};+pub use clipboard_history::{+ ClipboardHistoryEntry, ClipboardItemKind, MAX_CLIPBOARD_AGE_SECS, MAX_CLIPBOARD_HISTORY,+};pub use config::{NodeConfig, NoopPlatform, Platform};pub use deck::{COMMANDS_TOGGLE as DECK_COMMANDS_TOGGLE, DECK_ACTIONS, DeckAction, DeckConfig, DeckLayout, DeckPage,@@ -55,6 +59,10 @@ pub use events::{};pub use features::{CapabilityMatrix, FeatureState};pub use identity::{KeyProtector, PlainKeyProtector, default_protector};+pub use localsend::{+ LOCALSEND_DEFAULT_PORT, LOCALSEND_MULTICAST_ADDR, LOCALSEND_PROTOCOL_VERSION, LocalSendPeer,+ peer_device_id as localsend_peer_device_id,+};pub use media::{MediaAction, MediaError};pub use mirror::{MirrorSend, MirrorSink, MirrorStream};pub use nectarlink_protocol::messages::clip::{@@ -63,20 +71,25 @@ pub use nectarlink_protocol::messages::clip::{pub use nectarlink_protocol::{DeviceId, PacketKind,messages::{- AudioOutputDevice, Battery, CLIP_MAX_BYTES, CallControls, CallLogEntry, CallState, Contact,- ContactNumber, DeviceInfo, DeviceKind, LivePoint, LiveSegment, MediaPlayer, MirrorAudioConfig,- MirrorConfig, MirrorInput, MirrorResize, MirrorStart, Notification, NotificationAction,- NotificationLive, PcAudioSet, PcWakeInfo, PhoneApp, PhoneToggleSet, PhoneToggleValue, PhoneToggles,- PhotoAlbum, PhotoItem, PhotoNew as Photo, PhotoThumb, PowerLevel, ScreenCorners, ScreenRect,- ScreenShape, SmsMessage, SmsPart, SmsThread, StorageChanged, StorageDelete, StorageEntries,- StorageEntry, StorageList, StorageMkdir, StorageRead, StorageReadMeta, StorageRename,- StorageWriteAccept, StorageWriteDone, StorageWriteOffer, TaskNotify, TouchAction, WebcamConfig,- WebcamStart,+ AudioOutputDevice, Battery, CLIP_MAX_BYTES, CallControls, CallLogEntry, CallState,+ CameraCaptureCancel, CameraCaptureMode, CameraCaptureOk, CameraCaptureRequest,+ CameraCaptureResultMeta, Contact, ContactNumber, DeviceInfo, DeviceKind, LivePoint, LiveSegment,+ MediaPlayer, MirrorAudioConfig, MirrorConfig, MirrorInput, MirrorResize, MirrorStart, Notification,+ NotificationAction, NotificationChatMessage, NotificationConversation, NotificationLive, PcAudioSet,+ PcWakeInfo, PhoneApp, PhoneToggleSet, PhoneToggleValue, PhoneToggles, PhotoAlbum, PhotoItem,+ PhotoNew as Photo, PhotoThumb, PowerLevel, ScreenCorners, ScreenRect, ScreenShape, SmsAttachment,+ SmsMessage, SmsPart, SmsThread, StorageChanged, StorageDelete, StorageEntries, StorageEntry,+ StorageList, StorageMkdir, StorageRead, StorageReadMeta, StorageRename, StorageWriteAccept,+ StorageWriteDone, StorageWriteOffer, TaskNotify, TouchAction, WebcamConfig, WebcamStart,calls::{CONTROL as CALLS_CONTROL, DIAL as CALLS_DIAL, IN_CALL as CALLS_IN_CALL, LOG as CALLS_LOG,SHOW as CALLS_SHOW, STATE as CALLS_STATE,},contacts::{READ as CONTACTS_READ, SHOW as CONTACTS_SHOW},+ continuity_camera::{+ CAPTURE as CAMERA_CAPTURE, MAX_IMAGE_BYTES as CAMERA_CAPTURE_MAX_IMAGE_BYTES,+ RECEIVE as CAMERA_CONTINUITY,+ },format_mac, ipv4_broadcast, is_package_name, is_valid_storage_dir_path, is_valid_storage_id,is_valid_storage_name, is_valid_storage_path, magic_packet,mirror::{@@ -92,7 +105,10 @@ pub use nectarlink_protocol::{READ as PHOTOS_READ, SHOW as PHOTOS_SHOW,},ringer_modes, screen_limits,- sms::{READ as SMS_READ, SEND as SMS_SEND, SHOW as SMS_SHOW},+ sms::{+ MAX_ATTACHMENT_BYTES as SMS_MAX_ATTACHMENT_BYTES, MAX_ATTACHMENTS as SMS_MAX_ATTACHMENTS,+ READ as SMS_READ, SEND as SMS_SEND, SHOW as SMS_SHOW,+ },storage::{MOUNT as STORAGE_MOUNT, READ as STORAGE_READ, WRITE as STORAGE_WRITE},task_notify, toggle_ids,toggles::{@@ -108,10 +124,14 @@ pub use nectarlink_protocol::{},pairing::PairingUri,};-pub use node::Node;+pub use node::{DataRetentionCounts, Node};pub use notifications::NotificationError;pub use remote::{ButtonAction, INPUT_INJECT, KeyMod, MouseButton, RemoteInput, SlideAction, remote_keys};pub use storage::{FolderStorage, StorageError, StorageReadFile, TOGGLE as STORAGE_TOGGLE};+pub use store::{+ CHAT_RETENTION_SECS, ChatMessageRecord, ChatThreadRecord, MAX_CHAT_MESSAGES_PER_THREAD,+ MAX_CHAT_THREADS_PER_PEER, chat_thread_id,+};pub use timeline::{DEFAULT_TIMELINE_MAX_DAYS, DEFAULT_TIMELINE_MAX_ENTRIES, DEFAULT_TIMELINE_PAGE_LIMIT,MAX_TIMELINE_PAGE_LIMIT, TimelineEntry, TimelineKind, TimelinePage, TimelineQuery, TimelineRetention,diff --git a/core/nectarlink-core/src/localsend.rs b/core/nectarlink-core/src/localsend.rsnew file mode 100644index 0000000..e9874ed--- /dev/null+++ b/core/nectarlink-core/src/localsend.rs@@ -0,0 +1,2210 @@+// SPDX-License-Identifier: MPL-2.0+//! Clean-room Rust implementation of the LocalSend Protocol v2.1+//! (https://github.com/localsend/protocol, MIT License, Copyright (c)+//! 2022-2024 Tien Do Nam). No Dart code from LocalSend is used.+//!+//! Coexistence (`docs/PLAN.md` §3.6):+//! - Listens on UDP multicast `224.0.0.167:53317` (with `SO_REUSEADDR`) and TCP+//! port `53317` (configurable via `NodeConfig::localsend_port`).+//! - If TCP port `53317` is already taken by the standalone LocalSend app on the+//! same PC, Nectarlink logs a notice, leaves receiving to LocalSend, and still+//! discovers and sends files to LocalSend peers on the network.++use std::{+ collections::HashMap,+ fs,+ io::ErrorKind,+ net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4},+ path::{Path, PathBuf},+ sync::{+ Arc, Mutex,+ atomic::{AtomicBool, Ordering},+ },+ time::{Duration, Instant},+};++use nectarlink_protocol::{+ DeviceId,+ messages::{DeviceInfo, DeviceKind, files},+};+use ring::{+ rand::{SecureRandom, SystemRandom},+ signature::{ECDSA_P256_SHA256_ASN1_SIGNING, EcdsaKeyPair, KeyPair},+};+use rustls::{+ ClientConfig, DigitallySignedStruct, ServerConfig, SignatureScheme,+ client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier},+ crypto::{CryptoProvider, verify_tls12_signature, verify_tls13_signature},+};+use rustls_pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName, UnixTime};+use serde::{Deserialize, Serialize};+use sha2::{Digest, Sha256};+use socket2::{Domain, Protocol, Socket, Type};+use tokio::{+ io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},+ net::{TcpListener, TcpStream, UdpSocket},+ sync::oneshot,+};+use tokio_rustls::{TlsAcceptor, TlsConnector};+use tokio_util::sync::CancellationToken;++use crate::{+ Error, Result, TimelineKind,+ events::NodeEvent,+ identity::KeyProtector,+ node::Shared,+ timeline::NewTimelineEntry,+ transfer::{+ Direction, FileSource, OutgoingFile, Transfer, TransferFailure, TransferState, outgoing_target_path,+ safe_file_name, storable_name, transfer_timeline_title, unique_path,+ },+};++/// Default TCP and UDP port used by the LocalSend protocol.+pub const LOCALSEND_DEFAULT_PORT: u16 = 53317;++/// Default IPv4 multicast group used by the LocalSend protocol.+pub const LOCALSEND_MULTICAST_ADDR: Ipv4Addr = Ipv4Addr::new(224, 0, 0, 167);++/// LocalSend protocol version implemented by this module.+pub const LOCALSEND_PROTOCOL_VERSION: &str = "2.1";++const STATE_FILE: &str = "localsend.enc";+const APPROVAL_TIMEOUT: Duration = Duration::from_secs(60);+const CONNECT_TIMEOUT: Duration = Duration::from_secs(6);+const IO_TIMEOUT: Duration = Duration::from_secs(30);+const PROGRESS_EVERY: Duration = Duration::from_millis(150);+const UPLOAD_CHUNK: usize = 64 * 1024;+const MAX_HEADER_BYTES: usize = 64 * 1024;+const MAX_JSON_BODY_BYTES: usize = 4 * 1024 * 1024;++/// A LocalSend device discovered on the local network.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct LocalSendPeer {+ pub id: DeviceId,+ pub alias: String,+ pub device_model: Option<String>,+ pub device_type: String,+ pub fingerprint: String,+ pub ip: String,+ pub port: u16,+ /// `"https"` or `"http"`.+ pub protocol: String,+}++/// Derives a deterministic Nectarlink [`DeviceId`] for a LocalSend peer from its+/// certificate fingerprint (or `ip:port` when encryption is off).+pub fn peer_device_id(fingerprint: &str, ip: &str, port: u16) -> DeviceId {+ let mut hasher = Sha256::new();+ hasher.update(b"nectarlink-localsend:v2:");+ let trimmed = fingerprint.trim();+ if !trimmed.is_empty() && !trimmed.eq_ignore_ascii_case("random") {+ hasher.update(trimmed.as_bytes());+ } else {+ hasher.update(format!("{ip}:{port}").as_bytes());+ }+ let digest = hasher.finalize();+ let mut bytes = [0u8; 32];+ bytes.copy_from_slice(&digest);+ DeviceId(bytes)+}++#[derive(Debug, Clone, Serialize, Deserialize, Default)]+struct StoredLocalSendState {+ #[serde(default)]+ enabled: bool,+ #[serde(default)]+ cert_der_hex: String,+ #[serde(default)]+ key_pkcs8_hex: String,+}++#[derive(Debug, Clone)]+pub(crate) struct LocalSendIdentity {+ pub cert_der: Vec<u8>,+ pub key_pkcs8: Vec<u8>,+ pub fingerprint: String,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+struct MulticastDto {+ alias: String,+ #[serde(default = "default_version")]+ version: String,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ device_model: Option<String>,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ device_type: Option<String>,+ #[serde(default)]+ fingerprint: String,+ #[serde(default = "default_port")]+ port: u16,+ #[serde(default = "default_protocol")]+ protocol: String,+ #[serde(default)]+ download: bool,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ announce: Option<bool>,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ announcement: Option<bool>,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+struct InfoDto {+ alias: String,+ #[serde(default = "default_version")]+ version: String,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ device_model: Option<String>,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ device_type: Option<String>,+ #[serde(default)]+ fingerprint: String,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ port: Option<u16>,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ protocol: Option<String>,+ #[serde(default)]+ download: bool,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+struct FileDto {+ id: String,+ file_name: String,+ size: u64,+ file_type: String,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ sha256: Option<String>,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ preview: Option<String>,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+struct PrepareUploadRequestDto {+ info: InfoDto,+ files: HashMap<String, FileDto>,+}++#[derive(Debug, Clone, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+struct PrepareUploadResponseDto {+ session_id: String,+ files: HashMap<String, String>,+}++fn default_version() -> String {+ LOCALSEND_PROTOCOL_VERSION.into()+}++fn default_port() -> u16 {+ LOCALSEND_DEFAULT_PORT+}++fn default_protocol() -> String {+ "https".into()+}++fn device_kind_str(kind: DeviceKind) -> &'static str {+ match kind {+ DeviceKind::Phone => "mobile",+ DeviceKind::Tablet => "tablet",+ DeviceKind::Desktop | DeviceKind::Laptop | DeviceKind::Unknown => "desktop",+ }+}++#[derive(Debug, Clone)]+struct IncomingFileSpec {+ file_id: String,+ token: String,+ safe_name: String,+ folder: Option<String>,+ size: u64,+ part_path: PathBuf,+ uploaded: bool,+}++struct UploadSession {+ transfer: Transfer,+ peer_name: String,+ staging_dir: PathBuf,+ files: Vec<IncomingFileSpec>,+ folder_roots: HashMap<String, PathBuf>,+ saved_items: Vec<PathBuf>,+ cancel: CancellationToken,+ last_progress: Instant,+}++struct RunningState {+ cancel: CancellationToken,+ announce_tx: tokio::sync::mpsc::UnboundedSender<()>,+}++/// Manages LocalSend discovery, server, and active upload sessions for a [`Node`](crate::Node).+pub(crate) struct LocalSendState {+ data_dir: PathBuf,+ protector: Arc<dyn KeyProtector>,+ configured_port: u16,+ enabled: AtomicBool,+ receiving: AtomicBool,+ bound_port: Mutex<u16>,+ identity: LocalSendIdentity,+ server_tls: Arc<ServerConfig>,+ client_tls: Arc<ClientConfig>,+ peers: Mutex<HashMap<DeviceId, LocalSendPeer>>,+ running: Mutex<Option<RunningState>>,+ pending_approvals: Mutex<HashMap<String, oneshot::Sender<bool>>>,+ sessions: Mutex<HashMap<String, Arc<Mutex<UploadSession>>>>,+}++impl std::fmt::Debug for LocalSendState {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("LocalSendState")+ .field("enabled", &self.enabled.load(Ordering::Relaxed))+ .field("receiving", &self.receiving.load(Ordering::Relaxed))+ .field("fingerprint", &self.identity.fingerprint)+ .finish_non_exhaustive()+ }+}++impl LocalSendState {+ pub fn open(data_dir: &Path, protector: Arc<dyn KeyProtector>, configured_port: u16) -> Result<Self> {+ let state_path = data_dir.join(STATE_FILE);+ let mut stored: StoredLocalSendState = match fs::read(&state_path) {+ Ok(sealed) => protector+ .unprotect(&sealed)+ .ok()+ .and_then(|plain| serde_json::from_slice(&plain).ok())+ .unwrap_or_default(),+ Err(_) => StoredLocalSendState::default(),+ };++ let identity = match decode_stored_identity(&stored) {+ Some(id) => id,+ None => {+ let id = generate_self_signed_identity()?;+ stored.cert_der_hex = data_encoding::HEXLOWER.encode(&id.cert_der);+ stored.key_pkcs8_hex = data_encoding::HEXLOWER.encode(&id.key_pkcs8);+ let _ = save_stored_state(data_dir, &*protector, &stored);+ id+ }+ };++ let server_tls = Arc::new(build_server_tls_config(&identity)?);+ let client_tls = Arc::new(build_client_tls_config()?);++ Ok(Self {+ data_dir: data_dir.to_path_buf(),+ protector,+ configured_port: if configured_port == 0 { LOCALSEND_DEFAULT_PORT } else { configured_port },+ enabled: AtomicBool::new(stored.enabled),+ receiving: AtomicBool::new(false),+ bound_port: Mutex::new(if configured_port == 0 {+ LOCALSEND_DEFAULT_PORT+ } else {+ configured_port+ }),+ identity,+ server_tls,+ client_tls,+ peers: Mutex::new(HashMap::new()),+ running: Mutex::new(None),+ pending_approvals: Mutex::new(HashMap::new()),+ sessions: Mutex::new(HashMap::new()),+ })+ }++ pub fn enabled(&self) -> bool {+ self.enabled.load(Ordering::Relaxed)+ }++ pub fn receiving(&self) -> bool {+ self.receiving.load(Ordering::Relaxed)+ }++ pub fn bound_port(&self) -> u16 {+ *self.bound_port.lock().unwrap_or_else(|e| e.into_inner())+ }++ pub fn set_enabled(&self, shared: &Arc<Shared>, enabled: bool) -> Result<()> {+ let prev = self.enabled.swap(enabled, Ordering::SeqCst);+ let stored = StoredLocalSendState {+ enabled,+ cert_der_hex: data_encoding::HEXLOWER.encode(&self.identity.cert_der),+ key_pkcs8_hex: data_encoding::HEXLOWER.encode(&self.identity.key_pkcs8),+ };+ save_stored_state(&self.data_dir, &*self.protector, &stored)?;++ if enabled && !prev {+ self.start(shared);+ } else if !enabled && prev {+ self.stop(shared);+ }+ shared.emit(NodeEvent::LocalSendChanged);+ Ok(())+ }++ pub fn peers(&self) -> Vec<LocalSendPeer> {+ let mut list: Vec<LocalSendPeer> =+ self.peers.lock().unwrap_or_else(|e| e.into_inner()).values().cloned().collect();+ list.sort_by(|a, b| {+ a.alias.cmp(&b.alias).then_with(|| a.ip.cmp(&b.ip)).then_with(|| a.port.cmp(&b.port))+ });+ list+ }++ pub fn peer(&self, id: &DeviceId) -> Option<LocalSendPeer> {+ self.peers.lock().unwrap_or_else(|e| e.into_inner()).get(id).cloned()+ }++ pub fn has_peer(&self, id: &DeviceId) -> bool {+ self.peers.lock().unwrap_or_else(|e| e.into_inner()).contains_key(id)+ }++ pub fn peer_name(&self, id: &DeviceId) -> Option<String> {+ self.peers.lock().unwrap_or_else(|e| e.into_inner()).get(id).map(|p| p.alias.clone())+ }++ pub fn upsert_peer(&self, shared: &Shared, peer: LocalSendPeer) -> bool {+ if peer.fingerprint == self.identity.fingerprint {+ return false;+ }+ let changed = {+ let mut map = self.peers.lock().unwrap_or_else(|e| e.into_inner());+ map.insert(peer.id, peer.clone()).as_ref() != Some(&peer)+ };+ if changed {+ shared.emit(NodeEvent::LocalSendChanged);+ }+ changed+ }++ pub fn resolve_transfer_approval(&self, id: &str, accepted: bool) -> bool {+ if let Some(tx) = self.pending_approvals.lock().unwrap_or_else(|e| e.into_inner()).remove(id) {+ let _ = tx.send(accepted);+ true+ } else {+ false+ }+ }++ pub fn refresh(&self, shared: &Arc<Shared>) {+ if !self.enabled() {+ return;+ }+ if let Some(running) = self.running.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {+ let _ = running.announce_tx.send(());+ }+ let weak = Arc::downgrade(shared);+ let port = self.configured_port;+ tokio::spawn(async move {+ if let Some(s) = weak.upgrade() {+ let _ = probe_peer(&s, "127.0.0.1", port).await;+ if port != LOCALSEND_DEFAULT_PORT {+ let _ = probe_peer(&s, "127.0.0.1", LOCALSEND_DEFAULT_PORT).await;+ }+ }+ });+ }++ pub fn start(&self, shared: &Arc<Shared>) {+ let mut guard = self.running.lock().unwrap_or_else(|e| e.into_inner());+ if guard.is_some() {+ return;+ }+ let cancel = shared.cancel.child_token();+ let (announce_tx, mut announce_rx) = tokio::sync::mpsc::unbounded_channel::<()>();+ let configured_port = self.configured_port;++ // Bind TCP listener synchronously via std::net::TcpListener or inside tokio task.+ let std_listener = match std::net::TcpListener::bind((Ipv4Addr::UNSPECIFIED, configured_port)) {+ Ok(l) => {+ let _ = l.set_nonblocking(true);+ Some(l)+ }+ Err(e) => {+ tracing::info!(+ port = configured_port,+ error = %e,+ "LocalSend TCP port is in use by another app; Nectarlink will still discover and send to LocalSend peers"+ );+ None+ }+ };++ let (bound_port, receiving) = match &std_listener {+ Some(l) => (l.local_addr().map(|a| a.port()).unwrap_or(configured_port), true),+ None => (configured_port, false),+ };+ *self.bound_port.lock().unwrap_or_else(|e| e.into_inner()) = bound_port;+ self.receiving.store(receiving, Ordering::SeqCst);++ *guard = Some(RunningState { cancel: cancel.clone(), announce_tx: announce_tx.clone() });+ drop(guard);++ // Spawn TCP server if we acquired the port.+ if let Some(std_listener) = std_listener {+ let weak = Arc::downgrade(shared);+ let tcp_cancel = cancel.child_token();+ tokio::spawn(async move {+ let Ok(listener) = TcpListener::from_std(std_listener) else { return };+ loop {+ let accepted = tokio::select! {+ _ = tcp_cancel.cancelled() => return,+ res = listener.accept() => res,+ };+ let Ok((stream, remote_addr)) = accepted else { continue };+ let Some(shared) = weak.upgrade() else { return };+ let conn_cancel = tcp_cancel.child_token();+ tokio::spawn(async move {+ handle_incoming_connection(shared, stream, remote_addr, conn_cancel).await;+ });+ }+ });+ }++ // Spawn UDP multicast discovery + periodic announcer.+ let weak = Arc::downgrade(shared);+ let udp_cancel = cancel.child_token();+ tokio::spawn(async move {+ let udp =+ match bind_multicast_socket(LOCALSEND_DEFAULT_PORT).or_else(|_| bind_multicast_socket(0)) {+ Ok(sock) => Arc::new(sock),+ Err(e) => {+ tracing::warn!(error = %e, "can't bind LocalSend UDP discovery socket");+ return;+ }+ };++ let custom_udp = if bound_port != LOCALSEND_DEFAULT_PORT && bound_port != 0 {+ bind_multicast_socket(bound_port).ok().map(Arc::new)+ } else {+ None+ };++ if let Some(s) = weak.upgrade() {+ send_multicast_announcement(&s, &udp, bound_port, receiving).await;+ let _ = probe_peer(&s, "127.0.0.1", configured_port).await;+ if configured_port != LOCALSEND_DEFAULT_PORT {+ let _ = probe_peer(&s, "127.0.0.1", LOCALSEND_DEFAULT_PORT).await;+ }+ }++ if let Some(custom) = custom_udp {+ let weak_custom = weak.clone();+ let custom_cancel = udp_cancel.child_token();+ tokio::spawn(async move {+ let mut buf = vec![0u8; 8192];+ loop {+ let recv = tokio::select! {+ _ = custom_cancel.cancelled() => return,+ r = custom.recv_from(&mut buf) => r,+ };+ let Ok((n, src)) = recv else { continue };+ let Some(shared) = weak_custom.upgrade() else { return };+ handle_udp_packet(&shared, &buf[..n], src).await;+ }+ });+ }++ let mut buf = vec![0u8; 8192];+ let mut ticker = tokio::time::interval(Duration::from_secs(10));+ ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);+ loop {+ tokio::select! {+ _ = udp_cancel.cancelled() => return,+ _ = ticker.tick() => {+ let Some(shared) = weak.upgrade() else { return };+ send_multicast_announcement(&shared, &udp, bound_port, receiving).await;+ }+ Some(()) = announce_rx.recv() => {+ let Some(shared) = weak.upgrade() else { return };+ send_multicast_announcement(&shared, &udp, bound_port, receiving).await;+ }+ res = udp.recv_from(&mut buf) => {+ let Ok((n, src)) = res else { continue };+ let Some(shared) = weak.upgrade() else { return };+ handle_udp_packet(&shared, &buf[..n], src).await;+ }+ }+ }+ });+ }++ pub fn stop(&self, shared: &Shared) {+ if let Some(running) = self.running.lock().unwrap_or_else(|e| e.into_inner()).take() {+ running.cancel.cancel();+ }+ self.receiving.store(false, Ordering::SeqCst);+ self.peers.lock().unwrap_or_else(|e| e.into_inner()).clear();+ for (_, tx) in self.pending_approvals.lock().unwrap_or_else(|e| e.into_inner()).drain() {+ let _ = tx.send(false);+ }+ for (_, session) in self.sessions.lock().unwrap_or_else(|e| e.into_inner()).drain() {+ let s = session.lock().unwrap_or_else(|e| e.into_inner());+ s.cancel.cancel();+ }+ shared.emit(NodeEvent::LocalSendChanged);+ }+}++fn build_our_multicast_dto(shared: &Shared, port: u16, announce: bool) -> MulticastDto {+ let device: DeviceInfo = shared.local.read().unwrap_or_else(|e| e.into_inner()).device.clone();+ MulticastDto {+ alias: device.name,+ version: LOCALSEND_PROTOCOL_VERSION.into(),+ device_model: device.model.or_else(|| Some("Nectarlink".into())),+ device_type: Some(device_kind_str(device.kind).into()),+ fingerprint: shared.localsend.identity.fingerprint.clone(),+ port,+ protocol: "https".into(),+ download: false,+ announce: Some(announce),+ announcement: Some(announce),+ }+}++fn build_our_info_dto(shared: &Shared) -> InfoDto {+ let device: DeviceInfo = shared.local.read().unwrap_or_else(|e| e.into_inner()).device.clone();+ let port = shared.localsend.bound_port();+ InfoDto {+ alias: device.name,+ version: LOCALSEND_PROTOCOL_VERSION.into(),+ device_model: device.model.or_else(|| Some("Nectarlink".into())),+ device_type: Some(device_kind_str(device.kind).into()),+ fingerprint: shared.localsend.identity.fingerprint.clone(),+ port: Some(port),+ protocol: Some("https".into()),+ download: false,+ }+}++async fn send_multicast_announcement(shared: &Shared, udp: &UdpSocket, bound_port: u16, receiving: bool) {+ let dto = build_our_multicast_dto(shared, bound_port, receiving);+ let Ok(bytes) = serde_json::to_vec(&dto) else { return };+ let target = SocketAddr::V4(SocketAddrV4::new(LOCALSEND_MULTICAST_ADDR, LOCALSEND_DEFAULT_PORT));+ let _ = udp.send_to(&bytes, target).await;+ if bound_port != LOCALSEND_DEFAULT_PORT && bound_port != 0 {+ let custom_target = SocketAddr::V4(SocketAddrV4::new(LOCALSEND_MULTICAST_ADDR, bound_port));+ let _ = udp.send_to(&bytes, custom_target).await;+ let loopback_target = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, bound_port));+ let _ = udp.send_to(&bytes, loopback_target).await;+ }+}++async fn handle_udp_packet(shared: &Arc<Shared>, data: &[u8], src: SocketAddr) {+ let Ok(dto) = serde_json::from_slice::<MulticastDto>(data) else { return };+ if dto.fingerprint == shared.localsend.identity.fingerprint || dto.alias.trim().is_empty() {+ return;+ }+ let ip = match src.ip() {+ IpAddr::V4(v4) => v4.to_string(),+ IpAddr::V6(v6) => v6.to_string(),+ };+ let port = if dto.port == 0 { LOCALSEND_DEFAULT_PORT } else { dto.port };+ let protocol =+ if dto.protocol.eq_ignore_ascii_case("http") { "http".to_owned() } else { "https".to_owned() };+ let peer = LocalSendPeer {+ id: peer_device_id(&dto.fingerprint, &ip, port),+ alias: dto.alias,+ device_model: dto.device_model,+ device_type: dto.device_type.unwrap_or_else(|| "desktop".into()),+ fingerprint: dto.fingerprint,+ ip: ip.clone(),+ port,+ protocol: protocol.clone(),+ };+ shared.localsend.upsert_peer(shared, peer);++ let is_announce = dto.announce.or(dto.announcement).unwrap_or(false);+ if is_announce && shared.localsend.receiving() {+ let shared = shared.clone();+ tokio::spawn(async move {+ let _ = send_register_callback(&shared, &ip, port, &protocol).await;+ });+ }+}++async fn send_register_callback(shared: &Arc<Shared>, ip: &str, port: u16, protocol: &str) -> Result<()> {+ let our_port = shared.localsend.bound_port();+ let dto = build_our_multicast_dto(shared, our_port, false);+ let body = serde_json::to_vec(&dto).map_err(|e| Error::Internal(e.to_string()))?;+ let mut stream = connect_peer_stream(shared, ip, port, protocol == "https").await?;+ write_http_request(+ &mut stream,+ "POST",+ "/api/localsend/v2/register",+ &format!("{ip}:{port}"),+ Some("application/json"),+ &body,+ )+ .await?;+ let resp = read_http_response(&mut stream).await?;+ if (200..300).contains(&resp.status)+ && let Ok(info) = serde_json::from_slice::<InfoDto>(&resp.body)+ && info.fingerprint != shared.localsend.identity.fingerprint+ && !info.alias.trim().is_empty()+ {+ let peer_port = info.port.unwrap_or(port);+ let peer_proto = info+ .protocol+ .as_deref()+ .map(|p| if p.eq_ignore_ascii_case("http") { "http" } else { "https" })+ .unwrap_or(protocol)+ .to_owned();+ let peer = LocalSendPeer {+ id: peer_device_id(&info.fingerprint, ip, peer_port),+ alias: info.alias,+ device_model: info.device_model,+ device_type: info.device_type.unwrap_or_else(|| "desktop".into()),+ fingerprint: info.fingerprint,+ ip: ip.to_owned(),+ port: peer_port,+ protocol: peer_proto,+ };+ shared.localsend.upsert_peer(shared, peer);+ }+ Ok(())+}++/// Probes `host:port` over HTTPS (falling back to HTTP) via `GET /api/localsend/v2/info`+/// (and `POST /api/localsend/v2/register` if we are receiving) and records the peer if found.+pub(crate) async fn probe_peer(shared: &Arc<Shared>, host: &str, port: u16) -> Result<LocalSendPeer> {+ for use_tls in [true, false] {+ let protocol = if use_tls { "https" } else { "http" };+ let Ok(mut stream) = connect_peer_stream(shared, host, port, use_tls).await else {+ continue;+ };+ let req_res = if shared.localsend.receiving() {+ let dto = build_our_multicast_dto(shared, shared.localsend.bound_port(), false);+ let body = serde_json::to_vec(&dto).unwrap_or_default();+ write_http_request(+ &mut stream,+ "POST",+ "/api/localsend/v2/register",+ &format!("{host}:{port}"),+ Some("application/json"),+ &body,+ )+ .await+ } else {+ write_http_request(+ &mut stream,+ "GET",+ "/api/localsend/v2/info",+ &format!("{host}:{port}"),+ None,+ &[],+ )+ .await+ };+ if req_res.is_err() {+ continue;+ }+ let Ok(resp) = read_http_response(&mut stream).await else {+ continue;+ };+ if !(200..300).contains(&resp.status) {+ continue;+ }+ let Ok(info) = serde_json::from_slice::<InfoDto>(&resp.body) else {+ continue;+ };+ if info.fingerprint == shared.localsend.identity.fingerprint || info.alias.trim().is_empty() {+ return Err(Error::NotFound);+ }+ let peer_port = info.port.unwrap_or(port);+ let peer_proto = info+ .protocol+ .as_deref()+ .map(|p| if p.eq_ignore_ascii_case("http") { "http" } else { "https" })+ .unwrap_or(protocol)+ .to_owned();+ let peer = LocalSendPeer {+ id: peer_device_id(&info.fingerprint, host, peer_port),+ alias: info.alias,+ device_model: info.device_model,+ device_type: info.device_type.unwrap_or_else(|| "desktop".into()),+ fingerprint: info.fingerprint,+ ip: host.to_owned(),+ port: peer_port,+ protocol: peer_proto,+ };+ shared.localsend.upsert_peer(shared, peer.clone());+ return Ok(peer);+ }+ Err(Error::Offline)+}++// ---- Server ----++enum ServerStream {+ Plain(TcpStream),+ Tls(Box<tokio_rustls::server::TlsStream<TcpStream>>),+}++impl AsyncRead for ServerStream {+ fn poll_read(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ buf: &mut tokio::io::ReadBuf<'_>,+ ) -> std::task::Poll<std::io::Result<()>> {+ match &mut *self {+ ServerStream::Plain(s) => std::pin::Pin::new(s).poll_read(cx, buf),+ ServerStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_read(cx, buf),+ }+ }+}++impl AsyncWrite for ServerStream {+ fn poll_write(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ buf: &[u8],+ ) -> std::task::Poll<std::io::Result<usize>> {+ match &mut *self {+ ServerStream::Plain(s) => std::pin::Pin::new(s).poll_write(cx, buf),+ ServerStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_write(cx, buf),+ }+ }++ fn poll_flush(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ ) -> std::task::Poll<std::io::Result<()>> {+ match &mut *self {+ ServerStream::Plain(s) => std::pin::Pin::new(s).poll_flush(cx),+ ServerStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_flush(cx),+ }+ }++ fn poll_shutdown(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ ) -> std::task::Poll<std::io::Result<()>> {+ match &mut *self {+ ServerStream::Plain(s) => std::pin::Pin::new(s).poll_shutdown(cx),+ ServerStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_shutdown(cx),+ }+ }+}++async fn handle_incoming_connection(+ shared: Arc<Shared>,+ tcp: TcpStream,+ remote_addr: SocketAddr,+ cancel: CancellationToken,+) {+ let mut first = [0u8; 1];+ let peeked = tokio::time::timeout(Duration::from_secs(5), tcp.peek(&mut first)).await;+ let Ok(Ok(1)) = peeked else { return };++ let mut stream = if first[0] == 0x16 {+ let acceptor = TlsAcceptor::from(shared.localsend.server_tls.clone());+ let Ok(Ok(tls)) = tokio::time::timeout(Duration::from_secs(8), acceptor.accept(tcp)).await else {+ return;+ };+ ServerStream::Tls(Box::new(tls))+ } else {+ ServerStream::Plain(tcp)+ };++ let Ok(head) = read_http_head(&mut stream).await else { return };+ let (path, query) = match head.target.split_once('?') {+ Some((p, q)) => (p, q),+ None => (head.target.as_str(), ""),+ };++ match (head.method.as_str(), path) {+ ("GET", "/api/localsend/v2/info" | "/api/localsend/v1/info") => {+ let info = build_our_info_dto(&shared);+ let body = serde_json::to_vec(&info).unwrap_or_default();+ let _ = write_http_response(&mut stream, 200, "OK", Some("application/json"), &body).await;+ }+ ("POST", "/api/localsend/v2/register" | "/api/localsend/v1/register") => {+ let Ok(body) = read_http_body(&mut stream, &head).await else { return };+ if let Ok(dto) = serde_json::from_slice::<MulticastDto>(&body)+ && dto.fingerprint != shared.localsend.identity.fingerprint+ && !dto.alias.trim().is_empty()+ {+ let ip = remote_addr.ip().to_string();+ let port = if dto.port == 0 { LOCALSEND_DEFAULT_PORT } else { dto.port };+ let protocol = if dto.protocol.eq_ignore_ascii_case("http") {+ "http".to_owned()+ } else {+ "https".to_owned()+ };+ let peer = LocalSendPeer {+ id: peer_device_id(&dto.fingerprint, &ip, port),+ alias: dto.alias,+ device_model: dto.device_model,+ device_type: dto.device_type.unwrap_or_else(|| "desktop".into()),+ fingerprint: dto.fingerprint,+ ip,+ port,+ protocol,+ };+ shared.localsend.upsert_peer(&shared, peer);+ }+ let info = build_our_info_dto(&shared);+ let resp_body = serde_json::to_vec(&info).unwrap_or_default();+ let _ = write_http_response(&mut stream, 200, "OK", Some("application/json"), &resp_body).await;+ }+ ("POST", "/api/localsend/v2/prepare-upload" | "/api/localsend/v1/prepare-upload") => {+ handle_prepare_upload(&shared, &mut stream, &head, remote_addr, &cancel).await;+ }+ ("POST", "/api/localsend/v2/upload" | "/api/localsend/v1/upload") => {+ handle_upload(&shared, &mut stream, &head, query).await;+ }+ ("POST", "/api/localsend/v2/cancel" | "/api/localsend/v1/cancel") => {+ handle_cancel(&shared, &mut stream, query).await;+ }+ _ => {+ let _ = write_http_response(&mut stream, 404, "Not Found", None, &[]).await;+ }+ }+}++async fn handle_prepare_upload(+ shared: &Arc<Shared>,+ stream: &mut ServerStream,+ head: &HttpHead,+ remote_addr: SocketAddr,+ conn_cancel: &CancellationToken,+) {+ let Ok(body) = read_http_body(stream, head).await else { return };+ let Ok(req) = serde_json::from_slice::<PrepareUploadRequestDto>(&body) else {+ let _ = write_http_response(stream, 400, "Bad Request", None, &[]).await;+ return;+ };+ if req.files.is_empty() || req.files.len() > files::MAX_FILES {+ let _ = write_http_response(stream, 400, "Bad Request", None, &[]).await;+ return;+ }++ let ip = remote_addr.ip().to_string();+ let port = req.info.port.unwrap_or(LOCALSEND_DEFAULT_PORT);+ let protocol = req+ .info+ .protocol+ .as_deref()+ .map(|p| if p.eq_ignore_ascii_case("http") { "http" } else { "https" })+ .unwrap_or("https")+ .to_owned();+ let alias = if req.info.alias.trim().is_empty() { ip.clone() } else { req.info.alias.trim().to_owned() };+ let peer_id = peer_device_id(&req.info.fingerprint, &ip, port);+ let peer = LocalSendPeer {+ id: peer_id,+ alias: alias.clone(),+ device_model: req.info.device_model,+ device_type: req.info.device_type.unwrap_or_else(|| "desktop".into()),+ fingerprint: req.info.fingerprint,+ ip,+ port,+ protocol,+ };+ shared.localsend.upsert_peer(shared, peer);++ let session_id = format!("lss_{}", random_token());+ let transfer_id = format!("ls_{}", random_token());+ let staging_dir = shared.incoming_dir().join("localsend").join(&session_id);+ if tokio::fs::create_dir_all(&staging_dir).await.is_err() {+ let _ = write_http_response(stream, 500, "Internal Server Error", None, &[]).await;+ return;+ }++ let mut sorted_files: Vec<FileDto> = req.files.into_values().collect();+ sorted_files.sort_by(|a, b| a.file_name.cmp(&b.file_name).then_with(|| a.id.cmp(&b.id)));++ let mut specs = Vec::with_capacity(sorted_files.len());+ let mut tokens = HashMap::with_capacity(sorted_files.len());+ let mut names: Vec<String> = Vec::new();+ let mut total_bytes: u64 = 0;++ for (idx, dto) in sorted_files.into_iter().enumerate() {+ let (folder, safe_name) = split_localsend_file_name(&dto.file_name);+ if let Some(ref f) = folder {+ let top = f.split('/').next().unwrap_or(f);+ if !names.iter().any(|n| n == top) {+ names.push(top.to_owned());+ }+ } else {+ names.push(safe_name.clone());+ }+ total_bytes = total_bytes.saturating_add(dto.size);+ let token = format!("tok_{}", random_token());+ tokens.insert(dto.id.clone(), token.clone());+ specs.push(IncomingFileSpec {+ file_id: dto.id,+ token,+ safe_name,+ folder,+ size: dto.size,+ part_path: staging_dir.join(format!("{idx}.part")),+ uploaded: false,+ });+ }++ let transfer = Transfer {+ id: transfer_id.clone(),+ device: peer_id,+ direction: Direction::Incoming,+ names,+ files: specs.len(),+ total: total_bytes,+ done: 0,+ state: TransferState::Requested,+ recording: false,+ markers: Vec::new(),+ };++ let cancel = CancellationToken::new();+ shared.register_transfer(&transfer_id, cancel.clone());+ let (approval_tx, approval_rx) = oneshot::channel::<bool>();+ shared+ .localsend+ .pending_approvals+ .lock()+ .unwrap_or_else(|e| e.into_inner())+ .insert(transfer_id.clone(), approval_tx);++ shared.emit(NodeEvent::Transfer(transfer.clone()));++ let accepted = tokio::select! {+ _ = conn_cancel.cancelled() => false,+ _ = cancel.cancelled() => false,+ _ = tokio::time::sleep(APPROVAL_TIMEOUT) => false,+ res = approval_rx => res.unwrap_or(false),+ };++ shared.localsend.pending_approvals.lock().unwrap_or_else(|e| e.into_inner()).remove(&transfer_id);++ if !accepted {+ shared.unregister_transfer(&transfer_id);+ let _ = tokio::fs::remove_dir_all(&staging_dir).await;+ let mut cancelled = transfer;+ cancelled.state = TransferState::Cancelled;+ shared.emit(NodeEvent::Transfer(cancelled));+ let _ = write_http_response(stream, 403, "Forbidden", None, &[]).await;+ return;+ }++ let mut running_transfer = transfer;+ running_transfer.state = TransferState::Running;+ shared.emit(NodeEvent::Transfer(running_transfer.clone()));++ let session = Arc::new(Mutex::new(UploadSession {+ transfer: running_transfer,+ peer_name: alias,+ staging_dir,+ files: specs,+ folder_roots: HashMap::new(),+ saved_items: Vec::new(),+ cancel: cancel.clone(),+ last_progress: Instant::now() - PROGRESS_EVERY,+ }));+ shared.localsend.sessions.lock().unwrap_or_else(|e| e.into_inner()).insert(session_id.clone(), session);++ let resp = PrepareUploadResponseDto { session_id, files: tokens };+ let resp_body = serde_json::to_vec(&resp).unwrap_or_default();+ let _ = write_http_response(stream, 200, "OK", Some("application/json"), &resp_body).await;+}++async fn handle_upload(shared: &Arc<Shared>, stream: &mut ServerStream, head: &HttpHead, query: &str) {+ let params = parse_query(query);+ let (Some(session_id), Some(file_id), Some(token)) =+ (params.get("sessionId").cloned(), params.get("fileId").cloned(), params.get("token").cloned())+ else {+ let _ = write_http_response(stream, 400, "Bad Request", None, &[]).await;+ return;+ };++ let Some(session_arc) =+ shared.localsend.sessions.lock().unwrap_or_else(|e| e.into_inner()).get(&session_id).cloned()+ else {+ let _ = write_http_response(stream, 404, "Not Found", None, &[]).await;+ return;+ };++ enum UploadLookup {+ NotFound,+ Forbidden,+ Found(usize, PathBuf, u64, CancellationToken, String),+ }++ let lookup = {+ let guard = session_arc.lock().unwrap_or_else(|e| e.into_inner());+ match guard.files.iter().enumerate().find(|(_, f)| f.file_id == file_id) {+ None => UploadLookup::NotFound,+ Some((_, spec)) if spec.token != token || spec.uploaded => UploadLookup::Forbidden,+ Some((idx, spec)) => UploadLookup::Found(+ idx,+ spec.part_path.clone(),+ spec.size,+ guard.cancel.clone(),+ guard.transfer.id.clone(),+ ),+ }+ };++ let (spec_idx, part_path, expected_size, cancel, transfer_id) = match lookup {+ UploadLookup::NotFound => {+ let _ = write_http_response(stream, 404, "Not Found", None, &[]).await;+ return;+ }+ UploadLookup::Forbidden => {+ let _ = write_http_response(stream, 403, "Forbidden", None, &[]).await;+ return;+ }+ UploadLookup::Found(idx, p, sz, c, tid) => (idx, p, sz, c, tid),+ };++ let Ok(mut out) = tokio::fs::File::create(&part_path).await else {+ let _ = write_http_response(stream, 500, "Internal Server Error", None, &[]).await;+ return;+ };++ let stream_res = stream_body_to_file(stream, head, &mut out, expected_size, &cancel, |delta| {+ let mut guard = session_arc.lock().unwrap_or_else(|e| e.into_inner());+ guard.transfer.done = guard.transfer.done.saturating_add(delta);+ if guard.last_progress.elapsed() >= PROGRESS_EVERY {+ guard.last_progress = Instant::now();+ shared.emit(NodeEvent::Transfer(guard.transfer.clone()));+ }+ })+ .await;++ let _ = out.flush().await;+ drop(out);++ if let Err(err_state) = stream_res {+ shared.localsend.sessions.lock().unwrap_or_else(|e| e.into_inner()).remove(&session_id);+ shared.unregister_transfer(&transfer_id);+ let staging_dir = {+ let mut guard = session_arc.lock().unwrap_or_else(|e| e.into_inner());+ guard.transfer.state = err_state;+ shared.emit(NodeEvent::Transfer(guard.transfer.clone()));+ guard.staging_dir.clone()+ };+ let _ = tokio::fs::remove_dir_all(&staging_dir).await;+ let _ = write_http_response(stream, 500, "Upload Interrupted", None, &[]).await;+ return;+ }++ // Move completed part file into downloads_dir.+ let finalize_res = finalize_uploaded_file(shared, &session_arc, spec_idx).await;+ if finalize_res.is_err() {+ let _ = write_http_response(stream, 500, "Internal Server Error", None, &[]).await;+ return;+ }++ // Check if all files in this session are complete.+ let finished = {+ let mut guard = session_arc.lock().unwrap_or_else(|e| e.into_inner());+ if guard.files.iter().all(|f| f.uploaded) {+ let saved = guard.saved_items.clone();+ guard.transfer.done = guard.transfer.total;+ guard.transfer.state = TransferState::Done { saved: saved.clone() };+ Some((+ guard.transfer.clone(),+ guard.peer_name.clone(),+ guard.staging_dir.clone(),+ saved,+ guard.files.len(),+ guard.files.iter().any(|f| f.folder.is_some()),+ ))+ } else {+ None+ }+ };++ if let Some((done_transfer, peer_name, staging_dir, saved, file_count, has_folder)) = finished {+ shared.localsend.sessions.lock().unwrap_or_else(|e| e.into_inner()).remove(&session_id);+ shared.unregister_transfer(&transfer_id);+ let _ = tokio::fs::remove_dir_all(&staging_dir).await;+ let detail = if has_folder {+ match file_count {+ 1 => "Folder · 1 file (LocalSend)".into(),+ n => format!("Folder · {n} files (LocalSend)"),+ }+ } else {+ match file_count {+ 1 => "File (LocalSend)".into(),+ n => format!("{n} files (LocalSend)"),+ }+ };+ let target = saved.iter().map(|p| p.to_string_lossy().into_owned()).collect::<Vec<_>>().join("\n");+ let _ = shared.record_timeline(NewTimelineEntry {+ kind: TimelineKind::File,+ device_id: done_transfer.device,+ device_name: peer_name,+ incoming: true,+ timestamp: crate::now_unix(),+ title: transfer_timeline_title(&done_transfer.names),+ detail,+ target,+ size_bytes: done_transfer.total,+ duration_secs: 0,+ ref_id: Some(done_transfer.id.clone()),+ });+ shared.emit(NodeEvent::Transfer(done_transfer));+ }++ let _ = write_http_response(stream, 200, "OK", None, &[]).await;+}++async fn finalize_uploaded_file(+ shared: &Shared,+ session_arc: &Arc<Mutex<UploadSession>>,+ spec_idx: usize,+) -> std::io::Result<()> {+ let downloads_dir = shared.downloads_dir.clone();+ let session_arc = session_arc.clone();+ tokio::task::spawn_blocking(move || {+ fs::create_dir_all(&downloads_dir)?;+ let mut guard = session_arc.lock().unwrap_or_else(|e| e.into_inner());+ let folder = guard.files[spec_idx].folder.clone();+ let safe_name = guard.files[spec_idx].safe_name.clone();+ let part_path = guard.files[spec_idx].part_path.clone();++ let target_dir = match folder.as_deref() {+ None => downloads_dir.clone(),+ Some(f) => {+ let mut segments = f.split('/').map(storable_name);+ let top = segments.next().unwrap_or_else(|| "Folder".into());+ let mut at = match guard.folder_roots.get(&top) {+ Some(existing) => existing.clone(),+ None => {+ let root = unique_path(&downloads_dir, &top);+ fs::create_dir_all(&root)?;+ guard.folder_roots.insert(top, root.clone());+ guard.saved_items.push(root.clone());+ root+ }+ };+ at.extend(segments);+ fs::create_dir_all(&at)?;+ at+ }+ };++ let dest = unique_path(&target_dir, &storable_name(&safe_name));+ if fs::rename(&part_path, &dest).is_err() {+ fs::copy(&part_path, &dest)?;+ let _ = fs::remove_file(&part_path);+ }+ if folder.is_none() {+ guard.saved_items.push(dest);+ }+ guard.files[spec_idx].uploaded = true;+ Ok(())+ })+ .await+ .map_err(std::io::Error::other)?+}++async fn handle_cancel(shared: &Arc<Shared>, stream: &mut ServerStream, query: &str) {+ let params = parse_query(query);+ let removed = params.get("sessionId").and_then(|session_id| {+ shared.localsend.sessions.lock().unwrap_or_else(|e| e.into_inner()).remove(session_id)+ });+ if let Some(session_arc) = removed {+ let (transfer_id, staging_dir, mut transfer) = {+ let guard = session_arc.lock().unwrap_or_else(|e| e.into_inner());+ guard.cancel.cancel();+ (guard.transfer.id.clone(), guard.staging_dir.clone(), guard.transfer.clone())+ };+ shared.unregister_transfer(&transfer_id);+ let _ = tokio::fs::remove_dir_all(&staging_dir).await;+ transfer.state = TransferState::Cancelled;+ shared.emit(NodeEvent::Transfer(transfer));+ }+ let _ = write_http_response(stream, 200, "OK", None, &[]).await;+}++// ---- Client (Outgoing file transfer to a LocalSendPeer) ----++struct OutgoingLocalSendFile {+ file_id: String,+ file_name: String,+ folder: Option<String>,+ size: u64,+ file: tokio::fs::File,+}++pub(crate) async fn send_files(+ shared: &Arc<Shared>,+ peer_id: DeviceId,+ files: Vec<OutgoingFile>,+) -> Result<String> {+ let Some(peer) = shared.localsend.peer(&peer_id) else {+ return Err(Error::NotPaired);+ };+ let mut opened = Vec::with_capacity(files.len());+ let mut source_targets: Vec<String> = Vec::new();+ let mut names: Vec<String> = Vec::new();+ let mut total_bytes: u64 = 0;++ for (idx, file) in files.into_iter().enumerate() {+ let handle = match file.source {+ FileSource::Path(path) => {+ let target = outgoing_target_path(&path, file.folder.as_deref());+ if !source_targets.contains(&target) {+ source_targets.push(target);+ }+ std::fs::File::open(path)?+ }+ FileSource::File(handle) => handle,+ };+ let size = handle.metadata()?.len();+ let clean_name = safe_file_name(&file.name);+ let wire_name = match file.folder.as_deref() {+ Some(folder) => {+ let top = folder.split('/').next().unwrap_or(folder);+ if !names.iter().any(|n| n == top) {+ names.push(top.to_owned());+ }+ format!("{folder}/{clean_name}")+ }+ None => {+ names.push(clean_name.clone());+ clean_name.clone()+ }+ };+ total_bytes = total_bytes.saturating_add(size);+ opened.push(OutgoingLocalSendFile {+ file_id: format!("f_{idx}"),+ file_name: wire_name,+ folder: file.folder,+ size,+ file: tokio::fs::File::from_std(handle),+ });+ }++ let transfer_id = format!("ls_{}", random_token());+ let transfer = Transfer {+ id: transfer_id.clone(),+ device: peer_id,+ direction: Direction::Outgoing,+ names,+ files: opened.len(),+ total: total_bytes,+ done: 0,+ state: TransferState::Waiting,+ recording: false,+ markers: Vec::new(),+ };++ let cancel = CancellationToken::new();+ shared.register_transfer(&transfer_id, cancel.clone());+ shared.emit(NodeEvent::Transfer(transfer.clone()));++ let shared_clone = shared.clone();+ tokio::spawn(async move {+ let has_folder = opened.iter().any(|f| f.folder.is_some());+ let file_count = opened.len();+ let tl_target = source_targets.join("\n");+ let outcome = run_outgoing_transfer(&shared_clone, &peer, opened, &transfer, &cancel).await;+ let mut final_transfer = transfer;+ match outcome {+ Ok(()) => {+ final_transfer.done = final_transfer.total;+ final_transfer.state = TransferState::Done { saved: Vec::new() };+ let detail = if has_folder {+ match file_count {+ 1 => "Folder · 1 file (LocalSend)".into(),+ n => format!("Folder · {n} files (LocalSend)"),+ }+ } else {+ match file_count {+ 1 => "File (LocalSend)".into(),+ n => format!("{n} files (LocalSend)"),+ }+ };+ let _ = shared_clone.record_timeline(NewTimelineEntry {+ kind: TimelineKind::File,+ device_id: peer.id,+ device_name: peer.alias.clone(),+ incoming: false,+ timestamp: crate::now_unix(),+ title: transfer_timeline_title(&final_transfer.names),+ detail,+ target: tl_target,+ size_bytes: final_transfer.total,+ duration_secs: 0,+ ref_id: Some(final_transfer.id.clone()),+ });+ }+ Err(state) => {+ final_transfer.state = state;+ }+ }+ shared_clone.emit(NodeEvent::Transfer(final_transfer.clone()));+ shared_clone.unregister_transfer(&final_transfer.id);+ });++ Ok(transfer_id)+}++async fn run_outgoing_transfer(+ shared: &Arc<Shared>,+ peer: &LocalSendPeer,+ mut files: Vec<OutgoingLocalSendFile>,+ initial_transfer: &Transfer,+ cancel: &CancellationToken,+) -> std::result::Result<(), TransferState> {+ let use_tls = !peer.protocol.eq_ignore_ascii_case("http");+ let mut files_map = HashMap::with_capacity(files.len());+ for f in &files {+ files_map.insert(+ f.file_id.clone(),+ FileDto {+ id: f.file_id.clone(),+ file_name: f.file_name.clone(),+ size: f.size,+ file_type: guess_mime(&f.file_name).into(),+ sha256: None,+ preview: None,+ },+ );+ }+ let prepare_req = PrepareUploadRequestDto { info: build_our_info_dto(shared), files: files_map };+ let prepare_body = serde_json::to_vec(&prepare_req)+ .map_err(|e| TransferState::Failed(TransferFailure::Other(e.to_string())))?;++ let mut prep_stream = tokio::select! {+ _ = cancel.cancelled() => return Err(TransferState::Cancelled),+ res = connect_peer_stream(shared, &peer.ip, peer.port, use_tls) => {+ res.map_err(|_| TransferState::Failed(TransferFailure::Unreachable))?+ }+ };++ let host_header = format!("{}:{}", peer.ip, peer.port);+ if write_http_request(+ &mut prep_stream,+ "POST",+ "/api/localsend/v2/prepare-upload",+ &host_header,+ Some("application/json"),+ &prepare_body,+ )+ .await+ .is_err()+ {+ return Err(TransferState::Failed(TransferFailure::Unreachable));+ }++ let prep_resp = tokio::select! {+ _ = cancel.cancelled() => return Err(TransferState::Cancelled),+ res = tokio::time::timeout(APPROVAL_TIMEOUT, read_http_response(&mut prep_stream)) => {+ match res {+ Ok(Ok(r)) => r,+ _ => return Err(TransferState::Failed(TransferFailure::Denied)),+ }+ }+ };+ drop(prep_stream);++ if prep_resp.status == 204 {+ return Ok(());+ }+ if prep_resp.status == 403 || prep_resp.status == 401 {+ return Err(TransferState::Failed(TransferFailure::Denied));+ }+ if !(200..300).contains(&prep_resp.status) {+ return Err(TransferState::Failed(TransferFailure::Other(format!(+ "LocalSend prepare-upload status {}",+ prep_resp.status+ ))));+ }++ let prep: PrepareUploadResponseDto = serde_json::from_slice(&prep_resp.body).map_err(|_| {+ TransferState::Failed(TransferFailure::Other("invalid prepare-upload response".into()))+ })?;++ let mut progress_transfer = initial_transfer.clone();+ progress_transfer.state = TransferState::Running;+ shared.emit(NodeEvent::Transfer(progress_transfer.clone()));+ let mut last_emit = Instant::now();++ for f in &mut files {+ let Some(token) = prep.files.get(&f.file_id) else {+ continue;+ };+ let mut up_stream = tokio::select! {+ _ = cancel.cancelled() => {+ let _ = send_cancel_request(shared, peer, use_tls, &prep.session_id).await;+ return Err(TransferState::Cancelled);+ }+ res = connect_peer_stream(shared, &peer.ip, peer.port, use_tls) => {+ res.map_err(|_| TransferState::Failed(TransferFailure::Unreachable))?+ }+ };++ let path = format!(+ "/api/localsend/v2/upload?sessionId={}&fileId={}&token={}",+ crate::percent_encode(&prep.session_id),+ crate::percent_encode(&f.file_id),+ crate::percent_encode(token),+ );+ let head = format!(+ "POST {path} HTTP/1.1\r\nHost: {host_header}\r\nContent-Type: application/octet-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",+ f.size+ );+ if up_stream.write_all(head.as_bytes()).await.is_err() {+ return Err(TransferState::Failed(TransferFailure::Other("upload header write failed".into())));+ }++ let mut remaining = f.size;+ let mut buf = vec![0u8; UPLOAD_CHUNK];+ while remaining > 0 {+ let to_read = UPLOAD_CHUNK.min(remaining as usize);+ let n = tokio::select! {+ _ = cancel.cancelled() => {+ let _ = send_cancel_request(shared, peer, use_tls, &prep.session_id).await;+ return Err(TransferState::Cancelled);+ }+ r = f.file.read(&mut buf[..to_read]) => {+ r.map_err(|e| TransferState::Failed(TransferFailure::Other(e.to_string())))?+ }+ };+ if n == 0 {+ return Err(TransferState::Failed(TransferFailure::Other("file ended early".into())));+ }+ let write_res = tokio::select! {+ _ = cancel.cancelled() => {+ let _ = send_cancel_request(shared, peer, use_tls, &prep.session_id).await;+ return Err(TransferState::Cancelled);+ }+ w = up_stream.write_all(&buf[..n]) => w,+ };+ if write_res.is_err() {+ return Err(TransferState::Failed(TransferFailure::Other("upload write failed".into())));+ }+ remaining -= n as u64;+ progress_transfer.done = progress_transfer.done.saturating_add(n as u64);+ if last_emit.elapsed() >= PROGRESS_EVERY {+ last_emit = Instant::now();+ shared.emit(NodeEvent::Transfer(progress_transfer.clone()));+ }+ }+ let _ = up_stream.flush().await;+ let up_resp = tokio::select! {+ _ = cancel.cancelled() => {+ let _ = send_cancel_request(shared, peer, use_tls, &prep.session_id).await;+ return Err(TransferState::Cancelled);+ }+ r = tokio::time::timeout(IO_TIMEOUT, read_http_response(&mut up_stream)) => {+ match r {+ Ok(Ok(resp)) => resp,+ _ => return Err(TransferState::Failed(TransferFailure::Other("upload response timed out".into()))),+ }+ }+ };+ if !(200..300).contains(&up_resp.status) {+ return Err(TransferState::Failed(TransferFailure::Other(format!(+ "upload failed with HTTP {}",+ up_resp.status+ ))));+ }+ }++ Ok(())+}++async fn send_cancel_request(shared: &Arc<Shared>, peer: &LocalSendPeer, use_tls: bool, session_id: &str) {+ let Ok(mut stream) = connect_peer_stream(shared, &peer.ip, peer.port, use_tls).await else {+ return;+ };+ let path = format!("/api/localsend/v2/cancel?sessionId={}", crate::percent_encode(session_id));+ let _ = write_http_request(&mut stream, "POST", &path, &format!("{}:{}", peer.ip, peer.port), None, &[])+ .await;+}++enum ClientStream {+ Plain(TcpStream),+ Tls(Box<tokio_rustls::client::TlsStream<TcpStream>>),+}++impl AsyncRead for ClientStream {+ fn poll_read(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ buf: &mut tokio::io::ReadBuf<'_>,+ ) -> std::task::Poll<std::io::Result<()>> {+ match &mut *self {+ ClientStream::Plain(s) => std::pin::Pin::new(s).poll_read(cx, buf),+ ClientStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_read(cx, buf),+ }+ }+}++impl AsyncWrite for ClientStream {+ fn poll_write(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ buf: &[u8],+ ) -> std::task::Poll<std::io::Result<usize>> {+ match &mut *self {+ ClientStream::Plain(s) => std::pin::Pin::new(s).poll_write(cx, buf),+ ClientStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_write(cx, buf),+ }+ }++ fn poll_flush(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ ) -> std::task::Poll<std::io::Result<()>> {+ match &mut *self {+ ClientStream::Plain(s) => std::pin::Pin::new(s).poll_flush(cx),+ ClientStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_flush(cx),+ }+ }++ fn poll_shutdown(+ mut self: std::pin::Pin<&mut Self>,+ cx: &mut std::task::Context<'_>,+ ) -> std::task::Poll<std::io::Result<()>> {+ match &mut *self {+ ClientStream::Plain(s) => std::pin::Pin::new(s).poll_shutdown(cx),+ ClientStream::Tls(s) => std::pin::Pin::new(&mut **s).poll_shutdown(cx),+ }+ }+}++async fn connect_peer_stream(shared: &Shared, host: &str, port: u16, use_tls: bool) -> Result<ClientStream> {+ let addr = format!("{host}:{port}");+ let tcp = tokio::time::timeout(CONNECT_TIMEOUT, TcpStream::connect(&addr))+ .await+ .map_err(|_| Error::Offline)?+ .map_err(|_| Error::Offline)?;+ if use_tls {+ let connector = TlsConnector::from(shared.localsend.client_tls.clone());+ let server_name = ServerName::try_from("localsend.org").expect("static valid DNS name");+ let tls = tokio::time::timeout(CONNECT_TIMEOUT, connector.connect(server_name, tcp))+ .await+ .map_err(|_| Error::Offline)?+ .map_err(|_| Error::Offline)?;+ Ok(ClientStream::Tls(Box::new(tls)))+ } else {+ Ok(ClientStream::Plain(tcp))+ }+}++// ---- Minimal HTTP/1.1 reader/writer ----++struct HttpHead {+ method: String,+ target: String,+ content_length: Option<u64>,+ chunked: bool,+ prefetched_body: Vec<u8>,+}++struct HttpResponse {+ status: u16,+ body: Vec<u8>,+}++async fn read_http_head<S: AsyncRead + Unpin>(stream: &mut S) -> std::io::Result<HttpHead> {+ let mut buf = Vec::with_capacity(2048);+ let mut temp = [0u8; 2048];+ let header_end = loop {+ if let Some(pos) = find_header_end(&buf) {+ break pos;+ }+ if buf.len() >= MAX_HEADER_BYTES {+ return Err(std::io::Error::new(ErrorKind::InvalidData, "HTTP header too large"));+ }+ let n = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut temp))+ .await+ .map_err(|_| std::io::Error::new(ErrorKind::TimedOut, "HTTP header read timed out"))??;+ if n == 0 {+ return Err(std::io::Error::new(ErrorKind::UnexpectedEof, "EOF reading HTTP header"));+ }+ buf.extend_from_slice(&temp[..n]);+ };++ let prefetched_body = buf.split_off(header_end + 4);+ let header_str = String::from_utf8_lossy(&buf[..header_end]);+ let mut lines = header_str.split("\r\n");+ let first_line = lines.next().unwrap_or("");+ let mut parts = first_line.split_whitespace();+ let method = parts.next().unwrap_or("").to_uppercase();+ let target = parts.next().unwrap_or("/").to_owned();++ let mut content_length = None;+ let mut chunked = false;+ for line in lines {+ if let Some((k, v)) = line.split_once(':') {+ let k_lower = k.trim().to_ascii_lowercase();+ let v_trim = v.trim();+ if k_lower == "content-length" {+ content_length = v_trim.parse::<u64>().ok();+ } else if k_lower == "transfer-encoding" && v_trim.to_ascii_lowercase().contains("chunked") {+ chunked = true;+ }+ }+ }++ Ok(HttpHead { method, target, content_length, chunked, prefetched_body })+}++async fn read_http_body<S: AsyncRead + Unpin>(stream: &mut S, head: &HttpHead) -> std::io::Result<Vec<u8>> {+ if head.chunked {+ return read_chunked_vec(stream, &head.prefetched_body, MAX_JSON_BODY_BYTES).await;+ }+ let expected = head.content_length.unwrap_or(0) as usize;+ if expected > MAX_JSON_BODY_BYTES {+ return Err(std::io::Error::new(ErrorKind::InvalidData, "HTTP JSON body too large"));+ }+ let mut out = Vec::with_capacity(expected);+ let take_pre = head.prefetched_body.len().min(expected);+ out.extend_from_slice(&head.prefetched_body[..take_pre]);+ while out.len() < expected {+ let mut chunk = vec![0u8; (expected - out.len()).min(16 * 1024)];+ let n = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut chunk))+ .await+ .map_err(|_| std::io::Error::new(ErrorKind::TimedOut, "HTTP body read timed out"))??;+ if n == 0 {+ break;+ }+ out.extend_from_slice(&chunk[..n]);+ }+ Ok(out)+}++async fn read_chunked_vec<S: AsyncRead + Unpin>(+ stream: &mut S,+ prefetched: &[u8],+ max_bytes: usize,+) -> std::io::Result<Vec<u8>> {+ let mut buf = prefetched.to_vec();+ let mut out = Vec::new();+ loop {+ while find_crlf(&buf).is_none() {+ let mut tmp = [0u8; 4096];+ let n = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut tmp))+ .await+ .map_err(|_| std::io::Error::new(ErrorKind::TimedOut, "chunk header timeout"))??;+ if n == 0 {+ return Ok(out);+ }+ buf.extend_from_slice(&tmp[..n]);+ }+ let line_end = find_crlf(&buf).unwrap();+ let size_line = String::from_utf8_lossy(&buf[..line_end]);+ let hex_part = size_line.split(';').next().unwrap_or("").trim();+ let chunk_size = usize::from_str_radix(hex_part, 16)+ .map_err(|_| std::io::Error::new(ErrorKind::InvalidData, "invalid chunk size"))?;+ buf.drain(..line_end + 2);+ if chunk_size == 0 {+ break;+ }+ if out.len().saturating_add(chunk_size) > max_bytes {+ return Err(std::io::Error::new(ErrorKind::InvalidData, "chunked body too large"));+ }+ while buf.len() < chunk_size + 2 {+ let mut tmp = [0u8; 8192];+ let n = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut tmp))+ .await+ .map_err(|_| std::io::Error::new(ErrorKind::TimedOut, "chunk data timeout"))??;+ if n == 0 {+ break;+ }+ buf.extend_from_slice(&tmp[..n]);+ }+ let available = buf.len().min(chunk_size);+ out.extend_from_slice(&buf[..available]);+ let drain_len = buf.len().min(chunk_size + 2);+ buf.drain(..drain_len);+ }+ Ok(out)+}++async fn stream_body_to_file<S, F>(+ stream: &mut S,+ head: &HttpHead,+ out: &mut tokio::fs::File,+ expected_size: u64,+ cancel: &CancellationToken,+ mut on_progress: F,+) -> std::result::Result<(), TransferState>+where+ S: AsyncRead + Unpin,+ F: FnMut(u64),+{+ if head.chunked {+ let mut buf = head.prefetched_body.clone();+ loop {+ while find_crlf(&buf).is_none() {+ let mut tmp = [0u8; 8192];+ let n = tokio::select! {+ _ = cancel.cancelled() => return Err(TransferState::Cancelled),+ r = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut tmp)) => {+ match r {+ Ok(Ok(n)) => n,+ _ => return Err(TransferState::Failed(TransferFailure::Other("upload read error".into()))),+ }+ }+ };+ if n == 0 {+ return Ok(());+ }+ buf.extend_from_slice(&tmp[..n]);+ }+ let line_end = find_crlf(&buf).unwrap();+ let size_line = String::from_utf8_lossy(&buf[..line_end]).into_owned();+ let hex_part = size_line.split(';').next().unwrap_or("").trim();+ let Ok(chunk_size) = usize::from_str_radix(hex_part, 16) else {+ return Err(TransferState::Failed(TransferFailure::Other("invalid chunk size".into())));+ };+ buf.drain(..line_end + 2);+ if chunk_size == 0 {+ return Ok(());+ }+ let mut left_in_chunk = chunk_size;+ while left_in_chunk > 0 {+ if !buf.is_empty() {+ let take = buf.len().min(left_in_chunk);+ out.write_all(&buf[..take])+ .await+ .map_err(|e| TransferState::Failed(TransferFailure::Other(e.to_string())))?;+ buf.drain(..take);+ left_in_chunk -= take;+ on_progress(take as u64);+ } else {+ let mut tmp = vec![0u8; UPLOAD_CHUNK.min(left_in_chunk)];+ let n = tokio::select! {+ _ = cancel.cancelled() => return Err(TransferState::Cancelled),+ r = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut tmp)) => {+ match r {+ Ok(Ok(n)) => n,+ _ => return Err(TransferState::Failed(TransferFailure::Other("upload read error".into()))),+ }+ }+ };+ if n == 0 {+ return Err(TransferState::Failed(TransferFailure::Other("unexpected EOF".into())));+ }+ out.write_all(&tmp[..n])+ .await+ .map_err(|e| TransferState::Failed(TransferFailure::Other(e.to_string())))?;+ left_in_chunk -= n;+ on_progress(n as u64);+ }+ }+ // Consume trailing \r\n after chunk data.+ while buf.len() < 2 {+ let mut tmp = [0u8; 16];+ let Ok(Ok(n)) = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut tmp)).await else {+ break;+ };+ if n == 0 {+ break;+ }+ buf.extend_from_slice(&tmp[..n]);+ }+ let drain_crlf = buf.len().min(2);+ buf.drain(..drain_crlf);+ }+ } else {+ let total = head.content_length.unwrap_or(expected_size);+ let mut remaining = total;+ if !head.prefetched_body.is_empty() {+ let take = (head.prefetched_body.len() as u64).min(remaining) as usize;+ out.write_all(&head.prefetched_body[..take])+ .await+ .map_err(|e| TransferState::Failed(TransferFailure::Other(e.to_string())))?;+ remaining -= take as u64;+ on_progress(take as u64);+ }+ let mut buf = vec![0u8; UPLOAD_CHUNK];+ while remaining > 0 {+ let max_read = UPLOAD_CHUNK.min(remaining as usize);+ let n = tokio::select! {+ _ = cancel.cancelled() => return Err(TransferState::Cancelled),+ r = tokio::time::timeout(IO_TIMEOUT, stream.read(&mut buf[..max_read])) => {+ match r {+ Ok(Ok(n)) => n,+ _ => return Err(TransferState::Failed(TransferFailure::Other("upload stream interrupted".into()))),+ }+ }+ };+ if n == 0 {+ return Err(TransferState::Failed(TransferFailure::Other(+ "upload stream ended early".into(),+ )));+ }+ out.write_all(&buf[..n])+ .await+ .map_err(|e| TransferState::Failed(TransferFailure::Other(e.to_string())))?;+ remaining -= n as u64;+ on_progress(n as u64);+ }+ Ok(())+ }+}++async fn write_http_request<S: AsyncWrite + Unpin>(+ stream: &mut S,+ method: &str,+ path: &str,+ host: &str,+ content_type: Option<&str>,+ body: &[u8],+) -> Result<()> {+ let ct_header = content_type.map(|ct| format!("Content-Type: {ct}\r\n")).unwrap_or_default();+ let req = format!(+ "{method} {path} HTTP/1.1\r\nHost: {host}\r\nUser-Agent: Nectarlink/0.1\r\n{ct_header}Content-Length: {}\r\nConnection: close\r\n\r\n",+ body.len()+ );+ stream.write_all(req.as_bytes()).await.map_err(|_| Error::Offline)?;+ if !body.is_empty() {+ stream.write_all(body).await.map_err(|_| Error::Offline)?;+ }+ stream.flush().await.map_err(|_| Error::Offline)?;+ Ok(())+}++async fn write_http_response<S: AsyncWrite + Unpin>(+ stream: &mut S,+ status: u16,+ reason: &str,+ content_type: Option<&str>,+ body: &[u8],+) -> std::io::Result<()> {+ let ct_header = content_type.map(|ct| format!("Content-Type: {ct}\r\n")).unwrap_or_default();+ let head = format!(+ "HTTP/1.1 {status} {reason}\r\n{ct_header}Content-Length: {}\r\nConnection: close\r\n\r\n",+ body.len()+ );+ stream.write_all(head.as_bytes()).await?;+ if !body.is_empty() {+ stream.write_all(body).await?;+ }+ stream.flush().await+}++async fn read_http_response<S: AsyncRead + Unpin>(stream: &mut S) -> std::io::Result<HttpResponse> {+ let head = read_http_head(stream).await?;+ // For HTTP responses, `head.target` holds the status code (e.g. "200" in "HTTP/1.1 200 OK").+ let status = head.target.parse::<u16>().unwrap_or(500);+ let body = read_http_body(stream, &head).await?;+ Ok(HttpResponse { status, body })+}++fn find_header_end(buf: &[u8]) -> Option<usize> {+ buf.windows(4).position(|w| w == b"\r\n\r\n")+}++fn find_crlf(buf: &[u8]) -> Option<usize> {+ buf.windows(2).position(|w| w == b"\r\n")+}++fn parse_query(query: &str) -> HashMap<String, String> {+ url::form_urlencoded::parse(query.as_bytes()).into_owned().collect()+}++fn split_localsend_file_name(raw: &str) -> (Option<String>, String) {+ let normalized = raw.replace('\\', "/");+ let parts: Vec<&str> =+ normalized.split('/').map(str::trim).filter(|s| !s.is_empty() && *s != "." && *s != "..").collect();+ match parts.as_slice() {+ [] => (None, "file".into()),+ [single] => (None, safe_file_name(single)),+ [folders @ .., last] => {+ let folder = folders.iter().map(|s| storable_name(s)).collect::<Vec<_>>().join("/");+ (Some(folder), safe_file_name(last))+ }+ }+}++fn guess_mime(name: &str) -> &'static str {+ let lower = name.to_ascii_lowercase();+ if lower.ends_with(".jpg") || lower.ends_with(".jpeg") {+ "image/jpeg"+ } else if lower.ends_with(".png") {+ "image/png"+ } else if lower.ends_with(".pdf") {+ "application/pdf"+ } else if lower.ends_with(".txt") || lower.ends_with(".md") {+ "text/plain"+ } else if lower.ends_with(".mp4") {+ "video/mp4"+ } else if lower.ends_with(".mp3") {+ "audio/mpeg"+ } else {+ "application/octet-stream"+ }+}++fn random_token() -> String {+ const ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz0123456789";+ (0..16).map(|_| ALPHABET[rand::random_range(0..ALPHABET.len())] as char).collect()+}++// ---- Multicast UDP socket setup ----++fn bind_multicast_socket(port: u16) -> std::io::Result<UdpSocket> {+ let sock = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?;+ let _ = sock.set_reuse_address(true);+ let _ = sock.set_broadcast(true);+ let _ = sock.set_multicast_loop_v4(true);+ let _ = sock.set_multicast_ttl_v4(255);+ sock.set_nonblocking(true)?;+ let bind_addr = SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port);+ sock.bind(&bind_addr.into())?;+ let _ = sock.join_multicast_v4(&LOCALSEND_MULTICAST_ADDR, &Ipv4Addr::UNSPECIFIED);+ for ip in local_ipv4_addrs() {+ let _ = sock.join_multicast_v4(&LOCALSEND_MULTICAST_ADDR, &ip);+ }+ let std_udp: std::net::UdpSocket = sock.into();+ UdpSocket::from_std(std_udp)+}++fn local_ipv4_addrs() -> Vec<Ipv4Addr> {+ let mut out = vec![Ipv4Addr::LOCALHOST];+ if let Ok(sock) = std::net::UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))+ && sock.connect((Ipv4Addr::new(8, 8, 8, 8), 80)).is_ok()+ && let Ok(SocketAddr::V4(local)) = sock.local_addr()+ && !out.contains(local.ip())+ {+ out.push(*local.ip());+ }+ out+}++// ---- Self-signed X.509 v3 DER certificate & TLS configuration ----++fn decode_stored_identity(stored: &StoredLocalSendState) -> Option<LocalSendIdentity> {+ if stored.cert_der_hex.is_empty() || stored.key_pkcs8_hex.is_empty() {+ return None;+ }+ let cert_der = data_encoding::HEXLOWER.decode(stored.cert_der_hex.as_bytes()).ok()?;+ let key_pkcs8 = data_encoding::HEXLOWER.decode(stored.key_pkcs8_hex.as_bytes()).ok()?;+ if cert_der.is_empty() || key_pkcs8.is_empty() {+ return None;+ }+ let fingerprint = data_encoding::HEXLOWER.encode(&Sha256::digest(&cert_der));+ let id = LocalSendIdentity { cert_der, key_pkcs8, fingerprint };+ // Verify the stored key/cert builds a valid rustls ServerConfig.+ build_server_tls_config(&id).ok()?;+ Some(id)+}++fn save_stored_state(+ data_dir: &Path,+ protector: &dyn KeyProtector,+ state: &StoredLocalSendState,+) -> Result<()> {+ fs::create_dir_all(data_dir)?;+ let plain = serde_json::to_vec(state).map_err(|e| Error::Internal(e.to_string()))?;+ let sealed = protector.protect(&plain)?;+ let path = data_dir.join(STATE_FILE);+ let tmp = path.with_extension("enc.tmp");+ fs::write(&tmp, &sealed)?;+ fs::rename(&tmp, &path)?;+ Ok(())+}++/// Generates an ECDSA P-256 PKCS#8 keypair and a self-signed X.509 v3 DER certificate+/// (`CN=LocalSend User`) using `ring`, returning the DER bytes and SHA-256 hex fingerprint.+pub(crate) fn generate_self_signed_identity() -> Result<LocalSendIdentity> {+ let rng = SystemRandom::new();+ let pkcs8_doc = EcdsaKeyPair::generate_pkcs8(&ECDSA_P256_SHA256_ASN1_SIGNING, &rng)+ .map_err(|_| Error::Internal("can't generate LocalSend ECDSA key".into()))?;+ let key_pkcs8 = pkcs8_doc.as_ref().to_vec();+ let key_pair = EcdsaKeyPair::from_pkcs8(&ECDSA_P256_SHA256_ASN1_SIGNING, &key_pkcs8, &rng)+ .map_err(|_| Error::Internal("can't parse generated LocalSend ECDSA key".into()))?;++ let mut serial = [0u8; 16];+ rng.fill(&mut serial).map_err(|_| Error::Internal("RNG failure".into()))?;+ serial[0] &= 0x7F;+ if serial[0] == 0 {+ serial[0] = 1;+ }++ // ecdsa-with-SHA256 (1.2.840.10045.4.3.2)+ let sig_alg = der_seq(&[der_tlv(0x06, &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02])]);+ // CN = LocalSend User (2.5.4.3)+ let cn_attr = der_seq(&[der_tlv(0x06, &[0x55, 0x04, 0x03]), der_tlv(0x0C, b"LocalSend User")]);+ let name = der_seq(&[der_tlv(0x31, &cn_attr)]);+ // Validity: 2024-01-01 to 2036-01-01+ let validity = der_seq(&[der_tlv(0x17, b"240101000000Z"), der_tlv(0x17, b"360101000000Z")]);+ // SubjectPublicKeyInfo: id-ecPublicKey (1.2.840.10045.2.1) + prime256v1 (1.2.840.10045.3.1.7)+ let spki_alg = der_seq(&[+ der_tlv(0x06, &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01]),+ der_tlv(0x06, &[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07]),+ ]);+ let spki = der_seq(&[spki_alg, der_bit_string(key_pair.public_key().as_ref())]);++ let version = der_tlv(0xA0, &der_tlv(0x02, &[0x02]));+ let tbs =+ der_seq(&[version, der_tlv(0x02, &serial), sig_alg.clone(), name.clone(), validity, name, spki]);++ let signature =+ key_pair.sign(&rng, &tbs).map_err(|_| Error::Internal("can't sign LocalSend certificate".into()))?;+ let cert_der = der_seq(&[tbs, sig_alg, der_bit_string(signature.as_ref())]);+ let fingerprint = data_encoding::HEXLOWER.encode(&Sha256::digest(&cert_der));++ Ok(LocalSendIdentity { cert_der, key_pkcs8, fingerprint })+}++fn der_tlv(tag: u8, body: &[u8]) -> Vec<u8> {+ let len = body.len();+ let mut out = Vec::with_capacity(4 + len);+ out.push(tag);+ if len < 128 {+ out.push(len as u8);+ } else if len <= 0xFF {+ out.push(0x81);+ out.push(len as u8);+ } else {+ out.push(0x82);+ out.push((len >> 8) as u8);+ out.push((len & 0xFF) as u8);+ }+ out.extend_from_slice(body);+ out+}++fn der_seq(items: &[Vec<u8>]) -> Vec<u8> {+ let total: usize = items.iter().map(Vec::len).sum();+ let mut body = Vec::with_capacity(total);+ for item in items {+ body.extend_from_slice(item);+ }+ der_tlv(0x30, &body)+}++fn der_bit_string(bytes: &[u8]) -> Vec<u8> {+ let mut body = Vec::with_capacity(1 + bytes.len());+ body.push(0x00); // 0 unused bits+ body.extend_from_slice(bytes);+ der_tlv(0x03, &body)+}++fn build_server_tls_config(identity: &LocalSendIdentity) -> Result<ServerConfig> {+ let provider = Arc::new(rustls::crypto::ring::default_provider());+ let cert = CertificateDer::from(identity.cert_der.clone());+ let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(identity.key_pkcs8.clone()));+ ServerConfig::builder_with_provider(provider)+ .with_safe_default_protocol_versions()+ .map_err(|e| Error::Internal(e.to_string()))?+ .with_no_client_auth()+ .with_single_cert(vec![cert], key)+ .map_err(|e| Error::Internal(e.to_string()))+}++fn build_client_tls_config() -> Result<ClientConfig> {+ let provider = Arc::new(rustls::crypto::ring::default_provider());+ let verifier = Arc::new(LocalSendCertVerifier(provider.clone()));+ let config = ClientConfig::builder_with_provider(provider)+ .with_safe_default_protocol_versions()+ .map_err(|e| Error::Internal(e.to_string()))?+ .dangerous()+ .with_custom_certificate_verifier(verifier)+ .with_no_client_auth();+ Ok(config)+}++/// Accepts self-signed X.509 certificates presented by LocalSend peers on the LAN+/// while still verifying the TLS 1.2 / 1.3 handshake signature with `ring`.+#[derive(Debug)]+struct LocalSendCertVerifier(Arc<CryptoProvider>);++impl ServerCertVerifier for LocalSendCertVerifier {+ fn verify_server_cert(+ &self,+ _end_entity: &CertificateDer<'_>,+ _intermediates: &[CertificateDer<'_>],+ _server_name: &ServerName<'_>,+ _ocsp_response: &[u8],+ _now: UnixTime,+ ) -> std::result::Result<ServerCertVerified, rustls::Error> {+ Ok(ServerCertVerified::assertion())+ }++ fn verify_tls12_signature(+ &self,+ message: &[u8],+ cert: &CertificateDer<'_>,+ dss: &DigitallySignedStruct,+ ) -> std::result::Result<HandshakeSignatureValid, rustls::Error> {+ verify_tls12_signature(message, cert, dss, &self.0.signature_verification_algorithms)+ }++ fn verify_tls13_signature(+ &self,+ message: &[u8],+ cert: &CertificateDer<'_>,+ dss: &DigitallySignedStruct,+ ) -> std::result::Result<HandshakeSignatureValid, rustls::Error> {+ verify_tls13_signature(message, cert, dss, &self.0.signature_verification_algorithms)+ }++ fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {+ self.0.signature_verification_algorithms.supported_schemes()+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::identity::PlainKeyProtector;++ #[tokio::test]+ async fn self_signed_cert_completes_tls_handshake_and_matches_fingerprint() {+ let id = generate_self_signed_identity().unwrap();+ assert_eq!(id.fingerprint.len(), 64);++ let server_cfg = Arc::new(build_server_tls_config(&id).unwrap());+ let client_cfg = Arc::new(build_client_tls_config().unwrap());++ let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap();+ let addr = listener.local_addr().unwrap();++ let acceptor = TlsAcceptor::from(server_cfg);+ let server_task = tokio::spawn(async move {+ let (tcp, _) = listener.accept().await.unwrap();+ let mut tls = acceptor.accept(tcp).await.unwrap();+ let mut buf = [0u8; 4];+ tls.read_exact(&mut buf).await.unwrap();+ assert_eq!(&buf, b"ping");+ tls.write_all(b"pong").await.unwrap();+ tls.flush().await.unwrap();+ });++ let tcp = TcpStream::connect(addr).await.unwrap();+ let connector = TlsConnector::from(client_cfg);+ let domain = ServerName::try_from("localsend.org").unwrap();+ let mut client_tls = connector.connect(domain, tcp).await.unwrap();+ let peer_certs = client_tls.get_ref().1.peer_certificates().unwrap();+ let peer_fp = data_encoding::HEXLOWER.encode(&Sha256::digest(peer_certs[0].as_ref()));+ assert_eq!(peer_fp, id.fingerprint);++ client_tls.write_all(b"ping").await.unwrap();+ let mut reply = [0u8; 4];+ client_tls.read_exact(&mut reply).await.unwrap();+ assert_eq!(&reply, b"pong");+ server_task.await.unwrap();+ }++ #[test]+ fn persists_identity_and_enabled_flag_encrypted_on_disk() {+ let dir = tempfile::tempdir().unwrap();+ let state1 =+ LocalSendState::open(dir.path(), Arc::new(PlainKeyProtector), LOCALSEND_DEFAULT_PORT).unwrap();+ assert!(!state1.enabled());+ let fp1 = state1.identity.fingerprint.clone();++ let state2 =+ LocalSendState::open(dir.path(), Arc::new(PlainKeyProtector), LOCALSEND_DEFAULT_PORT).unwrap();+ assert_eq!(state2.identity.fingerprint, fp1);+ }+}diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rsindex 0a6b4a8..bdf8cc5 100644--- a/core/nectarlink-core/src/node.rs+++ b/core/nectarlink-core/src/node.rs@@ -20,8 +20,8 @@ use iroh_mdns_address_lookup::{DiscoveryEvent, MdnsAddressLookup};use nectarlink_protocol::{ALPN_PAIR, ALPN_SESSION, DeviceId, Envelope, ErrorCode,messages::{- Battery, ClipSet, DeviceInfo, HelloUpdate, MediaCommand, MediaPlayer, Notification, NotifyAction,- NotifyKey, PowerLevel, Ring, types,+ Battery, ClipSet, DeviceInfo, DeviceKind, HelloUpdate, MediaCommand, MediaPlayer, Notification,+ NotifyAction, NotifyKey, PowerLevel, Ring, types,},pairing::PairingUri,};@@ -80,6 +80,9 @@ pub(crate) struct LocalState {impl LocalState {pub fn capabilities(&self) -> Vec<String> {let mut caps: Vec<String> = BASE_CAPABILITIES.iter().map(|c| (*c).to_owned()).collect();+ if !matches!(self.device.kind, DeviceKind::Phone | DeviceKind::Tablet) {+ caps.push(nectarlink_protocol::messages::continuity_camera::RECEIVE.to_owned());+ }for cap in &self.extra_capabilities {if !caps.contains(cap) {caps.push(cap.clone());@@ -141,6 +144,8 @@ pub(crate) struct Shared {pub(crate) clipboard_history: crate::clipboard_history::ClipboardHistoryStore,/// Smart suggestion for the most recently received text clip, if any.pub(crate) last_clip_suggestion: Mutex<Option<(DeviceId, crate::ClipSuggestion)>>,+ /// LocalSend v2.1 discovery, server, and outgoing transfers.+ pub(crate) localsend: crate::localsend::LocalSendState,pub data_dir: std::path::PathBuf,/// Where received files go.pub downloads_dir: std::path::PathBuf,@@ -167,7 +172,13 @@ impl Shared {}pub(crate) fn peer_name(&self, peer: &DeviceId) -> String {- self.store.get_peer(peer).ok().flatten().map(|p| p.info.name).unwrap_or_else(|| peer.short())+ self.store+ .get_peer(peer)+ .ok()+ .flatten()+ .map(|p| p.info.name)+ .or_else(|| self.localsend.peer_name(peer))+ .unwrap_or_else(|| peer.short())}pub(crate) fn record_timeline(&self, mut entry: crate::timeline::NewTimelineEntry) -> Option<i64> {@@ -649,7 +660,9 @@ impl Node {let secret = identity::load_or_create(&config.data_dir, protector.as_ref())?;let store = Store::open(&config.data_dir)?;let clipboard_history =- crate::clipboard_history::ClipboardHistoryStore::open(&config.data_dir, protector);+ crate::clipboard_history::ClipboardHistoryStore::open(&config.data_dir, protector.clone());+ let localsend =+ crate::localsend::LocalSendState::open(&config.data_dir, protector, config.localsend_port)?;// A stable port lets paired devices reconnect to the addresses they// remember even where local discovery is blocked (guest Wi-Fi, some@@ -724,6 +737,7 @@ impl Node {storage: Mutex::new(Default::default()),clipboard_history,last_clip_suggestion: Mutex::new(None),+ localsend,data_dir: config.data_dir.clone(),downloads_dir: config.downloads_dir.clone().unwrap_or_else(|| config.data_dir.join("received")),transfers: Mutex::new(HashMap::new()),@@ -733,6 +747,9 @@ impl Node {if config.lan_discovery {start_lan_discovery(&shared, &config.device.name)?;}+ if shared.localsend.enabled() {+ shared.localsend.start(&shared);+ }let router = Router::builder(endpoint).accept(ALPN_SESSION, SessionProtocol(Arc::downgrade(&shared)))@@ -753,6 +770,7 @@ impl Node {/// Stops all sessions and networking.pub async fn shutdown(&self) {self.shared.cancel.cancel();+ self.shared.localsend.stop(&self.shared);let sessions: Vec<_> = lock(&self.shared.sessions).drain().map(|(_, s)| s).collect();for session in sessions {session.close(CLOSE_NORMAL, b"shutdown");@@ -837,6 +855,7 @@ impl Node {pub async fn refresh(&self) {self.network_changed().await;self.shared.refresh_all_capabilities();+ self.shared.localsend.refresh(&self.shared);for session in self.shared.live_sessions() {let _ = session.send(Envelope::empty(types::NOTIFY_SYNC)).await;let _ = session.send(Envelope::empty(types::MEDIA_SYNC)).await;@@ -1028,13 +1047,54 @@ impl Node {transfer::send_recording(&self.shared, peer, file, markers).await}- /// Cancels a transfer in either direction. Unknown IDs are ignored.+ /// Accepts an incoming transfer waiting for user confirmation (LocalSend).+ pub fn accept_transfer(&self, id: &str) {+ self.shared.localsend.resolve_transfer_approval(id, true);+ }++ /// Cancels a transfer in either direction (or declines a pending incoming+ /// LocalSend transfer). Unknown IDs are ignored.pub fn cancel_transfer(&self, id: &str) {+ self.shared.localsend.resolve_transfer_approval(id, false);if let Some(cancel) = lock(&self.shared.transfers).get(id) {cancel.cancel();}}+ // ---- LocalSend compatibility (docs/protocol/v0.md) ----++ /// Whether LocalSend v2.1 compatibility is enabled on this device.+ pub fn localsend_enabled(&self) -> bool {+ self.shared.localsend.enabled()+ }++ /// Turns LocalSend v2.1 compatibility on or off (persisted encrypted on disk).+ pub fn set_localsend_enabled(&self, enabled: bool) -> Result<()> {+ self.shared.localsend.set_enabled(&self.shared, enabled)+ }++ /// Whether this device's LocalSend HTTP/HTTPS server is actively bound and+ /// able to receive incoming files (false when turned off or when TCP port+ /// 53317 is already owned by a standalone LocalSend process on the same host).+ pub fn localsend_receiving(&self) -> bool {+ self.shared.localsend.receiving()+ }++ /// Returns the LocalSend peers currently discovered on the local network.+ pub fn localsend_peers(&self) -> Vec<crate::LocalSendPeer> {+ self.shared.localsend.peers()+ }++ /// Broadcasts a fresh LocalSend UDP multicast announcement and probes local ports.+ pub fn refresh_localsend(&self) {+ self.shared.localsend.refresh(&self.shared);+ }++ /// Probes `host:port` via the LocalSend v2.1 HTTP/HTTPS API and adds the peer if found.+ pub async fn probe_localsend_peer(&self, host: &str, port: u16) -> Result<crate::LocalSendPeer> {+ crate::localsend::probe_peer(&self.shared, host, port).await+ }+// ---- Clipboard (docs/protocol/clipboard.md) ----/// Puts text on a paired device's clipboard. Fails with@@ -1316,6 +1376,39 @@ impl Node {crate::webcam::open(&self.shared, &session).await}+ // ---- Continuity Camera (docs/protocol/v0.md §Continuity Camera) ----++ /// Asks a paired phone to capture a photo or scan a document and stream the result back.+ pub async fn request_camera_capture(+ &self,+ peer: DeviceId,+ request_id: String,+ mode: crate::CameraCaptureMode,+ ) -> Result<()> {+ crate::continuity_camera::request(&self.shared, peer, request_id, mode).await+ }++ /// Cancels an in-flight Continuity Camera request on either side.+ pub async fn cancel_camera_capture(+ &self,+ peer: DeviceId,+ request_id: String,+ reason: Option<String>,+ ) -> Result<()> {+ crate::continuity_camera::cancel(&self.shared, peer, request_id, reason).await+ }++ /// Sends a captured photo or scanned document from this phone to the requesting PC.+ pub async fn send_camera_capture_result(+ &self,+ peer: DeviceId,+ meta: crate::CameraCaptureResultMeta,+ bytes: Vec<u8>,+ ) -> Result<()> {+ let session = self.connected(&peer)?;+ crate::continuity_camera::send_result(&self.shared, &session, meta, bytes).await+ }+// ---- Messages (docs/protocol/sms.md) ----/// This phone's messages changed (in `thread`, or anywhere): connected@@ -1346,7 +1439,41 @@ impl Node {/// Sends a text through a paired phone.pub async fn sms_send(&self, peer: DeviceId, to: Vec<String>, body: String) -> Result<()> {let session = self.connected(&peer)?;- crate::sms::send(&self.shared, &session, to, body).await+ crate::sms::send(&self.shared, &session, to, body, Vec::new()).await+ }++ /// Sends a text (no attachments) through a paired phone.+ pub async fn send_sms(+ &self,+ peer: impl std::borrow::Borrow<DeviceId>,+ to: Vec<String>,+ body: String,+ ) -> Result<()> {+ let session = self.connected(peer.borrow())?;+ crate::sms::send(&self.shared, &session, to, body, Vec::new()).await+ }++ /// Sends a text or MMS with optional image attachments through a paired phone.+ pub async fn send_sms_with_attachments(+ &self,+ peer: impl std::borrow::Borrow<DeviceId>,+ to: Vec<String>,+ body: String,+ attachments: Vec<crate::SmsAttachment>,+ ) -> Result<()> {+ let session = self.connected(peer.borrow())?;+ crate::sms::send(&self.shared, &session, to, body, attachments).await+ }++ /// Alias for [`Node::send_sms_with_attachments`].+ pub async fn sms_send_with_attachments(+ &self,+ peer: impl std::borrow::Borrow<DeviceId>,+ to: Vec<String>,+ body: String,+ attachments: Vec<crate::SmsAttachment>,+ ) -> Result<()> {+ self.send_sms_with_attachments(peer, to, body, attachments).await}/// A picture in a message: its type and bytes.@@ -1355,6 +1482,82 @@ impl Node {crate::sms::part(&self.shared, &session, id).await}+ // ---- Unified Conversations (MessagingStyle notification threads) ----++ /// Lists locally stored `MessagingStyle` chat threads for a paired phone,+ /// newest first.+ pub fn chat_threads(&self, peer: impl std::borrow::Borrow<DeviceId>) -> Vec<crate::ChatThreadRecord> {+ self.shared.store.chat_threads(peer.borrow()).unwrap_or_default()+ }++ /// Looks up a single `MessagingStyle` chat thread by ID (`chat:<app>:<title>`).+ pub fn chat_thread(+ &self,+ peer: impl std::borrow::Borrow<DeviceId>,+ thread_id: &str,+ ) -> Option<crate::ChatThreadRecord> {+ self.shared.store.chat_thread(peer.borrow(), thread_id).ok().flatten()+ }++ /// Lists up to `limit` messages in a locally stored `MessagingStyle` chat+ /// thread, newest first.+ pub fn chat_messages(+ &self,+ peer: impl std::borrow::Borrow<DeviceId>,+ thread_id: &str,+ limit: u32,+ ) -> Vec<crate::ChatMessageRecord> {+ self.shared.store.chat_messages(peer.borrow(), thread_id, limit as usize).unwrap_or_default()+ }++ /// Marks a locally stored `MessagingStyle` chat thread as read (`unread = 0`).+ pub fn mark_chat_thread_read(&self, peer: impl std::borrow::Borrow<DeviceId>, thread_id: &str) -> bool {+ self.shared.store.mark_chat_thread_read(peer.borrow(), thread_id).unwrap_or(false)+ }++ /// Deletes a single `MessagingStyle` chat thread and its messages.+ pub fn delete_chat_thread(&self, peer: impl std::borrow::Borrow<DeviceId>, thread_id: &str) -> bool {+ self.shared.store.delete_chat_thread(peer.borrow(), thread_id).unwrap_or(false)+ }++ /// Clears all locally stored `MessagingStyle` chat threads.+ pub fn clear_chat_threads(&self) -> usize {+ self.shared.store.clear_chat_threads().unwrap_or(0)+ }++ /// Sends an inline reply to a `MessagingStyle` chat thread using its+ /// latest active notification key and reply action ID, and appends the+ /// outgoing message to the local thread history.+ pub async fn reply_chat_thread(+ &self,+ peer: impl std::borrow::Borrow<DeviceId>,+ thread_id: &str,+ text: String,+ ) -> Result<()> {+ let peer = *peer.borrow();+ let trimmed = text.trim();+ if trimmed.is_empty() {+ return Err(Error::Protocol("empty reply".into()));+ }+ let thread = self.shared.store.chat_thread(&peer, thread_id)?.ok_or(Error::NotFound)?;+ if !thread.active || thread.notification_key.is_empty() {+ return Err(Error::NotFound);+ }+ let Some(action) = thread.reply_action_id else {+ return Err(Error::NotFound);+ };+ let key = thread.notification_key;+ let env =+ Envelope::new(types::NOTIFY_ACTION, &NotifyAction { key, action, reply: Some(trimmed.into()) })?;+ self.request(peer, env).await?.expect(types::OK)?;+ let now_ms = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map_or(0, |d| d.as_millis() as i64);+ let _ = self.shared.store.append_chat_reply(&peer, thread_id, trimmed, now_ms);+ self.shared.emit(NodeEvent::SmsChanged { device: peer, thread: Some(thread_id.to_owned()) });+ Ok(())+ }+// ---- Calls (docs/protocol/calls.md) ----/// A call on this phone rang, was answered or ended; sent to every@@ -1565,8 +1768,20 @@ impl Node {action: String,reply: Option<String>,) -> Result<()> {- let env = Envelope::new(types::NOTIFY_ACTION, &NotifyAction { key, action, reply })?;+ let env = Envelope::new(+ types::NOTIFY_ACTION,+ &NotifyAction { key: key.clone(), action, reply: reply.clone() },+ )?;self.request(peer, env).await?.expect(types::OK)?;+ if let Some(text) = reply.as_deref().map(str::trim).filter(|t| !t.is_empty()) {+ let now_ms = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map_or(0, |d| d.as_millis() as i64);+ if let Ok(Some(thread_id)) = self.shared.store.append_chat_reply_by_key(&peer, &key, text, now_ms)+ {+ self.shared.emit(NodeEvent::SmsChanged { device: peer, thread: Some(thread_id) });+ }+ }Ok(())}@@ -1901,6 +2116,72 @@ impl Node {Ok(removed)}+ // ---- Data retention & privacy ----++ /// Returns current item/byte counts and retention settings across local history and caches.+ pub fn data_retention_counts(&self) -> DataRetentionCounts {+ let (clip_count, clip_bytes) = self.shared.clipboard_history.count_and_bytes();+ let timeline_items = self.shared.store.timeline_total_count().unwrap_or(0);+ let timeline_retention_days = self+ .shared+ .store+ .timeline_retention()+ .map(|r| r.max_days)+ .unwrap_or(crate::DEFAULT_TIMELINE_MAX_DAYS);+ let (chat_threads, chat_messages) = self.shared.store.chat_cache_counts().unwrap_or((0, 0));+ let received_file_records =+ self.shared.store.timeline_count_by_kind(crate::TimelineKind::File).unwrap_or(0);+ DataRetentionCounts {+ clipboard_items: u32::try_from(clip_count).unwrap_or(u32::MAX),+ clipboard_bytes: clip_bytes,+ timeline_items,+ timeline_retention_days,+ chat_threads,+ chat_messages,+ received_file_records,+ }+ }++ /// Clears all locally cached `MessagingStyle` chat threads and messages.+ pub fn clear_message_cache(&self) -> Result<()> {+ let removed = self.shared.store.clear_chat_threads()?;+ if removed > 0+ && let Ok(peers) = self.shared.store.list_peers()+ {+ for peer in peers {+ self.shared.emit(NodeEvent::SmsChanged { device: peer.id, thread: None });+ }+ }+ Ok(())+ }++ /// Clears all file transfer records (`TimelineKind::File`) from the local timeline+ /// without deleting downloaded files on disk.+ pub fn clear_received_file_history(&self) -> Result<()> {+ let removed = self.shared.store.delete_timeline_kind(crate::TimelineKind::File)?;+ if removed > 0 {+ self.shared.emit(NodeEvent::TimelineChanged);+ }+ Ok(())+ }++ /// Wipes all local history and caches (encrypted clipboard history + image files,+ /// timeline entries, and `MessagingStyle` conversation cache) while preserving+ /// device identity, paired devices, and per-device feature toggles.+ pub fn clear_all_local_data(&self) -> Result<()> {+ self.shared.clipboard_history.clear()?;+ *self.shared.last_clip_suggestion.lock().unwrap_or_else(|e| e.into_inner()) = None;+ self.shared.store.clear_all_history_and_cache()?;+ self.shared.emit(NodeEvent::ClipboardHistoryChanged);+ self.shared.emit(NodeEvent::TimelineChanged);+ if let Ok(peers) = self.shared.store.list_peers() {+ for peer in peers {+ self.shared.emit(NodeEvent::SmsChanged { device: peer.id, thread: None });+ }+ }+ Ok(())+ }+fn resolve_timeline_clip(&self, entry: &mut crate::TimelineEntry) {if entry.kind != crate::TimelineKind::Clip {return;@@ -1932,6 +2213,18 @@ impl Node {}}+/// Summary of locally stored history and cache counts for the Privacy & Data settings UI.+#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]+pub struct DataRetentionCounts {+ pub clipboard_items: u32,+ pub clipboard_bytes: u64,+ pub timeline_items: u32,+ pub timeline_retention_days: u32,+ pub chat_threads: u32,+ pub chat_messages: u32,+ pub received_file_records: u32,+}+const PORT_FILE: &str = "port";/// Retrying the saved ports: 5 × 200 ms.const PORT_RETRIES: usize = 5;diff --git a/core/nectarlink-core/src/notifications.rs b/core/nectarlink-core/src/notifications.rsindex 548272e..795c2a1 100644--- a/core/nectarlink-core/src/notifications.rs+++ b/core/nectarlink-core/src/notifications.rs@@ -56,6 +56,7 @@ struct FeedState {active: bool,items: HashMap<String, Notification>,icons: HashMap<String, Vec<u8>>,+ peer_icons: HashMap<(DeviceId, String), Vec<u8>>,}impl Feed {@@ -67,6 +68,18 @@ impl Feed {self.lock().active}+ /// Records an app icon received from a peer and returns the cached icon+ /// for `(peer, app)` when `incoming` is `None`.+ fn peer_icon(&self, peer: DeviceId, app: &str, incoming: Option<&[u8]>) -> Option<Vec<u8>> {+ let mut state = self.lock();+ if let Some(icon) = incoming {+ state.peer_icons.insert((peer, app.to_owned()), icon.to_vec());+ Some(icon.to_vec())+ } else {+ state.peer_icons.get(&(peer, app.to_owned())).cloned()+ }+ }+/// Stores a notification (its icon separately); returns it without the/// icon, sanitized, or `None` if it can't be shown.fn post(&self, notification: Notification) -> Option<Notification> {@@ -159,12 +172,27 @@ fn approx_size(n: &Notification) -> usize {let live = n.live.as_ref().map_or(0, |l| {l.chip.as_ref().map_or(0, String::len) + (l.segments.len() + l.points.len()) * 16 + 32});+ let conv = n.conversation.as_ref().map_or(0, |c| {+ c.title.len()+ + c.avatar.as_ref().map_or(0, Vec::len)+ + c.messages+ .iter()+ .map(|m| {+ m.sender.as_ref().map_or(0, String::len)+ + m.text.len()+ + m.avatar.as_ref().map_or(0, Vec::len)+ + 32+ })+ .sum::<usize>()+ + 32+ });n.key.len()+ n.app.len()+ n.app_name.len()+ text+ actions+ live+ + conv+ n.image.as_ref().map_or(0, Vec::len)+ 64}@@ -241,12 +269,24 @@ pub(crate) async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: &Etypes::NOTIFY_SNAPSHOT => {if shared.notifications_allowed(&peer) {let snapshot: NotifySnapshot = env.body()?;- let items = snapshot+ let items: Vec<Notification> = snapshot.items.into_iter().filter_map(Notification::sanitized).take(SNAPSHOT_ITEMS).collect();+ let mut changed_thread = None;+ for n in &items {+ let cached_icon = shared.notifications.peer_icon(peer, &n.app, n.icon.as_deref());+ if let Ok(Some(tid)) =+ shared.store.upsert_chat_notification(&peer, n, cached_icon.as_deref(), true)+ {+ changed_thread = Some(tid);+ }+ }+ if let Some(thread) = changed_thread {+ shared.emit(NodeEvent::SmsChanged { device: peer, thread: Some(thread) });+ }shared.emit(NodeEvent::NotificationsReset { device: peer, items });}}@@ -254,11 +294,21 @@ pub(crate) async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: &Eif shared.notifications_allowed(&peer)&& let Some(notification) = env.body::<Notification>()?.sanitized(){+ let cached_icon =+ shared.notifications.peer_icon(peer, ¬ification.app, notification.icon.as_deref());+ if let Ok(Some(thread)) =+ shared.store.upsert_chat_notification(&peer, ¬ification, cached_icon.as_deref(), false)+ {+ shared.emit(NodeEvent::SmsChanged { device: peer, thread: Some(thread) });+ }shared.emit(NodeEvent::NotificationPosted { device: peer, notification });}}types::NOTIFY_REMOVED => {let NotifyKey { key } = env.body()?;+ if let Ok(Some(thread)) = shared.store.mark_chat_notification_removed(&peer, &key) {+ shared.emit(NodeEvent::SmsChanged { device: peer, thread: Some(thread) });+ }shared.emit(NodeEvent::NotificationRemoved { device: peer, key });}// ---- On the phone ----diff --git a/core/nectarlink-core/src/session.rs b/core/nectarlink-core/src/session.rsindex 01dfad6..e18812b 100644--- a/core/nectarlink-core/src/session.rs+++ b/core/nectarlink-core/src/session.rs@@ -336,6 +336,7 @@ fn is_background_rpc(t: &str) -> bool {| types::MIRROR_START| types::MIRROR_APPS| types::WEBCAM_START+ | types::CAMERA_CAPTURE_REQUEST| types::STORAGE_LIST| types::STORAGE_MKDIR| types::STORAGE_RENAME@@ -412,6 +413,8 @@ async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: Envelope) ->t if t.starts_with("sms.") && crate::sms::handle(shared, session, &env).await? => {}t if t.starts_with("mirror.") && crate::mirror::handle(shared, session, &env).await? => {}t if t.starts_with("webcam.") && crate::webcam::handle(shared, session, &env).await? => {}+ t if t.starts_with("camera.capture.")+ && crate::continuity_camera::handle(shared, session, &env).await? => {}t if t.starts_with("remote.") && crate::remote::handle(shared, session, &env).await? => {}t if t.starts_with("phone.toggle") && crate::toggles::handle(shared, session, &env).await? => {}t if (t.starts_with("deck.") || t == types::PC_AUDIO_SET)diff --git a/core/nectarlink-core/src/sms.rs b/core/nectarlink-core/src/sms.rsindex 11a8a76..c642311 100644--- a/core/nectarlink-core/src/sms.rs+++ b/core/nectarlink-core/src/sms.rs@@ -8,8 +8,8 @@ use std::sync::Arc;use nectarlink_protocol::{DeviceId, Envelope, ErrorCode, MAX_FRAME_LEN,messages::{- SmsChanged, SmsMessage, SmsMessages, SmsMessagesGet, SmsPartData, SmsPartGet, SmsSend, SmsThread,- SmsThreads, SmsThreadsGet, sms, types,+ SmsAttachment, SmsChanged, SmsMessage, SmsMessages, SmsMessagesGet, SmsPartData, SmsPartGet, SmsSend,+ SmsThread, SmsThreads, SmsThreadsGet, sms, types,},};@@ -70,15 +70,27 @@ pub(crate) async fn messages(Ok(messages)}-pub(crate) async fn send(shared: &Shared, session: &Session, to: Vec<String>, body: String) -> Result<()> {+pub(crate) async fn send(+ shared: &Shared,+ session: &Session,+ to: Vec<String>,+ body: String,+ attachments: Vec<SmsAttachment>,+) -> Result<()> {allowed(shared, &session.peer)?;- let send = SmsSend { to, body };+ let send = SmsSend { to, body, attachments };if !send.is_valid() {- return Err(if send.body.len() > sms::MAX_SEND_BYTES {- Error::TooLarge- } else {- Error::Protocol("invalid text".into())- });+ let any_oversized = send.attachments.iter().any(|a| a.data.len() > sms::MAX_ATTACHMENT_BYTES);+ return Err(+ if send.body.len() > sms::MAX_SEND_BYTES+ || any_oversized+ || send.attachments.len() > sms::MAX_ATTACHMENTS+ {+ Error::TooLarge+ } else {+ Error::Protocol("invalid text".into())+ },+ );}let env = Envelope::new(types::SMS_SEND, &send)?;session.request(env, crate::session::REQUEST_TIMEOUT).await?.expect(types::OK)?;@@ -160,7 +172,9 @@ async fn answer(shared: &Arc<Shared>, session: &Session, env: &Envelope) -> Resureturn Ok(Envelope::error(ErrorCode::BadMessage, "invalid text"));}blocking(Box::new(move || {- platform.sms_send(&send.to, &send.body).map(|()| Envelope::empty(types::OK))+ platform+ .send_sms_with_attachments(&send.to, &send.body, &send.attachments)+ .map(|()| Envelope::empty(types::OK))})).await}diff --git a/core/nectarlink-core/src/store.rs b/core/nectarlink-core/src/store.rsindex 1bcd150..254fddc 100644--- a/core/nectarlink-core/src/store.rs+++ b/core/nectarlink-core/src/store.rs@@ -11,7 +11,7 @@ use std::{use nectarlink_protocol::{DeviceId,- messages::{DeviceInfo, DeviceKind, PcWakeInfo, PowerLevel, ScreenShape},+ messages::{DeviceInfo, DeviceKind, Notification, PcWakeInfo, PowerLevel, ScreenShape},};use rusqlite::{Connection, OptionalExtension, params};@@ -25,6 +25,87 @@ use crate::{const FILE_NAME: &str = "nectarlink.db";+/// Retention window for persisted notification-backed chat threads (90 days).+pub const CHAT_RETENTION_SECS: i64 = 90 * 86_400;+/// Maximum persisted notification-backed chat threads per paired phone.+pub const MAX_CHAT_THREADS_PER_PEER: usize = 200;+/// Maximum persisted messages per notification-backed chat thread.+pub const MAX_CHAT_MESSAGES_PER_THREAD: usize = 200;++/// Deterministic thread ID for a notification-backed messaging conversation.+pub fn chat_thread_id(app: &str, title: &str) -> String {+ format!("chat:{}:{}", app.trim(), title.trim().to_lowercase())+}++/// A persisted conversation from a messaging app notification (`MessagingStyle`).+#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]+pub struct ChatThreadRecord {+ pub peer: DeviceId,+ pub thread_id: String,+ pub app: String,+ pub app_name: String,+ pub notification_key: String,+ pub reply_action_id: Option<String>,+ pub title: String,+ pub is_group: bool,+ pub snippet: String,+ pub last_sender: Option<String>,+ pub updated_ms: i64,+ pub unread: u32,+ pub active: bool,+ pub avatar: Option<Vec<u8>>,+ pub app_icon: Option<Vec<u8>>,+}++/// Never prints conversation titles, snippets, or sender names (protocol v0 §11).+impl std::fmt::Debug for ChatThreadRecord {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("ChatThreadRecord")+ .field("peer", &self.peer)+ .field("thread_id", &self.thread_id)+ .field("app", &self.app)+ .field("is_group", &self.is_group)+ .field("updated_ms", &self.updated_ms)+ .field("unread", &self.unread)+ .field("active", &self.active)+ .finish_non_exhaustive()+ }+}++impl ChatThreadRecord {+ /// Whether this conversation's latest notification is still active and has+ /// an inline reply action.+ pub fn can_reply(&self) -> bool {+ self.active && self.reply_action_id.is_some()+ }+}++/// One persisted message inside a [`ChatThreadRecord`].+#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]+pub struct ChatMessageRecord {+ pub id: i64,+ pub peer: DeviceId,+ pub thread_id: String,+ pub sender: Option<String>,+ pub text: String,+ pub time_ms: i64,+ pub self_sent: bool,+ pub avatar: Option<Vec<u8>>,+}++/// Never prints message text or sender names (protocol v0 §11).+impl std::fmt::Debug for ChatMessageRecord {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("ChatMessageRecord")+ .field("id", &self.id)+ .field("peer", &self.peer)+ .field("thread_id", &self.thread_id)+ .field("time_ms", &self.time_ms)+ .field("self_sent", &self.self_sent)+ .finish_non_exhaustive()+ }+}+/// Schema migrations, applied in order. Index + 1 is the schema version.const MIGRATIONS: &[&str] = &[// v1: trust store@@ -77,6 +158,40 @@ const MIGRATIONS: &[&str] = &[key TEXT PRIMARY KEY NOT NULL,value TEXT NOT NULL) STRICT;",+ // v6: unified conversations persisted from MessagingStyle notifications.+ "CREATE TABLE IF NOT EXISTS chat_threads (+ peer TEXT NOT NULL,+ thread_id TEXT NOT NULL,+ app TEXT NOT NULL,+ app_name TEXT NOT NULL,+ notification_key TEXT NOT NULL,+ reply_action_id TEXT,+ title TEXT NOT NULL,+ is_group INTEGER NOT NULL DEFAULT 0,+ snippet TEXT NOT NULL DEFAULT '',+ last_sender TEXT,+ updated_ms INTEGER NOT NULL,+ unread INTEGER NOT NULL DEFAULT 0,+ active INTEGER NOT NULL DEFAULT 1,+ avatar BLOB,+ app_icon BLOB,+ PRIMARY KEY (peer, thread_id)+ );+ CREATE INDEX IF NOT EXISTS idx_chat_threads_peer_updated+ ON chat_threads (peer, updated_ms DESC);++ CREATE TABLE IF NOT EXISTS chat_messages (+ id INTEGER PRIMARY KEY AUTOINCREMENT,+ peer TEXT NOT NULL,+ thread_id TEXT NOT NULL,+ sender TEXT,+ text TEXT NOT NULL,+ time_ms INTEGER NOT NULL,+ self_sent INTEGER NOT NULL DEFAULT 0,+ avatar BLOB+ );+ CREATE INDEX IF NOT EXISTS idx_chat_messages_thread_time+ ON chat_messages (peer, thread_id, time_ms DESC, id DESC);",];/// A paired device as stored on disk.@@ -245,7 +360,12 @@ impl Store {/// Removes a device. Returns whether it existed.pub fn remove_peer(&self, id: &DeviceId) -> Result<bool> {- self.with(|c| c.execute("DELETE FROM peers WHERE id = ?1", [id.as_bytes().as_slice()]).map(|n| n > 0))+ let peer_str = id.to_string();+ self.with(|c| {+ let _ = c.execute("DELETE FROM chat_messages WHERE peer = ?1", [&peer_str]);+ let _ = c.execute("DELETE FROM chat_threads WHERE peer = ?1", [&peer_str]);+ c.execute("DELETE FROM peers WHERE id = ?1", [id.as_bytes().as_slice()]).map(|n| n > 0)+ })}pub fn get_peer(&self, id: &DeviceId) -> Result<Option<PeerRecord>> {@@ -453,6 +573,386 @@ impl Store {})})}++ // ---- Unified conversations (`MessagingStyle` notifications) ----++ pub fn upsert_chat_notification(+ &self,+ peer: &DeviceId,+ n: &Notification,+ app_icon: Option<&[u8]>,+ is_snapshot: bool,+ ) -> Result<Option<String>> {+ let Some(conv) = &n.conversation else {+ return Ok(None);+ };+ if conv.messages.is_empty() || conv.title.trim().is_empty() {+ return Ok(None);+ }+ let thread_id = chat_thread_id(&n.app, &conv.title);+ let reply_action_id = n.actions.iter().find(|a| a.reply).map(|a| a.id.clone());+ let latest = conv.messages.last().expect("checked non-empty");+ let snippet = latest.text.clone();+ let last_sender = if latest.self_sent { None } else { latest.sender.clone() };+ let updated_ms = conv.messages.iter().map(|m| m.time).max().unwrap_or(n.when).max(n.when);+ let peer_str = peer.to_string();+ let icon_bytes = app_icon.or(n.icon.as_deref());++ self.with(|c| {+ let existing: Vec<(String, String, bool)> = {+ let mut stmt = c.prepare(+ "SELECT IFNULL(sender, ''), text, self_sent+ FROM (+ SELECT id, sender, text, time_ms, self_sent+ FROM chat_messages+ WHERE peer = ?1 AND thread_id = ?2+ ORDER BY time_ms DESC, id DESC+ LIMIT 50+ )+ ORDER BY time_ms ASC, id ASC",+ )?;+ stmt.query_map(params![peer_str, thread_id], |r| {+ Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?, r.get::<_, i64>(2)? != 0))+ })?+ .collect::<rusqlite::Result<Vec<_>>>()?+ };++ let incoming_keys: Vec<(String, String, bool)> = conv+ .messages+ .iter()+ .map(|m| (m.sender.clone().unwrap_or_default(), m.text.clone(), m.self_sent))+ .collect();++ let mut matched_prefix = 0usize;+ for p in (1..=incoming_keys.len().min(existing.len())).rev() {+ let prefix = &incoming_keys[..p];+ if existing.windows(p).any(|w| w == prefix) {+ matched_prefix = p;+ break;+ }+ }++ let mut new_incoming_count = 0i64;+ for msg in conv.messages.iter().skip(matched_prefix) {+ let exists: Option<i64> = c+ .query_row(+ "SELECT id FROM chat_messages+ WHERE peer = ?1 AND thread_id = ?2+ AND IFNULL(sender, '') = IFNULL(?3, '')+ AND text = ?4+ AND self_sent = ?5+ AND (time_ms = ?6 OR (self_sent != 0 AND ABS(time_ms - ?6) <= 5000))+ LIMIT 1",+ params![+ peer_str,+ thread_id,+ msg.sender,+ msg.text,+ i64::from(msg.self_sent),+ msg.time,+ ],+ |r| r.get(0),+ )+ .optional()?;+ if exists.is_none() {+ c.execute(+ "INSERT INTO chat_messages (peer, thread_id, sender, text, time_ms, self_sent, avatar)+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",+ params![+ peer_str,+ thread_id,+ msg.sender,+ msg.text,+ msg.time,+ i64::from(msg.self_sent),+ msg.avatar,+ ],+ )?;+ if !msg.self_sent {+ new_incoming_count += 1;+ }+ }+ }++ let snapshot_unread = new_incoming_count.min(conv.messages.len() as i64);+ let initial_unread = if is_snapshot { snapshot_unread } else { new_incoming_count };+ c.execute(+ "INSERT INTO chat_threads (+ peer, thread_id, app, app_name, notification_key, reply_action_id,+ title, is_group, snippet, last_sender, updated_ms, unread, active, avatar, app_icon+ ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, 1, ?13, ?14)+ ON CONFLICT(peer, thread_id) DO UPDATE SET+ app = excluded.app,+ app_name = excluded.app_name,+ notification_key = excluded.notification_key,+ reply_action_id = excluded.reply_action_id,+ title = excluded.title,+ is_group = excluded.is_group,+ snippet = excluded.snippet,+ last_sender = excluded.last_sender,+ updated_ms = MAX(chat_threads.updated_ms, excluded.updated_ms),+ unread = CASE+ WHEN ?15 != 0 THEN CASE+ WHEN ?16 > 0 THEN ?16+ ELSE chat_threads.unread+ END+ ELSE chat_threads.unread + ?17+ END,+ active = 1,+ avatar = COALESCE(excluded.avatar, chat_threads.avatar),+ app_icon = COALESCE(excluded.app_icon, chat_threads.app_icon)",+ params![+ peer_str,+ thread_id,+ n.app,+ n.app_name,+ n.key,+ reply_action_id,+ conv.title.trim(),+ i64::from(conv.group),+ snippet,+ last_sender,+ updated_ms,+ initial_unread,+ conv.avatar,+ icon_bytes,+ i64::from(is_snapshot),+ snapshot_unread,+ new_incoming_count,+ ],+ )?;++ prune_chat_conn(c, &peer_str, &thread_id)?;+ Ok(Some(thread_id))+ })+ }++ pub fn append_chat_reply(+ &self,+ peer: &DeviceId,+ thread_id: &str,+ text: &str,+ time_ms: i64,+ ) -> Result<bool> {+ let trimmed = text.trim();+ if trimmed.is_empty() {+ return Ok(false);+ }+ let peer_str = peer.to_string();+ self.with(|c| {+ let exists_thread: Option<i64> = c+ .query_row(+ "SELECT 1 FROM chat_threads WHERE peer = ?1 AND thread_id = ?2",+ params![peer_str, thread_id],+ |r| r.get(0),+ )+ .optional()?;+ if exists_thread.is_none() {+ return Ok(false);+ }+ let dup: Option<i64> = c+ .query_row(+ "SELECT id FROM chat_messages+ WHERE peer = ?1 AND thread_id = ?2 AND self_sent = 1+ AND text = ?3 AND ABS(time_ms - ?4) <= 2000+ LIMIT 1",+ params![peer_str, thread_id, trimmed, time_ms],+ |r| r.get(0),+ )+ .optional()?;+ if dup.is_none() {+ c.execute(+ "INSERT INTO chat_messages (peer, thread_id, sender, text, time_ms, self_sent, avatar)+ VALUES (?1, ?2, NULL, ?3, ?4, 1, NULL)",+ params![peer_str, thread_id, trimmed, time_ms],+ )?;+ }+ let snippet = format!("You: {trimmed}");+ c.execute(+ "UPDATE chat_threads+ SET snippet = ?3,+ last_sender = NULL,+ updated_ms = MAX(updated_ms, ?4),+ unread = 0+ WHERE peer = ?1 AND thread_id = ?2",+ params![peer_str, thread_id, snippet, time_ms],+ )?;+ prune_chat_messages_conn(c, &peer_str, thread_id)?;+ Ok(true)+ })+ }++ pub fn append_chat_reply_by_key(+ &self,+ peer: &DeviceId,+ notification_key: &str,+ text: &str,+ time_ms: i64,+ ) -> Result<Option<String>> {+ let peer_str = peer.to_string();+ let thread_id: Option<String> = self.with(|c| {+ c.query_row(+ "SELECT thread_id FROM chat_threads+ WHERE peer = ?1 AND notification_key = ?2+ ORDER BY updated_ms DESC LIMIT 1",+ params![peer_str, notification_key],+ |r| r.get(0),+ )+ .optional()+ })?;+ let Some(tid) = thread_id else {+ return Ok(None);+ };+ if self.append_chat_reply(peer, &tid, text, time_ms)? { Ok(Some(tid)) } else { Ok(None) }+ }++ pub fn mark_chat_notification_removed(+ &self,+ peer: &DeviceId,+ notification_key: &str,+ ) -> Result<Option<String>> {+ let peer_str = peer.to_string();+ self.with(|c| {+ let thread_id: Option<String> = c+ .query_row(+ "SELECT thread_id FROM chat_threads+ WHERE peer = ?1 AND notification_key = ?2 AND active = 1+ ORDER BY updated_ms DESC LIMIT 1",+ params![peer_str, notification_key],+ |r| r.get(0),+ )+ .optional()?;+ if let Some(ref tid) = thread_id {+ c.execute(+ "UPDATE chat_threads SET active = 0, unread = 0+ WHERE peer = ?1 AND thread_id = ?2",+ params![peer_str, tid],+ )?;+ }+ Ok(thread_id)+ })+ }++ pub fn chat_threads(&self, peer: &DeviceId) -> Result<Vec<ChatThreadRecord>> {+ let peer_str = peer.to_string();+ let peer_id = *peer;+ self.with(|c| {+ let mut stmt = c.prepare(&format!(+ "{SELECT_CHAT_THREADS} WHERE peer = ?1 ORDER BY updated_ms DESC, thread_id ASC"+ ))?;+ stmt.query_map([&peer_str], |r| row_to_chat_thread(&peer_id, r))?.collect()+ })+ }++ pub fn chat_thread(&self, peer: &DeviceId, thread_id: &str) -> Result<Option<ChatThreadRecord>> {+ let peer_str = peer.to_string();+ let peer_id = *peer;+ self.with(|c| {+ c.query_row(+ &format!("{SELECT_CHAT_THREADS} WHERE peer = ?1 AND thread_id = ?2"),+ params![peer_str, thread_id],+ |r| row_to_chat_thread(&peer_id, r),+ )+ .optional()+ })+ }++ pub fn chat_messages(+ &self,+ peer: &DeviceId,+ thread_id: &str,+ limit: usize,+ ) -> Result<Vec<ChatMessageRecord>> {+ let peer_str = peer.to_string();+ let peer_id = *peer;+ let capped = limit.clamp(1, MAX_CHAT_MESSAGES_PER_THREAD) as i64;+ self.with(|c| {+ let mut stmt = c.prepare(+ "SELECT id, thread_id, sender, text, time_ms, self_sent, avatar+ FROM chat_messages+ WHERE peer = ?1 AND thread_id = ?2+ ORDER BY time_ms DESC, id DESC+ LIMIT ?3",+ )?;+ stmt.query_map(params![peer_str, thread_id, capped], |r| row_to_chat_message(&peer_id, r))?+ .collect()+ })+ }++ pub fn mark_chat_thread_read(&self, peer: &DeviceId, thread_id: &str) -> Result<bool> {+ let peer_str = peer.to_string();+ self.with(|c| {+ c.execute(+ "UPDATE chat_threads SET unread = 0 WHERE peer = ?1 AND thread_id = ?2 AND unread != 0",+ params![peer_str, thread_id],+ )+ .map(|n| n > 0)+ })+ }++ pub fn delete_chat_thread(&self, peer: &DeviceId, thread_id: &str) -> Result<bool> {+ let peer_str = peer.to_string();+ self.with(|c| {+ c.execute(+ "DELETE FROM chat_messages WHERE peer = ?1 AND thread_id = ?2",+ params![peer_str, thread_id],+ )?;+ c.execute(+ "DELETE FROM chat_threads WHERE peer = ?1 AND thread_id = ?2",+ params![peer_str, thread_id],+ )+ .map(|n| n > 0)+ })+ }++ pub fn clear_chat_threads(&self) -> Result<usize> {+ self.with(|c| {+ c.execute("DELETE FROM chat_messages", [])?;+ c.execute("DELETE FROM chat_threads", [])+ })+ }++ pub fn delete_timeline_kind(&self, kind: TimelineKind) -> Result<u64> {+ self.with(|c| c.execute("DELETE FROM timeline WHERE kind = ?1", [kind.as_str()]).map(|n| n as u64))+ }++ pub fn timeline_count_by_kind(&self, kind: TimelineKind) -> Result<u32> {+ self.with(|c| {+ let n: i64 =+ c.query_row("SELECT COUNT(*) FROM timeline WHERE kind = ?1", [kind.as_str()], |r| r.get(0))?;+ Ok(u32::try_from(n.max(0)).unwrap_or(u32::MAX))+ })+ }++ pub fn timeline_total_count(&self) -> Result<u32> {+ self.with(|c| {+ let n: i64 = c.query_row("SELECT COUNT(*) FROM timeline", [], |r| r.get(0))?;+ Ok(u32::try_from(n.max(0)).unwrap_or(u32::MAX))+ })+ }++ pub fn chat_cache_counts(&self) -> Result<(u32, u32)> {+ self.with(|c| {+ let threads: i64 = c.query_row("SELECT COUNT(*) FROM chat_threads", [], |r| r.get(0))?;+ let msgs: i64 = c.query_row("SELECT COUNT(*) FROM chat_messages", [], |r| r.get(0))?;+ Ok((+ u32::try_from(threads.max(0)).unwrap_or(u32::MAX),+ u32::try_from(msgs.max(0)).unwrap_or(u32::MAX),+ ))+ })+ }++ pub fn clear_all_history_and_cache(&self) -> Result<()> {+ self.with(|c| {+ let tx = c.unchecked_transaction()?;+ tx.execute("DELETE FROM timeline", [])?;+ tx.execute("DELETE FROM chat_messages", [])?;+ tx.execute("DELETE FROM chat_threads", [])?;+ tx.commit()?;+ let _ = c.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);");+ Ok(())+ })+ }}const SELECT: &str = "SELECT id, name, kind, os, os_ver, model, accent, paired_at, last_seen, last_addrs, \@@ -461,6 +961,100 @@ const SELECT: &str = "SELECT id, name, kind, os, os_ver, model, accent, paired_aconst SELECT_TIMELINE: &str = "SELECT id, kind, device_id, device_name, incoming, timestamp, \title, detail, target, size_bytes, duration_secs, ref_id FROM timeline";+const SELECT_CHAT_THREADS: &str = "SELECT thread_id, app, app_name, notification_key, reply_action_id, \+ title, is_group, snippet, last_sender, updated_ms, unread, active, avatar, app_icon \+ FROM chat_threads";++fn prune_chat_messages_conn(c: &Connection, peer_str: &str, thread_id: &str) -> rusqlite::Result<()> {+ c.execute(+ "DELETE FROM chat_messages+ WHERE peer = ?1 AND thread_id = ?2+ AND id NOT IN (+ SELECT id FROM chat_messages+ WHERE peer = ?1 AND thread_id = ?2+ ORDER BY time_ms DESC, id DESC+ LIMIT ?3+ )",+ params![peer_str, thread_id, MAX_CHAT_MESSAGES_PER_THREAD as i64],+ )?;+ Ok(())+}++fn prune_chat_conn(c: &Connection, peer_str: &str, thread_id: &str) -> rusqlite::Result<()> {+ prune_chat_messages_conn(c, peer_str, thread_id)?;+ let max_updated: Option<i64> = c+ .query_row("SELECT MAX(updated_ms) FROM chat_threads WHERE peer = ?1", [peer_str], |r| r.get(0))+ .optional()?+ .flatten();+ if let Some(latest_ms) = max_updated {+ let retention_ms = CHAT_RETENTION_SECS * 1000;+ if latest_ms > retention_ms {+ let cutoff_ms = latest_ms - retention_ms;+ c.execute(+ "DELETE FROM chat_threads WHERE peer = ?1 AND updated_ms < ?2",+ params![peer_str, cutoff_ms],+ )?;+ }+ }+ c.execute(+ "DELETE FROM chat_threads+ WHERE peer = ?1+ AND thread_id NOT IN (+ SELECT thread_id FROM chat_threads+ WHERE peer = ?1+ ORDER BY updated_ms DESC, thread_id ASC+ LIMIT ?2+ )",+ params![peer_str, MAX_CHAT_THREADS_PER_PEER as i64],+ )?;+ c.execute(+ "DELETE FROM chat_messages+ WHERE peer = ?1+ AND thread_id NOT IN (+ SELECT thread_id FROM chat_threads WHERE peer = ?1+ )",+ [peer_str],+ )?;+ Ok(())+}++fn row_to_chat_thread(peer: &DeviceId, row: &rusqlite::Row<'_>) -> rusqlite::Result<ChatThreadRecord> {+ let is_group: i64 = row.get(6)?;+ let unread: i64 = row.get(10)?;+ let active: i64 = row.get(11)?;+ Ok(ChatThreadRecord {+ peer: *peer,+ thread_id: row.get(0)?,+ app: row.get(1)?,+ app_name: row.get(2)?,+ notification_key: row.get(3)?,+ reply_action_id: row.get(4)?,+ title: row.get(5)?,+ is_group: is_group != 0,+ snippet: row.get(7)?,+ last_sender: row.get(8)?,+ updated_ms: row.get(9)?,+ unread: u32::try_from(unread.max(0)).unwrap_or(0),+ active: active != 0,+ avatar: row.get(12)?,+ app_icon: row.get(13)?,+ })+}++fn row_to_chat_message(peer: &DeviceId, row: &rusqlite::Row<'_>) -> rusqlite::Result<ChatMessageRecord> {+ let self_sent: i64 = row.get(5)?;+ Ok(ChatMessageRecord {+ id: row.get(0)?,+ peer: *peer,+ thread_id: row.get(1)?,+ sender: row.get(2)?,+ text: row.get(3)?,+ time_ms: row.get(4)?,+ self_sent: self_sent != 0,+ avatar: row.get(6)?,+ })+}+fn read_retention_conn(c: &Connection) -> rusqlite::Result<TimelineRetention> {let mut retention = TimelineRetention::default();if let Some(raw) = c@@ -954,4 +1548,189 @@ mod tests {assert_eq!(page25.entries.len(), 100);assert!(elapsed.as_millis() < 200, "paging 5,000 entries took {elapsed:?}");}++ #[test]+ fn chat_notifications_deduplicate_reply_and_prune() {+ use nectarlink_protocol::messages::{+ Notification, NotificationAction, NotificationChatMessage, NotificationConversation,+ };+ let store = Store::in_memory().unwrap();+ let peer = DeviceId([7u8; 32]);+ let now_ms = 1_760_000_000_000i64;++ let conv = NotificationConversation {+ v: 1,+ title: "Design Team".into(),+ group: true,+ avatar: Some(vec![0xff, 0xd8, 1]),+ messages: vec![+ NotificationChatMessage {+ sender: Some("Maya".into()),+ text: "Standup in 5?".into(),+ time: now_ms - 120_000,+ self_sent: false,+ avatar: Some(vec![0xff, 0xd8, 2]),+ },+ NotificationChatMessage {+ sender: None,+ text: "Joining now".into(),+ time: now_ms - 60_000,+ self_sent: true,+ avatar: None,+ },+ ],+ };+ let note = Notification {+ key: "wa|design".into(),+ app: "com.whatsapp".into(),+ app_name: "WhatsApp".into(),+ title: Some("Design Team".into()),+ text: Some("Joining now".into()),+ sub: None,+ when: now_ms - 60_000,+ actions: vec![NotificationAction { id: "reply".into(), title: "Reply".into(), reply: true }],+ silent: false,+ icon: Some(vec![0x89, b'P', b'N', b'G']),+ image: None,+ live: None,+ conversation: Some(conv),+ };++ let tid = store.upsert_chat_notification(&peer, ¬e, None, false).unwrap().unwrap();+ // Repeated snapshot of the exact same notification must not duplicate messages or bump unread.+ let tid_again = store.upsert_chat_notification(&peer, ¬e, None, true).unwrap().unwrap();+ assert_eq!(tid, tid_again);++ let threads = store.chat_threads(&peer).unwrap();+ assert_eq!(threads.len(), 1);+ assert_eq!(threads[0].thread_id, tid);+ assert_eq!(threads[0].title, "Design Team");+ assert!(threads[0].is_group);+ assert_eq!(threads[0].unread, 1, "only incoming message increments unread");+ assert!(threads[0].can_reply());+ assert!(threads[0].app_icon.is_some());+ assert!(threads[0].avatar.is_some());++ let msgs = store.chat_messages(&peer, &tid, 20).unwrap();+ assert_eq!(msgs.len(), 2);+ assert_eq!(msgs[0].text, "Joining now");+ assert!(msgs[0].self_sent);+ assert_eq!(msgs[1].text, "Standup in 5?");+ assert_eq!(msgs[1].sender.as_deref(), Some("Maya"));++ // Replying appends an outgoing message and resets unread count.+ assert!(store.append_chat_reply(&peer, &tid, "See you there!", now_ms + 5_000).unwrap());+ let thread = store.chat_thread(&peer, &tid).unwrap().unwrap();+ assert_eq!(thread.unread, 0);+ assert_eq!(thread.snippet, "You: See you there!");+ let msgs = store.chat_messages(&peer, &tid, 20).unwrap();+ assert_eq!(msgs.len(), 3);+ assert_eq!(msgs[0].text, "See you there!");+ assert!(msgs[0].self_sent);++ // Removing the notification clears the active reply target while keeping history.+ let cleared = store.mark_chat_notification_removed(&peer, "wa|design").unwrap();+ assert_eq!(cleared.as_deref(), Some(tid.as_str()));+ let thread = store.chat_thread(&peer, &tid).unwrap().unwrap();+ assert!(!thread.can_reply());+ assert_eq!(store.chat_messages(&peer, &tid, 20).unwrap().len(), 3);++ assert!(store.delete_chat_thread(&peer, &tid).unwrap());+ assert!(store.chat_threads(&peer).unwrap().is_empty());+ }++ #[test]+ fn data_retention_counts_and_clear_all_preserves_peers_and_meta() {+ use nectarlink_protocol::messages::{+ Notification, NotificationChatMessage, NotificationConversation,+ };+ let store = Store::in_memory().unwrap();+ let peer = DeviceId([8; 32]);+ let now = 1_700_000_000i64;++ // Seed peer, toggle, and meta.+ store.upsert_peer(&peer, &info("Pixel 9"), now).unwrap();+ store.set_toggle(&peer, "clipboard", false).unwrap();+ store.set_timeline_retention(TimelineRetention { max_days: 30, max_entries: 5000 }, now).unwrap();++ // Seed timeline entries (2 File + 1 Link).+ for (i, kind) in [TimelineKind::File, TimelineKind::File, TimelineKind::Link].into_iter().enumerate()+ {+ store+ .insert_timeline(+ &NewTimelineEntry {+ kind,+ device_id: peer,+ device_name: "Pixel 9".into(),+ incoming: true,+ timestamp: now + i as i64,+ title: format!("entry-{i}"),+ detail: "detail".into(),+ target: String::new(),+ size_bytes: 1024,+ duration_secs: 0,+ ref_id: None,+ },+ now + i as i64,+ )+ .unwrap();+ }++ // Seed chat conversation cache.+ let note = Notification {+ key: "wa|alex".into(),+ app: "com.whatsapp".into(),+ app_name: "WhatsApp".into(),+ title: Some("Alex".into()),+ text: Some("Hey there".into()),+ sub: None,+ when: now * 1000,+ actions: vec![],+ silent: false,+ icon: None,+ image: None,+ live: None,+ conversation: Some(NotificationConversation {+ v: 1,+ title: "Alex".into(),+ group: false,+ avatar: None,+ messages: vec![+ NotificationChatMessage {+ sender: Some("Alex".into()),+ text: "Hello!".into(),+ time: now * 1000 - 1000,+ self_sent: false,+ avatar: None,+ },+ NotificationChatMessage {+ sender: None,+ text: "Hi Alex".into(),+ time: now * 1000,+ self_sent: true,+ avatar: None,+ },+ ],+ }),+ };+ store.upsert_chat_notification(&peer, ¬e, None, false).unwrap();++ assert_eq!(store.timeline_total_count().unwrap(), 3);+ assert_eq!(store.timeline_count_by_kind(TimelineKind::File).unwrap(), 2);+ assert_eq!(store.timeline_count_by_kind(TimelineKind::Link).unwrap(), 1);+ assert_eq!(store.chat_cache_counts().unwrap(), (1, 2));++ // Deleting only File timeline entries leaves Link entries intact.+ assert_eq!(store.delete_timeline_kind(TimelineKind::File).unwrap(), 2);+ assert_eq!(store.timeline_count_by_kind(TimelineKind::File).unwrap(), 0);+ assert_eq!(store.timeline_total_count().unwrap(), 1);++ // Clear all history and cache wipes timeline + chat tables while preserving peers, toggles, and meta.+ store.clear_all_history_and_cache().unwrap();+ assert_eq!(store.timeline_total_count().unwrap(), 0);+ assert_eq!(store.chat_cache_counts().unwrap(), (0, 0));+ assert_eq!(store.list_peers().unwrap().len(), 1);+ assert_eq!(store.toggles(&peer).unwrap().get("clipboard"), Some(&false));+ assert_eq!(store.timeline_retention().unwrap().max_days, 30);+ }}diff --git a/core/nectarlink-core/src/transfer.rs b/core/nectarlink-core/src/transfer.rsindex 8033176..2cc6a1b 100644--- a/core/nectarlink-core/src/transfer.rs+++ b/core/nectarlink-core/src/transfer.rs@@ -72,6 +72,9 @@ pub enum TransferFailure {#[derive(Debug, Clone, PartialEq, Eq)]pub enum TransferState {+ /// An incoming transfer waiting for the user to accept or decline+ /// (e.g. from a nearby LocalSend device).+ Requested,/// Waiting for the other device: to connect (sending), or to continue/// after the connection dropped (receiving; the sender resumes it).Waiting,@@ -210,7 +213,7 @@ fn items(entries: &[FileEntry]) -> Vec<String> {/// A name Windows can store: no reserved device names (`CON`, `COM1`…),/// characters it rejects, or trailing dots and spaces.-fn storable_name(name: &str) -> String {+pub(crate) fn storable_name(name: &str) -> String {let mut clean: String = name.chars().map(|c| if matches!(c, '<' | '>' | ':' | '"' | '|' | '?' | '*') { '_' } else { c })@@ -316,7 +319,7 @@ fn take_photo_transfer(transfer_id: &str) -> bool {.is_some_and(|set| set.remove(transfer_id))}-fn transfer_timeline_title(names: &[String]) -> String {+pub(crate) fn transfer_timeline_title(names: &[String]) -> String {match names.len() {0 => "Files".into(),1 => names[0].clone(),@@ -399,7 +402,7 @@ fn sanitize_markers(markers: Vec<RecordingMarker>) -> Vec<RecordingMarker> {.collect()}-fn outgoing_target_path(path: &Path, folder: Option<&str>) -> String {+pub(crate) fn outgoing_target_path(path: &Path, folder: Option<&str>) -> String {if let Some(f) = folder {let depth = f.split('/').filter(|s| !s.is_empty()).count();let mut p = path;@@ -426,6 +429,9 @@ async fn send_inner(return Err(Error::Protocol("send between 1 and 5000 files".into()));}if !shared.store.is_paired(&peer)? {+ if !recording && !is_photo && shared.localsend.has_peer(&peer) {+ return crate::localsend::send_files(shared, peer, files).await;+ }return Err(Error::NotPaired);}let toggle = if recording { RECORDINGS_TOGGLE } else { TOGGLE };@@ -743,6 +749,13 @@ pub(crate) async fn accept_stream({crate::webcam::receive(shared, session.peer, send, recv).await;}+ Some(h)+ if h.svc == nectarlink_protocol::messages::continuity_camera::SERVICE+ && h.op == nectarlink_protocol::messages::continuity_camera::OP_RESULT+ && h.v == nectarlink_protocol::messages::continuity_camera::VERSION =>+ {+ crate::continuity_camera::receive_result(shared, session.peer, send, recv).await;+ }Some(h)if h.svc == nectarlink_protocol::messages::remote::SERVICE&& h.op == nectarlink_protocol::messages::remote::OP_MOTIONdiff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rsindex 8f2f450..ab33a93 100644--- a/core/nectarlink-core/tests/two_devices.rs+++ b/core/nectarlink-core/tests/two_devices.rs@@ -20,6 +20,8 @@ use tokio::sync::broadcast::Receiver;const WAIT: Duration = Duration::from_secs(20);+type MmsRecord = (Vec<String>, String, Vec<nectarlink_core::SmsAttachment>);+#[derive(Debug, Default)]struct RecordingPlatform {rings: Mutex<Vec<bool>>,@@ -33,11 +35,13 @@ struct RecordingPlatform {calls: Mutex<Vec<(String, nectarlink_core::CallCommand)>>,dialed: Mutex<Vec<String>>,texts: Mutex<Vec<(Vec<String>, String)>>,+ mms_sent: Mutex<Vec<MmsRecord>>,mirror_asks: Mutex<Vec<String>>,/// What a PC got of a phone's screen.screen: Arc<ScreenSink>,webcam_asks: Mutex<Vec<String>>,webcam: Arc<TestWebcamSink>,+ camera_asks: Mutex<Vec<String>>,/// Photos `open_photo` finds, by ID.photos: Mutex<std::collections::HashMap<String, std::path::PathBuf>>,/// Optional barrier `open_photo` waits on to simulate a slow platform call.@@ -159,6 +163,17 @@ impl Platform for RecordingPlatform {fn webcam_keyframe_requested(&self, _peer: &nectarlink_core::DeviceId) {self.webcam_asks.lock().unwrap().push("keyframe".into());}+ fn camera_capture_requested(+ &self,+ _peer: &nectarlink_core::DeviceId,+ request: &nectarlink_core::CameraCaptureRequest,+ ) -> Result<(), String> {+ self.camera_asks.lock().unwrap().push(format!("request {} {:?}", request.request_id, request.mode));+ Ok(())+ }+ fn camera_capture_cancelled(&self, _peer: &nectarlink_core::DeviceId, request_id: &str) {+ self.camera_asks.lock().unwrap().push(format!("cancel {request_id}"));+ }fn phone_apps(&self) -> Result<Vec<nectarlink_core::PhoneApp>, String> {let app = |pkg: &str, label: &str, icon: usize| nectarlink_core::PhoneApp {pkg: pkg.into(),@@ -209,6 +224,18 @@ impl Platform for RecordingPlatform {self.texts.lock().unwrap().push((to.to_vec(), body.to_owned()));Ok(())}+ fn send_sms_with_attachments(+ &self,+ to: &[String],+ body: &str,+ attachments: &[nectarlink_core::SmsAttachment],+ ) -> Result<(), String> {+ if attachments.is_empty() {+ return self.sms_send(to, body);+ }+ self.mms_sent.lock().unwrap().push((to.to_vec(), body.to_owned(), attachments.to_vec()));+ Ok(())+ }fn sms_part(&self, id: &str) -> Result<(String, Vec<u8>), String> {if id == "p1" { Ok(("image/jpeg".into(), vec![1, 2, 3])) } else { Err("gone".into()) }}@@ -996,6 +1023,7 @@ fn note(key: &str, app: &str, title: &str, icon: bool) -> Notification {icon: icon.then(|| vec![0x89, b'P', b'N', b'G', 1, 2, 3]),image: None,live: None,+ conversation: None,}}@@ -3522,3 +3550,799 @@ async fn timeline_records_files_clips_links_photos_recordings_and_sessions_witho.unwrap();assert!(after_del.entries.is_empty(), "deleting clip removes linked timeline entry");}++#[tokio::test(flavor = "multi_thread")]+async fn messaging_style_notifications_populate_unified_chat_threads_and_reply() {+ use nectarlink_core::{NotificationChatMessage, NotificationConversation};++ let mut pc = device("Desktop", DeviceKind::Desktop).await;+ let mut phone = device("Pixel", DeviceKind::Phone).await;+ pair_qr(&mut pc, &mut phone).await;+ let phone_id = phone.node.device_id();+ grant_notification_access(&phone).await;++ let now_ms = 1_760_000_000_000i64;+ let mut wa_note = note("wa|maya", "com.whatsapp", "Maya Lin", true);+ wa_note.app_name = "WhatsApp".into();+ wa_note.when = now_ms - 60_000;+ wa_note.conversation = Some(NotificationConversation {+ v: 1,+ title: "Maya Lin".into(),+ group: false,+ avatar: Some(vec![0xff, 0xd8, 1, 2]),+ messages: vec![+ NotificationChatMessage {+ sender: Some("Maya Lin".into()),+ text: "Are you free for a quick sync?".into(),+ time: now_ms - 120_000,+ self_sent: false,+ avatar: None,+ },+ NotificationChatMessage {+ sender: Some("Maya Lin".into()),+ text: "Sent over the deck too".into(),+ time: now_ms - 60_000,+ self_sent: false,+ avatar: None,+ },+ ],+ });++ phone.node.notification_posted(wa_note).await;+ let changed_thread = wait_for(&mut pc, "chat thread updated from notification", |e| match e {+ NodeEvent::SmsChanged { device, thread: Some(tid) }+ if *device == phone_id && tid.starts_with("chat:") =>+ {+ Some(tid.clone())+ }+ _ => None,+ })+ .await;++ let threads = pc.node.chat_threads(phone_id);+ assert_eq!(threads.len(), 1);+ assert_eq!(threads[0].thread_id, changed_thread);+ assert_eq!(threads[0].app, "com.whatsapp");+ assert_eq!(threads[0].app_name, "WhatsApp");+ assert_eq!(threads[0].title, "Maya Lin");+ assert!(!threads[0].is_group);+ assert_eq!(threads[0].unread, 2);+ assert!(threads[0].can_reply());+ assert!(threads[0].app_icon.is_some());+ assert!(threads[0].avatar.is_some());++ let msgs = pc.node.chat_messages(phone_id, &changed_thread, 20);+ assert_eq!(msgs.len(), 2);+ assert_eq!(msgs[0].text, "Sent over the deck too");+ assert_eq!(msgs[1].text, "Are you free for a quick sync?");++ // Reply directly from the unified conversation thread.+ with_timeout(+ "reply_chat_thread",+ pc.node.reply_chat_thread(phone_id, &changed_thread, "Give me 5 minutes!".into()),+ )+ .await+ .unwrap();+ assert_eq!(+ *phone.platform.actions.lock().unwrap(),+ vec![("wa|maya".into(), "0".into(), Some("Give me 5 minutes!".into()))]+ );++ let after_reply = pc.node.chat_thread(phone_id, &changed_thread).unwrap();+ assert_eq!(after_reply.unread, 0);+ assert_eq!(after_reply.snippet, "You: Give me 5 minutes!");+ let msgs = pc.node.chat_messages(phone_id, &changed_thread, 20);+ assert_eq!(msgs.len(), 3);+ assert_eq!(msgs[0].text, "Give me 5 minutes!");+ assert!(msgs[0].self_sent);++ // Removing the notification disables inline reply while keeping local history.+ phone.node.notification_removed("wa|maya".into()).await;+ wait_for(&mut pc, "notification removed", |e| match e {+ NodeEvent::NotificationRemoved { device, key } if *device == phone_id && key == "wa|maya" => Some(()),+ _ => None,+ })+ .await;+ let after_removed = pc.node.chat_thread(phone_id, &changed_thread).unwrap();+ assert!(!after_removed.can_reply());+ assert!(matches!(+ pc.node.reply_chat_thread(phone_id, &changed_thread, "Still there?".into()).await,+ Err(Error::NotFound)+ ));+}++#[tokio::test(flavor = "multi_thread")]+async fn mms_image_attachments_send_over_sms_service() {+ use nectarlink_core::{SMS_MAX_ATTACHMENT_BYTES, SmsAttachment};++ let mut pc = device_with("Desktop", DeviceKind::Desktop, &[nectarlink_core::SMS_SHOW]).await;+ let mut phone =+ device_with("Pixel", DeviceKind::Phone, &[nectarlink_core::SMS_READ, nectarlink_core::SMS_SEND])+ .await;+ pair_qr(&mut pc, &mut phone).await;+ let phone_id = phone.node.device_id();++ let jpeg = SmsAttachment { mime: "image/jpeg".into(), data: vec![0xff, 0xd8, 1, 2, 3, 4] };+ let png = SmsAttachment { mime: "image/png".into(), data: vec![0x89, b'P', b'N', b'G', 5, 6] };++ // Send MMS with both caption and a JPEG attachment.+ with_timeout(+ "send mms with caption",+ pc.node.send_sms_with_attachments(+ phone_id,+ vec!["+15550100".into()],+ "Check this out".into(),+ vec![jpeg.clone()],+ ),+ )+ .await+ .unwrap();++ // Send image-only MMS (empty body, PNG attachment).+ with_timeout(+ "send image-only mms",+ pc.node.send_sms_with_attachments(+ phone_id,+ vec!["+15550188".into()],+ String::new(),+ vec![png.clone()],+ ),+ )+ .await+ .unwrap();++ let sent = phone.platform.mms_sent.lock().unwrap().clone();+ assert_eq!(sent.len(), 2);+ assert_eq!(sent[0].0, vec!["+15550100".to_owned()]);+ assert_eq!(sent[0].1, "Check this out");+ assert_eq!(sent[0].2, vec![jpeg.clone()]);+ assert_eq!(sent[1].0, vec!["+15550188".to_owned()]);+ assert_eq!(sent[1].1, "");+ assert_eq!(sent[1].2, vec![png.clone()]);++ // Unsupported MIME type is rejected as a protocol error; oversized or too many attachments return TooLarge.+ let bad_mime = SmsAttachment { mime: "image/gif".into(), data: vec![1, 2, 3] };+ assert!(matches!(+ pc.node+ .send_sms_with_attachments(phone_id, vec!["+15550100".into()], String::new(), vec![bad_mime])+ .await,+ Err(Error::Protocol(_))+ ));+ assert!(matches!(+ pc.node+ .send_sms_with_attachments(phone_id, vec!["+15550100".into()], String::new(), vec![jpeg, png])+ .await,+ Err(Error::TooLarge)+ ));+ let oversized =+ SmsAttachment { mime: "image/jpeg".into(), data: vec![0u8; SMS_MAX_ATTACHMENT_BYTES + 1] };+ assert!(matches!(+ pc.node+ .send_sms_with_attachments(phone_id, vec!["+15550100".into()], String::new(), vec![oversized])+ .await,+ Err(Error::TooLarge)+ ));+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn continuity_camera_photo_and_scan_flow() {+ use nectarlink_core::{CameraCaptureMode, CameraCaptureResultMeta};++ let mut pc = device_with("pc", DeviceKind::Desktop, &["camera.continuity"]).await;+ let mut phone = device_with("phone", DeviceKind::Phone, &["camera.capture"]).await;+ pair_qr(&mut pc, &mut phone).await;+ let (pc_id, phone_id) = (pc.node.device_id(), phone.node.device_id());++ assert_eq!(+ pc.node.capabilities(phone_id).unwrap().state("camera.continuity"),+ Some(FeatureState::Available)+ );++ // 1. Request a photo capture from PC.+ with_timeout(+ "request photo capture",+ pc.node.request_camera_capture(phone_id, "req-photo-1".into(), CameraCaptureMode::Photo),+ )+ .await+ .unwrap();++ let (req_peer, req_id, req_mode) = wait_for(&mut phone, "photo capture requested", |e| match e {+ NodeEvent::CameraCaptureRequested { device, request_id, mode } => {+ Some((*device, request_id.clone(), *mode))+ }+ _ => None,+ })+ .await;+ assert_eq!(req_peer, pc_id);+ assert_eq!(req_id, "req-photo-1");+ assert_eq!(req_mode, CameraCaptureMode::Photo);++ let fake_jpeg = vec![0xFF, 0xD8, 0xFF, 0xE0, 1, 2, 3, 4, 0xFF, 0xD9];+ let photo_meta = CameraCaptureResultMeta {+ request_id: "req-photo-1".into(),+ mode: CameraCaptureMode::Photo,+ file_name: "IMG_20260409.jpg".into(),+ mime: "image/jpeg".into(),+ size: fake_jpeg.len() as u64,+ width: 1920,+ height: 1080,+ };+ with_timeout(+ "send photo capture result",+ phone.node.send_camera_capture_result(pc_id, photo_meta, fake_jpeg.clone()),+ )+ .await+ .unwrap();++ let (recv_peer, recv_id, recv_mode, recv_name, recv_mime, recv_w, recv_h, recv_data, saved_path) =+ wait_for(&mut pc, "photo capture received", |e| match e {+ NodeEvent::CameraCaptureReceived {+ device,+ request_id,+ mode,+ file_name,+ mime,+ width,+ height,+ data,+ saved_path,+ } => Some((+ *device,+ request_id.clone(),+ *mode,+ file_name.clone(),+ mime.clone(),+ *width,+ *height,+ data.clone(),+ saved_path.clone(),+ )),+ _ => None,+ })+ .await;+ assert_eq!(recv_peer, phone_id);+ assert_eq!(recv_id, "req-photo-1");+ assert_eq!(recv_mode, CameraCaptureMode::Photo);+ assert_eq!(recv_name, "IMG_20260409.jpg");+ assert_eq!(recv_mime, "image/jpeg");+ assert_eq!(recv_w, 1920);+ assert_eq!(recv_h, 1080);+ assert_eq!(recv_data, fake_jpeg);+ let path = saved_path.expect("saved to downloads_dir");+ assert_eq!(std::fs::read(&path).unwrap(), fake_jpeg);++ // 2. Request a document scan from PC.+ with_timeout(+ "request scan capture",+ pc.node.request_camera_capture(phone_id, "req-scan-2".into(), CameraCaptureMode::Scan),+ )+ .await+ .unwrap();++ let (_, scan_id, scan_mode) = wait_for(&mut phone, "scan capture requested", |e| match e {+ NodeEvent::CameraCaptureRequested { device, request_id, mode } => {+ Some((*device, request_id.clone(), *mode))+ }+ _ => None,+ })+ .await;+ assert_eq!(scan_id, "req-scan-2");+ assert_eq!(scan_mode, CameraCaptureMode::Scan);++ let fake_scan = vec![0xFF, 0xD8, 0xFF, 0xDB, 9, 8, 7, 6, 5, 0xFF, 0xD9];+ let scan_meta = CameraCaptureResultMeta {+ request_id: "req-scan-2".into(),+ mode: CameraCaptureMode::Scan,+ file_name: "Scan_20260409.jpg".into(),+ mime: "image/jpeg".into(),+ size: fake_scan.len() as u64,+ width: 1654,+ height: 2339,+ };+ with_timeout(+ "send scan capture result",+ phone.node.send_camera_capture_result(pc_id, scan_meta, fake_scan.clone()),+ )+ .await+ .unwrap();++ let (_, got_scan_id, got_scan_mode, got_scan_name, _, got_w, got_h, got_scan_data, got_scan_path) =+ wait_for(&mut pc, "scan capture received", |e| match e {+ NodeEvent::CameraCaptureReceived {+ device,+ request_id,+ mode,+ file_name,+ mime,+ width,+ height,+ data,+ saved_path,+ } => Some((+ *device,+ request_id.clone(),+ *mode,+ file_name.clone(),+ mime.clone(),+ *width,+ *height,+ data.clone(),+ saved_path.clone(),+ )),+ _ => None,+ })+ .await;+ assert_eq!(got_scan_id, "req-scan-2");+ assert_eq!(got_scan_mode, CameraCaptureMode::Scan);+ assert_eq!(got_scan_name, "Scan_20260409.jpg");+ assert_eq!(got_w, 1654);+ assert_eq!(got_h, 2339);+ assert_eq!(got_scan_data, fake_scan);+ assert!(got_scan_path.unwrap().exists());++ pc.node.shutdown().await;+ phone.node.shutdown().await;+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn continuity_camera_cancel_propagates() {+ use nectarlink_core::CameraCaptureMode;++ let mut pc = device_with("pc", DeviceKind::Desktop, &["camera.continuity"]).await;+ let mut phone = device_with("phone", DeviceKind::Phone, &["camera.capture"]).await;+ pair_qr(&mut pc, &mut phone).await;+ let (pc_id, phone_id) = (pc.node.device_id(), phone.node.device_id());++ // Phone cancels an active request -> PC receives CameraCaptureCancelled.+ with_timeout(+ "request capture 1",+ pc.node.request_camera_capture(phone_id, "req-cancel-1".into(), CameraCaptureMode::Photo),+ )+ .await+ .unwrap();+ wait_for(&mut phone, "capture 1 requested", |e| match e {+ NodeEvent::CameraCaptureRequested { request_id, .. } if request_id == "req-cancel-1" => Some(()),+ _ => None,+ })+ .await;++ with_timeout(+ "phone cancels capture 1",+ phone.node.cancel_camera_capture(pc_id, "req-cancel-1".into(), Some("user dismissed".into())),+ )+ .await+ .unwrap();++ let (from, id, reason) = wait_for(&mut pc, "pc sees cancel 1", |e| match e {+ NodeEvent::CameraCaptureCancelled { device, request_id, reason } => {+ Some((*device, request_id.clone(), reason.clone()))+ }+ _ => None,+ })+ .await;+ assert_eq!(from, phone_id);+ assert_eq!(id, "req-cancel-1");+ assert_eq!(reason.as_deref(), Some("user dismissed"));++ // PC cancels an active request -> Phone receives CameraCaptureCancelled.+ with_timeout(+ "request capture 2",+ pc.node.request_camera_capture(phone_id, "req-cancel-2".into(), CameraCaptureMode::Scan),+ )+ .await+ .unwrap();+ wait_for(&mut phone, "capture 2 requested", |e| match e {+ NodeEvent::CameraCaptureRequested { request_id, .. } if request_id == "req-cancel-2" => Some(()),+ _ => None,+ })+ .await;++ with_timeout(+ "pc cancels capture 2",+ pc.node.cancel_camera_capture(phone_id, "req-cancel-2".into(), Some("cancelled on PC".into())),+ )+ .await+ .unwrap();++ let (from2, id2, reason2) = wait_for(&mut phone, "phone sees cancel 2", |e| match e {+ NodeEvent::CameraCaptureCancelled { device, request_id, reason } => {+ Some((*device, request_id.clone(), reason.clone()))+ }+ _ => None,+ })+ .await;+ assert_eq!(from2, pc_id);+ assert_eq!(id2, "req-cancel-2");+ assert_eq!(reason2.as_deref(), Some("cancelled on PC"));++ pc.node.shutdown().await;+ phone.node.shutdown().await;+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn continuity_camera_respects_photos_toggle() {+ use nectarlink_core::CameraCaptureMode;++ let mut pc = device_with("pc", DeviceKind::Desktop, &["camera.continuity"]).await;+ let mut phone = device_with("phone", DeviceKind::Phone, &["camera.capture"]).await;+ pair_qr(&mut pc, &mut phone).await;+ let (_pc_id, phone_id) = (pc.node.device_id(), phone.node.device_id());++ // Turning off photos toggle on PC blocks outgoing request_camera_capture with Error::Denied.+ pc.node.set_device_toggle(phone_id, "photos", false).unwrap();+ let res =+ pc.node.request_camera_capture(phone_id, "req-denied-pc".into(), CameraCaptureMode::Photo).await;+ assert!(matches!(res, Err(Error::Denied)));++ // Re-enable on PC, but disable photos toggle on phone -> phone replies with cancel.+ pc.node.set_device_toggle(phone_id, "photos", true).unwrap();+ phone.node.set_device_toggle(pc.node.device_id(), "photos", false).unwrap();+ with_timeout(+ "request with phone photos toggle off",+ pc.node.request_camera_capture(phone_id, "req-denied-phone".into(), CameraCaptureMode::Photo),+ )+ .await+ .unwrap();++ let (from, id, reason) = wait_for(&mut pc, "phone cancels because photos off", |e| match e {+ NodeEvent::CameraCaptureCancelled { device, request_id, reason } => {+ Some((*device, request_id.clone(), reason.clone()))+ }+ _ => None,+ })+ .await;+ assert_eq!(from, phone_id);+ assert_eq!(id, "req-denied-phone");+ assert!(reason.unwrap_or_default().contains("photos"));++ pc.node.shutdown().await;+ phone.node.shutdown().await;+}++#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn data_retention_counts_and_clear_all_local_data_preserves_pairing_and_settings() {+ use nectarlink_core::{+ Notification, NotificationAction, NotificationChatMessage, NotificationConversation,+ };++ let mut pc = device_with("pc", DeviceKind::Desktop, &["notify.show", "clip.write"]).await;+ let mut phone = device_with("phone", DeviceKind::Phone, &["notify.mirror", "notify.reply"]).await;+ pair_qr(&mut pc, &mut phone).await;+ let (pc_id, phone_id) = (pc.node.device_id(), phone.node.device_id());++ // Configure non-default retention and a device toggle on PC.+ pc.node+ .set_timeline_retention(nectarlink_core::timeline::TimelineRetention {+ max_days: 30,+ max_entries: 5000,+ })+ .unwrap();+ pc.node.set_device_toggle(phone_id, "clipboard", true).unwrap();++ // Send a clipboard item from phone -> PC.+ with_timeout("send clip", phone.node.send_clipboard(pc_id, "Secret recipe notes".into())).await.unwrap();+ wait_for(&mut pc, "clip history changed", |e| match e {+ NodeEvent::ClipboardHistoryChanged => Some(()),+ _ => None,+ })+ .await;++ // Post a MessagingStyle chat notification from phone -> PC.+ let note = Notification {+ key: "sig|sam".into(),+ app: "org.thoughtcrime.securesms".into(),+ app_name: "Signal".into(),+ title: Some("Sam Rivera".into()),+ text: Some("Dinner at 7?".into()),+ sub: None,+ when: 1_760_000_000_000,+ actions: vec![NotificationAction { id: "reply".into(), title: "Reply".into(), reply: true }],+ silent: false,+ icon: None,+ image: None,+ live: None,+ conversation: Some(NotificationConversation {+ v: 1,+ title: "Sam Rivera".into(),+ group: false,+ avatar: None,+ messages: vec![NotificationChatMessage {+ sender: Some("Sam Rivera".into()),+ text: "Dinner at 7?".into(),+ time: 1_760_000_000_000,+ self_sent: false,+ avatar: None,+ }],+ }),+ };+ phone.node.notification_posted(note).await;+ wait_for(&mut pc, "chat sms changed", |e| match e {+ NodeEvent::SmsChanged { device, .. } if *device == phone_id => Some(()),+ _ => None,+ })+ .await;++ let counts = pc.node.data_retention_counts();+ assert!(counts.clipboard_items >= 1);+ assert!(counts.timeline_items >= 1);+ assert_eq!(counts.timeline_retention_days, 30);+ assert_eq!(counts.chat_threads, 1);+ assert_eq!(counts.chat_messages, 1);++ // Clear everything on PC: all history and caches become 0, while pairing and settings remain intact.+ pc.node.clear_all_local_data().unwrap();+ let after = pc.node.data_retention_counts();+ assert_eq!(after.clipboard_items, 0);+ assert_eq!(after.timeline_items, 0);+ assert_eq!(after.chat_threads, 0);+ assert_eq!(after.chat_messages, 0);+ assert_eq!(after.received_file_records, 0);+ assert_eq!(after.timeline_retention_days, 30, "timeline retention setting must be preserved");+ assert_eq!(pc.node.paired_devices().unwrap().len(), 1, "paired devices must never be deleted");+ assert!(+ pc.node.device_toggles(phone_id).unwrap().into_iter().any(|(k, v)| k == "clipboard" && v),+ "per-device toggles must be preserved"+ );++ pc.node.shutdown().await;+ phone.node.shutdown().await;+}++fn free_tcp_port() -> u16 {+ std::net::TcpListener::bind("127.0.0.1:0")+ .expect("bind ephemeral port")+ .local_addr()+ .expect("local addr")+ .port()+}++async fn start_localsend_in(dir: TempDir, name: &'static str, kind: DeviceKind, port: u16) -> TestDevice {+ init_logging();+ let mut config = NodeConfig::new(dir.path(), info(name, kind), "0.0.1-test");+ config.lan_discovery = false;+ config.away_mode = false;+ config.localsend_port = port;+ config.key_protector = Some(Arc::new(PlainKeyProtector));+ config.capabilities = vec!["media.control".into()];+ let platform = Arc::new(RecordingPlatform::default());+ let node = Node::start(config, platform.clone()).await.expect("node starts");+ let events = node.events();+ TestDevice { node, events, platform, dir, name }+}++#[tokio::test]+async fn localsend_is_off_by_default_and_persists_setting() {+ let dir = tempfile::tempdir().unwrap();+ let port1 = free_tcp_port();+ let mut dev = start_localsend_in(dir, "PC", DeviceKind::Desktop, port1).await;++ assert!(!dev.node.localsend_enabled());+ assert!(!dev.node.localsend_receiving());+ assert!(dev.node.localsend_peers().is_empty());++ dev.node.set_localsend_enabled(true).unwrap();+ wait_for(&mut dev, "localsend enabled event", |e| match e {+ NodeEvent::LocalSendChanged => Some(()),+ _ => None,+ })+ .await;+ assert!(dev.node.localsend_enabled());+ // Wait up to 2s for the background TCP listener to bind and set receiving=true.+ for _ in 0..40 {+ if dev.node.localsend_receiving() {+ break;+ }+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ assert!(dev.node.localsend_receiving());++ let dir = dev.dir;+ dev.node.shutdown().await;++ let port2 = free_tcp_port();+ let dev2 = start_localsend_in(dir, "PC", DeviceKind::Desktop, port2).await;+ assert!(dev2.node.localsend_enabled(), "enabled state must persist across restarts");+ for _ in 0..40 {+ if dev2.node.localsend_receiving() {+ break;+ }+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ assert!(dev2.node.localsend_receiving(), "server must start automatically when persisted enabled");+ dev2.node.shutdown().await;+}++#[tokio::test]+async fn localsend_coexists_when_port_is_already_bound() {+ let occupied = std::net::TcpListener::bind(("0.0.0.0", 0)).expect("bind occupied port");+ let port = occupied.local_addr().unwrap().port();++ let dir = tempfile::tempdir().unwrap();+ let dev = start_localsend_in(dir, "PC", DeviceKind::Desktop, port).await;+ dev.node.set_localsend_enabled(true).unwrap();+ tokio::time::sleep(Duration::from_millis(150)).await;++ assert!(dev.node.localsend_enabled());+ assert!(+ !dev.node.localsend_receiving(),+ "when port is occupied by standalone LocalSend, Nectarlink must leave receiving to it without failing"+ );++ dev.node.shutdown().await;+ drop(occupied);+}++#[tokio::test]+async fn localsend_discovers_peer_requires_accept_and_transfers_files_over_https() {+ let sender_port = free_tcp_port();+ let receiver_port = free_tcp_port();+ let mut sender =+ start_localsend_in(tempfile::tempdir().unwrap(), "Phone Sender", DeviceKind::Phone, sender_port)+ .await;+ let mut receiver =+ start_localsend_in(tempfile::tempdir().unwrap(), "PC Receiver", DeviceKind::Desktop, receiver_port)+ .await;++ sender.node.set_localsend_enabled(true).unwrap();+ receiver.node.set_localsend_enabled(true).unwrap();+ for _ in 0..40 {+ if receiver.node.localsend_receiving() {+ break;+ }+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ assert!(receiver.node.localsend_receiving());++ let peer = sender+ .node+ .probe_localsend_peer("127.0.0.1", receiver_port)+ .await+ .expect("probe receiver over HTTPS");+ assert_eq!(peer.alias, "PC Receiver");+ assert_eq!(peer.port, receiver_port);+ assert_eq!(peer.protocol, "https");+ assert_eq!(peer.device_type, "desktop");+ assert!(sender.node.localsend_peers().iter().any(|p| p.id == peer.id));++ let src_dir = sender.dir.path().join("src_files");+ std::fs::create_dir_all(src_dir.join("sub")).unwrap();+ let file_a = src_dir.join("notes.txt");+ let file_b = src_dir.join("sub").join("hello.txt");+ std::fs::write(&file_a, b"LocalSend interop over HTTPS!").unwrap();+ std::fs::write(&file_b, b"Nested folder payload").unwrap();++ let out_id = sender+ .node+ .send_files(+ peer.id,+ vec![+ OutgoingFile { name: "notes.txt".into(), folder: None, source: FileSource::Path(file_a) },+ OutgoingFile {+ name: "hello.txt".into(),+ folder: Some("sub".into()),+ source: FileSource::Path(file_b),+ },+ ],+ )+ .await+ .expect("start LocalSend upload");++ let req: Transfer = wait_for(&mut receiver, "incoming LocalSend transfer request", |e| match e {+ NodeEvent::Transfer(t)+ if t.direction == Direction::Incoming && t.state == TransferState::Requested =>+ {+ Some(t.clone())+ }+ _ => None,+ })+ .await;+ assert_eq!(req.files, 2);+ assert!(+ !receiver.dir.path().join("received").join("notes.txt").exists(),+ "unpaired LocalSend transfer must wait for user acceptance before writing files"+ );++ receiver.node.accept_transfer(&req.id);++ wait_for(&mut sender, "sender LocalSend transfer done", |e| match e {+ NodeEvent::Transfer(t) if t.id == out_id && matches!(t.state, TransferState::Done { .. }) => Some(()),+ _ => None,+ })+ .await;+ wait_for(&mut receiver, "receiver LocalSend transfer done", |e| match e {+ NodeEvent::Transfer(t) if t.id == req.id && matches!(t.state, TransferState::Done { .. }) => Some(()),+ _ => None,+ })+ .await;++ let dl_a = receiver.dir.path().join("received").join("notes.txt");+ let dl_b = receiver.dir.path().join("received").join("sub").join("hello.txt");+ assert_eq!(std::fs::read(&dl_a).unwrap(), b"LocalSend interop over HTTPS!");+ assert_eq!(std::fs::read(&dl_b).unwrap(), b"Nested folder payload");++ assert!(+ !sender+ .node+ .timeline_page(&nectarlink_core::timeline::TimelineQuery::default())+ .unwrap()+ .entries+ .is_empty()+ );+ assert!(+ !receiver+ .node+ .timeline_page(&nectarlink_core::timeline::TimelineQuery::default())+ .unwrap()+ .entries+ .is_empty()+ );++ sender.node.shutdown().await;+ receiver.node.shutdown().await;+}++#[tokio::test]+async fn localsend_declining_transfer_returns_denied_to_sender() {+ let sender_port = free_tcp_port();+ let receiver_port = free_tcp_port();+ let mut sender =+ start_localsend_in(tempfile::tempdir().unwrap(), "Phone Sender", DeviceKind::Phone, sender_port)+ .await;+ let mut receiver =+ start_localsend_in(tempfile::tempdir().unwrap(), "PC Receiver", DeviceKind::Desktop, receiver_port)+ .await;++ sender.node.set_localsend_enabled(true).unwrap();+ receiver.node.set_localsend_enabled(true).unwrap();+ for _ in 0..40 {+ if receiver.node.localsend_receiving() {+ break;+ }+ tokio::time::sleep(Duration::from_millis(50)).await;+ }++ let peer = sender.node.probe_localsend_peer("127.0.0.1", receiver_port).await.unwrap();++ let file_a = sender.dir.path().join("reject_me.txt");+ std::fs::write(&file_a, b"do not accept").unwrap();++ let out_id = sender+ .node+ .send_files(+ peer.id,+ vec![OutgoingFile {+ name: "reject_me.txt".into(),+ folder: None,+ source: FileSource::Path(file_a),+ }],+ )+ .await+ .unwrap();++ let req: Transfer = wait_for(&mut receiver, "incoming LocalSend transfer request", |e| match e {+ NodeEvent::Transfer(t)+ if t.direction == Direction::Incoming && t.state == TransferState::Requested =>+ {+ Some(t.clone())+ }+ _ => None,+ })+ .await;++ receiver.node.cancel_transfer(&req.id);++ wait_for(&mut sender, "sender sees Denied failure", |e| match e {+ NodeEvent::Transfer(t)+ if t.id == out_id && t.state == TransferState::Failed(TransferFailure::Denied) =>+ {+ Some(())+ }+ _ => None,+ })+ .await;++ assert!(!receiver.dir.path().join("received").join("reject_me.txt").exists());++ sender.node.shutdown().await;+ receiver.node.shutdown().await;+}diff --git a/core/nectarlink-ffi/src/lib.rs b/core/nectarlink-ffi/src/lib.rsindex 30841dd..5069f42 100644--- a/core/nectarlink-ffi/src/lib.rs+++ b/core/nectarlink-ffi/src/lib.rs@@ -132,6 +132,19 @@ pub struct DiscoveredDevice {pub name: Option<String>,}+/// A LocalSend device discovered on the local network (`docs/protocol/v0.md`).+#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]+pub struct LocalSendPeer {+ pub id: String,+ pub alias: String,+ pub device_model: Option<String>,+ pub device_type: String,+ pub fingerprint: String,+ pub ip: String,+ pub port: u16,+ pub protocol: String,+}+#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)]pub enum PairingFailure {Rejected,@@ -259,6 +272,63 @@ impl std::fmt::Debug for TaskNotify {}}+/// One chat message inside a `MessagingStyle` notification conversation.+#[derive(Clone, PartialEq, Eq, uniffi::Record)]+pub struct NotificationChatMessage {+ /// Sender display name (`None` for the user's own messages or 1:1 chats+ /// where the conversation title is the sender).+ #[uniffi(default = None)]+ pub sender: Option<String>,+ pub text: String,+ /// Unix milliseconds.+ pub time: i64,+ /// True when sent by the phone user.+ #[uniffi(default = false)]+ pub self_sent: bool,+ /// Optional sender avatar (JPEG/PNG, at most 16 KiB).+ #[uniffi(default = None)]+ pub avatar: Option<Vec<u8>>,+}++/// Never prints message text or sender names (protocol v0 §11).+impl std::fmt::Debug for NotificationChatMessage {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("NotificationChatMessage")+ .field("time", &self.time)+ .field("self_sent", &self.self_sent)+ .finish_non_exhaustive()+ }+}++/// Structured `MessagingStyle` conversation metadata carried on a notification.+#[derive(Clone, PartialEq, Eq, uniffi::Record)]+pub struct NotificationConversation {+ /// Schema version (`1`).+ #[uniffi(default = 1)]+ pub v: u32,+ /// Conversation or sender display title.+ pub title: String,+ /// True for group conversations (`MessagingStyle.isGroupConversation`).+ #[uniffi(default = false)]+ pub group: bool,+ /// Optional conversation/sender avatar (JPEG/PNG, at most 16 KiB).+ #[uniffi(default = None)]+ pub avatar: Option<Vec<u8>>,+ /// Chronological messages in the notification (oldest first).+ pub messages: Vec<NotificationChatMessage>,+}++/// Never prints conversation title or messages (protocol v0 §11).+impl std::fmt::Debug for NotificationConversation {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("NotificationConversation")+ .field("v", &self.v)+ .field("group", &self.group)+ .field("messages_len", &self.messages.len())+ .finish_non_exhaustive()+ }+}+/// A notification, as mirrored between devices/// (docs/protocol/notifications.md).#[derive(Clone, PartialEq, Eq, uniffi::Record)]@@ -285,6 +355,9 @@ pub struct Notification {/// Live Update / ongoing progress or timer metadata, when present.#[uniffi(default = None)]pub live: Option<NotificationLive>,+ /// Structured `MessagingStyle` conversation metadata, when present.+ #[uniffi(default = None)]+ pub conversation: Option<NotificationConversation>,}/// Something playing (or paused) on a device (docs/protocol/media.md).@@ -401,6 +474,8 @@ pub enum TransferDirection {#[derive(Debug, Clone, PartialEq, Eq, uniffi::Enum)]pub enum TransferStatus {+ /// Incoming transfer waiting for the user to accept or decline (LocalSend).+ Requested,/// Waiting for the other device (to connect, or to continue).Waiting,Running,@@ -633,6 +708,8 @@ pub enum Event {},/// The local cross-device timeline changed.TimelineChanged,+ /// The LocalSend compatibility setting or discovered LocalSend peers changed.+ LocalSendChanged,}/// One tile on a PC's Deck (`docs/protocol/deck.md`).@@ -1056,6 +1133,13 @@ pub struct SmsPartData {pub data: Vec<u8>,}+/// An outgoing MMS image attachment (`image/jpeg` or `image/png`).+#[derive(Clone, PartialEq, Eq, uniffi::Record)]+pub struct SmsAttachment {+ pub mime: String,+ pub data: Vec<u8>,+}+macro_rules! private_debug {($($t:ty),*) => {$(/// Never prints numbers, names or text (protocol v0 §11).@@ -1066,7 +1150,7 @@ macro_rules! private_debug {})*};}-private_debug!(SmsThread, SmsMessage, SmsPartData, CallLogEntry, ContactNumber, Contact);+private_debug!(SmsThread, SmsMessage, SmsPartData, SmsAttachment, CallLogEntry, ContactNumber, Contact);/// One call in this phone's call history (docs/protocol/calls.md).#[derive(Clone, PartialEq, Eq, uniffi::Record)]@@ -1411,6 +1495,32 @@ impl From<core::TimelineRetention> for TimelineRetention {}}+/// Summary of locally stored history and cache counts for Privacy & Data settings.+#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]+pub struct DataRetentionCounts {+ pub clipboard_items: u32,+ pub clipboard_bytes: u64,+ pub timeline_items: u32,+ pub timeline_retention_days: u32,+ pub chat_threads: u32,+ pub chat_messages: u32,+ pub received_file_records: u32,+}++impl From<core::DataRetentionCounts> for DataRetentionCounts {+ fn from(c: core::DataRetentionCounts) -> Self {+ DataRetentionCounts {+ clipboard_items: c.clipboard_items,+ clipboard_bytes: c.clipboard_bytes,+ timeline_items: c.timeline_items,+ timeline_retention_days: c.timeline_retention_days,+ chat_threads: c.chat_threads,+ chat_messages: c.chat_messages,+ received_file_records: c.received_file_records,+ }+ }+}+private_debug!(PhotoAlbum, PhotoItem, PhotoThumb, StorageEntry, ClipboardHistoryEntry, TimelineEntry);#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, uniffi::Error)]@@ -1650,6 +1760,21 @@ impl From<core::DiscoveredDevice> for DiscoveredDevice {}}+impl From<core::LocalSendPeer> for LocalSendPeer {+ fn from(p: core::LocalSendPeer) -> Self {+ LocalSendPeer {+ id: p.id.to_string(),+ alias: p.alias,+ device_model: p.device_model,+ device_type: p.device_type,+ fingerprint: p.fingerprint,+ ip: p.ip,+ port: p.port,+ protocol: p.protocol,+ }+ }+}+impl From<core::PairingFailure> for PairingFailure {fn from(f: core::PairingFailure) -> Self {match f {@@ -1742,6 +1867,66 @@ impl From<TaskNotify> for core::TaskNotify {}}+impl From<core::NotificationChatMessage> for NotificationChatMessage {+ fn from(m: core::NotificationChatMessage) -> Self {+ NotificationChatMessage {+ sender: m.sender,+ text: m.text,+ time: m.time,+ self_sent: m.self_sent,+ avatar: m.avatar,+ }+ }+}++impl From<NotificationChatMessage> for core::NotificationChatMessage {+ fn from(m: NotificationChatMessage) -> Self {+ core::NotificationChatMessage {+ sender: m.sender,+ text: m.text,+ time: m.time,+ self_sent: m.self_sent,+ avatar: m.avatar,+ }+ }+}++impl From<core::NotificationConversation> for NotificationConversation {+ fn from(c: core::NotificationConversation) -> Self {+ NotificationConversation {+ v: c.v,+ title: c.title,+ group: c.group,+ avatar: c.avatar,+ messages: c.messages.into_iter().map(Into::into).collect(),+ }+ }+}++impl From<NotificationConversation> for core::NotificationConversation {+ fn from(c: NotificationConversation) -> Self {+ core::NotificationConversation {+ v: c.v,+ title: c.title,+ group: c.group,+ avatar: c.avatar,+ messages: c.messages.into_iter().map(Into::into).collect(),+ }+ }+}++impl From<core::SmsAttachment> for SmsAttachment {+ fn from(a: core::SmsAttachment) -> Self {+ SmsAttachment { mime: a.mime, data: a.data }+ }+}++impl From<SmsAttachment> for core::SmsAttachment {+ fn from(a: SmsAttachment) -> Self {+ core::SmsAttachment { mime: a.mime, data: a.data }+ }+}+impl From<core::Notification> for Notification {fn from(n: core::Notification) -> Self {Notification {@@ -1761,6 +1946,7 @@ impl From<core::Notification> for Notification {icon: n.icon,image: n.image,live: n.live.map(Into::into),+ conversation: n.conversation.map(Into::into),}}}@@ -1784,6 +1970,7 @@ impl From<Notification> for core::Notification {icon: n.icon,image: n.image,live: n.live.map(Into::into),+ conversation: n.conversation.map(Into::into),}}}@@ -1814,6 +2001,7 @@ impl From<core::Transfer> for Transfer {total: t.total,done: t.done,status: match t.state {+ core::TransferState::Requested => TransferStatus::Requested,core::TransferState::Waiting => TransferStatus::Waiting,core::TransferState::Running => TransferStatus::Running,core::TransferState::Done { saved } => TransferStatus::Done {@@ -1885,9 +2073,9 @@ fn features(matrix: &CapabilityMatrix) -> Vec<Feature> {matrix.features.iter().map(|(id, state)| Feature { id: (*id).into(), status: (*state).into() }).collect()}-impl From<NodeEvent> for Event {- fn from(event: NodeEvent) -> Self {- match event {+impl Event {+ fn from_node(event: NodeEvent) -> Option<Self> {+ Some(match event {NodeEvent::DeviceAdded(device) => Event::DeviceAdded { device: device.into() },NodeEvent::DeviceRemoved(id) => Event::DeviceRemoved { id: id.to_string() },NodeEvent::LinkChanged { device, link } => {@@ -1946,6 +2134,9 @@ impl From<NodeEvent> for Event {Event::Mirroring { id: device.to_string(), session, on }}NodeEvent::Webcam { device, on } => Event::Webcam { id: device.to_string(), on },+ NodeEvent::CameraCaptureRequested { .. }+ | NodeEvent::CameraCaptureReceived { .. }+ | NodeEvent::CameraCaptureCancelled { .. } => return None,NodeEvent::RemoteInputRequested { device } => {Event::RemoteInputRequested { id: device.to_string() }}@@ -1966,7 +2157,8 @@ impl From<NodeEvent> for Event {Event::StorageChanged { id: device.to_string(), path }}NodeEvent::TimelineChanged => Event::TimelineChanged,- }+ NodeEvent::LocalSendChanged => Event::LocalSendChanged,+ })}}@@ -2164,6 +2356,11 @@ pub trait Platform: Send + Sync {fn webcam_stop_requested(&self, pc_id: String);/// The PC needs a fresh H.264 keyframe on the webcam stream.fn webcam_keyframe_requested(&self, pc_id: String);+ /// A PC asked this phone to capture a photo or scan a document (`mode` is+ /// `"photo"` or `"scan"`). Return `true` if the capture UI was launched.+ fn camera_capture_requested(&self, pc_id: String, request_id: String, mode: String) -> bool;+ /// The PC cancelled an in-flight Continuity Camera capture request.+ fn camera_capture_cancelled(&self, pc_id: String, request_id: String);/// A PC asked for the apps it may open in windows (launchable ones).fn phone_apps(&self) -> Vec<PhoneApp>;/// A PC asked for the latest conversations, newest first.@@ -2174,6 +2371,14 @@ pub trait Platform: Send + Sync {/// A PC asked to send a text (checked: 1–20 recipients, not empty)./// False if it couldn't be sent.fn sms_send(&self, to: Vec<String>, body: String) -> bool;+ /// A PC asked to send a text or MMS with optional image attachments.+ /// False if it couldn't be sent.+ fn send_sms_with_attachments(+ &self,+ to: Vec<String>,+ body: String,+ attachments: Vec<SmsAttachment>,+ ) -> bool;/// A PC asked for a message's attachment; null when it's gone.fn sms_part(&self, id: String) -> Option<SmsPartData>;/// A PC asked to change one of this phone's quick settings (`id`: `"dnd"`,@@ -2315,6 +2520,24 @@ impl core::Platform for PlatformAdapter {fn webcam_keyframe_requested(&self, peer: &DeviceId) {self.0.webcam_keyframe_requested(peer.to_string());}+ fn camera_capture_requested(+ &self,+ peer: &DeviceId,+ request: &core::CameraCaptureRequest,+ ) -> Result<(), String> {+ let mode = match request.mode {+ core::CameraCaptureMode::Photo => "photo",+ core::CameraCaptureMode::Scan => "scan",+ };+ if self.0.camera_capture_requested(peer.to_string(), request.request_id.clone(), mode.to_owned()) {+ Ok(())+ } else {+ Err("camera capture unavailable".into())+ }+ }+ fn camera_capture_cancelled(&self, peer: &DeviceId, request_id: &str) {+ self.0.camera_capture_cancelled(peer.to_string(), request_id.to_owned());+ }fn phone_apps(&self) -> Result<Vec<core::PhoneApp>, String> {Ok(self.0@@ -2368,6 +2591,23 @@ impl core::Platform for PlatformAdapter {fn sms_send(&self, to: &[String], body: &str) -> Result<(), String> {if self.0.sms_send(to.to_vec(), body.to_owned()) { Ok(()) } else { Err("not sent".into()) }}+ fn send_sms_with_attachments(+ &self,+ to: &[String],+ body: &str,+ attachments: &[core::SmsAttachment],+ ) -> Result<(), String> {+ if attachments.is_empty() {+ return self.sms_send(to, body);+ }+ let att: Vec<SmsAttachment> =+ attachments.iter().cloned().map(|a| SmsAttachment { mime: a.mime, data: a.data }).collect();+ if self.0.send_sms_with_attachments(to.to_vec(), body.to_owned(), att) {+ Ok(())+ } else {+ Err("not sent".into())+ }+ }fn sms_part(&self, id: &str) -> Result<(String, Vec<u8>), String> {let part = self.0.sms_part(id.to_owned()).ok_or("it's gone")?;Ok((part.mime, part.data))@@ -2598,7 +2838,11 @@ impl NectarlinkNode {runtime.spawn(async move {loop {match events.recv().await {- Ok(event) => listener.on_event(event.into()),+ Ok(event) => {+ if let Some(ev) = Event::from_node(event) {+ listener.on_event(ev);+ }+ }Err(RecvError::Lagged(missed)) => tracing::warn!(missed, "event listener fell behind"),Err(RecvError::Closed) => return,}@@ -2719,11 +2963,50 @@ impl NectarlinkNode {self.run(async move { Ok(node.send_recording(id, file, markers).await?) }).await}- /// Cancels a transfer in either direction.+ /// Accepts an incoming transfer waiting for user confirmation (LocalSend).+ pub fn accept_transfer(&self, transfer_id: String) {+ self.node.accept_transfer(&transfer_id);+ }++ /// Cancels a transfer in either direction (or declines a pending incoming+ /// LocalSend transfer).pub fn cancel_transfer(&self, transfer_id: String) {self.node.cancel_transfer(&transfer_id);}+ // ---- LocalSend compatibility ----++ /// Whether LocalSend v2.1 compatibility is enabled on this phone.+ pub fn localsend_enabled(&self) -> bool {+ self.node.localsend_enabled()+ }++ /// Turns LocalSend v2.1 compatibility on or off (persisted encrypted on disk).+ pub fn set_localsend_enabled(&self, enabled: bool) -> Result<()> {+ Ok(self.node.set_localsend_enabled(enabled)?)+ }++ /// Whether this phone's LocalSend server is actively bound and receiving.+ pub fn localsend_receiving(&self) -> bool {+ self.node.localsend_receiving()+ }++ /// Returns the LocalSend peers currently discovered on the local network.+ pub fn localsend_peers(&self) -> Vec<LocalSendPeer> {+ self.node.localsend_peers().into_iter().map(Into::into).collect()+ }++ /// Broadcasts a fresh LocalSend UDP multicast announcement.+ pub fn refresh_localsend(&self) {+ self.node.refresh_localsend();+ }++ /// Probes `host:port` via the LocalSend v2.1 HTTP/HTTPS API and adds the peer if found.+ pub async fn probe_localsend_peer(&self, host: String, port: u16) -> Result<LocalSendPeer> {+ let node = self.node.clone();+ self.run(async move { Ok(node.probe_localsend_peer(&host, port).await?.into()) }).await+ }+// ---- Clipboard ----/// Puts text on a paired PC's clipboard.@@ -2852,6 +3135,29 @@ impl NectarlinkNode {self.node.update_timeline_by_ref(&ref_id, kind.into(), &title, &detail, &target, size_bytes);}+ // ---- Data retention & privacy ----++ /// Returns current item/byte counts and retention settings across local history and caches.+ pub fn data_retention_counts(&self) -> DataRetentionCounts {+ self.node.data_retention_counts().into()+ }++ /// Clears all locally cached `MessagingStyle` chat threads and messages.+ pub fn clear_message_cache(&self) -> Result<()> {+ Ok(self.node.clear_message_cache()?)+ }++ /// Clears all file transfer records from the local timeline without deleting downloaded files.+ pub fn clear_received_file_history(&self) -> Result<()> {+ Ok(self.node.clear_received_file_history()?)+ }++ /// Wipes all local history and caches (encrypted clipboard history, timeline, and message cache)+ /// while preserving device identity, paired devices, and per-device feature toggles.+ pub fn clear_all_local_data(&self) -> Result<()> {+ Ok(self.node.clear_all_local_data()?)+ }+// ---- Media ----/// This phone's media players changed (most relevant first).@@ -3214,6 +3520,67 @@ impl NectarlinkNode {self.run(async move { Ok(node.toggles_changed(toggles).await?) }).await}+ // ---- Continuity Camera ----++ /// Sends a captured photo or scanned document from this phone to a requesting PC.+ #[allow(clippy::too_many_arguments)]+ pub async fn send_camera_capture_result(+ &self,+ pc_id: String,+ request_id: String,+ mode: String,+ file_name: String,+ mime: String,+ width: u32,+ height: u32,+ data: Vec<u8>,+ ) -> Result<()> {+ let peer = parse_id(&pc_id)?;+ let mode = match mode.as_str() {+ "scan" => core::CameraCaptureMode::Scan,+ _ => core::CameraCaptureMode::Photo,+ };+ let meta = core::CameraCaptureResultMeta {+ request_id,+ mode,+ file_name,+ mime,+ size: data.len() as u64,+ width,+ height,+ };+ let node = self.node.clone();+ self.run(async move { Ok(node.send_camera_capture_result(peer, meta, data).await?) }).await+ }++ /// Cancels an in-flight Continuity Camera capture request on either side.+ pub async fn cancel_camera_capture(+ &self,+ peer_id: String,+ request_id: String,+ reason: Option<String>,+ ) -> Result<()> {+ let peer = parse_id(&peer_id)?;+ let node = self.node.clone();+ self.run(async move { Ok(node.cancel_camera_capture(peer, request_id, reason).await?) }).await+ }++ /// Requests a Continuity Camera capture (`mode` is `"photo"` or `"scan"`) from a paired phone.+ pub async fn request_camera_capture(+ &self,+ peer_id: String,+ request_id: String,+ mode: String,+ ) -> Result<()> {+ let peer = parse_id(&peer_id)?;+ let mode = match mode.as_str() {+ "scan" => core::CameraCaptureMode::Scan,+ _ => core::CameraCaptureMode::Photo,+ };+ let node = self.node.clone();+ self.run(async move { Ok(node.request_camera_capture(peer, request_id, mode).await?) }).await+ }+/// Reconnects to PCs that aren't connected and syncs connected ones.pub async fn refresh(&self) {let node = self.node.clone();diff --git a/core/nectarlink-protocol/src/messages.rs b/core/nectarlink-protocol/src/messages.rsindex 2235825..3485bfd 100644--- a/core/nectarlink-protocol/src/messages.rs+++ b/core/nectarlink-protocol/src/messages.rs@@ -93,6 +93,9 @@ pub mod types {pub const WEBCAM_STOP: &str = "webcam.stop";pub const WEBCAM_KEYFRAME: &str = "webcam.keyframe";pub const WEBCAM_OK: &str = "webcam.ok";+ pub const CAMERA_CAPTURE_REQUEST: &str = "camera.capture.request";+ pub const CAMERA_CAPTURE_OK: &str = "camera.capture.ok";+ pub const CAMERA_CAPTURE_CANCEL: &str = "camera.capture.cancel";}/// What kind of device this is.@@ -465,12 +468,20 @@ pub mod notify_limits {pub const CHIP_CHARS: usize = 32;pub const SEGMENTS: usize = 16;pub const POINTS: usize = 16;+ pub const CONVERSATION_VERSION: u32 = 1;+ pub const CONVERSATION_MESSAGES: usize = 25;+ pub const SENDER_CHARS: usize = 128;+ pub const AVATAR_BYTES: usize = 16 * 1024;}fn default_live_v() -> u32 {notify_limits::LIVE_VERSION}+fn default_conversation_v() -> u32 {+ notify_limits::CONVERSATION_VERSION+}+/// One segment of a segmented progress bar (`Notification.ProgressStyle.Segment` on Android 16+).#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]pub struct LiveSegment {@@ -610,6 +621,98 @@ impl NotificationLive {}}+/// One message in a `Notification.MessagingStyle` conversation (`docs/protocol/notifications.md` §2.3).+#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct NotificationChatMessage {+ /// Sender display name (`None` when sent by the phone's user).+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub sender: Option<String>,+ /// Message text.+ pub text: String,+ /// Message timestamp, Unix milliseconds.+ pub time: i64,+ /// True when sent by the phone's user (`MessagingStyle.user` or `Person == null`).+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]+ pub self_sent: bool,+ /// Optional sender avatar (JPEG/PNG, at most [`notify_limits::AVATAR_BYTES`]).+ #[serde(default, skip_serializing_if = "Option::is_none", with = "serde_bytes")]+ pub avatar: Option<Vec<u8>>,+}++/// Never prints sender names or message text (protocol v0 §11).+impl std::fmt::Debug for NotificationChatMessage {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("NotificationChatMessage")+ .field("time", &self.time)+ .field("self_sent", &self.self_sent)+ .field("has_avatar", &self.avatar.is_some())+ .finish_non_exhaustive()+ }+}++/// `Notification.MessagingStyle` conversation metadata on a mirrored notification (`docs/protocol/notifications.md` §2.3).+#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct NotificationConversation {+ /// Schema version (currently `1`).+ #[serde(default = "default_conversation_v")]+ pub v: u32,+ /// Conversation title (group name or 1:1 contact name).+ pub title: String,+ /// True when `MessagingStyle.isGroupConversation` is true.+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]+ pub group: bool,+ /// Conversation or contact avatar (JPEG/PNG, at most [`notify_limits::AVATAR_BYTES`]).+ #[serde(default, skip_serializing_if = "Option::is_none", with = "serde_bytes")]+ pub avatar: Option<Vec<u8>>,+ /// Messages in chronological order (oldest first, at most [`notify_limits::CONVERSATION_MESSAGES`]).+ #[serde(default, skip_serializing_if = "Vec::is_empty")]+ pub messages: Vec<NotificationChatMessage>,+}++/// Never prints conversation title or messages (protocol v0 §11).+impl std::fmt::Debug for NotificationConversation {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("NotificationConversation")+ .field("v", &self.v)+ .field("group", &self.group)+ .field("messages", &self.messages.len())+ .field("has_avatar", &self.avatar.is_some())+ .finish_non_exhaustive()+ }+}++impl NotificationConversation {+ /// Applies limits and drops unknown schema versions (`v != 1`) or empty conversations.+ pub fn sanitized(mut self) -> Option<Self> {+ use notify_limits::*;+ if self.v != CONVERSATION_VERSION {+ return None;+ }+ self.title = clean_text(Some(self.title), TITLE_CHARS)?;+ if self.avatar.as_ref().is_some_and(|a| a.is_empty() || a.len() > AVATAR_BYTES) {+ self.avatar = None;+ }+ let mut cleaned = Vec::with_capacity(self.messages.len().min(CONVERSATION_MESSAGES));+ let skip = self.messages.len().saturating_sub(CONVERSATION_MESSAGES);+ for mut m in self.messages.into_iter().skip(skip) {+ let Some(text) = clean_text(Some(m.text), TEXT_CHARS) else {+ continue;+ };+ m.text = text;+ m.sender = if m.self_sent { None } else { clean_text(m.sender, SENDER_CHARS) };+ if m.avatar.as_ref().is_some_and(|a| a.is_empty() || a.len() > AVATAR_BYTES) {+ m.avatar = None;+ }+ cleaned.push(m);+ }+ if cleaned.is_empty() {+ return None;+ }+ self.messages = cleaned;+ Some(self)+ }+}+/// A button on a notification.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct NotificationAction {@@ -654,6 +757,9 @@ pub struct Notification {/// Live Update metadata (ongoing progress, timer, or Android 16 Live Update).#[serde(default, skip_serializing_if = "Option::is_none")]pub live: Option<NotificationLive>,+ /// `Notification.MessagingStyle` conversation metadata (RCS, WhatsApp, Telegram, Signal, etc.).+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub conversation: Option<NotificationConversation>,}/// Never prints content: notification text must not reach logs (protocol@@ -665,6 +771,7 @@ impl std::fmt::Debug for Notification {.field("actions", &self.actions.len()).field("silent", &self.silent).field("live", &self.live.is_some())+ .field("conversation", &self.conversation.is_some()).finish_non_exhaustive()}}@@ -694,9 +801,21 @@ impl Notification {self.text = clean_text(self.text, TEXT_CHARS);self.sub = clean_text(self.sub, TITLE_CHARS);self.live = self.live.and_then(NotificationLive::sanitized);- if self.title.is_none() && self.text.is_none() {+ self.conversation = self.conversation.and_then(NotificationConversation::sanitized);+ if self.title.is_none() && self.text.is_none() && self.conversation.is_none() {return None;}+ if self.title.is_none()+ && let Some(conv) = &self.conversation+ {+ self.title = Some(conv.title.clone());+ }+ if self.text.is_none()+ && let Some(conv) = &self.conversation+ && let Some(last) = conv.messages.last()+ {+ self.text = Some(last.text.clone());+ }truncate_chars(&mut self.app, TITLE_CHARS);truncate_chars(&mut self.app_name, TITLE_CHARS);self.actions.retain_mut(|a| {@@ -1421,6 +1540,102 @@ impl WebcamConfig {}}+// ---- Continuity Camera (docs/protocol/v0.md §Continuity Camera) ----++pub mod continuity_camera {+ /// Offered by phones that can capture a still photo or scan a document on request.+ pub const CAPTURE: &str = "camera.capture";+ /// Offered by PCs that can request and receive Continuity Camera captures.+ pub const RECEIVE: &str = "camera.continuity";++ /// The `stream` header's `svc` for a Continuity Camera result upload.+ pub const SERVICE: &str = "camera";+ /// The `stream` header's `op` for sending a captured photo or scanned document.+ pub const OP_RESULT: &str = "result";+ pub const VERSION: u32 = 1;++ /// Stream frame type sent before the raw image bytes on a `camera` / `result` stream.+ pub const RESULT_META: &str = "camera.capture.meta";++ /// Capture mode string values.+ pub const MODE_PHOTO: &str = "photo";+ pub const MODE_SCAN: &str = "scan";++ /// Maximum image payload size for a Continuity Camera capture (32 MiB).+ pub const MAX_IMAGE_BYTES: u64 = 32 * 1024 * 1024;+ /// Maximum length of a request ID in bytes.+ pub const MAX_ID_BYTES: usize = 64;+ /// Maximum length of a file name in bytes.+ pub const MAX_NAME_BYTES: usize = 255;+}++/// Mode requested for a Continuity Camera capture.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "lowercase")]+pub enum CameraCaptureMode {+ /// Capture a single high-resolution photo.+ Photo,+ /// Capture and perspective-crop / contrast-enhance a document scan.+ Scan,+}++impl CameraCaptureMode {+ /// Wire/display string (`"photo"` or `"scan"`).+ #[must_use]+ pub const fn as_str(self) -> &'static str {+ match self {+ Self::Photo => continuity_camera::MODE_PHOTO,+ Self::Scan => continuity_camera::MODE_SCAN,+ }+ }++ /// Parses `"photo"` or `"scan"` (case-insensitive).+ #[must_use]+ pub fn parse(raw: &str) -> Option<Self> {+ match raw.trim().to_ascii_lowercase().as_str() {+ continuity_camera::MODE_PHOTO => Some(Self::Photo),+ continuity_camera::MODE_SCAN => Some(Self::Scan),+ _ => None,+ }+ }+}++/// `camera.capture.request`: PC asks a paired phone to open its camera UI and capture a photo or scan.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct CameraCaptureRequest {+ pub request_id: String,+ pub mode: CameraCaptureMode,+}++/// `camera.capture.ok`: Phone acknowledges that the capture UI was launched.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct CameraCaptureOk {+ pub request_id: String,+}++/// `camera.capture.cancel`: Either side cancels an in-flight capture request.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct CameraCaptureCancel {+ pub request_id: String,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub reason: Option<String>,+}++/// `camera.capture.meta`: Header frame sent on a `camera` / `result` stream before the raw image bytes.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct CameraCaptureResultMeta {+ pub request_id: String,+ pub mode: CameraCaptureMode,+ pub file_name: String,+ /// `"image/jpeg"` or `"image/png"`.+ pub mime: String,+ pub size: u64,+ #[serde(default)]+ pub width: u32,+ #[serde(default)]+ pub height: u32,+}+// ---- Messages (docs/protocol/sms.md) ----pub mod sms {@@ -1438,6 +1653,10 @@ pub mod sms {pub const MAX_RECIPIENTS: usize = 20;/// A picture in a message, fetched with `sms.part`: at most this many bytes.pub const MAX_PART_BYTES: usize = 900 * 1024;+ /// Most image attachments in one `sms.send` message.+ pub const MAX_ATTACHMENTS: usize = 1;+ /// An image sent with `sms.send` (MMS): at most this many bytes.+ pub const MAX_ATTACHMENT_BYTES: usize = MAX_PART_BYTES;/// A contact's photo with a conversation: a JPEG of at most this many bytes.pub const MAX_PHOTO_BYTES: usize = 16 * 1024;}@@ -1547,17 +1766,48 @@ pub struct SmsMessages {pub messages: Vec<SmsMessage>,}+/// An image attachment sent with `sms.send` (MMS).+#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct SmsAttachment {+ /// `"image/jpeg"` or `"image/png"`.+ pub mime: String,+ #[serde(with = "serde_bytes")]+ pub data: Vec<u8>,+}++impl std::fmt::Debug for SmsAttachment {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("SmsAttachment").field("mime", &self.mime).field("bytes", &self.data.len()).finish()+ }+}++impl SmsAttachment {+ pub fn is_valid(&self) -> bool {+ matches!(self.mime.as_str(), "image/jpeg" | "image/png")+ && !self.data.is_empty()+ && self.data.len() <= sms::MAX_ATTACHMENT_BYTES+ }+}+/// Body of `sms.send`.#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]pub struct SmsSend {pub to: Vec<String>,+ #[serde(default)]pub body: String,+ /// Optional image attachment(s) to send as MMS.+ #[serde(default, skip_serializing_if = "Vec::is_empty")]+ pub attachments: Vec<SmsAttachment>,}/// Never prints numbers or text (protocol v0 §11).impl std::fmt::Debug for SmsSend {fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- f.debug_struct("SmsSend").field("to", &self.to.len()).field("bytes", &self.body.len()).finish()+ f.debug_struct("SmsSend")+ .field("to", &self.to.len())+ .field("bytes", &self.body.len())+ .field("attachments", &self.attachments.len())+ .finish()}}@@ -1565,8 +1815,10 @@ impl SmsSend {pub fn is_valid(&self) -> bool {(1..=sms::MAX_RECIPIENTS).contains(&self.to.len())&& self.to.iter().all(|a| (1..=64).contains(&a.trim().len()))- && !self.body.trim().is_empty()+ && (!self.body.trim().is_empty() || !self.attachments.is_empty())&& self.body.len() <= sms::MAX_SEND_BYTES+ && self.attachments.len() <= sms::MAX_ATTACHMENTS+ && self.attachments.iter().all(SmsAttachment::is_valid)}}@@ -3721,6 +3973,7 @@ mod tests {icon: Some(vec![0x89, b'P', b'N', b'G']),image: None,live: None,+ conversation: None,}}@@ -3757,6 +4010,34 @@ mod tests {assert_eq!(n.image, None);}+ #[test]+ fn conversation_notifications_are_sanitized_and_hide_content_in_debug() {+ let mut n = notification();+ n.conversation = Some(NotificationConversation {+ v: 1,+ title: "Design Team".into(),+ group: true,+ avatar: Some(vec![0xff, 0xd8]),+ messages: (0..30)+ .map(|i| NotificationChatMessage {+ sender: Some(format!("Sender {i}")),+ text: format!("Message {i}"),+ time: 1_760_000_000_000 + i,+ self_sent: i % 2 == 0,+ avatar: Some(vec![0; if i == 29 { notify_limits::AVATAR_BYTES + 1 } else { 4 }]),+ })+ .collect(),+ });+ let shown = format!("{n:?} {:?}", n.conversation);+ assert!(!shown.contains("Design Team") && !shown.contains("Message 29"), "{shown}");+ let clean = n.sanitized().unwrap();+ let conv = clean.conversation.unwrap();+ assert_eq!(conv.messages.len(), notify_limits::CONVERSATION_MESSAGES);+ assert_eq!(conv.messages[0].text, "Message 5", "keeps newest messages");+ assert_eq!(conv.messages[1].sender, None, "self-sent messages drop sender name");+ assert_eq!(conv.messages.last().unwrap().avatar, None, "oversized avatar is dropped");+ }+#[test]fn notifications_without_content_or_key_are_dropped() {let mut n = notification();@@ -3886,13 +4167,33 @@ mod tests {#[test]fn texts_to_send_are_checked() {- let send = SmsSend { to: vec!["+15550100".into()], body: "On my way".into() };+ let send =+ SmsSend { to: vec!["+15550100".into()], body: "On my way".into(), attachments: Vec::new() };assert!(send.is_valid());assert!(!format!("{send:?}").contains("way"));assert!(!SmsSend { to: vec![], ..send.clone() }.is_valid());assert!(!SmsSend { body: " ".into(), ..send.clone() }.is_valid());assert!(!SmsSend { body: "x".repeat(sms::MAX_SEND_BYTES + 1), ..send.clone() }.is_valid());- assert!(!SmsSend { to: vec!["1".into(); sms::MAX_RECIPIENTS + 1], ..send }.is_valid());+ assert!(!SmsSend { to: vec!["1".into(); sms::MAX_RECIPIENTS + 1], ..send.clone() }.is_valid());+ let with_image = SmsSend {+ body: String::new(),+ attachments: vec![SmsAttachment { mime: "image/png".into(), data: vec![1, 2, 3] }],+ ..send.clone()+ };+ assert!(with_image.is_valid(), "an image attachment can be sent without body text");+ let bad_mime = SmsSend {+ attachments: vec![SmsAttachment { mime: "text/plain".into(), data: vec![1] }],+ ..send.clone()+ };+ assert!(!bad_mime.is_valid());+ let oversized = SmsSend {+ attachments: vec![SmsAttachment {+ mime: "image/jpeg".into(),+ data: vec![0; sms::MAX_ATTACHMENT_BYTES + 1],+ }],+ ..send+ };+ assert!(!oversized.is_valid());}#[test]@@ -4527,4 +4828,38 @@ mod tests {assert_eq!(TaskNotify::format_took(252_000), "Took 4m 12s");assert_eq!(TaskNotify::format_took(3_840_000), "Took 1h 04m");}++ #[test]+ fn continuity_camera_messages_round_trip() {+ let req = CameraCaptureRequest { request_id: "req-1".into(), mode: CameraCaptureMode::Scan };+ let env = Envelope::new(types::CAMERA_CAPTURE_REQUEST, &req).unwrap();+ let back: CameraCaptureRequest = Envelope::from_cbor(&env.to_cbor()).unwrap().body().unwrap();+ assert_eq!(back, req);++ let ok = CameraCaptureOk { request_id: "req-1".into() };+ let env_ok = Envelope::new(types::CAMERA_CAPTURE_OK, &ok).unwrap();+ let back_ok: CameraCaptureOk = Envelope::from_cbor(&env_ok.to_cbor()).unwrap().body().unwrap();+ assert_eq!(back_ok, ok);++ let cancel =+ CameraCaptureCancel { request_id: "req-1".into(), reason: Some("user dismissed".into()) };+ let env_cancel = Envelope::new(types::CAMERA_CAPTURE_CANCEL, &cancel).unwrap();+ let back_cancel: CameraCaptureCancel =+ Envelope::from_cbor(&env_cancel.to_cbor()).unwrap().body().unwrap();+ assert_eq!(back_cancel, cancel);++ let meta = CameraCaptureResultMeta {+ request_id: "req-1".into(),+ mode: CameraCaptureMode::Photo,+ file_name: "IMG_20260409.jpg".into(),+ mime: "image/jpeg".into(),+ size: 12345,+ width: 1920,+ height: 1080,+ };+ let env_meta = Envelope::new(continuity_camera::RESULT_META, &meta).unwrap();+ let back_meta: CameraCaptureResultMeta =+ Envelope::from_cbor(&env_meta.to_cbor()).unwrap().body().unwrap();+ assert_eq!(back_meta, meta);+ }}diff --git a/desktop/app/build.rs b/desktop/app/build.rsindex a2e2f8f..0264b7e 100644--- a/desktop/app/build.rs+++ b/desktop/app/build.rs@@ -28,6 +28,7 @@ const QML: &[&str] = &["qml/components/Card.qml","qml/components/Chip.qml","qml/components/CodeDigits.qml",+ "qml/components/CommandPalette.qml","qml/components/Divider.qml","qml/components/DoctorSheet.qml","qml/components/HistoryItem.qml",@@ -66,6 +67,13 @@ const QML_SINGLETONS: &[&str] = &["qml/Tokens.qml", "qml/Theme.qml", "qml/Icons.const FONTS: &[&str] =&["Figtree.ttf", "InstrumentSerif-Regular.ttf", "SpaceMono-Regular.ttf", "SpaceMono-Bold.ttf"];+fn write_if_changed(path: &Path, content: impl AsRef<[u8]>) {+ let bytes = content.as_ref();+ if fs::read(path).ok().as_deref() != Some(bytes) {+ fs::write(path, bytes).unwrap_or_else(|e| panic!("write {}: {e}", path.display()));+ }+}+/// Writes a resource file listing the fonts (they live outside this crate,/// so each needs an alias) and returns its path.fn fonts_qrc() -> PathBuf {@@ -80,7 +88,7 @@ fn fonts_qrc() -> PathBuf {}qrc.push_str(" </qresource>\n</RCC>\n");let out = PathBuf::from(std::env::var_os("OUT_DIR").expect("OUT_DIR")).join("fonts.qrc");- fs::write(&out, qrc).expect("write fonts.qrc");+ write_if_changed(&out, qrc);out}@@ -141,7 +149,7 @@ fn icon_file() -> Vec<u8> {fn windows_resources() {let out = PathBuf::from(std::env::var_os("OUT_DIR").expect("OUT_DIR"));let ico = out.join("nectarlink.ico");- fs::write(&ico, icon_file()).expect("write the icon");+ write_if_changed(&ico, icon_file());let version = std::env::var("CARGO_PKG_VERSION").expect("version");let numbers: Vec<&str> = version.split(['.', '-', '+']).take(3).collect();let numeric = format!("{},0", numbers.join(","));@@ -178,7 +186,7 @@ ENDico = ico.display().to_string().replace('\\', "/"),);let rc_path = out.join("nectarlink.rc");- fs::write(&rc_path, rc).expect("write the resource script");+ write_if_changed(&rc_path, rc);embed_resource::compile(&rc_path, embed_resource::NONE).manifest_required().expect("compile resources");}diff --git a/desktop/app/qml/Icons.qml b/desktop/app/qml/Icons.qmlindex e090b0e..32b752f 100644--- a/desktop/app/qml/Icons.qml+++ b/desktop/app/qml/Icons.qml@@ -44,6 +44,8 @@ QtObject {readonly property string mark: "M12 2.5l8.2 4.75v9.5L12 21.5l-8.2-4.75v-9.5zM9 12h6"readonly property string back: "M15 6l-6 6 6 6"readonly property string chevronRight: "M9 6l6 6-6 6"+ readonly property string chevronDown: "M6 9l6 6 6-6"+ readonly property string clipboardList: "M9 3h6v4H9zM16 5h2a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2M8.5 11.5h7M8.5 14.5h7M8.5 17.5h4.5"readonly property string battery: "M4 7h14a2 2 0 0 1 2 2v6a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2zM22 11v2"readonly property string phone: "M8 2h8a2 2 0 0 1 2 2v16a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2zM11 18h2"readonly property string laptop: "M5 5h14a1 1 0 0 1 1 1v10H4V6a1 1 0 0 1 1-1zM2 19h20"diff --git a/desktop/app/qml/MainWindow.qml b/desktop/app/qml/MainWindow.qmlindex b6fd967..f7ead27 100644--- a/desktop/app/qml/MainWindow.qml+++ b/desktop/app/qml/MainWindow.qml@@ -191,6 +191,19 @@ NativeWindow {role: "headline"size: 20}+ IconButton {+ id: paletteTopBtn+ anchors.right: timelineTopBtn.visible ? timelineTopBtn.left : (refresh.visible ? refresh.left : parent.right)+ anchors.rightMargin: (timelineTopBtn.visible || refresh.visible) ? 4 : 8+ anchors.verticalCenter: parent.verticalCenter+ iconPath: Icons.search+ text: qsTr("Commands")+ label: Preferences.commandPaletteHotkey.length > 0+ ? qsTr("Command palette (%1)").arg(Preferences.commandPaletteHotkey)+ : qsTr("Command palette (Ctrl+K)")+ onClicked: AppController.openCommandPalette()+ Component.onCompleted: window.addCaptionHole(paletteTopBtn)+ }IconButton {id: timelineTopBtnanchors.right: refresh.visible ? refresh.left : parent.right@@ -198,8 +211,9 @@ NativeWindow {anchors.verticalCenter: parent.verticalCentervisible: (window.page === "home" || window.page === "timeline") && AppController.hasDevicesiconPath: Icons.history+ text: qsTr("Timeline")tonal: window.page === "timeline"- label: qsTr("Timeline")+ label: qsTr("Files, clips, links, photos, recordings and sessions")onClicked: window.page = window.page === "timeline" ? "home" : "timeline"Component.onCompleted: window.addCaptionHole(timelineTopBtn)}@@ -369,6 +383,56 @@ NativeWindow {}}+ Sheet {+ id: continuityPasteSheet+ opened: AppController.continuityPastePromptVisible+ cardWidth: 440+ onOpenedChanged: {+ if (!opened && AppController.continuityPastePromptVisible)+ AppController.dismissContinuityPaste()+ }+ Column {+ width: parent.width+ spacing: 16+ Avatar {+ anchors.horizontalCenter: parent.horizontalCenter+ width: 56; height: 56+ iconPath: Icons.camera+ emphasized: true+ }+ Txt {+ width: parent.width+ horizontalAlignment: Text.AlignHCenter+ text: AppController.continuityPastePromptTitle.length > 0+ ? AppController.continuityPastePromptTitle+ : qsTr("Photo ready on clipboard")+ role: "headline"+ wrapMode: Text.WordWrap+ }+ Txt {+ width: parent.width+ horizontalAlignment: Text.AlignHCenter+ text: AppController.continuityPastePromptBody+ role: "body"+ muted: true+ wrapMode: Text.WordWrap+ }+ Row {+ anchors.right: parent.right+ spacing: 10+ Button {+ variant: "text"+ text: qsTr("Keep on clipboard")+ onClicked: AppController.dismissContinuityPaste()+ }+ Button {+ text: qsTr("Paste into active window")+ onClicked: AppController.confirmContinuityPaste()+ }+ }+ }+ }+CaptionButtons {id: captionButtonswindow: window@@ -393,6 +457,23 @@ NativeWindow {}}+ Shortcut {+ sequences: ["Ctrl+K", "Ctrl+P"]+ onActivated: {+ if (AppController.commandPaletteOpen)+ AppController.closeCommandPalette()+ else+ AppController.openCommandPalette()+ }+ }++ Loader {+ id: commandPaletteLoader+ anchors.fill: parent+ active: AppController.commandPaletteOpen+ source: active ? "qrc:/qt/qml/app/nectarlink/qml/components/CommandPalette.qml" : ""+ }+Toast {id: toastonActionTriggered: AppController.runClipSuggestion()diff --git a/desktop/app/qml/Theme.qml b/desktop/app/qml/Theme.qmlindex 64d0173..e3bf264 100644--- a/desktop/app/qml/Theme.qml+++ b/desktop/app/qml/Theme.qml@@ -71,6 +71,7 @@ QtObject {readonly property color secondaryContainerContent: palette.onSecondaryContainerreadonly property color surface: palette.surfacereadonly property color surfaceLow: palette.surfaceContainerLow+ readonly property color surfaceContainerLow: palette.surfaceContainerLowreadonly property color surfaceContainer: palette.surfaceContainerreadonly property color surfaceContainerHigh: palette.surfaceContainerHighreadonly property color surfaceContainerHighest: palette.surfaceContainerHighestdiff --git a/desktop/app/qml/components/CommandPalette.qml b/desktop/app/qml/components/CommandPalette.qmlnew file mode 100644index 0000000..2655825--- /dev/null+++ b/desktop/app/qml/components/CommandPalette.qml@@ -0,0 +1,355 @@+// SPDX-License-Identifier: GPL-3.0-or-later+import QtQuick+import app.nectarlink++// Fast, keyboard-first Command Palette modal overlay (PLAN.md §3.9 / §3.11 P1).+// Loaded lazily via a Loader in MainWindow.qml only while open, so it costs+// zero memory and zero CPU while closed.+Item {+ id: palette+ anchors.fill: parent+ z: 150++ property int selectedIndex: 0+ readonly property var items: {+ const raw = AppController.commandPaletteResults+ if (!raw || raw.length === 0)+ return []+ try {+ return JSON.parse(raw)+ } catch (e) {+ return []+ }+ }++ onItemsChanged: {+ if (selectedIndex >= items.length)+ selectedIndex = Math.max(0, items.length - 1)+ }++ function runSelected() {+ if (items.length === 0)+ return+ const idx = Math.max(0, Math.min(selectedIndex, items.length - 1))+ const item = items[idx]+ if (item && item.id)+ AppController.runCommandPalette(item.id)+ }++ Component.onCompleted: {+ searchInput.forceActiveFocus()+ Qt.callLater(() => AppController.noteCommandPaletteShown())+ }++ Rectangle {+ id: scrim+ anchors.fill: parent+ color: Qt.rgba(0, 0, 0, Theme.dark ? 0.52 : 0.28)+ TapHandler {+ onTapped: (eventPoint) => {+ const p = card.mapFromItem(scrim, eventPoint.position)+ const inside = p.x >= 0 && p.y >= 0 && p.x <= card.width && p.y <= card.height+ if (!inside)+ AppController.closeCommandPalette()+ }+ }+ }++ Rectangle {+ id: card+ anchors.horizontalCenter: parent.horizontalCenter+ y: Math.max(56, Math.round(parent.height * 0.12))+ width: Math.min(580, parent.width - 48)+ height: cardColumn.height+ radius: Theme.radiusLg+ color: Theme.surfaceContainerHigh+ border.width: 1+ border.color: Theme.outlineVariant+ clip: true++ Column {+ id: cardColumn+ width: parent.width++ // ---- Search bar ----+ Item {+ width: parent.width+ height: 54++ Icon {+ id: searchIcon+ anchors.left: parent.left+ anchors.leftMargin: 18+ anchors.verticalCenter: parent.verticalCenter+ width: 18+ height: 18+ path: Icons.search+ color: Theme.surfaceContentVariant+ }++ Item {+ anchors.left: searchIcon.right+ anchors.leftMargin: 12+ anchors.right: escBadge.left+ anchors.rightMargin: 10+ anchors.top: parent.top+ anchors.bottom: parent.bottom++ Txt {+ anchors.verticalCenter: parent.verticalCenter+ visible: searchInput.text.length === 0+ text: qsTr("Type a command, contact, or app…")+ role: "body"+ muted: true+ }++ TextInput {+ id: searchInput+ anchors.fill: parent+ verticalAlignment: TextInput.AlignVCenter+ font.family: Theme.fontUi+ font.pixelSize: 15+ color: Theme.surfaceContent+ selectionColor: Theme.primaryContainer+ selectedTextColor: Theme.primaryContainerContent+ focus: true+ clip: true+ Accessible.name: qsTr("Command palette search")++ onTextChanged: {+ palette.selectedIndex = 0+ AppController.searchCommandPalette(searchInput.text)+ }++ Keys.onPressed: (event) => {+ if (event.key === Qt.Key_Down) {+ if (palette.items.length > 0) {+ palette.selectedIndex = (palette.selectedIndex + 1) % palette.items.length+ resultsList.positionViewAtIndex(palette.selectedIndex, ListView.Contain)+ }+ event.accepted = true+ } else if (event.key === Qt.Key_Up) {+ if (palette.items.length > 0) {+ palette.selectedIndex = (palette.selectedIndex - 1 + palette.items.length) % palette.items.length+ resultsList.positionViewAtIndex(palette.selectedIndex, ListView.Contain)+ }+ event.accepted = true+ } else if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter) {+ palette.runSelected()+ event.accepted = true+ } else if (event.key === Qt.Key_Escape) {+ AppController.closeCommandPalette()+ event.accepted = true+ }+ }+ }+ }++ Rectangle {+ id: escBadge+ anchors.right: parent.right+ anchors.rightMargin: 16+ anchors.verticalCenter: parent.verticalCenter+ width: escLabel.implicitWidth + 12+ height: 22+ radius: Theme.radiusXs+ color: Theme.surfaceContainerHighest+ border.width: 1+ border.color: Theme.outlineVariant++ Txt {+ id: escLabel+ anchors.centerIn: parent+ text: searchInput.text.length > 0 ? qsTr("Clear") : "Esc"+ role: "caption"+ size: 11+ muted: true+ }++ HoverHandler { cursorShape: Qt.PointingHandCursor }+ TapHandler {+ onTapped: {+ if (searchInput.text.length > 0) {+ searchInput.text = ""+ searchInput.forceActiveFocus()+ } else {+ AppController.closeCommandPalette()+ }+ }+ }+ }+ }++ Divider { width: parent.width }++ // ---- Results list ----+ Item {+ width: parent.width+ height: palette.items.length > 0 ? Math.min(resultsList.contentHeight + 12, 360) : 72++ ListView {+ id: resultsList+ anchors.fill: parent+ anchors.margins: 6+ clip: true+ boundsBehavior: Flickable.StopAtBounds+ visible: palette.items.length > 0+ model: palette.items+ spacing: 2++ delegate: Rectangle {+ id: row+ required property var modelData+ required property int index+ readonly property bool selected: index === palette.selectedIndex+ readonly property bool isEnabled: modelData.enabled !== false++ width: resultsList.width+ height: 52+ radius: Theme.radiusSm+ color: selected+ ? Theme.secondaryContainer+ : rowHover.hovered+ ? Theme.surfaceContainerHighest+ : "transparent"++ HoverHandler {+ id: rowHover+ cursorShape: Qt.PointingHandCursor+ onHoveredChanged: {+ if (hovered)+ palette.selectedIndex = row.index+ }+ }++ TapHandler {+ onTapped: {+ palette.selectedIndex = row.index+ AppController.runCommandPalette(row.modelData.id)+ }+ }++ Rectangle {+ id: iconBox+ anchors.left: parent.left+ anchors.leftMargin: 10+ anchors.verticalCenter: parent.verticalCenter+ width: 32+ height: 32+ radius: Theme.radiusSm+ color: row.selected ? Theme.primaryContainer : Theme.surfaceContainerHighest++ Icon {+ anchors.centerIn: parent+ width: 16+ height: 16+ path: (row.modelData.icon && Icons[row.modelData.icon])+ ? Icons[row.modelData.icon]+ : Icons.sparkle+ color: row.selected ? Theme.primaryContainerContent : Theme.surfaceContent+ }+ }++ Column {+ anchors.left: iconBox.right+ anchors.leftMargin: 12+ anchors.right: badgePill.visible ? badgePill.left : parent.right+ anchors.rightMargin: 12+ anchors.verticalCenter: parent.verticalCenter+ spacing: 1+ opacity: row.isEnabled ? 1.0 : 0.6++ Txt {+ width: parent.width+ text: row.modelData.title || ""+ role: "label"+ size: 14+ color: row.selected ? Theme.secondaryContainerContent : Theme.surfaceContent+ }++ Txt {+ width: parent.width+ visible: (row.modelData.subtitle || "").length > 0+ text: row.modelData.subtitle || ""+ role: "caption"+ size: 12+ muted: !row.selected+ color: row.selected ? Theme.secondaryContainerContent : Theme.surfaceContentVariant+ }+ }++ Rectangle {+ id: badgePill+ readonly property string badgeText: (row.modelData.badge && row.modelData.badge.length > 0)+ ? row.modelData.badge+ : (row.modelData.category || "")+ visible: badgeText.length > 0+ anchors.right: parent.right+ anchors.rightMargin: 12+ anchors.verticalCenter: parent.verticalCenter+ width: badgeLabel.implicitWidth + 14+ height: 22+ radius: Theme.pill(height)+ color: row.selected ? Theme.surfaceContainerHigh : Theme.surfaceContainerHighest++ Txt {+ id: badgeLabel+ anchors.centerIn: parent+ text: badgePill.badgeText+ role: "caption"+ size: 11+ muted: true+ }+ }+ }+ }++ Txt {+ anchors.centerIn: parent+ visible: palette.items.length === 0+ text: qsTr("No matching commands")+ role: "body"+ muted: true+ }+ }++ Divider { width: parent.width }++ // ---- Footer ----+ Item {+ width: parent.width+ height: 32++ Txt {+ anchors.left: parent.left+ anchors.leftMargin: 16+ anchors.verticalCenter: parent.verticalCenter+ text: qsTr("↑↓ to navigate · Enter to run · Esc to close")+ role: "caption"+ size: 11+ muted: true+ }++ Txt {+ anchors.right: parent.right+ anchors.rightMargin: 16+ anchors.verticalCenter: parent.verticalCenter+ text: {+ const hk = Preferences.commandPaletteHotkey.length > 0+ ? Preferences.commandPaletteHotkey+ : "Ctrl+K"+ if (AppController.commandPaletteOpenMs <= 0)+ return hk+ const msText = AppController.commandPaletteOpenMs < 10+ ? AppController.commandPaletteOpenMs.toFixed(1)+ : String(Math.round(AppController.commandPaletteOpenMs))+ return qsTr("%1 · %2 ms").arg(hk).arg(msText)+ }+ role: "caption"+ size: 11+ muted: true+ }+ }+ }+ }+}diff --git a/desktop/app/qml/components/IconButton.qml b/desktop/app/qml/components/IconButton.qmlindex 3a616a8..06f6e84 100644--- a/desktop/app/qml/components/IconButton.qml+++ b/desktop/app/qml/components/IconButton.qml@@ -2,22 +2,28 @@import QtQuickimport app.nectarlink-// A round icon-only button with a tooltip-style accessible name.+// A round icon button (or compact icon+text pill) with a hover tooltip and accessible name.FocusScope {id: buttonproperty string iconPath+ property string text: ""property string labelproperty bool tonal: falseproperty color iconColor: tonal ? Theme.secondaryContainerContent : Theme.surfaceContentVariantsignal clicked- implicitWidth: 36- implicitHeight: 36+ readonly property bool hasText: text.length > 0+ readonly property string effectiveTip: label.length > 0 ? label : text++ implicitWidth: hasText ? contentRow.implicitWidth + 24 : 36+ implicitHeight: hasText ? 32 : 36activeFocusOnTab: trueopacity: enabled ? 1 : 0.38+ Component.onDestruction: tooltipPill.shown = falseAccessible.role: Accessible.Button- Accessible.name: label+ Accessible.name: hasText ? text : label+ Accessible.description: hasText && label.length > 0 && label !== text ? label : ""Accessible.onPressAction: if (enabled) button.clicked()Keys.onPressed: (event) => {if (event.key === Qt.Key_Return || event.key === Qt.Key_Enter || event.key === Qt.Key_Space) {@@ -31,6 +37,8 @@ FocusScope {anchors.fill: parentradius: Theme.pill(height)color: button.tonal ? Theme.secondaryContainer : "transparent"+ border.width: Theme.graphite && button.hasText ? 1 : 0+ border.color: button.tonal ? Theme.surfaceContent : Theme.outlineVariantscale: tap.pressed ? Theme.pressScale : 1Behavior on scale { SpringAnimation { spring: Theme.springSnappy; damping: Theme.dampingSnappy } }@@ -42,11 +50,32 @@ FocusScope {Behavior on opacity { NumberAnimation { duration: Theme.fadeFast } }}Icon {+ visible: !button.hasTextanchors.centerIn: parentwidth: 18; height: 18path: button.iconPathcolor: button.iconColor}+ Row {+ id: contentRow+ visible: button.hasText+ anchors.centerIn: parent+ spacing: 6+ Icon {+ visible: button.iconPath.length > 0+ anchors.verticalCenter: parent.verticalCenter+ width: 15; height: 15+ path: button.iconPath+ color: button.iconColor+ }+ Txt {+ anchors.verticalCenter: parent.verticalCenter+ text: button.text+ role: "label"+ size: Theme.graphite ? 11 : 13+ color: button.tonal ? Theme.secondaryContainerContent : Theme.surfaceContent+ }+ }}Rectangle {anchors.fill: body@@ -58,6 +87,57 @@ FocusScope {visible: Theme.focusVisible(button)}+ Timer {+ id: tipTimer+ interval: 450+ running: hover.hovered && !tap.pressed && button.enabled && button.visible && button.effectiveTip.length > 0+ onRunningChanged: if (!running) tooltipPill.shown = false+ onTriggered: {+ tooltipPill.reposition()+ tooltipPill.shown = true+ }+ }++ Rectangle {+ id: tooltipPill+ property bool shown: false+ parent: button.Window.contentItem ? button.Window.contentItem : button+ z: 1000+ visible: opacity > 0 && button.effectiveTip.length > 0+ opacity: shown && hover.hovered && !tap.pressed && button.visible ? 1 : 0+ Behavior on opacity { NumberAnimation { duration: Theme.fadeFast } }+ width: Math.min(tipText.implicitWidth + 18, 300)+ height: tipText.implicitHeight + 10+ radius: Theme.graphite ? Theme.radiusXs : Theme.radiusSm+ color: Theme.graphite ? Theme.surface : Theme.surfaceContainerHighest+ border.width: 1+ border.color: Theme.outlineVariant+ onWidthChanged: if (shown) reposition()++ function reposition() {+ const host = parent+ if (!host || host.width <= 0 || host.height <= 0)+ return+ const w = width+ const h = height+ const below = button.mapToItem(host, button.width / 2, button.height + 6)+ const above = button.mapToItem(host, button.width / 2, -h - 6)+ const maxX = Math.max(8, host.width - w - 8)+ x = Math.round(Math.max(8, Math.min(maxX, below.x - w / 2)))+ y = Math.round((below.y + h + 8 <= host.height) ? below.y : Math.max(8, above.y))+ }++ Txt {+ id: tipText+ anchors.centerIn: parent+ width: Math.min(implicitWidth, 282)+ elide: Text.ElideRight+ role: "caption"+ color: Theme.surfaceContent+ text: button.effectiveTip+ }+ }+HoverHandler { id: hover; cursorShape: Qt.PointingHandCursor }TapHandler { id: tap; onTapped: button.clicked() }}diff --git a/desktop/app/qml/components/TransferItem.qml b/desktop/app/qml/components/TransferItem.qmlindex bd45e84..3bcba37 100644--- a/desktop/app/qml/components/TransferItem.qml+++ b/desktop/app/qml/components/TransferItem.qml@@ -19,7 +19,7 @@ Item {required property string reasonrequired property string savedPath- readonly property bool active: status === "waiting" || status === "running"+ readonly property bool active: status === "requested" || status === "waiting" || status === "running"implicitHeight: column.height + 20@@ -34,6 +34,10 @@ Item {function detail() {switch (status) {+ case "requested":+ return qsTr("%1 wants to send this · %2")+ .arg(item.deviceName.length > 0 ? item.deviceName : qsTr("Nearby device"))+ .arg(size(total))case "waiting":return item.incoming ? qsTr("Paused; continues when %1 is back").arg(item.deviceName): qsTr("Waiting for %1").arg(item.deviceName)@@ -85,7 +89,7 @@ Item {width: parent.widthheight: 4radius: 2- visible: item.active+ visible: item.status === "waiting" || item.status === "running"color: Theme.surfaceContainerHighestRectangle {height: parent.height@@ -109,6 +113,20 @@ Item {id: actionsanchors.verticalCenter: parent.verticalCenterspacing: 4+ Button {+ visible: item.status === "requested"+ variant: "fill"+ size: "sm"+ text: qsTr("Accept")+ onClicked: TransferList.accept(item.transferId)+ }+ Button {+ visible: item.status === "requested"+ variant: "outline"+ size: "sm"+ text: qsTr("Decline")+ onClicked: TransferList.cancel(item.transferId)+ }Button {visible: item.status === "done" && item.incoming && item.savedPath.length > 0variant: "text"@@ -123,7 +141,7 @@ Item {onClicked: TransferList.showInFolder(item.transferId)}IconButton {- visible: item.active+ visible: item.status === "waiting" || item.status === "running"iconPath: Icons.closelabel: qsTr("Cancel")onClicked: TransferList.cancel(item.transferId)diff --git a/desktop/app/qml/pages/HomePage.qml b/desktop/app/qml/pages/HomePage.qmlindex b14f729..3410541 100644--- a/desktop/app/qml/pages/HomePage.qml+++ b/desktop/app/qml/pages/HomePage.qml@@ -17,6 +17,27 @@ Item {property bool currentCanReceive: falsesignal pairRequested+ readonly property var localsendPeers: {+ if (!AppController.localsendEnabled || !AppController.localsendPeersJson || AppController.localsendPeersJson.length === 0)+ return []+ try {+ return JSON.parse(AppController.localsendPeersJson)+ } catch (e) {+ return []+ }+ }++ FileDialog {+ id: localsendFilePicker+ property string targetDeviceId: ""+ title: qsTr("Send files via LocalSend")+ fileMode: FileDialog.OpenFiles+ onAccepted: {+ if (targetDeviceId.length > 0 && selectedFiles.length > 0)+ TransferList.send(targetDeviceId, selectedFiles.map(url => url.toString()))+ }+ }+opacity: active ? 1 : 0visible: opacity > 0Behavior on opacity { NumberAnimation { duration: Theme.fadeNormal } }@@ -195,6 +216,49 @@ Item {visible: index === page.current}}++ Card {+ width: column.width+ visible: DeviceList.count === 0 && TransferList.count > 0+ Column {+ width: parent.width+ spacing: 8+ Item {+ width: parent.width+ height: Math.max(standaloneTransfersHeader.height, standaloneClearTransfers.height)+ Txt {+ id: standaloneTransfersHeader+ anchors.verticalCenter: parent.verticalCenter+ role: "label"+ muted: true+ text: qsTr("Transfers")+ }+ Button {+ id: standaloneClearTransfers+ anchors.right: parent.right+ anchors.verticalCenter: parent.verticalCenter+ visible: TransferList.count > TransferList.active+ variant: "text"+ size: "sm"+ text: qsTr("Clear finished")+ onClicked: TransferList.clearFinished()+ }+ }+ ListView {+ id: standaloneTransferList+ width: parent.width+ height: contentHeight+ interactive: false+ model: TransferList+ delegate: TransferItem { width: standaloneTransferList.width }+ }+ }+ }++ LocalSendCard {+ width: column.width+ visible: DeviceList.count === 0 && AppController.localsendEnabled+ }}}@@ -614,7 +678,7 @@ Item {width: 15height: 15path: Icons.battery- color: home.battery <= 15 && !home.charging ? Theme.danger : Theme.heroContent+ color: home.battery <= 15 && !home.charging ? Theme.error : Theme.heroContent}Txt {anchors.verticalCenter: parent.verticalCenter@@ -658,7 +722,7 @@ Item {height: parent.heightradius: 3color: home.battery <= 15 && !home.charging- ? Theme.danger+ ? Theme.error: (Theme.graphite ? Theme.heroContent : Theme.primary)}}@@ -897,7 +961,7 @@ Item {iconPath: Icons.clipboardfeature: home.feature("clipboard.pc_to_phone")onClicked: AppController.sendClipboard(home.deviceId)- sideIcon: Preferences.clipboardHistory ? Icons.history : ""+ sideIcon: Preferences.clipboardHistory ? Icons.clipboardList : ""sideLabel: qsTr("Clipboard history")sideAlwaysAvailable: trueonSideClicked: {@@ -937,6 +1001,95 @@ Item {onSideClicked: folderPicker.open()}}++ // ---- Continuity Camera ----+ Card {+ id: continuityHomeCard+ width: parent.width+ readonly property var continuityFeature: home.feature("camera.continuity")+ readonly property bool available: continuityFeature.state === "available"+ visible: home.online && (available || continuityFeature.state === "locked" || AppController.continuityCameraBusy)+ Column {+ width: parent.width+ spacing: 8+ Item {+ width: parent.width+ height: Math.max(continuityLeftRow.height, continuityRightRow.height)+ Row {+ id: continuityLeftRow+ anchors.left: parent.left+ anchors.right: continuityRightRow.left+ anchors.rightMargin: 12+ anchors.verticalCenter: parent.verticalCenter+ spacing: 10+ Icon {+ anchors.verticalCenter: parent.verticalCenter+ width: 18; height: 18+ path: Icons.camera+ color: Theme.primary+ }+ Column {+ width: parent.width - 28+ anchors.verticalCenter: parent.verticalCenter+ spacing: 1+ Txt {+ width: parent.width+ role: "label"+ elide: Text.ElideRight+ text: AppController.continuityCameraBusy+ ? AppController.continuityCameraStatus+ : qsTr("Continuity Camera")+ }+ Txt {+ width: parent.width+ role: "caption"+ muted: true+ elide: Text.ElideRight+ text: AppController.continuityCameraBusy+ ? qsTr("It goes on the clipboard and into the active window when ready.")+ : qsTr("With %1's camera, pasted where your cursor is.").arg(home.name)+ }+ }+ }+ Row {+ id: continuityRightRow+ anchors.right: parent.right+ anchors.verticalCenter: parent.verticalCenter+ spacing: 8+ Button {+ visible: !AppController.continuityCameraBusy+ enabled: continuityHomeCard.available+ variant: "tonal"+ size: "sm"+ iconPath: Icons.camera+ text: qsTr("Take photo")+ onClicked: AppController.startContinuityCamera("photo")+ }+ Button {+ visible: !AppController.continuityCameraBusy+ enabled: continuityHomeCard.available+ variant: "tonal"+ size: "sm"+ iconPath: Icons.clipboardList+ text: qsTr("Scan document")+ onClicked: AppController.startContinuityCamera("scan")+ }+ Button {+ visible: AppController.continuityCameraBusy+ variant: "outline"+ size: "sm"+ iconPath: Icons.close+ text: qsTr("Cancel")+ onClicked: AppController.cancelContinuityCamera()+ }+ }+ }+ LockChip {+ visible: !continuityHomeCard.available && label.length > 0+ feature: continuityHomeCard.continuityFeature+ }+ }+ }FileDialog {id: filePickertitle: qsTr("Send to %1").arg(home.name)@@ -973,7 +1126,6 @@ Item {Button {variant: "text"size: "sm"- iconPath: Icons.historytext: qsTr("Timeline")onClicked: AppController.currentPage = "timeline"}@@ -1006,6 +1158,12 @@ Item {}}+ // ---- LocalSend nearby ----+ LocalSendCard {+ width: parent.width+ visible: AppController.localsendEnabled+ }+// ---- Notifications ----Card {id: feed@@ -1026,15 +1184,14 @@ Item {muted: truetext: feed.count > 0 ? qsTr("Notifications · %1").arg(feed.count) : qsTr("Notifications")}- Button {+ IconButton {anchors.right: clearAll.visible ? clearAll.left : parent.rightanchors.rightMargin: clearAll.visible ? 8 : 0anchors.verticalCenter: parent.verticalCentervisible: NotificationList.historyEnabled && NotificationHistory.count > 0- variant: "text"- size: "sm"iconPath: Icons.historytext: qsTr("History")+ label: qsTr("Notification history (last 24 hours)")onClicked: {historySearch.text = ""historySheet.open()@@ -1698,7 +1855,7 @@ Item {Item {width: parent.widthheight: Math.max(historyTitle.height, clearHistory.height)- Txt { id: historyTitle; anchors.verticalCenter: parent.verticalCenter; text: qsTr("History"); role: "headline" }+ Txt { id: historyTitle; anchors.verticalCenter: parent.verticalCenter; text: qsTr("Notification history"); role: "headline" }Button {id: clearHistoryanchors.right: parent.right@@ -1759,13 +1916,13 @@ Item {event.accepted = text.length > 0text = ""}- Accessible.name: qsTr("Search history")+ Accessible.name: qsTr("Search notification history")Txt {anchors.verticalCenter: parent.verticalCentervisible: historySearch.text.length === 0role: "body"muted: true- text: qsTr("Search history")+ text: qsTr("Search notification history")}}}@@ -2365,4 +2522,118 @@ Item {maxWidth: parent.width}}++ // Nearby devices running LocalSend on the local Wi-Fi network.+ component LocalSendCard: Card {+ id: lsCard+ Column {+ width: parent.width+ spacing: 10+ Item {+ width: parent.width+ height: Math.max(lsHeaderRow.height, lsRefreshBtn.height)+ Row {+ id: lsHeaderRow+ anchors.left: parent.left+ anchors.verticalCenter: parent.verticalCenter+ spacing: 8+ StatusDot {+ anchors.verticalCenter: parent.verticalCenter+ online: AppController.localsendReceiving+ }+ Txt {+ anchors.verticalCenter: parent.verticalCenter+ role: "label"+ muted: true+ text: page.localsendPeers.length > 0+ ? qsTr("LocalSend nearby · %1").arg(page.localsendPeers.length)+ : qsTr("LocalSend nearby")+ }+ }+ Button {+ id: lsRefreshBtn+ anchors.right: parent.right+ anchors.verticalCenter: parent.verticalCenter+ variant: "text"+ size: "sm"+ text: qsTr("Refresh")+ onClicked: AppController.refreshLocalsend()+ }+ }+ Txt {+ width: parent.width+ visible: page.localsendPeers.length === 0+ role: "bodySmall"+ muted: true+ wrapMode: Text.WordWrap+ text: AppController.localsendReceiving+ ? qsTr("Listening for LocalSend devices on your Wi‑Fi network.")+ : qsTr("LocalSend port 53317 is busy on this PC (another LocalSend app may be running).")+ }+ Repeater {+ model: page.localsendPeers+ delegate: Item {+ id: peerRow+ required property var modelData+ required property int index+ width: parent.width+ height: Math.max(36, peerInfoCol.height + 8)++ Row {+ anchors.fill: parent+ spacing: 12+ Rectangle {+ anchors.verticalCenter: parent.verticalCenter+ width: 32; height: 32+ radius: Theme.graphite ? Theme.radiusSm : 16+ color: Theme.graphite ? "transparent" : Theme.secondaryContainer+ border.width: Theme.graphite ? 1 : 0+ border.color: Theme.outlineVariant+ Icon {+ anchors.centerIn: parent+ width: 18; height: 18+ path: peerRow.modelData.deviceType === "mobile" || peerRow.modelData.deviceType === "tablet"+ ? Icons.phone : Icons.laptop+ color: Theme.graphite ? Theme.surfaceContent : Theme.secondaryContainerContent+ }+ }+ Column {+ id: peerInfoCol+ anchors.verticalCenter: parent.verticalCenter+ width: parent.width - 32 - sendPeerBtn.width - 24+ spacing: 2+ Txt {+ width: parent.width+ role: "title"+ size: 14+ elide: Text.ElideRight+ text: peerRow.modelData.alias || qsTr("Nearby device")+ }+ Txt {+ width: parent.width+ role: "bodySmall"+ muted: true+ elide: Text.ElideRight+ text: [peerRow.modelData.deviceModel, peerRow.modelData.ip]+ .filter(s => s && s.length > 0)+ .join(" · ")+ }+ }+ Button {+ id: sendPeerBtn+ anchors.verticalCenter: parent.verticalCenter+ variant: "tonal"+ size: "sm"+ iconPath: Icons.send+ text: qsTr("Send files")+ onClicked: {+ localsendFilePicker.targetDeviceId = peerRow.modelData.id+ localsendFilePicker.open()+ }+ }+ }+ }+ }+ }+ }}diff --git a/desktop/app/qml/pages/MessagesPage.qml b/desktop/app/qml/pages/MessagesPage.qmlindex 5ee862c..6c3c4cf 100644--- a/desktop/app/qml/pages/MessagesPage.qml+++ b/desktop/app/qml/pages/MessagesPage.qml@@ -1,9 +1,11 @@// SPDX-License-Identifier: GPL-3.0-or-laterimport QtQuick+import QtQuick.Dialogsimport app.nectarlink-// Messages: the phone's conversations on the left, the open one on the-// right, and a box to text from the PC (sent through the phone).+// Messages: the phone's SMS and mirrored MessagingStyle chat conversations on+// the left, the open one on the right, and a box to text, attach an MMS image,+// or reply inline from the PC (sent through the phone).Item {id: pageproperty bool active: true@@ -16,12 +18,13 @@ Item {readonly property var threads: { try { return JSON.parse(Messages.threads) } catch (e) { return [] } }readonly property var messages: { try { return JSON.parse(Messages.messages) } catch (e) { return [] } }readonly property var contacts: { try { return JSON.parse(PhoneCall.contacts) } catch (e) { return [] } }+ readonly property var availableApps: { try { return JSON.parse(Messages.availableApps) } catch (e) { return [] } }readonly property var openThread: threads.find(t => t.id === Messages.thread) || nullreadonly property bool ready: Messages.status === "ready"// Conversations matching the search box.readonly property string query: search.text.trim().toLocaleLowerCase()readonly property var shownThreads: query.length === 0 ? threads- : threads.filter(t => (t.title + " " + t.addresses + " " + t.snippet).toLocaleLowerCase().indexOf(query) >= 0)+ : threads.filter(t => (t.title + " " + t.addresses + " " + (t.appName || "") + " " + (t.lastSender || "") + " " + t.snippet).toLocaleLowerCase().indexOf(query) >= 0)// Contacts matching the "To" field when starting a new message.readonly property string recipientQuery: toField.text.trim().toLocaleLowerCase()@@ -49,6 +52,18 @@ Item {return out}+ FileDialog {+ id: imageFileDialog+ title: qsTr("Attach image")+ nameFilters: [qsTr("Images (*.png *.jpg *.jpeg *.webp *.gif *.bmp)")]+ onAccepted: {+ if (selectedFile.toString().length > 0) {+ Messages.attachFile(selectedFile.toString())+ composer.focusText()+ }+ }+ }+// Back to the list: nothing open.function closeChat() {Messages.closeThread()@@ -110,7 +125,12 @@ Item {Connections {target: Messagesfunction onComposeToChanged() { page.applyComposeTarget() }- function onThreadChanged() { if (Messages.thread.length > 0) page.composing = false }+ function onThreadChanged() {+ if (Messages.thread.length > 0) {+ page.composing = false+ Qt.callLater(() => composer.focusText())+ }+ }}function load() {@@ -140,6 +160,15 @@ Item {enabled: page.active && page.readyonActivated: page.startNew()}+ Shortcut {+ sequence: StandardKey.Paste+ enabled: page.active && (!!page.openThread || page.composing) && !composer.isChat && !composer.groupSms && !input.activeFocus+ onActivated: {+ if (Messages.pasteClipboardImage()) {+ composer.focusText()+ }+ }+ }function startNew() {Messages.closeThread()composing = true@@ -208,13 +237,13 @@ Item {Spinner {anchors.centerIn: parent- visible: Messages.status === "loading" && page.threads.length === 0+ visible: Messages.status === "loading" && page.threads.length === 0 && !Messages.hasChatThreads}// ---- Conversations ----Item {id: list- visible: page.threads.length > 0 || (page.ready && page.deviceId.length > 0)+ visible: page.threads.length > 0 || Messages.hasChatThreads || (page.ready && page.deviceId.length > 0)width: Math.min(340, Math.max(260, page.width * 0.34))height: parent.height@@ -250,7 +279,7 @@ Item {}}- // Search: by name, number or the latest text.+ // Search: by name, number, app, or the latest text.Rectangle {id: searchBoxanchors.top: listHeader.bottom@@ -310,9 +339,40 @@ Item {}}+ // Calm app filter chips when MessagingStyle chat conversations are present.+ Flickable {+ id: appFilterBar+ anchors.top: searchBox.bottom+ anchors.topMargin: visible ? 8 : 0+ anchors.left: parent.left+ anchors.right: parent.right+ height: visible ? 34 : 0+ visible: Messages.hasChatThreads && page.availableApps.length > 2+ contentWidth: filterRow.implicitWidth + 32+ clip: true+ boundsBehavior: Flickable.StopAtBounds++ Row {+ id: filterRow+ x: 16+ anchors.verticalCenter: parent.verticalCenter+ spacing: 6+ Repeater {+ model: page.availableApps+ delegate: Chip {+ required property var modelData+ text: modelData.label+ selected: Messages.appFilter === modelData.key+ HoverHandler { cursorShape: Qt.PointingHandCursor }+ TapHandler { onTapped: Messages.filterByApp(modelData.key) }+ }+ }+ }+ }+ListView {id: threadList- anchors.top: searchBox.bottom+ anchors.top: appFilterBar.bottomanchors.topMargin: 8anchors.bottom: parent.bottomwidth: parent.width@@ -325,7 +385,7 @@ Item {}Txt {anchors.centerIn: parent- visible: page.ready && page.shownThreads.length === 0+ visible: (page.ready || Messages.hasChatThreads) && page.shownThreads.length === 0role: "body"muted: truetext: page.query.length > 0 ? qsTr("No conversations match.") : qsTr("No conversations yet.")@@ -342,6 +402,54 @@ Item {height: parent.heightvisible: list.visible+ // Drag-and-drop image onto an open SMS conversation or new SMS.+ DropArea {+ id: mmsDropArea+ anchors.fill: parent+ enabled: (!!page.openThread || page.composing) && !composer.isChat && !composer.groupSms+ onDropped: (drop) => {+ if (drop.hasUrls && drop.urls.length > 0) {+ if (Messages.attachFile(drop.urls[0].toString())) {+ drop.acceptProposedAction()+ composer.focusText()+ }+ }+ }+ }++ Rectangle {+ anchors.fill: parent+ anchors.margins: 16+ z: 10+ visible: mmsDropArea.containsDrag+ radius: Theme.radiusLg+ color: Qt.rgba(Theme.primaryContainer.r, Theme.primaryContainer.g, Theme.primaryContainer.b, 0.90)+ border.width: 2+ border.color: Theme.primary+ Column {+ anchors.centerIn: parent+ spacing: 8+ Icon {+ anchors.horizontalCenter: parent.horizontalCenter+ width: 32; height: 32+ path: Icons.photo+ color: Theme.primaryContainerContent+ }+ Txt {+ anchors.horizontalCenter: parent.horizontalCenter+ role: "title"+ color: Theme.primaryContainerContent+ text: qsTr("Drop image to attach (MMS)")+ }+ Txt {+ anchors.horizontalCenter: parent.horizontalCenter+ role: "bodySmall"+ color: Theme.primaryContainerContent+ text: qsTr("PNG, JPEG, WebP, GIF, or BMP up to 900 KB")+ }+ }+ }+Txt {anchors.centerIn: parentvisible: !page.openThread && !page.composing && list.visible@@ -367,21 +475,46 @@ Item {Column {anchors.left: backButton.rightanchors.leftMargin: 8- anchors.right: callThreadButton.visible ? callThreadButton.left : parent.right- anchors.rightMargin: callThreadButton.visible ? 8 : 24+ anchors.right: callThreadButton.visible ? callThreadButton.left+ : deleteChatButton.visible ? deleteChatButton.left : parent.right+ anchors.rightMargin: (callThreadButton.visible || deleteChatButton.visible) ? 8 : 24anchors.verticalCenter: parent.verticalCenterspacing: 2visible: !page.composing- Txt {+ Row {width: parent.width- text: page.openThread ? page.openThread.title : ""- role: "title"- elide: Text.ElideRight+ spacing: 8+ Txt {+ id: headerTitleTxt+ width: Math.min(implicitWidth, parent.width - (headerAppPill.visible ? headerAppPill.width + 8 : 0))+ text: page.openThread ? page.openThread.title : ""+ role: "title"+ elide: Text.ElideRight+ }+ Rectangle {+ id: headerAppPill+ anchors.verticalCenter: headerTitleTxt.verticalCenter+ visible: !!page.openThread && !!page.openThread.isChat+ height: 20+ width: headerAppPillLabel.implicitWidth + 14+ radius: 10+ color: Theme.secondaryContainer+ Txt {+ id: headerAppPillLabel+ anchors.centerIn: parent+ role: "label"+ size: 11+ color: Theme.secondaryContainerContent+ text: page.openThread && page.openThread.appName ? page.openThread.appName : Messages.currentAppName+ }+ }}Txt {width: parent.width- visible: page.openThread && page.openThread.title !== page.openThread.addresses- text: page.openThread ? page.openThread.addresses : ""+ visible: page.openThread && (page.openThread.isChat || page.openThread.title !== page.openThread.addresses)+ text: !page.openThread ? ""+ : page.openThread.isChat ? qsTr("Only messages that arrived while connected")+ : page.openThread.addressesrole: "bodySmall"muted: trueelide: Text.ElideRight@@ -392,13 +525,23 @@ Item {anchors.right: parent.rightanchors.rightMargin: 14anchors.verticalCenter: parent.verticalCenter- visible: !page.composing && !!page.openThread && !page.openThread.group+ visible: !page.composing && !!page.openThread && !page.openThread.isChat && !page.openThread.group&& page.openThread.addresses.length > 0 && PhoneCall.canDialenabled: !PhoneCall.dialingiconPath: Icons.calllabel: page.openThread ? qsTr("Call %1").arg(page.openThread.title) : qsTr("Call")onClicked: if (page.openThread) PhoneCall.dial(page.openThread.addresses)}+ IconButton {+ id: deleteChatButton+ anchors.right: parent.right+ anchors.rightMargin: 14+ anchors.verticalCenter: parent.verticalCenter+ visible: !page.composing && !!page.openThread && !!page.openThread.isChat+ iconPath: Icons.trash+ label: qsTr("Remove conversation from PC")+ onClicked: if (page.openThread) Messages.deleteChatThread(page.openThread.id)+ }// New message: who to.Row {anchors.left: backButton.right@@ -535,7 +678,7 @@ Item {ListView {id: messageListanchors.top: chatHeader.bottom- anchors.bottom: composer.top+ anchors.bottom: attachmentBar.visible ? attachmentBar.top : composer.topanchors.bottomMargin: 8width: parent.widthvisible: !!page.openThread@@ -589,20 +732,92 @@ Item {width: Math.min(parent.width - 48, 420)horizontalAlignment: Text.AlignHCenterwrapMode: Text.WordWrap- visible: page.openThread && page.messages.length === 0 && page.ready+ visible: page.openThread && page.messages.length === 0 && (page.ready || Messages.currentIsChat)role: "body"muted: true- text: qsTr("No messages to show. Android keeps texts with one-time codes from other apps for a few hours.")+ text: Messages.currentIsChat+ ? qsTr("Only messages that arrived while connected.")+ : qsTr("No messages to show. Android keeps texts with one-time codes from other apps for a few hours.")+ }+ }++ // Staged MMS image attachment preview card.+ Rectangle {+ id: attachmentBar+ visible: (!!page.openThread || page.composing) && !composer.isChat && Messages.hasAttachment+ anchors.left: composer.left+ anchors.right: composer.right+ anchors.bottom: composer.top+ anchors.bottomMargin: 8+ height: 64+ radius: Theme.radiusMd+ color: Theme.surfaceContainerHigh+ border.width: 1+ border.color: Messages.attachmentError.length > 0 ? Theme.error : Theme.outlineVariant++ RoundedImage {+ id: attThumb+ x: 8+ anchors.verticalCenter: parent.verticalCenter+ width: 48; height: 48+ radius: Theme.radiusSm+ visible: Messages.attachmentPreview.length > 0+ source: Messages.attachmentPreview+ sourceSize: Qt.size(96, 96)+ fillMode: Image.PreserveAspectCrop+ }+ Column {+ anchors.left: attThumb.visible ? attThumb.right : parent.left+ anchors.leftMargin: 12+ anchors.right: removeAttBtn.left+ anchors.rightMargin: 8+ anchors.verticalCenter: parent.verticalCenter+ spacing: 2+ Txt {+ width: parent.width+ role: "bodySmall"+ weight: 600+ elide: Text.ElideRight+ text: Messages.attachmentName+ }+ Txt {+ width: parent.width+ role: "caption"+ color: Messages.attachmentError.length > 0 ? Theme.error : Theme.surfaceContentVariant+ elide: Text.ElideRight+ text: Messages.attachmentError.length > 0+ ? Messages.attachmentError+ : qsTr("%1 · Sent as MMS").arg(Messages.attachmentSize)+ }+ }+ IconButton {+ id: removeAttBtn+ anchors.right: parent.right+ anchors.rightMargin: 6+ anchors.verticalCenter: parent.verticalCenter+ width: 32; height: 32+ iconPath: Icons.close+ label: qsTr("Remove attachment")+ onClicked: Messages.clearAttachment()}}- // Writing a text.+ // Writing a text or inline chat reply.Rectangle {id: composervisible: !!page.openThread || page.composing- readonly property bool group: !!page.openThread && page.openThread.group- readonly property bool canSend: page.ready && !Messages.sending && !group- && input.text.trim().length > 0 && (!page.composing || toField.text.trim().length > 0)+ readonly property bool isChat: Messages.currentIsChat && !page.composing+ readonly property bool groupSms: !isChat && !!page.openThread && page.openThread.group+ readonly property bool chatReplyDisabled: isChat && !Messages.currentCanReply+ readonly property bool inputDisabled: groupSms || chatReplyDisabled+ readonly property bool validAttachment: !isChat && Messages.hasAttachment && Messages.attachmentError.length === 0+ readonly property bool hasAttachmentError: !isChat && Messages.hasAttachment && Messages.attachmentError.length > 0+ readonly property bool canSend: !Messages.sending+ && !inputDisabled+ && !hasAttachmentError+ && (isChat ? Messages.currentCanReply : page.ready)+ && (input.text.trim().length > 0 || validAttachment)+ && (!page.composing || toField.text.trim().length > 0)function focusText() { input.forceActiveFocus() }function send() {if (!canSend)@@ -625,10 +840,30 @@ Item {border.width: input.activeFocus ? 2 : 1border.color: input.activeFocus ? Theme.primary : Theme.outlineVariant+ IconButton {+ id: attachButton+ anchors.left: parent.left+ anchors.leftMargin: 6+ anchors.bottom: parent.bottom+ anchors.bottomMargin: 6+ visible: !composer.isChat && !composer.groupSms+ enabled: page.ready && !Messages.sending+ iconPath: Icons.photo+ label: qsTr("Attach image (MMS)")+ onClicked: imageFileDialog.open()+ TapHandler {+ acceptedButtons: Qt.RightButton+ onTapped: {+ if (Messages.pasteClipboardImage())+ composer.focusText()+ }+ }+ }+Flickable {id: inputScroll- anchors.left: parent.left- anchors.leftMargin: 18+ anchors.left: attachButton.visible ? attachButton.right : parent.left+ anchors.leftMargin: attachButton.visible ? 4 : 18anchors.right: sendButton.leftanchors.rightMargin: 8anchors.top: parent.top@@ -642,15 +877,25 @@ Item {id: inputwidth: inputScroll.widthwrapMode: TextEdit.Wrap- readOnly: composer.group+ readOnly: composer.inputDisabledfont.family: Theme.fontUifont.pixelSize: 14color: Theme.surfaceContentselectionColor: Theme.primaryContainerselectedTextColor: Theme.primaryContainerContent- Accessible.name: qsTr("Text message")- // Enter sends; Shift+Enter starts a new line.+ Accessible.name: composer.isChat+ ? qsTr("Reply in %1").arg(Messages.currentAppName)+ : qsTr("Text message")+ // Enter sends; Shift+Enter starts a new line; Ctrl+V pastes clipboard image when available.Keys.onPressed: (event) => {+ if (!composer.isChat && !composer.groupSms+ && ((event.key === Qt.Key_V && (event.modifiers & Qt.ControlModifier))+ || event.matches(StandardKey.Paste))) {+ if (Messages.pasteClipboardImage()) {+ event.accepted = true+ return+ }+ }if ((event.key === Qt.Key_Return || event.key === Qt.Key_Enter) && !(event.modifiers & Qt.ShiftModifier)) {composer.send()event.accepted = true@@ -666,8 +911,17 @@ Item {visible: input.text.length === 0role: "body"muted: true- text: composer.group ? qsTr("Group texts can't be sent from the PC yet")- : qsTr("Text message")+ elide: Text.ElideRight+ width: input.width+ text: composer.chatReplyDisabled+ ? qsTr("Replies are available while the notification is active on your phone")+ : composer.isChat+ ? qsTr("Reply in %1…").arg(Messages.currentAppName)+ : composer.groupSms+ ? qsTr("Group texts can't be sent from the PC yet")+ : Messages.hasAttachment+ ? qsTr("Add a caption (optional)…")+ : qsTr("Text message")}}}@@ -691,9 +945,9 @@ Item {Txt {anchors.right: parent.rightanchors.rightMargin: 16- anchors.bottom: parent.top+ anchors.bottom: attachmentBar.visible ? attachmentBar.top : parent.topanchors.bottomMargin: 4- visible: input.text.length > 120+ visible: !composer.isChat && input.text.length > 120role: "bodySmall"muted: truetext: qsTr("%1 characters · %n text(s)", "", page.smsCount(input.text)).arg(input.text.length)@@ -706,10 +960,11 @@ Item {id: rowproperty var threadreadonly property bool selected: thread.id === Messages.thread+ readonly property bool isChat: !!thread.isChatheight: 72activeFocusOnTab: trueAccessible.role: Accessible.ListItem- Accessible.name: thread.title + ". " + thread.snippet+ Accessible.name: thread.title + (isChat && thread.appName ? " (" + thread.appName + ")" : "") + ". " + thread.snippetAccessible.onPressAction: open()function open() {page.composing = false@@ -757,6 +1012,25 @@ Item {hue: named && !row.thread.group ? page.hueOf(row.thread.title) : -1emphasized: row.thread.unread > 0}+ Rectangle {+ anchors.right: parent.right+ anchors.bottom: parent.bottom+ anchors.rightMargin: -2+ anchors.bottomMargin: -2+ width: 18; height: 18+ radius: 9+ visible: row.isChat && !!row.thread.appIcon && row.thread.appIcon.length > 0 && appBadgeImg.status === Image.Ready+ color: Theme.surface+ RoundedImage {+ id: appBadgeImg+ anchors.centerIn: parent+ width: 14; height: 14+ radius: 7+ source: row.thread.appIcon || ""+ sourceSize: Qt.size(28, 28)+ fillMode: Image.PreserveAspectCrop+ }+ }}Column {anchors.left: face.right@@ -771,13 +1045,31 @@ Item {Txt {id: nameanchors.left: parent.left- anchors.right: when.left- anchors.rightMargin: 8+ width: Math.min(implicitWidth, parent.width - when.width - (appTag.visible ? appTag.width + 12 : 8))text: row.thread.titlerole: "body"weight: row.thread.unread > 0 ? 650 : 500elide: Text.ElideRight}+ Rectangle {+ id: appTag+ anchors.left: name.right+ anchors.leftMargin: 6+ anchors.verticalCenter: name.verticalCenter+ visible: row.isChat && !!row.thread.appName && row.thread.appName.length > 0+ height: 18+ width: appTagTxt.implicitWidth + 12+ radius: 9+ color: row.selected ? Theme.surfaceContainerHigh : Theme.surfaceContainerHighest+ Txt {+ id: appTagTxt+ anchors.centerIn: parent+ role: "label"+ size: 10+ muted: true+ text: row.thread.appName || ""+ }+ }Txt {id: whenanchors.right: parent.right@@ -795,7 +1087,13 @@ Item {anchors.left: parent.leftanchors.right: badge.visible ? badge.left : parent.rightanchors.rightMargin: 8- text: row.thread.snippet+ text: {+ const s = row.thread.snippet || ""+ const sender = row.thread.lastSender || ""+ if (row.isChat && row.thread.group && sender.length > 0 && s.indexOf(sender + ":") !== 0)+ return sender + ": " + s+ return s+ }role: "bodySmall"muted: row.thread.unread === 0maximumLineCount: 1diff --git a/desktop/app/qml/pages/PhotosPage.qml b/desktop/app/qml/pages/PhotosPage.qmlindex 79a0adc..370db50 100644--- a/desktop/app/qml/pages/PhotosPage.qml+++ b/desktop/app/qml/pages/PhotosPage.qml@@ -409,10 +409,49 @@ Item {anchors.rightMargin: 16anchors.verticalCenter: parent.verticalCenterspacing: 8- visible: page.selectedCount === 0 && !page.selectMode && Photos.count > 0+ visible: page.selectedCount === 0 && !page.selectMode++ Txt {+ anchors.verticalCenter: parent.verticalCenter+ visible: AppController.continuityCameraBusy+ role: "caption"+ muted: true+ text: AppController.continuityCameraStatus+ }++ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: AppController.continuityCameraBusy+ variant: "outline"+ size: "sm"+ iconPath: Icons.close+ text: qsTr("Cancel")+ onClicked: AppController.cancelContinuityCamera()+ }++ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: !AppController.continuityCameraBusy+ variant: "tonal"+ size: "sm"+ iconPath: Icons.camera+ text: gridPane.compactBar ? qsTr("Photo") : qsTr("Take photo")+ onClicked: AppController.startContinuityCamera("photo")+ }++ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: !AppController.continuityCameraBusy+ variant: "tonal"+ size: "sm"+ iconPath: Icons.clipboardList+ text: gridPane.compactBar ? qsTr("Scan") : qsTr("Scan document")+ onClicked: AppController.startContinuityCamera("scan")+ }Chip {anchors.verticalCenter: parent.verticalCenter+ visible: Photos.count > 0iconPath: Icons.foldertext: {const f = Photos.saveFolder || ""@@ -427,6 +466,7 @@ Item {Button {anchors.verticalCenter: parent.verticalCenter+ visible: Photos.count > 0variant: "tonal"size: "sm"iconPath: Icons.checkdiff --git a/desktop/app/qml/pages/SettingsPage.qml b/desktop/app/qml/pages/SettingsPage.qmlindex e75f3e4..1387e6d 100644--- a/desktop/app/qml/pages/SettingsPage.qml+++ b/desktop/app/qml/pages/SettingsPage.qml@@ -10,10 +10,38 @@ Item {property bool active: falsesignal pairRequested- onActiveChanged: Webcam.setPreviewActive(active)- Component.onCompleted: if (active) Webcam.setPreviewActive(true)+ onActiveChanged: {+ Webcam.setPreviewActive(active)+ if (active)+ AppController.refreshDataRetention()+ }+ Component.onCompleted: {+ if (active) {+ Webcam.setPreviewActive(true)+ AppController.refreshDataRetention()+ }+ }Component.onDestruction: Webcam.setPreviewActive(false)+ readonly property var retentionSummary: {+ try {+ return JSON.parse(AppController.dataRetentionSummary || "{}")+ } catch (e) {+ return ({})+ }+ }++ function formatRetentionBytes(bytes) {+ const b = Number(bytes || 0)+ if (b <= 0)+ return "0 B"+ if (b < 1024)+ return b + " B"+ if (b < 1024 * 1024)+ return Math.max(1, Math.round(b / 1024)) + " KB"+ return (b / (1024 * 1024)).toFixed(1) + " MB"+ }+opacity: active ? 1 : 0visible: opacity > 0Behavior on opacity { NumberAnimation { duration: Theme.fadeNormal } }@@ -500,58 +528,6 @@ Item {}}Divider { width: parent.width }- ListRow {- width: parent.width- iconPath: Icons.history- title: qsTr("Keep clipboard history")- description: qsTr("Keep the last 50 text and image clips shared between your devices, encrypted on this PC. Passwords and one-time codes are never saved.")- Row {- spacing: 10- Button {- anchors.verticalCenter: parent.verticalCenter- visible: Preferences.clipboardHistory && AppController.clipboardHistory.length > 2- variant: "text"- size: "sm"- text: qsTr("Clear")- onClicked: AppController.clearClipboardHistory()- }- Toggle {- anchors.verticalCenter: parent.verticalCenter- label: qsTr("Keep clipboard history")- checked: Preferences.clipboardHistory- onToggled: (on) => Preferences.clipboardHistory = on- }- }- }- Divider { width: parent.width }- ListRow {- width: parent.width- iconPath: Icons.history- title: qsTr("Timeline retention")- description: qsTr("Keep a searchable local history of files, clips, links, photos, recordings and sessions on this PC (up to 5,000 items).")- Row {- spacing: 8- Button {- anchors.verticalCenter: parent.verticalCenter- variant: "text"- size: "sm"- text: qsTr("Open")- onClicked: AppController.currentPage = "timeline"- }- Segmented {- anchors.verticalCenter: parent.verticalCenter- options: [- { value: "30", label: qsTr("30d") },- { value: "90", label: qsTr("90d") },- { value: "365", label: qsTr("1y") },- { value: "0", label: qsTr("All") }- ]- value: String(Preferences.timelineRetentionDays)- onPicked: (value) => Preferences.timelineRetentionDays = Number(value)- }- }- }- Divider { width: parent.width }ListRow {width: parent.widthiconPath: Icons.copy@@ -588,6 +564,18 @@ Item {}}Divider { width: parent.width }+ ListRow {+ width: parent.width+ iconPath: Icons.globe+ title: qsTr("LocalSend on local network")+ description: qsTr("Send and receive files with devices running LocalSend on the same Wi‑Fi network without pairing. Incoming files ask before saving.")+ Toggle {+ label: qsTr("LocalSend on local network")+ checked: AppController.localsendEnabled+ onToggled: (on) => AppController.toggleLocalsend(on)+ }+ }+ Divider { width: parent.width }ListRow {width: parent.widthiconPath: Icons.battery@@ -599,6 +587,147 @@ Item {onToggled: (on) => Preferences.batteryAlerts = on}}+ Divider { width: parent.width }+ ListRow {+ width: parent.width+ iconPath: Icons.search+ title: qsTr("Command palette shortcut")+ description: qsTr("Global shortcut to open the Command Palette from any app so you can run actions, text or call contacts, or mirror apps.")+ Row {+ spacing: 8+ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: Preferences.commandPaletteHotkey !== "Ctrl+Alt+Space"+ variant: "text"+ size: "sm"+ text: qsTr("Reset")+ onClicked: {+ paletteHotkeyInput.text = "Ctrl+Alt+Space"+ Preferences.commandPaletteHotkey = "Ctrl+Alt+Space"+ }+ }+ Rectangle {+ anchors.verticalCenter: parent.verticalCenter+ width: 132+ height: 32+ radius: Theme.radiusSm+ color: Theme.surfaceContainerHighest+ border.width: paletteHotkeyInput.activeFocus ? 2 : 1+ border.color: paletteHotkeyInput.activeFocus ? Theme.primary : Theme.outlineVariant+ TextInput {+ id: paletteHotkeyInput+ anchors.fill: parent+ anchors.leftMargin: 10+ anchors.rightMargin: 10+ verticalAlignment: TextInput.AlignVCenter+ horizontalAlignment: TextInput.AlignHCenter+ font.family: Theme.fontUi+ font.pixelSize: 13+ color: Theme.surfaceContent+ selectionColor: Theme.primaryContainer+ selectedTextColor: Theme.primaryContainerContent+ clip: true+ text: Preferences.commandPaletteHotkey+ onEditingFinished: Preferences.commandPaletteHotkey = text.trim()+ Accessible.name: qsTr("Command palette shortcut")+ }+ }+ }+ }+ Divider { width: parent.width }+ ListRow {+ width: parent.width+ iconPath: Icons.camera+ title: qsTr("Take photo shortcut")+ description: qsTr("Global shortcut to open your phone's camera from any app and paste the photo back into the active window.")+ Row {+ spacing: 8+ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: Preferences.continuityPhotoHotkey !== "Ctrl+Alt+C"+ variant: "text"+ size: "sm"+ text: qsTr("Reset")+ onClicked: {+ photoHotkeyInput.text = "Ctrl+Alt+C"+ Preferences.continuityPhotoHotkey = "Ctrl+Alt+C"+ }+ }+ Rectangle {+ anchors.verticalCenter: parent.verticalCenter+ width: 132+ height: 32+ radius: Theme.radiusSm+ color: Theme.surfaceContainerHighest+ border.width: photoHotkeyInput.activeFocus ? 2 : 1+ border.color: photoHotkeyInput.activeFocus ? Theme.primary : Theme.outlineVariant+ TextInput {+ id: photoHotkeyInput+ anchors.fill: parent+ anchors.leftMargin: 10+ anchors.rightMargin: 10+ verticalAlignment: TextInput.AlignVCenter+ horizontalAlignment: TextInput.AlignHCenter+ font.family: Theme.fontUi+ font.pixelSize: 13+ color: Theme.surfaceContent+ selectionColor: Theme.primaryContainer+ selectedTextColor: Theme.primaryContainerContent+ clip: true+ text: Preferences.continuityPhotoHotkey+ onEditingFinished: Preferences.continuityPhotoHotkey = text.trim()+ Accessible.name: qsTr("Take photo shortcut")+ }+ }+ }+ }+ Divider { width: parent.width }+ ListRow {+ width: parent.width+ iconPath: Icons.clipboardList+ title: qsTr("Scan document shortcut")+ description: qsTr("Global shortcut to scan a document with your phone's camera and paste the cropped scan into the active window.")+ Row {+ spacing: 8+ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: Preferences.continuityScanHotkey !== "Ctrl+Alt+D"+ variant: "text"+ size: "sm"+ text: qsTr("Reset")+ onClicked: {+ scanHotkeyInput.text = "Ctrl+Alt+D"+ Preferences.continuityScanHotkey = "Ctrl+Alt+D"+ }+ }+ Rectangle {+ anchors.verticalCenter: parent.verticalCenter+ width: 132+ height: 32+ radius: Theme.radiusSm+ color: Theme.surfaceContainerHighest+ border.width: scanHotkeyInput.activeFocus ? 2 : 1+ border.color: scanHotkeyInput.activeFocus ? Theme.primary : Theme.outlineVariant+ TextInput {+ id: scanHotkeyInput+ anchors.fill: parent+ anchors.leftMargin: 10+ anchors.rightMargin: 10+ verticalAlignment: TextInput.AlignVCenter+ horizontalAlignment: TextInput.AlignHCenter+ font.family: Theme.fontUi+ font.pixelSize: 13+ color: Theme.surfaceContent+ selectionColor: Theme.primaryContainer+ selectedTextColor: Theme.primaryContainerContent+ clip: true+ text: Preferences.continuityScanHotkey+ onEditingFinished: Preferences.continuityScanHotkey = text.trim()+ Accessible.name: qsTr("Scan document shortcut")+ }+ }+ }+ }}}@@ -865,18 +994,6 @@ Item {width: parent.widthColumn {width: parent.width- ListRow {- width: parent.width- iconPath: Icons.history- title: qsTr("Keep a day of history")- description: qsTr("Notifications that go away on your phone stay in History on this PC for a day.")- Toggle {- label: qsTr("Keep a day of history")- checked: NotificationList.historyEnabled- onToggled: (on) => NotificationList.setHistoryEnabled(on)- }- }- Divider { width: parent.width }ListRow {width: parent.widthiconPath: Icons.moon@@ -957,6 +1074,217 @@ Item {}}+ // ---- Data & storage ----+ Txt { text: qsTr("Data & storage"); role: "label"; muted: true }+ Card {+ width: parent.width+ Column {+ width: parent.width+ // 1. Clipboard history+ ListRow {+ width: parent.width+ iconPath: Icons.clipboard+ title: qsTr("Clipboard history")+ description: {+ const count = Number(page.retentionSummary.clipboardItems || 0)+ const size = page.formatRetentionBytes(page.retentionSummary.clipboardBytes)+ return qsTr("7 days · up to 50 text and image clips encrypted on this PC (%1 stored · %2). Passwords and one-time codes are never saved.")+ .arg(count)+ .arg(size)+ }+ Row {+ spacing: 10+ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: Number(page.retentionSummary.clipboardItems || 0) > 0+ variant: "text"+ size: "sm"+ text: qsTr("Clear")+ onClicked: {+ AppController.clearClipboardHistory()+ AppController.refreshDataRetention()+ }+ }+ Toggle {+ anchors.verticalCenter: parent.verticalCenter+ label: qsTr("Clipboard history")+ checked: Preferences.clipboardHistory+ onToggled: (on) => {+ Preferences.clipboardHistory = on+ AppController.refreshDataRetention()+ }+ }+ }+ }+ Divider { width: parent.width }+ // 2. Cross-device timeline+ ListRow {+ width: parent.width+ iconPath: Icons.history+ title: qsTr("Cross-device timeline")+ description: {+ const count = Number(page.retentionSummary.timelineItems || 0)+ return qsTr("Searchable local history of files, clips, links, photos, recordings and sessions (%1 items · up to 5,000).")+ .arg(count)+ }+ Row {+ spacing: 8+ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: Number(page.retentionSummary.timelineItems || 0) > 0+ variant: "text"+ size: "sm"+ text: qsTr("Clear")+ onClicked: {+ TimelineModel.clearAll()+ AppController.refreshDataRetention()+ }+ }+ Button {+ anchors.verticalCenter: parent.verticalCenter+ variant: "text"+ size: "sm"+ text: qsTr("Open")+ onClicked: AppController.currentPage = "timeline"+ }+ Segmented {+ anchors.verticalCenter: parent.verticalCenter+ options: [+ { value: "30", label: qsTr("30d") },+ { value: "90", label: qsTr("90d") },+ { value: "365", label: qsTr("1y") },+ { value: "0", label: qsTr("All") }+ ]+ value: String(Preferences.timelineRetentionDays)+ onPicked: (value) => {+ Preferences.timelineRetentionDays = Number(value)+ AppController.refreshDataRetention()+ }+ }+ }+ }+ Divider { width: parent.width }+ // 3. Notification history+ ListRow {+ width: parent.width+ iconPath: Icons.bell+ title: qsTr("Notification history")+ description: {+ const count = Number(page.retentionSummary.notificationHistory || 0)+ const size = page.formatRetentionBytes(page.retentionSummary.notificationImageBytes)+ return qsTr("24 hours · up to 100 dismissed notifications (%1 stored · %2 of cached images).")+ .arg(count)+ .arg(size)+ }+ Row {+ spacing: 10+ Button {+ anchors.verticalCenter: parent.verticalCenter+ visible: Number(page.retentionSummary.notificationHistory || 0) > 0+ || Number(page.retentionSummary.notificationImageBytes || 0) > 0+ variant: "text"+ size: "sm"+ text: qsTr("Clear")+ onClicked: AppController.clearNotificationHistory()+ }+ Toggle {+ anchors.verticalCenter: parent.verticalCenter+ label: qsTr("Notification history")+ checked: NotificationList.historyEnabled+ onToggled: (on) => {+ NotificationList.setHistoryEnabled(on)+ AppController.refreshDataRetention()+ }+ }+ }+ }+ Divider { width: parent.width }+ // 4. Cached messages & conversations+ ListRow {+ width: parent.width+ iconPath: Icons.messages+ title: qsTr("Cached messages & conversations")+ description: {+ const chatThreads = Number(page.retentionSummary.chatThreads || 0)+ const smsThreads = Number(page.retentionSummary.cachedSmsThreads || 0)+ const totalMsgs = Number(page.retentionSummary.chatMessages || 0)+ + Number(page.retentionSummary.cachedSmsMessages || 0)+ const size = page.formatRetentionBytes(page.retentionSummary.messageAttachmentBytes)+ return qsTr("90 days · up to 500 messages per conversation (%1 conversations · %2 messages · %3 of attachments).")+ .arg(chatThreads + smsThreads)+ .arg(totalMsgs)+ .arg(size)+ }+ Button {+ variant: "text"+ size: "sm"+ visible: Number(page.retentionSummary.chatThreads || 0) > 0+ || Number(page.retentionSummary.cachedSmsThreads || 0) > 0+ || Number(page.retentionSummary.chatMessages || 0) > 0+ || Number(page.retentionSummary.cachedSmsMessages || 0) > 0+ || Number(page.retentionSummary.messageAttachmentBytes || 0) > 0+ text: qsTr("Clear")+ onClicked: AppController.clearMessageCache()+ }+ }+ Divider { width: parent.width }+ // 5. Photo thumbnails cache+ ListRow {+ width: parent.width+ iconPath: Icons.photo+ title: qsTr("Photo thumbnails cache")+ description: {+ const files = Number(page.retentionSummary.photoThumbFiles || 0)+ const size = page.formatRetentionBytes(page.retentionSummary.photoThumbBytes)+ return qsTr("30 days · up to 500 cached thumbnails (%1 files · %2). Original photos on your phone are never touched.")+ .arg(files)+ .arg(size)+ }+ Button {+ variant: "text"+ size: "sm"+ visible: Number(page.retentionSummary.photoThumbFiles || 0) > 0+ text: qsTr("Clear")+ onClicked: AppController.clearPhotoThumbnails()+ }+ }+ Divider { width: parent.width }+ // 6. Received file transfer history+ ListRow {+ width: parent.width+ iconPath: Icons.send+ title: qsTr("Received file transfer history")+ description: {+ const records = Number(page.retentionSummary.receivedFileRecords || 0)+ return qsTr("Kept with timeline · %1 transfer records on this PC. Saved files in Downloads are kept.")+ .arg(records)+ }+ Button {+ variant: "text"+ size: "sm"+ visible: Number(page.retentionSummary.receivedFileRecords || 0) > 0+ text: qsTr("Clear")+ onClicked: AppController.clearReceivedFileHistory()+ }+ }+ Divider { width: parent.width }+ // 7. Clear everything+ ListRow {+ width: parent.width+ iconPath: Icons.trash+ title: qsTr("Clear all local history & caches")+ description: qsTr("Wipes clipboard history, timeline, notification history, cached messages, photo thumbnails, and transfer history on this PC. Paired devices and settings are kept.")+ Button {+ variant: "outline"+ size: "sm"+ iconPath: Icons.trash+ text: qsTr("Clear everything…")+ onClicked: clearEverythingSheet.open()+ }+ }+ }+ }+// ---- Devices ----Row {width: parent.width@@ -1183,4 +1511,39 @@ Item {}}}++ Sheet {+ id: clearEverythingSheet+ cardWidth: 420+ Column {+ width: parent.width+ spacing: 16+ Txt {+ width: parent.width+ text: qsTr("Clear all local history & caches?")+ role: "headline"+ wrapMode: Text.WordWrap+ }+ Txt {+ width: parent.width+ wrapMode: Text.WordWrap+ role: "body"+ muted: true+ text: qsTr("This wipes clipboard history, the cross-device timeline, notification history, cached messages, photo thumbnails, and transfer history on this PC. Your paired devices, settings, and saved files in Downloads are kept.")+ }+ Row {+ anchors.right: parent.right+ spacing: 10+ Button { variant: "text"; text: qsTr("Cancel"); onClicked: clearEverythingSheet.close() }+ Button {+ iconPath: Icons.trash+ text: qsTr("Clear everything")+ onClicked: {+ AppController.clearEverything()+ clearEverythingSheet.close()+ }+ }+ }+ }+ }}diff --git a/desktop/app/qml/pages/TimelinePage.qml b/desktop/app/qml/pages/TimelinePage.qmlindex f6dd93e..d15e7fb 100644--- a/desktop/app/qml/pages/TimelinePage.qml+++ b/desktop/app/qml/pages/TimelinePage.qml@@ -9,6 +9,9 @@ Item {id: pageproperty bool active: falseproperty bool confirmClearAll: false+ property bool deviceMenuOpen: false++ onActiveChanged: if (!active) deviceMenuOpen = falseopacity: active ? 1 : 0visible: opacity > 0@@ -28,6 +31,42 @@ Item {{ key: "session", label: qsTr("Sessions"), icon: Icons.mirror }]+ readonly property string selectedDeviceName: {+ if (TimelineModel.deviceFilter.length === 0 || DeviceList.count <= 0)+ return qsTr("All devices")+ const row = DeviceList.rowOf(TimelineModel.deviceFilter)+ return row >= 0 ? DeviceList.deviceNameAt(row) : qsTr("All devices")+ }++ Connections {+ target: TimelineModel+ function onHasAnyChanged() {+ if (!TimelineModel.hasAny) {+ page.confirmClearAll = false+ page.deviceMenuOpen = false+ if (searchInput.text.length > 0)+ searchInput.text = ""+ if (TimelineModel.searchQuery.length > 0)+ TimelineModel.searchQuery = ""+ if (TimelineModel.kindFilter.length > 0)+ TimelineModel.kindFilter = ""+ if (TimelineModel.deviceFilter.length > 0)+ TimelineModel.deviceFilter = ""+ }+ }+ }++ Connections {+ target: DeviceList+ function onCountChanged() {+ if (DeviceList.count <= 1) {+ page.deviceMenuOpen = false+ if (TimelineModel.deviceFilter.length > 0)+ TimelineModel.deviceFilter = ""+ }+ }+ }+function dayKey(unixSecs) {if (!unixSecs || unixSecs <= 0)return ""@@ -66,6 +105,7 @@ Item {Column {id: headerCol+ visible: TimelineModel.hasAnywidth: Math.min(860, parent.width - Theme.contentPadding * 2)x: Math.max(Theme.contentPadding, (parent.width - width) / 2)y: Theme.contentPadding@@ -151,53 +191,90 @@ Item {}}- // Filter chips (by kind, and by device when multiple devices are paired)- Flow {+ // Single-line filter bar: kind chips on the left, compact device dropdown on the right+ Item {+ id: filterBarwidth: parent.width- spacing: 8+ height: 30- Repeater {- model: page.kindOptions- delegate: Chip {- required property var modelData- text: modelData.label- iconPath: modelData.icon- selected: TimelineModel.kindFilter === modelData.key- TapHandler { onTapped: TimelineModel.kindFilter = modelData.key }- HoverHandler { cursorShape: Qt.PointingHandCursor }+ Row {+ id: kindRow+ anchors.left: parent.left+ anchors.verticalCenter: parent.verticalCenter+ spacing: 6++ Repeater {+ model: page.kindOptions+ delegate: Chip {+ required property var modelData+ text: modelData.label+ iconPath: modelData.icon+ selected: TimelineModel.kindFilter === modelData.key+ TapHandler {+ onTapped: {+ page.deviceMenuOpen = false+ TimelineModel.kindFilter = modelData.key+ }+ }+ HoverHandler { cursorShape: Qt.PointingHandCursor }+ }}}Rectangle {+ id: deviceDropdownBtnvisible: DeviceList.count > 1- width: 1- height: 22- y: 4- color: Theme.outlineVariant- }-- Chip {- visible: DeviceList.count > 1- text: qsTr("All devices")- selected: TimelineModel.deviceFilter === ""- TapHandler { onTapped: TimelineModel.deviceFilter = "" }- HoverHandler { cursorShape: Qt.PointingHandCursor }- }+ anchors.right: parent.right+ anchors.verticalCenter: parent.verticalCenter+ height: 30+ readonly property real maxAllowedWidth: Math.max(110, filterBar.width - kindRow.width - 12)+ width: Math.min(maxAllowedWidth, deviceBtnRow.implicitWidth + 22)+ radius: Theme.pill(height)+ readonly property bool filtered: TimelineModel.deviceFilter.length > 0+ color: Theme.graphite+ ? (filtered ? Theme.surfaceContent : "transparent")+ : (filtered ? Theme.secondaryContainer : Theme.surfaceContainerHigh)+ border.width: Theme.graphite || page.deviceMenuOpen ? 1 : 0+ border.color: page.deviceMenuOpen+ ? Theme.primary+ : (filtered ? Theme.surfaceContent : Theme.outlineVariant)+ Behavior on color { ColorAnimation { duration: Theme.fadeFast } }++ Accessible.role: Accessible.ComboBox+ Accessible.name: qsTr("Device filter: %1").arg(page.selectedDeviceName)+ Accessible.onPressAction: page.deviceMenuOpen = !page.deviceMenuOpen- Repeater {- model: DeviceList.count > 1 ? DeviceList : null- delegate: Chip {- required property string deviceId- required property string name- text: name- iconPath: Icons.phone- selected: TimelineModel.deviceFilter === deviceId- TapHandler {- onTapped: TimelineModel.deviceFilter =- (TimelineModel.deviceFilter === deviceId ? "" : deviceId)+ Row {+ id: deviceBtnRow+ anchors.centerIn: parent+ spacing: 6+ Icon {+ anchors.verticalCenter: parent.verticalCenter+ width: 14; height: 14+ path: Icons.phone+ color: deviceBtnLabel.color+ }+ Txt {+ id: deviceBtnLabel+ anchors.verticalCenter: parent.verticalCenter+ width: Math.min(implicitWidth, deviceDropdownBtn.maxAllowedWidth - 56)+ elide: Text.ElideRight+ role: "label"+ text: page.selectedDeviceName+ color: Theme.graphite+ ? (deviceDropdownBtn.filtered ? Theme.surface : Theme.surfaceContent)+ : (deviceDropdownBtn.filtered ? Theme.secondaryContainerContent : Theme.surfaceContent)+ }+ Icon {+ anchors.verticalCenter: parent.verticalCenter+ width: 12; height: 12+ path: Icons.chevronDown+ color: deviceBtnLabel.color}- HoverHandler { cursorShape: Qt.PointingHandCursor }}++ HoverHandler { cursorShape: Qt.PointingHandCursor }+ TapHandler { onTapped: page.deviceMenuOpen = !page.deviceMenuOpen }}}@@ -257,7 +334,7 @@ Item {// Empty stateColumn {anchors.centerIn: parent- anchors.verticalCenterOffset: 30+ anchors.verticalCenterOffset: TimelineModel.hasAny ? 30 : 0width: Math.min(420, parent.width - 48)spacing: 10visible: TimelineModel.count === 0@@ -279,9 +356,7 @@ Item {width: parent.widthhorizontalAlignment: Text.AlignHCenterrole: "title"- text: (TimelineModel.searchQuery.length > 0- || TimelineModel.kindFilter.length > 0- || TimelineModel.deviceFilter.length > 0)+ text: TimelineModel.hasAny? qsTr("No matching timeline items"): qsTr("Nothing in the timeline yet")}@@ -291,9 +366,7 @@ Item {role: "bodySmall"muted: truewrapMode: Text.WordWrap- text: (TimelineModel.searchQuery.length > 0- || TimelineModel.kindFilter.length > 0- || TimelineModel.deviceFilter.length > 0)+ text: TimelineModel.hasAny? qsTr("Try clearing the search or filter to see everything shared with your devices."): qsTr("Files, folders, clipboard items, links, saved photos, voice recordings, and screen mirroring or webcam sessions appear here automatically.")}@@ -501,4 +574,128 @@ Item {}}}++ // Device filter dropdown menu overlay+ Item {+ id: deviceMenuBackdrop+ anchors.fill: parent+ z: 49+ visible: page.deviceMenuOpen && TimelineModel.hasAny && DeviceList.count > 1+ TapHandler { onTapped: page.deviceMenuOpen = false }+ }++ Rectangle {+ id: deviceMenuCard+ z: 50+ visible: page.deviceMenuOpen && TimelineModel.hasAny && DeviceList.count > 1+ width: Math.max(deviceDropdownBtn.width, 200)+ height: deviceMenuCol.height + 10+ x: headerCol.x + headerCol.width - width+ y: headerCol.y + filterBar.y + filterBar.height + 6+ radius: Theme.radiusMd+ color: Theme.graphite ? Theme.surface : Theme.surfaceContainerHighest+ border.width: 1+ border.color: Theme.outlineVariant++ Column {+ id: deviceMenuCol+ x: 5+ y: 5+ width: parent.width - 10+ spacing: 2++ Rectangle {+ width: parent.width+ height: 32+ radius: Theme.radiusSm+ color: allDevHover.hovered ? Theme.surfaceContainerHigh : "transparent"++ Row {+ x: 10+ anchors.verticalCenter: parent.verticalCenter+ spacing: 8+ Icon {+ anchors.verticalCenter: parent.verticalCenter+ width: 14; height: 14+ path: Icons.phone+ color: TimelineModel.deviceFilter === "" ? Theme.primary : Theme.surfaceContentVariant+ }+ Txt {+ anchors.verticalCenter: parent.verticalCenter+ width: deviceMenuCol.width - 56+ elide: Text.ElideRight+ role: "bodySmall"+ weight: TimelineModel.deviceFilter === "" ? 600 : 400+ text: qsTr("All devices")+ }+ }+ Icon {+ visible: TimelineModel.deviceFilter === ""+ anchors.right: parent.right+ anchors.rightMargin: 10+ anchors.verticalCenter: parent.verticalCenter+ width: 14; height: 14+ path: Icons.check+ color: Theme.primary+ }+ HoverHandler { id: allDevHover; cursorShape: Qt.PointingHandCursor }+ TapHandler {+ onTapped: {+ TimelineModel.deviceFilter = ""+ page.deviceMenuOpen = false+ }+ }+ }++ Repeater {+ model: DeviceList.count > 1 ? DeviceList : null+ delegate: Rectangle {+ required property string deviceId+ required property string name+ required property string kind+ readonly property bool isCurrent: TimelineModel.deviceFilter === deviceId+ width: deviceMenuCol.width+ height: 32+ radius: Theme.radiusSm+ color: devOptHover.hovered ? Theme.surfaceContainerHigh : "transparent"++ Row {+ x: 10+ anchors.verticalCenter: parent.verticalCenter+ spacing: 8+ Icon {+ anchors.verticalCenter: parent.verticalCenter+ width: 14; height: 14+ path: kind === "desktop" || kind === "laptop" ? Icons.laptop : Icons.phone+ color: isCurrent ? Theme.primary : Theme.surfaceContentVariant+ }+ Txt {+ anchors.verticalCenter: parent.verticalCenter+ width: deviceMenuCol.width - 56+ elide: Text.ElideRight+ role: "bodySmall"+ weight: isCurrent ? 600 : 400+ text: name+ }+ }+ Icon {+ visible: isCurrent+ anchors.right: parent.right+ anchors.rightMargin: 10+ anchors.verticalCenter: parent.verticalCenter+ width: 14; height: 14+ path: Icons.check+ color: Theme.primary+ }+ HoverHandler { id: devOptHover; cursorShape: Qt.PointingHandCursor }+ TapHandler {+ onTapped: {+ TimelineModel.deviceFilter = deviceId+ page.deviceMenuOpen = false+ }+ }+ }+ }+ }+ }}diff --git a/desktop/app/src/bridge/app.rs b/desktop/app/src/bridge/app.rsindex 71d66b6..9feeed2 100644--- a/desktop/app/src/bridge/app.rs+++ b/desktop/app/src/bridge/app.rs@@ -105,8 +105,33 @@ pub mod qobject {/// Bumped whenever the Home card's summary (unread messages, missed calls,/// latest photo) changes for the watched phone.#[qproperty(i32, home_summary_revision)]+ /// Continuity Camera state (in-flight request and fallback paste prompt).+ #[qproperty(bool, continuity_camera_busy)]+ #[qproperty(QString, continuity_camera_status)]+ #[qproperty(QString, continuity_camera_mode)]+ #[qproperty(bool, continuity_paste_prompt_visible)]+ #[qproperty(QString, continuity_paste_prompt_title)]+ #[qproperty(QString, continuity_paste_prompt_body)]+ #[qproperty(QString, continuity_paste_target_name)]+ /// Command Palette state and latest open latency measurement.+ #[qproperty(bool, command_palette_open)]+ #[qproperty(QString, command_palette_results)]+ #[qproperty(f64, command_palette_open_ms)]+ /// Local storage and retention counts/sizes for Settings → Data & storage, as JSON.+ #[qproperty(QString, data_retention_summary)]+ /// LocalSend LAN sharing state and discovered peers (`[{ id, alias, deviceModel, deviceType, fingerprint, ip, port, protocol }]`).+ #[qproperty(bool, localsend_enabled)]+ #[qproperty(bool, localsend_receiving)]+ #[qproperty(QString, localsend_peers_json)]type AppController = super::AppControllerRust;+ /// Enables or disables LocalSend LAN discovery and file sharing.+ #[qinvokable]+ fn toggle_localsend(self: Pin<&mut AppController>, enabled: bool);+ /// Triggers an immediate LocalSend multicast/HTTP discovery announcement.+ #[qinvokable]+ fn refresh_localsend(self: Pin<&mut AppController>);+/// Asks a paired device to ring (or stop).#[qinvokable]fn ring(self: Pin<&mut AppController>, device: &QString, on: bool);@@ -165,6 +190,35 @@ pub mod qobject {#[qinvokable]fn run_clip_suggestion(self: &AppController);+ /// Starts a Continuity Camera capture (`"photo"` or `"scan"`) on the active phone.+ #[qinvokable]+ fn start_continuity_camera(self: &AppController, mode: &QString);+ /// Cancels an active Continuity Camera capture request.+ #[qinvokable]+ fn cancel_continuity_camera(self: &AppController);+ /// Pastes the captured Continuity Camera image into the current external window.+ #[qinvokable]+ fn confirm_continuity_paste(self: &AppController);+ /// Dismisses the missing-window paste confirmation prompt, keeping the image on the clipboard.+ #[qinvokable]+ fn dismiss_continuity_paste(self: &AppController);++ /// Opens the Command Palette and populates initial results from local state.+ #[qinvokable]+ fn open_command_palette(self: Pin<&mut AppController>);+ /// Closes the Command Palette.+ #[qinvokable]+ fn close_command_palette(self: Pin<&mut AppController>);+ /// Updates the Command Palette search results for `query`.+ #[qinvokable]+ fn search_command_palette(self: Pin<&mut AppController>, query: &QString);+ /// Executes a Command Palette item by ID and closes the palette.+ #[qinvokable]+ fn run_command_palette(self: Pin<&mut AppController>, id: &QString);+ /// Records when the Command Palette UI finished rendering its first frame.+ #[qinvokable]+ fn note_command_palette_shown(self: Pin<&mut AppController>);+/// Re-reads whether Windows shows this app's notifications (the user/// may have changed it in Settings).#[qinvokable]@@ -206,6 +260,25 @@ pub mod qobject {#[qinvokable]fn trim_working_set(self: &AppController);+ /// Recomputes `data_retention_summary` from local stores and disk caches.+ #[qinvokable]+ fn refresh_data_retention(self: Pin<&mut AppController>);+ /// Clears local notification history (in-memory, on-disk JSON, and cached notification images).+ #[qinvokable]+ fn clear_notification_history(self: Pin<&mut AppController>);+ /// Clears cached SMS/MMS threads, chat threads/messages, and downloaded attachment files.+ #[qinvokable]+ fn clear_message_cache(self: Pin<&mut AppController>);+ /// Clears cached photo thumbnails and viewer files on disk (without touching phone photos).+ #[qinvokable]+ fn clear_photo_thumbnails(self: Pin<&mut AppController>);+ /// Clears received file transfer history records (without deleting saved files in Downloads).+ #[qinvokable]+ fn clear_received_file_history(self: Pin<&mut AppController>);+ /// Clears all local histories and caches across all 6 categories while keeping paired devices and settings intact.+ #[qinvokable]+ fn clear_everything(self: Pin<&mut AppController>);+/// A short message for the user (e.g. a command failed).#[qsignal]fn toast(self: Pin<&mut AppController>, message: QString);@@ -263,6 +336,20 @@ pub struct AppControllerRust {clipboard_history: QString,timeline_preview: QString,home_summary_revision: i32,+ continuity_camera_busy: bool,+ continuity_camera_status: QString,+ continuity_camera_mode: QString,+ continuity_paste_prompt_visible: bool,+ continuity_paste_prompt_title: QString,+ continuity_paste_prompt_body: QString,+ continuity_paste_target_name: QString,+ command_palette_open: bool,+ command_palette_results: QString,+ command_palette_open_ms: f64,+ data_retention_summary: QString,+ localsend_enabled: bool,+ localsend_receiving: bool,+ localsend_peers_json: QString,tray: Option<tray::Tray>,}@@ -407,9 +494,13 @@ impl cxx_qt::Initialize for qobject::AppController {self.as_mut().set_can_update(crate::updater::can_update());self.as_mut().set_clipboard_history(QString::from(&crate::clipboard::history_json()));self.as_mut().set_timeline_preview(QString::from(&super::timeline::preview_json()));+ self.as_mut().set_command_palette_results(QString::from("[]"));+ self.as_mut().set_data_retention_summary(QString::from(&compute_data_retention_json()));+ self.as_mut().set_localsend_peers_json(QString::from("[]"));let initial_phone_colors = core_host::host().hub.read(|s| phone_colors_json(&s.devices));self.as_mut().set_phone_colors(QString::from(&initial_phone_colors));self.as_mut().refresh_appearance();+ self.as_mut().refresh_localsend_state();let qt = self.qt_thread();*CONTROLLER.get_or_init(Mutex::default).lock().unwrap_or_else(|e| e.into_inner()) = Some(qt.clone());super::subscribe(@@ -436,9 +527,29 @@ impl cxx_qt::Initialize for qobject::AppController {let preview = super::timeline::preview_json();object.set_timeline_preview(QString::from(&preview));});+ super::subscribe(qt.clone(), Changes::CONTINUITY, Self::refresh_continuity);+ super::subscribe(qt.clone(), Changes::PALETTE, Self::refresh_palette);+ super::subscribe(qt.clone(), Changes::LOCALSEND | Changes::STATUS, Self::refresh_localsend_state);+ super::subscribe(+ qt.clone(),+ Changes::DATA_RETENTION+ | Changes::CLIPBOARD+ | Changes::TIMELINE+ | Changes::HISTORY+ | Changes::TRANSFERS+ | Changes::MESSAGES+ | Changes::PHOTOS,+ |object| {+ let summary = compute_data_retention_json();+ object.set_data_retention_summary(QString::from(&summary));+ },+ );let labels = tray::MenuLabels {open: "Open Nectarlink".into(),+ command_palette: "Command palette".into(),+ take_photo: "Take photo with phone".into(),+ scan_document: "Scan document with phone".into(),find_phone: "Find my phone".into(),open_link: "Open copied link on phone".into(),quit: "Quit Nectarlink".into(),@@ -455,6 +566,16 @@ fn on_tray_event(qt: &CxxQtThread<qobject::AppController>, event: tray::TrayEventray::TrayEvent::Open => {let _ = qt.queue(|object| object.activate_requested());}+ tray::TrayEvent::CommandPalette => {+ let results = crate::command_palette::open();+ let _ = qt.queue(move |mut object| {+ object.as_mut().set_command_palette_results(QString::from(&results));+ object.as_mut().set_command_palette_open(true);+ object.activate_requested();+ });+ }+ tray::TrayEvent::TakePhoto => crate::continuity_camera::start("photo"),+ tray::TrayEvent::ScanDocument => crate::continuity_camera::start("scan"),tray::TrayEvent::OpenLinkOnPhone => crate::links::open_copied_link_on_phone(),tray::TrayEvent::Quit => {let _ = qt.queue(|object| object.quit_requested());@@ -659,6 +780,77 @@ impl qobject::AppController {}}+ fn refresh_continuity(mut self: Pin<&mut Self>) {+ let view = crate::continuity_camera::view();+ self.as_mut().set_continuity_camera_busy(view.busy);+ self.as_mut().set_continuity_camera_mode(QString::from(&view.mode));+ self.as_mut().set_continuity_camera_status(QString::from(&view.status));+ self.as_mut().set_continuity_paste_prompt_visible(view.paste_prompt_visible);+ self.as_mut().set_continuity_paste_prompt_title(QString::from(&view.paste_prompt_title));+ self.as_mut().set_continuity_paste_prompt_body(QString::from(&view.paste_prompt_body));+ self.as_mut().set_continuity_paste_target_name(QString::from(&view.paste_target_name));+ }++ fn refresh_palette(mut self: Pin<&mut Self>) {+ let was_open = self.command_palette_open;+ let is_open = crate::command_palette::is_open();+ self.as_mut().set_command_palette_open(is_open);+ self.as_mut().set_command_palette_open_ms(crate::command_palette::last_open_ms());+ if is_open && !was_open {+ let results = crate::command_palette::search("");+ self.as_mut().set_command_palette_results(QString::from(&results));+ }+ }++ fn refresh_localsend_state(mut self: Pin<&mut Self>) {+ let (enabled, receiving, peers) = match core_host::node() {+ Some(node) => (node.localsend_enabled(), node.localsend_receiving(), node.localsend_peers()),+ None => (false, false, Vec::new()),+ };+ core_host::host().hub.update(|s| {+ s.localsend_enabled = enabled;+ s.localsend_receiving = receiving;+ s.localsend_peers = peers.clone();+ Changes::NONE+ });+ let entries: Vec<serde_json::Value> = peers+ .into_iter()+ .map(|p| {+ serde_json::json!({+ "id": p.id.to_string(),+ "alias": p.alias,+ "deviceModel": p.device_model.unwrap_or_default(),+ "deviceType": p.device_type,+ "fingerprint": p.fingerprint,+ "ip": p.ip,+ "port": p.port,+ "protocol": p.protocol,+ })+ })+ .collect();+ let json = serde_json::Value::Array(entries).to_string();+ self.as_mut().set_localsend_enabled(enabled);+ self.as_mut().set_localsend_receiving(receiving);+ self.as_mut().set_localsend_peers_json(QString::from(&json));+ }++ pub fn toggle_localsend(mut self: Pin<&mut Self>, enabled: bool) {+ if let Some(node) = core_host::node()+ && let Err(e) = node.set_localsend_enabled(enabled)+ {+ show_message(describe(&e));+ }+ self.as_mut().refresh_localsend_state();+ core_host::host().hub.changed(Changes::LOCALSEND);+ }++ pub fn refresh_localsend(mut self: Pin<&mut Self>) {+ if let Some(node) = core_host::node() {+ node.refresh_localsend();+ }+ self.as_mut().refresh_localsend_state();+ }+fn refresh_appearance(mut self: Pin<&mut Self>) {self.as_mut().set_system_dark(win::system_dark());self.as_mut().set_reduce_motion(win::reduce_motion());@@ -692,6 +884,86 @@ impl qobject::AppController {crate::clipboard::run_last_suggestion();}+ pub fn start_continuity_camera(&self, mode: &QString) {+ let idx = self.current_device.max(0) as usize;+ crate::continuity_camera::start_for_index(idx, &String::from(mode));+ }++ pub fn cancel_continuity_camera(&self) {+ crate::continuity_camera::cancel();+ }++ pub fn confirm_continuity_paste(&self) {+ crate::continuity_camera::confirm_paste();+ }++ pub fn dismiss_continuity_paste(&self) {+ crate::continuity_camera::dismiss_paste();+ }++ pub fn open_command_palette(mut self: Pin<&mut Self>) {+ let results = crate::command_palette::open();+ self.as_mut().set_command_palette_results(QString::from(&results));+ self.as_mut().set_command_palette_open(true);+ self.activate_requested();+ }++ pub fn close_command_palette(self: Pin<&mut Self>) {+ crate::command_palette::close();+ self.set_command_palette_open(false);+ }++ pub fn search_command_palette(self: Pin<&mut Self>, query: &QString) {+ let results = crate::command_palette::search(&String::from(query));+ self.set_command_palette_results(QString::from(&results));+ }++ pub fn note_command_palette_shown(self: Pin<&mut Self>) {+ let ms = crate::command_palette::mark_shown();+ self.set_command_palette_open_ms(ms);+ }++ pub fn run_command_palette(mut self: Pin<&mut Self>, id: &QString) {+ let action = crate::command_palette::execute(&String::from(id));+ self.as_mut().set_command_palette_open(false);+ let Some(action) = action else { return };+ use crate::command_palette::PaletteUiAction;+ match action {+ PaletteUiAction::NavigatePage { page, device_index } => {+ if let Some(idx) = device_index {+ self.as_mut().set_current_device(idx as i32);+ }+ self.as_mut().set_current_page(QString::from(&page));+ self.activate_requested();+ }+ PaletteUiAction::StartChat { device_id, number, name } => {+ if let Ok(dev) = device_id.parse::<DeviceId>() {+ if let Some(idx) =+ core_host::host().hub.read(|s| s.devices.iter().position(|d| d.id == dev))+ {+ self.as_mut().set_current_device(idx as i32);+ }+ crate::messages::open_device(dev);+ crate::messages::start_chat(dev, number, name);+ }+ self.as_mut().set_current_page(QString::from("messages"));+ self.activate_requested();+ }+ PaletteUiAction::OpenDialer { device_id, number } => {+ if let Ok(dev) = device_id.parse::<DeviceId>() {+ open_dialer(dev, &number);+ }+ }+ PaletteUiAction::OpenDoctor => {+ self.run_doctor();+ }+ PaletteUiAction::OpenPairing => {+ self.as_mut().set_current_page(QString::from("home"));+ self.activate_requested();+ }+ }+ }+pub fn refresh_toasts_enabled(self: Pin<&mut Self>) {self.set_toasts_enabled(win::toast::enabled());core_host::spawn(core_host::refresh_wake());@@ -895,6 +1167,91 @@ impl qobject::AppController {crate::calls::release_idle_resources();super::native::ffi::trim_memory_caches();}++ pub fn refresh_data_retention(self: Pin<&mut Self>) {+ let summary = compute_data_retention_json();+ self.set_data_retention_summary(QString::from(&summary));+ }++ pub fn clear_notification_history(self: Pin<&mut Self>) {+ core_host::host().hub.update(|s| s.clear_notification_history() | Changes::DATA_RETENTION);+ crate::notification_store::clear_disk();+ self.refresh_data_retention();+ }++ pub fn clear_message_cache(self: Pin<&mut Self>) {+ if let Some(node) = core_host::node() {+ let _ = node.clear_message_cache();+ }+ crate::messages::clear_cache();+ core_host::host().hub.changed(Changes::DATA_RETENTION);+ self.refresh_data_retention();+ }++ pub fn clear_photo_thumbnails(self: Pin<&mut Self>) {+ crate::photos::clear_cache();+ core_host::host().hub.changed(Changes::DATA_RETENTION);+ self.refresh_data_retention();+ }++ pub fn clear_received_file_history(self: Pin<&mut Self>) {+ if let Some(node) = core_host::node() {+ let _ = node.clear_received_file_history();+ }+ core_host::host()+ .hub+ .update(|s| s.clear_transfer_history() | Changes::TIMELINE | Changes::DATA_RETENTION);+ self.refresh_data_retention();+ }++ pub fn clear_everything(mut self: Pin<&mut Self>) {+ if let Some(node) = core_host::node() {+ let _ = node.clear_all_local_data();+ }+ crate::clipboard::clear_history();+ core_host::host().hub.update(|s| {+ s.clear_notification_history()+ | s.clear_transfer_history()+ | Changes::CLIPBOARD+ | Changes::TIMELINE+ | Changes::DATA_RETENTION+ });+ crate::notification_store::clear_disk();+ crate::messages::clear_cache();+ crate::photos::clear_cache();+ self.as_mut().refresh_data_retention();+ self.toast(QString::from("Cleared all local history and caches"));+ }+}++fn compute_data_retention_json() -> String {+ let counts = core_host::node().map(|n| n.data_retention_counts()).unwrap_or_default();+ let (notification_history, notification_history_enabled, transfer_records) =+ core_host::host().hub.read(|s| {+ let finished = s.transfers.iter().filter(|t| t.transfer.state.is_finished()).count();+ (s.history.len(), s.history_enabled, finished)+ });+ let notification_image_bytes = crate::notifications::cached_image_bytes();+ let (cached_sms_threads, cached_sms_messages, message_attachment_bytes) = crate::messages::cache_stats();+ let (photo_thumb_files, photo_thumb_bytes) = crate::photos::cache_stats();+ serde_json::json!({+ "clipboardItems": counts.clipboard_items,+ "clipboardBytes": counts.clipboard_bytes,+ "timelineItems": counts.timeline_items,+ "timelineRetentionDays": counts.timeline_retention_days,+ "notificationHistory": notification_history,+ "notificationHistoryEnabled": notification_history_enabled,+ "notificationImageBytes": notification_image_bytes,+ "chatThreads": counts.chat_threads,+ "chatMessages": counts.chat_messages,+ "cachedSmsThreads": cached_sms_threads,+ "cachedSmsMessages": cached_sms_messages,+ "messageAttachmentBytes": message_attachment_bytes,+ "photoThumbFiles": photo_thumb_files,+ "photoThumbBytes": photo_thumb_bytes,+ "receivedFileRecords": counts.received_file_records as usize + transfer_records,+ })+ .to_string()}impl Drop for AppControllerRust {@@ -978,6 +1335,7 @@ fn home_summary_json(device: DeviceId) -> String {}fn watch_home(device: Option<DeviceId>) {+ crate::command_palette::set_preferred_device(device);home_store(|s| s.watched = device);if let Some(id) = device {refresh_home_if_needed(id, false, false, false);diff --git a/desktop/app/src/bridge/messages.rs b/desktop/app/src/bridge/messages.rsindex 837bcb5..ad11ac0 100644--- a/desktop/app/src/bridge/messages.rs+++ b/desktop/app/src/bridge/messages.rs@@ -37,6 +37,30 @@ pub mod qobject {/// A number pre-filled in the new-message box ("" for none).#[qproperty(QString, compose_to, cxx_name = "composeTo")]#[qproperty(QString, compose_name, cxx_name = "composeName")]+ /// Active app filter ("" = All, "sms" = SMS only, or package name).+ #[qproperty(QString, app_filter, cxx_name = "appFilter")]+ /// Filter chips JSON array (`[{ key, label }, ...]`).+ #[qproperty(QString, available_apps, cxx_name = "availableApps")]+ /// True when at least one MessagingStyle chat thread exists for this phone.+ #[qproperty(bool, has_chat_threads, cxx_name = "hasChatThreads")]+ /// True when the open conversation is a MessagingStyle chat thread (`chat:...`).+ #[qproperty(bool, current_is_chat, cxx_name = "currentIsChat")]+ /// Display name of the open conversation's app ("SMS", "WhatsApp", etc.).+ #[qproperty(QString, current_app_name, cxx_name = "currentAppName")]+ /// True when the open conversation supports sending/replying right now.+ #[qproperty(bool, current_can_reply, cxx_name = "currentCanReply")]+ /// True when the open conversation is a group thread.+ #[qproperty(bool, current_is_group, cxx_name = "currentIsGroup")]+ /// True when an image is staged in the composer for MMS.+ #[qproperty(bool, has_attachment, cxx_name = "hasAttachment")]+ /// File URL (`file:///...`) for previewing the staged attachment.+ #[qproperty(QString, attachment_preview, cxx_name = "attachmentPreview")]+ /// File name of the staged attachment.+ #[qproperty(QString, attachment_name, cxx_name = "attachmentName")]+ /// Human-readable size of the staged attachment (e.g. "245 KB").+ #[qproperty(QString, attachment_size, cxx_name = "attachmentSize")]+ /// Non-empty when the staged attachment exceeds the 900 KB MMS limit or is invalid.+ #[qproperty(QString, attachment_error, cxx_name = "attachmentError")]type Messages = super::MessagesRust;}@@ -60,10 +84,10 @@ pub mod qobject {#[qinvokable]#[cxx_name = "loadOlder"]fn load_older(self: &Messages);- /// Sends a text in the open conversation.+ /// Sends a text or inline chat reply in the open conversation.#[qinvokable]fn send(self: &Messages, body: &QString);- /// Sends a text to a number, starting a conversation.+ /// Sends a text (and optional staged MMS attachment) to a number, starting a conversation.#[qinvokable]#[cxx_name = "sendTo"]fn send_to(self: &Messages, to: &QString, body: &QString);@@ -73,6 +97,26 @@ pub mod qobject {/// Puts text on this PC's clipboard (it isn't sent back to the phone).#[qinvokable]fn copy(self: &Messages, text: &QString);+ /// Filters the conversation list by app (`""` = All, `"sms"` = SMS, or package name).+ #[qinvokable]+ #[cxx_name = "filterByApp"]+ fn filter_by_app(self: &Messages, app: &QString);+ /// Deletes a persisted MessagingStyle chat conversation locally.+ #[qinvokable]+ #[cxx_name = "deleteChatThread"]+ fn delete_chat_thread(self: &Messages, thread: &QString);+ /// Stages an image file (from drag-and-drop or file picker) as an MMS attachment.+ #[qinvokable]+ #[cxx_name = "attachFile"]+ fn attach_file(self: &Messages, path_or_url: &QString) -> bool;+ /// Stages an image from the PC clipboard (`Ctrl+V`) as an MMS attachment.+ #[qinvokable]+ #[cxx_name = "pasteClipboardImage"]+ fn paste_clipboard_image(self: &Messages) -> bool;+ /// Clears any staged MMS image attachment.+ #[qinvokable]+ #[cxx_name = "clearAttachment"]+ fn clear_attachment(self: &Messages);}}@@ -88,6 +132,18 @@ pub struct MessagesRust {sending: bool,compose_to: QString,compose_name: QString,+ app_filter: QString,+ available_apps: QString,+ has_chat_threads: bool,+ current_is_chat: bool,+ current_app_name: QString,+ current_can_reply: bool,+ current_is_group: bool,+ has_attachment: bool,+ attachment_preview: QString,+ attachment_name: QString,+ attachment_size: QString,+ attachment_error: QString,/// What was last shown, to skip refreshes that change nothing.last: Option<messages::View>,}@@ -97,6 +153,7 @@ impl cxx_qt::Initialize for qobject::Messages {self.as_mut().set_status(QString::from("idle"));self.as_mut().set_threads(QString::from("[]"));self.as_mut().set_messages(QString::from("[]"));+ self.as_mut().set_available_apps(QString::from("[]"));super::subscribe(self.qt_thread(), Changes::MESSAGES, Self::update_view);self.update_view();}@@ -124,6 +181,20 @@ impl qobject::Messages {self.as_mut().set_sending(view.sending);self.as_mut().set_compose_to(QString::from(view.compose_to.as_deref().unwrap_or_default()));self.as_mut().set_compose_name(QString::from(view.compose_name.as_deref().unwrap_or_default()));+ self.as_mut().set_app_filter(QString::from(&view.app_filter));+ if differs(|v| v.available_apps.to_string()) {+ self.as_mut().set_available_apps(QString::from(&view.available_apps.to_string()));+ }+ self.as_mut().set_has_chat_threads(view.has_chat_threads);+ self.as_mut().set_current_is_chat(view.current_is_chat);+ self.as_mut().set_current_app_name(QString::from(&view.current_app_name));+ self.as_mut().set_current_can_reply(view.current_can_reply);+ self.as_mut().set_current_is_group(view.current_is_group);+ self.as_mut().set_has_attachment(view.has_attachment);+ self.as_mut().set_attachment_preview(QString::from(&view.attachment_preview));+ self.as_mut().set_attachment_name(QString::from(&view.attachment_name));+ self.as_mut().set_attachment_size(QString::from(&view.attachment_size));+ self.as_mut().set_attachment_error(QString::from(&view.attachment_error));self.as_mut().rust_mut().last = Some(view);}@@ -152,10 +223,7 @@ impl qobject::Messages {}pub fn send(&self, body: &QString) {- let body = String::from(body);- if !body.trim().is_empty() {- messages::send(body);- }+ messages::send(String::from(body));}pub fn refresh(&self) {@@ -172,8 +240,28 @@ impl qobject::Messages {let (to, body) = (String::from(to), String::from(body));let to: Vec<String> =to.split([',', ';']).map(str::trim).filter(|t| !t.is_empty()).map(Into::into).collect();- if !to.is_empty() && !body.trim().is_empty() {+ if !to.is_empty() {messages::send_to(to, body);}}++ pub fn filter_by_app(&self, app: &QString) {+ messages::set_app_filter(String::from(app));+ }++ pub fn delete_chat_thread(&self, thread: &QString) {+ messages::delete_chat_thread(String::from(thread));+ }++ pub fn attach_file(&self, path_or_url: &QString) -> bool {+ messages::attach_file(String::from(path_or_url))+ }++ pub fn paste_clipboard_image(&self) -> bool {+ messages::paste_clipboard_image()+ }++ pub fn clear_attachment(&self) {+ messages::clear_attachment();+ }}diff --git a/desktop/app/src/bridge/notifications.rs b/desktop/app/src/bridge/notifications.rsindex be7ad86..3819a0d 100644--- a/desktop/app/src/bridge/notifications.rs+++ b/desktop/app/src/bridge/notifications.rs@@ -491,6 +491,7 @@ mod tests {chronometer: false,countdown: false,}),+ conversation: None,},},device_name: "Pixel".into(),diff --git a/desktop/app/src/bridge/prefs.rs b/desktop/app/src/bridge/prefs.rsindex 3a9bec6..9abc8be 100644--- a/desktop/app/src/bridge/prefs.rs+++ b/desktop/app/src/bridge/prefs.rs@@ -72,6 +72,12 @@ pub mod qobject {#[qproperty(QString, recordings_format)]/// Timeline retention in days (`0` means keep up to 5,000 entries regardless of age).#[qproperty(i32, timeline_retention_days)]+ /// Global hotkey for "Take photo with phone" (e.g. `"Ctrl+Alt+C"`).+ #[qproperty(QString, continuity_photo_hotkey)]+ /// Global hotkey for "Scan document with phone" (e.g. `"Ctrl+Alt+D"`).+ #[qproperty(QString, continuity_scan_hotkey)]+ /// Global hotkey for the Command Palette (e.g. `"Ctrl+Alt+Space"`).+ #[qproperty(QString, command_palette_hotkey)]type Preferences = super::PreferencesRust;/// Sets the folder where voice recordings are saved (from a `file:` URL or path).@@ -159,6 +165,9 @@ pub struct PreferencesRust {recordings_folder_is_default: bool,recordings_format: QString,timeline_retention_days: i32,+ continuity_photo_hotkey: QString,+ continuity_scan_hotkey: QString,+ command_palette_hotkey: QString,/// What the user chose (`start_with_windows` shows the default until then).start_choice: Option<bool>,/// Custom recordings folder if chosen (`None` means default).@@ -203,6 +212,14 @@ impl cxx_qt::Initialize for qobject::Preferences {self.as_mut().rust_mut().custom_recordings_folder = settings.recordings_folder.clone();self.as_mut().set_recordings_format(QString::from(settings.recordings_format.as_str()));self.as_mut().set_timeline_retention_days(settings.timeline_retention_days as i32);+ self.as_mut().set_continuity_photo_hotkey(QString::from(&settings.continuity_photo_hotkey));+ self.as_mut().set_continuity_scan_hotkey(QString::from(&settings.continuity_scan_hotkey));+ self.as_mut().set_command_palette_hotkey(QString::from(&settings.command_palette_hotkey));+ crate::win::tray::update_hotkeys(+ &settings.continuity_photo_hotkey,+ &settings.continuity_scan_hotkey,+ &settings.command_palette_hotkey,+ );crate::recordings::init(&settings);// Save after any change (connected after loading, so loading doesn't@@ -293,6 +310,36 @@ impl cxx_qt::Initialize for qobject::Preferences {p.save();}).release();+ self.as_mut()+ .on_continuity_photo_hotkey_changed(|p| {+ crate::win::tray::update_hotkeys(+ &String::from(&p.continuity_photo_hotkey),+ &String::from(&p.continuity_scan_hotkey),+ &String::from(&p.command_palette_hotkey),+ );+ p.save();+ })+ .release();+ self.as_mut()+ .on_continuity_scan_hotkey_changed(|p| {+ crate::win::tray::update_hotkeys(+ &String::from(&p.continuity_photo_hotkey),+ &String::from(&p.continuity_scan_hotkey),+ &String::from(&p.command_palette_hotkey),+ );+ p.save();+ })+ .release();+ self.as_mut()+ .on_command_palette_hotkey_changed(|p| {+ crate::win::tray::update_hotkeys(+ &String::from(&p.continuity_photo_hotkey),+ &String::from(&p.continuity_scan_hotkey),+ &String::from(&p.command_palette_hotkey),+ );+ p.save();+ })+ .release();}}@@ -352,6 +399,9 @@ impl qobject::Preferences {webcam_height: crate::webcam::height(),webcam_mirror: crate::webcam::mirror(),timeline_retention_days: p.timeline_retention_days.max(0) as u32,+ continuity_photo_hotkey: String::from(&p.continuity_photo_hotkey),+ continuity_scan_hotkey: String::from(&p.continuity_scan_hotkey),+ command_palette_hotkey: String::from(&p.command_palette_hotkey),};save_in_background(settings);}diff --git a/desktop/app/src/bridge/timeline.rs b/desktop/app/src/bridge/timeline.rsindex 935535b..3d32cae 100644--- a/desktop/app/src/bridge/timeline.rs+++ b/desktop/app/src/bridge/timeline.rs@@ -46,6 +46,7 @@ pub mod qobject {#[qml_element]#[qml_singleton]#[qproperty(i32, count)]+ #[qproperty(bool, has_any)]#[qproperty(bool, has_more)]#[qproperty(QString, search_query)]#[qproperty(QString, kind_filter)]@@ -151,6 +152,7 @@ struct Row {#[derive(Default)]pub struct TimelineModelRust {count: i32,+ has_any: bool,has_more: bool,search_query: QString,kind_filter: QString,@@ -261,6 +263,18 @@ impl qobject::TimelineModel {offset += fetched;}let new = build_rows(&all_entries, None);+ let has_any = if !all_entries.is_empty() {+ true+ } else {+ let q = self.current_query(0, 1);+ if q.kind.is_none() && q.device.is_none() && q.search.is_none() {+ false+ } else {+ node.timeline_page(&TimelineQuery { limit: 1, ..TimelineQuery::default() })+ .is_ok_and(|p| !p.entries.is_empty())+ }+ };+ self.as_mut().set_has_any(has_any);self.as_mut().set_has_more(has_more);let root = QModelIndex::default();diff --git a/desktop/app/src/bridge/transfers.rs b/desktop/app/src/bridge/transfers.rsindex 3d537a3..895e498 100644--- a/desktop/app/src/bridge/transfers.rs+++ b/desktop/app/src/bridge/transfers.rs@@ -103,6 +103,8 @@ pub mod qobject {#[qinvokable]fn send(self: &TransferList, device: &QString, urls: &QStringList);#[qinvokable]+ fn accept(self: &TransferList, id: &QString);+ #[qinvokable]fn cancel(self: &TransferList, id: &QString);/// Opens what a finished incoming transfer saved (its first file).#[qinvokable]@@ -171,6 +173,7 @@ fn role_value(row: &Row, role: &str) -> QVariant {"progress" => QVariant::from(&if t.total == 0 { 1.0 } else { t.done as f64 / t.total as f64 }),"rate" => QVariant::from(&row.view.rate),"status" => text(match t.state {+ TransferState::Requested => "requested",TransferState::Waiting => "waiting",TransferState::Running => "running",TransferState::Done { .. } => "done",@@ -199,18 +202,31 @@ fn saved_path(row: &Row) -> Option<PathBuf> {impl cxx_qt::Initialize for qobject::TransferList {fn initialize(self: Pin<&mut Self>) {- super::subscribe(self.qt_thread(), Changes::TRANSFERS | Changes::DEVICES, Self::refresh);+ super::subscribe(+ self.qt_thread(),+ Changes::TRANSFERS | Changes::DEVICES | Changes::LOCALSEND,+ Self::refresh,+ );}}impl qobject::TransferList {fn refresh(mut self: Pin<&mut Self>) {+ let localsend_peers = core_host::node().map(|n| n.localsend_peers()).unwrap_or_default();let new: Vec<Row> = core_host::host().hub.read(|s| {s.transfers.iter()- .map(|view| Row {- device_name: s.name_of(&view.transfer.device).unwrap_or_default(),- view: view.clone(),+ .map(|view| {+ let device_name = s+ .name_of(&view.transfer.device)+ .or_else(|| {+ localsend_peers+ .iter()+ .find(|p| p.id == view.transfer.device)+ .map(|p| p.alias.clone())+ })+ .unwrap_or_default();+ Row { device_name, view: view.clone() }}).collect()});@@ -280,6 +296,10 @@ impl qobject::TransferList {transfers::send(device, paths);}+ pub fn accept(&self, id: &QString) {+ transfers::accept(&String::from(id));+ }+pub fn cancel(&self, id: &QString) {transfers::cancel(&String::from(id));}diff --git a/desktop/app/src/calls.rs b/desktop/app/src/calls.rsindex 0f5a3a8..266337b 100644--- a/desktop/app/src/calls.rs+++ b/desktop/app/src/calls.rs@@ -791,6 +791,68 @@ pub fn dial(device: DeviceId, number: String) {});}+/// Returns cached `(name, number)` pairs for `device` synchronously (0 ms) for+/// the Command Palette, and kicks off background loading of contacts/call log+/// if not yet cached.+pub fn palette_contacts(device: DeviceId) -> Vec<(String, String)> {+ let (out, need_contacts, need_log) = page_state(|s| {+ if s.device != Some(device) {+ *s = PageState { device: Some(device), generation: s.generation + 1, ..PageState::default() };+ return (Vec::new(), true, true);+ }+ let mut seen = std::collections::HashSet::new();+ let mut list = Vec::new();+ for c in &s.contacts {+ let name = c.name.trim();+ if name.is_empty() {+ continue;+ }+ for num in &c.numbers {+ let raw = num.number.trim();+ if raw.is_empty() {+ continue;+ }+ let key = {+ let d = digits(raw);+ if d.is_empty() { raw.to_ascii_lowercase() } else { d }+ };+ if seen.insert(key) {+ list.push((name.to_owned(), raw.to_owned()));+ }+ }+ }+ for e in &s.log {+ let raw = e.number.trim();+ if raw.is_empty() {+ continue;+ }+ let key = {+ let d = digits(raw);+ if d.is_empty() { raw.to_ascii_lowercase() } else { d }+ };+ if seen.insert(key) {+ let name = e.name.as_deref().map(str::trim).filter(|n| !n.is_empty()).unwrap_or(raw);+ list.push((name.to_owned(), raw.to_owned()));+ }+ }+ let need_contacts = s.contacts.is_empty() && s.contacts_status != Status::Loading;+ let need_log = s.log.is_empty() && s.log_status != Status::Loading;+ (list, need_contacts, need_log)+ });+ if need_contacts {+ load_contacts();+ }+ if need_log {+ load_call_log(None);+ }+ out+}++/// Places a call from the Command Palette on `device`.+pub fn dial_from_palette(device: DeviceId, number: &str) {+ dial(device, number.to_owned());+}+#[cfg(test)]mod tests {use super::*;diff --git a/desktop/app/src/command_palette.rs b/desktop/app/src/command_palette.rsnew file mode 100644index 0000000..2240578--- /dev/null+++ b/desktop/app/src/command_palette.rs@@ -0,0 +1,1387 @@+// SPDX-License-Identifier: GPL-3.0-or-later+//! Global Command Palette (`PLAN.md` §3.9): instant fuzzy search over local+//! actions, contacts, conversations, mirrorable apps, quick settings toggles,+//! navigation pages, clipboard history, and recent files.+//!+//! Designed so that:+//! - Opening builds candidates synchronously from in-memory state and local+//! SQLite in a few milliseconds (< 100 ms end-to-end to visible popup).+//! - Closing drops the candidate vector (`shrink_to_fit`) so the palette costs+//! zero memory and zero CPU while closed.++use std::{collections::HashSet, path::PathBuf, sync::Mutex, time::Instant};++use nectarlink_core::{ClipboardItemKind, DeviceId, LinkState, PhoneToggleValue, TimelineKind};+use serde_json::{Value, json};++use crate::{+ bridge::app::{describe, show_message},+ core_host,+ settings::Settings,+ state::Changes,+};++/// Maximum number of search results serialized to QML per keystroke.+const MAX_RESULTS: usize = 24;++/// UI-level navigation or sheet action returned by [`execute`] when a command+/// needs `AppController` to switch pages or open a modal sheet in the main window.+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum PaletteUiAction {+ NavigatePage { page: String, device_index: Option<usize> },+ StartChat { device_id: String, number: String, name: String },+ OpenDialer { device_id: String, number: String },+ OpenDoctor,+ OpenPairing,+}++#[derive(Debug, Clone)]+pub(crate) struct PaletteCandidate {+ pub id: String,+ pub title: String,+ pub subtitle: String,+ pub category: String,+ pub icon: String,+ pub badge: String,+ pub shortcut: String,+ pub keywords: String,+ pub base_boost: i64,+}++impl PaletteCandidate {+ fn to_json(&self) -> Value {+ json!({+ "id": self.id,+ "title": self.title,+ "subtitle": self.subtitle,+ "category": self.category,+ "icon": self.icon,+ "badge": self.badge,+ "shortcut": self.shortcut,+ })+ }+}++#[derive(Debug, Default)]+struct PaletteState {+ open: bool,+ opened_at: Option<Instant>,+ last_open_ms: f64,+ candidates: Vec<PaletteCandidate>,+ preferred_device: Option<DeviceId>,+}++static STATE: Mutex<PaletteState> = Mutex::new(PaletteState {+ open: false,+ opened_at: None,+ last_open_ms: 0.0,+ candidates: Vec::new(),+ preferred_device: None,+});++fn with_state<T>(f: impl FnOnce(&mut PaletteState) -> T) -> T {+ f(&mut STATE.lock().unwrap_or_else(|e| e.into_inner()))+}++/// Updates the preferred primary phone for the Command Palette (synced with+/// `AppController.primaryDeviceId` when that device is online).+pub fn set_preferred_device(id: Option<DeviceId>) {+ with_state(|s| s.preferred_device = id);+}++/// Opens the Command Palette, snapshots local candidates into memory, notifies+/// QML via `Changes::PALETTE`, and returns the initial JSON results list.+pub fn open() -> String {+ let t0 = Instant::now();+ let candidates = build_candidates();+ let initial = filter_and_rank(&candidates, "");+ let json = serialize_results(&initial);+ let elapsed_ms = t0.elapsed().as_secs_f64() * 1000.0;+ with_state(|s| {+ s.open = true;+ s.opened_at = Some(t0);+ s.last_open_ms = elapsed_ms;+ s.candidates = candidates;+ });+ core_host::host().hub.changed(Changes::PALETTE);+ json+}++/// Closes the Command Palette and frees all cached candidates so it costs+/// nothing while closed.+pub fn close() {+ let was_open = with_state(|s| {+ let prev = s.open;+ s.open = false;+ s.opened_at = None;+ s.candidates.clear();+ s.candidates.shrink_to_fit();+ prev+ });+ if was_open {+ core_host::host().hub.changed(Changes::PALETTE);+ }+}++/// Whether the Command Palette popup is currently open.+pub fn is_open() -> bool {+ with_state(|s| s.open)+}++/// Called by QML as soon as the Command Palette window is visible on screen.+/// Records and returns the end-to-end latency in milliseconds from [`open`].+pub fn mark_shown() -> f64 {+ let ms = with_state(|s| {+ if let Some(t0) = s.opened_at {+ s.last_open_ms = t0.elapsed().as_secs_f64() * 1000.0;+ }+ s.last_open_ms+ });+ tracing::info!(open_ms = format!("{ms:.2}"), "command palette shown");+ ms+}++/// Returns the most recent open-to-visible latency in milliseconds.+pub fn last_open_ms() -> f64 {+ with_state(|s| s.last_open_ms)+}++/// Searches the Command Palette candidates for `query` and returns a JSON array+/// of matching items ordered by relevance.+pub fn search(query: &str) -> String {+ let results = with_state(|s| {+ if s.candidates.is_empty() {+ s.candidates = build_candidates();+ }+ filter_and_rank(&s.candidates, query)+ });+ serialize_results(&results)+}++fn serialize_results(items: &[PaletteCandidate]) -> String {+ Value::Array(items.iter().take(MAX_RESULTS).map(PaletteCandidate::to_json).collect()).to_string()+}++/// Executes a Command Palette item by `id`, closing the palette and returning+/// an optional [`PaletteUiAction`] if the main window needs to navigate or show+/// a sheet.+pub fn execute(id: &str) -> Option<PaletteUiAction> {+ let id = id.trim().to_owned();+ close();+ if id.is_empty() {+ return None;+ }++ if let Some(page) = id.strip_prefix("nav:") {+ return Some(PaletteUiAction::NavigatePage { page: page.to_owned(), device_index: None });+ }+ if let Some(idx_str) = id.strip_prefix("nav_device:")+ && let Ok(idx) = idx_str.parse::<usize>()+ {+ return Some(PaletteUiAction::NavigatePage { page: "home".to_owned(), device_index: Some(idx) });+ }++ if id == "action:continuity_photo" {+ crate::continuity_camera::start("photo");+ return None;+ }+ if id == "action:continuity_scan" {+ crate::continuity_camera::start("scan");+ return None;+ }+ if id == "action:open_link" {+ crate::links::open_copied_link_on_phone();+ return None;+ }+ if id == "action:sync_now" {+ if let Some(node) = core_host::node() {+ show_message("Syncing devices…");+ core_host::spawn(async move { node.refresh().await });+ }+ return None;+ }+ if id == "action:doctor" {+ return Some(PaletteUiAction::OpenDoctor);+ }+ if id == "action:pair" {+ return Some(PaletteUiAction::OpenPairing);+ }+ if id == "action:wake_pc" {+ core_host::spawn(core_host::refresh_wake());+ let msg = core_host::host().hub.read(|s| match s.wake.primary() {+ Some(adapter) if s.wake.state_str() == "enabled" => {+ format!("Wake-on-LAN is ready on {} (phones can wake this PC).", adapter.label)+ }+ Some(adapter) => {+ format!("Wake-on-LAN on {} is {}. Check Settings.", adapter.label, s.wake.state_str())+ }+ None => "No physical network adapter found for Wake-on-LAN.".to_owned(),+ });+ show_message(msg);+ return Some(PaletteUiAction::NavigatePage { page: "settings".to_owned(), device_index: None });+ }++ if let Some(dev_str) = id.strip_prefix("action:send_clipboard:")+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ crate::clipboard::send_now(dev);+ return None;+ }+ if let Some(dev_str) = id.strip_prefix("action:ring:")+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ ring_device_from_palette(dev);+ return None;+ }+ if let Some(dev_str) = id.strip_prefix("action:open_last_photo:")+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ crate::photos::open_latest_photo(dev);+ return None;+ }+ if let Some(dev_str) = id.strip_prefix("action:record_voice:")+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ open_voice_recordings(dev);+ return None;+ }+ if let Some(dev_str) = id.strip_prefix("action:mirror_screen:")+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ crate::mirror::start(dev);+ return None;+ }+ if let Some(dev_str) = id.strip_prefix("action:webcam:")+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ match crate::webcam::phase() {+ crate::webcam::Phase::Asking { .. } | crate::webcam::Phase::Streaming { .. } => {+ crate::webcam::stop();+ show_message("Stopped phone webcam.");+ }+ _ => {+ crate::webcam::start(Some(dev));+ show_message("Starting phone webcam…");+ }+ }+ return None;+ }+ if let Some(dev_str) = id.strip_prefix("action:phone_storage:")+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ if let Err(e) = crate::storage::open_in_explorer(dev) {+ show_message(e);+ }+ return None;+ }++ if let Some(rest) = id.strip_prefix("toggle:") {+ execute_phone_toggle(rest);+ return None;+ }++ if let Some(rest) = id.strip_prefix("text_thread:")+ && let Some((dev_str, thread_id)) = rest.split_once(':')+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ let idx = device_index_of(dev);+ crate::messages::open_thread_from_palette(dev, thread_id);+ return Some(PaletteUiAction::NavigatePage { page: "messages".to_owned(), device_index: idx });+ }++ if let Some(rest) = id.strip_prefix("text_contact:")+ && let Some((dev_str, payload)) = rest.split_once(':')+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ let (number, name) = payload.split_once('\t').unwrap_or((payload, payload));+ return Some(PaletteUiAction::StartChat {+ device_id: dev.to_string(),+ number: number.to_owned(),+ name: name.to_owned(),+ });+ }++ if let Some(rest) = id.strip_prefix("call_contact:")+ && let Some((dev_str, number)) = rest.split_once(':')+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ if crate::calls::can_dial(dev) {+ crate::calls::dial_from_palette(dev, number);+ let name = core_host::host().hub.read(|s| s.name_of(&dev)).unwrap_or_else(|| "phone".into());+ show_message(format!("Calling {number} on {name}…"));+ return None;+ }+ return Some(PaletteUiAction::OpenDialer { device_id: dev.to_string(), number: number.to_owned() });+ }++ if let Some(rest) = id.strip_prefix("mirror_app:")+ && let Some((dev_str, payload)) = rest.split_once(':')+ && let Ok(dev) = dev_str.parse::<DeviceId>()+ {+ let (pkg, label) = payload.split_once('\t').unwrap_or((payload, payload));+ crate::mirror::start_app(dev, pkg.to_owned(), label.to_owned());+ return None;+ }++ if let Some(clip_id) = id.strip_prefix("clip_copy:") {+ crate::clipboard::copy_history_item(clip_id);+ return None;+ }++ if let Some(path_str) = id.strip_prefix("timeline_open:") {+ let path = PathBuf::from(path_str);+ if path.exists() {+ crate::transfers::open(&path);+ } else {+ show_message("That file is no longer in its saved location.");+ }+ return None;+ }++ None+}++fn device_index_of(device: DeviceId) -> Option<usize> {+ core_host::host().hub.read(|s| s.devices.iter().position(|d| d.id == device))+}++fn ring_device_from_palette(device: DeviceId) {+ let Some(node) = core_host::node() else { return };+ let name = core_host::host().hub.read(|s| s.name_of(&device)).unwrap_or_else(|| "your phone".into());+ show_message(format!("Ringing {name}…"));+ core_host::spawn(async move {+ if let Err(e) = node.ring(device, true).await {+ show_message(describe(&e));+ }+ });+}++fn open_voice_recordings(device: DeviceId) {+ let data_dir = &core_host::host().data_dir;+ let settings = Settings::load(data_dir);+ let folder = crate::recordings::effective_folder(settings.recordings_folder.as_deref());+ let _ = std::fs::create_dir_all(&folder);+ crate::transfers::open(&folder);+ let phone_name =+ core_host::host().hub.read(|s| s.name_of(&device)).unwrap_or_else(|| "your phone".into());+ show_message(format!(+ "Opened Recordings folder — start a voice note in Nectarlink on {phone_name} to send it here."+ ));+}++fn execute_phone_toggle(rest: &str) {+ let parts: Vec<&str> = rest.split(':').collect();+ let Some(dev_str) = parts.first().copied() else { return };+ let Ok(device) = dev_str.parse::<DeviceId>() else { return };+ let Some(kind) = parts.get(1).copied() else { return };+ let Some(node) = core_host::node() else { return };++ let current = core_host::host().hub.read(|s| s.toggles.get(&device).cloned());+ let (toggle_id, val, label) = match kind {+ "dnd" => {+ let next = !current.as_ref().is_some_and(|t| t.dnd);+ (+ "dnd".to_owned(),+ PhoneToggleValue::Bool(next),+ if next { "Do Not Disturb turned on" } else { "Do Not Disturb turned off" },+ )+ }+ "flashlight" => {+ let next = !current.as_ref().and_then(|t| t.flashlight).unwrap_or(false);+ (+ "flashlight".to_owned(),+ PhoneToggleValue::Bool(next),+ if next { "Flashlight turned on" } else { "Flashlight turned off" },+ )+ }+ "wifi" => {+ let next = !current.as_ref().is_some_and(|t| t.wifi);+ (+ "wifi".to_owned(),+ PhoneToggleValue::Bool(next),+ if next { "Wi-Fi turned on" } else { "Wi-Fi turned off" },+ )+ }+ "bluetooth" => {+ let next = !current.as_ref().is_some_and(|t| t.bluetooth);+ (+ "bluetooth".to_owned(),+ PhoneToggleValue::Bool(next),+ if next { "Bluetooth turned on" } else { "Bluetooth turned off" },+ )+ }+ "ringer" => {+ let mode = parts.get(2).copied().unwrap_or("normal");+ let desc = match mode {+ "silent" => "Ringer set to silent",+ "vibrate" => "Ringer set to vibrate",+ _ => "Ringer set to sound",+ };+ ("ringer".to_owned(), PhoneToggleValue::Mode(mode.to_owned()), desc)+ }+ _ => return,+ };++ core_host::host().hub.update(|s| {+ let Some(mut next) = s.toggles.get(&device).cloned() else { return Changes::NONE };+ match (toggle_id.as_str(), &val) {+ ("dnd", PhoneToggleValue::Bool(on)) => next.dnd = *on,+ ("flashlight", PhoneToggleValue::Bool(on)) => next.flashlight = Some(*on),+ ("wifi", PhoneToggleValue::Bool(on)) => next.wifi = *on,+ ("bluetooth", PhoneToggleValue::Bool(on)) => next.bluetooth = *on,+ ("ringer", PhoneToggleValue::Mode(m)) => next.ringer.clone_from(m),+ _ => {}+ }+ s.set_toggles(device, Some(next))+ });+ show_message(label);+ core_host::spawn(async move {+ if let Err(e) = node.set_phone_toggle(device, toggle_id, val).await {+ let latest = node.phone_toggles(device);+ core_host::host().hub.update(|s| s.set_toggles(device, latest));+ show_message(describe(&e));+ }+ });+}++/// Builds the full set of searchable Command Palette candidates synchronously+/// from in-memory state and local SQLite.+fn build_candidates() -> Vec<PaletteCandidate> {+ let settings = Settings::load(&core_host::host().data_dir);+ let (devices, toggles_map, wake_subtitle) = core_host::host().hub.read(|s| {+ let devs: Vec<(usize, DeviceId, String, bool)> = s+ .devices+ .iter()+ .enumerate()+ .map(|(idx, d)| (idx, d.id, d.info.name.clone(), matches!(d.link, LinkState::Online { .. })))+ .collect();+ let wake_sub = match s.wake.primary() {+ Some(a) if s.wake.state_str() == "enabled" => format!("Ready on {}", a.label),+ Some(a) => format!("{} ({})", a.label, s.wake.state_str()),+ None => "Check Wake-on-LAN status in Settings".to_owned(),+ };+ (devs, s.toggles.clone(), wake_sub)+ });++ let preferred = with_state(|s| s.preferred_device);+ let primary = preferred+ .and_then(|pref| devices.iter().find(|(_, id, _, online)| *id == pref && *online))+ .or_else(|| devices.iter().find(|(_, _, _, online)| *online))+ .or_else(|| preferred.and_then(|pref| devices.iter().find(|(_, id, _, _)| *id == pref)))+ .or_else(|| devices.first())+ .cloned();+ let multi = devices.len() > 1;++ let mut out = Vec::with_capacity(128);++ // 1. Primary phone quick commands ("Send clipboard", "Find my phone", "Open last photo", "Record voice", etc.)+ if let Some((_, dev_id, ref dev_name, online)) = primary {+ let status_badge = if online { dev_name.clone() } else { format!("{dev_name} (offline)") };+ out.push(PaletteCandidate {+ id: format!("action:send_clipboard:{dev_id}"),+ title: "Send clipboard".into(),+ subtitle: format!("Send copied text or image to {dev_name}"),+ category: "Actions".into(),+ icon: "clipboard".into(),+ badge: status_badge.clone(),+ shortcut: String::new(),+ keywords: format!("send clipboard copy paste share text image {dev_name}"),+ base_boost: 95,+ });+ out.push(PaletteCandidate {+ id: format!("action:ring:{dev_id}"),+ title: "Find my phone".into(),+ subtitle: format!("Make {dev_name} ring at full volume"),+ category: "Actions".into(),+ icon: "ring".into(),+ badge: status_badge.clone(),+ shortcut: String::new(),+ keywords: format!("find my phone ring locate alarm lost {dev_name}"),+ base_boost: 92,+ });+ out.push(PaletteCandidate {+ id: format!("action:open_last_photo:{dev_id}"),+ title: "Open last photo".into(),+ subtitle: format!("Open the newest photo or screenshot from {dev_name}"),+ category: "Actions".into(),+ icon: "photo".into(),+ badge: status_badge.clone(),+ shortcut: String::new(),+ keywords: format!("open last photo latest picture image camera screenshot {dev_name}"),+ base_boost: 90,+ });+ out.push(PaletteCandidate {+ id: format!("action:record_voice:{dev_id}"),+ title: "Record voice".into(),+ subtitle: format!("Open voice recordings folder for {dev_name}"),+ category: "Actions".into(),+ icon: "mic".into(),+ badge: status_badge.clone(),+ shortcut: String::new(),+ keywords: format!("record voice audio memo note microphone recordings {dev_name}"),+ base_boost: 84,+ });+ }++ // Continuity Camera actions (global hotkeys shown as badges/shortcuts)+ out.push(PaletteCandidate {+ id: "action:continuity_photo".into(),+ title: "Take photo with phone".into(),+ subtitle: "Capture a photo on your phone and paste it into the active window".into(),+ category: "Actions".into(),+ icon: "camera".into(),+ badge: "Continuity".into(),+ shortcut: settings.continuity_photo_hotkey.clone(),+ keywords: "take photo with phone continuity camera capture picture paste".into(),+ base_boost: 88,+ });+ out.push(PaletteCandidate {+ id: "action:continuity_scan".into(),+ title: "Scan document with phone".into(),+ subtitle: "Scan a document on your phone and paste it into the active window".into(),+ category: "Actions".into(),+ icon: "clipboardList".into(),+ badge: "Continuity".into(),+ shortcut: settings.continuity_scan_hotkey.clone(),+ keywords: "scan document with phone continuity camera receipt paper pdf paste".into(),+ base_boost: 86,+ });+ out.push(PaletteCandidate {+ id: "action:open_link".into(),+ title: "Open copied link on phone".into(),+ subtitle: "Send the web URL on your clipboard to your phone's browser".into(),+ category: "Actions".into(),+ icon: "globe".into(),+ badge: String::new(),+ shortcut: String::new(),+ keywords: "open copied link url browser web share phone".into(),+ base_boost: 78,+ });++ if let Some((_, dev_id, ref dev_name, _)) = primary {+ out.push(PaletteCandidate {+ id: format!("action:mirror_screen:{dev_id}"),+ title: "Mirror phone screen".into(),+ subtitle: format!("Stream {dev_name}'s screen in a desktop window"),+ category: "Actions".into(),+ icon: "mirror".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("mirror phone screen cast display stream {dev_name}"),+ base_boost: 80,+ });+ let webcam_streaming = matches!(+ crate::webcam::phase(),+ crate::webcam::Phase::Asking { .. } | crate::webcam::Phase::Streaming { .. }+ );+ out.push(PaletteCandidate {+ id: format!("action:webcam:{dev_id}"),+ title: if webcam_streaming { "Stop phone webcam".into() } else { "Use phone as webcam".into() },+ subtitle: format!("Stream {dev_name}'s camera into the Nectarlink virtual webcam"),+ category: "Actions".into(),+ icon: "video".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("webcam camera video stream call zoom teams {dev_name}"),+ base_boost: 76,+ });+ out.push(PaletteCandidate {+ id: format!("action:phone_storage:{dev_id}"),+ title: "Browse phone storage in File Explorer".into(),+ subtitle: format!("Open {dev_name}'s Cloud Files folder in Explorer"),+ category: "Actions".into(),+ icon: "folder".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("browse phone storage files explorer folder drive {dev_name}"),+ base_boost: 74,+ });+ }++ // Additional per-phone actions when multiple phones are paired+ if multi {+ let primary_id = primary.as_ref().map(|(_, id, _, _)| *id);+ for (_, dev_id, dev_name, online) in &devices {+ if Some(*dev_id) == primary_id {+ continue;+ }+ let badge = if *online { dev_name.clone() } else { format!("{dev_name} (offline)") };+ out.push(PaletteCandidate {+ id: format!("action:send_clipboard:{dev_id}"),+ title: format!("Send clipboard to {dev_name}"),+ subtitle: "Send copied text or image".into(),+ category: "Actions".into(),+ icon: "clipboard".into(),+ badge: badge.clone(),+ shortcut: String::new(),+ keywords: format!("send clipboard copy paste {dev_name}"),+ base_boost: 68,+ });+ out.push(PaletteCandidate {+ id: format!("action:ring:{dev_id}"),+ title: format!("Find {dev_name}"),+ subtitle: "Make phone ring at full volume".into(),+ category: "Actions".into(),+ icon: "ring".into(),+ badge: badge.clone(),+ shortcut: String::new(),+ keywords: format!("find my phone ring locate {dev_name}"),+ base_boost: 66,+ });+ out.push(PaletteCandidate {+ id: format!("action:open_last_photo:{dev_id}"),+ title: format!("Open last photo from {dev_name}"),+ subtitle: "Open newest photo or screenshot".into(),+ category: "Actions".into(),+ icon: "photo".into(),+ badge,+ shortcut: String::new(),+ keywords: format!("open last photo latest picture {dev_name}"),+ base_boost: 65,+ });+ }+ }++ // Wake PC, Sync now, Connection Doctor, Pair new phone+ out.push(PaletteCandidate {+ id: "action:wake_pc".into(),+ title: "Wake PC".into(),+ subtitle: wake_subtitle,+ category: "Actions".into(),+ icon: "power".into(),+ badge: "Wake-on-LAN".into(),+ shortcut: String::new(),+ keywords: "wake pc wake on lan wol power magic packet sleep".into(),+ base_boost: 72,+ });+ out.push(PaletteCandidate {+ id: "action:sync_now".into(),+ title: "Sync now".into(),+ subtitle: "Reconnect paired phones and refresh notifications and media".into(),+ category: "Actions".into(),+ icon: "refresh".into(),+ badge: String::new(),+ shortcut: String::new(),+ keywords: "sync now reconnect refresh devices status".into(),+ base_boost: 70,+ });+ out.push(PaletteCandidate {+ id: "action:doctor".into(),+ title: "Connection Doctor".into(),+ subtitle: "Diagnose and fix firewall, discovery, or network issues".into(),+ category: "Actions".into(),+ icon: "bolt".into(),+ badge: String::new(),+ shortcut: String::new(),+ keywords: "connection doctor diagnose troubleshoot firewall network fix repair".into(),+ base_boost: 68,+ });+ out.push(PaletteCandidate {+ id: "action:pair".into(),+ title: "Pair a new phone".into(),+ subtitle: "Show QR code and pairing code to link another phone".into(),+ category: "Actions".into(),+ icon: "plus".into(),+ badge: String::new(),+ shortcut: String::new(),+ keywords: "pair new phone add device qr code link connect".into(),+ base_boost: 64,+ });++ // 2. Quick Toggles for the primary phone+ if let Some((_, dev_id, ref dev_name, _)) = primary {+ let cur = toggles_map.get(&dev_id);+ let dnd_badge = cur.map(|t| if t.dnd { "On" } else { "Off" }).unwrap_or("");+ let flash_badge =+ cur.and_then(|t| t.flashlight).map(|on| if on { "On" } else { "Off" }).unwrap_or("");+ let wifi_badge = cur.map(|t| if t.wifi { "On" } else { "Off" }).unwrap_or("");+ let bt_badge = cur.map(|t| if t.bluetooth { "On" } else { "Off" }).unwrap_or("");++ out.push(PaletteCandidate {+ id: format!("toggle:{dev_id}:dnd"),+ title: "Toggle Do Not Disturb".into(),+ subtitle: format!("Switch Do Not Disturb on {dev_name}"),+ category: "Quick Toggles".into(),+ icon: "moon".into(),+ badge: dnd_badge.into(),+ shortcut: String::new(),+ keywords: format!("toggle dnd do not disturb quiet silence {dev_name}"),+ base_boost: 62,+ });+ out.push(PaletteCandidate {+ id: format!("toggle:{dev_id}:flashlight"),+ title: "Toggle Flashlight".into(),+ subtitle: format!("Switch torch / flashlight on {dev_name}"),+ category: "Quick Toggles".into(),+ icon: "flashlight".into(),+ badge: flash_badge.into(),+ shortcut: String::new(),+ keywords: format!("toggle flashlight torch light lamp {dev_name}"),+ base_boost: 61,+ });+ out.push(PaletteCandidate {+ id: format!("toggle:{dev_id}:ringer:normal"),+ title: "Ringer: Sound".into(),+ subtitle: format!("Set {dev_name}'s ringer mode to normal sound"),+ category: "Quick Toggles".into(),+ icon: "speaker".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("ringer sound normal unmute ringtone {dev_name}"),+ base_boost: 56,+ });+ out.push(PaletteCandidate {+ id: format!("toggle:{dev_id}:ringer:vibrate"),+ title: "Ringer: Vibrate".into(),+ subtitle: format!("Set {dev_name}'s ringer mode to vibrate"),+ category: "Quick Toggles".into(),+ icon: "ring".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("ringer vibrate haptic {dev_name}"),+ base_boost: 55,+ });+ out.push(PaletteCandidate {+ id: format!("toggle:{dev_id}:ringer:silent"),+ title: "Ringer: Silent".into(),+ subtitle: format!("Mute {dev_name}'s ringer"),+ category: "Quick Toggles".into(),+ icon: "soundOff".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("ringer silent mute quiet {dev_name}"),+ base_boost: 55,+ });+ out.push(PaletteCandidate {+ id: format!("toggle:{dev_id}:wifi"),+ title: "Toggle Wi-Fi".into(),+ subtitle: format!("Switch Wi-Fi on {dev_name}"),+ category: "Quick Toggles".into(),+ icon: "wifi".into(),+ badge: wifi_badge.into(),+ shortcut: String::new(),+ keywords: format!("toggle wifi wireless network {dev_name}"),+ base_boost: 54,+ });+ out.push(PaletteCandidate {+ id: format!("toggle:{dev_id}:bluetooth"),+ title: "Toggle Bluetooth".into(),+ subtitle: format!("Switch Bluetooth on {dev_name}"),+ category: "Quick Toggles".into(),+ icon: "bluetooth".into(),+ badge: bt_badge.into(),+ shortcut: String::new(),+ keywords: format!("toggle bluetooth bt {dev_name}"),+ base_boost: 54,+ });+ }++ // 3. Contacts & Conversations ("Text <contact>", "Call <contact>") + "Mirror <app>"+ for (_, dev_id, dev_name, _) in &devices {+ let mut seen_text = HashSet::new();+ for (title, sub, thread_id) in crate::messages::palette_recipients(*dev_id) {+ let clean = title.trim();+ if clean.is_empty() {+ continue;+ }+ let key = clean.to_ascii_lowercase();+ if !seen_text.insert(key) {+ continue;+ }+ let sub_text = if sub.trim().is_empty() || sub.trim() == clean {+ format!("Open conversation on {dev_name}")+ } else {+ format!("{sub} · {dev_name}")+ };+ out.push(PaletteCandidate {+ id: format!("text_thread:{dev_id}:{thread_id}"),+ title: format!("Text {clean}"),+ subtitle: sub_text,+ category: "Messages".into(),+ icon: "messages".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("text message sms chat reply {clean} {sub} {dev_name}"),+ base_boost: 60,+ });+ }++ let mut seen_call = HashSet::new();+ for (name, number) in crate::calls::palette_contacts(*dev_id) {+ let clean_name = name.trim();+ let clean_num = number.trim();+ if clean_name.is_empty() || clean_num.is_empty() {+ continue;+ }+ let text_key = clean_name.to_ascii_lowercase();+ if seen_text.insert(text_key) {+ out.push(PaletteCandidate {+ id: format!("text_contact:{dev_id}:{clean_num}\t{clean_name}"),+ title: format!("Text {clean_name}"),+ subtitle: format!("{clean_num} · {dev_name}"),+ category: "Messages".into(),+ icon: "messages".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("text message sms chat {clean_name} {clean_num} {dev_name}"),+ base_boost: 52,+ });+ }+ let call_key = format!("{}|{}", clean_name.to_ascii_lowercase(), clean_num);+ if seen_call.insert(call_key) {+ let subtitle = if clean_name == clean_num {+ format!("Place call on {dev_name}")+ } else {+ format!("{clean_num} · {dev_name}")+ };+ out.push(PaletteCandidate {+ id: format!("call_contact:{dev_id}:{clean_num}"),+ title: format!("Call {clean_name}"),+ subtitle,+ category: "Calls".into(),+ icon: "call".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("call dial phone ring {clean_name} {clean_num} {dev_name}"),+ base_boost: 58,+ });+ }+ }++ for (idx, (pkg, label)) in crate::mirror::palette_apps(*dev_id).into_iter().enumerate() {+ let boost = if idx < 5 { 57 } else { 48 };+ out.push(PaletteCandidate {+ id: format!("mirror_app:{dev_id}:{pkg}\t{label}"),+ title: format!("Mirror {label}"),+ subtitle: format!("Open {label} in a window from {dev_name}"),+ category: "Mirror Apps".into(),+ icon: "mirror".into(),+ badge: dev_name.clone(),+ shortcut: String::new(),+ keywords: format!("mirror app open launch window {label} {pkg} {dev_name}"),+ base_boost: boost,+ });+ }+ }++ // 4. Navigation pages+ let nav_pages = [+ ("home", "Go to Home", "Device status, quick toggles, transfers, and notifications", "home"),+ ("messages", "Go to Messages", "SMS, MMS, RCS, and chat conversations", "messages"),+ ("calls", "Go to Calls", "Call history, contacts, and dialpad", "call"),+ ("photos", "Go to Photos", "Recent photos, screenshots, and videos from your phone", "photo"),+ ("apps", "Go to Apps & Mirroring", "Mirror your phone screen or open individual apps", "apps"),+ ("deck", "Go to Media & Deck", "Now Playing controls and audio output switcher", "deck"),+ ("timeline", "Go to Timeline", "Unified history of files, photos, clips, and calls", "history"),+ (+ "settings",+ "Go to Settings",+ "Appearance, hotkeys, webcam, recordings, and permissions",+ "settings",+ ),+ ];+ for (page, title, subtitle, icon) in nav_pages {+ out.push(PaletteCandidate {+ id: format!("nav:{page}"),+ title: title.into(),+ subtitle: subtitle.into(),+ category: "Navigation".into(),+ icon: icon.into(),+ badge: "Page".into(),+ shortcut: String::new(),+ keywords: format!("navigate go to open page {page} {title}"),+ base_boost: 50,+ });+ }+ if multi {+ for (idx, _, dev_name, online) in &devices {+ out.push(PaletteCandidate {+ id: format!("nav_device:{idx}"),+ title: format!("Switch to {dev_name}"),+ subtitle: if *online { "Connected".into() } else { "Offline".into() },+ category: "Navigation".into(),+ icon: "phone".into(),+ badge: "Device".into(),+ shortcut: String::new(),+ keywords: format!("switch device phone {dev_name}"),+ base_boost: 49,+ });+ }+ }++ // 5. Recent clipboard items & timeline files from local SQLite+ if let Some(node) = core_host::node() {+ for entry in node.clipboard_history(None).into_iter().take(10) {+ let preview: String = match entry.kind {+ ClipboardItemKind::Text => {+ let single_line: String = entry+ .text+ .lines()+ .map(str::trim)+ .filter(|l| !l.is_empty())+ .collect::<Vec<_>>()+ .join(" ");+ let mut chars = single_line.chars();+ let head: String = chars.by_ref().take(60).collect();+ if chars.next().is_some() { format!("{head}…") } else { head }+ }+ ClipboardItemKind::Image => "Copied image".into(),+ };+ if preview.is_empty() {+ continue;+ }+ out.push(PaletteCandidate {+ id: format!("clip_copy:{}", entry.id),+ title: format!("Copy: {preview}"),+ subtitle: format!("From {}", entry.device_name),+ category: "Clipboard".into(),+ icon: "copy".into(),+ badge: if entry.pinned { "Pinned".into() } else { String::new() },+ shortcut: String::new(),+ keywords: format!("clipboard history copy paste {preview} {}", entry.device_name),+ base_boost: if entry.pinned { 46 } else { 40 },+ });+ }++ let timeline_entries = node+ .timeline_page(&nectarlink_core::TimelineQuery {+ limit: 20,+ ..nectarlink_core::TimelineQuery::default()+ })+ .map(|p| p.entries)+ .unwrap_or_default();+ for item in timeline_entries.into_iter().take(12) {+ let target = item.target.lines().next().unwrap_or("").trim();+ if target.is_empty() {+ continue;+ }+ let icon = match item.kind {+ TimelineKind::Photo => "photo",+ TimelineKind::Recording => "mic",+ TimelineKind::File => "folder",+ _ => continue,+ };+ let p = PathBuf::from(target);+ if !p.exists() {+ continue;+ }+ out.push(PaletteCandidate {+ id: format!("timeline_open:{target}"),+ title: format!("Open {}", item.title),+ subtitle: if item.detail.trim().is_empty() {+ item.device_name.clone()+ } else {+ format!("{} · {}", item.detail, item.device_name)+ },+ category: "Recent Activity".into(),+ icon: icon.into(),+ badge: item.kind.as_str().into(),+ shortcut: String::new(),+ keywords: format!("open recent file timeline {} {}", item.title, item.device_name),+ base_boost: 42,+ });+ }+ }++ out+}++/// Filters and ranks `candidates` against `query` using fast fuzzy matching,+/// synthesizing direct `"Text <query>"` / `"Call <query>"` actions when the+/// user types an explicit prefix like `text ...` or `call ...`.+pub(crate) fn filter_and_rank(candidates: &[PaletteCandidate], query: &str) -> Vec<PaletteCandidate> {+ let trimmed = query.trim();+ if trimmed.is_empty() {+ let mut initial: Vec<PaletteCandidate> = candidates.to_vec();+ initial.sort_by(|a, b| b.base_boost.cmp(&a.base_boost).then_with(|| a.title.cmp(&b.title)));+ initial.truncate(MAX_RESULTS);+ return initial;+ }++ let mut scored: Vec<(i64, PaletteCandidate)> = Vec::new();+ for c in candidates {+ if let Some(score) = score_candidate(c, trimmed) {+ scored.push((score + c.base_boost, c.clone()));+ }+ }++ // Synthesize ad-hoc "Text <target>" or "Call <target>" if the user typed a direct prefix+ if let Some(synth) = synthesize_direct_command(candidates, trimmed) {+ let already_exact = scored.iter().any(|(_, c)| c.title.eq_ignore_ascii_case(&synth.title));+ if !already_exact {+ scored.push((1_800, synth));+ }+ }++ scored.sort_by(|(sa, ca), (sb, cb)| sb.cmp(sa).then_with(|| ca.title.cmp(&cb.title)));+ scored.into_iter().take(MAX_RESULTS).map(|(_, c)| c).collect()+}++fn synthesize_direct_command(candidates: &[PaletteCandidate], query: &str) -> Option<PaletteCandidate> {+ let lower = query.to_ascii_lowercase();+ let dev_id = candidates.iter().find_map(|c| {+ c.id.strip_prefix("action:send_clipboard:")+ .or_else(|| c.id.strip_prefix("action:ring:"))+ .map(str::to_owned)+ })?;++ for prefix in ["text ", "message ", "sms "] {+ if lower.starts_with(prefix) {+ let target = query[prefix.len()..].trim();+ if !target.is_empty() {+ return Some(PaletteCandidate {+ id: format!("text_contact:{dev_id}:{target}\t{target}"),+ title: format!("Text {target}"),+ subtitle: "Start a new message conversation".into(),+ category: "Messages".into(),+ icon: "messages".into(),+ badge: "New".into(),+ shortcut: String::new(),+ keywords: String::new(),+ base_boost: 100,+ });+ }+ }+ }++ for prefix in ["call ", "dial "] {+ if lower.starts_with(prefix) {+ let target = query[prefix.len()..].trim();+ if !target.is_empty() {+ return Some(PaletteCandidate {+ id: format!("call_contact:{dev_id}:{target}"),+ title: format!("Call {target}"),+ subtitle: "Place a phone call or open in dialer".into(),+ category: "Calls".into(),+ icon: "call".into(),+ badge: "Dial".into(),+ shortcut: String::new(),+ keywords: String::new(),+ base_boost: 100,+ });+ }+ }+ }++ None+}++fn score_candidate(c: &PaletteCandidate, query: &str) -> Option<i64> {+ let q_lower = query.to_ascii_lowercase();+ let title_lower = c.title.to_ascii_lowercase();+ let sub_lower = c.subtitle.to_ascii_lowercase();+ let kw_lower = c.keywords.to_ascii_lowercase();+ let cat_lower = c.category.to_ascii_lowercase();++ // Multi-token queries: every whitespace-separated token must match somewhere+ // in title, subtitle, keywords, or category, and each token's best score sums.+ let tokens: Vec<&str> = q_lower.split_whitespace().filter(|t| !t.is_empty()).collect();+ if tokens.is_empty() {+ return Some(0);+ }++ let mut total: i64 = 0;++ // Bonus for full-query match on title+ if title_lower == q_lower {+ total += 2_000;+ } else if title_lower.starts_with(&q_lower) {+ total += 1_200;+ } else if let Some(pos) = title_lower.find(&q_lower) {+ total += 850 - (pos as i64 * 4).min(200);+ } else if let Some(fscore) = fuzzy_subsequence_score(&title_lower, &q_lower) {+ total += fscore;+ }++ for tok in &tokens {+ let mut best_tok: Option<i64> = None;+ if let Some(s) = token_score(&title_lower, tok, 500) {+ best_tok = Some(best_tok.map_or(s, |b| b.max(s)));+ }+ if let Some(s) = token_score(&kw_lower, tok, 300) {+ best_tok = Some(best_tok.map_or(s, |b| b.max(s)));+ }+ if let Some(s) = token_score(&sub_lower, tok, 240) {+ best_tok = Some(best_tok.map_or(s, |b| b.max(s)));+ }+ if let Some(s) = token_score(&cat_lower, tok, 180) {+ best_tok = Some(best_tok.map_or(s, |b| b.max(s)));+ }+ total += best_tok?;+ }++ Some(total)+}++fn token_score(haystack: &str, needle: &str, weight: i64) -> Option<i64> {+ if haystack == needle {+ return Some(weight + 250);+ }+ if haystack.starts_with(needle) {+ return Some(weight + 180);+ }+ // Word-boundary prefix match inside haystack+ for (idx, word) in haystack.split(|c: char| !c.is_alphanumeric()).enumerate() {+ if word == needle {+ return Some(weight + 150 - (idx as i64 * 8).min(80));+ }+ if word.starts_with(needle) {+ return Some(weight + 110 - (idx as i64 * 8).min(80));+ }+ }+ if let Some(pos) = haystack.find(needle) {+ return Some(weight + 40 - (pos as i64 * 2).min(60));+ }+ fuzzy_subsequence_score(haystack, needle).map(|s| (s * weight) / 400)+}++/// Scores a fuzzy subsequence match of `needle` inside `haystack` (both lowercase),+/// rewarding consecutive character runs and word-boundary alignments.+pub(crate) fn fuzzy_subsequence_score(haystack: &str, needle: &str) -> Option<i64> {+ let n_chars: Vec<char> = needle.chars().filter(|c| !c.is_whitespace()).collect();+ if n_chars.is_empty() {+ return Some(0);+ }+ let h_chars: Vec<char> = haystack.chars().collect();+ if n_chars.len() > h_chars.len() {+ return None;+ }++ let mut score: i64 = 0;+ let mut n_idx = 0;+ let mut prev_match_idx: Option<usize> = None;+ let mut first_match_idx: Option<usize> = None;++ for (h_idx, &hc) in h_chars.iter().enumerate() {+ if hc == n_chars[n_idx] {+ if first_match_idx.is_none() {+ first_match_idx = Some(h_idx);+ }+ let at_boundary = h_idx == 0 || !h_chars[h_idx - 1].is_alphanumeric();+ if at_boundary {+ score += 35;+ }+ if let Some(prev) = prev_match_idx {+ if h_idx == prev + 1 {+ score += 45;+ } else {+ let gap = (h_idx - prev - 1) as i64;+ score -= (gap * 4).min(32);+ }+ } else {+ score += 25;+ }+ prev_match_idx = Some(h_idx);+ n_idx += 1;+ if n_idx == n_chars.len() {+ break;+ }+ }+ }++ if n_idx < n_chars.len() {+ return None;+ }++ let span = prev_match_idx.unwrap_or(0).saturating_sub(first_match_idx.unwrap_or(0)) + 1;+ let compactness_bonus = ((n_chars.len() * 20) / span.max(1)) as i64;+ let final_score = score + compactness_bonus;+ (final_score > 20).then_some(final_score)+}++#[cfg(test)]+mod tests {+ use super::*;++ fn sample_candidates() -> Vec<PaletteCandidate> {+ let dev = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20";+ vec![+ PaletteCandidate {+ id: format!("action:send_clipboard:{dev}"),+ title: "Send clipboard".into(),+ subtitle: "Send copied text or image to Pixel 9".into(),+ category: "Actions".into(),+ icon: "clipboard".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: "send clipboard copy paste share text image Pixel 9".into(),+ base_boost: 95,+ },+ PaletteCandidate {+ id: format!("action:ring:{dev}"),+ title: "Find my phone".into(),+ subtitle: "Make Pixel 9 ring at full volume".into(),+ category: "Actions".into(),+ icon: "ring".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: "find my phone ring locate alarm lost Pixel 9".into(),+ base_boost: 92,+ },+ PaletteCandidate {+ id: format!("action:open_last_photo:{dev}"),+ title: "Open last photo".into(),+ subtitle: "Open the newest photo or screenshot from Pixel 9".into(),+ category: "Actions".into(),+ icon: "photo".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: "open last photo latest picture screenshot Pixel 9".into(),+ base_boost: 90,+ },+ PaletteCandidate {+ id: "action:continuity_photo".into(),+ title: "Take photo with phone".into(),+ subtitle: "Capture a photo on your phone and paste it".into(),+ category: "Actions".into(),+ icon: "camera".into(),+ badge: "Continuity".into(),+ shortcut: "Ctrl+Alt+C".into(),+ keywords: "take photo with phone continuity camera capture".into(),+ base_boost: 88,+ },+ PaletteCandidate {+ id: "action:continuity_scan".into(),+ title: "Scan document with phone".into(),+ subtitle: "Scan a document on your phone and paste it".into(),+ category: "Actions".into(),+ icon: "clipboardList".into(),+ badge: "Continuity".into(),+ shortcut: "Ctrl+Alt+D".into(),+ keywords: "scan document with phone continuity camera receipt".into(),+ base_boost: 86,+ },+ PaletteCandidate {+ id: format!("action:record_voice:{dev}"),+ title: "Record voice".into(),+ subtitle: "Open voice recordings folder for Pixel 9".into(),+ category: "Actions".into(),+ icon: "mic".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: "record voice audio memo note recordings".into(),+ base_boost: 84,+ },+ PaletteCandidate {+ id: "action:wake_pc".into(),+ title: "Wake PC".into(),+ subtitle: "Ready on Ethernet".into(),+ category: "Actions".into(),+ icon: "power".into(),+ badge: "Wake-on-LAN".into(),+ shortcut: String::new(),+ keywords: "wake pc wake on lan wol power magic packet".into(),+ base_boost: 72,+ },+ PaletteCandidate {+ id: format!("text_contact:{dev}:+15550101\tAlice Rivera"),+ title: "Text Alice Rivera".into(),+ subtitle: "+15550101 · Pixel 9".into(),+ category: "Messages".into(),+ icon: "messages".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: "text message sms chat Alice Rivera +15550101".into(),+ base_boost: 60,+ },+ PaletteCandidate {+ id: format!("call_contact:{dev}:+15550101"),+ title: "Call Alice Rivera".into(),+ subtitle: "+15550101 · Pixel 9".into(),+ category: "Calls".into(),+ icon: "call".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: "call dial phone ring Alice Rivera +15550101".into(),+ base_boost: 58,+ },+ PaletteCandidate {+ id: format!("mirror_app:{dev}:com.whatsapp\tWhatsApp"),+ title: "Mirror WhatsApp".into(),+ subtitle: "Open WhatsApp in a window from Pixel 9".into(),+ category: "Mirror Apps".into(),+ icon: "mirror".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: "mirror app open launch window WhatsApp com.whatsapp".into(),+ base_boost: 57,+ },+ ]+ }++ #[test]+ fn fuzzy_search_ranks_all_planned_palette_commands() {+ let candidates = sample_candidates();++ let top = |q: &str| filter_and_rank(&candidates, q).first().unwrap().title.clone();+ assert_eq!(top("send clip"), "Send clipboard");+ assert_eq!(top("sndclp"), "Send clipboard");+ assert_eq!(top("find phone"), "Find my phone");+ assert_eq!(top("text alice"), "Text Alice Rivera");+ assert_eq!(top("call alice"), "Call Alice Rivera");+ assert_eq!(top("last photo"), "Open last photo");+ assert_eq!(top("mirror whatsapp"), "Mirror WhatsApp");+ assert_eq!(top("record voice"), "Record voice");+ assert_eq!(top("take photo"), "Take photo with phone");+ assert_eq!(top("scan doc"), "Scan document with phone");+ assert_eq!(top("wake pc"), "Wake PC");+ }++ #[test]+ fn synthesizes_direct_text_and_call_actions_for_unlisted_numbers() {+ let candidates = sample_candidates();++ let text_res = filter_and_rank(&candidates, "Text +1 555 0199");+ assert_eq!(text_res[0].title, "Text +1 555 0199");+ assert!(text_res[0].id.starts_with("text_contact:"));++ let call_res = filter_and_rank(&candidates, "Call +1 555 0199");+ assert_eq!(call_res[0].title, "Call +1 555 0199");+ assert!(call_res[0].id.starts_with("call_contact:"));+ }++ #[test]+ fn search_across_500_candidates_completes_well_under_20ms() {+ let mut candidates = sample_candidates();+ let dev = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20";+ for i in 0..500 {+ candidates.push(PaletteCandidate {+ id: format!("call_contact:{dev}:+1555{i:04}"),+ title: format!("Call Contact Number {i}"),+ subtitle: format!("+1555{i:04} · Pixel 9"),+ category: "Calls".into(),+ icon: "call".into(),+ badge: "Pixel 9".into(),+ shortcut: String::new(),+ keywords: format!("call contact {i} +1555{i:04}"),+ base_boost: 40,+ });+ }++ let start = Instant::now();+ let results = filter_and_rank(&candidates, "mirror whatsapp");+ let elapsed = start.elapsed();+ assert_eq!(results.first().map(|c| c.title.as_str()), Some("Mirror WhatsApp"));+ assert!(+ elapsed.as_millis() < 20,+ "expected < 20 ms fuzzy search for 500+ candidates, took {} ms",+ elapsed.as_millis()+ );+ }++ #[test]+ fn close_releases_candidate_memory_to_zero_capacity() {+ with_state(|s| {+ s.open = true;+ s.candidates = sample_candidates();+ assert!(s.candidates.capacity() > 0);+ });+ with_state(|s| {+ s.open = false;+ s.opened_at = None;+ s.candidates.clear();+ s.candidates.shrink_to_fit();+ assert_eq!(s.candidates.capacity(), 0);+ });+ }+}diff --git a/desktop/app/src/continuity_camera.rs b/desktop/app/src/continuity_camera.rsnew file mode 100644index 0000000..58227cc--- /dev/null+++ b/desktop/app/src/continuity_camera.rs@@ -0,0 +1,425 @@+// SPDX-License-Identifier: GPL-3.0-or-later+//! Continuity Camera coordinator for Windows: initiates "Take photo with phone"+//! and "Scan document with phone" from the tray menu, global hotkeys, or the+//! Nectarlink UI, remembers the external foreground window (`HWND`) that was+//! active when the request started, places the captured image onto the Windows+//! clipboard on arrival, and either restores focus + synthesizes `Ctrl+V` into+//! the original target window or prompts the user if that window was closed.++use std::{+ sync::{+ Mutex,+ atomic::{AtomicU64, Ordering},+ },+ time::{Duration, SystemTime, UNIX_EPOCH},+};++use nectarlink_core::{CameraCaptureMode, DeviceId, Error, LinkState, NodeEvent};++use crate::{core_host, state::Changes};++static REQUEST_SEQ: AtomicU64 = AtomicU64::new(1);++#[derive(Debug, Clone)]+struct ActiveRequest {+ request_id: String,+ device: DeviceId,+ device_name: String,+ mode: CameraCaptureMode,+ target_hwnd: Option<isize>,+ target_title: String,+}++#[derive(Debug, Clone)]+struct PendingPastePrompt {+ title: String,+ body: String,+ target_name: String,+}++#[derive(Debug, Default)]+struct ContinuityState {+ active: Option<ActiveRequest>,+ prompt: Option<PendingPastePrompt>,+}++static STATE: Mutex<ContinuityState> = Mutex::new(ContinuityState { active: None, prompt: None });++/// Snapshot of Continuity Camera state exposed to QML via `AppController`.+#[derive(Debug, Clone, Default, PartialEq, Eq)]+pub struct ContinuityView {+ pub busy: bool,+ pub mode: String,+ pub status: String,+ pub paste_prompt_visible: bool,+ pub paste_prompt_title: String,+ pub paste_prompt_body: String,+ pub paste_target_name: String,+}++/// Pure decision for what to do after placing a captured image on the clipboard.+#[derive(Debug, Clone, PartialEq, Eq)]+enum DeliveryDecision {+ /// The remembered target window is still alive and visible: focus it and paste.+ AutoPaste { hwnd: isize, toast: String },+ /// A target window was remembered when the request started, but is no longer+ /// alive/visible when the capture arrived. Do not blindly paste elsewhere;+ /// ask the user in Nectarlink.+ PromptMissingTarget { title: String, body: String, target_name: String },+ /// No external target window was remembered when the request started.+ ClipboardOnly { toast: String },+}++fn decide_delivery_action(+ mode: CameraCaptureMode,+ device_name: &str,+ target: Option<(isize, &str)>,+ target_still_alive: bool,+) -> DeliveryDecision {+ let (item_cap, item_lower) = match mode {+ CameraCaptureMode::Photo => ("Photo", "photo"),+ CameraCaptureMode::Scan => ("Scanned document", "scanned document"),+ };+ if let Some((hwnd, raw_title)) = target+ && hwnd != 0+ {+ let trimmed = raw_title.trim();+ let target_name =+ if trimmed.is_empty() { "your previous window".to_owned() } else { trimmed.to_owned() };+ if target_still_alive {+ return DeliveryDecision::AutoPaste {+ hwnd,+ toast: format!("{item_cap} from {device_name} pasted into {target_name}"),+ };+ }+ return DeliveryDecision::PromptMissingTarget {+ title: format!("{item_cap} ready on clipboard"),+ body: format!(+ "“{target_name}” is no longer open, so Nectarlink didn’t paste automatically. Switch to the window where you want your {item_lower} from {device_name} and click Paste into active window, or press Ctrl+V anywhere."+ ),+ target_name,+ };+ }+ DeliveryDecision::ClipboardOnly { toast: format!("{item_cap} from {device_name} copied to clipboard") }+}++fn next_request_id() -> String {+ let ms = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0);+ let seq = REQUEST_SEQ.fetch_add(1, Ordering::Relaxed);+ format!("cc-{ms}-{seq}")+}++fn notify_continuity() {+ core_host::host().hub.update(|_| Changes::CONTINUITY);+}++/// Returns a snapshot of the current Continuity Camera UI state.+pub fn view() -> ContinuityView {+ let guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ let (busy, mode, status) = if let Some(active) = &guard.active {+ let mode_str = active.mode.as_str().to_owned();+ let status_str = match active.mode {+ CameraCaptureMode::Photo => format!("Take a photo on {}…", active.device_name),+ CameraCaptureMode::Scan => format!("Scan a document on {}…", active.device_name),+ };+ (true, mode_str, status_str)+ } else {+ (false, String::new(), String::new())+ };+ let (paste_prompt_visible, paste_prompt_title, paste_prompt_body, paste_target_name) =+ if let Some(prompt) = &guard.prompt {+ (true, prompt.title.clone(), prompt.body.clone(), prompt.target_name.clone())+ } else {+ (false, String::new(), String::new(), String::new())+ };+ ContinuityView {+ busy,+ mode,+ status,+ paste_prompt_visible,+ paste_prompt_title,+ paste_prompt_body,+ paste_target_name,+ }+}++/// Starts a Continuity Camera capture (`"photo"` or `"scan"`) on the active+/// paired phone, remembering the current external foreground window.+pub fn start(mode_raw: &str) {+ start_for_index(0, mode_raw);+}++/// Starts a Continuity Camera capture preferring the paired device at+/// `preferred_index` if it is online, or the first online phone otherwise.+pub fn start_for_index(preferred_index: usize, mode_raw: &str) {+ let mode = CameraCaptureMode::parse(mode_raw).unwrap_or(CameraCaptureMode::Photo);+ let target = crate::win::tray::snapshot_target_window();++ let chosen = core_host::host().hub.read(|s| {+ s.devices+ .get(preferred_index)+ .filter(|d| matches!(d.link, LinkState::Online { .. }))+ .or_else(|| s.devices.iter().find(|d| matches!(d.link, LinkState::Online { .. })))+ .map(|d| (d.id, d.info.name.clone()))+ });+ let Some((device_id, device_name)) = chosen else {+ crate::bridge::app::show_message("Connect your phone to take a photo or scan a document.");+ return;+ };++ let request_id = next_request_id();+ let (target_hwnd, target_title) = match target {+ Some((hwnd, title)) => (Some(hwnd), title),+ None => (None, String::new()),+ };++ let previous = {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ guard.prompt = None;+ guard.active.replace(ActiveRequest {+ request_id: request_id.clone(),+ device: device_id,+ device_name: device_name.clone(),+ mode,+ target_hwnd,+ target_title,+ })+ };+ notify_continuity();++ let open_msg = match mode {+ CameraCaptureMode::Photo => format!("Opening camera on {device_name}…"),+ CameraCaptureMode::Scan => format!("Opening document scanner on {device_name}…"),+ };+ crate::bridge::app::show_message(open_msg);++ let Some(node) = core_host::node() else { return };+ core_host::spawn(async move {+ if let Some(prev) = previous {+ let _ = node.cancel_camera_capture(prev.device, prev.request_id, Some("replaced".into())).await;+ }+ if let Err(err) = node.request_camera_capture(device_id, request_id.clone(), mode).await {+ let should_report = {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ if guard.active.as_ref().is_some_and(|a| a.request_id == request_id) {+ guard.active = None;+ true+ } else {+ false+ }+ };+ if should_report {+ notify_continuity();+ let msg = match err {+ Error::Denied => format!("Allow Photos access for {device_name} in Settings first."),+ Error::Unsupported => format!("{device_name} doesn’t support Continuity Camera yet."),+ Error::Offline => format!("{device_name} is not connected."),+ other => format!("Couldn’t open camera on {device_name}: {other}"),+ };+ crate::bridge::app::show_message(msg);+ }+ }+ });+}++/// Cancels an in-flight Continuity Camera request from the PC side.+pub fn cancel() {+ let active = {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ guard.active.take()+ };+ let Some(active) = active else { return };+ notify_continuity();+ if let Some(node) = core_host::node() {+ core_host::spawn(async move {+ let _ =+ node.cancel_camera_capture(active.device, active.request_id, Some("cancelled".into())).await;+ });+ }+}++/// Confirms pasting the clipboard image when the original target window had closed.+pub fn confirm_paste() {+ {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ guard.prompt = None;+ }+ notify_continuity();++ if let Some((hwnd, title)) = crate::win::tray::snapshot_target_window() {+ spawn_focus_and_paste(hwnd);+ let label = if title.is_empty() { "active window".to_owned() } else { title };+ crate::bridge::app::show_message(format!("Pasted into {label}"));+ } else {+ crate::bridge::app::show_message("Image is on your clipboard — press Ctrl+V in your target app.");+ }+}++/// Dismisses the missing-window paste confirmation prompt, keeping the image on the clipboard.+pub fn dismiss_paste() {+ {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ guard.prompt = None;+ }+ notify_continuity();+}++fn spawn_focus_and_paste(hwnd: isize) {+ let _ = std::thread::Builder::new().name("continuity-paste".into()).spawn(move || {+ if crate::win::tray::focus_window(hwnd) {+ std::thread::sleep(Duration::from_millis(65));+ if crate::win::tray::is_window_alive_and_visible(hwnd) {+ crate::win::input::press_key("paste", &[]);+ crate::win::tray::send_paste_fallback_if_not_foreground(hwnd);+ }+ }+ });+}++/// Handles Continuity Camera events from `nectarlink_core`.+pub fn on_event(event: &NodeEvent) {+ match event {+ NodeEvent::CameraCaptureReceived { device, request_id, mode, mime, data, .. } => {+ let matched = {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ if guard.active.as_ref().is_some_and(|a| a.request_id == *request_id || a.device == *device) {+ guard.active.take()+ } else {+ None+ }+ };++ let device_name = matched.as_ref().map(|a| a.device_name.clone()).unwrap_or_else(|| {+ core_host::host().hub.read(|s| s.name_of(device).unwrap_or_else(|| device.short()))+ });++ if let Err(e) = crate::win::clipboard::write_image(mime, data) {+ tracing::warn!(error = %e, "can't copy continuity camera image to clipboard");+ notify_continuity();+ crate::bridge::app::show_message(format!("Couldn’t copy image to clipboard: {e}"));+ return;+ }++ let target_ref =+ matched.as_ref().and_then(|a| a.target_hwnd.map(|hwnd| (hwnd, a.target_title.as_str())));+ let alive =+ target_ref.is_some_and(|(hwnd, _)| crate::win::tray::is_window_alive_and_visible(hwnd));++ match decide_delivery_action(*mode, &device_name, target_ref, alive) {+ DeliveryDecision::AutoPaste { hwnd, toast } => {+ {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ guard.prompt = None;+ }+ notify_continuity();+ crate::bridge::app::show_message(toast);+ spawn_focus_and_paste(hwnd);+ }+ DeliveryDecision::PromptMissingTarget { title, body, target_name } => {+ {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ guard.prompt = Some(PendingPastePrompt { title, body, target_name });+ }+ notify_continuity();+ crate::bridge::app::request_activation();+ }+ DeliveryDecision::ClipboardOnly { toast } => {+ {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ guard.prompt = None;+ }+ notify_continuity();+ crate::bridge::app::show_message(toast);+ }+ }+ }+ NodeEvent::CameraCaptureCancelled { device, request_id, reason } => {+ let removed = {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ if guard.active.as_ref().is_some_and(|a| {+ a.request_id == *request_id || (request_id.is_empty() && a.device == *device)+ }) {+ guard.active.take()+ } else {+ None+ }+ };+ if let Some(active) = removed {+ notify_continuity();+ let msg = match reason.as_deref() {+ Some("busy") => format!("Camera is busy on {}.", active.device_name),+ Some("permission_denied") => {+ format!("Camera permission was denied on {}.", active.device_name)+ }+ _ => format!("Camera capture cancelled on {}.", active.device_name),+ };+ crate::bridge::app::show_message(msg);+ }+ }+ NodeEvent::LinkChanged { device, link } if !matches!(link, LinkState::Online { .. }) => {+ let cleared = {+ let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());+ if guard.active.as_ref().is_some_and(|a| a.device == *device) {+ guard.active = None;+ true+ } else {+ false+ }+ };+ if cleared {+ notify_continuity();+ }+ }+ _ => {}+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn delivery_decision_auto_pastes_when_target_window_is_alive() {+ let decision = decide_delivery_action(+ CameraCaptureMode::Photo,+ "Pixel 9",+ Some((0x1234, "Document.docx - Word")),+ true,+ );+ assert_eq!(+ decision,+ DeliveryDecision::AutoPaste {+ hwnd: 0x1234,+ toast: "Photo from Pixel 9 pasted into Document.docx - Word".into(),+ }+ );+ }++ #[test]+ fn delivery_decision_prompts_when_target_window_disappeared() {+ let decision = decide_delivery_action(+ CameraCaptureMode::Scan,+ "Pixel 9",+ Some((0x1234, "Notes - Notion")),+ false,+ );+ match decision {+ DeliveryDecision::PromptMissingTarget { title, body, target_name } => {+ assert_eq!(title, "Scanned document ready on clipboard");+ assert_eq!(target_name, "Notes - Notion");+ assert!(body.contains("Notes - Notion"));+ assert!(body.contains("no longer open"));+ }+ other => panic!("expected PromptMissingTarget, got {other:?}"),+ }+ }++ #[test]+ fn delivery_decision_copies_only_when_no_external_target_was_recorded() {+ let decision = decide_delivery_action(CameraCaptureMode::Photo, "Pixel 9", None, false);+ assert_eq!(+ decision,+ DeliveryDecision::ClipboardOnly { toast: "Photo from Pixel 9 copied to clipboard".into() }+ );+ }+}diff --git a/desktop/app/src/core_host.rs b/desktop/app/src/core_host.rsindex e3e99ca..68ee6c2 100644--- a/desktop/app/src/core_host.rs+++ b/desktop/app/src/core_host.rs@@ -101,6 +101,7 @@ async fn run(data_dir: PathBuf, platform: Arc<dyn Platform>) {"pc.power".into(),nectarlink_core::PC_WAKE.into(),nectarlink_core::PHOTOS_SHOW.into(),+ nectarlink_core::CAMERA_CONTINUITY.into(),nectarlink_core::CALLS_SHOW.into(),nectarlink_core::CONTACTS_SHOW.into(),nectarlink_core::SMS_SHOW.into(),@@ -176,6 +177,7 @@ async fn run(data_dir: PathBuf, platform: Arc<dyn Platform>) {crate::notifications::update_toasts(&event);crate::clipboard::on_event(&event);crate::photos::on_event(&event);+ crate::continuity_camera::on_event(&event);crate::calls::on_event(&event);crate::messages::on_event(&event);crate::mirror::on_event(&event);diff --git a/desktop/app/src/main.rs b/desktop/app/src/main.rsindex 2a5ac5d..004a22f 100644--- a/desktop/app/src/main.rs+++ b/desktop/app/src/main.rs@@ -12,6 +12,8 @@ mod battery;mod bridge;mod calls;mod clipboard;+mod command_palette;+mod continuity_camera;mod core_host;mod deck;mod doctor;@@ -313,6 +315,11 @@ fn main() -> ExitCode {battery::set_enabled(settings.battery_alerts);storage::start();webcam::init(&settings);+ win::tray::update_hotkeys(+ &settings.continuity_photo_hotkey,+ &settings.continuity_scan_hotkey,+ &settings.command_palette_hotkey,+ );// A test instance (own data folder) leaves the user's menu and sign-in// alone.if options.data_dir.is_none() {diff --git a/desktop/app/src/messages.rs b/desktop/app/src/messages.rsindex d19ac8c..70b05fd 100644--- a/desktop/app/src/messages.rs+++ b/desktop/app/src/messages.rs@@ -1,11 +1,20 @@// SPDX-License-Identifier: GPL-3.0-or-later-//! Text messages on the PC (docs/protocol/sms.md): a phone's conversations-//! and the open one's messages, read through the phone on demand and kept-//! only in memory. Texts sent from here go out through the phone.+//! Text and chat messages on the PC (docs/protocol/sms.md,+//! docs/protocol/notifications.md §2.3): a phone's SMS conversations and+//! mirrored MessagingStyle chat conversations, read on demand and unified in+//! one inbox. Texts, MMS image attachments, and inline chat replies sent from+//! here go out through the phone.-use std::{collections::HashMap, path::PathBuf, sync::Mutex};+use std::{+ collections::{HashMap, HashSet},+ path::{Path, PathBuf},+ sync::Mutex,+};-use nectarlink_core::{DeviceId, Error, LinkState, NodeEvent, SmsMessage, SmsThread};+use nectarlink_core::{+ ChatMessageRecord, ChatThreadRecord, DeviceId, Error, LinkState, NodeEvent, SmsAttachment, SmsMessage,+ SmsPart, SmsThread,+};use serde_json::{Value, json};use crate::{core_host, state::Changes};@@ -14,6 +23,9 @@ use crate::{core_host, state::Changes};const THREADS: u32 = 60;const PAGE: u32 = 100;+/// Maximum MMS image attachment size (`nectarlink_protocol::messages::sms::MAX_ATTACHMENT_BYTES`).+pub const MAX_ATTACHMENT_BYTES: usize = 900 * 1024;+/// What the Messages page can show.#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]pub enum Status {@@ -54,26 +66,47 @@ impl Status {}}+/// An image staged in the composer to be sent as an MMS attachment.+#[derive(Debug, Clone, PartialEq, Eq, Default)]+struct StagedAttachment {+ name: String,+ mime: String,+ data: Vec<u8>,+ preview_path: Option<PathBuf>,+ preview_url: String,+ size_text: String,+ error: String,+}+#[derive(Debug, Default)]struct State {device: Option<DeviceId>,status: Status,threads: Vec<SmsThread>,- /// The open conversation.+ chat_threads: Vec<ChatThreadRecord>,+ /// The open conversation (`chat:...` for MessagingStyle chats, or SMS thread ID).thread: Option<String>,- /// Its messages, newest first.+ /// Its SMS messages, newest first.messages: Vec<SmsMessage>,- /// Older messages may exist.+ /// Its MessagingStyle chat messages, when `thread` starts with `"chat:"`.+ chat_messages: Vec<ChatMessageRecord>,+ /// Older messages may exist (SMS threads only).more: bool,loading_older: bool,sending: bool,/// Pictures fetched, by part ID.pictures: HashMap<String, PathBuf>,- /// Contact photos saved, by thread ID.+ /// Contact/conversation photos saved, by thread ID.photos: HashMap<String, PathBuf>,+ /// App icons saved, by Android package name.+ app_icons: HashMap<String, PathBuf>,/// A number pre-filled in the new-message box (from Calls / Contacts).compose_to: Option<String>,compose_name: Option<String>,+ /// Active app filter (`""` = All, `"sms"` = SMS only, or package name).+ app_filter: String,+ /// Staged MMS image attachment for the current SMS composer.+ staged_attachment: Option<StagedAttachment>,/// Bumped when the phone changes, so late answers are dropped.generation: u64,}@@ -93,7 +126,13 @@ fn changed() {pub fn release_idle_resources() {let mut released = false;state(|s| {- if !s.sending && (s.device.is_some() || !s.threads.is_empty() || !s.messages.is_empty()) {+ if !s.sending+ && (s.device.is_some()+ || !s.threads.is_empty()+ || !s.chat_threads.is_empty()+ || !s.messages.is_empty()+ || !s.chat_messages.is_empty())+ {let generation = s.generation.wrapping_add(1);*s = State { generation, ..State::default() };released = true;@@ -108,6 +147,58 @@ fn files_dir() -> PathBuf {crate::notifications::images_dir()}+fn is_message_cache_file(name: &str) -> bool {+ name.starts_with("sms-") || name.starts_with("mms-") || name.starts_with("chat-")+}++/// Clears cached SMS/MMS threads, messages, and downloaded attachment/avatar files on disk.+pub fn clear_cache() {+ if let Ok(entries) = std::fs::read_dir(files_dir()) {+ for entry in entries.flatten() {+ let path = entry.path();+ if path.file_name().and_then(|n| n.to_str()).is_some_and(is_message_cache_file) {+ let _ = std::fs::remove_file(path);+ }+ }+ }+ state(|s| {+ s.generation = s.generation.wrapping_add(1);+ s.threads.clear();+ s.chat_threads.clear();+ s.messages.clear();+ s.chat_messages.clear();+ s.pictures.clear();+ s.photos.clear();+ s.app_icons.clear();+ s.staged_attachment = None;+ s.more = false;+ s.loading_older = false;+ });+ changed();+}++/// Returns `(cached_sms_threads, cached_sms_messages, attachment_bytes)` for the+/// Data & storage summary in Settings.+pub fn cache_stats() -> (usize, usize, u64) {+ let (threads, messages) = state(|s| (s.threads.len(), s.messages.len()));+ let bytes: u64 = std::fs::read_dir(files_dir())+ .ok()+ .into_iter()+ .flatten()+ .flatten()+ .filter_map(|e| {+ let name = e.file_name();+ let name_str = name.to_str()?;+ if !is_message_cache_file(name_str) {+ return None;+ }+ let meta = e.metadata().ok()?;+ meta.is_file().then_some(meta.len())+ })+ .sum();+ (threads, messages, bytes)+}+// ---- What the page shows ----/// Everything the page shows, for QML.@@ -123,15 +214,72 @@ pub struct View {pub sending: bool,pub compose_to: Option<String>,pub compose_name: Option<String>,+ pub app_filter: String,+ pub available_apps: Value,+ pub has_chat_threads: bool,+ pub current_is_chat: bool,+ pub current_app_name: String,+ pub current_can_reply: bool,+ pub current_is_group: bool,+ pub has_attachment: bool,+ pub attachment_preview: String,+ pub attachment_name: String,+ pub attachment_size: String,+ pub attachment_error: String,}pub fn view() -> View {- state(|s| View {- device: s.device,- status: s.status,- threads: Value::Array(s.threads.iter().map(|t| thread_json(t, s.photos.get(&t.id))).collect()),- thread: s.thread.clone(),- messages: {+ state(|s| {+ let mut combined: Vec<(i64, String, Value)> = Vec::new();+ if s.app_filter.is_empty() || s.app_filter == "sms" {+ for t in &s.threads {+ combined.push((t.date, t.id.clone(), thread_json(t, s.photos.get(&t.id))));+ }+ }+ if s.app_filter != "sms" {+ for ct in &s.chat_threads {+ if s.app_filter.is_empty() || ct.app == s.app_filter {+ combined.push((+ ct.updated_ms,+ ct.thread_id.clone(),+ chat_thread_json(ct, s.photos.get(&ct.thread_id), s.app_icons.get(&ct.app)),+ ));+ }+ }+ }+ combined.sort_by(|a, b| b.0.cmp(&a.0).then_with(|| a.1.cmp(&b.1)));+ let threads = Value::Array(combined.into_iter().map(|(_, _, v)| v).collect());++ let mut apps = vec![json!({ "key": "", "label": "All" }), json!({ "key": "sms", "label": "SMS" })];+ let mut seen = HashSet::new();+ for ct in &s.chat_threads {+ if !ct.app.is_empty() && seen.insert(ct.app.clone()) {+ let label = if ct.app_name.trim().is_empty() { ct.app.clone() } else { ct.app_name.clone() };+ apps.push(json!({ "key": ct.app, "label": label }));+ }+ }+ let available_apps = Value::Array(apps);++ let open_id = s.thread.as_deref();+ let current_is_chat = open_id.is_some_and(|id| id.starts_with("chat:"));+ let (current_app_name, current_can_reply, current_is_group) = if current_is_chat {+ let ct = open_id.and_then(|id| s.chat_threads.iter().find(|c| c.thread_id == id));+ (+ ct.map(|c| if c.app_name.trim().is_empty() { "Chat".into() } else { c.app_name.clone() })+ .unwrap_or_else(|| "Chat".into()),+ ct.is_some_and(|c| c.active && c.reply_action_id.is_some()),+ ct.is_some_and(|c| c.is_group),+ )+ } else {+ let sms_thread = open_id.and_then(|id| s.threads.iter().find(|t| t.id == id));+ ("SMS".into(), true, sms_thread.is_some_and(|t| t.addresses.len() > 1))+ };++ let messages = if current_is_chat {+ let mut ordered = s.chat_messages.clone();+ ordered.sort_by(|a, b| a.time_ms.cmp(&b.time_ms).then_with(|| a.id.cmp(&b.id)));+ Value::Array(ordered.iter().map(chat_message_json).collect())+ } else {// In a group, who sent each message (by contact name when known).let thread = s.thread.as_ref().and_then(|id| s.threads.iter().find(|t| &t.id == id));let group = thread.filter(|t| t.addresses.len() > 1);@@ -142,12 +290,44 @@ pub fn view() -> View {.map(|m| message_json(m, &s.pictures, group.map(|t| sender_of(t, &m.address)))).collect(),)- },- more: s.more,- loading_older: s.loading_older,- sending: s.sending,- compose_to: s.compose_to.clone(),- compose_name: s.compose_name.clone(),+ };++ let (has_attachment, attachment_preview, attachment_name, attachment_size, attachment_error) =+ match &s.staged_attachment {+ Some(att) => (+ true,+ att.preview_url.clone(),+ att.name.clone(),+ att.size_text.clone(),+ att.error.clone(),+ ),+ None => (false, String::new(), String::new(), String::new(), String::new()),+ };++ View {+ device: s.device,+ status: s.status,+ threads,+ thread: s.thread.clone(),+ messages,+ more: if current_is_chat { false } else { s.more },+ loading_older: if current_is_chat { false } else { s.loading_older },+ sending: s.sending,+ compose_to: s.compose_to.clone(),+ compose_name: s.compose_name.clone(),+ app_filter: s.app_filter.clone(),+ available_apps,+ has_chat_threads: !s.chat_threads.is_empty(),+ current_is_chat,+ current_app_name,+ current_can_reply,+ current_is_group,+ has_attachment,+ attachment_preview,+ attachment_name,+ attachment_size,+ attachment_error,+ }})}@@ -172,6 +352,33 @@ fn thread_json(t: &SmsThread, photo: Option<&PathBuf>) -> Value {"unread": t.unread,"group": t.addresses.len() > 1,"photo": photo.map(|p| crate::icons::file_url(p)).unwrap_or_default(),+ "isChat": false,+ "app": "",+ "appName": "SMS",+ "appIcon": "",+ "canReply": true,+ "lastSender": "",+ "active": true,+ })+}++fn chat_thread_json(t: &ChatThreadRecord, photo: Option<&PathBuf>, app_icon: Option<&PathBuf>) -> Value {+ json!({+ "id": t.thread_id,+ "title": t.title,+ "addresses": "",+ "snippet": t.snippet,+ "date": t.updated_ms,+ "unread": t.unread,+ "group": t.is_group,+ "photo": photo.map(|p| crate::icons::file_url(p)).unwrap_or_default(),+ "isChat": true,+ "app": t.app,+ "appName": if t.app_name.trim().is_empty() { &t.app } else { &t.app_name },+ "appIcon": app_icon.map(|p| crate::icons::file_url(p)).unwrap_or_default(),+ "canReply": t.active && t.reply_action_id.is_some(),+ "lastSender": t.last_sender.clone().unwrap_or_default(),+ "active": t.active,})}@@ -219,8 +426,59 @@ fn message_json(m: &SmsMessage, pictures: &HashMap<String, PathBuf>, sender: Opt})}+fn chat_message_json(m: &ChatMessageRecord) -> Value {+ let sender = if m.self_sent {+ None+ } else {+ m.sender.as_ref().map(|s| s.trim().to_owned()).filter(|s| !s.is_empty())+ };+ json!({+ "id": m.id.to_string(),+ "body": m.text,+ "date": m.time_ms,+ "outgoing": m.self_sent,+ "status": if m.id < 0 { "pending" } else { "" },+ "images": [],+ "attachments": 0,+ "sender": sender,+ "code": if m.self_sent { None } else { one_time_code(&m.text) },+ })+}+// ---- What the page asks ----+/// Sets the active app filter (`""` for all, `"sms"` for SMS only, or package name).+pub fn set_app_filter(app: String) {+ let app = app.trim().to_owned();+ let updated = state(|s| {+ if s.app_filter == app {+ false+ } else {+ s.app_filter = app;+ true+ }+ });+ if updated {+ changed();+ }+}++/// Deletes a persisted MessagingStyle chat conversation locally.+pub fn delete_chat_thread(thread_id: String) {+ let Some(device) = state(|s| s.device) else { return };+ let Some(node) = core_host::node() else { return };+ if node.delete_chat_thread(device, &thread_id) {+ state(|s| {+ s.chat_threads.retain(|t| t.thread_id != thread_id);+ if s.thread.as_deref() == Some(&thread_id) {+ s.thread = None;+ s.chat_messages.clear();+ }+ });+ changed();+ }+}+/// Shows `device`'s messages (again: reloads).pub fn open_device(device: DeviceId) {let same = state(|s| {@@ -236,9 +494,45 @@ pub fn open_device(device: DeviceId) {load_threads();}+/// Synchronously refreshes persisted `MessagingStyle` chat threads from SQLite.+pub fn load_chat_threads() {+ let Some(device) = state(|s| s.device) else { return };+ let Some(node) = core_host::node() else { return };+ let chat_threads = node.chat_threads(device);+ let (photos, app_icons) = save_chat_assets(device, &chat_threads);+ let open_chat = state(|s| {+ s.chat_threads = chat_threads;+ s.photos.extend(photos);+ s.app_icons.extend(app_icons);+ if !s.chat_threads.is_empty() && matches!(s.status, Status::Idle | Status::Loading) {+ s.status = Status::Ready;+ }+ if !s.app_filter.is_empty()+ && s.app_filter != "sms"+ && !s.chat_threads.iter().any(|ct| ct.app == s.app_filter)+ {+ s.app_filter.clear();+ }+ s.thread.clone().filter(|t| t.starts_with("chat:"))+ });+ if let Some(thread) = open_chat {+ let _ = node.mark_chat_thread_read(device, &thread);+ let msgs = node.chat_messages(device, &thread, PAGE);+ let refreshed = node.chat_threads(device);+ state(|s| {+ s.chat_threads = refreshed;+ if s.thread.as_deref() == Some(&thread) {+ s.chat_messages = msgs;+ }+ });+ }+ changed();+}+pub fn load_threads() {+ load_chat_threads();let Some((device, generation)) = state(|s| {- if s.threads.is_empty() {+ if s.threads.is_empty() && s.chat_threads.is_empty() {s.status = Status::Loading;}s.device.map(|d| (d, s.generation))@@ -266,7 +560,11 @@ pub fn load_threads() {}Err(e) => {tracing::debug!(error = %e, "can't list conversations");- s.status = Status::of(&e);+ if s.chat_threads.is_empty() {+ s.status = Status::of(&e);+ } else {+ s.status = Status::Ready;+ }}}});@@ -298,19 +596,77 @@ fn save_photos(device: DeviceId, threads: &[SmsThread]) -> HashMap<String, PathB.collect()}+/// Saves chat conversation avatars and app icons to disk for QML `RoundedImage`.+fn save_chat_assets(+ device: DeviceId,+ threads: &[ChatThreadRecord],+) -> (HashMap<String, PathBuf>, HashMap<String, PathBuf>) {+ let dir = files_dir();+ if std::fs::create_dir_all(&dir).is_err() {+ return (HashMap::new(), HashMap::new());+ }+ let mut photos = HashMap::new();+ let mut app_icons = HashMap::new();+ for t in threads {+ if let Some(avatar) = t.avatar.as_ref().filter(|b| !b.is_empty()) {+ let ext = if avatar.starts_with(b"\x89PNG") { "png" } else { "jpg" };+ let path = dir.join(format!(+ "chat-avatar-{:016x}.{ext}",+ crate::photos::fingerprint(&format!("{device} {}", t.thread_id))+ ^ crate::photos::fingerprint_bytes(avatar)+ ));+ if path.exists() || std::fs::write(&path, avatar).is_ok() {+ photos.insert(t.thread_id.clone(), path);+ }+ }+ if !app_icons.contains_key(&t.app)+ && let Some(icon) = t.app_icon.as_ref().filter(|b| !b.is_empty())+ {+ let path = dir.join(format!(+ "chat-app-{:016x}.png",+ crate::photos::fingerprint(&format!("{device} {}", t.app))+ ^ crate::photos::fingerprint_bytes(icon)+ ));+ if path.exists() || std::fs::write(&path, icon).is_ok() {+ app_icons.insert(t.app.clone(), path);+ }+ }+ }+ (photos, app_icons)+}+/// Opens a conversation: its latest messages.pub fn open_thread(thread: String) {- state(|s| {+ let is_chat = thread.starts_with("chat:");+ let device = state(|s| {s.compose_to = None;s.compose_name = None;if s.thread.as_deref() != Some(&thread) {- s.thread = Some(thread);+ s.thread = Some(thread.clone());s.messages.clear();+ s.chat_messages.clear();s.more = false;+ s.staged_attachment = None;}+ s.device});- changed();- load_messages(None);+ if is_chat {+ if let (Some(device), Some(node)) = (device, core_host::node()) {+ let _ = node.mark_chat_thread_read(device, &thread);+ let msgs = node.chat_messages(device, &thread, PAGE);+ let refreshed = node.chat_threads(device);+ state(|s| {+ s.chat_threads = refreshed;+ if s.thread.as_deref() == Some(&thread) {+ s.chat_messages = msgs;+ }+ });+ }+ changed();+ } else {+ changed();+ load_messages(None);+ }}/// Opens an existing 1-on-1 conversation with `number` on `device`, or starts@@ -335,6 +691,7 @@ pub fn start_chat(device: DeviceId, number: String, name: String) {if found.is_none() {s.thread = None;s.messages.clear();+ s.chat_messages.clear();s.more = false;s.compose_to = Some(number.clone());s.compose_name = (!name.is_empty() && name != number).then_some(name);@@ -357,10 +714,52 @@ pub fn start_chat(device: DeviceId, number: String, name: String) {}}+/// Returns cached `(display_name, address_or_app, thread_id)` triples for+/// `device` synchronously (0 ms) for the Command Palette, and triggers+/// background thread loading if not yet cached.+pub fn palette_recipients(device: DeviceId) -> Vec<(String, String, String)> {+ let mut out = Vec::new();+ let should_load_sms = state(|s| {+ if s.device == Some(device) {+ for t in &s.threads {+ let title = title_of(t);+ let addr = t.addresses.first().cloned().unwrap_or_default();+ out.push((title, addr, t.id.clone()));+ }+ s.threads.is_empty() && s.status != Status::Loading+ } else if s.device.is_none() {+ *s = State { device: Some(device), generation: s.generation + 1, ..State::default() };+ true+ } else {+ false+ }+ });+ if let Some(node) = core_host::node() {+ for ct in node.chat_threads(device) {+ let app_label = if ct.app_name.trim().is_empty() { ct.app } else { ct.app_name };+ out.push((ct.title, app_label, ct.thread_id));+ }+ }+ if should_load_sms {+ load_threads();+ }+ out+}++/// Opens a specific SMS or chat thread on `device` from the Command Palette.+pub fn open_thread_from_palette(device: DeviceId, thread_id: &str) {+ let thread_id = thread_id.trim();+ if thread_id.is_empty() {+ return;+ }+ open_device(device);+ open_thread(thread_id.to_owned());+}+/// The open conversation's older messages.pub fn load_older() {let before = state(|s| {- if s.loading_older || !s.more {+ if s.loading_older || !s.more || s.thread.as_deref().is_some_and(|t| t.starts_with("chat:")) {return None;}s.loading_older = true;@@ -377,6 +776,10 @@ fn load_messages(before: Option<i64>) {else {return;};+ if thread.starts_with("chat:") {+ load_chat_threads();+ return;+ }let Some(node) = core_host::node() else { return };core_host::spawn(async move {let result = node.sms_messages(device, thread.clone(), before, PAGE).await;@@ -411,7 +814,9 @@ fn load_messages(before: Option<i64>) {}Err(e) => {tracing::debug!(error = %e, "can't read a conversation");- s.status = Status::of(&e);+ if s.chat_threads.is_empty() {+ s.status = Status::of(&e);+ }Vec::new()}}@@ -452,6 +857,151 @@ async fn fetch_pictures(device: DeviceId, generation: u64, parts: Vec<String>) {}}+// ---- MMS Attachment Staging ----++/// Formats a byte count into a human-readable string (`245 KB`, `1.4 MB`).+pub fn format_attachment_size(bytes: usize) -> String {+ if bytes < 1024 {+ format!("{bytes} B")+ } else if bytes < 1024 * 1024 {+ let kb = (bytes + 512) / 1024;+ format!("{} KB", kb.max(1))+ } else {+ format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))+ }+}++fn url_to_path(raw: &str) -> PathBuf {+ let trimmed = raw.trim();+ let stripped = if let Some(rest) = trimmed.strip_prefix("file:///") {+ rest+ } else if let Some(rest) = trimmed.strip_prefix("file://") {+ rest+ } else {+ return PathBuf::from(trimmed);+ };+ let bytes = stripped.as_bytes();+ let mut out = Vec::with_capacity(bytes.len());+ let mut i = 0;+ while i < bytes.len() {+ if bytes[i] == b'%'+ && i + 2 < bytes.len()+ && let Ok(hex) = std::str::from_utf8(&bytes[i + 1..i + 3])+ && let Ok(val) = u8::from_str_radix(hex, 16)+ {+ out.push(val);+ i += 3;+ continue;+ }+ out.push(bytes[i]);+ i += 1;+ }+ PathBuf::from(String::from_utf8_lossy(&out).into_owned())+}++fn detect_or_convert_image(path: &Path, raw: Vec<u8>) -> Result<(String, Vec<u8>), String> {+ if raw.starts_with(b"\x89PNG\r\n\x1a\n") {+ return Ok(("image/png".into(), raw));+ }+ if raw.starts_with(b"\xff\xd8\xff") {+ return Ok(("image/jpeg".into(), raw));+ }+ if raw.starts_with(b"GIF87a") || raw.starts_with(b"GIF89a") {+ return Ok(("image/gif".into(), raw));+ }+ if raw.len() >= 12 && &raw[0..4] == b"RIFF" && &raw[8..12] == b"WEBP" {+ return Ok(("image/webp".into(), raw));+ }+ let ext = path.extension().and_then(|e| e.to_str()).unwrap_or_default().to_ascii_lowercase();+ match ext.as_str() {+ "png" => return Ok(("image/png".into(), raw)),+ "jpg" | "jpeg" => return Ok(("image/jpeg".into(), raw)),+ "gif" => return Ok(("image/gif".into(), raw)),+ "webp" => return Ok(("image/webp".into(), raw)),+ _ => {}+ }+ // Convert BMP/TIFF/ICO or other Windows-decodable images to PNG.+ let bitmap = crate::win::image::decode(&raw)+ .map_err(|_| "Only image files (PNG, JPEG, WebP, GIF, BMP) can be attached.".to_owned())?;+ let png = crate::win::image::encode_png(&bitmap).map_err(|e| e.to_string())?;+ Ok(("image/png".into(), png))+}++fn stage_image_bytes(name: String, mime: String, data: Vec<u8>) -> bool {+ let size_text = format_attachment_size(data.len());+ let ext = if mime.contains("png") {+ "png"+ } else if mime.contains("gif") {+ "gif"+ } else if mime.contains("webp") {+ "webp"+ } else {+ "jpg"+ };+ let dir = files_dir();+ let preview_path = std::fs::create_dir_all(&dir).ok().and_then(|()| {+ let p = dir.join(format!("mms-stage-{:016x}.{ext}", crate::photos::fingerprint_bytes(&data)));+ (p.exists() || std::fs::write(&p, &data).is_ok()).then_some(p)+ });+ let preview_url = preview_path.as_ref().map(|p| crate::icons::file_url(p)).unwrap_or_default();+ let (data, error) = if data.len() > MAX_ATTACHMENT_BYTES {+ (Vec::new(), format!("Image is {size_text} — MMS supports up to 900 KB"))+ } else {+ (data, String::new())+ };+ state(|s| {+ s.staged_attachment =+ Some(StagedAttachment { name, mime, data, preview_path, preview_url, size_text, error });+ });+ changed();+ true+}++/// Stages an image file from disk (via drag-and-drop or file picker) as an MMS attachment.+pub fn attach_file(path_or_url: String) -> bool {+ let path = url_to_path(&path_or_url);+ let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("image.png").to_owned();+ let raw = match std::fs::read(&path) {+ Ok(b) if !b.is_empty() => b,+ _ => return false,+ };+ match detect_or_convert_image(&path, raw) {+ Ok((mime, data)) => stage_image_bytes(name, mime, data),+ Err(error) => {+ state(|s| {+ s.staged_attachment = Some(StagedAttachment {+ name,+ mime: String::new(),+ data: Vec::new(),+ preview_path: None,+ preview_url: String::new(),+ size_text: String::new(),+ error,+ });+ });+ changed();+ false+ }+ }+}++/// Stages an image from the Windows clipboard (`Ctrl+V`) as an MMS attachment,+/// returning `true` when an image was on the clipboard.+pub fn paste_clipboard_image() -> bool {+ let Some(png) = crate::win::clipboard::read_image_png() else {+ return false;+ };+ stage_image_bytes("Clipboard image.png".into(), "image/png".into(), png)+}++/// Clears any staged MMS image attachment.+pub fn clear_attachment() {+ let had = state(|s| s.staged_attachment.take().is_some());+ if had {+ changed();+ }+}+/// Why a text wasn't sent, for the page.pub fn send_problem(error: &Error) -> &'static str {match error {@@ -460,13 +1010,28 @@ pub fn send_problem(error: &Error) -> &'static str {"The phone doesn't send texts for this PC. Allow it in the Nectarlink app on the phone."}Error::Offline | Error::NotPaired | Error::Timeout => "The phone isn't connected.",- Error::TooLarge => "That's too long for a text.",+ Error::TooLarge => "That attachment or message is too large (MMS supports up to 900 KB).",_ => "The phone couldn't send it.",}}-/// Sends a text in the open conversation.+fn chat_reply_problem(error: &Error) -> &'static str {+ match error {+ Error::Denied => "Notifications are turned off for this phone.",+ Error::Offline | Error::NotPaired | Error::Timeout => "The phone isn't connected.",+ _ => "Couldn't send that reply — the notification may have been dismissed on your phone.",+ }+}++/// Sends a text or inline chat reply in the open conversation.pub fn send(body: String) {+ let open = state(|s| s.thread.clone());+ if let Some(thread) = open.as_deref()+ && thread.starts_with("chat:")+ {+ send_chat_reply(thread.to_owned(), body);+ return;+ }let to = state(|s| {let thread = s.thread.as_ref()?;s.threads.iter().find(|t| &t.id == thread).map(|t| t.addresses.clone())@@ -478,15 +1043,75 @@ pub fn send(body: String) {}}-/// Sends a text to a number (a new conversation), then opens it.+fn send_chat_reply(thread_id: String, body: String) {+ let text = body.trim().to_owned();+ if text.is_empty() {+ return;+ }+ let now = now_ms();+ let local_id = -now.max(1);+ let Some(device) = state(|s| {+ s.sending = true;+ s.chat_messages.push(ChatMessageRecord {+ id: local_id,+ peer: s.device?,+ thread_id: thread_id.clone(),+ sender: None,+ text: text.clone(),+ time_ms: now,+ self_sent: true,+ avatar: None,+ });+ s.device+ }) else {+ return;+ };+ changed();+ let Some(node) = core_host::node() else { return };+ core_host::spawn(async move {+ let result = node.reply_chat_thread(device, &thread_id, text).await;+ let refreshed_threads = node.chat_threads(device);+ let refreshed_messages = node.chat_messages(device, &thread_id, PAGE);+ state(|s| {+ s.sending = false;+ s.chat_threads = refreshed_threads;+ if s.thread.as_deref() == Some(&thread_id) {+ s.chat_messages = refreshed_messages;+ }+ });+ changed();+ if let Err(e) = result {+ crate::bridge::app::show_message(chat_reply_problem(&e));+ }+ });+}++/// Sends a text (and optional staged MMS image attachment) to a number, then opens it.pub fn send_to(to: Vec<String>, body: String) {static NEXT_LOCAL: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);let now = now_ms();- let local_id = format!("local:{now}:{}", NEXT_LOCAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed));- let Some(device) = state(|s| {+ let seq = NEXT_LOCAL.fetch_add(1, std::sync::atomic::Ordering::Relaxed);+ let local_id = format!("local:{now}:{seq}");+ let Some((device, attachments)) = state(|s| {+ let valid_att =+ s.staged_attachment.as_ref().filter(|a| a.error.is_empty() && !a.data.is_empty()).cloned();+ if body.trim().is_empty() && valid_att.is_none() {+ return None;+ }+ s.staged_attachment = None;s.sending = true;s.compose_to = None;s.compose_name = None;+ let mut parts = Vec::new();+ let mut attachments = Vec::new();+ if let Some(att) = valid_att {+ let part_id = format!("local-part:{now}:{seq}");+ if let Some(path) = att.preview_path {+ s.pictures.insert(part_id.clone(), path);+ }+ parts.push(SmsPart { id: part_id, mime: att.mime.clone(), size: att.data.len() as u64 });+ attachments.push(SmsAttachment { mime: att.mime, data: att.data });+ }// Shown at once as "Sending…"; the phone's copy replaces it.if let Some(thread) = s.thread.clone() {s.messages.insert(@@ -499,18 +1124,22 @@ pub fn send_to(to: Vec<String>, body: String) {date: now,outgoing: true,status: Some("pending".into()),- parts: Vec::new(),+ parts,},);}- s.device+ Some((s.device?, attachments))}) else {return;};changed();let Some(node) = core_host::node() else { return };core_host::spawn(async move {- let result = node.sms_send(device, to.clone(), body).await;+ let result = if attachments.is_empty() {+ node.sms_send(device, to.clone(), body).await+ } else {+ node.send_sms_with_attachments(device, to.clone(), body, attachments).await+ };state(|s| {s.sending = false;if result.is_err() {@@ -570,7 +1199,9 @@ pub fn close_thread() {s.compose_to = None;s.compose_name = None;s.messages.clear();+ s.chat_messages.clear();s.more = false;+ s.staged_attachment = None;});changed();}@@ -601,13 +1232,23 @@ pub fn on_event(event: &NodeEvent) {});}}- NodeEvent::LinkChanged { device: d, link: LinkState::Online { .. } }- if Some(*d) == device && state(|s| s.status != Status::Ready) =>- {+ NodeEvent::NotificationPosted { device: d, .. }+ | NodeEvent::NotificationRemoved { device: d, .. }+ | NodeEvent::NotificationsReset { device: d, .. } => {+ if Some(*d) == device {+ load_chat_threads();+ }+ }+ NodeEvent::LinkChanged { device: d, link: LinkState::Online { .. } } if Some(*d) == device => {load_threads();}NodeEvent::LinkChanged { device: d, link: LinkState::Offline { .. } } if Some(*d) == device => {- state(|s| s.status = Status::Offline);+ load_chat_threads();+ state(|s| {+ if s.threads.is_empty() && s.chat_threads.is_empty() {+ s.status = Status::Offline;+ }+ });changed();}_ => {}@@ -682,4 +1323,15 @@ mod tests {assert_eq!(digits("+1 (555) 010-0100"), digits("5550100100"));assert_eq!(digits("12"), "12");}++ #[test]+ fn attachment_sizes_and_url_paths_are_formatted() {+ assert_eq!(format_attachment_size(512), "512 B");+ assert_eq!(format_attachment_size(245 * 1024), "245 KB");+ assert_eq!(format_attachment_size(1_468_006), "1.4 MB");+ assert_eq!(+ url_to_path("file:///C:/Users/Sam/My%20Photo.png"),+ PathBuf::from("C:/Users/Sam/My Photo.png")+ );+ }}diff --git a/desktop/app/src/mirror.rs b/desktop/app/src/mirror.rsindex 3d6c735..328d0c7 100644--- a/desktop/app/src/mirror.rs+++ b/desktop/app/src/mirror.rs@@ -424,6 +424,69 @@ pub fn load_apps(device: DeviceId) {});}+/// Returns cached `(pkg, label)` pairs for `device` synchronously (0 ms) for+/// the Command Palette, placing recently mirrored apps first and triggering a+/// background app list load if not yet cached.+pub fn palette_apps(device: DeviceId) -> Vec<(String, String)> {+ let recent = recent_apps(&device);+ let current = apps(&device);+ let (app_names, can_load) = core_host::host().hub.read(|s| {+ let online = s+ .devices+ .iter()+ .any(|d| d.id == device && matches!(d.link, nectarlink_core::LinkState::Online { .. }));+ let avail = s.matrices.get(&device).and_then(|m| m.state("mirroring.app_windows"))+ == Some(nectarlink_core::FeatureState::Available);+ (s.app_names.clone(), online && avail)+ });++ if current.is_none() && can_load {+ load_apps(device);+ }++ let ready_list = match current {+ Some(Apps::Ready(list)) => list,+ _ => Vec::new(),+ };++ let mut seen = std::collections::HashSet::new();+ let mut out = Vec::new();++ for pkg in &recent {+ let pkg_trim = pkg.trim();+ if pkg_trim.is_empty() {+ continue;+ }+ let label = ready_list+ .iter()+ .find(|a| a.pkg == pkg_trim)+ .map(|a| a.label.clone())+ .or_else(|| app_names.get(pkg_trim).cloned())+ .unwrap_or_else(|| pkg_trim.rsplit('.').next().unwrap_or(pkg_trim).to_owned());+ if !label.trim().is_empty() && seen.insert(pkg_trim.to_owned()) {+ out.push((pkg_trim.to_owned(), label));+ }+ }++ for a in ready_list {+ let pkg_trim = a.pkg.trim();+ let label_trim = a.label.trim();+ if !pkg_trim.is_empty() && !label_trim.is_empty() && seen.insert(pkg_trim.to_owned()) {+ out.push((pkg_trim.to_owned(), label_trim.to_owned()));+ }+ }++ for (pkg, label) in app_names {+ let pkg_trim = pkg.trim();+ let label_trim = label.trim();+ if !pkg_trim.is_empty() && !label_trim.is_empty() && seen.insert(pkg_trim.to_owned()) {+ out.push((pkg_trim.to_owned(), label_trim.to_owned()));+ }+ }++ out+}+/// The PC's mouse and keyboard on a mirrored screen or app window, sent in/// order.pub fn input(window: Window, input: nectarlink_core::MirrorInput) {diff --git a/desktop/app/src/notification_store.rs b/desktop/app/src/notification_store.rsindex d9219dc..2d49c3f 100644--- a/desktop/app/src/notification_store.rs+++ b/desktop/app/src/notification_store.rs@@ -95,6 +95,14 @@ pub fn load(dir: &Path, state: &mut AppState) {}}+/// Immediately removes the on-disk notification history file (`notification-history.json`)+/// and cached notification images in `cache/images/`.+pub fn clear_disk() {+ let data_dir = &core_host::host().data_dir;+ let _ = remove(&data_dir.join(HISTORY_FILE));+ crate::notifications::clear_cached_images();+}+fn snapshot(state: &AppState) -> (Rules, Vec<HistoryEntry>) {let apps = state.app_names@@ -135,8 +143,11 @@ pub fn start(dir: PathBuf) {tracing::warn!(error = %e, "can't save notification rules");}let history_file = dir.join(HISTORY_FILE);- let saved =- if rules.history { write(&history_file, &history) } else { remove(&history_file) };+ let saved = if rules.history && !history.is_empty() {+ write(&history_file, &history)+ } else {+ remove(&history_file)+ };if let Err(e) = saved {tracing::warn!(error = %e, "can't save notification history");}@@ -176,6 +187,7 @@ mod tests {icon: None,image: None,live: None,+ conversation: None,}}diff --git a/desktop/app/src/notifications.rs b/desktop/app/src/notifications.rsindex 4dce686..90cb6c5 100644--- a/desktop/app/src/notifications.rs+++ b/desktop/app/src/notifications.rs@@ -202,6 +202,42 @@ pub fn prune_images() {}}+fn is_notification_image_file(name: &str) -> bool {+ !name.starts_with("sms-")+ && !name.starts_with("mms-")+ && !name.starts_with("chat-")+ && !name.starts_with("photo-")+}++/// Immediately removes cached notification images from `cache/images/`.+pub fn clear_cached_images() {+ let Ok(entries) = std::fs::read_dir(images_dir()) else { return };+ for entry in entries.flatten() {+ let path = entry.path();+ let is_notif = path.file_name().and_then(|n| n.to_str()).is_some_and(is_notification_image_file);+ if is_notif {+ let _ = std::fs::remove_file(path);+ }+ }+}++/// Total byte size of cached notification images on disk.+pub fn cached_image_bytes() -> u64 {+ let Ok(entries) = std::fs::read_dir(images_dir()) else { return 0 };+ entries+ .flatten()+ .filter_map(|e| {+ let name = e.file_name();+ let name_str = name.to_str()?;+ if !is_notification_image_file(name_str) {+ return None;+ }+ let meta = e.metadata().ok()?;+ meta.is_file().then_some(meta.len())+ })+ .sum()+}+fn fnv(bytes: &[u8]) -> u64 {bytes.iter().fold(0xcbf2_9ce4_8422_2325_u64, |h, b| (h ^ u64::from(*b)).wrapping_mul(0x0100_0000_01b3))}@@ -383,6 +419,12 @@ pub fn on_toast(event: ToastEvent) {return crate::transfers::open(std::path::Path::new(key));}ToastEvent::Action { device, key, action } if device == crate::transfers::TOAST_GROUP => {+ if action == crate::transfers::ACTION_ACCEPT {+ return crate::transfers::accept(key.strip_prefix("req:").unwrap_or(key));+ }+ if action == crate::transfers::ACTION_DECLINE {+ return crate::transfers::cancel(key.strip_prefix("req:").unwrap_or(key));+ }if action == crate::transfers::ACTION_OPEN {return crate::transfers::open(std::path::Path::new(key));}@@ -537,6 +579,7 @@ mod tests {icon: None,image: None,live: None,+ conversation: None,}}diff --git a/desktop/app/src/photos.rs b/desktop/app/src/photos.rsindex ccbdc92..0395285 100644--- a/desktop/app/src/photos.rs+++ b/desktop/app/src/photos.rs@@ -549,8 +549,12 @@ pub fn release_idle_resources() {}}+fn photos_cache_root() -> PathBuf {+ core_host::host().data_dir.join("cache").join("photos-thumbs")+}+fn thumbs_dir(device: DeviceId) -> PathBuf {- core_host::host().data_dir.join("cache").join("photos-thumbs").join(device.to_string())+ photos_cache_root().join(device.to_string())}/// How much the gallery keeps on disk per phone: thumbnails, and full@@ -558,29 +562,121 @@ fn thumbs_dir(device: DeviceId) -> PathBuf {const THUMBS_BUDGET: u64 = 64 << 20;const FULL_BUDGET: u64 = 512 << 20;-/// Trims a phone's gallery cache to its budgets.-fn prune_cache(device: DeviceId) {- let Ok(entries) = std::fs::read_dir(thumbs_dir(device)) else { return };- let (mut thumbs, mut full): (Vec<_>, Vec<_>) = entries- .filter_map(|e| {- let e = e.ok()?;- let meta = e.metadata().ok().filter(|m| m.is_file())?;- Some((meta.modified().ok()?, meta.len(), e.path()))- })- .partition(|(_, _, path)| {- path.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("thumb-"))- });- for (files, budget) in [(&mut thumbs, THUMBS_BUDGET), (&mut full, FULL_BUDGET)] {- // Newest first; delete beyond the budget.- files.sort_by_key(|f| std::cmp::Reverse(f.0));- let mut kept = 0u64;- for (_, len, path) in files.iter() {- kept += len;- if kept > budget {+/// Retention limits for cached photo thumbnails (30 days, max 500 files).+pub const MAX_PHOTO_CACHE_AGE_SECS: u64 = 30 * 86_400;+pub const MAX_PHOTO_CACHE_FILES: usize = 500;++fn collect_cache_files(dir: &Path, out: &mut Vec<(std::time::SystemTime, u64, PathBuf)>) {+ let Ok(entries) = std::fs::read_dir(dir) else { return };+ for entry in entries.flatten() {+ let path = entry.path();+ let Ok(meta) = entry.metadata() else { continue };+ if meta.is_dir() {+ collect_cache_files(&path, out);+ } else if meta.is_file() {+ let modified = meta.modified().unwrap_or(std::time::UNIX_EPOCH);+ out.push((modified, meta.len(), path));+ }+ }+}++/// Trims the photo thumbnail and viewer cache to the 30-day age limit,+/// 500-file cap, and per-phone byte budgets.+pub fn prune_cache() {+ let root = photos_cache_root();+ let now = std::time::SystemTime::now();+ let max_age = Duration::from_secs(MAX_PHOTO_CACHE_AGE_SECS);++ let mut all_files = Vec::new();+ collect_cache_files(&root, &mut all_files);++ // 1. Remove any cached file older than 30 days.+ all_files.retain(|(modified, _, path)| {+ let expired = now.duration_since(*modified).unwrap_or_default() > max_age;+ if expired {+ let _ = std::fs::remove_file(path);+ }+ !expired+ });++ // 2. Enforce the 500-file cap across the thumbnail cache (newest first).+ if all_files.len() > MAX_PHOTO_CACHE_FILES {+ all_files.sort_by_key(|f| std::cmp::Reverse(f.0));+ for (_, _, path) in all_files.drain(MAX_PHOTO_CACHE_FILES..) {+ let _ = std::fs::remove_file(path);+ }+ }++ // 3. Enforce per-device byte budgets for thumbnails and full-res viewer files.+ let Ok(device_dirs) = std::fs::read_dir(&root) else { return };+ for dev_entry in device_dirs.flatten() {+ if !dev_entry.metadata().is_ok_and(|m| m.is_dir()) {+ continue;+ }+ let Ok(entries) = std::fs::read_dir(dev_entry.path()) else { continue };+ let (mut thumbs, mut full): (Vec<_>, Vec<_>) = entries+ .filter_map(|e| {+ let e = e.ok()?;+ let meta = e.metadata().ok().filter(|m| m.is_file())?;+ Some((meta.modified().ok()?, meta.len(), e.path()))+ })+ .partition(|(_, _, path)| {+ path.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("thumb-"))+ });+ for (files, budget) in [(&mut thumbs, THUMBS_BUDGET), (&mut full, FULL_BUDGET)] {+ files.sort_by_key(|f| std::cmp::Reverse(f.0));+ let mut kept = 0u64;+ for (_, len, path) in files.iter() {+ kept += len;+ if kept > budget {+ let _ = std::fs::remove_file(path);+ }+ }+ }+ }+}++/// Immediately deletes all cached photo thumbnails and viewer files on disk+/// and resets in-memory thumbnail state.+pub fn clear_cache() {+ let _ = std::fs::remove_dir_all(photos_cache_root());+ if let Ok(entries) = std::fs::read_dir(crate::notifications::images_dir()) {+ for entry in entries.flatten() {+ let path = entry.path();+ if path.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("photo-")) {let _ = std::fs::remove_file(path);}}}+ gallery_state(|s| {+ s.generation = s.generation.wrapping_add(1);+ s.thumbs.clear();+ s.failed_thumbs.clear();+ s.full_files.clear();+ s.wanted_thumbs.clear();+ });+ gallery_changed();+}++/// Returns `(file_count, total_bytes)` for cached photo thumbnails and previews.+pub fn cache_stats() -> (usize, u64) {+ let mut files = Vec::new();+ collect_cache_files(&photos_cache_root(), &mut files);+ let mut count = files.len();+ let mut bytes: u64 = files.iter().map(|(_, len, _)| *len).sum();+ if let Ok(entries) = std::fs::read_dir(crate::notifications::images_dir()) {+ for entry in entries.flatten() {+ let path = entry.path();+ if path.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with("photo-"))+ && let Ok(meta) = entry.metadata()+ && meta.is_file()+ {+ count += 1;+ bytes += meta.len();+ }+ }+ }+ (count, bytes)}pub(crate) fn thumb_path(device: DeviceId, id: &str) -> PathBuf {@@ -726,7 +822,7 @@ pub fn open_device(device: DeviceId) {if !same {gallery_changed();core_host::spawn(async move {- let _ = tokio::task::spawn_blocking(move || prune_cache(device)).await;+ let _ = tokio::task::spawn_blocking(prune_cache).await;});}reload();@@ -1055,6 +1151,120 @@ pub fn open_item(id: String) {});}+/// Opens the most recent photo from `device` in the default viewer, using a+/// cached local copy when available or fetching it from the phone.+pub fn open_latest_photo(device: DeviceId) {+ let cached_or_first = gallery_state(|s| {+ if s.device != Some(device) {+ return None;+ }+ let first = s.items.first()?;+ if let Some(existing) = s.full_files.get(&first.id).filter(|p| p.exists()).cloned() {+ return Some((first.id.clone(), Some(existing)));+ }+ let dir = thumbs_dir(device);+ for ext in ["jpg", "jpeg", "png", "webp", "heic"] {+ let candidate = dir.join(format!("full-{:016x}.{ext}", fingerprint(&first.id)));+ if candidate.exists() {+ s.full_files.insert(first.id.clone(), candidate.clone());+ return Some((first.id.clone(), Some(candidate)));+ }+ }+ Some((first.id.clone(), None))+ });++ if let Some((id, Some(path))) = cached_or_first {+ let _ = id;+ crate::transfers::open(&path);+ return;+ }++ let Some(node) = core_host::node() else { return };++ if let Some((id, None)) = cached_or_first {+ show_message("Opening latest photo…");+ core_host::spawn(async move {+ match node.fetch_photo(device, id.clone()).await {+ Ok(transfer) => asked(transfer, Intent::Open { item_id: Some(id) }),+ Err(e) => show_photo_error(&e),+ }+ });+ return;+ }++ // Check if the timeline already has a local saved photo for this phone while we also query the phone.+ let timeline_fallback = node+ .timeline_page(&nectarlink_core::TimelineQuery {+ kind: Some(nectarlink_core::TimelineKind::Photo),+ device: Some(device),+ limit: 10,+ ..nectarlink_core::TimelineQuery::default()+ })+ .map(|p| p.entries)+ .unwrap_or_default()+ .into_iter()+ .find_map(|entry| {+ let first_target = entry.target.lines().next().unwrap_or("").trim();+ let p = PathBuf::from(first_target);+ (!first_target.is_empty() && p.exists()).then_some(p)+ });++ let online = core_host::host().hub.read(|s| {+ s.devices+ .iter()+ .any(|d| d.id == device && matches!(d.link, nectarlink_core::LinkState::Online { .. }))+ });+ if !online {+ if let Some(path) = timeline_fallback {+ crate::transfers::open(&path);+ } else {+ show_message("The phone isn't connected right now.");+ }+ return;+ }++ show_message("Opening latest photo…");+ core_host::spawn(async move {+ match node.photo_list(device, None, None, 1).await {+ Ok(items) => {+ let Some(item) = items.into_iter().next() else {+ if let Some(path) = timeline_fallback {+ crate::transfers::open(&path);+ } else {+ show_message("No recent photos found on the phone.");+ }+ return;+ };+ let dir = thumbs_dir(device);+ for ext in ["jpg", "jpeg", "png", "webp", "heic"] {+ let candidate = dir.join(format!("full-{:016x}.{ext}", fingerprint(&item.id)));+ if candidate.exists() {+ crate::transfers::open(&candidate);+ return;+ }+ }+ match node.fetch_photo(device, item.id.clone()).await {+ Ok(transfer) => asked(transfer, Intent::Open { item_id: Some(item.id) }),+ Err(e) => {+ if let Some(path) = timeline_fallback {+ crate::transfers::open(&path);+ } else {+ show_photo_error(&e);+ }+ }+ }+ }+ Err(e) => {+ if let Some(path) = timeline_fallback {+ crate::transfers::open(&path);+ } else {+ show_photo_error(&e);+ }+ }+ }+ });+}+/// Copies one image `id` to the PC clipboard, downloading it first if needed.pub fn copy_item(id: String) {if id.is_empty() {diff --git a/desktop/app/src/recordings.rs b/desktop/app/src/recordings.rsindex ac13411..9b0370c 100644--- a/desktop/app/src/recordings.rs+++ b/desktop/app/src/recordings.rs@@ -20,16 +20,12 @@ use windows::{IMFAttributes, IMFMediaType, IMFSample, IMFSourceReader, MF_MT_AUDIO_AVG_BYTES_PER_SECOND,MF_MT_AUDIO_BITS_PER_SAMPLE, MF_MT_AUDIO_NUM_CHANNELS, MF_MT_AUDIO_SAMPLES_PER_SECOND,MF_MT_MAJOR_TYPE, MF_MT_SUBTYPE, MF_SOURCE_READER_FIRST_AUDIO_STREAM,- MF_SOURCE_READERF_ENDOFSTREAM, MF_TRANSCODE_CONTAINERTYPE, MF_VERSION, MFAudioFormat_FLAC,- MFAudioFormat_MP3, MFAudioFormat_PCM, MFCreateAttributes, MFCreateMediaType,- MFCreateSinkWriterFromURL, MFCreateSourceReaderFromURL, MFMediaType_Audio, MFSTARTUP_NOSOCKET,- MFShutdown, MFStartup, MFT_ENUM_FLAG_ALL, MFTranscodeContainerType_MP3,+ MF_SOURCE_READERF_ENDOFSTREAM, MF_TRANSCODE_CONTAINERTYPE, MFAudioFormat_FLAC, MFAudioFormat_MP3,+ MFAudioFormat_PCM, MFCreateAttributes, MFCreateMediaType, MFCreateSinkWriterFromURL,+ MFCreateSourceReaderFromURL, MFMediaType_Audio, MFT_ENUM_FLAG_ALL, MFTranscodeContainerType_MP3,MFTranscodeGetAudioOutputAvailableTypes,},- System::{- Com::{COINIT_MULTITHREADED, CoInitializeEx},- SystemInformation::GetLocalTime,- },+ System::SystemInformation::GetLocalTime,},core::{HSTRING, Interface},};@@ -38,7 +34,10 @@ use crate::{core_host,settings::{RecordingFormat, Settings},transfers::{ACTION_OPEN, ACTION_SHOW, TOAST_GROUP},- win::toast::{self, Toast},+ win::{+ h264::ensure_mf_started,+ toast::{self, Toast},+ },};#[derive(Debug, Default)]@@ -246,32 +245,10 @@ fn move_file(src: &Path, dest: &Path) -> std::io::Result<()> {Ok(())}-struct MfSession;--impl MfSession {- fn start() -> windows::core::Result<Self> {- // SAFETY: plain COM and Media Foundation initialization for this worker thread.- unsafe {- let _ = CoInitializeEx(None, COINIT_MULTITHREADED);- MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET)?;- }- Ok(Self)- }-}--impl Drop for MfSession {- fn drop(&mut self) {- // SAFETY: balances MFStartup in MfSession::start.- unsafe {- let _ = MFShutdown();- }- }-}-/// Converts `src` (`.m4a`) to `dest` in `format` (`Mp3`, `Wav`, or `Flac`)/// using Windows Media Foundation.pub fn convert_audio(src: &Path, dest: &Path, format: RecordingFormat) -> windows::core::Result<()> {- let _mf = MfSession::start()?;+ ensure_mf_started()?;let stream_idx = MF_SOURCE_READER_FIRST_AUDIO_STREAM.0 as u32;// SAFETY: Media Foundation COM interfaces with owned parameters.diff --git a/desktop/app/src/send_to.rs b/desktop/app/src/send_to.rsindex 3f36aad..4f0cb87 100644--- a/desktop/app/src/send_to.rs+++ b/desktop/app/src/send_to.rs@@ -340,9 +340,9 @@ fn update(map: &mut HashMap<String, Tracked>, transfer: &Transfer) {call: false,};match &transfer.state {- TransferState::Waiting | TransferState::Running => {+ TransferState::Requested | TransferState::Waiting | TransferState::Running => {let progress = Progress {- status: if transfer.state == TransferState::Waiting {+ status: if matches!(transfer.state, TransferState::Requested | TransferState::Waiting) {format!("Waiting for {device}…")} else {format!("Sending to {device}…")diff --git a/desktop/app/src/settings.rs b/desktop/app/src/settings.rsindex ac929fb..fe8d380 100644--- a/desktop/app/src/settings.rs+++ b/desktop/app/src/settings.rs@@ -108,6 +108,12 @@ pub struct Settings {pub webcam_mirror: bool,/// Days to keep timeline entries before auto-purging (`0` = keep up to max entries).pub timeline_retention_days: u32,+ /// Global hotkey to trigger "Take photo with phone" (e.g. `"Ctrl+Alt+C"`, or `""` to disable).+ pub continuity_photo_hotkey: String,+ /// Global hotkey to trigger "Scan document with phone" (e.g. `"Ctrl+Alt+D"`, or `""` to disable).+ pub continuity_scan_hotkey: String,+ /// Global hotkey to open the Command Palette (e.g. `"Ctrl+Alt+Space"`, or `""` to disable).+ pub command_palette_hotkey: String,}impl Default for Settings {@@ -134,6 +140,9 @@ impl Default for Settings {webcam_height: 720,webcam_mirror: false,timeline_retention_days: 90,+ continuity_photo_hotkey: "Ctrl+Alt+C".into(),+ continuity_scan_hotkey: "Ctrl+Alt+D".into(),+ command_palette_hotkey: "Ctrl+Alt+Space".into(),}}}@@ -185,7 +194,9 @@ mod tests {#[test]fn round_trips_and_defaults() {let dir = tempfile::tempdir().unwrap();- assert_eq!(Settings::load(dir.path()), Settings::default());+ let def = Settings::load(dir.path());+ assert_eq!(def, Settings::default());+ assert_eq!(def.command_palette_hotkey, "Ctrl+Alt+Space");let settings = Settings {theme: Theme::Graphite,@@ -209,6 +220,9 @@ mod tests {webcam_height: 1080,webcam_mirror: true,timeline_retention_days: 30,+ continuity_photo_hotkey: "Ctrl+Alt+P".into(),+ continuity_scan_hotkey: "Ctrl+Alt+S".into(),+ command_palette_hotkey: "Ctrl+Shift+Space".into(),};settings.save(dir.path()).unwrap();assert_eq!(Settings::load(dir.path()), settings);diff --git a/desktop/app/src/state.rs b/desktop/app/src/state.rsindex 0826af0..486f684 100644--- a/desktop/app/src/state.rs+++ b/desktop/app/src/state.rs@@ -11,8 +11,8 @@ use std::{};use nectarlink_core::{- Battery, CapabilityMatrix, DeviceId, DeviceInfo, DiscoveredDevice, LinkState, MediaPlayer, NodeEvent,- Notification, PairedDevice, PairingEvent, PairingFailure, PhoneToggles, PowerLevel, Transfer,+ Battery, CapabilityMatrix, DeviceId, DeviceInfo, DiscoveredDevice, LinkState, LocalSendPeer, MediaPlayer,+ NodeEvent, Notification, PairedDevice, PairingEvent, PairingFailure, PhoneToggles, PowerLevel, Transfer,TransferState,};@@ -57,6 +57,14 @@ impl Changes {pub const CLIPBOARD: Changes = Changes(1 << 20);/// Local cross-device timeline (kept by nectarlink_core).pub const TIMELINE: Changes = Changes(1 << 21);+ /// Continuity Camera capture request or fallback paste prompt state.+ pub const CONTINUITY: Changes = Changes(1 << 22);+ /// Command Palette open/search/results state (kept by crate::command_palette).+ pub const PALETTE: Changes = Changes(1 << 23);+ /// Data retention & local storage summary (Settings → Data & storage).+ pub const DATA_RETENTION: Changes = Changes(1 << 24);+ /// LocalSend LAN interop settings or discovered peers changed.+ pub const LOCALSEND: Changes = Changes(1 << 25);pub fn is_empty(self) -> bool {self.0 == 0@@ -260,6 +268,11 @@ pub struct AppState {pub laser: Option<(DeviceId, f32, f32)>,/// This PC's physical adapters and Wake-on-LAN readiness.pub wake: crate::win::wol::WakeStatus,+ /// Whether LocalSend LAN interop is enabled and whether port 53317 is bound for receiving.+ pub localsend_enabled: bool,+ pub localsend_receiving: bool,+ /// Discovered LocalSend peers on the LAN.+ pub localsend_peers: Vec<LocalSendPeer>,}impl AppState {@@ -275,11 +288,12 @@ impl AppState {self.devices.iter_mut().find(|d| d.id == *id)}- /// A name for any device we know of (paired or nearby).+ /// A name for any device we know of (paired, nearby, or LocalSend peer).pub fn name_of(&self, id: &DeviceId) -> Option<String> {self.device(id).map(|d| d.info.name.clone()).or_else(|| self.discovered.iter().find(|d| d.id == *id).and_then(|d| d.name.clone()))+ .or_else(|| self.localsend_peers.iter().find(|p| p.id == *id).map(|p| p.alias.clone()))}/// Replaces the device list (at startup, from the core's store).@@ -446,9 +460,13 @@ impl AppState {| NodeEvent::DeckState { .. }| NodeEvent::StorageRequested { .. }| NodeEvent::StorageChanged { .. }- | NodeEvent::Webcam { .. } => Changes::NONE,+ | NodeEvent::Webcam { .. }+ | NodeEvent::CameraCaptureRequested { .. }+ | NodeEvent::CameraCaptureCancelled { .. } => Changes::NONE,+ NodeEvent::CameraCaptureReceived { .. } => Changes::TIMELINE,NodeEvent::ClipboardHistoryChanged => Changes::CLIPBOARD | Changes::TIMELINE,NodeEvent::TimelineChanged => Changes::TIMELINE,+ NodeEvent::LocalSendChanged => Changes::LOCALSEND,// The gallery's own downloads (for its viewer, or the clipboard)// aren't files the user keeps.NodeEvent::Transfer(transfer) if crate::photos::is_private(&transfer.id) => {@@ -517,6 +535,11 @@ impl AppState {if self.transfers.len() == before { Changes::NONE } else { Changes::TRANSFERS }}+ /// Clears finished transfer records from in-memory state.+ pub fn clear_transfer_history(&mut self) -> Changes {+ self.clear_finished_transfers()+ }+/// Records a reply on its way to the phone.pub fn reply_sending(&mut self, device: DeviceId, key: &str, text: &str) -> Changes {let reply = SentReply { text: text.to_owned(), pending: true };@@ -637,18 +660,24 @@ impl AppState {self.history_enabled = on;if !on {self.history.clear();+ self.notification_images.clear();}Changes::HISTORY}pub fn clear_history(&mut self) -> Changes {- if self.history.is_empty() {+ if self.history.is_empty() && self.notification_images.is_empty() {return Changes::NONE;}self.history.clear();+ self.notification_images.clear();Changes::HISTORY}+ pub fn clear_notification_history(&mut self) -> Changes {+ self.clear_history()+ }+/// Records where a notification's picture is stored.pub fn set_notification_image(&mut self, key: (DeviceId, String), path: PathBuf) -> Changes {if self.notification_images.get(&key) == Some(&path) {@@ -907,6 +936,7 @@ mod tests {icon: Some(vec![1, 2, 3]),image: None,live: None,+ conversation: None,}}diff --git a/desktop/app/src/transfers.rs b/desktop/app/src/transfers.rsindex a303147..5959f5b 100644--- a/desktop/app/src/transfers.rs+++ b/desktop/app/src/transfers.rs@@ -14,12 +14,16 @@ use crate::{};/// The toast "device" for received-file notifications; their key is the-/// saved file's path.+/// saved file's path (or `req:<id>` for a pending incoming transfer request).pub const TOAST_GROUP: &str = "files";/// The "Open" toast action.pub const ACTION_OPEN: &str = "open";/// The "Show in folder" toast action.pub const ACTION_SHOW: &str = "show";+/// The "Accept" toast action for an incoming LocalSend transfer request.+pub const ACTION_ACCEPT: &str = "accept";+/// The "Decline" toast action for an incoming LocalSend transfer request.+pub const ACTION_DECLINE: &str = "decline";/// What a transfer is called: its file or folder's name, or how many.pub fn title(t: &Transfer) -> String {@@ -49,6 +53,13 @@ pub fn send(device: DeviceId, paths: Vec<PathBuf>) {});}+/// Accepts an incoming transfer waiting in `TransferState::Requested`.+pub fn accept(id: &str) {+ if let Some(node) = core_host::node() {+ node.accept_transfer(id);+ }+}+pub fn cancel(id: &str) {if let Some(node) = core_host::node() {node.cancel_transfer(id);@@ -57,6 +68,10 @@ pub fn cancel(id: &str) {/// Opens a received file with its app (or a folder in File Explorer).pub fn open(path: &Path) {+ if path.to_str().is_some_and(|s| s.starts_with("req:")) {+ crate::bridge::app::request_activation();+ return;+ }if let Err(e) = std::process::Command::new("explorer").arg(path).spawn() {tracing::warn!(error = %e, "can't open a received file");}@@ -64,6 +79,10 @@ pub fn open(path: &Path) {/// Shows a received file or folder in File Explorer, selected.pub fn show_in_folder(path: &Path) {+ if path.to_str().is_some_and(|s| s.starts_with("req:")) {+ crate::bridge::app::request_activation();+ return;+ }let mut select = std::ffi::OsString::from("/select,");select.push(path);if let Err(e) = std::process::Command::new("explorer").arg(select).spawn() {@@ -71,19 +90,51 @@ pub fn show_in_folder(path: &Path) {}}-/// A Windows notification when files from a phone are saved.+/// A Windows notification when files from a phone or LocalSend peer are requested or saved.pub fn on_event(event: &NodeEvent) {let NodeEvent::Transfer(t) = event else { return };// Photos the user asked to open or copy say nothing about being saved.if t.direction != Direction::Incoming || crate::photos::handles(&t.id) {return;}+ if t.state == TransferState::Requested {+ let device = core_host::host()+ .hub+ .read(|s| s.name_of(&t.device))+ .or_else(|| {+ core_host::node()+ .and_then(|n| n.localsend_peers().into_iter().find(|p| p.id == t.device).map(|p| p.alias))+ })+ .unwrap_or_else(|| "Nearby device".into());+ toast::show(Toast {+ device: TOAST_GROUP.into(),+ key: format!("req:{}", t.id),+ title: format!("{device} wants to send {}", title(t)),+ body: "Accept in Nectarlink to save to Downloads\\Nectarlink".into(),+ attribution: "LocalSend".into(),+ icon: None,+ image: None,+ actions: vec![(ACTION_ACCEPT.into(), "Accept".into()), (ACTION_DECLINE.into(), "Decline".into())],+ reply: None,+ silent: false,+ progress: None,+ call: false,+ });+ return;+ }let TransferState::Done { saved } = &t.state else { return };if t.recording {return crate::recordings::on_done(t.clone());}let Some(first) = saved.first() else { return };- let device = core_host::host().hub.read(|s| s.name_of(&t.device)).unwrap_or_else(|| "your phone".into());+ let device = core_host::host()+ .hub+ .read(|s| s.name_of(&t.device))+ .or_else(|| {+ core_host::node()+ .and_then(|n| n.localsend_peers().into_iter().find(|p| p.id == t.device).map(|p| p.alias))+ })+ .unwrap_or_else(|| "your phone".into());let title = title(t);toast::show(Toast {device: TOAST_GROUP.into(),diff --git a/desktop/app/src/win/clipboard.rs b/desktop/app/src/win/clipboard.rsindex 68688e2..58c6bca 100644--- a/desktop/app/src/win/clipboard.rs+++ b/desktop/app/src/win/clipboard.rs@@ -205,6 +205,33 @@ pub fn read() -> Clip {read_with(window())}+/// Reads an image from the clipboard as PNG bytes, if an image (`PNG`,+/// `CF_DIBV5`, or `CF_DIB`) is on the clipboard and isn't marked private.+pub fn read_image_png() -> Option<Vec<u8>> {+ // SAFETY: format availability checks need no open clipboard.+ let has = |format: u32| format != 0 && unsafe { IsClipboardFormatAvailable(format) }.is_ok();+ let png = format("PNG");+ if !has(png) && !has(CF_DIBV5) && !has(CF_DIB) {+ return None;+ }+ if has(format("ExcludeClipboardContentFromMonitorProcessing")) || has(format("Clipboard Viewer Ignore")) {+ return None;+ }+ let _open = Opened::open(window())?;+ if let Some(0) = read_dword(format("CanIncludeInClipboardHistory")) {+ return None;+ }+ let image = [(png, true), (CF_DIBV5, false), (CF_DIB, false)]+ .into_iter()+ .filter(|&(format, _)| has(format))+ .find_map(|(format, is_png)| {+ let bytes = read_block(format)?;+ Some(if is_png { Image::Png(bytes) } else { Image::Bitmap(bytes) })+ })?;+ drop(_open);+ image.to_png().ok()+}+/// Puts text on the clipboard (owned by the watcher window, so it isn't/// reported as a new copy).pub fn write(text: &str) -> Result<(), String> {diff --git a/desktop/app/src/win/h264.rs b/desktop/app/src/win/h264.rsindex 4bee853..0b4dcaf 100644--- a/desktop/app/src/win/h264.rs+++ b/desktop/app/src/win/h264.rs@@ -3,19 +3,19 @@//! MFT, licensed with Windows), in low-latency mode: each access unit in,//! its picture out right away, as NV12.-use std::mem::ManuallyDrop;+use std::{mem::ManuallyDrop, sync::Mutex, time::Duration};use windows::{Win32::{Media::MediaFoundation::{- CLSID_MSH264DecoderMFT, IMF2DBuffer, IMFMediaType, IMFSample, IMFTransform, MF_E_NOTACCEPTING,- MF_E_TRANSFORM_NEED_MORE_INPUT, MF_E_TRANSFORM_STREAM_CHANGE, MF_LOW_LATENCY, MF_MT_FRAME_SIZE,- MF_MT_MAJOR_TYPE, MF_MT_SUBTYPE, MF_MT_VIDEO_NOMINAL_RANGE, MF_MT_YUV_MATRIX, MF_VERSION,- MFCreateMediaType, MFCreateMemoryBuffer, MFCreateSample, MFMediaType_Video, MFNominalRange_0_255,- MFSTARTUP_NOSOCKET, MFShutdown, MFStartup, MFT_MESSAGE_COMMAND_FLUSH,- MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, MFT_MESSAGE_NOTIFY_START_OF_STREAM, MFT_OUTPUT_DATA_BUFFER,- MFT_OUTPUT_STREAM_PROVIDES_SAMPLES, MFVideoFormat_H264, MFVideoFormat_NV12,- MFVideoTransferMatrix_BT601,+ CLSID_MSH264DecoderMFT, CODECAPI_AVDecNumWorkerThreads, IMF2DBuffer, IMFMediaType, IMFSample,+ IMFTransform, MF_E_NOTACCEPTING, MF_E_TRANSFORM_NEED_MORE_INPUT, MF_E_TRANSFORM_STREAM_CHANGE,+ MF_LOW_LATENCY, MF_MT_FRAME_SIZE, MF_MT_MAJOR_TYPE, MF_MT_SUBTYPE, MF_MT_VIDEO_NOMINAL_RANGE,+ MF_MT_YUV_MATRIX, MF_VERSION, MFCreateMediaType, MFCreateMemoryBuffer, MFCreateSample,+ MFMediaType_Video, MFNominalRange_0_255, MFSTARTUP_NOSOCKET, MFStartup,+ MFT_MESSAGE_COMMAND_FLUSH, MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, MFT_MESSAGE_NOTIFY_END_STREAMING,+ MFT_MESSAGE_NOTIFY_START_OF_STREAM, MFT_OUTPUT_DATA_BUFFER, MFT_OUTPUT_STREAM_PROVIDES_SAMPLES,+ MFVideoFormat_H264, MFVideoFormat_NV12, MFVideoTransferMatrix_BT601,},System::Com::{CLSCTX_INPROC_SERVER, COINIT_MULTITHREADED, CoCreateInstance, CoInitializeEx},},@@ -57,24 +57,26 @@ impl Colors {}}-/// Media Foundation, started for as long as a decoder lives.-struct MfGuard;--impl MfGuard {- fn start() -> windows::core::Result<Self> {- // SAFETY: plain Media Foundation startup call.- unsafe { MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET)? };- Ok(Self)+/// Initializes COM on the calling thread and starts Media Foundation once for+/// the process so concurrent decoders and transcoders never tear down Media+/// Foundation's platform work queues under each other.+pub(crate) fn ensure_mf_started() -> windows::core::Result<()> {+ static MF_STARTED: Mutex<bool> = Mutex::new(false);+ // SAFETY: plain COM and Media Foundation startup calls.+ unsafe {+ let _ = CoInitializeEx(None, COINIT_MULTITHREADED);+ let mut started = MF_STARTED.lock().unwrap_or_else(|e| e.into_inner());+ if !*started {+ MFStartup(MF_VERSION, MFSTARTUP_NOSOCKET)?;+ *started = true;+ }+ Ok(())}}-impl Drop for MfGuard {- fn drop(&mut self) {- // SAFETY: paired with the `MFStartup` in `MfGuard::start`.- unsafe {- let _ = MFShutdown();- }- }+/// Whether an Annex B buffer contains a coded slice NAL unit (non-IDR `1` or IDR `5`).+fn has_slice(data: &[u8]) -> bool {+ data.windows(4).any(|w| w[..3] == [0, 0, 1] && matches!(w[3] & 0x1f, 1 | 5))}pub struct Decoder {@@ -84,7 +86,6 @@ pub struct Decoder {provides_samples: bool,output_size: u32,colors: Colors,- _mf: MfGuard,}impl std::fmt::Debug for Decoder {@@ -93,18 +94,28 @@ impl std::fmt::Debug for Decoder {}}+impl Drop for Decoder {+ fn drop(&mut self) {+ // SAFETY: tells the transform streaming is over before it is released.+ unsafe {+ let _ = self.transform.ProcessMessage(MFT_MESSAGE_COMMAND_FLUSH, 0);+ let _ = self.transform.ProcessMessage(MFT_MESSAGE_NOTIFY_END_STREAMING, 0);+ }+ }+}+impl Decoder {/// A decoder for the calling thread (which it stays on).pub fn new() -> windows::core::Result<Decoder> {// SAFETY: plain COM and Media Foundation calls with owned values.unsafe {- let _ = CoInitializeEx(None, COINIT_MULTITHREADED);- let mf = MfGuard::start()?;+ ensure_mf_started()?;let transform: IMFTransform =CoCreateInstance(&CLSID_MSH264DecoderMFT, None, CLSCTX_INPROC_SERVER)?;- // One picture in, one out: no reordering delay.+ // One picture in, one out: no reordering delay, single worker thread.if let Ok(attributes) = transform.GetAttributes() {let _ = attributes.SetUINT32(&MF_LOW_LATENCY, 1);+ let _ = attributes.SetUINT32(&CODECAPI_AVDecNumWorkerThreads, 1);}let input = MFCreateMediaType()?;input.SetGUID(&MF_MT_MAJOR_TYPE, &MFMediaType_Video)?;@@ -116,7 +127,6 @@ impl Decoder {provides_samples: false,output_size: 0,colors: Colors::default(),- _mf: mf,};decoder.choose_output()?;decoder.transform.ProcessMessage(MFT_MESSAGE_NOTIFY_BEGIN_STREAMING, 0)?;@@ -158,6 +168,7 @@ impl Decoder {/// Decodes one access unit (Annex B); returns the pictures it completed.pub fn decode(&mut self, data: &[u8], time_us: u64) -> windows::core::Result<Vec<Picture>> {+ let expects_picture = has_slice(data);let sample = sample_of(data, time_us)?;let mut pictures = Vec::new();// SAFETY: as above.@@ -165,11 +176,11 @@ impl Decoder {match unsafe { self.transform.ProcessInput(0, &sample, 0) } {Ok(()) => break,// Full: take what's ready first, then try again.- Err(e) if e.code() == MF_E_NOTACCEPTING => pictures.extend(self.drain()?),+ Err(e) if e.code() == MF_E_NOTACCEPTING => pictures.extend(self.drain(false)?),Err(e) => return Err(e),}}- pictures.extend(self.drain()?);+ pictures.extend(self.drain(expects_picture)?);Ok(pictures)}@@ -181,8 +192,9 @@ impl Decoder {}}- fn drain(&mut self) -> windows::core::Result<Vec<Picture>> {+ fn drain(&mut self, expects_picture: bool) -> windows::core::Result<Vec<Picture>> {let mut pictures = Vec::new();+ let mut waits = 0u32;loop {let provided = if self.provides_samples {None@@ -212,7 +224,20 @@ impl Decoder {pictures.push(self.picture(&sample)?);}}- Err(e) if e.code() == MF_E_TRANSFORM_NEED_MORE_INPUT => return Ok(pictures),+ Err(e) if e.code() == MF_E_TRANSFORM_NEED_MORE_INPUT => {+ // When an access unit includes both an SPS that triggers+ // `MF_E_TRANSFORM_STREAM_CHANGE` and a coded slice, the MFT's+ // worker thread signals the stream change as soon as it parses+ // the SPS and continues decoding the slice in parallel. Under+ // heavy CPU load, the next `ProcessOutput` after `SetOutputType`+ // can run before the worker thread finishes pushing the picture.+ if expects_picture && pictures.is_empty() && waits < 500 {+ waits += 1;+ std::thread::sleep(Duration::from_millis(1));+ continue;+ }+ return Ok(pictures);+ }Err(e) if e.code() == MF_E_TRANSFORM_STREAM_CHANGE => self.choose_output()?,Err(e) => return Err(e),}diff --git a/desktop/app/src/win/tray.rs b/desktop/app/src/win/tray.rsindex 651d231..7825c3d 100644--- a/desktop/app/src/win/tray.rs+++ b/desktop/app/src/win/tray.rs@@ -6,7 +6,13 @@//! Everything runs on the Qt GUI thread: the hidden window is created there,//! and Qt's event loop dispatches its messages.-use std::cell::{Cell, RefCell};+use std::{+ cell::{Cell, RefCell},+ sync::{+ Mutex,+ atomic::{AtomicIsize, Ordering},+ },+};use windows::{Win32::{@@ -15,19 +21,31 @@ use windows::{BI_RGB, BITMAPINFO, BITMAPINFOHEADER, CreateBitmap, CreateDIBSection, DIB_RGB_COLORS,DeleteObject, HBITMAP,},- System::LibraryLoader::GetModuleHandleW,+ System::{+ LibraryLoader::GetModuleHandleW,+ Threading::{AttachThreadInput, GetCurrentProcessId, GetCurrentThreadId},+ },UI::{+ Input::KeyboardAndMouse::{+ HOT_KEY_MODIFIERS, INPUT, INPUT_0, INPUT_MOUSE, MOD_ALT, MOD_CONTROL, MOD_NOREPEAT,+ MOD_SHIFT, MOD_WIN, MOUSEEVENTF_MOVE, MOUSEINPUT, RegisterHotKey, SendInput,+ UnregisterHotKey,+ },Shell::{NIF_ICON, NIF_MESSAGE, NIF_SHOWTIP, NIF_TIP, NIM_ADD, NIM_DELETE, NIM_MODIFY, NIM_SETVERSION,NIN_SELECT, NOTIFYICON_VERSION_4, NOTIFYICONDATAW, Shell_NotifyIconW,},WindowsAndMessaging::{- AppendMenuW, CreateIconIndirect, CreatePopupMenu, CreateWindowExW, DefWindowProcW,- DestroyIcon, DestroyMenu, DestroyWindow, GetSystemMetrics, HICON, HMENU, ICONINFO, MF_GRAYED,- MF_SEPARATOR, MF_STRING, PostMessageW, RegisterClassExW, RegisterWindowMessageW, SM_CXSMICON,- SetForegroundWindow, TPM_BOTTOMALIGN, TPM_RETURNCMD, TPM_RIGHTBUTTON, TrackPopupMenuEx,- WINDOW_EX_STYLE, WM_APP, WM_CONTEXTMENU, WM_NULL, WM_POWERBROADCAST, WM_SETTINGCHANGE,- WNDCLASSEXW, WS_EX_TOOLWINDOW, WS_POPUP,+ AppendMenuW, BringWindowToTop, CreateIconIndirect, CreatePopupMenu, CreateWindowExW,+ DefWindowProcW, DestroyIcon, DestroyMenu, DestroyWindow, GW_HWNDNEXT, GWL_EXSTYLE,+ GetClassNameW, GetForegroundWindow, GetSystemMetrics, GetTopWindow, GetWindow,+ GetWindowLongW, GetWindowTextW, GetWindowThreadProcessId, HICON, HMENU, ICONINFO, IsIconic,+ IsWindow, IsWindowVisible, KillTimer, MF_GRAYED, MF_SEPARATOR, MF_STRING, PostMessageW,+ RegisterClassExW, RegisterWindowMessageW, SM_CXSMICON, SW_RESTORE, SendMessageW,+ SetForegroundWindow, SetTimer, ShowWindow, TPM_BOTTOMALIGN, TPM_RETURNCMD, TPM_RIGHTBUTTON,+ TrackPopupMenuEx, WINDOW_EX_STYLE, WM_APP, WM_CONTEXTMENU, WM_HOTKEY, WM_NULL,+ WM_POWERBROADCAST, WM_SETTINGCHANGE, WM_TIMER, WNDCLASSEXW, WS_EX_NOACTIVATE,+ WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_POPUP,},},},@@ -38,6 +56,9 @@ use windows::{#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum TrayEvent {Open,+ CommandPalette,+ TakePhoto,+ ScanDocument,FindPhone,/// Open the link on the clipboard on the phone.OpenLinkOnPhone,@@ -53,25 +74,42 @@ pub enum TrayEvent {#[derive(Debug, Clone)]pub struct MenuLabels {pub open: String,+ pub command_palette: String,+ pub take_photo: String,+ pub scan_document: String,pub find_phone: String,pub open_link: String,pub quit: String,}const CALLBACK_MESSAGE: u32 = WM_APP + 1;+const WM_UPDATE_HOTKEYS: u32 = WM_APP + 2;+const WM_PASTE_MSG: u32 = 0x0302;+const TIMER_TRACK_FG: usize = 1;+const HOTKEY_PHOTO_ID: i32 = 101;+const HOTKEY_SCAN_ID: i32 = 102;+const HOTKEY_PALETTE_ID: i32 = 103;const ICON_ID: u32 = 1;const CMD_OPEN: u32 = 1;const CMD_FIND_PHONE: u32 = 2;const CMD_QUIT: u32 = 3;const CMD_OPEN_LINK: u32 = 4;+const CMD_TAKE_PHOTO: u32 = 5;+const CMD_SCAN_DOCUMENT: u32 = 6;+const CMD_COMMAND_PALETTE: u32 = 7;const PBT_APMRESUMEAUTOMATIC: usize = 0x12;/// NIN_SELECT | NINF_KEY (shellapi.h): the icon was activated with the keyboard.const NIN_KEYSELECT: u32 = NIN_SELECT | 0x1;const SPI_SETCLIENTAREAANIMATION: usize = 0x1043;-const SPI_SETDESKWALLPAPER: usize = 0x0014;+const SPI_SETDESKWALLPAPER: usize = 0x014;type Handler = Box<dyn Fn(TrayEvent)>;+static TRAY_HWND: AtomicIsize = AtomicIsize::new(0);+static LAST_EXTERNAL_FOREGROUND: Mutex<Option<(isize, String)>> = Mutex::new(None);+static HOTKEY_SPECS: Mutex<(String, String, String)> =+ Mutex::new((String::new(), String::new(), String::new()));+thread_local! {static HANDLER: RefCell<Option<Handler>> = RefCell::new(None);static LABELS: RefCell<Option<MenuLabels>> = const { RefCell::new(None) };@@ -81,6 +119,295 @@ thread_local! {static TASKBAR_CREATED: Cell<u32> = const { Cell::new(0) };}+fn window_title(hwnd: HWND) -> String {+ let mut buf = [0u16; 256];+ // SAFETY: `buf` is a live stack UTF-16 buffer.+ let len = unsafe { GetWindowTextW(hwnd, &mut buf) };+ if len <= 0 {+ return String::new();+ }+ String::from_utf16_lossy(&buf[..len as usize]).trim().to_owned()+}++fn window_class_name(hwnd: HWND) -> String {+ let mut buf = [0u16; 128];+ // SAFETY: `buf` is a live stack UTF-16 buffer.+ let len = unsafe { GetClassNameW(hwnd, &mut buf) };+ if len <= 0 {+ return String::new();+ }+ String::from_utf16_lossy(&buf[..len as usize])+}++unsafe fn inspect_external_window(hwnd: HWND, self_pid: u32) -> Option<(isize, String, bool)> {+ if hwnd.0.is_null() {+ return None;+ }+ // SAFETY: Standard Win32 queries on a candidate top-level HWND.+ unsafe {+ if !IsWindow(Some(hwnd)).as_bool() || !IsWindowVisible(hwnd).as_bool() || IsIconic(hwnd).as_bool() {+ return None;+ }+ let mut pid: u32 = 0;+ GetWindowThreadProcessId(hwnd, Some(&mut pid));+ if pid == 0 || pid == self_pid {+ return None;+ }+ let ex_style = GetWindowLongW(hwnd, GWL_EXSTYLE) as u32;+ if (ex_style & WS_EX_TOOLWINDOW.0) != 0 || (ex_style & WS_EX_NOACTIVATE.0) != 0 {+ return None;+ }+ let class_name = window_class_name(hwnd);+ if matches!(+ class_name.as_str(),+ "Shell_TrayWnd"+ | "NotifyIconOverflowWindow"+ | "TopLevelWindowForOverflowXamlIsland"+ | "Progman"+ | "WorkerW"+ | "MultitaskingViewFrame"+ | "Windows.UI.Core.CoreWindow"+ ) {+ return None;+ }+ let title = window_title(hwnd);+ if title.is_empty() || title == "Default IME" || title == "MSCTFIME UI" {+ return None;+ }+ let is_topmost = (ex_style & WS_EX_TOPMOST.0) != 0;+ Some((hwnd.0 as isize, title, is_topmost))+ }+}++unsafe fn find_top_external_window(self_pid: u32) -> Option<(isize, String, bool)> {+ // SAFETY: Walking top-level windows in Z-order via GetTopWindow / GetWindow.+ unsafe {+ let mut cur = GetTopWindow(None).ok()?;+ for _ in 0..256 {+ if let Some(found) = inspect_external_window(cur, self_pid) {+ return Some(found);+ }+ match GetWindow(cur, GW_HWNDNEXT) {+ Ok(next) if !next.0.is_null() => cur = next,+ _ => break,+ }+ }+ None+ }+}++/// Records the current foreground window if it belongs to an external+/// user-facing application (not Nectarlink itself and not the Windows shell tray).+pub fn record_foreground_window() {+ // SAFETY: Standard Win32 queries on the foreground and Z-order HWNDs.+ unsafe {+ let self_pid = GetCurrentProcessId();+ let fg = GetForegroundWindow();+ if let Some((hwnd, title, _)) = inspect_external_window(fg, self_pid) {+ let mut guard = LAST_EXTERNAL_FOREGROUND.lock().unwrap_or_else(|e| e.into_inner());+ *guard = Some((hwnd, title));+ return;+ }+ if let Some((hwnd, title, is_topmost)) = find_top_external_window(self_pid) {+ let mut guard = LAST_EXTERNAL_FOREGROUND.lock().unwrap_or_else(|e| e.into_inner());+ let current_alive = guard.as_ref().is_some_and(|(h, _)| is_window_alive_and_visible(*h));+ if is_topmost || !current_alive {+ *guard = Some((hwnd, title));+ }+ }+ }+}++/// Captures the last active external foreground window `(hwnd, title)` at the+/// moment a Continuity Camera request begins, if that window is still alive+/// and visible right now.+pub fn snapshot_target_window() -> Option<(isize, String)> {+ record_foreground_window();+ let recorded = LAST_EXTERNAL_FOREGROUND.lock().unwrap_or_else(|e| e.into_inner()).clone()?;+ let (hwnd, saved_title) = recorded;+ if !is_window_alive_and_visible(hwnd) {+ return None;+ }+ let live_title = window_title(HWND(hwnd as *mut _));+ let title = if live_title.is_empty() { saved_title } else { live_title };+ Some((hwnd, title))+}++/// Returns true if `hwnd` still refers to an existing, visible top-level window.+pub fn is_window_alive_and_visible(hwnd: isize) -> bool {+ if hwnd == 0 {+ return false;+ }+ let h = HWND(hwnd as *mut _);+ // SAFETY: `IsWindow` and `IsWindowVisible` safely inspect any handle value.+ unsafe { IsWindow(Some(h)).as_bool() && IsWindowVisible(h).as_bool() }+}++/// Restores (if minimized) and brings `hwnd` to the foreground so a paste+/// shortcut can be synthesized into it. Returns false if the window is gone.+pub fn focus_window(hwnd: isize) -> bool {+ if !is_window_alive_and_visible(hwnd) {+ return false;+ }+ let h = HWND(hwnd as *mut _);+ // SAFETY: `h` was just verified to be a live visible window.+ unsafe {+ if IsIconic(h).as_bool() {+ let _ = ShowWindow(h, SW_RESTORE);+ }+ let fg = GetForegroundWindow();+ if fg == h {+ return true;+ }+ // Synthesize a zero-delta mouse input so Windows unlocks SetForegroundWindow+ // for this thread, and attach thread input to the current foreground thread.+ let noop = INPUT {+ r#type: INPUT_MOUSE,+ Anonymous: INPUT_0 {+ mi: MOUSEINPUT {+ dx: 0,+ dy: 0,+ mouseData: 0,+ dwFlags: MOUSEEVENTF_MOVE,+ time: 0,+ dwExtraInfo: 0,+ },+ },+ };+ let _ = SendInput(&[noop], std::mem::size_of::<INPUT>() as i32);++ let cur_tid = GetCurrentThreadId();+ let fg_tid = if !fg.0.is_null() { GetWindowThreadProcessId(fg, None) } else { 0 };+ let attached = if fg_tid != 0 && fg_tid != cur_tid {+ AttachThreadInput(cur_tid, fg_tid, true).as_bool()+ } else {+ false+ };+ let _ = BringWindowToTop(h);+ let _ = SetForegroundWindow(h);+ if attached {+ let _ = AttachThreadInput(cur_tid, fg_tid, false);+ }+ is_window_alive_and_visible(hwnd)+ }+}++/// If the OS foreground lock prevented `SetForegroundWindow` from making `hwnd`+/// the foreground window, sends `WM_PASTE` directly to `hwnd` so it still receives+/// the paste action.+pub fn send_paste_fallback_if_not_foreground(hwnd: isize) {+ if !is_window_alive_and_visible(hwnd) {+ return;+ }+ let h = HWND(hwnd as *mut _);+ // SAFETY: `h` is a valid live window handle.+ unsafe {+ if GetForegroundWindow() != h {+ let _ = SendMessageW(h, WM_PASTE_MSG, Some(WPARAM(0)), Some(LPARAM(0)));+ }+ }+}++/// Parses a hotkey string such as `"Ctrl+Alt+C"`, `"Ctrl+Alt+Space"` or `"Ctrl+Shift+F9"` into+/// `(HOT_KEY_MODIFIERS bits including MOD_NOREPEAT, virtual_key_code)`.+pub fn parse_hotkey(spec: &str) -> Option<(u32, u32)> {+ let trimmed = spec.trim();+ if trimmed.is_empty() || trimmed.eq_ignore_ascii_case("none") {+ return None;+ }+ let mut mods: u32 = 0;+ let mut key_vk: Option<u32> = None;+ for raw_part in trimmed.split('+') {+ let part = raw_part.trim();+ if part.is_empty() {+ return None;+ }+ let lower = part.to_ascii_lowercase();+ match lower.as_str() {+ "ctrl" | "control" => mods |= MOD_CONTROL.0,+ "alt" => mods |= MOD_ALT.0,+ "shift" => mods |= MOD_SHIFT.0,+ "win" | "super" | "meta" => mods |= MOD_WIN.0,+ "space" | "spacebar" => {+ if key_vk.is_some() {+ return None;+ }+ key_vk = Some(0x20);+ }+ _ => {+ if key_vk.is_some() {+ return None;+ }+ let vk = if part.len() == 1 {+ let b = part.as_bytes()[0];+ if b.is_ascii_alphabetic() {+ u32::from(b.to_ascii_uppercase())+ } else if b.is_ascii_digit() {+ u32::from(b)+ } else {+ return None;+ }+ } else {+ let num_str = lower.strip_prefix('f')?;+ let n: u32 = num_str.parse().ok()?;+ if (1..=12).contains(&n) {+ 0x70 + (n - 1)+ } else {+ return None;+ }+ };+ key_vk = Some(vk);+ }+ }+ }+ let vk = key_vk?;+ if mods == 0 {+ return None;+ }+ Some((mods | MOD_NOREPEAT.0, vk))+}++/// Updates the Continuity Camera and Command Palette global hotkeys (`photo_spec`, `scan_spec`, and `palette_spec`).+pub fn update_hotkeys(photo_spec: &str, scan_spec: &str, palette_spec: &str) {+ {+ let mut specs = HOTKEY_SPECS.lock().unwrap_or_else(|e| e.into_inner());+ *specs = (photo_spec.trim().to_owned(), scan_spec.trim().to_owned(), palette_spec.trim().to_owned());+ }+ let raw = TRAY_HWND.load(Ordering::Acquire);+ if raw != 0 {+ // SAFETY: Posting WM_UPDATE_HOTKEYS to the tray window wakes the tray thread to apply hotkeys.+ unsafe {+ let _ = PostMessageW(Some(HWND(raw as *mut _)), WM_UPDATE_HOTKEYS, WPARAM(0), LPARAM(0));+ }+ }+}++unsafe fn apply_hotkeys_on_window(hwnd: HWND) {+ let (photo_spec, scan_spec, palette_spec) =+ HOTKEY_SPECS.lock().unwrap_or_else(|e| e.into_inner()).clone();+ // SAFETY: Unregistering and registering hotkeys on the thread that owns `hwnd`.+ unsafe {+ let _ = UnregisterHotKey(Some(hwnd), HOTKEY_PHOTO_ID);+ let _ = UnregisterHotKey(Some(hwnd), HOTKEY_SCAN_ID);+ let _ = UnregisterHotKey(Some(hwnd), HOTKEY_PALETTE_ID);+ if let Some((mods, vk)) = parse_hotkey(&photo_spec)+ && let Err(e) = RegisterHotKey(Some(hwnd), HOTKEY_PHOTO_ID, HOT_KEY_MODIFIERS(mods), vk)+ {+ tracing::debug!(hotkey = %photo_spec, error = %e, "could not register Take Photo hotkey");+ }+ if let Some((mods, vk)) = parse_hotkey(&scan_spec)+ && let Err(e) = RegisterHotKey(Some(hwnd), HOTKEY_SCAN_ID, HOT_KEY_MODIFIERS(mods), vk)+ {+ tracing::debug!(hotkey = %scan_spec, error = %e, "could not register Scan Document hotkey");+ }+ if let Some((mods, vk)) = parse_hotkey(&palette_spec)+ && let Err(e) = RegisterHotKey(Some(hwnd), HOTKEY_PALETTE_ID, HOT_KEY_MODIFIERS(mods), vk)+ {+ tracing::debug!(hotkey = %palette_spec, error = %e, "could not register Command Palette hotkey");+ }+ }+}+/// The tray icon. Removed when dropped. Create and drop on the GUI thread.#[derive(Debug)]pub struct Tray {@@ -112,7 +439,7 @@ impl Tray {TASKBAR_CREATED.with(|t| t.set(RegisterWindowMessageW(w!("TaskbarCreated"))));// A hidden top-level window (not message-only) so it also gets// power and setting broadcasts.- CreateWindowExW(+ let hwnd = CreateWindowExW(WS_EX_TOOLWINDOW | WINDOW_EX_STYLE(0),w!("Nectarlink.TrayWindow"),w!("Nectarlink"),@@ -125,7 +452,12 @@ impl Tray {None,Some(instance.into()),None,- )?+ )?;+ TRAY_HWND.store(hwnd.0 as isize, Ordering::Release);+ let _ = SetTimer(Some(hwnd), TIMER_TRACK_FG, 250, None);+ record_foreground_window();+ apply_hotkeys_on_window(hwnd);+ hwnd};ICON.with(|i| i.set(create_icon().unwrap_or_default()));add_icon(hwnd);@@ -150,9 +482,14 @@ impl Tray {impl Drop for Tray {fn drop(&mut self) {+ TRAY_HWND.store(0, Ordering::Release);let data = icon_data(self.hwnd);- // SAFETY: removing our own icon and destroying our own window/icon.+ // SAFETY: removing our own icon, hotkeys, timer, and destroying our own window/icon.unsafe {+ let _ = UnregisterHotKey(Some(self.hwnd), HOTKEY_PHOTO_ID);+ let _ = UnregisterHotKey(Some(self.hwnd), HOTKEY_SCAN_ID);+ let _ = UnregisterHotKey(Some(self.hwnd), HOTKEY_PALETTE_ID);+ let _ = KillTimer(Some(self.hwnd), TIMER_TRACK_FG);let _ = Shell_NotifyIconW(NIM_DELETE, &data);let _ = DestroyWindow(self.hwnd);let icon = ICON.with(|i| i.replace(HICON::default()));@@ -243,8 +580,12 @@ fn emit(event: TrayEvent) {fn show_menu(hwnd: HWND, x: i32, y: i32) {let Some(labels) = LABELS.with(|l| l.borrow().clone()) else { return };+ record_foreground_window();let to_wide = |s: &str| s.encode_utf16().chain(Some(0)).collect::<Vec<u16>>();let (open, find, quit) = (to_wide(&labels.open), to_wide(&labels.find_phone), to_wide(&labels.quit));+ let palette = to_wide(&labels.command_palette);+ let take_photo = to_wide(&labels.take_photo);+ let scan_doc = to_wide(&labels.scan_document);let link = to_wide(&labels.open_link);// SAFETY: the menu and strings outlive TrackPopupMenuEx; the menu is// destroyed afterwards. SetForegroundWindow/WM_NULL are the documented@@ -252,7 +593,11 @@ fn show_menu(hwnd: HWND, x: i32, y: i32) {let command = unsafe {let Ok(menu): windows::core::Result<HMENU> = CreatePopupMenu() else { return };let find_flags = if CAN_FIND_PHONE.with(Cell::get) { MF_STRING } else { MF_STRING | MF_GRAYED };+ let _ = AppendMenuW(menu, find_flags, CMD_TAKE_PHOTO as usize, PCWSTR(take_photo.as_ptr()));+ let _ = AppendMenuW(menu, find_flags, CMD_SCAN_DOCUMENT as usize, PCWSTR(scan_doc.as_ptr()));+ let _ = AppendMenuW(menu, MF_SEPARATOR, 0, PCWSTR::null());let _ = AppendMenuW(menu, MF_STRING, CMD_OPEN as usize, PCWSTR(open.as_ptr()));+ let _ = AppendMenuW(menu, MF_STRING, CMD_COMMAND_PALETTE as usize, PCWSTR(palette.as_ptr()));let _ = AppendMenuW(menu, find_flags, CMD_FIND_PHONE as usize, PCWSTR(find.as_ptr()));let _ = AppendMenuW(menu, find_flags, CMD_OPEN_LINK as usize, PCWSTR(link.as_ptr()));let _ = AppendMenuW(menu, MF_SEPARATOR, 0, PCWSTR::null());@@ -265,7 +610,10 @@ fn show_menu(hwnd: HWND, x: i32, y: i32) {command.0 as u32};match command {+ CMD_TAKE_PHOTO => emit(TrayEvent::TakePhoto),+ CMD_SCAN_DOCUMENT => emit(TrayEvent::ScanDocument),CMD_OPEN => emit(TrayEvent::Open),+ CMD_COMMAND_PALETTE => emit(TrayEvent::CommandPalette),CMD_FIND_PHONE => emit(TrayEvent::FindPhone),CMD_OPEN_LINK => emit(TrayEvent::OpenLinkOnPhone),CMD_QUIT => emit(TrayEvent::Quit),@@ -290,6 +638,27 @@ unsafe extern "system" fn window_proc(hwnd: HWND, msg: u32, wparam: WPARAM, lpar}LRESULT(0)}+ WM_TIMER => {+ if wparam.0 == TIMER_TRACK_FG {+ record_foreground_window();+ }+ LRESULT(0)+ }+ WM_UPDATE_HOTKEYS => {+ // SAFETY: `hwnd` is owned by this thread.+ unsafe { apply_hotkeys_on_window(hwnd) };+ LRESULT(0)+ }+ WM_HOTKEY => {+ record_foreground_window();+ match wparam.0 as i32 {+ HOTKEY_PHOTO_ID => emit(TrayEvent::TakePhoto),+ HOTKEY_SCAN_ID => emit(TrayEvent::ScanDocument),+ HOTKEY_PALETTE_ID => emit(TrayEvent::CommandPalette),+ _ => {}+ }+ LRESULT(0)+ }WM_POWERBROADCAST => {if wparam.0 == PBT_APMRESUMEAUTOMATIC {emit(TrayEvent::Resumed);@@ -318,3 +687,29 @@ unsafe extern "system" fn window_proc(hwnd: HWND, msg: u32, wparam: WPARAM, lpar_ => unsafe { DefWindowProcW(hwnd, msg, wparam, lparam) },}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn parses_valid_and_invalid_hotkey_specs() {+ let (mods, vk) = parse_hotkey("Ctrl+Alt+C").expect("Ctrl+Alt+C");+ assert_eq!(mods, MOD_CONTROL.0 | MOD_ALT.0 | MOD_NOREPEAT.0);+ assert_eq!(vk, u32::from(b'C'));++ let (mods, vk) = parse_hotkey("Ctrl+Alt+Space").expect("Ctrl+Alt+Space");+ assert_eq!(mods, MOD_CONTROL.0 | MOD_ALT.0 | MOD_NOREPEAT.0);+ assert_eq!(vk, 0x20);++ let (mods, vk) = parse_hotkey(" ctrl + shift + f10 ").expect("ctrl+shift+f10");+ assert_eq!(mods, MOD_CONTROL.0 | MOD_SHIFT.0 | MOD_NOREPEAT.0);+ assert_eq!(vk, 0x79);++ assert!(parse_hotkey("").is_none());+ assert!(parse_hotkey("none").is_none());+ assert!(parse_hotkey("C").is_none());+ assert!(parse_hotkey("Ctrl+Alt").is_none());+ assert!(parse_hotkey("Ctrl+Alt+C+D").is_none());+ }+}diff --git a/docs/PLAN.md b/docs/PLAN.mdindex 5d14484..8b20f4a 100644--- a/docs/PLAN.md+++ b/docs/PLAN.md@@ -129,10 +129,10 @@ Power: 🟢 Basic · 🔵 Assist · 🟣 Elevated (see §4.6).### 3.3 Messages (SMS/MMS + chat apps)| Feature | Tier ||---|---|-| SMS/MMS threads, send, receive, search, **attachments (MMS images)** | P0 🟢 |-| **Unified Conversations**: RCS, WhatsApp, Telegram, Signal and others built from MessagingStyle notifications, with reply via RemoteInput. One inbox on PC | P1 🟢 |-| Contacts (photos, search, favorites), start a conversation or call from PC | P1 🟢 |-| Drag an image from the desktop into a thread to send it as MMS | P1 |+| SMS/MMS threads, send, receive, search, **attachments (MMS images)** ✅ | P0 🟢 |+| **Unified Conversations**: RCS, WhatsApp, Telegram, Signal and others built from MessagingStyle notifications, with reply via RemoteInput. One inbox on PC ✅ | P1 🟢 |+| Contacts (photos, search, favorites), start a conversation or call from PC ✅ | P1 🟢 |+| Drag an image from the desktop into a thread (or paste from the PC clipboard) to send it as MMS ✅ | P1 |### 3.4 Calls| Feature | Tier |@@ -163,8 +163,8 @@ current Android. Talking through the PC comes with the HFP item above.| Send files/folders both ways: drag-drop, share sheet, Explorer **"Send to phone"** context menu, Windows share sheet | P0 || Fast, resumable, multi-stream transfers with progress, pause/resume, and integrity hashing | P0 || **Flick to send**: flick a photo/file upward on the phone and it lands on the PC. Drag a file to the screen edge on the PC and it flies to the phone. Physics-based animation on both ends | P1 |-| **LocalSend protocol interop**: send to and receive from any LocalSend device (iOS, Mac, Linux). We implement its open protocol in Rust; we don't reuse its (Dart) code | P1 |-| **Coexistence**: runs side by side with KDE Connect (ports 1714–1764) and LocalSend (53317) on the same PC without conflicts. If LocalSend's port is taken, Nectarlink still sends but leaves receiving to the LocalSend app | P0 |+| **LocalSend protocol interop**: send to and receive from any LocalSend device (iOS, Mac, Linux). We implement its open protocol in Rust; we don't reuse its (Dart) code ✅ | P1 |+| **Coexistence**: runs side by side with KDE Connect (ports 1714–1764) and LocalSend (53317) on the same PC without conflicts. If LocalSend's port is taken, Nectarlink still sends but leaves receiving to the LocalSend app ✅ | P0 || **Phone storage in Explorer** (Cloud Files API: placeholders, on-demand download, native sync icons) ✅ | P1 || **Photos**: gallery with thumbnails, recent screenshots/photos pop up on PC, drag out to the desktop | P0 (recent), P1 (gallery) || Folder sync (Syncthing-style two-way folders, e.g. Camera → PC) | P2 |@@ -313,7 +313,7 @@ nectarlink/- **Encryption:** QUIC + TLS 1.3 (raw public keys), always. Relays only forward ciphertext.- **Per-device permissions:** each paired device has feature toggles. Dangerous ones (run commands, remote input into PC, file browse, PC unlock) are **off by default**.- **Local API/CLI:** a named pipe with a per-user token. No open localhost TCP port.-- **At-rest:** message and notification caches are encrypted with a device key, with an auto-purge window (default 7 days for notifications).+- **At-rest & data retention ✅:** clipboard history is sealed on disk with `KeyProtector` (DPAPI on Windows, Keystore AES-GCM on Android), capped at 50 items, and auto-purges unpinned clips older than 7 days (pinned clips stay; OTPs are never stored). Timeline defaults to 90 days (configurable: 30 / 90 / 365 days or Forever) and 5,000 entries. Notification history on PC is capped at 200 items and 24 hours. `MessagingStyle` conversation cache retains up to 200 threads per peer (100 messages/thread) for 30 days. Both apps expose per-category clear buttons and a single **"Clear everything"** action in *Settings → Privacy & Data* that wipes all local history and caches while keeping device identity, paired devices, and feature toggles intact.- Security audit before 1.0. Fuzz the protocol decoder and pairing handshake. The credential provider gets its own threat model and review.### 4.4 Connectivity: "it just works"@@ -731,7 +731,7 @@ Each phase ends with a usable, releasable product. Phase sizes are relative; act- Battery soak tests on testers' phones.- An opt-in diagnostics bundle, since there's no telemetry.10. **Island etiquette.** Auto-hide during fullscreen games, videos and presentations and when Windows is in Do Not Disturb. Handle multiple monitors.-11. **Data retention defaults** for Timeline (90 days or 5,000 entries, configurable, plus Clear all ✅), clipboard history (50 entries, encrypted, plus Clear history ✅), notification history (24 hours) and message cache: size caps, auto-purge, and one "Clear everything" button.+11. **Data retention defaults ✅:** Timeline (90 days default — configurable 30 / 90 / 365 days or Forever — and 5,000 entries cap, auto-purged + Clear all ✅), clipboard history (50 entries, 7-day auto-purge for unpinned items while pinned stay, encrypted at rest with `KeyProtector`, OTPs excluded + Clear history ✅), notification history (24 hours, 200 items cap + Clear notifications ✅), `MessagingStyle` message cache (30 days, 200 threads/peer, 100 messages/thread + Clear message cache ✅), received file transfer records (Clear transfer history without deleting files on disk ✅), and one **"Clear everything"** button (`clear_all_local_data`) in *Settings → Privacy & Data* on both PC and Android that wipes all local history and caches while preserving device identity, paired devices, and per-device feature toggles ✅.12. **Accessibility and translations from day one.** All strings translatable (`qsTr`, Android resources), RTL layouts, screen-reader labels, keyboard focus order. Retrofitting is expensive.13. **Asset licenses.** The display font under OFL, an icon set under a permissive license (e.g. Material Symbols, Apache-2.0). No Microsoft or Google logos; competitor names only in plain comparison wording.14. **Realistic pacing.** Claude writes most of the code, but every phase needs maintainer review and tester feedback. Estimate phase durations after Phase 0 from real velocity, not before.diff --git a/docs/protocol/capabilities.md b/docs/protocol/capabilities.mdindex b101902..9c1170c 100644--- a/docs/protocol/capabilities.md+++ b/docs/protocol/capabilities.md@@ -28,6 +28,12 @@ capability is missing.| `media.control` | Shares its media players for remote control ([media service](media.md); phone: needs notification access) | phone: notification access · desktop: app update || `media.remote` | Shows and controls other devices' media players | app update |+> **LocalSend v2.1 LAN interop:** Unpaired LocalSend discovery and file transfer+> (`224.0.0.167:53317` UDP multicast + HTTP/HTTPS `/api/localsend/v2/*` on TCP+> port `53317`, see [protocol §13](v0.md#13-localsend-v21-interoperability-optional-lan-interop))+> is an opt-in local setting (`localsend.enabled`) rather than a paired-session+> capability, because it operates with unpaired third-party LocalSend devices.+## Phone| ID | Meaning | Unlocked by |@@ -64,6 +70,7 @@ capability is missing.| `storage.read` | Lists folders and reads files in shared storage or picked SAF folders ([storage](storage.md)) | All files access or picked SAF folder || `storage.write` | Creates folders, writes/uploads files, renames and deletes entries in shared storage or picked SAF folders ([storage](storage.md)) | All files access or picked SAF folder || `camera.stream` | Streams a camera for webcam use ([webcam](webcam.md)) | camera permission |+| `camera.capture` | Captures a photo or document scan on request ([Continuity Camera](v0.md#12-continuity-camera-cameracapture)) | camera permission |## Desktop@@ -86,4 +93,6 @@ capability is missing.| `pc.audio` | Reports active audio output devices in `deck.state` and accepts `pc.audio.set` volume/mute changes ([deck](deck.md)) | app update || `camera.virtual` | Receives and decodes a phone's camera stream ([webcam](webcam.md)) | app update || `addon.vcam` | The virtual camera add-on is installed ([webcam](webcam.md)) | installing the add-on |+| `camera.continuity` | Requests and receives Continuity Camera photos and document scans ([Continuity Camera](v0.md#12-continuity-camera-cameracapture)) | app update |+diff --git a/docs/protocol/notifications.md b/docs/protocol/notifications.mdindex 98ebaea..a2f8edb 100644--- a/docs/protocol/notifications.md+++ b/docs/protocol/notifications.md@@ -36,8 +36,9 @@ Notification = {actions: [Action], // In display order, at most 5silent: bool, // Arrived without sound or pop-up on the phone; don't alert eithericon: bytes?, // The app's icon, PNG, at most 64 KiB (see §2.1)- image: bytes?, // The picture it shows (a photo in a message, a big picture), JPEG, at most 160 KiB- live: Live?, // Ongoing progress, timer, or Android 16 Live Update metadata (see §2.2)+ image: bytes?, // The picture it shows (a photo in a message, a big picture), JPEG, at most 160 KiB+ live: Live?, // Ongoing progress, timer, or Android 16 Live Update metadata (see §2.2)+ conversation: Conversation?, // Notification.MessagingStyle conversation metadata (see §2.3)}Action = {@@ -75,7 +76,7 @@ Live = {progress: uint?, // Current progress, 0..=maxmax: uint?, // Maximum progress (> 0 when determinate)indeterminate: bool?, // True when progress is indeterminate- chip: text?, // Short status pill text (Notification.getShortCriticalText), at most 16 chars+ chip: text?, // Short status pill text (Notification.getShortCriticalText), at most 32 charssegments: [Segment]?, // Android 16 ProgressStyle segments, at most 16points: [Point]?, // Android 16 ProgressStyle milestone points, at most 16chronometer: bool?, // Live elapsed or countdown timer relative to Notification.when@@ -99,6 +100,36 @@ or `notify.removed`). Receivers pin live notifications above regularnotifications in the feed and update both the feed card and the native toast inplace without re-alerting.+### 2.3 Conversation notifications (`conversation`)++Chat notifications built with `Notification.MessagingStyle` (RCS, WhatsApp,+Telegram, Signal, and other messaging apps) carry an optional `conversation`+map so the PC can present them in a unified Messages inbox alongside SMS:++```+Conversation = {+ v: uint, // Schema version (currently 1)+ title: text, // Group title or 1:1 contact name, at most 256 chars+ group: bool?, // True when MessagingStyle.isGroupConversation is true+ avatar: bytes?, // Conversation or contact avatar (JPEG/PNG), at most 16 KiB+ messages: [ChatMessage], // Chronological (oldest first), at most 25+}++ChatMessage = {+ sender: text?, // Display name (omitted when self_sent is true), at most 128 chars+ text: text, // Message body, at most 4,096 chars+ time: int, // Unix milliseconds+ self_sent: bool?, // True when sent by the phone's user+ avatar: bytes?, // Optional sender avatar (JPEG/PNG), at most 16 KiB+}+```++To avoid duplicating SMS threads that the SMS provider already supplies, a+phone with `sms.read` omits `conversation` when a notification from the default+SMS app matches a message already present in the system SMS/MMS provider.+Replies from the unified inbox use `notify.action` (§4) with the notification's+`reply: true` action (`RemoteInput`).+## 3. Events (phone → PC)```diff --git a/docs/protocol/sms.md b/docs/protocol/sms.mdindex e3adefb..708bd9d 100644--- a/docs/protocol/sms.md+++ b/docs/protocol/sms.md@@ -31,7 +31,7 @@ t = "sms.threads" re = n b = { threads: [ thread ] } // newt = "sms.messages" id = n b = { thread: text, ? before: int, limit: uint }t = "sms.messages" re = n b = { messages: [ message ] } // newest first-t = "sms.send" id = n b = { to: [ text ], body: text }+t = "sms.send" id = n b = { to: [ text ], ? body: text, ? attachments: [ attachment ] }t = "ok" re = nt = "sms.part" id = n b = { id: text }@@ -39,6 +39,11 @@ t = "sms.part" re = n b = { mime: text, data: bytes }t = "sms.changed" b = { ? thread: text } // phone → PC+attachment = {+ mime: "image/jpeg" | "image/png",+ data: bytes, // At most 900 KiB+}+thread = {id: text,addresses: [ text ], // The other people's numbers; more than one is a group@@ -72,9 +77,11 @@ with `BUSY`.### 2.2 Sending-`sms.send` sends `body` (at most 8 KiB, not blank) to each of `to` (1–20-numbers) as a text; the phone keeps it with the messages it sent. Errors:-`DENIED` when messages are off for the PC, `UNSUPPORTED` when the phone+`sms.send` sends `body` (at most 8 KiB) and/or `attachments` (at most 1 image,+`image/jpeg` or `image/png`, at most 900 KiB) to `to` (1–20 numbers). When+`attachments` is empty, `body` must not be blank and is sent as SMS; when an+image attachment is included, the phone sends it as MMS (`sendMultimediaMessage`).+Errors: `DENIED` when messages are off for the PC, `UNSUPPORTED` when the phonedoesn't offer `sms.send`, `INTERNAL` when it couldn't send.### 2.3 Changesdiff --git a/docs/protocol/v0.md b/docs/protocol/v0.mdindex 299c2cc..b073ed5 100644--- a/docs/protocol/v0.md+++ b/docs/protocol/v0.md@@ -359,3 +359,90 @@ rejected with `UNPAIRED` on its next connection attempt.- Implementations **MUST NOT** log message bodies of notification, message,clipboard or file-name content.- Frame and message decoders are fuzzed in CI.++## 12. Continuity Camera (`camera.capture.*`)++A paired PC that announces `camera.continuity` can ask a paired phone that+announces `camera.capture` to open its camera UI and capture either a still+photo (`"photo"`) or a perspective-cropped, contrast-enhanced document scan+(`"scan"`). Continuity Camera is gated by the per-device `"photos"` toggle on+both ends.++### 12.1 Control messages++- `camera.capture.request`: `{ request_id: text, mode: "photo" | "scan" }`+ — sent by the PC on the control stream (`request_id` is 1..=64 non-control+ bytes).+- `camera.capture.ok`: `{ request_id: text }` — sent by the phone when the+ capture UI has been launched.+- `camera.capture.cancel`: `{ request_id: text, reason?: text }` — sent by+ either device to cancel an in-flight capture request (or by the phone if+ `"photos"` sharing is disabled or the camera UI cannot be launched).++### 12.2 Result stream (`svc = "camera"`, `op = "result"`, `v = 1`)++When the user accepts the captured photo or scanned document on the phone:++1. The phone opens a bidirectional QUIC stream and sends the stream header:+ `t = "stream"`, `b = { svc: "camera", op: "result", v: 1 }`.+2. Next, the phone sends a metadata frame:+ `t = "camera.capture.meta"`,+ `b = { request_id: text, mode: "photo" | "scan", file_name: text, mime: "image/jpeg" | "image/png", size: uint, width: uint, height: uint }`+ (`1 <= size <= 33_554_432` bytes, `file_name` up to 255 bytes).+3. Immediately after the metadata frame, the phone writes `size` raw image+ bytes and finishes its send side.+4. The PC saves a copy into its downloads directory, replies `t = "ok"` (or+ `t = "error"` if denied or malformed), finishes its send side, records a+ `Photo` entry in the local timeline, and surfaces the image to the UI for+ one-click clipboard copy, Save As, or drag-and-drop.++## 13. LocalSend v2.1 Interoperability (Optional LAN Interop)++In addition to Nectarlink's native QUIC/iroh protocol, any Nectarlink node can+optionally enable **LocalSend v2.1** interoperability (off by default, stored in+the local settings table as `localsend.enabled`) to send files to and receive+files from unpaired LocalSend devices (iOS, macOS, Linux, Windows, Android) on+the local network.++### 13.1 Discovery & Coexistence++- **Multicast group:** UDP `224.0.0.167:53317` (`SO_REUSEADDR` enabled so+ multiple listeners can share the multicast group).+- **HTTP/HTTPS server port:** TCP `53317` by default (configurable via+ `NodeConfig::localsend_port` or `NECTARLINK_LOCALSEND_PORT`). The TCP listener+ peeks at the first byte of each incoming connection (`0x16` = TLS ClientHello)+ to serve both HTTPS and plain HTTP on the same socket.+- **Coexistence:** If TCP port `53317` is already bound by a standalone+ LocalSend app on the same machine, Nectarlink logs a warning, sets+ `localsend_receiving() = false` (leaving incoming transfers to the standalone+ LocalSend app), and still joins UDP multicast discovery and supports outgoing+ transfers to discovered LocalSend peers.+- **Identity & TLS:** Nectarlink generates a deterministic self-signed X.509 v3+ certificate (Ed25519) from the node's device key and advertises its uppercase+ hex SHA-256 DER digest as the LocalSend `fingerprint`. If a peer's fingerprint+ matches a paired Nectarlink `DeviceId`, it is de-duplicated in favor of the+ native Nectarlink session; otherwise a deterministic synthetic `DeviceId` is+ derived from `BLAKE3("nectarlink-localsend-peer-v1:" || fingerprint_or_addr)`.++### 13.2 REST Endpoints (`/api/localsend/v2/...`)++- `GET /api/localsend/v2/info` (and `GET /api/localsend/v1/info`) — returns+ `{ alias, version: "2.1", deviceModel, deviceType, fingerprint, download: false }`.+- `POST /api/localsend/v2/register` (and `POST /api/localsend/v1/register`) —+ records the announcing peer in the in-memory `LocalSendPeer` table and replies+ with this node's info JSON.+- `POST /api/localsend/v2/prepare-upload` — validates the incoming file manifest+ (up to 1,000 files, valid relative paths with no `..` traversal), creates a+ transfer in `TransferState::Requested`, emits `NodeEvent::TransferUpdated`,+ and waits up to 60 seconds for explicit user consent (`accept_transfer` or+ `cancel_transfer`) before returning `{ sessionId, files: { fileId: token } }`+ or HTTP `403` when declined.+- `POST /api/localsend/v2/upload?sessionId=...&fileId=...&token=...` — streams+ the raw request body to a `.part` file in the downloads folder, atomically+ renames it to the final destination path on completion, updates+ `NodeEvent::TransferUpdated`, and records the completed transfer in the+ timeline and received-files store.+- `POST /api/localsend/v2/cancel?sessionId=...` — cancels an in-flight upload+ session and transitions the transfer to `TransferState::Cancelled`.++