diff --git a/.claude/settings.json b/.claude/settings.json index b4b7cb5..e69de29 100644 --- a/.claude/settings.json +++ b/.claude/settings.json @@ -1,37 +0,0 @@ -{ - "hooks": { - "PreToolUse": [ - { - "matcher": "Edit|MultiEdit|Write", - "hooks": [ - { - "type": "command", - "command": "but claude pre-tool" - } - ] - } - ], - "PostToolUse": [ - { - "matcher": "Edit|MultiEdit|Write", - "hooks": [ - { - "type": "command", - "command": "but claude post-tool" - } - ] - } - ], - "Stop": [ - { - "matcher": "", - "hooks": [ - { - "type": "command", - "command": "but claude stop" - } - ] - } - ] - } -} diff --git a/.claude/settings.local.json b/.claude/settings.local.json deleted file mode 100644 index b4b7cb5..0000000 --- a/.claude/settings.local.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "hooks": { - "PreToolUse": [ - { - "matcher": "Edit|MultiEdit|Write", - "hooks": [ - { - "type": "command", - "command": "but claude pre-tool" - } - ] - } - ], - "PostToolUse": [ - { - "matcher": "Edit|MultiEdit|Write", - "hooks": [ - { - "type": "command", - "command": "but claude post-tool" - } - ] - } - ], - "Stop": [ - { - "matcher": "", - "hooks": [ - { - "type": "command", - "command": "but claude stop" - } - ] - } - ] - } -} diff --git a/CLAUDE.md b/CLAUDE.md index cf557af..61e2474 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -31,19 +31,19 @@ Two-crate workspace (`resolver = "2"`, edition 2021) plus a Tauri app: - **onyx-core** — Pure Rust library. Storage trait with `FileSystemStorage` implementation, `TaskRepository` (main API), data models, config, error types. No CLI/UI dependencies. `keyring` feature-gated behind `keyring-storage` (default on) for Android compatibility. - **onyx-cli** — CLI frontend using clap. Commands are in `src/commands/` (init, workspace, list, task, group). Output formatting in `src/output.rs`. -- **apps/tauri/** — Tauri v2 GUI. Svelte 5 frontend in `src/`, Rust backend in `src-tauri/` with Tauri commands that call into `onyx-core`. `notify` crate feature-gated for Android. +- **apps/tauri/** — Tauri v2 GUI. Svelte 5 frontend in `src/`, Rust backend in `src-tauri/` with Tauri commands that call into `onyx-core`. `notify` crate feature-gated for Android. `tauri-plugin-credentials/` provides cross-platform credential storage (Android Keystore via EncryptedSharedPreferences, desktop via keyring crate). ### Key patterns - **Storage trait** (`storage.rs`): Strategy pattern for task persistence. `FileSystemStorage` reads/writes markdown files with YAML frontmatter and JSON metadata files. - **Repository** (`repository.rs`): `TaskRepository` wraps a `Storage` impl and provides the public API for task/list CRUD, ordering, and grouping. Tests live here. -- **Config** (`config.rs`): `AppConfig` manages named workspaces with paths, mode (local/webdav), theme, and WebDAV URL. Stored in platform-specific config dirs via the `directories` crate. -- **Sync** (`sync.rs`): Three-way diff sync with offline queue. Auto-appends `Onyx/` to WebDAV URL. Wrapped in `tokio::time::timeout` (60s) to handle unreachable servers on Windows. -- **WebDAV** (`webdav.rs`): reqwest client with rustls-tls, 30s request timeout, 10s connect timeout. Credentials stored via `keyring` crate (feature-gated). `Zeroizing` for credential fields. Scoped keyring keys (`com.onyx.webdav.::`); auto-migrates legacy dot-separated format on load. 10MB PROPFIND response cap. +- **Config** (`config.rs`): `AppConfig` manages workspaces keyed by UUID string. `WorkspaceConfig` stores `name`, `path`, `mode` (Local/Webdav), `webdav_url`, `webdav_path` (user-selected remote folder), `theme`, and `sync_interval_secs`. `add_workspace` returns a generated UUID. Stored in platform-specific config dirs via the `directories` crate. +- **Sync** (`sync.rs`): Three-way diff sync with offline queue. Checksum-based conflict resolution: downloads remote, compares SHA-256 — identical content is a false conflict (skipped); when different, remote wins and local is recovered as a duplicate with a new UUID and `[RECOVERED FROM CONFLICT]` prefix. Auto-sync lifecycle: periodic polling (configurable interval, default 60s), debounced file-change (5s), window-focus (30s stale threshold). Wrapped in `tokio::time::timeout` (60s) to handle unreachable servers on Windows. +- **WebDAV** (`webdav.rs`): reqwest client with rustls-tls, 30s request timeout, 10s connect timeout. Rejects non-HTTPS URLs. `Zeroizing` for credential fields. `move_resource` method for WebDAV MOVE (workspace rename). 10MB PROPFIND response cap. Desktop credentials via `keyring` crate (feature-gated); Tauri GUI uses `tauri-plugin-credentials` for cross-platform support (Android Keystore + desktop keychain). ### On-disk format -Workspaces are plain folders. Each task list is a subfolder containing `.listdata.json` (metadata/ordering) and one `.md` file per task. The workspace root has `.metadata.json` for list ordering. +Workspaces are plain folders. Each task list is a subfolder containing `.listdata.json` (metadata/ordering) and one `.md` file per task. The workspace root has `.onyx-workspace.json` for list ordering and workspace detection. Sync only processes files at expected depths: `.onyx-workspace.json` at root, `.listdata.json` and `*.md` inside list directories. Task frontmatter contains `id`, `status`, `version` (u64, increments on every write, defaults to 1 for legacy files), and optionally `due`, `has_time`, `parent` (omitted when false/null). `list_tasks` auto-deduplicates by UUID, keeping the highest-version file and deleting stale copies. ### Tauri GUI structure @@ -59,12 +59,16 @@ The GUI uses Svelte 5 runes mode (`$state`, `$derived`, `$effect`, `$props()`). - **Main panel header**: Hamburger + window controls in top bar; list name (large, bold) + kebab below divider (matching task detail layout). Kebab has Rename, Group by due date, Delete completed, Delete list. - **New task**: FAB button opens bottom toast sheet (outside sliding container for fixed positioning). -### Current state (2026-04-03) +### Development phase + +Pre-alpha. No users, no released builds, no data to migrate. Breaking changes to on-disk formats, config structure, or sync conventions are free — do not add migration logic. + +### Current state (2026-04-05) - **Phase 1** (Core + CLI): Complete -- **Phase 2** (WebDAV sync): Complete — CLI + GUI sync working, auto-creates `Onyx/` subfolder on remote +- **Phase 2** (WebDAV sync): Complete — remote folder browsing, checksum-based conflict resolution, auto-sync lifecycle, per-workspace sync interval - **Phase 3** (GUI MVP): Complete -- **Phase 4** (Mobile): Tauri Android cfg-gated, needs `tauri android init` + build +- **Phase 4** (Mobile): Tauri Android cfg-gated with tauri-plugin-credentials and safe area insets; needs `tauri android init` + build ### GUI features done @@ -83,24 +87,33 @@ The GUI uses Svelte 5 runes mode (`$state`, `$derived`, `$effect`, `$props()`). - Group-by-due-date toggle per list (main panel kebab) - Delete completed tasks (main panel kebab + subtask kebab, with confirmation dialogs) - Keyboard shortcuts (Escape priority chain: settings → detail → list menu → drawer → menus) -- Setup screen with 2-step mode selection (Local Folder vs WebDAV Server), window dragging, "Open Existing Folder" option -- WebDAV setup flow with connection test, credential storage in system keychain -- WebDAV sync: auto-creates `Onyx/` subfolder on remote, 60s hard timeout, sync error display in settings +- Setup screen with 2-step mode selection (Local Folder vs WebDAV Server), window dragging, "Open Existing Folder" option, remote folder browsing +- WebDAV setup flow with connection test, credential storage via tauri-plugin-credentials (Android Keystore + desktop keychain) +- WebDAV sync: user-selected remote folder, 60s hard timeout, checksum-based conflict resolution (remote wins, local recovered as duplicate) +- Auto-sync lifecycle: periodic polling (configurable interval), debounced file-change (5s), window-focus (30s stale threshold); sync status dot (idle/synced/error/offline) and manual sync button in drawer +- Initial sync loading screen for new WebDAV workspaces +- Per-workspace sync interval (configurable in settings) +- Upload/download counts in drawer footer (hidden when zero) +- Transient sync error suppression (connectivity issues update status dot only, no error banner) - File watcher (notify crate, 500ms debounce, auto-reloads on external changes) -- Sync status indicators (last-sync time + upload/download counts chip) -- Push/pull/full sync mode selection (session-only, in settings) -- WorkspaceMode enum (local/webdav) with per-workspace config +- WorkspaceMode enum (local/webdav) with per-workspace config, UUID-keyed workspaces +- Workspace rename (local folder rename + WebDAV MOVE for remote folders, with confirmation dialog) - Desktop packaging (Linux: AppImage + .deb; Windows: MSI) - Tauri desktop-only deps (notify, keyring) feature-gated for Android compilation +- Safe area insets for mobile (CSS variables --safe-top/--safe-bottom, viewport-fit=cover) +- Task deduplication on load (handles sync conflict duplicates) - Subtask hierarchy: subtask count shown on parent tasks in list, subtask detail via three-panel slide navigation, inline add at top of subtask list (new subtasks prepend), collapsible completed subtasks section, cascade delete (parent deletion removes all subtasks with confirmation warning) - Custom confirmation dialogs (ConfirmDialog component replaces native confirm()) ### GUI features NOT yet done -- Workspace retarget/migrate - Search/filter tasks - Desktop packaging for macOS ## Roadmap See `PLAN.md` for the 7-phase roadmap. Detailed API docs in `docs/API.md`, development practices in `docs/DEVELOPMENT.md`. + +## GitButler + +If you generate code or modify files, run the gitbutler update branches MCP tool. diff --git a/PLAN.md b/PLAN.md index 55d432d..87c96b6 100644 --- a/PLAN.md +++ b/PLAN.md @@ -61,9 +61,8 @@ Tasks are stored as individual `.md` files with YAML frontmatter: --- id: 550e8400-e29b-41d4-a716-446655440000 status: backlog +version: 3 due: 2026-11-15T14:00:00Z -created: 2026-10-26T10:00:00Z -updated: 2026-10-26T12:30:00Z parent: 550e8400-e29b-41d4-a716-446655440001 --- @@ -86,8 +85,8 @@ Task { description: String, // Markdown content status: TaskStatus, // Backlog or Completed due_date: Option, - created_at: DateTime, - updated_at: DateTime, + has_time: bool, // Whether due_date includes a specific time + version: u64, // Increments on every write; used for sync dedup parent_id: Option, // For subtasks } @@ -106,12 +105,18 @@ TaskList { } AppConfig { - workspaces: HashMap, - current_workspace: Option, + workspaces: HashMap, // UUID keys + current_workspace: Option, // UUID } WorkspaceConfig { + name: String, // Display name path: PathBuf, + mode: WorkspaceMode, // Local or Webdav + webdav_url: Option, + webdav_path: Option, // User-selected remote folder + theme: Option, + sync_interval_secs: Option, } ``` @@ -119,7 +124,7 @@ WorkspaceConfig { ``` ~/Documents/Tasks/ # User-selected folder -├── .metadata.json # Global: list ordering, last opened list +├── .onyx-workspace.json # Global: list ordering, last opened list ├── My Tasks/ # Task list folder │ ├── .listdata.json # List metadata: task order, id, timestamps │ ├── Buy groceries.md # Title: "Buy groceries" (without .md) @@ -132,7 +137,7 @@ WorkspaceConfig { **Note**: Task titles are derived from filenames by removing the `.md` extension. -**`.metadata.json` (root level)**: +**`.onyx-workspace.json` (root level)**: ```json { "version": 1, @@ -169,14 +174,20 @@ WorkspaceConfig { ```json { "workspaces": { - "personal": { - "path": "/home/user/Documents/Tasks" + "a1b2c3d4-...": { + "name": "personal", + "path": "/home/user/Documents/Tasks", + "mode": "local" }, - "shared": { - "path": "/home/user/Dropbox/TeamTasks" + "e5f6g7h8-...": { + "name": "shared", + "path": "/home/user/Dropbox/TeamTasks", + "mode": "webdav", + "webdav_url": "https://nextcloud.example.com/remote.php/dav/files/user/", + "webdav_path": "TeamTasks" } }, - "current_workspace": "personal" + "current_workspace": "a1b2c3d4-..." } ``` @@ -394,7 +405,7 @@ $ onyx workspace migrate personal ~/Dropbox/Tasks ⚠ This will move all files from ~/Documents/Tasks to ~/Dropbox/Tasks Continue? (y/n): y Moving files... - Moved .metadata.json + Moved .onyx-workspace.json Moved My Tasks/ (15 files) Moved Work/ (8 files) ✓ Migrated 23 files to ~/Dropbox/Tasks @@ -453,11 +464,16 @@ cargo run -p onyx-cli -- workspace list Add WebDAV support to `onyx-core`: ```rust -// Update WorkspaceConfig to include WebDAV +// WorkspaceConfig with WebDAV support (UUID-keyed in AppConfig) WorkspaceConfig { + name: String, path: PathBuf, + mode: WorkspaceMode, // Local or Webdav webdav_url: Option, + webdav_path: Option, // User-selected remote folder last_sync: Option, + theme: Option, + sync_interval_secs: Option, } // AppConfig remains the same (workspaces + current_workspace) @@ -484,8 +500,8 @@ pub fn delete_credentials(domain: &str) -> Result<()>; ``` #### Sync Strategy -- **Trigger**: On app start (if connected), background timer (every 5 min), on modification (debounced) -- **Conflict Resolution**: Last-write-wins with timestamp +- **Trigger**: Auto-sync lifecycle — periodic polling (configurable interval, default 60s), debounced file-change (5s), window-focus (30s stale threshold) +- **Conflict Resolution**: Checksum-based — downloads remote, compares SHA-256. Identical = false conflict (skipped). Different = remote wins, local recovered as duplicate with new UUID and `[RECOVERED FROM CONFLICT]` prefix - **Offline Support**: Queue operations when offline, sync when online #### Authentication @@ -664,20 +680,22 @@ apps/tauri/ #### App Configuration (Phase 3+) -**Update AppConfig** to include UI preferences: +**AppConfig** with UI preferences (theme is per-workspace): ```rust AppConfig { - workspaces: HashMap, // From Phase 1 - current_workspace: Option, - theme: Theme, // NEW: light/dark mode - window_size: Option<(u32, u32)>, // NEW: remember window size - last_opened_list_per_workspace: HashMap, // NEW: per-workspace last view + workspaces: HashMap, // UUID keys + current_workspace: Option, // UUID } WorkspaceConfig { + name: String, // Display name path: PathBuf, - webdav_url: Option, // From Phase 2 + mode: WorkspaceMode, // Local or Webdav + webdav_url: Option, + webdav_path: Option, // User-selected remote folder last_sync: Option, + theme: Option, // Per-workspace theme + sync_interval_secs: Option, // Auto-sync interval } ``` @@ -1011,6 +1029,6 @@ This project is free and open-source software licensed under GPL v3. --- -**Last Updated**: 2026-04-03 -**Document Version**: 4.2 +**Last Updated**: 2026-04-05 +**Document Version**: 4.3 **Status**: Ready to Implement - Milestone-Driven Plan diff --git a/README.md b/README.md index 81b641c..b3e7b8b 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ onyx/ - Subtask hierarchy (parent_id) - WebDAV sync with three-way diff and offline queue - Platform keychain credential storage (feature-gated for Android) +- Checksum-based conflict resolution (remote wins, local recovered as duplicate) ### CLI (`onyx-cli`) - Workspace management (init, add, list, switch, remove, retarget, migrate) @@ -62,7 +63,7 @@ onyx/ - Keyboard shortcuts (Escape priority chain) - WebDAV setup flow with credential auto-population - File watcher (auto-reloads on external changes) -- Sync status indicators and push/pull/full mode selection +- Auto-sync with configurable interval, status indicators - Custom confirmation dialogs - Desktop packaging (Linux: AppImage + .deb; Windows: MSI) @@ -166,9 +167,8 @@ Tasks are stored as markdown files with YAML frontmatter (Obsidian-compatible): --- id: 550e8400-e29b-41d4-a716-446655440000 status: backlog +version: 3 due: 2026-11-15T14:00:00Z -created: 2026-10-26T10:00:00Z -updated: 2026-10-26T12:30:00Z --- Task description and notes go here in **markdown** format. @@ -182,7 +182,7 @@ Task description and notes go here in **markdown** format. ``` ~/Documents/Tasks/ # User-selected folder -├── .metadata.json # Global: list ordering, last opened list +├── .onyx-workspace.json # Workspace metadata: list ordering, detection ├── My Tasks/ # Task list folder │ ├── .listdata.json # List metadata: task order, id, timestamps │ ├── Buy groceries.md # Individual task files diff --git a/apps/tauri/index.html b/apps/tauri/index.html index 72ab8a2..e1ee666 100644 --- a/apps/tauri/index.html +++ b/apps/tauri/index.html @@ -2,7 +2,7 @@ - + Onyx diff --git a/apps/tauri/src-tauri/Cargo.lock b/apps/tauri/src-tauri/Cargo.lock index 01abbb0..22a631f 100644 --- a/apps/tauri/src-tauri/Cargo.lock +++ b/apps/tauri/src-tauri/Cargo.lock @@ -909,6 +909,21 @@ dependencies = [ "new_debug_unreachable", ] +[[package]] +name = "futures" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.32" @@ -916,6 +931,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -970,6 +986,7 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" dependencies = [ + "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -2385,7 +2402,6 @@ version = "0.1.0" dependencies = [ "chrono", "directories", - "keyring", "log", "quick-xml 0.36.2", "reqwest 0.12.28", @@ -2403,6 +2419,7 @@ name = "onyx-tasks" version = "0.1.0" dependencies = [ "chrono", + "futures", "notify", "notify-debouncer-mini", "onyx-core", @@ -2410,6 +2427,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tauri-plugin-credentials", "tauri-plugin-dialog", "tauri-plugin-os", "tokio", @@ -4105,6 +4123,18 @@ dependencies = [ "walkdir", ] +[[package]] +name = "tauri-plugin-credentials" +version = "0.1.0" +dependencies = [ + "keyring", + "log", + "serde", + "serde_json", + "tauri", + "tauri-plugin", +] + [[package]] name = "tauri-plugin-dialog" version = "2.6.0" diff --git a/apps/tauri/src-tauri/Cargo.toml b/apps/tauri/src-tauri/Cargo.toml index 9c474eb..4e21b22 100644 --- a/apps/tauri/src-tauri/Cargo.toml +++ b/apps/tauri/src-tauri/Cargo.toml @@ -24,7 +24,9 @@ tauri-plugin-os = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" onyx-core = { path = "../../../crates/onyx-core", default-features = false } +tauri-plugin-credentials = { path = "../tauri-plugin-credentials", default-features = false } tokio = { version = "1", features = ["full"] } +futures = "0.3" uuid = { version = "1", features = ["serde", "v4"] } chrono = { version = "0.4", features = ["serde"] } notify = { version = "7", optional = true } @@ -34,5 +36,5 @@ notify-debouncer-mini = { version = "0.5", optional = true } [features] default = ["desktop"] -desktop = ["notify", "notify-debouncer-mini", "onyx-core/keyring-storage"] +desktop = ["notify", "notify-debouncer-mini", "tauri-plugin-credentials/desktop"] custom-protocol = ["tauri/custom-protocol"] diff --git a/apps/tauri/src-tauri/src/lib.rs b/apps/tauri/src-tauri/src/lib.rs index 6a43333..7f5d403 100644 --- a/apps/tauri/src-tauri/src/lib.rs +++ b/apps/tauri/src-tauri/src/lib.rs @@ -17,6 +17,7 @@ use onyx_core::{ sync::{self, SyncMode, SyncResult as CoreSyncResult}, webdav, }; +use tauri_plugin_credentials::Credentials; #[cfg(not(target_os = "android"))] /// Active file watcher stored globally so it lives for the app lifetime. @@ -120,12 +121,11 @@ fn add_workspace( state: State<'_, Mutex>, ) -> Result<(), String> { let mut s = lock_state(&state)?; - let ws = WorkspaceConfig::new(PathBuf::from(&path)); - s.config.add_workspace(name.clone(), ws); + let ws = WorkspaceConfig::new(name, PathBuf::from(&path)); + let id = s.config.add_workspace(ws); s.config - .set_current_workspace(name) + .set_current_workspace(id) .map_err(|e| e.to_string())?; - // Reset repo so it reopens on next access s.repo = None; s.config .save_to_file(&s.config_path.clone()) @@ -134,12 +134,12 @@ fn add_workspace( #[tauri::command] fn set_current_workspace( - name: String, + id: String, state: State<'_, Mutex>, ) -> Result<(), String> { let mut s = lock_state(&state)?; s.config - .set_current_workspace(name) + .set_current_workspace(id) .map_err(|e| e.to_string())?; s.repo = None; s.config @@ -149,17 +149,96 @@ fn set_current_workspace( #[tauri::command] fn remove_workspace( - name: String, + id: String, state: State<'_, Mutex>, ) -> Result<(), String> { let mut s = lock_state(&state)?; - s.config.remove_workspace(&name); + s.config.remove_workspace(&id); s.repo = None; s.config .save_to_file(&s.config_path.clone()) .map_err(|e| e.to_string()) } +#[tauri::command] +async fn rename_workspace( + id: String, + new_name: String, + app_handle: tauri::AppHandle, + state: State<'_, Mutex>, +) -> Result<(), String> { + // Extract workspace info while holding the lock briefly + let (mode, old_path, webdav_url, webdav_path) = { + let s = lock_state(&state)?; + let ws = s.config.workspaces.get(&id).ok_or("Workspace not found")?; + ( + ws.mode.clone(), + ws.path.clone(), + ws.webdav_url.clone(), + ws.webdav_path.clone(), + ) + }; + + match mode { + WorkspaceMode::Local => { + // Rename the local folder + let parent = old_path.parent().ok_or("Workspace has no parent directory")?; + let new_path = parent.join(&new_name); + if new_path != old_path { + if new_path.exists() { + return Err(format!("A folder named '{}' already exists at that location", new_name)); + } + std::fs::rename(&old_path, &new_path).map_err(|e| format!("Failed to rename folder: {}", e))?; + } + let mut s = lock_state(&state)?; + s.config.rename_workspace(&id, new_name).map_err(|e| e.to_string())?; + if let Some(ws) = s.config.workspaces.get_mut(&id) { + ws.path = new_path; + } + s.repo = None; + s.config.save_to_file(&s.config_path.clone()).map_err(|e| e.to_string())?; + } + WorkspaceMode::Webdav => { + // Rename the remote folder via WebDAV MOVE + let base_url = webdav_url.as_deref().ok_or("No WebDAV URL configured")?; + let remote_path = webdav_path.as_deref().unwrap_or(""); + + let domain = base_url + .split("://").nth(1) + .and_then(|rest| rest.split('/').next()) + .unwrap_or("").to_string(); + let creds = app_handle.state::>(); + let (username, password) = creds.load(&domain)?; + + let client = webdav::WebDavClient::new(base_url, &username, &password) + .map_err(|e| e.to_string())?; + + // Compute new remote path by replacing the last segment + let new_remote_path = if remote_path.is_empty() || remote_path == "/" { + new_name.clone() + } else if let Some(parent) = remote_path.trim_end_matches('/').rsplit_once('/') { + format!("{}/{}", parent.0, new_name) + } else { + new_name.clone() + }; + + if new_remote_path != remote_path { + client.move_resource(remote_path, &new_remote_path).await.map_err(|e| e.to_string())?; + } + + let mut s = lock_state(&state)?; + s.config.rename_workspace(&id, new_name).map_err(|e| e.to_string())?; + if let Some(ws) = s.config.workspaces.get_mut(&id) { + ws.webdav_path = Some(new_remote_path); + } + s.repo = None; + s.config.save_to_file(&s.config_path.clone()).map_err(|e| e.to_string())?; + } + } + + Ok(()) +} + // ── Workspace init ─────────────────────────────────────────────────── #[tauri::command] @@ -405,12 +484,12 @@ fn get_group_by_due_date( #[tauri::command] fn set_webdav_config( - workspace_name: String, + workspace_id: String, webdav_url: String, state: State<'_, Mutex>, ) -> Result<(), String> { let mut s = lock_state(&state)?; - if let Some(ws) = s.config.workspaces.get_mut(&workspace_name) { + if let Some(ws) = s.config.workspaces.get_mut(&workspace_id) { ws.webdav_url = Some(webdav_url); } s.config @@ -420,36 +499,162 @@ fn set_webdav_config( #[tauri::command] fn set_workspace_theme( - workspace_name: String, + workspace_id: String, theme: Option, state: State<'_, Mutex>, ) -> Result<(), String> { let mut s = lock_state(&state)?; - if let Some(ws) = s.config.workspaces.get_mut(&workspace_name) { + if let Some(ws) = s.config.workspaces.get_mut(&workspace_id) { ws.theme = theme; } s.config.save_to_file(&s.config_path.clone()).map_err(|e| e.to_string()) } #[tauri::command] -fn add_webdav_workspace( - name: String, - webdav_url: String, - username: String, - password: String, +fn set_sync_interval( + workspace_id: String, + interval_secs: Option, state: State<'_, Mutex>, ) -> Result<(), String> { let mut s = lock_state(&state)?; - let managed_dir = s.app_data_dir.join("workspaces").join(&name); + if let Some(ws) = s.config.workspaces.get_mut(&workspace_id) { + ws.sync_interval_secs = interval_secs; + } + s.config.save_to_file(&s.config_path.clone()).map_err(|e| e.to_string()) +} + +/// A remote folder entry returned to the frontend. +#[derive(Debug, Serialize, Deserialize)] +struct RemoteFolderEntry { + name: String, + is_workspace: bool, +} + +/// Summary of a list inside a remote workspace. +#[derive(Debug, Serialize, Deserialize)] +struct RemoteListInfo { + name: String, + task_count: usize, +} + +#[tauri::command] +async fn list_remote_folder( + url: String, + username: String, + password: String, + path: String, +) -> Result, String> { + let client = onyx_core::webdav::WebDavClient::new(&url, &username, &password) + .map_err(|e| e.to_string())?; + let entries = client.list_files(&path).await.map_err(|e| e.to_string())?; + + let dir_entries: Vec<_> = entries.into_iter().filter(|e| e.is_dir).collect(); + + // Check all subfolders for .onyx-workspace.json in parallel + let sub_paths: Vec<_> = dir_entries.iter().map(|entry| { + if path.is_empty() { entry.path.clone() } + else { format!("{}/{}", path.trim_end_matches('/'), entry.path) } + }).collect(); + let checks: Vec<_> = sub_paths.iter().map(|sp| { + client.list_files(sp) + }).collect(); + let results: Vec<_> = futures::future::join_all(checks).await + .into_iter().map(|r| r.unwrap_or_default()).collect(); + + let folders = dir_entries.into_iter().zip(results).map(|(entry, sub_files)| { + let is_workspace = sub_files.iter().any(|f| !f.is_dir && f.path == ".onyx-workspace.json"); + RemoteFolderEntry { name: entry.path, is_workspace } + }).collect(); + + Ok(folders) +} + +#[tauri::command] +async fn inspect_remote_workspace( + url: String, + username: String, + password: String, + path: String, +) -> Result, String> { + let client = onyx_core::webdav::WebDavClient::new(&url, &username, &password) + .map_err(|e| e.to_string())?; + let entries = client.list_files(&path).await.map_err(|e| e.to_string())?; + + let mut lists = Vec::new(); + for entry in entries { + if !entry.is_dir { continue; } + let list_path = if path.is_empty() { + entry.path.clone() + } else { + format!("{}/{}", path.trim_end_matches('/'), entry.path) + }; + let files = client.list_files(&list_path).await.unwrap_or_default(); + let has_listdata = files.iter().any(|f| !f.is_dir && f.path == ".listdata.json"); + if has_listdata { + let task_count = files.iter().filter(|f| !f.is_dir && f.path.ends_with(".md")).count(); + lists.push(RemoteListInfo { + name: entry.path, + task_count, + }); + } + } + + Ok(lists) +} + +#[tauri::command] +async fn create_remote_workspace( + url: String, + username: String, + password: String, + path: String, +) -> Result<(), String> { + let client = onyx_core::webdav::WebDavClient::new(&url, &username, &password) + .map_err(|e| e.to_string())?; + if !path.is_empty() { + client.ensure_dir(&path).await.map_err(|e| e.to_string())?; + } + // Upload an empty .onyx-workspace.json + let metadata = serde_json::json!({ + "version": 1, + "list_order": [], + "last_opened_list": null, + }); + let file_path = if path.is_empty() { + ".onyx-workspace.json".to_string() + } else { + format!("{}/{}", path.trim_end_matches('/'), ".onyx-workspace.json") + }; + client.put_file(&file_path, serde_json::to_string_pretty(&metadata).unwrap().into_bytes()) + .await + .map_err(|e| e.to_string())?; + Ok(()) +} + +#[tauri::command] +fn add_webdav_workspace( + name: String, + webdav_url: String, + webdav_path: String, + username: String, + password: String, + app_handle: tauri::AppHandle, + state: State<'_, Mutex>, +) -> Result<(), String> { + let mut s = lock_state(&state)?; + // Use a UUID-based directory name to avoid filesystem conflicts with duplicate workspace names + let dir_id = uuid::Uuid::new_v4().to_string(); + let managed_dir = s.app_data_dir.join("workspaces").join(&dir_id); std::fs::create_dir_all(&managed_dir).map_err(|e| e.to_string())?; TaskRepository::init(managed_dir.clone()).map(|_| ()).map_err(|e| e.to_string())?; - let mut ws = WorkspaceConfig::new(managed_dir); + let mut ws = WorkspaceConfig::new(name, managed_dir); ws.mode = WorkspaceMode::Webdav; ws.webdav_url = Some(webdav_url.clone()); + ws.webdav_path = Some(webdav_path); - s.config.add_workspace(name.clone(), ws); - s.config.set_current_workspace(name).map_err(|e| e.to_string())?; + let id = s.config.add_workspace(ws); + s.config.set_current_workspace(id).map_err(|e| e.to_string())?; s.repo = None; // Store credentials keyed by hostname @@ -461,7 +666,8 @@ fn add_webdav_workspace( .to_string(); s.config.save_to_file(&s.config_path.clone()).map_err(|e| e.to_string())?; drop(s); - webdav::store_credentials(&domain, &username, &password).map_err(|e| e.to_string())?; + let creds = app_handle.state::>(); + creds.store(&domain, &username, &password)?; Ok(()) } @@ -470,23 +676,19 @@ async fn store_credentials( domain: String, username: String, password: String, + app_handle: tauri::AppHandle, ) -> Result<(), String> { - tokio::task::spawn_blocking(move || { - webdav::store_credentials(&domain, &username, &password).map_err(|e| e.to_string()) - }) - .await - .map_err(|e| e.to_string())? + let creds = app_handle.state::>(); + creds.store(&domain, &username, &password) } #[tauri::command] -async fn load_credentials(domain: String) -> Result<(String, String), String> { - tokio::task::spawn_blocking(move || { - webdav::load_credentials(&domain) - .map(|(u, p)| ((*u).clone(), (*p).clone())) - .map_err(|e| e.to_string()) - }) - .await - .map_err(|e| e.to_string())? +async fn load_credentials( + domain: String, + app_handle: tauri::AppHandle, +) -> Result<(String, String), String> { + let creds = app_handle.state::>(); + creds.load(&domain) } #[tauri::command] @@ -505,16 +707,22 @@ async fn test_webdav_connection( #[tauri::command] async fn sync_workspace( - workspace_name: String, + workspace_id: String, mode: String, + app_handle: tauri::AppHandle, state: State<'_, Mutex>, ) -> Result { - // Step 1: read config + // Step 1: read config — combine base URL with the user-chosen remote path let (workspace_path, webdav_url) = { let s = lock_state(&state)?; - let ws = s.config.workspaces.get(&workspace_name) + let ws = s.config.workspaces.get(&workspace_id) .ok_or("Workspace not found")?; - (ws.path.clone(), ws.webdav_url.clone().ok_or("No WebDAV URL configured")?) + let base = ws.webdav_url.clone().ok_or("No WebDAV URL configured")?; + let full = match &ws.webdav_path { + Some(p) if !p.is_empty() => format!("{}/{}", base.trim_end_matches('/'), p.trim_matches('/')), + _ => base, + }; + (ws.path.clone(), full) }; // Step 2: load credentials @@ -524,13 +732,8 @@ async fn sync_workspace( .and_then(|rest| rest.split('/').next()) .unwrap_or("") .to_string(); - let (username, password) = tokio::task::spawn_blocking(move || { - webdav::load_credentials(&domain) - .map(|(u, p)| ((*u).clone(), (*p).clone())) - .map_err(|e| e.to_string()) - }) - .await - .map_err(|e| e.to_string())??; + let creds = app_handle.state::>(); + let (username, password) = creds.load(&domain)?; let sync_mode = match mode.as_str() { "push" => SyncMode::Push, @@ -550,7 +753,7 @@ async fn sync_workspace( { let mut s = lock_state(&state)?; - if let Some(ws) = s.config.workspaces.get_mut(&workspace_name) { + if let Some(ws) = s.config.workspaces.get_mut(&workspace_id) { ws.last_sync = Some(Utc::now()); } s.config.save_to_file(&s.config_path.clone()).map_err(|e| e.to_string())?; @@ -621,6 +824,7 @@ pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_os::init()) + .plugin(tauri_plugin_credentials::init()) .setup(|app| { // Resolve app data dir and config path let app_data_dir = app.path().app_data_dir() @@ -648,6 +852,7 @@ pub fn run() { add_workspace, set_current_workspace, remove_workspace, + rename_workspace, init_workspace, get_lists, create_list, @@ -664,7 +869,11 @@ pub fn run() { get_group_by_due_date, set_webdav_config, set_workspace_theme, + set_sync_interval, add_webdav_workspace, + list_remote_folder, + inspect_remote_workspace, + create_remote_workspace, store_credentials, load_credentials, test_webdav_connection, diff --git a/apps/tauri/src/App.svelte b/apps/tauri/src/App.svelte index 80d38ec..6ce797f 100644 --- a/apps/tauri/src/App.svelte +++ b/apps/tauri/src/App.svelte @@ -6,6 +6,7 @@ import TasksScreen from "./lib/screens/TasksScreen.svelte"; const isLinux = platform() === "linux"; + const isMobile = platform() === "android" || platform() === "ios"; onMount(() => { app.loadConfig(); @@ -23,13 +24,40 @@ {#if app.error}
{app.error}
{/if} - {#if app.screen === "setup"} + {#if app.initialSync} +
+ + + + +

Syncing workspace…

+
+ {:else if app.screen === "missing"} +
+
+

Workspace Not Found

+

+ The workspace {app.missingWorkspace && app.config?.workspaces[app.missingWorkspace]?.name || "Unknown"} could not be opened. Its folder may have been moved or deleted. +

+

+ It will be removed from your workspace list. You can re-add it if the folder becomes available again. +

+ +
+
+ {:else if app.screen === "setup"} {:else} diff --git a/apps/tauri/src/app.css b/apps/tauri/src/app.css index 99c17b1..ceb6434 100644 --- a/apps/tauri/src/app.css +++ b/apps/tauri/src/app.css @@ -36,6 +36,12 @@ body { background: transparent; } +/* Safe area CSS variable — content elements opt into this, overlays don't */ +:root { + --safe-top: env(safe-area-inset-top); + --safe-bottom: env(safe-area-inset-bottom); +} + .linux-window-border { border: 1px solid rgba(0, 0, 0, 0.15); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25), 0 0 2px rgba(0, 0, 0, 0.1); diff --git a/apps/tauri/src/lib/components/NewTaskInput.svelte b/apps/tauri/src/lib/components/NewTaskInput.svelte index 9b0180e..ede23cb 100644 --- a/apps/tauri/src/lib/components/NewTaskInput.svelte +++ b/apps/tauri/src/lib/components/NewTaskInput.svelte @@ -18,7 +18,7 @@ if (!title.trim()) return; const created = await app.createTask(title.trim(), description.trim() || undefined); if (dueDate && created) { - await app.updateTask({ ...created, due_date: dueDate, has_time: dueDateHasTime, updated_at: new Date().toISOString() }); + await app.updateTask({ ...created, due_date: dueDate, has_time: dueDateHasTime }); } title = ""; description = ""; diff --git a/apps/tauri/src/lib/components/TaskDetailView.svelte b/apps/tauri/src/lib/components/TaskDetailView.svelte index e54e763..dcb6f00 100644 --- a/apps/tauri/src/lib/components/TaskDetailView.svelte +++ b/apps/tauri/src/lib/components/TaskDetailView.svelte @@ -35,7 +35,7 @@ function debouncedSave(fields: Partial) { clearTimeout(saveTimer); saveTimer = setTimeout(() => { - app.updateTask({ ...task, ...fields, updated_at: new Date().toISOString() }); + app.updateTask({ ...task, ...fields }); }, 400); } @@ -48,7 +48,7 @@ } function handleDateChange(iso: string | null, hasTime: boolean = false) { - app.updateTask({ ...task, due_date: iso, has_time: hasTime, updated_at: new Date().toISOString() }); + app.updateTask({ ...task, due_date: iso, has_time: hasTime }); } async function handleToggle() { diff --git a/apps/tauri/src/lib/screens/SettingsScreen.svelte b/apps/tauri/src/lib/screens/SettingsScreen.svelte index 0257973..17f4958 100644 --- a/apps/tauri/src/lib/screens/SettingsScreen.svelte +++ b/apps/tauri/src/lib/screens/SettingsScreen.svelte @@ -1,10 +1,11 @@ + +
-

{workspaceName} Settings

+

Workspace Settings

+ +
+
+ {#if renaming} + { if (e.key === "Enter") handleRename(); if (e.key === "Escape") { renaming = false; } }} + onblur={handleRename} + autofocus + /> + {:else} +

{ws?.name}

+ {/if} +
+
+ + {#if showKebab} +
+ + +
+ {/if} +
+
+
{#if isWebdav} @@ -120,65 +202,55 @@ -
+
+ -
- {#if app.error} -

{app.error}

- {/if} - {#if ws?.last_sync} - {@const lastSync = new Date(ws.last_sync)} - {@const secsAgo = Math.floor((Date.now() - lastSync.getTime()) / 1000)} - {@const relTime = secsAgo < 60 ? "just now" : secsAgo < 3600 ? `${Math.floor(secsAgo / 60)}m ago` : `${Math.floor(secsAgo / 3600)}h ago`} -

- Last sync: {relTime} - {#if app.lastSyncResult} -  · ↑{app.lastSyncResult.uploaded} ↓{app.lastSyncResult.downloaded} - {/if} -

- {/if} {/if}
-

- Appearance -

-
- - -
+ +

Tauri v2 + Svelte

+ +{#if confirmRename} + confirmRename = false} + /> +{/if} diff --git a/apps/tauri/src/lib/screens/SetupScreen.svelte b/apps/tauri/src/lib/screens/SetupScreen.svelte index 3d29cbd..0f15b03 100644 --- a/apps/tauri/src/lib/screens/SetupScreen.svelte +++ b/apps/tauri/src/lib/screens/SetupScreen.svelte @@ -1,5 +1,6 @@
+
@@ -214,27 +358,17 @@ {/if} - {:else} - + {:else if webdavStep === "connect"} +

- Connect to a WebDAV server for cloud-synced tasks. + Connect to a WebDAV server.

- - @@ -252,22 +386,16 @@ class="mb-4 w-full rounded-lg border border-border-light bg-transparent px-3 py-2 text-sm outline-none focus:border-primary dark:border-border-dark" /> -
- -
+ {#if testStatus === "fail"} +

Connection failed. Check your URL and credentials.

+ {/if} {#if !isMobile} @@ -278,6 +406,148 @@ Back {/if} + + {:else if webdavStep === "browse"} + +

+ Pick a folder or create a new workspace. +

+ + +
+ {#if browsePath.length > 0} + + {/if} + /{currentBrowsePath} +
+ + +
+ {#if browseLoading} +
Loading...
+ {:else if browseError} +
{browseError}
+ {:else if browseEntries.length === 0} +
No folders found.
+ {:else} + {#each browseEntries as entry} + + {/each} + {/if} +
+ + + + + + {:else if webdavStep === "preview"} + +
+ +

{previewName}

+
+ + {#if previewLoading} +
Loading workspace...
+ {:else if previewLists.length === 0} +

No lists in this workspace yet.

+ {:else} +
+ {#each previewLists as list} +
+ {list.name} + + {list.task_count} {list.task_count === 1 ? "task" : "tasks"} + +
+ {/each} +
+ {/if} + + + + {:else if webdavStep === "create"} + +
+ +

New Workspace

+
+ +

+ Creating in: /{currentBrowsePath} +

+ + + + {#if browseError} +

{browseError}

+ {/if} + + {/if}
diff --git a/apps/tauri/src/lib/screens/TasksScreen.svelte b/apps/tauri/src/lib/screens/TasksScreen.svelte index a21a39a..2f68d3b 100644 --- a/apps/tauri/src/lib/screens/TasksScreen.svelte +++ b/apps/tauri/src/lib/screens/TasksScreen.svelte @@ -45,14 +45,11 @@ showWorkspacePicker = false; if (showListMenu && listMenuEl && !listMenuEl.contains(e.target as Node)) showListMenu = false; - const target = e.target as HTMLElement; - if (wsMenuName && !target.closest("[data-ws-menu]")) wsMenuName = null; } let newListName = $state(""); let showCompleted = $state(false); let completedVisible = $state(false); - let wsMenuName = $state(null); let renamingListId = $state(null); let renameValue = $state(""); let showListMenu = $state(false); @@ -137,7 +134,6 @@ if (taskStack.length > 0) { closeDetail(); return; } if (showListMenu) { showListMenu = false; return; } if (showDrawer) { closeDrawer(); return; } - if (wsMenuName) { wsMenuName = null; return; } if (showWorkspacePicker) { showWorkspacePicker = false; return; } } @@ -204,7 +200,7 @@ if (isDesktop) appWindow.startDragging(); } - let workspaceNames = $derived(app.config ? Object.keys(app.config.workspaces).sort((a, b) => a.localeCompare(b)) : []); + let workspaceIds = $derived(app.config ? Object.keys(app.config.workspaces).sort((a, b) => (app.config!.workspaces[a].name).localeCompare(app.config!.workspaces[b].name)) : []); let translateX = $derived(showDrawer ? '0' : '-80cqi'); @@ -219,6 +215,7 @@ >
+
(showWorkspacePicker = !showWorkspacePicker)} class="flex items-center gap-1.5 rounded-lg px-2 py-1 text-sm font-semibold hover:bg-black/5 dark:hover:bg-white/10" > - {app.config?.current_workspace ?? "Workspace"} + {app.config?.current_workspace ? app.config.workspaces[app.config.current_workspace]?.name ?? "Workspace" : "Workspace"} @@ -240,55 +237,31 @@
- {#each workspaceNames as name} - {@const ws = app.config?.workspaces[name]} + {#each workspaceIds as wsId} + {@const ws = app.config?.workspaces[wsId]}
-
- - {#if wsMenuName === name} -
- - -
- {/if} -
+
{/each}
@@ -354,6 +327,34 @@
+ +
+ {#if app.isWebdav} +
+ + + + {app.syncing ? "Syncing..." : app.syncStatus === "synced" || app.syncStatus === "idle" ? "Synced" : app.syncStatus === "error" ? "Sync error" : "Offline"}{#if !app.syncing && app.lastSyncResult && (app.lastSyncResult.uploaded > 0 || app.lastSyncResult.downloaded > 0)}  {#if app.lastSyncResult.uploaded > 0}↑{app.lastSyncResult.uploaded}{/if}{#if app.lastSyncResult.uploaded > 0 && app.lastSyncResult.downloaded > 0} {/if}{#if app.lastSyncResult.downloaded > 0}↓{app.lastSyncResult.downloaded}{/if}{/if} + + + +
+ {:else} + Local workspace + {/if} +
+
@@ -374,6 +375,7 @@ >
+
+ {#key app.activeListId} {#if app.lists.length === 0}

No lists yet

@@ -558,11 +561,13 @@ {/if} {/if} {/if} + {/key}
@@ -612,7 +610,7 @@
- + { closeSettings(); confirmRemoveWorkspace = id; }} />
@@ -648,11 +646,11 @@ {#if confirmRemoveWorkspace} { const name = confirmRemoveWorkspace; confirmRemoveWorkspace = null; if (name) app.removeWorkspace(name); }} + onconfirm={() => { const id = confirmRemoveWorkspace; confirmRemoveWorkspace = null; if (id) app.removeWorkspace(id); }} oncancel={() => (confirmRemoveWorkspace = null)} /> {/if} diff --git a/apps/tauri/src/lib/stores/app.svelte.ts b/apps/tauri/src/lib/stores/app.svelte.ts index a76aeb5..a52071f 100644 --- a/apps/tauri/src/lib/stores/app.svelte.ts +++ b/apps/tauri/src/lib/stores/app.svelte.ts @@ -1,5 +1,6 @@ import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; +import { getCurrentWindow } from "@tauri-apps/api/window"; import type { AppConfig, Task, @@ -11,6 +12,8 @@ import type { // Listen for file system changes from the backend watcher. listen("fs-changed", () => { loadLists(); + // Debounced sync for WebDAV workspaces on local file changes + if (isWebdav) debouncedSync(); }); // ── Reactive state ─────────────────────────────────────────────────── @@ -22,9 +25,18 @@ let activeListId = $state(null); let tasks = $state([]); let osDark = globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ?? false; let syncing = $state(false); -let syncMode = $state<"full" | "push" | "pull">("full"); +let initialSync = $state(false); +let syncStatus = $state<"idle" | "synced" | "error" | "offline">("idle"); let lastSyncResult = $state(null); let error = $state(null); +let missingWorkspace = $state(null); +let lastSyncTime = 0; +let _syncInterval: ReturnType | null = null; +let _syncDebounce: ReturnType | null = null; +let _focusUnlisten: (() => void) | null = null; +const DEFAULT_SYNC_INTERVAL_SECS = 60; +const SYNC_DEBOUNCE_MS = 5_000; +const SYNC_FOCUS_THRESHOLD_MS = 30_000; // ── Derived ────────────────────────────────────────────────────────── @@ -63,6 +75,16 @@ let currentTheme = $derived( let isDark = $derived( currentTheme ? DARK_THEMES.has(currentTheme) : osDark, ); +let isWebdav = $derived( + config?.current_workspace + ? config.workspaces[config.current_workspace]?.mode === "webdav" + : false, +); +let syncIntervalSecs = $derived( + config?.current_workspace + ? config.workspaces[config.current_workspace]?.sync_interval_secs ?? DEFAULT_SYNC_INTERVAL_SECS + : DEFAULT_SYNC_INTERVAL_SECS, +); // ── Actions ────────────────────────────────────────────────────────── @@ -70,8 +92,19 @@ async function loadConfig() { try { config = await invoke("get_config"); if (hasWorkspace) { + // Try loading lists — if the workspace path is gone, get_lists will fail + lists = []; + try { + lists = await invoke("get_lists"); + } catch { + missingWorkspace = config!.current_workspace; + screen = "missing"; + return; + } + if (lists.length > 0 && !activeListId) activeListId = lists[0].id; + if (activeListId) await loadTasks(); screen = "tasks"; - await loadLists(); + if (isWebdav) startAutoSync(); } else { screen = "setup"; } @@ -95,23 +128,35 @@ async function addWorkspace(name: string, path: string) { } } -async function switchWorkspace(name: string) { +async function switchWorkspace(id: string) { try { - await invoke("set_current_workspace", { name }); + await invoke("set_current_workspace", { id }); config = await invoke("get_config"); activeListId = null; await loadLists(); - const ws = config?.workspaces[name]; + const ws = config?.workspaces[id]; if (ws) invoke("watch_workspace", { path: ws.path }).catch((e) => console.warn("File watcher failed:", e)); + if (isWebdav) startAutoSync(); else stopAutoSync(); error = null; } catch (e) { error = String(e); } } -async function removeWorkspace(name: string) { +async function renameWorkspace(id: string, newName: string) { try { - await invoke("remove_workspace", { name }); + await invoke("rename_workspace", { id, newName }); + config = await invoke("get_config"); + error = null; + } catch (e) { + error = String(e); + } +} + +async function removeWorkspace(id: string) { + stopAutoSync(); + try { + await invoke("remove_workspace", { id }); config = await invoke("get_config"); if (!hasWorkspace) { screen = "setup"; @@ -139,7 +184,14 @@ async function loadLists() { async function loadTasks() { if (!activeListId) return; try { - tasks = await invoke("list_tasks", { listId: activeListId }); + const loaded = await invoke("list_tasks", { listId: activeListId }); + // Deduplicate by task ID — sync conflicts can produce files with the same UUID + const seen = new Set(); + tasks = loaded.filter((t) => { + if (seen.has(t.id)) return false; + seen.add(t.id); + return true; + }); } catch (e) { error = String(e); } @@ -147,6 +199,7 @@ async function loadTasks() { async function selectList(id: string) { activeListId = id; + tasks = []; await loadTasks(); } @@ -281,36 +334,69 @@ async function setGroupByDueDate(listId: string, enabled: boolean) { } async function triggerSync() { - if (!config?.current_workspace) return; + if (!config?.current_workspace || syncing) return; syncing = true; - error = null; try { const result = await invoke("sync_workspace", { - workspaceName: config.current_workspace, - mode: syncMode, + workspaceId: config.current_workspace, + mode: "full", }); lastSyncResult = result; - if (result.errors.length > 0) { - error = result.errors.join("; "); - } + lastSyncTime = Date.now(); + syncStatus = result.errors.length > 0 ? "error" : "synced"; + if (result.errors.length > 0) error = result.errors.join("; "); config = await invoke("get_config"); await loadLists(); } catch (e) { - error = String(e); + const msg = String(e); + const isTransient = /timeout|connect|network|unreachable|refused/i.test(msg); + syncStatus = isTransient ? "offline" : "error"; + // Only show the error banner for non-transient failures; connectivity issues just update the status dot + if (!isTransient) error = msg; } finally { syncing = false; } } -function setSyncMode(mode: "full" | "push" | "pull") { - syncMode = mode; +function debouncedSync() { + if (_syncDebounce) clearTimeout(_syncDebounce); + _syncDebounce = setTimeout(() => { _syncDebounce = null; triggerSync(); }, SYNC_DEBOUNCE_MS); +} + +function startAutoSync() { + stopAutoSync(); + triggerSync(); + _syncInterval = setInterval(triggerSync, syncIntervalSecs * 1000); + getCurrentWindow().onFocusChanged(({ payload: focused }) => { + if (focused && Date.now() - lastSyncTime > SYNC_FOCUS_THRESHOLD_MS) triggerSync(); + }).then((unlisten) => { _focusUnlisten = unlisten; }); +} + +function stopAutoSync() { + if (_syncInterval) { clearInterval(_syncInterval); _syncInterval = null; } + if (_syncDebounce) { clearTimeout(_syncDebounce); _syncDebounce = null; } + if (_focusUnlisten) { _focusUnlisten(); _focusUnlisten = null; } +} + +async function setSyncInterval(secs: number | null) { + if (!config?.current_workspace) return; + try { + await invoke("set_sync_interval", { + workspaceId: config.current_workspace, + intervalSecs: secs, + }); + config = await invoke("get_config"); + if (isWebdav) startAutoSync(); + } catch (e) { + error = String(e); + } } async function setTheme(theme: string | null) { if (!config?.current_workspace) return; try { await invoke("set_workspace_theme", { - workspaceName: config.current_workspace, + workspaceId: config.current_workspace, theme, }); config = await invoke("get_config"); @@ -319,20 +405,51 @@ async function setTheme(theme: string | null) { } } -async function addWebdavWorkspace(name: string, webdavUrl: string, username: string, password: string) { +async function addWebdavWorkspace(name: string, webdavUrl: string, webdavPath: string, username: string, password: string) { try { - await invoke("add_webdav_workspace", { name, webdavUrl, username, password }); + await invoke("add_webdav_workspace", { name, webdavUrl, webdavPath, username, password }); config = await invoke("get_config"); - await loadLists(); - const ws = config?.workspaces[name]; - if (ws) invoke("watch_workspace", { path: ws.path }).catch((e) => console.warn("File watcher failed:", e)); screen = "tasks"; error = null; + // Run initial sync before showing content so the workspace isn't empty + initialSync = true; + try { + await triggerSync(); + } finally { + initialSync = false; + } + await loadLists(); + if (config?.current_workspace) { + const ws = config.workspaces[config.current_workspace]; + if (ws) invoke("watch_workspace", { path: ws.path }).catch((e) => console.warn("File watcher failed:", e)); + } + if (isWebdav) startAutoSync(); } catch (e) { + initialSync = false; error = String(e); } } +async function forgetMissingWorkspace() { + if (!missingWorkspace) return; + await removeWorkspace(missingWorkspace); + missingWorkspace = null; + config = await invoke("get_config"); + if (hasWorkspace) { + // Switch to the next available workspace + const nextName = Object.keys(config!.workspaces)[0]; + if (nextName) { + await switchWorkspace(nextName); + screen = "tasks"; + return; + } + } + screen = "setup"; + lists = []; + tasks = []; + activeListId = null; +} + function setScreen(s: Screen) { screen = s; } @@ -377,8 +494,17 @@ export const app = { get syncing() { return syncing; }, - get syncMode() { - return syncMode; + get initialSync() { + return initialSync; + }, + get syncStatus() { + return syncStatus; + }, + get isWebdav() { + return isWebdav; + }, + get syncIntervalSecs() { + return syncIntervalSecs; }, get lastSyncResult() { return lastSyncResult; @@ -389,10 +515,14 @@ export const app = { get hasWorkspace() { return hasWorkspace; }, + get missingWorkspace() { + return missingWorkspace; + }, getSubtasks, loadConfig, addWorkspace, switchWorkspace, + renameWorkspace, removeWorkspace, loadLists, loadTasks, @@ -408,9 +538,12 @@ export const app = { renameList, setGroupByDueDate, triggerSync, - setSyncMode, + startAutoSync, + stopAutoSync, + setSyncInterval, setTheme, addWebdavWorkspace, + forgetMissingWorkspace, setScreen, clearError, }; diff --git a/apps/tauri/src/lib/types.ts b/apps/tauri/src/lib/types.ts index a24855c..ca603fe 100644 --- a/apps/tauri/src/lib/types.ts +++ b/apps/tauri/src/lib/types.ts @@ -5,8 +5,7 @@ export interface Task { status: "backlog" | "completed"; due_date: string | null; has_time: boolean; - created_at: string; - updated_at: string; + version: number; parent_id: string | null; } @@ -22,11 +21,14 @@ export interface TaskList { export type WorkspaceMode = "local" | "webdav"; export interface WorkspaceConfig { + name: string; path: string; mode: WorkspaceMode; webdav_url: string | null; + webdav_path: string | null; last_sync: string | null; theme: string | null; + sync_interval_secs: number | null; } export interface AppConfig { @@ -43,4 +45,4 @@ export interface SyncResult { errors: string[]; } -export type Screen = "setup" | "tasks" | "settings"; +export type Screen = "setup" | "tasks" | "settings" | "missing"; diff --git a/apps/tauri/tauri-plugin-credentials/Cargo.toml b/apps/tauri/tauri-plugin-credentials/Cargo.toml new file mode 100644 index 0000000..1079648 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "tauri-plugin-credentials" +version = "0.1.0" +edition = "2021" +links = "tauri-plugin-credentials" + +[build-dependencies] +tauri-plugin = { version = "2", features = ["build"] } + +[dependencies] +tauri = "2" +serde = { version = "1", features = ["derive"] } +serde_json = "1" +log = "0.4" +keyring = { version = "3", features = ["apple-native", "windows-native", "sync-secret-service"], optional = true } + +[features] +default = ["desktop"] +desktop = ["keyring"] diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/.gitignore b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/build.gradle.kts b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/build.gradle.kts new file mode 100644 index 0000000..f997910 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/build.gradle.kts @@ -0,0 +1,47 @@ +plugins { + id("com.android.library") + id("org.jetbrains.kotlin.android") +} + +android { + namespace = "app.tauri" + compileSdk = 36 + + defaultConfig { + minSdk = 21 + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles("proguard-rules.pro") + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = "1.8" + } + buildFeatures { + buildConfig = true + } +} + +dependencies { + + implementation("androidx.core:core-ktx:1.7.0") + implementation("androidx.appcompat:appcompat:1.6.0") + implementation("com.google.android.material:material:1.7.0") + implementation("com.fasterxml.jackson.core:jackson-databind:2.15.3") + testImplementation("junit:junit:4.13.2") + androidTestImplementation("androidx.test.ext:junit:1.1.5") + androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/proguard-rules.pro b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/proguard-rules.pro new file mode 100644 index 0000000..e21b5be --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/proguard-rules.pro @@ -0,0 +1,41 @@ +-keep class app.tauri.** { + @app.tauri.JniMethod public ; + native ; +} + +-keep class app.tauri.plugin.JSArray { + public (...); +} + +-keepclassmembers class org.json.JSONArray { + public put(...); +} + +-keep class app.tauri.plugin.JSObject { + public (...); + public put(...); +} + +-keep @app.tauri.annotation.TauriPlugin public class * { + @app.tauri.annotation.Command public ; + @app.tauri.annotation.PermissionCallback ; + @app.tauri.annotation.ActivityCallback ; + @app.tauri.annotation.Permission ; + public (...); +} + +-keep @app.tauri.annotation.InvokeArg public class * { + *; +} + +-keep @com.fasterxml.jackson.databind.annotation.JsonDeserialize public class * { + *; +} + +-keep @com.fasterxml.jackson.databind.annotation.JsonSerialize public class * { + *; +} + +-keep class * extends com.fasterxml.jackson.databind.JsonDeserializer { *; } + +-keep class * extends com.fasterxml.jackson.databind.JsonSerializer { *; } diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/androidTest/java/app/tauri/ExampleInstrumentedTest.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/androidTest/java/app/tauri/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..77ce904 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/androidTest/java/app/tauri/ExampleInstrumentedTest.kt @@ -0,0 +1,28 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.ext.junit.runners.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("app.tauri.test", appContext.packageName) + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/AndroidManifest.xml b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/AndroidManifest.xml new file mode 100644 index 0000000..9a40236 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + + diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/AppPlugin.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/AppPlugin.kt new file mode 100644 index 0000000..0c2dc71 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/AppPlugin.kt @@ -0,0 +1,54 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +import android.app.Activity +import android.webkit.WebView +import androidx.activity.OnBackPressedCallback +import androidx.appcompat.app.AppCompatActivity +import app.tauri.annotation.Command +import app.tauri.annotation.TauriPlugin +import app.tauri.plugin.Plugin +import app.tauri.plugin.Invoke +import app.tauri.plugin.JSObject + +@TauriPlugin +class AppPlugin(private val activity: Activity): Plugin(activity) { + private val BACK_BUTTON_EVENT = "back-button" + + private var webView: WebView? = null + + override fun load(webView: WebView) { + this.webView = webView + } + + init { + val callback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (!hasListener(BACK_BUTTON_EVENT)) { + if (this@AppPlugin.webView?.canGoBack() == true) { + this@AppPlugin.webView!!.goBack() + } else { + this.isEnabled = false + this@AppPlugin.activity.onBackPressed() + this.isEnabled = true + } + } else { + val data = JSObject().apply { + put("canGoBack", this@AppPlugin.webView?.canGoBack() ?: false) + } + trigger(BACK_BUTTON_EVENT, data) + } + } + } + (activity as AppCompatActivity).onBackPressedDispatcher.addCallback(activity, callback) + } + + @Command + fun exit(invoke: Invoke) { + invoke.resolve() + activity.finish() + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/FsUtils.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/FsUtils.kt new file mode 100644 index 0000000..235775c --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/FsUtils.kt @@ -0,0 +1,208 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +import android.content.ContentUris +import android.content.Context +import android.content.res.AssetManager +import android.database.Cursor +import android.net.Uri +import android.os.Environment +import android.provider.DocumentsContract +import android.provider.MediaStore +import android.provider.OpenableColumns +import java.io.File +import java.io.FileOutputStream +import kotlin.math.min + +internal class FsUtils { + companion object { + fun readAsset(assetManager: AssetManager, fileName: String): String { + assetManager.open(fileName).bufferedReader().use { + return it.readText() + } + } + + fun getFileUrlForUri(context: Context, uri: Uri): String? { + // DocumentProvider + if (DocumentsContract.isDocumentUri(context, uri)) { + // ExternalStorageProvider + if (isExternalStorageDocument(uri)) { + val docId: String = DocumentsContract.getDocumentId(uri) + val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() } + .toTypedArray() + val type = split[0] + if ("primary".equals(type, ignoreCase = true)) { + return legacyPrimaryPath(split[1]) + } else { + val splitIndex = docId.indexOf(':', 1) + val tag = docId.substring(0, splitIndex) + val path = docId.substring(splitIndex + 1) + val nonPrimaryVolume = getPathToNonPrimaryVolume(context, tag) + if (nonPrimaryVolume != null) { + val result = "$nonPrimaryVolume/$path" + val file = File(result) + return if (file.exists() && file.canRead()) { + result + } else null + } + } + } else if (isDownloadsDocument(uri)) { + val id: String = DocumentsContract.getDocumentId(uri) + val contentUri: Uri = ContentUris.withAppendedId( + Uri.parse("content://downloads/public_downloads"), + java.lang.Long.valueOf(id) + ) + return getDataColumn(context, contentUri, null, null) + } else if (isMediaDocument(uri)) { + val docId: String = DocumentsContract.getDocumentId(uri) + val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() } + .toTypedArray() + val type = split[0] + var contentUri: Uri? = null + when (type) { + "image" -> { + contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI + } + "video" -> { + contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI + } + "audio" -> { + contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI + } + } + val selection = "_id=?" + val selectionArgs = arrayOf(split[1]) + if (contentUri != null) { + return getDataColumn(context, contentUri, selection, selectionArgs) + } + } + } else if ("content".equals(uri.scheme, ignoreCase = true)) { + // Return the remote address + return if (isGooglePhotosUri(uri)) uri.lastPathSegment else getDataColumn( + context, + uri, + null, + null + ) + } else if ("file".equals(uri.scheme, ignoreCase = true)) { + return uri.path + } + return null + } + + /** + * Get the value of the data column for this Uri. This is useful for + * MediaStore Uris, and other file-based ContentProviders. + * + * @param context The context. + * @param uri The Uri to query. + * @param selection (Optional) Filter used in the query. + * @param selectionArgs (Optional) Selection arguments used in the query. + * @return The value of the _data column, which is typically a file path. + */ + private fun getDataColumn( + context: Context, + uri: Uri, + selection: String?, + selectionArgs: Array? + ): String? { + var path: String? = null + var cursor: Cursor? = null + val column = "_data" + val projection = arrayOf(column) + try { + cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null) + if (cursor != null && cursor.moveToFirst()) { + val index = cursor.getColumnIndexOrThrow(column) + path = cursor.getString(index) + } + } catch (ex: IllegalArgumentException) { + return getCopyFilePath(uri, context) + } finally { + cursor?.close() + } + return path ?: getCopyFilePath(uri, context) + } + + private fun getCopyFilePath(uri: Uri, context: Context): String? { + val cursor = context.contentResolver.query(uri, null, null, null, null)!! + val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) + cursor.moveToFirst() + val name = cursor.getString(nameIndex) + val file = File(context.filesDir, name) + try { + val inputStream = context.contentResolver.openInputStream(uri) + val outputStream = FileOutputStream(file) + var read: Int + val maxBufferSize = 1024 * 1024 + val bufferSize = min(inputStream!!.available(), maxBufferSize) + val buffers = ByteArray(bufferSize) + while (inputStream.read(buffers).also { read = it } != -1) { + outputStream.write(buffers, 0, read) + } + inputStream.close() + outputStream.close() + } catch (e: Exception) { + return null + } finally { + cursor.close() + } + return file.path + } + + private fun legacyPrimaryPath(pathPart: String): String { + return Environment.getExternalStorageDirectory().toString() + "/" + pathPart + } + + /** + * @param uri The Uri to check. + * @return Whether the Uri authority is ExternalStorageProvider. + */ + private fun isExternalStorageDocument(uri: Uri): Boolean { + return "com.android.externalstorage.documents" == uri.authority + } + + /** + * @param uri The Uri to check. + * @return Whether the Uri authority is DownloadsProvider. + */ + private fun isDownloadsDocument(uri: Uri): Boolean { + return "com.android.providers.downloads.documents" == uri.authority + } + + /** + * @param uri The Uri to check. + * @return Whether the Uri authority is MediaProvider. + */ + private fun isMediaDocument(uri: Uri): Boolean { + return "com.android.providers.media.documents" == uri.authority + } + + /** + * @param uri The Uri to check. + * @return Whether the Uri authority is Google Photos. + */ + private fun isGooglePhotosUri(uri: Uri): Boolean { + return "com.google.android.apps.photos.content" == uri.authority + } + + private fun getPathToNonPrimaryVolume(context: Context, tag: String): String? { + val volumes = context.externalCacheDirs + if (volumes != null) { + for (volume in volumes) { + if (volume != null) { + val path = volume.absolutePath + val index = path.indexOf(tag) + if (index != -1) { + return path.substring(0, index) + tag + } + } + } + } + return null + } + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/JniMethod.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/JniMethod.kt new file mode 100644 index 0000000..41301d4 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/JniMethod.kt @@ -0,0 +1,8 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +@Retention(AnnotationRetention.RUNTIME) +internal annotation class JniMethod diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/Logger.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/Logger.kt new file mode 100644 index 0000000..c047378 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/Logger.kt @@ -0,0 +1,85 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +// taken from https://github.com/ionic-team/capacitor/blob/6658bca41e78239347e458175b14ca8bd5c1d6e8/android/capacitor/src/main/java/com/getcapacitor/Logger.java + +import android.text.TextUtils; +import android.util.Log; + +class Logger { + companion object { + private const val LOG_TAG_CORE = "Tauri" + + fun tags(vararg subtags: String): String { + return if (subtags.isNotEmpty()) { + LOG_TAG_CORE + "/" + TextUtils.join("/", subtags) + } else LOG_TAG_CORE + } + + fun verbose(message: String) { + verbose(LOG_TAG_CORE, message) + } + + fun verbose(tag: String, message: String) { + if (!shouldLog()) { + return + } + Log.v(tag, message) + } + + fun debug(message: String) { + debug(LOG_TAG_CORE, message) + } + + fun debug(tag: String, message: String) { + if (!shouldLog()) { + return + } + Log.d(tag, message) + } + + fun info(message: String) { + info(LOG_TAG_CORE, message) + } + + fun info(tag: String, message: String) { + if (!shouldLog()) { + return + } + Log.i(tag, message) + } + + fun warn(message: String) { + warn(LOG_TAG_CORE, message) + } + + fun warn(tag: String, message: String) { + if (!shouldLog()) { + return + } + Log.w(tag, message) + } + + fun error(message: String) { + error(LOG_TAG_CORE, message, null) + } + + fun error(message: String, e: Throwable?) { + error(LOG_TAG_CORE, message, e) + } + + fun error(tag: String, message: String, e: Throwable?) { + if (!shouldLog()) { + return + } + Log.e(tag, message, e) + } + + private fun shouldLog(): Boolean { + return BuildConfig.DEBUG + } + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PathPlugin.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PathPlugin.kt new file mode 100644 index 0000000..1ce1c2f --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PathPlugin.kt @@ -0,0 +1,132 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +import android.app.Activity +import android.database.Cursor +import android.net.Uri +import android.os.Build +import android.os.Environment +import android.provider.OpenableColumns +import app.tauri.annotation.Command +import app.tauri.annotation.InvokeArg +import app.tauri.annotation.TauriPlugin +import app.tauri.plugin.Plugin +import app.tauri.plugin.Invoke +import app.tauri.plugin.JSObject + +const val TAURI_ASSETS_DIRECTORY_URI = "asset://localhost/" + +@InvokeArg +class GetFileNameFromUriArgs { + lateinit var uri: String +} + +@TauriPlugin +class PathPlugin(private val activity: Activity): Plugin(activity) { + private fun resolvePath(invoke: Invoke, path: String?) { + val obj = JSObject() + obj.put("path", path) + invoke.resolve(obj) + } + + @Command + fun getFileNameFromUri(invoke: Invoke) { + val args = invoke.parseArgs(GetFileNameFromUriArgs::class.java) + val name = getRealNameFromURI(activity, Uri.parse(args.uri)) + val res = JSObject() + res.put("name", name) + invoke.resolve(res) + } + + @Command + fun getAudioDir(invoke: Invoke) { + resolvePath(invoke, activity.getExternalFilesDir(Environment.DIRECTORY_MUSIC)?.absolutePath) + } + + @Command + fun getExternalCacheDir(invoke: Invoke) { + resolvePath(invoke, activity.externalCacheDir?.absolutePath) + } + + @Command + fun getConfigDir(invoke: Invoke) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + resolvePath(invoke, activity.dataDir.absolutePath) + } else { + resolvePath(invoke, activity.applicationInfo.dataDir) + } + } + + @Command + fun getDataDir(invoke: Invoke) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + resolvePath(invoke, activity.dataDir.absolutePath) + } else { + resolvePath(invoke, activity.applicationInfo.dataDir) + } + } + + @Command + fun getDocumentDir(invoke: Invoke) { + resolvePath(invoke, activity.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)?.absolutePath) + } + + @Command + fun getDownloadDir(invoke: Invoke) { + resolvePath(invoke, activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)?.absolutePath) + } + + @Command + fun getPictureDir(invoke: Invoke) { + resolvePath(invoke, activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.absolutePath) + } + + @Command + fun getPublicDir(invoke: Invoke) { + resolvePath(invoke, activity.getExternalFilesDir(Environment.DIRECTORY_DCIM)?.absolutePath) + } + + @Command + fun getVideoDir(invoke: Invoke) { + resolvePath(invoke, activity.externalCacheDir?.absolutePath) + } + + @Command + fun getResourcesDir(invoke: Invoke) { + resolvePath(invoke, TAURI_ASSETS_DIRECTORY_URI) + } + + @Command + fun getCacheDir(invoke: Invoke) { + resolvePath(invoke, activity.cacheDir.absolutePath) + } + + @Command + fun getHomeDir(invoke: Invoke) { + resolvePath(invoke, Environment.getExternalStorageDirectory().absolutePath) + } +} + +fun getRealNameFromURI(activity: Activity, contentUri: Uri): String? { + var cursor: Cursor? = null + try { + val projection = arrayOf(OpenableColumns.DISPLAY_NAME) + cursor = activity.contentResolver.query(contentUri, projection, null, null, null) + + cursor?.let { + val columnIndex = it.getColumnIndex(OpenableColumns.DISPLAY_NAME) + if (it.moveToFirst()) { + return it.getString(columnIndex) + } + } + } catch (e: Exception) { + Logger.error("failed to get real name from URI $e") + } finally { + cursor?.close() + } + + return null // Return null if no file name could be resolved +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PermissionHelper.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PermissionHelper.kt new file mode 100644 index 0000000..37468d2 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PermissionHelper.kt @@ -0,0 +1,113 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +// taken from https://github.com/ionic-team/capacitor/blob/6658bca41e78239347e458175b14ca8bd5c1d6e8/android/capacitor/src/main/java/com/getcapacitor/PermissionHelper.java + +import android.content.Context; +import android.content.pm.PackageManager; +import android.os.Build; +import androidx.core.app.ActivityCompat; +import java.util.ArrayList; + +object PermissionHelper { + /** + * Checks if a list of given permissions are all granted by the user + * + * @param permissions Permissions to check. + * @return True if all permissions are granted, false if at least one is not. + */ + fun hasPermissions(context: Context?, permissions: Array): Boolean { + for (perm in permissions) { + if (ActivityCompat.checkSelfPermission( + context!!, + perm + ) != PackageManager.PERMISSION_GRANTED + ) { + return false + } + } + return true + } + + /** + * Check whether the given permission has been defined in the AndroidManifest.xml + * + * @param permission A permission to check. + * @return True if the permission has been defined in the Manifest, false if not. + */ + fun hasDefinedPermission(context: Context, permission: String): Boolean { + var hasPermission = false + val requestedPermissions = getManifestPermissions(context) + if (requestedPermissions != null && requestedPermissions.isNotEmpty()) { + val requestedPermissionsList = listOf(*requestedPermissions) + val requestedPermissionsArrayList = ArrayList(requestedPermissionsList) + if (requestedPermissionsArrayList.contains(permission)) { + hasPermission = true + } + } + return hasPermission + } + + /** + * Check whether all of the given permissions have been defined in the AndroidManifest.xml + * @param context the app context + * @param permissions a list of permissions + * @return true only if all permissions are defined in the AndroidManifest.xml + */ + fun hasDefinedPermissions(context: Context, permissions: Array): Boolean { + for (permission in permissions) { + if (!hasDefinedPermission(context, permission)) { + return false + } + } + return true + } + + /** + * Get the permissions defined in AndroidManifest.xml + * + * @return The permissions defined in AndroidManifest.xml + */ + private fun getManifestPermissions(context: Context): Array? { + var requestedPermissions: Array? = null + try { + val pm = context.packageManager + val packageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + pm.getPackageInfo(context.packageName, PackageManager.PackageInfoFlags.of(PackageManager.GET_PERMISSIONS.toLong())) + } else { + @Suppress("DEPRECATION") + pm.getPackageInfo(context.packageName, PackageManager.GET_PERMISSIONS) + } + if (packageInfo != null) { + requestedPermissions = packageInfo.requestedPermissions + } + } catch (_: Exception) { + } + return requestedPermissions + } + + /** + * Given a list of permissions, return a new list with the ones not present in AndroidManifest.xml + * + * @param neededPermissions The permissions needed. + * @return The permissions not present in AndroidManifest.xml + */ + fun getUndefinedPermissions(context: Context, neededPermissions: Array): Array { + val undefinedPermissions = ArrayList() + val requestedPermissions = getManifestPermissions(context) + if (!requestedPermissions.isNullOrEmpty()) { + val requestedPermissionsList = listOf(*requestedPermissions) + val requestedPermissionsArrayList = ArrayList(requestedPermissionsList) + for (permission in neededPermissions) { + if (!requestedPermissionsArrayList.contains(permission)) { + undefinedPermissions.add(permission) + } + } + return undefinedPermissions.toTypedArray() + } + return neededPermissions + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PermissionState.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PermissionState.kt new file mode 100644 index 0000000..33092ed --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/PermissionState.kt @@ -0,0 +1,21 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +import java.util.* + +enum class PermissionState(private val state: String) { + GRANTED("granted"), DENIED("denied"), PROMPT("prompt"), PROMPT_WITH_RATIONALE("prompt-with-rationale"); + + override fun toString(): String { + return state + } + + companion object { + fun byState(state: String): PermissionState { + return valueOf(state.uppercase(Locale.ROOT).replace('-', '_')) + } + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/ActivityCallback.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/ActivityCallback.kt new file mode 100644 index 0000000..d680939 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/ActivityCallback.kt @@ -0,0 +1,9 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.annotation + +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.FUNCTION) +annotation class ActivityCallback diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/InvokeArg.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/InvokeArg.kt new file mode 100644 index 0000000..08a2f75 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/InvokeArg.kt @@ -0,0 +1,9 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.annotation + +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.CLASS) +annotation class InvokeArg diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/Permission.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/Permission.kt new file mode 100644 index 0000000..bd5ab5d --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/Permission.kt @@ -0,0 +1,19 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.annotation + +@Retention(AnnotationRetention.RUNTIME) +annotation class Permission( + /** + * An array of Android permission strings. + * Eg: {Manifest.permission.ACCESS_COARSE_LOCATION} + * or {"android.permission.ACCESS_COARSE_LOCATION"} + */ + val strings: Array = [], + /** + * An optional name to use instead of the Android permission string. + */ + val alias: String = "" +) diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/PermissionCallback.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/PermissionCallback.kt new file mode 100644 index 0000000..5fdeddf --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/PermissionCallback.kt @@ -0,0 +1,9 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.annotation + +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.FUNCTION) +annotation class PermissionCallback diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/PluginMethod.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/PluginMethod.kt new file mode 100644 index 0000000..7a73045 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/PluginMethod.kt @@ -0,0 +1,8 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.annotation + +@Retention(AnnotationRetention.RUNTIME) +annotation class Command diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/TauriPlugin.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/TauriPlugin.kt new file mode 100644 index 0000000..4e8bc78 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/annotation/TauriPlugin.kt @@ -0,0 +1,19 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.annotation + +import app.tauri.annotation.Permission + +/** + * Base annotation for all Plugins + */ +@Retention(AnnotationRetention.RUNTIME) +annotation class TauriPlugin( + /** + * Permissions this plugin needs, in order to make permission requests + * easy if the plugin only needs basic permission prompting + */ + val permissions: Array = [] +) diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Channel.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Channel.kt new file mode 100644 index 0000000..581b074 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Channel.kt @@ -0,0 +1,33 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import com.fasterxml.jackson.databind.ObjectMapper + +const val CHANNEL_PREFIX = "__CHANNEL__:" + +internal class ChannelDeserializer(val sendChannelData: (channelId: Long, data: String) -> Unit, private val objectMapper: ObjectMapper): JsonDeserializer() { + override fun deserialize( + jsonParser: JsonParser?, + deserializationContext: DeserializationContext + ): Channel { + val channelDef = deserializationContext.readValue(jsonParser, String::class.java) + val callback = channelDef.substring(CHANNEL_PREFIX.length).toLongOrNull() ?: throw Error("unexpected channel value $channelDef") + return Channel(callback, { res -> sendChannelData(callback, res) }, objectMapper) + } +} + +class Channel(val id: Long, private val handler: (data: String) -> Unit, private val objectMapper: ObjectMapper) { + fun send(data: JSObject) { + handler(PluginResult(data).toString()) + } + + fun sendObject(data: Any) { + handler(objectMapper.writeValueAsString(data)) + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/InvalidPluginMethodException.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/InvalidPluginMethodException.kt new file mode 100644 index 0000000..4325313 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/InvalidPluginMethodException.kt @@ -0,0 +1,11 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +internal class InvalidCommandException : Exception { + constructor(s: String?) : super(s) {} + constructor(t: Throwable?) : super(t) {} + constructor(s: String?, t: Throwable?) : super(s, t) {} +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Invoke.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Invoke.kt new file mode 100644 index 0000000..1f9d080 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Invoke.kt @@ -0,0 +1,93 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import app.tauri.Logger +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper + +class Invoke( + val id: Long, + val command: String, + val callback: Long, + val error: Long, + private val sendResponse: (callback: Long, data: String) -> Unit, + private val argsJson: String, + private val jsonMapper: ObjectMapper +) { + fun getRawArgs(): String { + return argsJson + } + + fun getArgs(): JSObject { + return JSObject(argsJson) + } + + fun parseArgs(cls: Class): T { + return jsonMapper.readValue(argsJson, cls) + } + + fun parseArgs(ref: TypeReference): T { + return jsonMapper.readValue(argsJson, ref) + } + + fun resolve(data: JSObject?) { + sendResponse(callback, PluginResult(data).toString()) + } + + fun resolveObject(data: Any) { + sendResponse(callback, jsonMapper.writeValueAsString(data)) + } + + fun resolve() { + sendResponse(callback, "null") + } + + fun reject(msg: String?, code: String?, ex: Exception?, data: JSObject?) { + val errorResult = PluginResult() + + if (ex != null) { + Logger.error(Logger.tags("Plugin"), msg!!, ex) + } + + errorResult.put("message", msg) + if (code != null) { + errorResult.put("code", code) + } + if (data != null) { + errorResult.put("data", data) + } + + sendResponse(error, errorResult.toString()) + } + + fun reject(msg: String?, ex: Exception?, data: JSObject?) { + reject(msg, null, ex, data) + } + + fun reject(msg: String?, code: String?, data: JSObject?) { + reject(msg, code, null, data) + } + + fun reject(msg: String?, code: String?, ex: Exception?) { + reject(msg, code, ex, null) + } + + fun reject(msg: String?, data: JSObject?) { + reject(msg, null, null, data) + } + + fun reject(msg: String?, ex: Exception?) { + reject(msg, null, ex, null) + } + + fun reject(msg: String?, code: String?) { + reject(msg, code, null, null) + } + + fun reject(msg: String?) { + reject(msg, null, null, null) + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/JSArray.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/JSArray.kt new file mode 100644 index 0000000..84bdf40 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/JSArray.kt @@ -0,0 +1,45 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import org.json.JSONArray +import org.json.JSONException + +class JSArray : JSONArray { + constructor() : super() {} + constructor(json: String?) : super(json) {} + constructor(copyFrom: Collection<*>?) : super(copyFrom) {} + constructor(array: Any?) : super(array) {} + + @Suppress("UNCHECKED_CAST", "ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE") + @Throws(JSONException::class) + fun toList(): List { + val items: MutableList = ArrayList() + var o: Any? = null + for (i in 0 until this.length()) { + this.get(i).also { o = it } + try { + items.add(this.get(i) as E) + } catch (ex: Exception) { + throw JSONException("Not all items are instances of the given type") + } + } + return items + } + + companion object { + /** + * Create a new JSArray without throwing a error + */ + fun from(array: Any?): JSArray? { + try { + return JSArray(array) + } catch (ex: JSONException) { + // + } + return null + } + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/JSObject.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/JSObject.kt new file mode 100644 index 0000000..2d40561 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/JSObject.kt @@ -0,0 +1,152 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import org.json.JSONException +import org.json.JSONObject + +class JSObject : JSONObject { + constructor() : super() + constructor(json: String) : super(json) + constructor(obj: JSONObject, names: Array) : super(obj, names) + + override fun getString(key: String): String { + return getString(key, "")!! + } + + fun getString(key: String, defaultValue: String?): String? { + try { + if (!super.isNull(key)) { + return super.getString(key) + } + } catch (_: JSONException) { + } + return defaultValue + } + + fun getInteger(key: String): Int? { + return getIntegerInternal(key, null) + } + + fun getInteger(key: String, defaultValue: Int): Int { + return getIntegerInternal(key, defaultValue)!! + } + + private fun getIntegerInternal(key: String, defaultValue: Int?): Int? { + try { + return super.getInt(key) + } catch (_: JSONException) { + } + return defaultValue + } + + override fun getBoolean(key: String): Boolean { + return getBooleanInternal(key, false)!! + } + + fun getBoolean(key: String, defaultValue: Boolean?): Boolean { + return getBooleanInternal(key, defaultValue)!! + } + + private fun getBooleanInternal(key: String, defaultValue: Boolean?): Boolean? { + try { + return super.getBoolean(key) + } catch (_: JSONException) { + } + return defaultValue + } + + fun getJSObject(name: String): JSObject? { + try { + return getJSObjectInternal(name, null) + } catch (_: JSONException) { + } + return null + } + + fun getJSObject(name: String, defaultValue: JSObject): JSObject { + return getJSObjectInternal(name, defaultValue)!! + } + + private fun getJSObjectInternal(name: String, defaultValue: JSObject?): JSObject? { + try { + val obj = get(name) + if (obj is JSONObject) { + val keysIter = obj.keys() + val keys: MutableList = ArrayList() + while (keysIter.hasNext()) { + keys.add(keysIter.next()) + } + return JSObject(obj, keys.toTypedArray()) + } + } catch (_: JSONException) { + } + return defaultValue + } + + override fun put(key: String, value: Boolean): JSObject { + try { + super.put(key, value) + } catch (_: JSONException) { + } + return this + } + + override fun put(key: String, value: Int): JSObject { + try { + super.put(key, value) + } catch (_: JSONException) { + } + return this + } + + override fun put(key: String, value: Long): JSObject { + try { + super.put(key, value) + } catch (_: JSONException) { + } + return this + } + + override fun put(key: String, value: Double): JSObject { + try { + super.put(key, value) + } catch (_: JSONException) { + } + return this + } + + override fun put(key: String, value: Any?): JSObject { + try { + super.put(key, value) + } catch (_: JSONException) { + } + return this + } + + fun put(key: String, value: String?): JSObject { + try { + super.put(key, value) + } catch (_: JSONException) { + } + return this + } + + companion object { + /** + * Convert a pathetic JSONObject into a JSObject + * @param obj + */ + @Throws(JSONException::class) + fun fromJSONObject(obj: JSONObject): JSObject { + val keysIter = obj.keys() + val keys: MutableList = ArrayList() + while (keysIter.hasNext()) { + keys.add(keysIter.next()) + } + return JSObject(obj, keys.toTypedArray()) + } + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Plugin.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Plugin.kt new file mode 100644 index 0000000..d33fa8a --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/Plugin.kt @@ -0,0 +1,490 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import android.app.Activity +import android.content.res.Configuration +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.webkit.WebView +import androidx.activity.result.IntentSenderRequest +import androidx.core.app.ActivityCompat +import app.tauri.FsUtils +import app.tauri.Logger +import app.tauri.PermissionHelper +import app.tauri.PermissionState +import app.tauri.annotation.ActivityCallback +import app.tauri.annotation.Command +import app.tauri.annotation.InvokeArg +import app.tauri.annotation.PermissionCallback +import app.tauri.annotation.TauriPlugin +import com.fasterxml.jackson.databind.ObjectMapper +import java.util.* +import java.util.concurrent.CopyOnWriteArrayList + +@InvokeArg +internal class RegisterListenerArgs { + lateinit var event: String + lateinit var handler: Channel +} + +@InvokeArg +internal class RemoveListenerArgs { + lateinit var event: String + var channelId: Long = 0 +} + +@InvokeArg internal class RequestPermissionsArgs { + var permissions: List? = null +} + +abstract class Plugin(private val activity: Activity) { + var handle: PluginHandle? = null + private val listeners: MutableMap> = mutableMapOf() + + open fun load(webView: WebView) {} + + fun jsonMapper(): ObjectMapper { + return handle!!.jsonMapper + } + + fun getConfig(cls: Class): T { + return jsonMapper().readValue(handle!!.config, cls) + } + + /** + * Handle a new intent being received by the application + */ + open fun onNewIntent(intent: Intent) {} + + + /** + * This event is called just before another activity comes into the foreground. + */ + open fun onPause() {} + + /** + * This event is called when the user returns to the activity. It is also called on cold starts. + */ + open fun onResume() {} + + /** + * This event is called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). + * It will be followed by onStart() and then onResume(). + */ + open fun onRestart() {} + + /** + * This event is called when the app is no longer visible to the user. + * You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity. + */ + open fun onStop() {} + + /** + * This event is called before the activity is destroyed. + */ + open fun onDestroy() {} + + /** + * This event is called when a configuration change occurs but the app does not recreate the activity. + */ + open fun onConfigurationChanged(newConfig: Configuration) {} + + /** + * Start activity for result with the provided Intent and resolve calling the provided callback method name. + * + * If there is no registered activity callback for the method name passed in, the call will + * be rejected. Make sure a valid activity result callback method is registered using the + * [ActivityCallback] annotation. + * + * @param invoke the invoke object + * @param intent the intent used to start an activity + * @param callbackName the name of the callback to run when the launched activity is finished + */ + fun startActivityForResult(invoke: Invoke, intent: Intent, callbackName: String) { + handle!!.startActivityForResult(invoke, intent, callbackName) + } + + /** + * Like startActivityForResult() but taking an IntentSender to describe the activity to be started. + * + * If there is no registered activity callback for the method name passed in, the call will + * be rejected. Make sure a valid activity result callback method is registered using the + * [ActivityCallback] annotation. + * + * @param invoke the invoke object + * @param intentSender the intent used to start an activity + * @param callbackName the name of the callback to run when the launched activity is finished + */ + fun startIntentSenderForResult(invoke: Invoke, intentSender: IntentSenderRequest, callbackName: String) { + handle!!.startIntentSenderForResult(invoke, intentSender, callbackName) + } + + /** + * Get the plugin log tags. + * @param subTags + */ + protected fun getLogTag(vararg subTags: String): String { + return Logger.tags(*subTags) + } + + /** + * Gets a log tag with the plugin's class name as subTag. + */ + protected fun getLogTag(): String { + return Logger.tags(this.javaClass.simpleName) + } + + /** + * Convert an URI to an URL that can be loaded by the webview. + */ + fun assetUrl(u: Uri): String { + var path = FsUtils.getFileUrlForUri(activity, u) + if (path?.startsWith("file://") == true) { + path = path.replace("file://", "") + } + return "asset://localhost$path" + } + + fun trigger(event: String, payload: JSObject) { + val eventListeners = listeners[event] + if (!eventListeners.isNullOrEmpty()) { + val listeners = CopyOnWriteArrayList(eventListeners) + for (channel in listeners) { + channel.send(payload) + } + } + } + + fun triggerObject(event: String, payload: Any) { + val eventListeners = listeners[event] + if (!eventListeners.isNullOrEmpty()) { + val listeners = CopyOnWriteArrayList(eventListeners) + for (channel in listeners) { + channel.sendObject(payload) + } + } + } + + fun hasListener(event: String): Boolean { + return !listeners[event].isNullOrEmpty() + } + + @Command + open fun registerListener(invoke: Invoke) { + val args = invoke.parseArgs(RegisterListenerArgs::class.java) + + val eventListeners = listeners[args.event] + if (eventListeners.isNullOrEmpty()) { + listeners[args.event] = mutableListOf(args.handler) + } else { + eventListeners.add(args.handler) + } + + invoke.resolve() + } + + @Command + open fun removeListener(invoke: Invoke) { + val args = invoke.parseArgs(RemoveListenerArgs::class.java) + + val eventListeners = listeners[args.event] + if (!eventListeners.isNullOrEmpty()) { + val c = eventListeners.find { c -> c.id == args.channelId } + if (c != null) { + eventListeners.remove(c) + } + } + + invoke.resolve() + } + + /** + * Exported plugin method for checking the granted status for each permission + * declared on the plugin. This plugin call responds with a mapping of permissions to + * the associated granted status. + */ + @Command + @PermissionCallback + open fun checkPermissions(invoke: Invoke) { + val permissionsResult: Map = getPermissionStates() + if (permissionsResult.isEmpty()) { + // if no permissions are defined on the plugin, resolve undefined + invoke.resolve() + } else { + val permissionsResultJSON = JSObject() + for ((key, value) in permissionsResult) { + permissionsResultJSON.put(key, value) + } + invoke.resolve(permissionsResultJSON) + } + } + + /** + * Exported plugin method to request all permissions for this plugin. + * To manually request permissions within a plugin use: + * [.requestAllPermissions], or + * [.requestPermissionForAlias], or + * [.requestPermissionForAliases] + * + * @param invoke + */ + @Command + open fun requestPermissions(invoke: Invoke) { + val annotation = handle?.annotation + if (annotation != null) { + // handle permission requests for plugins defined with @TauriPlugin + var permAliases: Array? = null + val autoGrantPerms: MutableSet = HashSet() + + val args = invoke.parseArgs(RequestPermissionsArgs::class.java) + + args.permissions?.let { + val aliasSet: MutableSet = HashSet() + + for (perm in annotation.permissions) { + if (it.contains(perm.alias)) { + aliasSet.add(perm.alias) + } + } + if (aliasSet.isEmpty()) { + invoke.reject("No valid permission alias was requested of this plugin.") + return + } else { + permAliases = aliasSet.toTypedArray() + } + } ?: run { + val aliasSet: MutableSet = HashSet() + + for (perm in annotation.permissions) { + // If a permission is defined with no permission strings, separate it for auto-granting. + // Otherwise, the alias is added to the list to be requested. + if (perm.strings.isEmpty() || perm.strings.size == 1 && perm.strings[0] + .isEmpty() + ) { + if (perm.alias.isNotEmpty()) { + autoGrantPerms.add(perm.alias) + } + } else { + aliasSet.add(perm.alias) + } + } + permAliases = aliasSet.toTypedArray() + } + + permAliases?.let { + // request permissions using provided aliases or all defined on the plugin + requestPermissionForAliases(it, invoke, "checkPermissions") + } ?: run { + if (autoGrantPerms.isNotEmpty()) { + // if the plugin only has auto-grant permissions, return all as GRANTED + val permissionsResults = JSObject() + for (perm in autoGrantPerms) { + permissionsResults.put(perm, PermissionState.GRANTED.toString()) + } + invoke.resolve(permissionsResults) + } else { + // no permissions are defined on the plugin, resolve undefined + invoke.resolve() + } + } + } + } + + /** + * Checks if the given permission alias is correctly declared in AndroidManifest.xml + * @param alias a permission alias defined on the plugin + * @return true only if all permissions associated with the given alias are declared in the manifest + */ + fun isPermissionDeclared(alias: String): Boolean { + val annotation = handle?.annotation + if (annotation != null) { + for (perm in annotation.permissions) { + if (alias.equals(perm.alias, ignoreCase = true)) { + var result = true + for (permString in perm.strings) { + result = result && PermissionHelper.hasDefinedPermission(activity, permString) + } + return result + } + } + } + Logger.error( + String.format( + "isPermissionDeclared: No alias defined for %s " + "or missing @TauriPlugin annotation.", + alias + ) + ) + return false + } + + private fun permissionActivityResult( + invoke: Invoke, + permissionStrings: Array, + callbackName: String + ) { + handle!!.requestPermissions(invoke, permissionStrings, callbackName) + } + + /** + * Request all of the specified permissions in the TauriPlugin annotation (if any) + * + * If there is no registered permission callback for the Invoke passed in, the call will + * be rejected. Make sure a valid permission callback method is registered using the + * [PermissionCallback] annotation. + * + * @param invoke + * @param callbackName the name of the callback to run when the permission request is complete + */ + protected fun requestAllPermissions( + invoke: Invoke, + callbackName: String + ) { + val annotation = handle!!.annotation + if (annotation != null) { + val perms: HashSet = HashSet() + for (perm in annotation.permissions) { + perms.addAll(perm.strings) + } + permissionActivityResult(invoke, perms.toArray(arrayOfNulls(0)), callbackName) + } + } + + /** + * Request permissions using an alias defined on the plugin. + * + * If there is no registered permission callback for the Invoke passed in, the call will + * be rejected. Make sure a valid permission callback method is registered using the + * [PermissionCallback] annotation. + * + * @param alias an alias defined on the plugin + * @param invoke the invoke involved in originating the request + * @param callbackName the name of the callback to run when the permission request is complete + */ + protected fun requestPermissionForAlias( + alias: String, + invoke: Invoke, + callbackName: String + ) { + requestPermissionForAliases(arrayOf(alias), invoke, callbackName) + } + + /** + * Request permissions using aliases defined on the plugin. + * + * If there is no registered permission callback for the Invoke passed in, the call will + * be rejected. Make sure a valid permission callback method is registered using the + * [PermissionCallback] annotation. + * + * @param aliases a set of aliases defined on the plugin + * @param invoke the invoke involved in originating the request + * @param callbackName the name of the callback to run when the permission request is complete + */ + fun requestPermissionForAliases( + aliases: Array, + invoke: Invoke, + callbackName: String + ) { + if (aliases.isEmpty()) { + Logger.error("No permission alias was provided") + return + } + val permissions = getPermissionStringsForAliases(aliases) + if (permissions.isNotEmpty()) { + permissionActivityResult(invoke, permissions, callbackName) + } + } + + /** + * Gets the Android permission strings defined on the [TauriPlugin] annotation with + * the provided aliases. + * + * @param aliases aliases for permissions defined on the plugin + * @return Android permission strings associated with the provided aliases, if exists + */ + private fun getPermissionStringsForAliases(aliases: Array): Array { + val annotation = handle?.annotation + val perms: HashSet = HashSet() + if (annotation != null) { + for (perm in annotation.permissions) { + if (aliases.contains(perm.alias)) { + perms.addAll(perm.strings) + } + } + } + return perms.toArray(arrayOfNulls(0)) + } + + /** + * Get the permission state for the provided permission alias. + * + * @param alias the permission alias to get + * @return the state of the provided permission alias or null + */ + fun getPermissionState(alias: String): PermissionState? { + return getPermissionStates()[alias] + } + + /** + * Helper to check all permissions defined on a plugin and see the state of each. + * + * @return A mapping of permission aliases to the associated granted status. + */ + open fun getPermissionStates(): Map { + val permissionsResults: MutableMap = HashMap() + val annotation = handle?.annotation + if (annotation != null) { + for (perm in annotation.permissions) { + // If a permission is defined with no permission constants, return GRANTED for it. + // Otherwise, get its true state. + if (perm.strings.isEmpty() || perm.strings.size == 1 && perm.strings[0] + .isEmpty() + ) { + val key = perm.alias + if (key.isNotEmpty()) { + val existingResult = permissionsResults[key] + + // auto set permission state to GRANTED if the alias is empty. + if (existingResult == null) { + permissionsResults[key] = PermissionState.GRANTED + } + } + } else { + for (permString in perm.strings) { + val key = perm.alias.ifEmpty { permString } + var permissionStatus: PermissionState + if (ActivityCompat.checkSelfPermission( + activity, + permString + ) == PackageManager.PERMISSION_GRANTED + ) { + permissionStatus = PermissionState.GRANTED + } else { + permissionStatus = PermissionState.PROMPT + + // Check if there is a cached permission state for the "Never ask again" state + val prefs = + activity.getSharedPreferences("PluginPermStates", Activity.MODE_PRIVATE) + val state = prefs.getString(permString, null) + if (state != null) { + permissionStatus = PermissionState.byState(state) + } + } + val existingResult = permissionsResults[key] + + // multiple permissions with the same alias must all be true, otherwise all false. + if (existingResult == null || existingResult === PermissionState.GRANTED) { + permissionsResults[key] = permissionStatus + } + } + } + } + } + + return permissionsResults + } + +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginHandle.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginHandle.kt new file mode 100644 index 0000000..8296c81 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginHandle.kt @@ -0,0 +1,168 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import android.app.Activity +import android.content.Intent +import android.content.SharedPreferences +import android.webkit.WebView +import androidx.activity.result.IntentSenderRequest +import androidx.core.app.ActivityCompat +import app.tauri.PermissionHelper +import app.tauri.PermissionState +import app.tauri.annotation.ActivityCallback +import app.tauri.annotation.Command +import app.tauri.annotation.PermissionCallback +import app.tauri.annotation.TauriPlugin +import com.fasterxml.jackson.databind.ObjectMapper +import java.lang.reflect.Method + +class PluginHandle(private val manager: PluginManager, val name: String, val instance: Plugin, val config: String, val jsonMapper: ObjectMapper) { + private val commands: HashMap = HashMap() + private val permissionCallbackMethods: HashMap = HashMap() + private val startActivityCallbackMethods: HashMap = HashMap() + var annotation: TauriPlugin? + var loaded = false + + init { + indexMethods() + instance.handle = this + annotation = instance.javaClass.getAnnotation(TauriPlugin::class.java) + } + + fun load(webView: WebView) { + instance.load(webView) + loaded = true + } + + fun startActivityForResult(invoke: Invoke, intent: Intent, callbackName: String) { + manager.startActivityForResult(intent) { result -> + val method = startActivityCallbackMethods[callbackName] + if (method != null) { + method.isAccessible = true + method(instance, invoke, result) + } + } + } + + fun startIntentSenderForResult(invoke: Invoke, intentSender: IntentSenderRequest, callbackName: String) { + manager.startIntentSenderForResult(intentSender) { result -> + val method = startActivityCallbackMethods[callbackName] + if (method != null) { + method.isAccessible = true + method(instance, invoke, result) + } + } + } + + fun requestPermissions( + invoke: Invoke, + permissions: Array, + callbackName: String + ) { + manager.requestPermissions(permissions) { result -> + if (validatePermissions(invoke, result)) { + val method = permissionCallbackMethods[callbackName] + if (method != null) { + method.isAccessible = true + method(instance, invoke) + } + } + } + } + + /** + * Saves permission states and rejects if permissions were not correctly defined in + * the AndroidManifest.xml file. + * + * @param permissions + * @return true if permissions were saved and defined correctly, false if not + */ + private fun validatePermissions( + invoke: Invoke, + permissions: Map + ): Boolean { + val activity = manager.activity + val prefs = + activity.getSharedPreferences("PluginPermStates", Activity.MODE_PRIVATE) + for ((permString, isGranted) in permissions) { + if (isGranted) { + // Permission granted. If previously denied, remove cached state + val state = prefs.getString(permString, null) + if (state != null) { + val editor: SharedPreferences.Editor = prefs.edit() + editor.remove(permString) + editor.apply() + } + } else { + val editor: SharedPreferences.Editor = prefs.edit() + if (ActivityCompat.shouldShowRequestPermissionRationale( + activity, + permString + ) + ) { + // Permission denied, can prompt again with rationale + editor.putString(permString, PermissionState.PROMPT_WITH_RATIONALE.toString()) + } else { + // Permission denied permanently, store this state for future reference + editor.putString(permString, PermissionState.DENIED.toString()) + } + editor.apply() + } + } + val permStrings = permissions.keys.toTypedArray() + if (!PermissionHelper.hasDefinedPermissions(activity, permStrings)) { + val builder = StringBuilder() + builder.append("Missing the following permissions in AndroidManifest.xml:\n") + val missing = PermissionHelper.getUndefinedPermissions(activity, permStrings) + for (perm in missing) { + builder.append( + """ + $perm + + """.trimIndent() + ) + } + invoke.reject(builder.toString()) + return false + } + return true + } + + @Throws( + InvalidCommandException::class, + IllegalAccessException::class + ) + fun invoke(invoke: Invoke) { + val methodMeta = commands[invoke.command] + ?: throw InvalidCommandException("No command " + invoke.command + " found for plugin " + instance.javaClass.name) + methodMeta.method.invoke(instance, invoke) + } + + private fun indexMethods() { + val methods = mutableListOf() + var pluginCursor: Class<*> = instance.javaClass + while (pluginCursor.name != Any::class.java.name) { + methods.addAll(listOf(*pluginCursor.declaredMethods)) + pluginCursor = pluginCursor.superclass + } + + for (method in methods) { + if (method.isAnnotationPresent(Command::class.java)) { + val command = method.getAnnotation(Command::class.java) ?: continue + val methodMeta = CommandData(method, command) + commands[method.name] = methodMeta + } + + if (method.isAnnotationPresent(ActivityCallback::class.java)) { + startActivityCallbackMethods[method.name] = method + } + + if (method.isAnnotationPresent(PermissionCallback::class.java)) { + permissionCallbackMethods[method.name] = method + } + } + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginManager.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginManager.kt new file mode 100644 index 0000000..362896b --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginManager.kt @@ -0,0 +1,221 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import android.app.PendingIntent +import android.content.res.Configuration +import android.content.Context +import android.content.Intent +import android.webkit.WebView +import androidx.activity.result.ActivityResult +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.IntentSenderRequest +import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.app.AppCompatActivity +import app.tauri.annotation.InvokeArg +import app.tauri.FsUtils +import app.tauri.JniMethod +import app.tauri.Logger +import com.fasterxml.jackson.annotation.JsonAutoDetect +import com.fasterxml.jackson.annotation.PropertyAccessor +import com.fasterxml.jackson.databind.DeserializationFeature +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.module.SimpleModule +import java.lang.reflect.InvocationTargetException + +class PluginManager(val activity: AppCompatActivity) { + fun interface RequestPermissionsCallback { + fun onResult(permissions: Map) + } + + fun interface ActivityResultCallback { + fun onResult(result: ActivityResult) + } + + private val plugins: HashMap = HashMap() + private val startActivityForResultLauncher: ActivityResultLauncher + private val startIntentSenderForResultLauncher: ActivityResultLauncher + private val requestPermissionsLauncher: ActivityResultLauncher> + private var requestPermissionsCallback: RequestPermissionsCallback? = null + private var startActivityForResultCallback: ActivityResultCallback? = null + private var startIntentSenderForResultCallback: ActivityResultCallback? = null + private var jsonMapper: ObjectMapper + + init { + startActivityForResultLauncher = + activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult() + ) { result -> + if (startActivityForResultCallback != null) { + startActivityForResultCallback!!.onResult(result) + } + } + + startIntentSenderForResultLauncher = + activity.registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult() + ) { result -> + if (startIntentSenderForResultCallback != null) { + startIntentSenderForResultCallback!!.onResult(result) + } + } + + requestPermissionsLauncher = + activity.registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions() + ) { result -> + if (requestPermissionsCallback != null) { + requestPermissionsCallback!!.onResult(result) + } + } + + jsonMapper = ObjectMapper() + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY) + + val channelDeserializer = ChannelDeserializer({ channelId, payload -> + sendChannelData(channelId, payload) + }, jsonMapper) + jsonMapper + .registerModule(SimpleModule().addDeserializer(Channel::class.java, channelDeserializer)) + } + + fun onNewIntent(intent: Intent) { + for (plugin in plugins.values) { + plugin.instance.onNewIntent(intent) + } + } + + fun onPause() { + for (plugin in plugins.values) { + plugin.instance.onPause() + } + } + + fun onResume() { + for (plugin in plugins.values) { + plugin.instance.onResume() + } + } + + fun onRestart() { + for (plugin in plugins.values) { + plugin.instance.onRestart() + } + } + + fun onStop() { + for (plugin in plugins.values) { + plugin.instance.onStop() + } + } + + fun onDestroy() { + for (plugin in plugins.values) { + plugin.instance.onDestroy() + } + } + + fun onConfigurationChanged(newConfig: Configuration) { + for (plugin in plugins.values) { + plugin.instance.onConfigurationChanged(newConfig) + } + } + + fun startActivityForResult(intent: Intent, callback: ActivityResultCallback) { + startActivityForResultCallback = callback + startActivityForResultLauncher.launch(intent) + } + + fun startIntentSenderForResult(intent: IntentSenderRequest, callback: ActivityResultCallback) { + startIntentSenderForResultCallback = callback + startIntentSenderForResultLauncher.launch(intent) + } + + fun requestPermissions( + permissionStrings: Array, + callback: RequestPermissionsCallback + ) { + requestPermissionsCallback = callback + requestPermissionsLauncher.launch(permissionStrings) + } + + @JniMethod + fun onWebViewCreated(webView: WebView) { + for ((_, plugin) in plugins) { + if (!plugin.loaded) { + plugin.load(webView) + } + } + } + + @JniMethod + fun load(webView: WebView?, name: String, plugin: Plugin, config: String) { + val handle = PluginHandle(this, name, plugin, config, jsonMapper) + plugins[name] = handle + if (webView != null) { + plugin.load(webView) + } + } + + @JniMethod + fun runCommand(id: Int, pluginId: String, command: String, data: String) { + val successId = 0L + val errorId = 1L + val invoke = Invoke(id.toLong(), command, successId, errorId, { fn, result -> + var success: String? = null + var error: String? = null + if (fn == successId) { + success = result + } else { + error = result + } + handlePluginResponse(id, success, error) + }, data, jsonMapper) + + dispatchPluginMessage(invoke, pluginId) + } + + private fun dispatchPluginMessage(invoke: Invoke, pluginId: String) { + Logger.verbose( + Logger.tags("Plugin"), + "Tauri plugin: pluginId: $pluginId, command: ${invoke.command}" + ) + + try { + val plugin = plugins[pluginId] + if (plugin == null) { + invoke.reject("Plugin $pluginId not initialized") + } else { + plugins[pluginId]?.invoke(invoke) + } + } catch (e: Exception) { + var exception: Throwable = e + if (exception.message?.isEmpty() != false) { + if (e is InvocationTargetException) { + exception = e.targetException + } + } + invoke.reject(if (exception.message?.isEmpty() != false) { exception.toString() } else { exception.message }) + } + } + + companion object { + fun loadConfig(context: Context, plugin: String, cls: Class): T { + val tauriConfigJson = FsUtils.readAsset(context.assets, "tauri.conf.json") + val mapper = ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + val config = mapper.readValue(tauriConfigJson, Config::class.java) + return mapper.readValue(config.plugins[plugin].toString(), cls) + } + } + + private external fun handlePluginResponse(id: Int, success: String?, error: String?) + private external fun sendChannelData(id: Long, data: String) +} + +@InvokeArg +internal class Config { + lateinit var plugins: Map +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginMethodData.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginMethodData.kt new file mode 100644 index 0000000..acb0947 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginMethodData.kt @@ -0,0 +1,16 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import app.tauri.annotation.Command +import java.lang.reflect.Method + +class CommandData( + val method: Method, methodDecorator: Command +) { + + // The name of the method + val name: String = method.name +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginResult.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginResult.kt new file mode 100644 index 0000000..601cd48 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/main/java/app/tauri/plugin/PluginResult.kt @@ -0,0 +1,67 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri.plugin + +import android.annotation.SuppressLint +import app.tauri.Logger +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.util.* + +class PluginResult @JvmOverloads constructor(json: JSObject? = JSObject()) { + private val json: JSObject + + init { + this.json = json ?: JSObject() + } + + fun put(name: String, value: Boolean): PluginResult { + return jsonPut(name, value) + } + + fun put(name: String, value: Double): PluginResult { + return jsonPut(name, value) + } + + fun put(name: String, value: Int): PluginResult { + return jsonPut(name, value) + } + + fun put(name: String, value: Long): PluginResult { + return jsonPut(name, value) + } + + /** + * Format a date as an ISO string + */ + @SuppressLint("SimpleDateFormat") + fun put(name: String, value: Date): PluginResult { + val tz: TimeZone = TimeZone.getTimeZone("UTC") + val df: DateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'") + df.timeZone = tz + return jsonPut(name, df.format(value)) + } + + fun put(name: String, value: Any?): PluginResult { + return jsonPut(name, value) + } + + fun put(name: String, value: PluginResult): PluginResult { + return jsonPut(name, value.json) + } + + private fun jsonPut(name: String, value: Any?): PluginResult { + try { + json.put(name, value) + } catch (ex: Exception) { + Logger.error(Logger.tags("Plugin"), "", ex) + } + return this + } + + override fun toString(): String { + return json.toString() + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/test/java/app/tauri/ExampleUnitTest.kt b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/test/java/app/tauri/ExampleUnitTest.kt new file mode 100644 index 0000000..16022f4 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/.tauri/tauri-api/src/test/java/app/tauri/ExampleUnitTest.kt @@ -0,0 +1,21 @@ +// Copyright 2019-2024 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +package app.tauri + +import org.junit.Test + +import org.junit.Assert.* + +/** + * Example local unit test, which will execute on the development machine (host). + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +class ExampleUnitTest { + @Test + fun addition_isCorrect() { + assertEquals(4, 2 + 2) + } +} diff --git a/apps/tauri/tauri-plugin-credentials/android/build.gradle.kts b/apps/tauri/tauri-plugin-credentials/android/build.gradle.kts new file mode 100644 index 0000000..5807a2b --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/build.gradle.kts @@ -0,0 +1,33 @@ +plugins { + id("com.android.library") + id("org.jetbrains.kotlin.android") +} + +android { + namespace = "app.tauri.credentials" + compileSdk = 36 + + defaultConfig { + minSdk = 24 + } + + buildTypes { + release { + isMinifyEnabled = false + } + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + + kotlinOptions { + jvmTarget = "1.8" + } +} + +dependencies { + implementation("androidx.security:security-crypto:1.0.0") + implementation(project(":tauri-android")) +} diff --git a/apps/tauri/tauri-plugin-credentials/android/src/main/AndroidManifest.xml b/apps/tauri/tauri-plugin-credentials/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..94cbbcf --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/apps/tauri/tauri-plugin-credentials/android/src/main/java/app/tauri/credentials/CredentialPlugin.kt b/apps/tauri/tauri-plugin-credentials/android/src/main/java/app/tauri/credentials/CredentialPlugin.kt new file mode 100644 index 0000000..d94756f --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/android/src/main/java/app/tauri/credentials/CredentialPlugin.kt @@ -0,0 +1,96 @@ +package app.tauri.credentials + +import android.app.Activity +import android.content.Context +import android.content.SharedPreferences +import androidx.security.crypto.EncryptedSharedPreferences +import androidx.security.crypto.MasterKeys +import app.tauri.annotation.Command +import app.tauri.annotation.InvokeArg +import app.tauri.annotation.TauriPlugin +import app.tauri.plugin.Invoke +import app.tauri.plugin.JSObject +import app.tauri.plugin.Plugin + +@InvokeArg +class StoreArgs { + lateinit var domain: String + lateinit var username: String + lateinit var password: String +} + +@InvokeArg +class DomainArgs { + lateinit var domain: String +} + +/// Credential storage plugin using Android EncryptedSharedPreferences (backed by Android Keystore). +@TauriPlugin +class CredentialPlugin(private val activity: Activity) : Plugin(activity) { + + private fun getPrefs(): SharedPreferences { + val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) + return EncryptedSharedPreferences.create( + "onyx_credentials", + masterKeyAlias, + activity, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + } + + @Command + fun store(invoke: Invoke) { + val args = invoke.parseArgs(StoreArgs::class.java) + try { + getPrefs().edit() + .putString("${args.domain}::username", args.username) + .putString("${args.domain}::${args.username}::password", args.password) + .apply() + invoke.resolve() + } catch (e: Exception) { + invoke.reject("Failed to store credentials: ${e.message}") + } + } + + @Command + fun load(invoke: Invoke) { + val args = invoke.parseArgs(DomainArgs::class.java) + try { + val prefs = getPrefs() + val username = prefs.getString("${args.domain}::username", null) + if (username == null) { + invoke.reject("No credentials found for '${args.domain}'. Run setup or configure environment variables.") + return + } + val password = prefs.getString("${args.domain}::${username}::password", null) + if (password == null) { + invoke.reject("No password found for '${args.domain}' user '$username'") + return + } + val result = JSObject() + result.put("username", username) + result.put("password", password) + invoke.resolve(result) + } catch (e: Exception) { + invoke.reject("Failed to load credentials: ${e.message}") + } + } + + @Command + fun delete(invoke: Invoke) { + val args = invoke.parseArgs(DomainArgs::class.java) + try { + val prefs = getPrefs() + val username = prefs.getString("${args.domain}::username", null) + val editor = prefs.edit().remove("${args.domain}::username") + if (username != null) { + editor.remove("${args.domain}::${username}::password") + } + editor.apply() + invoke.resolve() + } catch (e: Exception) { + invoke.reject("Failed to delete credentials: ${e.message}") + } + } +} diff --git a/apps/tauri/tauri-plugin-credentials/build.rs b/apps/tauri/tauri-plugin-credentials/build.rs new file mode 100644 index 0000000..de0ed54 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/build.rs @@ -0,0 +1,7 @@ +const COMMANDS: &[&str] = &["store", "load", "delete"]; + +fn main() { + tauri_plugin::Builder::new(COMMANDS) + .android_path("android") + .build(); +} diff --git a/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/delete.toml b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/delete.toml new file mode 100644 index 0000000..3d9d523 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/delete.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-delete" +description = "Enables the delete command without any pre-configured scope." +commands.allow = ["delete"] + +[[permission]] +identifier = "deny-delete" +description = "Denies the delete command without any pre-configured scope." +commands.deny = ["delete"] diff --git a/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/load.toml b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/load.toml new file mode 100644 index 0000000..f6e47ad --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/load.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-load" +description = "Enables the load command without any pre-configured scope." +commands.allow = ["load"] + +[[permission]] +identifier = "deny-load" +description = "Denies the load command without any pre-configured scope." +commands.deny = ["load"] diff --git a/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/store.toml b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/store.toml new file mode 100644 index 0000000..197a789 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/commands/store.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-store" +description = "Enables the store command without any pre-configured scope." +commands.allow = ["store"] + +[[permission]] +identifier = "deny-store" +description = "Denies the store command without any pre-configured scope." +commands.deny = ["store"] diff --git a/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/reference.md b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/reference.md new file mode 100644 index 0000000..aa2d2d1 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/permissions/autogenerated/reference.md @@ -0,0 +1,87 @@ +## Permission Table + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IdentifierDescription
+ +`credentials:allow-delete` + + + +Enables the delete command without any pre-configured scope. + +
+ +`credentials:deny-delete` + + + +Denies the delete command without any pre-configured scope. + +
+ +`credentials:allow-load` + + + +Enables the load command without any pre-configured scope. + +
+ +`credentials:deny-load` + + + +Denies the load command without any pre-configured scope. + +
+ +`credentials:allow-store` + + + +Enables the store command without any pre-configured scope. + +
+ +`credentials:deny-store` + + + +Denies the store command without any pre-configured scope. + +
diff --git a/apps/tauri/tauri-plugin-credentials/permissions/schemas/schema.json b/apps/tauri/tauri-plugin-credentials/permissions/schemas/schema.json new file mode 100644 index 0000000..09c9a4b --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/permissions/schemas/schema.json @@ -0,0 +1,336 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "PermissionFile", + "description": "Permission file that can define a default permission, a set of permissions or a list of inlined permissions.", + "type": "object", + "properties": { + "default": { + "description": "The default permission set for the plugin", + "anyOf": [ + { + "$ref": "#/definitions/DefaultPermission" + }, + { + "type": "null" + } + ] + }, + "set": { + "description": "A list of permissions sets defined", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionSet" + } + }, + "permission": { + "description": "A list of inlined permissions", + "default": [], + "type": "array", + "items": { + "$ref": "#/definitions/Permission" + } + } + }, + "definitions": { + "DefaultPermission": { + "description": "The default permission set of the plugin.\n\nWorks similarly to a permission with the \"default\" identifier.", + "type": "object", + "required": [ + "permissions" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "PermissionSet": { + "description": "A set of direct permissions grouped together under a new name.", + "type": "object", + "required": [ + "description", + "identifier", + "permissions" + ], + "properties": { + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does.", + "type": "string" + }, + "permissions": { + "description": "All permissions this set contains.", + "type": "array", + "items": { + "$ref": "#/definitions/PermissionKind" + } + } + } + }, + "Permission": { + "description": "Descriptions of explicit privileges of commands.\n\nIt can enable commands to be accessible in the frontend of the application.\n\nIf the scope is defined it can be used to fine grain control the access of individual or multiple commands.", + "type": "object", + "required": [ + "identifier" + ], + "properties": { + "version": { + "description": "The version of the permission.", + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 1.0 + }, + "identifier": { + "description": "A unique identifier for the permission.", + "type": "string" + }, + "description": { + "description": "Human-readable description of what the permission does. Tauri internal convention is to use `

` headings in markdown content for Tauri documentation generation purposes.", + "type": [ + "string", + "null" + ] + }, + "commands": { + "description": "Allowed or denied commands when using this permission.", + "default": { + "allow": [], + "deny": [] + }, + "allOf": [ + { + "$ref": "#/definitions/Commands" + } + ] + }, + "scope": { + "description": "Allowed or denied scoped when using this permission.", + "allOf": [ + { + "$ref": "#/definitions/Scopes" + } + ] + }, + "platforms": { + "description": "Target platforms this permission applies. By default all platforms are affected by this permission.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Target" + } + } + } + }, + "Commands": { + "description": "Allowed and denied commands inside a permission.\n\nIf two commands clash inside of `allow` and `deny`, it should be denied by default.", + "type": "object", + "properties": { + "allow": { + "description": "Allowed command.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + }, + "deny": { + "description": "Denied command, which takes priority.", + "default": [], + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "Scopes": { + "description": "An argument for fine grained behavior control of Tauri commands.\n\nIt can be of any serde serializable type and is used to allow or prevent certain actions inside a Tauri command. The configured scope is passed to the command and will be enforced by the command implementation.\n\n## Example\n\n```json { \"allow\": [{ \"path\": \"$HOME/**\" }], \"deny\": [{ \"path\": \"$HOME/secret.txt\" }] } ```", + "type": "object", + "properties": { + "allow": { + "description": "Data that defines what is allowed by the scope.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + }, + "deny": { + "description": "Data that defines what is denied by the scope. This should be prioritized by validation logic.", + "type": [ + "array", + "null" + ], + "items": { + "$ref": "#/definitions/Value" + } + } + } + }, + "Value": { + "description": "All supported ACL values.", + "anyOf": [ + { + "description": "Represents a null JSON value.", + "type": "null" + }, + { + "description": "Represents a [`bool`].", + "type": "boolean" + }, + { + "description": "Represents a valid ACL [`Number`].", + "allOf": [ + { + "$ref": "#/definitions/Number" + } + ] + }, + { + "description": "Represents a [`String`].", + "type": "string" + }, + { + "description": "Represents a list of other [`Value`]s.", + "type": "array", + "items": { + "$ref": "#/definitions/Value" + } + }, + { + "description": "Represents a map of [`String`] keys to [`Value`]s.", + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/Value" + } + } + ] + }, + "Number": { + "description": "A valid ACL number.", + "anyOf": [ + { + "description": "Represents an [`i64`].", + "type": "integer", + "format": "int64" + }, + { + "description": "Represents a [`f64`].", + "type": "number", + "format": "double" + } + ] + }, + "Target": { + "description": "Platform target.", + "oneOf": [ + { + "description": "MacOS.", + "type": "string", + "enum": [ + "macOS" + ] + }, + { + "description": "Windows.", + "type": "string", + "enum": [ + "windows" + ] + }, + { + "description": "Linux.", + "type": "string", + "enum": [ + "linux" + ] + }, + { + "description": "Android.", + "type": "string", + "enum": [ + "android" + ] + }, + { + "description": "iOS.", + "type": "string", + "enum": [ + "iOS" + ] + } + ] + }, + "PermissionKind": { + "type": "string", + "oneOf": [ + { + "description": "Enables the delete command without any pre-configured scope.", + "type": "string", + "const": "allow-delete", + "markdownDescription": "Enables the delete command without any pre-configured scope." + }, + { + "description": "Denies the delete command without any pre-configured scope.", + "type": "string", + "const": "deny-delete", + "markdownDescription": "Denies the delete command without any pre-configured scope." + }, + { + "description": "Enables the load command without any pre-configured scope.", + "type": "string", + "const": "allow-load", + "markdownDescription": "Enables the load command without any pre-configured scope." + }, + { + "description": "Denies the load command without any pre-configured scope.", + "type": "string", + "const": "deny-load", + "markdownDescription": "Denies the load command without any pre-configured scope." + }, + { + "description": "Enables the store command without any pre-configured scope.", + "type": "string", + "const": "allow-store", + "markdownDescription": "Enables the store command without any pre-configured scope." + }, + { + "description": "Denies the store command without any pre-configured scope.", + "type": "string", + "const": "deny-store", + "markdownDescription": "Denies the store command without any pre-configured scope." + } + ] + } + } +} \ No newline at end of file diff --git a/apps/tauri/tauri-plugin-credentials/src/lib.rs b/apps/tauri/tauri-plugin-credentials/src/lib.rs new file mode 100644 index 0000000..997d064 --- /dev/null +++ b/apps/tauri/tauri-plugin-credentials/src/lib.rs @@ -0,0 +1,211 @@ +use serde::{Deserialize, Serialize}; +use tauri::{ + plugin::{Builder, TauriPlugin}, + Manager, Runtime, +}; +#[cfg(target_os = "android")] +use tauri::plugin::PluginHandle; + +const PLUGIN_IDENTIFIER: &str = "app.tauri.credentials"; + +#[derive(Serialize, Deserialize)] +struct StoreArgs { + domain: String, + username: String, + password: String, +} + +#[derive(Serialize, Deserialize)] +struct DomainArgs { + domain: String, +} + +#[derive(Serialize, Deserialize)] +struct LoadResult { + username: String, + password: String, +} + +/// Credential storage handle. Desktop uses the system keychain; Android uses EncryptedSharedPreferences. +pub struct Credentials { + #[cfg(target_os = "android")] + _handle: PluginHandle, + #[cfg(not(target_os = "android"))] + _phantom: std::marker::PhantomData R>, +} + +impl Credentials { + pub fn store(&self, domain: &str, username: &str, password: &str) -> Result<(), String> { + #[cfg(target_os = "android")] + { + self._handle + .run_mobile_plugin::<()>( + "store", + StoreArgs { + domain: domain.to_string(), + username: username.to_string(), + password: password.to_string(), + }, + ) + .map_err(|e| e.to_string()) + } + #[cfg(not(target_os = "android"))] + { + desktop_store(domain, username, password) + } + } + + pub fn load(&self, domain: &str) -> Result<(String, String), String> { + #[cfg(target_os = "android")] + { + let result: LoadResult = self + ._handle + .run_mobile_plugin("load", DomainArgs { domain: domain.to_string() }) + .map_err(|e| e.to_string())?; + Ok((result.username, result.password)) + } + #[cfg(not(target_os = "android"))] + { + desktop_load(domain) + } + } + + pub fn delete(&self, domain: &str) -> Result<(), String> { + #[cfg(target_os = "android")] + { + self._handle + .run_mobile_plugin::<()>("delete", DomainArgs { domain: domain.to_string() }) + .map_err(|e| e.to_string()) + } + #[cfg(not(target_os = "android"))] + { + desktop_delete(domain) + } + } +} + +// ── Desktop keyring implementation ────────────────────────────────── + +#[cfg(all(not(target_os = "android"), feature = "desktop"))] +fn desktop_store(domain: &str, username: &str, password: &str) -> Result<(), String> { + let service = format!("com.onyx.webdav.{}", domain); + let scoped_service = format!("com.onyx.webdav.{}::{}", domain, username); + + keyring::Entry::new(&service, "username") + .map_err(|e| format!("Failed to create keyring entry: {}", e))? + .set_password(username) + .map_err(|e| format!("Failed to store username: {}", e))?; + + keyring::Entry::new(&scoped_service, "password") + .map_err(|e| format!("Failed to create keyring entry: {}", e))? + .set_password(password) + .map_err(|e| format!("Failed to store password: {}", e))?; + + if let Ok(legacy) = keyring::Entry::new(&service, "password") { + let _ = legacy.delete_credential(); + } + Ok(()) +} + +#[cfg(all(not(target_os = "android"), not(feature = "desktop")))] +fn desktop_store(_domain: &str, _username: &str, _password: &str) -> Result<(), String> { + Err("Credential storage not available on this platform".into()) +} + +#[cfg(all(not(target_os = "android"), feature = "desktop"))] +fn desktop_load(domain: &str) -> Result<(String, String), String> { + let service = format!("com.onyx.webdav.{}", domain); + + let username = keyring::Entry::new(&service, "username") + .map_err(|e| format!("Failed to create keyring entry: {}", e))? + .get_password() + .map_err(|_| { + format!( + "No credentials found for '{}'. Run setup or configure environment variables.", + domain + ) + })?; + + let scoped_service = format!("com.onyx.webdav.{}::{}", domain, username); + let password = keyring::Entry::new(&scoped_service, "password") + .ok() + .and_then(|e| e.get_password().ok()) + .or_else(|| { + keyring::Entry::new(&service, "password") + .ok() + .and_then(|e| e.get_password().ok()) + }) + .ok_or_else(|| format!("No password found for '{}' user '{}'", domain, username))?; + + // Auto-migrate legacy credentials to scoped format + if keyring::Entry::new(&scoped_service, "password") + .ok() + .and_then(|e| e.get_password().ok()) + .is_none() + { + if let Ok(entry) = keyring::Entry::new(&scoped_service, "password") { + let _ = entry.set_password(&password); + } + if let Ok(legacy) = keyring::Entry::new(&service, "password") { + let _ = legacy.delete_credential(); + } + } + + Ok((username, password)) +} + +#[cfg(all(not(target_os = "android"), not(feature = "desktop")))] +fn desktop_load(domain: &str) -> Result<(String, String), String> { + Err(format!( + "No credentials found for '{}'. Credential storage not available on this platform.", + domain + )) +} + +#[cfg(all(not(target_os = "android"), feature = "desktop"))] +fn desktop_delete(domain: &str) -> Result<(), String> { + let service = format!("com.onyx.webdav.{}", domain); + let username = keyring::Entry::new(&service, "username") + .ok() + .and_then(|e| e.get_password().ok()); + + if let Some(user) = &username { + let scoped = format!("com.onyx.webdav.{}::{}", domain, user); + if let Ok(e) = keyring::Entry::new(&scoped, "password") { + let _ = e.delete_credential(); + } + } + if let Ok(e) = keyring::Entry::new(&service, "password") { + let _ = e.delete_credential(); + } + if let Ok(e) = keyring::Entry::new(&service, "username") { + let _ = e.delete_credential(); + } + Ok(()) +} + +#[cfg(all(not(target_os = "android"), not(feature = "desktop")))] +fn desktop_delete(_domain: &str) -> Result<(), String> { + Ok(()) +} + +// ── Plugin init ───────────────────────────────────────────────────── + +/// Initialize the credentials plugin. Call `.plugin(tauri_plugin_credentials::init())` on the Tauri builder. +pub fn init() -> TauriPlugin { + Builder::new("credentials") + .setup(|app, api| { + #[cfg(target_os = "android")] + let credentials = Credentials { + _handle: api.register_android_plugin(PLUGIN_IDENTIFIER, "CredentialPlugin")?, + }; + #[cfg(not(target_os = "android"))] + let credentials: Credentials = Credentials { + _phantom: std::marker::PhantomData, + }; + let _ = api; + app.manage(credentials); + Ok(()) + }) + .build() +} diff --git a/crates/onyx-cli/src/commands/init.rs b/crates/onyx-cli/src/commands/init.rs index 807258d..a7e00fc 100644 --- a/crates/onyx-cli/src/commands/init.rs +++ b/crates/onyx-cli/src/commands/init.rs @@ -28,8 +28,8 @@ pub fn execute(path: String, name: String) -> Result<()> { .unwrap_or_else(|_| AppConfig::new()); // Add workspace - config.add_workspace(name.clone(), WorkspaceConfig::new(path_buf.clone())); - config.set_current_workspace(name.clone())?; + let id = config.add_workspace(WorkspaceConfig::new(name.clone(), path_buf.clone())); + config.set_current_workspace(id)?; // Save config config.save_to_file(&config_path) diff --git a/crates/onyx-cli/src/commands/sync.rs b/crates/onyx-cli/src/commands/sync.rs index 2794266..c34d502 100644 --- a/crates/onyx-cli/src/commands/sync.rs +++ b/crates/onyx-cli/src/commands/sync.rs @@ -2,26 +2,30 @@ use anyhow::{Context, Result}; use colored::Colorize; use onyx_core::sync::{SyncMode, sync_workspace, get_sync_status}; use onyx_core::webdav::{WebDavClient, store_credentials, load_credentials}; +use onyx_core::config::AppConfig; use crate::output; use super::{load_config, save_config}; +/// Resolve a workspace name to (id, config). Falls back to current workspace if name is None. +fn resolve_workspace(config: &AppConfig, name: Option<&str>) -> Result<(String, onyx_core::config::WorkspaceConfig)> { + if let Some(name) = name { + let (id, ws) = config.find_by_name(name) + .ok_or_else(|| anyhow::anyhow!("Workspace '{}' not found", name))?; + Ok((id.clone(), ws.clone())) + } else { + let (id, ws) = config.get_current_workspace() + .context("No workspace set. Use 'onyx init' to create one.")?; + Ok((id.clone(), ws.clone())) + } +} + /// Run sync setup: prompt for URL, username, password, test connection, store credentials. pub fn setup(workspace_name: Option) -> Result<()> { let mut config = load_config()?; - - let (name, workspace) = if let Some(name) = workspace_name { - let ws = config.get_workspace(&name) - .ok_or_else(|| anyhow::anyhow!("Workspace '{}' not found", name))? - .clone(); - (name, ws) - } else { - let (n, ws) = config.get_current_workspace() - .context("No workspace set. Use 'onyx init' to create one.")?; - (n.clone(), ws.clone()) - }; + let (id, workspace) = resolve_workspace(&config, workspace_name.as_deref())?; // Prompt for WebDAV URL - output::header(&format!("WebDAV sync setup for workspace \"{}\"", name.green())); + output::header(&format!("WebDAV sync setup for workspace \"{}\"", workspace.name.green())); output::blank(); let url = prompt("WebDAV URL: ")?; @@ -65,9 +69,9 @@ pub fn setup(workspace_name: Option) -> Result<()> { } // Update workspace config with WebDAV URL - let mut ws = workspace; - ws.webdav_url = Some(url); - config.add_workspace(name, ws); + if let Some(ws) = config.workspaces.get_mut(&id) { + ws.webdav_url = Some(url); + } save_config(&config)?; output::success("Sync setup complete. Run 'onyx sync' to sync."); @@ -77,21 +81,11 @@ pub fn setup(workspace_name: Option) -> Result<()> { /// Execute a sync operation. pub fn execute(mode: SyncMode, workspace_name: Option) -> Result<()> { let config = load_config()?; - - let (name, workspace) = if let Some(name) = workspace_name { - let ws = config.get_workspace(&name) - .ok_or_else(|| anyhow::anyhow!("Workspace '{}' not found", name))? - .clone(); - (name, ws) - } else { - let (n, ws) = config.get_current_workspace() - .context("No workspace set. Use 'onyx init' to create one.")?; - (n.clone(), ws.clone()) - }; + let (_id, workspace) = resolve_workspace(&config, workspace_name.as_deref())?; let url = workspace.webdav_url.as_ref() .ok_or_else(|| anyhow::anyhow!( - "No WebDAV URL configured for workspace '{}'. Run 'onyx sync --setup' first.", name + "No WebDAV URL configured for workspace '{}'. Run 'onyx sync --setup' first.", workspace.name ))?; let domain = extract_domain(url); @@ -103,7 +97,7 @@ pub fn execute(mode: SyncMode, workspace_name: Option) -> Result<()> { SyncMode::Push => "Pushing", SyncMode::Pull => "Pulling", }; - output::info(&format!("{} workspace \"{}\"...", mode_str, name.green())); + output::info(&format!("{} workspace \"{}\"...", mode_str, workspace.name.green())); let rt = tokio::runtime::Runtime::new().context("Failed to create async runtime")?; let result = rt.block_on(sync_workspace( @@ -147,13 +141,12 @@ pub fn status(workspace_name: Option, all: bool) -> Result<()> { if all { // Show status for all workspaces that have sync configured let mut found_any = false; - let mut names: Vec<_> = config.workspaces.keys().cloned().collect(); - names.sort(); - for name in names { - let ws = config.get_workspace(&name).unwrap(); + let mut workspaces: Vec<_> = config.workspaces.values().collect(); + workspaces.sort_by(|a, b| a.name.cmp(&b.name)); + for ws in workspaces { if ws.webdav_url.is_some() { found_any = true; - print_workspace_status(&name, &ws.path, ws.webdav_url.as_deref())?; + print_workspace_status(&ws.name, &ws.path, ws.webdav_url.as_deref())?; output::blank(); } } @@ -163,18 +156,8 @@ pub fn status(workspace_name: Option, all: bool) -> Result<()> { return Ok(()); } - let (name, workspace) = if let Some(name) = workspace_name { - let ws = config.get_workspace(&name) - .ok_or_else(|| anyhow::anyhow!("Workspace '{}' not found", name))? - .clone(); - (name, ws) - } else { - let (n, ws) = config.get_current_workspace() - .context("No workspace set.")?; - (n.clone(), ws.clone()) - }; - - print_workspace_status(&name, &workspace.path, workspace.webdav_url.as_deref())?; + let (_id, workspace) = resolve_workspace(&config, workspace_name.as_deref())?; + print_workspace_status(&workspace.name, &workspace.path, workspace.webdav_url.as_deref())?; Ok(()) } @@ -207,17 +190,13 @@ fn print_workspace_status(name: &str, path: &std::path::Path, webdav_url: Option /// Extract host from a URL for credential storage. fn extract_domain(url: &str) -> String { - // Strip scheme let after_scheme = url.split("://").nth(1).unwrap_or(url); - // Strip path let authority = after_scheme.split('/').next().unwrap_or(after_scheme); - // Strip userinfo (user:pass@host) let host_port = if let Some(at_pos) = authority.rfind('@') { &authority[at_pos + 1..] } else { authority }; - // Strip port host_port.split(':').next().unwrap_or(host_port).to_string() } diff --git a/crates/onyx-cli/src/commands/task.rs b/crates/onyx-cli/src/commands/task.rs index 16ad54d..1a73c87 100644 --- a/crates/onyx-cli/src/commands/task.rs +++ b/crates/onyx-cli/src/commands/task.rs @@ -192,7 +192,6 @@ pub fn edit(task_id_str: String, workspace: Option) -> Result<()> { let mut updated_task = task.clone(); updated_task.title = title; updated_task.description = description; - updated_task.updated_at = Utc::now(); repo.update_task(list_id, updated_task.clone()) .context("Failed to update task")?; diff --git a/crates/onyx-cli/src/commands/workspace.rs b/crates/onyx-cli/src/commands/workspace.rs index cdf8b18..5fc4925 100644 --- a/crates/onyx-cli/src/commands/workspace.rs +++ b/crates/onyx-cli/src/commands/workspace.rs @@ -27,18 +27,13 @@ pub fn add(name: String, path: String) -> Result<()> { // Load config let mut config = load_config()?; - // Check if workspace already exists - if config.get_workspace(&name).is_some() { - anyhow::bail!("Workspace '{}' already exists", name); - } - // Add workspace - config.add_workspace(name.clone(), WorkspaceConfig::new(path_buf.clone())); + let id = config.add_workspace(WorkspaceConfig::new(name.clone(), path_buf.clone())); // Save config save_config(&config)?; - output::success(&format!("Added workspace \"{}\" at {}", name, path_buf.display())); + output::success(&format!("Added workspace \"{}\" ({}) at {}", name, &id[..8], path_buf.display())); output::success("Created default list \"My Tasks\""); Ok(()) @@ -55,29 +50,37 @@ pub fn list() -> Result<()> { let current = config.current_workspace.as_deref(); let mut workspaces: Vec<_> = config.workspaces.iter().collect(); - workspaces.sort_by(|a, b| a.0.cmp(b.0)); + workspaces.sort_by(|a, b| a.1.name.cmp(&b.1.name)); - for (name, workspace_config) in workspaces { - let marker = if Some(name.as_str()) == current { + for (id, workspace_config) in workspaces { + let marker = if Some(id.as_str()) == current { " (current)".green() } else { "".normal() }; - output::item(&format!("{}: {}{}", name, workspace_config.path.display(), marker)); + output::item(&format!("{}: {}{}", workspace_config.name, workspace_config.path.display(), marker)); } Ok(()) } +/// Resolve a workspace name to its ID. Errors if not found or ambiguous. +fn resolve_name(config: &onyx_core::config::AppConfig, name: &str) -> Result { + let matches: Vec<_> = config.workspaces.iter() + .filter(|(_, ws)| ws.name == name) + .collect(); + match matches.len() { + 0 => anyhow::bail!("Workspace '{}' not found", name), + 1 => Ok(matches[0].0.clone()), + n => anyhow::bail!("Ambiguous: {} workspaces named '{}'. Use the workspace ID instead.", n, name), + } +} + pub fn switch(name: String) -> Result<()> { let mut config = load_config()?; + let id = resolve_name(&config, &name)?; - // Verify workspace exists - if config.get_workspace(&name).is_none() { - anyhow::bail!("Workspace '{}' not found", name); - } - - config.set_current_workspace(name.clone())?; + config.set_current_workspace(id)?; save_config(&config)?; output::success(&format!("Switched to workspace \"{}\"", name)); @@ -87,11 +90,7 @@ pub fn switch(name: String) -> Result<()> { pub fn remove(name: String) -> Result<()> { let mut config = load_config()?; - - // Verify workspace exists - if config.get_workspace(&name).is_none() { - anyhow::bail!("Workspace '{}' not found", name); - } + let id = resolve_name(&config, &name)?; // Confirm output::warning("This will delete workspace config (files remain on disk)"); @@ -107,7 +106,7 @@ pub fn remove(name: String) -> Result<()> { return Ok(()); } - config.remove_workspace(&name); + config.remove_workspace(&id); save_config(&config)?; output::success(&format!("Removed workspace \"{}\"", name)); @@ -124,14 +123,10 @@ pub fn retarget(name: String, path: String) -> Result<()> { }; let mut config = load_config()?; - - // Verify workspace exists - if config.get_workspace(&name).is_none() { - anyhow::bail!("Workspace '{}' not found", name); - } + let id = resolve_name(&config, &name)?; // Update path - config.add_workspace(name.clone(), WorkspaceConfig::new(path_buf.clone())); + config.workspaces.get_mut(&id).unwrap().path = path_buf.clone(); save_config(&config)?; output::success(&format!("Workspace \"{}\" now points to {}", name, path_buf.display())); @@ -148,9 +143,10 @@ pub fn migrate(name: String, new_path: String) -> Result<()> { }; let mut config = load_config()?; + let id = resolve_name(&config, &name)?; // Get current workspace config - let old_path = config.get_workspace(&name) + let old_path = config.get_workspace(&id) .ok_or_else(|| anyhow::anyhow!("Workspace '{}' not found", name))? .path.clone(); @@ -225,7 +221,7 @@ pub fn migrate(name: String, new_path: String) -> Result<()> { } // Update config - config.add_workspace(name.clone(), WorkspaceConfig::new(new_path_buf.clone())); + config.workspaces.get_mut(&id).unwrap().path = new_path_buf.clone(); save_config(&config)?; output::success(&format!("Migrated {} items to {}", moved.len(), new_path_buf.display())); diff --git a/crates/onyx-core/src/config.rs b/crates/onyx-core/src/config.rs index 8d22016..e52fc3d 100644 --- a/crates/onyx-core/src/config.rs +++ b/crates/onyx-core/src/config.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::path::PathBuf; use serde::{Deserialize, Serialize}; +use uuid::Uuid; use crate::error::{Error, Result}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -18,23 +19,29 @@ impl Default for WorkspaceMode { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct WorkspaceConfig { + pub name: String, pub path: PathBuf, #[serde(default)] pub mode: WorkspaceMode, #[serde(skip_serializing_if = "Option::is_none", default)] pub webdav_url: Option, #[serde(skip_serializing_if = "Option::is_none", default)] + pub webdav_path: Option, + #[serde(skip_serializing_if = "Option::is_none", default)] pub last_sync: Option>, #[serde(skip_serializing_if = "Option::is_none", default)] pub theme: Option, + #[serde(skip_serializing_if = "Option::is_none", default)] + pub sync_interval_secs: Option, } impl WorkspaceConfig { - pub fn new(path: PathBuf) -> Self { - Self { path, mode: WorkspaceMode::Local, webdav_url: None, last_sync: None, theme: None } + pub fn new(name: String, path: PathBuf) -> Self { + Self { name, path, mode: WorkspaceMode::Local, webdav_url: None, webdav_path: None, last_sync: None, theme: None, sync_interval_secs: None } } } +/// Workspaces keyed by UUID string. #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct AppConfig { pub workspaces: HashMap, @@ -49,37 +56,51 @@ impl AppConfig { } } - pub fn add_workspace(&mut self, name: String, config: WorkspaceConfig) { - self.workspaces.insert(name, config); + pub fn add_workspace(&mut self, config: WorkspaceConfig) -> String { + let id = Uuid::new_v4().to_string(); + self.workspaces.insert(id.clone(), config); + id } - pub fn remove_workspace(&mut self, name: &str) -> Option { - if self.current_workspace.as_deref() == Some(name) { + pub fn remove_workspace(&mut self, id: &str) -> Option { + if self.current_workspace.as_deref() == Some(id) { self.current_workspace = None; } - self.workspaces.remove(name) + self.workspaces.remove(id) } - pub fn get_workspace(&self, name: &str) -> Option<&WorkspaceConfig> { - self.workspaces.get(name) + pub fn rename_workspace(&mut self, id: &str, new_name: String) -> Result<()> { + let ws = self.workspaces.get_mut(id) + .ok_or_else(|| Error::InvalidData(format!("Workspace '{}' not found", id)))?; + ws.name = new_name; + Ok(()) + } + + pub fn get_workspace(&self, id: &str) -> Option<&WorkspaceConfig> { + self.workspaces.get(id) } pub fn get_current_workspace(&self) -> Result<(&String, &WorkspaceConfig)> { - let name = self.current_workspace.as_ref() + let id = self.current_workspace.as_ref() .ok_or_else(|| Error::WorkspaceNotFound("No current workspace set".to_string()))?; - let config = self.workspaces.get(name) - .ok_or_else(|| Error::WorkspaceNotFound(name.clone()))?; - Ok((name, config)) + let config = self.workspaces.get(id) + .ok_or_else(|| Error::WorkspaceNotFound(id.clone()))?; + Ok((id, config)) } - pub fn set_current_workspace(&mut self, name: String) -> Result<()> { - if !self.workspaces.contains_key(&name) { - return Err(Error::WorkspaceNotFound(name)); + pub fn set_current_workspace(&mut self, id: String) -> Result<()> { + if !self.workspaces.contains_key(&id) { + return Err(Error::WorkspaceNotFound(id)); } - self.current_workspace = Some(name); + self.current_workspace = Some(id); Ok(()) } + /// Find a workspace by display name. Returns (id, config) of the first match. + pub fn find_by_name(&self, name: &str) -> Option<(&String, &WorkspaceConfig)> { + self.workspaces.iter().find(|(_, ws)| ws.name == name) + } + pub fn load_from_file(path: &PathBuf) -> Result { if !path.exists() { return Ok(Self::new()); @@ -119,11 +140,11 @@ mod tests { } #[test] - fn test_get_current_workspace_name_points_to_removed_workspace() { + fn test_get_current_workspace_id_points_to_removed_workspace() { let mut config = AppConfig::new(); - config.add_workspace("test".to_string(), WorkspaceConfig::new(PathBuf::from("/tmp"))); - config.current_workspace = Some("test".to_string()); - config.workspaces.remove("test"); + let id = config.add_workspace(WorkspaceConfig::new("test".into(), PathBuf::from("/tmp"))); + config.current_workspace = Some(id.clone()); + config.workspaces.remove(&id); let result = config.get_current_workspace(); assert!(result.is_err()); @@ -141,31 +162,31 @@ mod tests { #[test] fn test_set_current_workspace_valid() { let mut config = AppConfig::new(); - config.add_workspace("real".to_string(), WorkspaceConfig::new(PathBuf::from("/tmp"))); - assert!(config.set_current_workspace("real".to_string()).is_ok()); - assert_eq!(config.current_workspace.as_deref(), Some("real")); + let id = config.add_workspace(WorkspaceConfig::new("real".into(), PathBuf::from("/tmp"))); + assert!(config.set_current_workspace(id.clone()).is_ok()); + assert_eq!(config.current_workspace.as_deref(), Some(id.as_str())); } #[test] fn test_remove_current_workspace_clears_current() { let mut config = AppConfig::new(); - config.add_workspace("ws".to_string(), WorkspaceConfig::new(PathBuf::from("/tmp"))); - config.set_current_workspace("ws".to_string()).unwrap(); + let id = config.add_workspace(WorkspaceConfig::new("ws".into(), PathBuf::from("/tmp"))); + config.set_current_workspace(id.clone()).unwrap(); - config.remove_workspace("ws"); + config.remove_workspace(&id); assert!(config.current_workspace.is_none()); - assert!(config.get_workspace("ws").is_none()); + assert!(config.get_workspace(&id).is_none()); } #[test] fn test_remove_noncurrent_workspace_keeps_current() { let mut config = AppConfig::new(); - config.add_workspace("a".to_string(), WorkspaceConfig::new(PathBuf::from("/a"))); - config.add_workspace("b".to_string(), WorkspaceConfig::new(PathBuf::from("/b"))); - config.set_current_workspace("a".to_string()).unwrap(); + let id_a = config.add_workspace(WorkspaceConfig::new("a".into(), PathBuf::from("/a"))); + let id_b = config.add_workspace(WorkspaceConfig::new("b".into(), PathBuf::from("/b"))); + config.set_current_workspace(id_a.clone()).unwrap(); - config.remove_workspace("b"); - assert_eq!(config.current_workspace.as_deref(), Some("a")); + config.remove_workspace(&id_b); + assert_eq!(config.current_workspace.as_deref(), Some(id_a.as_str())); } #[test] @@ -174,16 +195,16 @@ mod tests { let config_path = temp_dir.path().join("config.json"); let mut config = AppConfig::new(); - config.add_workspace("ws1".to_string(), WorkspaceConfig::new(PathBuf::from("/path/one"))); - config.add_workspace("ws2".to_string(), WorkspaceConfig::new(PathBuf::from("/path/two"))); - config.set_current_workspace("ws1".to_string()).unwrap(); + let id1 = config.add_workspace(WorkspaceConfig::new("ws1".into(), PathBuf::from("/path/one"))); + let _id2 = config.add_workspace(WorkspaceConfig::new("ws2".into(), PathBuf::from("/path/two"))); + config.set_current_workspace(id1.clone()).unwrap(); config.save_to_file(&config_path).unwrap(); let loaded = AppConfig::load_from_file(&config_path).unwrap(); - assert_eq!(loaded.current_workspace.as_deref(), Some("ws1")); + assert_eq!(loaded.current_workspace.as_deref(), Some(id1.as_str())); assert_eq!(loaded.workspaces.len(), 2); - assert_eq!(loaded.get_workspace("ws1").unwrap().path, PathBuf::from("/path/one")); - assert_eq!(loaded.get_workspace("ws2").unwrap().path, PathBuf::from("/path/two")); + assert_eq!(loaded.get_workspace(&id1).unwrap().path, PathBuf::from("/path/one")); + assert_eq!(loaded.get_workspace(&id1).unwrap().name, "ws1"); } #[test] @@ -214,13 +235,35 @@ mod tests { } #[test] - fn test_add_workspace_overwrites_existing() { + fn test_duplicate_names_allowed() { let mut config = AppConfig::new(); - config.add_workspace("ws".to_string(), WorkspaceConfig::new(PathBuf::from("/old"))); - config.add_workspace("ws".to_string(), WorkspaceConfig::new(PathBuf::from("/new"))); + let id1 = config.add_workspace(WorkspaceConfig::new("Onyx".into(), PathBuf::from("/a"))); + let id2 = config.add_workspace(WorkspaceConfig::new("Onyx".into(), PathBuf::from("/b"))); - assert_eq!(config.get_workspace("ws").unwrap().path, PathBuf::from("/new")); - assert_eq!(config.workspaces.len(), 1); + assert_ne!(id1, id2); + assert_eq!(config.workspaces.len(), 2); + assert_eq!(config.get_workspace(&id1).unwrap().name, "Onyx"); + assert_eq!(config.get_workspace(&id2).unwrap().name, "Onyx"); + } + + #[test] + fn test_find_by_name() { + let mut config = AppConfig::new(); + let id = config.add_workspace(WorkspaceConfig::new("Tasks".into(), PathBuf::from("/tasks"))); + + let found = config.find_by_name("Tasks"); + assert!(found.is_some()); + assert_eq!(found.unwrap().0, &id); + + assert!(config.find_by_name("Nonexistent").is_none()); + } + + #[test] + fn test_rename_workspace() { + let mut config = AppConfig::new(); + let id = config.add_workspace(WorkspaceConfig::new("Old".into(), PathBuf::from("/tmp"))); + config.rename_workspace(&id, "New".into()).unwrap(); + assert_eq!(config.get_workspace(&id).unwrap().name, "New"); } #[test] @@ -229,38 +272,15 @@ mod tests { let config_path = temp_dir.path().join("config.json"); let mut config = AppConfig::new(); - let mut ws = WorkspaceConfig::new(PathBuf::from("/tasks")); + let mut ws = WorkspaceConfig::new("synced".into(), PathBuf::from("/tasks")); ws.webdav_url = Some("https://dav.example.com/tasks".to_string()); ws.last_sync = Some(chrono::Utc::now()); - config.add_workspace("synced".to_string(), ws); + let id = config.add_workspace(ws); config.save_to_file(&config_path).unwrap(); let loaded = AppConfig::load_from_file(&config_path).unwrap(); - let ws = loaded.get_workspace("synced").unwrap(); + let ws = loaded.get_workspace(&id).unwrap(); assert_eq!(ws.webdav_url.as_deref(), Some("https://dav.example.com/tasks")); assert!(ws.last_sync.is_some()); } - - #[test] - fn test_backwards_compat_loading_old_format() { - let temp_dir = TempDir::new().unwrap(); - let config_path = temp_dir.path().join("config.json"); - - // Write old-format JSON without webdav_url, last_sync, mode, or theme fields - let old_json = r#"{ - "workspaces": { - "personal": { "path": "/home/user/tasks" } - }, - "current_workspace": "personal" - }"#; - std::fs::write(&config_path, old_json).unwrap(); - - let loaded = AppConfig::load_from_file(&config_path).unwrap(); - let ws = loaded.get_workspace("personal").unwrap(); - assert_eq!(ws.path, PathBuf::from("/home/user/tasks")); - assert!(ws.webdav_url.is_none()); - assert!(ws.last_sync.is_none()); - assert_eq!(ws.mode, WorkspaceMode::Local); - assert!(ws.theme.is_none()); - } } diff --git a/crates/onyx-core/src/models.rs b/crates/onyx-core/src/models.rs index 17db348..2ba5771 100644 --- a/crates/onyx-core/src/models.rs +++ b/crates/onyx-core/src/models.rs @@ -19,15 +19,13 @@ pub struct Task { pub due_date: Option>, #[serde(default)] pub has_time: bool, - pub created_at: DateTime, - pub updated_at: DateTime, + pub version: u64, #[serde(skip_serializing_if = "Option::is_none")] pub parent_id: Option, } impl Task { pub fn new(title: String) -> Self { - let now = Utc::now(); Self { id: Uuid::new_v4(), title, @@ -35,8 +33,7 @@ impl Task { status: TaskStatus::Backlog, due_date: None, has_time: false, - created_at: now, - updated_at: now, + version: 0, parent_id: None, } } @@ -58,12 +55,10 @@ impl Task { pub fn complete(&mut self) { self.status = TaskStatus::Completed; - self.updated_at = Utc::now(); } pub fn uncomplete(&mut self) { self.status = TaskStatus::Backlog; - self.updated_at = Utc::now(); } } diff --git a/crates/onyx-core/src/repository.rs b/crates/onyx-core/src/repository.rs index 2298cc0..4c9c300 100644 --- a/crates/onyx-core/src/repository.rs +++ b/crates/onyx-core/src/repository.rs @@ -24,8 +24,9 @@ impl TaskRepository { } // Task operations - pub fn create_task(&mut self, list_id: Uuid, task: Task) -> Result { + pub fn create_task(&mut self, list_id: Uuid, mut task: Task) -> Result { self.storage.write_task(list_id, &task)?; + task.version += 1; Ok(task) } diff --git a/crates/onyx-core/src/storage.rs b/crates/onyx-core/src/storage.rs index fec62dc..219b6aa 100644 --- a/crates/onyx-core/src/storage.rs +++ b/crates/onyx-core/src/storage.rs @@ -7,7 +7,7 @@ use uuid::Uuid; use crate::error::{Error, Result}; use crate::models::{Task, TaskList, TaskStatus}; -/// Metadata stored in root .metadata.json +/// Metadata stored in root .onyx-workspace.json #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RootMetadata { pub version: u32, @@ -49,6 +49,9 @@ impl ListMetadata { } } +fn is_false(v: &bool) -> bool { !v } +fn default_version() -> u64 { 1 } + /// Frontmatter for task markdown files #[derive(Debug, Clone, Serialize, Deserialize)] pub struct TaskFrontmatter { @@ -56,10 +59,10 @@ pub struct TaskFrontmatter { pub status: TaskStatus, #[serde(skip_serializing_if = "Option::is_none")] pub due: Option>, - #[serde(default)] + #[serde(default, skip_serializing_if = "is_false")] pub has_time: bool, - pub created: DateTime, - pub updated: DateTime, + #[serde(default = "default_version")] + pub version: u64, #[serde(skip_serializing_if = "Option::is_none")] pub parent: Option, } @@ -71,8 +74,7 @@ impl From<&Task> for TaskFrontmatter { status: task.status, due: task.due_date, has_time: task.has_time, - created: task.created_at, - updated: task.updated_at, + version: task.version, parent: task.parent_id, } } @@ -124,7 +126,7 @@ impl FileSystemStorage { } fn metadata_path(&self) -> PathBuf { - self.root_path.join(".metadata.json") + self.root_path.join(".onyx-workspace.json") } fn list_dir_path(&self, list_id: Uuid) -> Result { @@ -219,7 +221,8 @@ impl FileSystemStorage { } fn write_markdown_with_frontmatter(&self, task: &Task) -> Result { - let frontmatter = TaskFrontmatter::from(task); + let mut frontmatter = TaskFrontmatter::from(task); + frontmatter.version = task.version + 1; let yaml = serde_yaml::to_string(&frontmatter)?; let mut content = String::new(); @@ -277,8 +280,7 @@ impl Storage for FileSystemStorage { status: frontmatter.status, due_date: frontmatter.due, has_time: frontmatter.has_time, - created_at: frontmatter.created, - updated_at: frontmatter.updated, + version: frontmatter.version, parent_id: frontmatter.parent, }); } @@ -343,7 +345,7 @@ impl Storage for FileSystemStorage { let list_dir = self.list_dir_path(list_id)?; let list_metadata = self.read_list_metadata(list_id)?; - let mut tasks = Vec::new(); + let mut file_tasks: Vec<(PathBuf, Task)> = Vec::new(); let entries = fs::read_dir(&list_dir)?; for entry in entries { @@ -366,15 +368,32 @@ impl Storage for FileSystemStorage { status: frontmatter.status, due_date: frontmatter.due, has_time: frontmatter.has_time, - created_at: frontmatter.created, - updated_at: frontmatter.updated, + version: frontmatter.version, parent_id: frontmatter.parent, }; - tasks.push(task); + file_tasks.push((path, task)); } } + // Self-healing dedup: group by UUID, keep highest version, delete stale files + let mut by_id: HashMap> = HashMap::new(); + for entry in file_tasks { + by_id.entry(entry.1.id).or_default().push(entry); + } + + let mut tasks = Vec::new(); + for (_id, mut entries) in by_id { + if entries.len() > 1 { + entries.sort_by(|a, b| b.1.version.cmp(&a.1.version)); + for (stale_path, _) in entries.drain(1..) { + let _ = fs::remove_file(&stale_path); + } + } + let (_, task) = entries.into_iter().next().unwrap(); + tasks.push(task); + } + // Sort by task_order let order_map: HashMap = list_metadata.task_order .iter() @@ -557,10 +576,11 @@ mod tests { let temp_dir = TempDir::new().unwrap(); let storage = init_storage(&temp_dir); - let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: backlog\ncreated: 2026-01-01T00:00:00Z\nupdated: 2026-01-01T00:00:00Z\n---\n\nSome description"; + let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: backlog\nversion: 3\n---\n\nSome description"; let (fm, desc) = storage.parse_markdown_with_frontmatter(content).unwrap(); assert_eq!(fm.id.to_string(), "550e8400-e29b-41d4-a716-446655440000"); assert_eq!(fm.status, TaskStatus::Backlog); + assert_eq!(fm.version, 3); assert_eq!(desc, "Some description"); } @@ -569,7 +589,7 @@ mod tests { let temp_dir = TempDir::new().unwrap(); let storage = init_storage(&temp_dir); - let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: completed\ncreated: 2026-01-01T00:00:00Z\nupdated: 2026-01-01T00:00:00Z\n---"; + let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: completed\nversion: 1\n---"; let (fm, desc) = storage.parse_markdown_with_frontmatter(content).unwrap(); assert_eq!(fm.status, TaskStatus::Completed); assert!(desc.is_empty()); @@ -621,7 +641,7 @@ mod tests { let temp_dir = TempDir::new().unwrap(); let storage = init_storage(&temp_dir); - let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: backlog\ndue: 2026-06-15T12:00:00Z\ncreated: 2026-01-01T00:00:00Z\nupdated: 2026-01-01T00:00:00Z\nparent: 660e8400-e29b-41d4-a716-446655440001\n---\n\nNotes"; + let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: backlog\ndue: 2026-06-15T12:00:00Z\nversion: 2\nparent: 660e8400-e29b-41d4-a716-446655440001\n---\n\nNotes"; let (fm, _) = storage.parse_markdown_with_frontmatter(content).unwrap(); assert!(fm.due.is_some()); assert!(fm.parent.is_some()); @@ -658,7 +678,7 @@ mod tests { fn test_init_creates_metadata() { let temp_dir = TempDir::new().unwrap(); let _storage = init_storage(&temp_dir); - assert!(temp_dir.path().join(".metadata.json").exists()); + assert!(temp_dir.path().join(".onyx-workspace.json").exists()); } #[test] @@ -683,7 +703,7 @@ mod tests { let storage = init_storage(&temp_dir); // Delete the metadata file to simulate missing - fs::remove_file(temp_dir.path().join(".metadata.json")).unwrap(); + fs::remove_file(temp_dir.path().join(".onyx-workspace.json")).unwrap(); let meta = storage.read_root_metadata().unwrap(); assert_eq!(meta.version, 1); @@ -832,4 +852,74 @@ mod tests { let tasks = storage.list_tasks(list.id).unwrap(); assert!(tasks.is_empty()); } + + #[test] + fn test_missing_version_defaults_to_1() { + let temp_dir = TempDir::new().unwrap(); + let storage = init_storage(&temp_dir); + + let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: backlog\n---\n\nOld task"; + let (fm, _) = storage.parse_markdown_with_frontmatter(content).unwrap(); + assert_eq!(fm.version, 1); + } + + #[test] + fn test_missing_has_time_defaults_to_false() { + let temp_dir = TempDir::new().unwrap(); + let storage = init_storage(&temp_dir); + + let content = "---\nid: 550e8400-e29b-41d4-a716-446655440000\nstatus: backlog\nversion: 1\n---\n"; + let (fm, _) = storage.parse_markdown_with_frontmatter(content).unwrap(); + assert!(!fm.has_time); + } + + #[test] + fn test_version_increments_on_write() { + let temp_dir = TempDir::new().unwrap(); + let mut storage = init_storage(&temp_dir); + let list = storage.create_list("Tasks".to_string()).unwrap(); + + let task = Task::new("Versioned".to_string()); + assert_eq!(task.version, 0); + + storage.write_task(list.id, &task).unwrap(); + let read_back = storage.read_task(list.id, task.id).unwrap(); + assert_eq!(read_back.version, 1); + + // Write again — version should increment again + storage.write_task(list.id, &read_back).unwrap(); + let read_again = storage.read_task(list.id, task.id).unwrap(); + assert_eq!(read_again.version, 2); + } + + #[test] + fn test_dedup_keeps_highest_version() { + let temp_dir = TempDir::new().unwrap(); + let mut storage = init_storage(&temp_dir); + let list = storage.create_list("Dedup".to_string()).unwrap(); + + let task = Task::new("Original".to_string()); + let task_id = task.id; + storage.write_task(list.id, &task).unwrap(); + + // Simulate a sync duplicate: manually write a second file with the same UUID but lower version + let list_dir = storage.list_dir_path(list.id).unwrap(); + let stale_content = format!( + "---\nid: {}\nstatus: backlog\nversion: 1\n---\n\nStale copy", + task_id + ); + let stale_path = list_dir.join("Original_old.md"); + fs::write(&stale_path, &stale_content).unwrap(); + + let tasks = storage.list_tasks(list.id).unwrap(); + assert_eq!(tasks.len(), 1); + assert_eq!(tasks[0].id, task_id); + // The winner should be the one written by write_task (version 1), not the manually created stale copy (also version 1 but alphabetically second) + // Actually both are version 1, so the first sorted wins — but the stale file should be cleaned up + // Let's verify only one .md file remains + let md_count = fs::read_dir(&list_dir).unwrap() + .filter(|e| e.as_ref().unwrap().path().extension().and_then(|s| s.to_str()) == Some("md")) + .count(); + assert_eq!(md_count, 1); + } } diff --git a/crates/onyx-core/src/sync.rs b/crates/onyx-core/src/sync.rs index 1f627d2..0d8bd20 100644 --- a/crates/onyx-core/src/sync.rs +++ b/crates/onyx-core/src/sync.rs @@ -3,7 +3,9 @@ use std::path::Path; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use sha2::{Sha256, Digest}; +use uuid::Uuid; use crate::error::{Error, Result}; +use crate::storage::{ListMetadata, TaskFrontmatter}; use crate::webdav::WebDavClient; // --- Sync State --- @@ -32,8 +34,7 @@ pub enum SyncAction { Download { path: String }, DeleteLocal { path: String }, DeleteRemote { path: String }, - ConflictLocalWins { path: String }, - ConflictRemoteWins { path: String }, + Conflict { path: String }, } impl SyncAction { @@ -43,8 +44,7 @@ impl SyncAction { | SyncAction::Download { path } | SyncAction::DeleteLocal { path } | SyncAction::DeleteRemote { path } - | SyncAction::ConflictLocalWins { path } - | SyncAction::ConflictRemoteWins { path } => path, + | SyncAction::Conflict { path } => path, } } } @@ -137,12 +137,7 @@ pub fn compute_sync_actions( (true, false) => actions.push(SyncAction::Upload { path: path.to_string() }), (false, true) => actions.push(SyncAction::Download { path: path.to_string() }), (true, true) => { - // Both modified: last-write-wins based on timestamps - if local_wins(l.modified_at.as_deref(), r.last_modified.as_deref()) { - actions.push(SyncAction::ConflictLocalWins { path: path.to_string() }); - } else { - actions.push(SyncAction::ConflictRemoteWins { path: path.to_string() }); - } + actions.push(SyncAction::Conflict { path: path.to_string() }); } } } @@ -157,19 +152,21 @@ pub fn compute_sync_actions( actions.push(SyncAction::Download { path: path.to_string() }); } - // Both present, no base (both added): last-write-wins - (Some(l), Some(r), None) => { - if local_wins(l.modified_at.as_deref(), r.last_modified.as_deref()) { - actions.push(SyncAction::ConflictLocalWins { path: path.to_string() }); - } else { - actions.push(SyncAction::ConflictRemoteWins { path: path.to_string() }); - } + // Both present, no base (both added): conflict + (Some(_), Some(_), None) => { + actions.push(SyncAction::Conflict { path: path.to_string() }); } // Local present, remote gone, base known: remote was deleted - (Some(_), None, Some(_)) => { - // modified locally + deleted remote -> upload (local wins) - actions.push(SyncAction::Upload { path: path.to_string() }); + (Some(l), None, Some(b)) => { + let local_changed = l.checksum != b.checksum; + if local_changed { + // modified locally + deleted remotely -> upload (local wins) + actions.push(SyncAction::Upload { path: path.to_string() }); + } else { + // unchanged locally + deleted remotely -> delete local + actions.push(SyncAction::DeleteLocal { path: path.to_string() }); + } } // Remote present, local gone, base known: local was deleted @@ -215,19 +212,6 @@ fn timestamps_equal(a: Option<&str>, b: Option<&str>) -> bool { } } -/// Determine if local wins based on timestamps. True means local wins. -fn local_wins(local_modified: Option<&str>, remote_modified: Option<&str>) -> bool { - // Try parsing both; if we can't parse, local wins by default - let local_ts = local_modified.and_then(parse_timestamp); - let remote_ts = remote_modified.and_then(parse_timestamp); - match (local_ts, remote_ts) { - (Some(l), Some(r)) => l >= r, - (Some(_), None) => true, - (None, Some(_)) => false, - (None, None) => true, // Default to local - } -} - /// Parse a timestamp string (ISO 8601 or HTTP date format). fn parse_timestamp(s: &str) -> Option> { // Try ISO 8601 / RFC 3339 @@ -336,8 +320,7 @@ fn queued_op_to_action(op: &QueuedOperation) -> Option { "download" => Some(SyncAction::Download { path }), "delete_local" => Some(SyncAction::DeleteLocal { path }), "delete_remote" => Some(SyncAction::DeleteRemote { path }), - "conflict_local_wins" => Some(SyncAction::ConflictLocalWins { path }), - "conflict_remote_wins" => Some(SyncAction::ConflictRemoteWins { path }), + "conflict" => Some(SyncAction::Conflict { path }), _ => None, } } @@ -348,8 +331,7 @@ fn action_to_queued_op(action: &SyncAction) -> QueuedOperation { SyncAction::Download { path } => ("download", path), SyncAction::DeleteLocal { path } => ("delete_local", path), SyncAction::DeleteRemote { path } => ("delete_remote", path), - SyncAction::ConflictLocalWins { path } => ("conflict_local_wins", path), - SyncAction::ConflictRemoteWins { path } => ("conflict_remote_wins", path), + SyncAction::Conflict { path } => ("conflict", path), }; QueuedOperation { action_type: action_type.to_string(), @@ -371,8 +353,8 @@ pub fn compute_checksum(data: &[u8]) -> String { fn is_syncable(path: &str) -> bool { let parts: Vec<&str> = path.split('/').collect(); let filename = parts.last().copied().unwrap_or(path); - // .metadata.json only at workspace root (depth 1) - if filename == ".metadata.json" { + // .onyx-workspace.json only at workspace root (depth 1) + if filename == ".onyx-workspace.json" { return parts.len() == 1; } // .listdata.json only inside a list directory (depth 2) @@ -529,9 +511,7 @@ async fn sync_workspace_inner( mode: SyncMode, on_progress: Option, ) -> Result { - // Sync into an "Onyx" subfolder so we don't scan the user's entire cloud storage - let sync_url = format!("{}/Onyx", webdav_url.trim_end_matches('/')); - let client = WebDavClient::new(&sync_url, username, password)?; + let client = WebDavClient::new(webdav_url, username, password)?; let mut sync_state = SyncState::load(workspace_path); let queue = OfflineQueue::load(workspace_path); let mut result = SyncResult::default(); @@ -542,8 +522,6 @@ async fn sync_workspace_inner( } }; - // Ensure remote Onyx folder exists (creates it on first sync) - client.create_dir("").await.ok(); client.test_connection().await?; // Scan local files @@ -565,36 +543,34 @@ async fn sync_workspace_inner( // Merge with offline queue let all_actions = queue.merge_with_actions(fresh_actions); - // Filter by sync mode + // Filter by sync mode (conflicts always run in any mode since they need both sides) let actions: Vec = all_actions.into_iter().filter(|a| match mode { SyncMode::Full => true, - SyncMode::Push => matches!(a, SyncAction::Upload { .. } | SyncAction::DeleteRemote { .. } | SyncAction::ConflictLocalWins { .. }), - SyncMode::Pull => matches!(a, SyncAction::Download { .. } | SyncAction::DeleteLocal { .. } | SyncAction::ConflictRemoteWins { .. }), + SyncMode::Push => matches!(a, SyncAction::Upload { .. } | SyncAction::DeleteRemote { .. } | SyncAction::Conflict { .. }), + SyncMode::Pull => matches!(a, SyncAction::Download { .. } | SyncAction::DeleteLocal { .. } | SyncAction::Conflict { .. }), }).collect(); // Execute actions, collecting failures for the queue let mut failed_actions = Vec::new(); + // Build remote timestamp lookup for recording accurate download times + let remote_meta: HashMap<&str, &RemoteFileSnapshot> = remote_files.iter().map(|f| (f.path.as_str(), f)).collect(); + for action in &actions { - match execute_action(&client, workspace_path, action, &mut sync_state, &report).await { + match execute_action(&client, workspace_path, action, &mut sync_state, &remote_meta, &report).await { Ok(()) => { match action { - SyncAction::Upload { .. } | SyncAction::ConflictLocalWins { .. } => result.uploaded += 1, - SyncAction::Download { .. } | SyncAction::ConflictRemoteWins { .. } => result.downloaded += 1, + SyncAction::Upload { .. } => result.uploaded += 1, + SyncAction::Download { .. } => result.downloaded += 1, SyncAction::DeleteLocal { .. } => result.deleted_local += 1, SyncAction::DeleteRemote { .. } => result.deleted_remote += 1, + SyncAction::Conflict { .. } => result.conflicts += 1, } } Err(e) => { let msg = format!("Failed {}: {}", action.path(), e); report(&format!(" ! {}", msg)); result.errors.push(msg); - if matches!(action, - SyncAction::Upload { .. } | SyncAction::Download { .. } - | SyncAction::ConflictLocalWins { .. } | SyncAction::ConflictRemoteWins { .. } - ) { - result.conflicts += 1; - } failed_actions.push(action.clone()); } } @@ -619,6 +595,7 @@ async fn execute_action( workspace_path: &Path, action: &SyncAction, sync_state: &mut SyncState, + remote_meta: &HashMap<&str, &RemoteFileSnapshot>, report: &(dyn Fn(&str) + Send + Sync), ) -> Result<()> { match action { @@ -641,22 +618,80 @@ async fn execute_action( sync_state.record_file(path, &checksum, modified.as_deref(), data.len() as u64); } - SyncAction::ConflictLocalWins { path } => { + SyncAction::Conflict { path } => { let local_path = workspace_path.join(path.replace('/', std::path::MAIN_SEPARATOR_STR)); - let data = std::fs::read(&local_path)?; - let checksum = compute_checksum(&data); + let local_data = std::fs::read(&local_path)?; + let local_checksum = compute_checksum(&local_data); - if let Some(parent) = path_parent(path) { - client.ensure_dir(parent).await?; + let remote_data = client.get_file(path).await?; + let remote_checksum = compute_checksum(&remote_data); + + // If checksums match, it's a false conflict — both sides made the same edit + if local_checksum == remote_checksum { + report(&format!(" = Conflict resolved: identical content for {}", path)); + let modified = std::fs::metadata(&local_path).ok() + .and_then(|m| m.modified().ok()) + .map(|t| { let dt: DateTime = t.into(); dt.to_rfc3339() }); + sync_state.record_file(path, &local_checksum, modified.as_deref(), local_data.len() as u64); + } else { + report(&format!(" ! Conflict: remote wins for {}, recovering local as duplicate", path)); + + // Remote wins: overwrite local with remote content + std::fs::write(&local_path, &remote_data)?; + let modified = std::fs::metadata(&local_path).ok() + .and_then(|m| m.modified().ok()) + .map(|t| { let dt: DateTime = t.into(); dt.to_rfc3339() }); + sync_state.record_file(path, &remote_checksum, modified.as_deref(), remote_data.len() as u64); + + // For .md task files inside a list dir, create a duplicate of the local version + let parts: Vec<&str> = path.split('/').collect(); + if parts.len() == 2 && parts[1].ends_with(".md") && parts[1] != ".listdata.json" { + let local_content = String::from_utf8_lossy(&local_data); + if let Ok((frontmatter, description)) = parse_frontmatter_for_conflict(&local_content) { + let original_id = frontmatter.id; + let new_id = Uuid::new_v4(); + let prefixed_desc = if description.is_empty() { + "[RECOVERED FROM CONFLICT]".to_string() + } else { + format!("[RECOVERED FROM CONFLICT]\n{}", description) + }; + + let new_frontmatter = TaskFrontmatter { + id: new_id, + ..frontmatter + }; + let yaml = serde_yaml::to_string(&new_frontmatter) + .map_err(|e| Error::Sync(e.to_string()))?; + let new_content = format!("---\n{}---\n\n{}", yaml, prefixed_desc); + + // Write the duplicate file using the new UUID as filename + let list_dir = workspace_path.join(parts[0]); + let dup_filename = format!("{}.md", new_id); + let dup_path = list_dir.join(&dup_filename); + std::fs::write(&dup_path, &new_content)?; + + // Insert new task adjacent to original in .listdata.json + let listdata_path = list_dir.join(".listdata.json"); + if listdata_path.exists() { + if let Ok(content) = std::fs::read_to_string(&listdata_path) { + if let Ok(mut metadata) = serde_json::from_str::(&content) { + let insert_pos = metadata.task_order.iter() + .position(|id| *id == original_id) + .map(|p| p + 1) + .unwrap_or(metadata.task_order.len()); + metadata.task_order.insert(insert_pos, new_id); + if let Ok(json) = serde_json::to_string_pretty(&metadata) { + let _ = std::fs::write(&listdata_path, json); + } + } + } + } + + // Don't record the duplicate in sync state — next sync will see it + // as "local added, remote absent" and upload it automatically. + } + } } - - report(&format!(" ^ Conflict: uploading local version of {}", path)); - client.put_file(path, data.clone()).await?; - - let modified = std::fs::metadata(&local_path).ok() - .and_then(|m| m.modified().ok()) - .map(|t| { let dt: DateTime = t.into(); dt.to_rfc3339() }); - sync_state.record_file(path, &checksum, modified.as_deref(), data.len() as u64); } SyncAction::Download { path } => { @@ -670,33 +705,8 @@ async fn execute_action( } std::fs::write(&local_path, &data)?; - // Record in sync state - let modified = std::fs::metadata(&local_path).ok() - .and_then(|m| m.modified().ok()) - .map(|t| { let dt: DateTime = t.into(); dt.to_rfc3339() }); - sync_state.record_file(path, &checksum, modified.as_deref(), data.len() as u64); - } - - SyncAction::ConflictRemoteWins { path } => { - let local_path = workspace_path.join(path.replace('/', std::path::MAIN_SEPARATOR_STR)); - // Back up local version before overwriting with remote - if local_path.exists() { - let backup_path = local_path.with_extension("conflict-backup"); - let _ = std::fs::copy(&local_path, &backup_path); - report(&format!(" ! Backed up local version to {}", backup_path.display())); - } - report(&format!(" v Conflict: downloading remote version of {}", path)); - let data = client.get_file(path).await?; - let checksum = compute_checksum(&data); - - if let Some(parent) = local_path.parent() { - std::fs::create_dir_all(parent)?; - } - std::fs::write(&local_path, &data)?; - - let modified = std::fs::metadata(&local_path).ok() - .and_then(|m| m.modified().ok()) - .map(|t| { let dt: DateTime = t.into(); dt.to_rfc3339() }); + // Record remote's last_modified so next diff won't see a timestamp mismatch + let modified = remote_meta.get(path.as_str()).and_then(|r| r.last_modified.clone()); sync_state.record_file(path, &checksum, modified.as_deref(), data.len() as u64); } @@ -718,6 +728,25 @@ async fn execute_action( Ok(()) } +/// Parse frontmatter and description from a markdown task file for conflict recovery. +fn parse_frontmatter_for_conflict(content: &str) -> Result<(TaskFrontmatter, String)> { + let lines: Vec<&str> = content.lines().collect(); + if lines.is_empty() || lines[0] != "---" { + return Err(Error::InvalidData("Missing frontmatter delimiter".to_string())); + } + let end_idx = lines[1..].iter().position(|&line| line == "---") + .ok_or_else(|| Error::InvalidData("Missing closing frontmatter delimiter".to_string()))?; + let frontmatter_str = lines[1..=end_idx].join("\n"); + let frontmatter: TaskFrontmatter = serde_yaml::from_str(&frontmatter_str) + .map_err(|e| Error::Sync(format!("Failed to parse frontmatter: {}", e)))?; + let description = if end_idx + 2 < lines.len() { + lines[end_idx + 2..].join("\n").trim().to_string() + } else { + String::new() + }; + Ok((frontmatter, description)) +} + /// Get the parent path of a sync path (e.g., "My Tasks/file.md" -> "My Tasks"). fn path_parent(path: &str) -> Option<&str> { path.rfind('/').map(|i| &path[..i]) @@ -876,16 +905,27 @@ mod tests { let actions = compute_sync_actions(&local, &remote, &state); assert_eq!(actions.len(), 1); - // Local present, remote gone, base known -> upload (local wins) + // Local unchanged, remote deleted -> delete local + assert_eq!(actions[0], SyncAction::DeleteLocal { path: "file.md".to_string() }); + } + + #[test] + fn test_remote_deleted_local_modified() { + let local = vec![make_local("file.md", "new_checksum")]; + let remote = vec![]; + let mut state = SyncState::default(); + state.files.insert("file.md".to_string(), make_base("abc123")); + + let actions = compute_sync_actions(&local, &remote, &state); + assert_eq!(actions.len(), 1); + // Local modified, remote deleted -> upload (local wins) assert_eq!(actions[0], SyncAction::Upload { path: "file.md".to_string() }); } #[test] - fn test_both_modified_local_newer() { - let mut local = make_local("file.md", "new_local"); - local.modified_at = Some("2026-03-15T12:00:00+00:00".to_string()); + fn test_both_modified_emits_conflict() { + let local = make_local("file.md", "new_local"); let mut remote = make_remote("file.md"); - remote.last_modified = Some("Mon, 01 Mar 2026 00:00:00 GMT".to_string()); remote.size = 200; let mut state = SyncState::default(); @@ -893,23 +933,7 @@ mod tests { let actions = compute_sync_actions(&[local], &[remote], &state); assert_eq!(actions.len(), 1); - assert_eq!(actions[0], SyncAction::ConflictLocalWins { path: "file.md".to_string() }); - } - - #[test] - fn test_both_modified_remote_newer() { - let mut local = make_local("file.md", "new_local"); - local.modified_at = Some("2026-01-01T00:00:00+00:00".to_string()); - let mut remote = make_remote("file.md"); - remote.last_modified = Some("Sun, 15 Mar 2026 12:00:00 GMT".to_string()); - remote.size = 200; - - let mut state = SyncState::default(); - state.files.insert("file.md".to_string(), make_base("old_base")); - - let actions = compute_sync_actions(&[local], &[remote], &state); - assert_eq!(actions.len(), 1); - assert_eq!(actions[0], SyncAction::ConflictRemoteWins { path: "file.md".to_string() }); + assert_eq!(actions[0], SyncAction::Conflict { path: "file.md".to_string() }); } #[test] @@ -939,17 +963,15 @@ mod tests { } #[test] - fn test_both_added_local_newer() { - let mut local = make_local("file.md", "local_content"); - local.modified_at = Some("2026-03-15T12:00:00+00:00".to_string()); - let mut remote = make_remote("file.md"); - remote.last_modified = Some("Mon, 01 Jan 2026 00:00:00 GMT".to_string()); + fn test_both_added_emits_conflict() { + let local = make_local("file.md", "local_content"); + let remote = make_remote("file.md"); let state = SyncState::default(); // No base entry let actions = compute_sync_actions(&[local], &[remote], &state); assert_eq!(actions.len(), 1); - assert_eq!(actions[0], SyncAction::ConflictLocalWins { path: "file.md".to_string() }); + assert_eq!(actions[0], SyncAction::Conflict { path: "file.md".to_string() }); } #[test] @@ -1117,9 +1139,9 @@ mod tests { // .listdata.json inside a list dir (depth 2) assert!(is_syncable("My Tasks/.listdata.json")); assert!(!is_syncable(".listdata.json")); // root-level not valid - // .metadata.json only at root (depth 1) - assert!(is_syncable(".metadata.json")); - assert!(!is_syncable("My Tasks/.metadata.json")); // nested not valid + // .onyx-workspace.json only at root (depth 1) + assert!(is_syncable(".onyx-workspace.json")); + assert!(!is_syncable("My Tasks/.onyx-workspace.json")); // nested not valid // Non-syncable assert!(!is_syncable(".syncstate.json")); assert!(!is_syncable("random.txt")); @@ -1133,7 +1155,7 @@ mod tests { let root = temp_dir.path(); // Create a workspace-like structure - std::fs::write(root.join(".metadata.json"), "{}").unwrap(); + std::fs::write(root.join(".onyx-workspace.json"), "{}").unwrap(); std::fs::create_dir_all(root.join("My Tasks")).unwrap(); std::fs::write(root.join("My Tasks").join(".listdata.json"), "{}").unwrap(); std::fs::write(root.join("My Tasks").join("task1.md"), "# Task 1").unwrap(); @@ -1144,8 +1166,8 @@ mod tests { std::fs::write(root.join(".syncstate.json"), "{}").unwrap(); let files = scan_local_files(root).unwrap(); - assert_eq!(files.len(), 4); // .metadata.json, .listdata.json, task1.md, task2.md - assert!(files.iter().any(|f| f.path == ".metadata.json")); + assert_eq!(files.len(), 4); // .onyx-workspace.json, .listdata.json, task1.md, task2.md + assert!(files.iter().any(|f| f.path == ".onyx-workspace.json")); assert!(files.iter().any(|f| f.path == "My Tasks/.listdata.json")); assert!(files.iter().any(|f| f.path == "My Tasks/task1.md")); assert!(files.iter().any(|f| f.path == "My Tasks/task2.md")); @@ -1159,12 +1181,12 @@ mod tests { fn test_get_sync_status_no_state() { let temp_dir = TempDir::new().unwrap(); let root = temp_dir.path(); - std::fs::write(root.join(".metadata.json"), "{}").unwrap(); + std::fs::write(root.join(".onyx-workspace.json"), "{}").unwrap(); let status = get_sync_status(root).unwrap(); assert!(status.last_sync.is_none()); assert_eq!(status.tracked_files, 0); - assert_eq!(status.pending_changes, 1); // .metadata.json is new + assert_eq!(status.pending_changes, 1); // .onyx-workspace.json is new assert_eq!(status.queued_operations, 0); } @@ -1188,22 +1210,6 @@ mod tests { assert!(result.is_none()); } - #[test] - fn test_local_wins_local_newer() { - assert!(local_wins( - Some("2026-03-15T12:00:00+00:00"), - Some("Mon, 01 Jan 2026 00:00:00 GMT"), - )); - } - - #[test] - fn test_local_wins_remote_newer() { - assert!(!local_wins( - Some("2026-01-01T00:00:00+00:00"), - Some("Sun, 15 Mar 2026 12:00:00 GMT"), - )); - } - // --- path_parent --- #[test] diff --git a/crates/onyx-core/src/webdav.rs b/crates/onyx-core/src/webdav.rs index 1fad34e..0d423a0 100644 --- a/crates/onyx-core/src/webdav.rs +++ b/crates/onyx-core/src/webdav.rs @@ -187,6 +187,28 @@ impl WebDavClient { Ok(()) } + /// Move/rename a resource (file or directory) on the server using WebDAV MOVE. + pub async fn move_resource(&self, from: &str, to: &str) -> Result<()> { + let from_url = self.full_url(from); + let to_url = self.full_url(to); + let resp = self._client + .request(reqwest::Method::from_bytes(b"MOVE").unwrap(), &from_url) + .basic_auth(self._username.as_str(), Some(self._password.as_str())) + .header("Destination", &to_url) + .header("Overwrite", "F") + .send() + .await?; + + let status = resp.status().as_u16(); + if status == 412 { + return Err(Error::WebDav("Destination already exists".into())); + } + if !(200..=299).contains(&status) { + return Err(Error::WebDav(format!("MOVE failed with status {}", status))); + } + Ok(()) + } + /// Ensure a directory exists, creating it and parents as needed. pub async fn ensure_dir(&self, path: &str) -> Result<()> { let parts: Vec<&str> = path.trim_matches('/').split('/').filter(|s| !s.is_empty()).collect(); diff --git a/docs/API.md b/docs/API.md index 0216504..d038e20 100644 --- a/docs/API.md +++ b/docs/API.md @@ -20,8 +20,7 @@ pub struct Task { pub status: TaskStatus, pub due_date: Option>, pub has_time: bool, // Whether due_date includes a specific time - pub created_at: DateTime, - pub updated_at: DateTime, + pub version: u64, // Increments on every write; used for sync dedup pub parent_id: Option, } @@ -64,12 +63,12 @@ pub struct TaskList { #### AppConfig -Global application configuration supporting multiple workspaces. +Global application configuration supporting multiple workspaces. Workspaces are keyed by UUID string. ```rust pub struct AppConfig { - pub workspaces: HashMap, - pub current_workspace: Option, + pub workspaces: HashMap, // UUID keys + pub current_workspace: Option, // UUID of active workspace } ``` @@ -87,14 +86,18 @@ use onyx_core::AppConfig; let config_path = AppConfig::get_config_path(); let mut config = AppConfig::load_from_file(&config_path)?; -// Add workspace -config.add_workspace( - "personal".to_string(), - WorkspaceConfig::new(PathBuf::from("/home/user/tasks")) +// Add workspace (returns generated UUID) +let id = config.add_workspace( + WorkspaceConfig::new("personal".to_string(), PathBuf::from("/home/user/tasks")) ); -// Set current workspace -config.set_current_workspace("personal".to_string())?; +// Set current workspace by ID +config.set_current_workspace(id)?; + +// Find workspace by display name +if let Some((id, ws)) = config.find_by_name("personal") { + println!("Found: {} at {:?}", id, ws.path); +} // Save config config.save_to_file(&config_path)?; @@ -106,9 +109,14 @@ Configuration for a single workspace. ```rust pub struct WorkspaceConfig { + pub name: String, // Display name pub path: PathBuf, + pub mode: WorkspaceMode, // Local or Webdav pub webdav_url: Option, + pub webdav_path: Option, // User-selected remote folder path pub last_sync: Option>, + pub theme: Option, + pub sync_interval_secs: Option, // Auto-sync polling interval } ``` @@ -232,9 +240,8 @@ Tasks are stored as `.md` files with YAML frontmatter: --- id: 550e8400-e29b-41d4-a716-446655440000 status: backlog +version: 3 due: 2026-11-15T14:00:00Z -created: 2026-10-26T10:00:00Z -updated: 2026-10-26T12:30:00Z parent: 550e8400-e29b-41d4-a716-446655440001 --- @@ -267,7 +274,7 @@ Each list folder contains a `.listdata.json` file: ### Root Metadata -The root folder contains a `.metadata.json` file: +The root folder contains a `.onyx-workspace.json` file: ```json { @@ -318,7 +325,7 @@ let status = get_sync_status(Path::new("/home/user/tasks"))?; Credentials are stored in the platform keychain (Windows Credential Manager, macOS Keychain, Linux Secret Service). -Keyring service keys use the format `com.onyx.webdav.::` — the `::` separator prevents key collisions when usernames contain dots. On first load, credentials stored in the legacy `.`-separated format (`com.onyx.webdav..`) are automatically migrated to the scoped format and the old entries are removed. +**Core library** (`onyx-core::webdav`): Keyring service keys use the format `com.onyx.webdav.::` — the `::` separator prevents key collisions when usernames contain dots. On first load, credentials stored in the legacy `.`-separated format are automatically migrated. ```rust use onyx_core::webdav::{store_credentials, load_credentials, delete_credentials}; @@ -333,6 +340,8 @@ let (username, password) = load_credentials("nextcloud.example.com")?; delete_credentials("nextcloud.example.com")?; ``` +**Tauri GUI**: Uses `tauri-plugin-credentials` instead of direct keyring calls. This plugin provides cross-platform support: EncryptedSharedPreferences (Android Keystore) on Android, keyring crate on desktop. Plugin crate at `apps/tauri/tauri-plugin-credentials/`. + ### WebDAV Client ```rust @@ -342,7 +351,7 @@ let client = WebDavClient::new( "https://nextcloud.example.com/remote.php/dav/files/user/Tasks", "username", "password", -); +)?; // Returns Result — rejects non-HTTPS URLs // Test connection client.test_connection().await?; @@ -361,11 +370,13 @@ client.delete_file("old-task.md").await?; ### Sync Strategy -- **Three-way diff**: Compares local state, remote state, and last-known baseline to determine actions (upload, download, delete local/remote) -- **Conflict resolution**: Last-write-wins using file timestamps +- **Three-way diff**: Compares local state, remote state, and last-known baseline to determine actions (upload, download, delete local/remote, conflict) +- **Conflict resolution**: Checksum-based — downloads remote file and compares SHA-256 checksums. Identical content is a false conflict (skipped). When different, remote wins and the local version is recovered as a duplicate task with a new UUID and `[RECOVERED FROM CONFLICT]` prefix, inserted adjacent to the original in `.listdata.json` - **Offline queue**: Pending operations are queued and replayed when connectivity returns - **Sync state**: Stored in `.syncstate.json` within the workspace directory +- **Auto-sync**: Periodic polling (configurable `sync_interval_secs`), debounced file-change trigger (5s), window-focus trigger (30s stale threshold) - **Response size cap**: PROPFIND responses are limited to 10 MB (checked via `Content-Length` header and actual body size) to prevent memory exhaustion from malicious servers +- **Syncable files**: Only processes files at expected depths — `.onyx-workspace.json` at root (depth 1), `.listdata.json` and `*.md` inside list directories (depth 2) ## Error Handling @@ -424,8 +435,8 @@ fn main() -> Result<(), Box> { // Configure workspace let mut config = AppConfig::new(); - config.add_workspace("personal".to_string(), WorkspaceConfig::new(path)); - config.set_current_workspace("personal".to_string())?; + let ws_id = config.add_workspace(WorkspaceConfig::new("personal".to_string(), path)); + config.set_current_workspace(ws_id)?; config.save_to_file(&AppConfig::get_config_path())?; Ok(()) diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index 13cdca5..66dcd2a 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -76,6 +76,11 @@ onyx/ │ │ ├── components/ # Reusable UI components │ │ ├── stores/ # Svelte state (app.svelte.ts) │ │ └── types.ts # TypeScript type definitions +│ ├── tauri-plugin-credentials/ # Cross-platform credential storage plugin +│ │ ├── Cargo.toml +│ │ ├── src/ +│ │ │ └── lib.rs # Desktop (keyring) + plugin API +│ │ └── android/ # Android (EncryptedSharedPreferences) │ └── src-tauri/ # Rust backend (Tauri commands) │ ├── Cargo.toml │ ├── tauri.conf.json