|
| 1 | +# Plan: Implement Custom Data Location Feature |
| 2 | + |
| 3 | +This document outlines the plan to implement the feature allowing users to specify a custom location for the PasteBar application's data. |
| 4 | + |
| 5 | +## 1. Goals |
| 6 | + |
| 7 | +* Allow users to specify a custom parent directory for application data via the settings UI. |
| 8 | +* The application will create and manage a `pastebar-data` subdirectory within the user-specified location. |
| 9 | +* This `pastebar-data` directory will contain the database file (`pastebar-db.data`), the `clip-images` folder, and the `clipboard-images` folder. |
| 10 | +* Provide options to either **move** the existing data, **copy** it, or **use the new location without moving/copying**. |
| 11 | +* Ensure the application uses the data from the new location after a restart. |
| 12 | +* Handle potential errors gracefully and inform the user. |
| 13 | +* Update the application state and backend configuration accordingly. |
| 14 | + |
| 15 | +## 2. Backend (Rust - `src-tauri`) |
| 16 | + |
| 17 | +### 2.1. Configuration (`user_settings_service.rs`) |
| 18 | + |
| 19 | +* The `UserConfig` struct's `custom_db_path: Option<String>` will now be repurposed to store the path to the **user-selected parent directory**. The application logic will handle appending the `/pastebar-data/` segment. This requires no change to the struct itself, only to how the path is interpreted. |
| 20 | + |
| 21 | +### 2.2. Path Logic (`db.rs` and new helpers) |
| 22 | + |
| 23 | +* We will introduce new helper functions to consistently resolve data paths, whether default or custom. |
| 24 | + * `get_data_dir() -> PathBuf`: This will be the core helper. It checks for a `custom_db_path` in the settings. |
| 25 | + * If present, it returns `PathBuf::from(custom_path)`. |
| 26 | + * If `None`, it returns the default application data directory. |
| 27 | + * `get_db_path()`: This function will be refactored to use `get_data_dir().join("pastebar-db.data")`. |
| 28 | + * `get_clip_images_dir()`: A new helper that returns `get_data_dir().join("clip-images")`. |
| 29 | + * `get_clipboard_images_dir()`: A new helper that returns `get_data_dir().join("clipboard-images")`. |
| 30 | + |
| 31 | +### 2.3. New & Updated Tauri Commands (`user_settings_command.rs`) |
| 32 | + |
| 33 | +* **`cmd_validate_custom_db_path(path: String) -> Result<bool, String>`** |
| 34 | + * **No change in purpose.** This command will still check if the user-selected directory is valid and writable. |
| 35 | +* **`cmd_check_custom_data_path(path: String) -> Result<PathStatus, String>`** |
| 36 | + * A new command to check the status of a selected directory. It returns one of the following statuses: `Empty`, `NotEmpty`, `IsPastebarDataAndNotEmpty`. |
| 37 | +* **`cmd_set_and_relocate_data(new_parent_dir_path: String, operation: String) -> Result<String, String>`** (renamed from `set_and_relocate_db`) |
| 38 | + * `new_parent_dir_path`: The new directory path selected by the user. |
| 39 | + * `operation`: Either "move", "copy", or "none". |
| 40 | + * **Updated Steps:** |
| 41 | + 1. Get the source paths: |
| 42 | + * Current DB file path. |
| 43 | + * Current `clip-images` directory path. |
| 44 | + * Current `clipboard-images` directory path. |
| 45 | + 2. Define the new data directory: `let new_data_dir = Path::new(&new_parent_dir_path);` |
| 46 | + 3. Create the new data directory: `fs::create_dir_all(&new_data_dir)`. |
| 47 | + 4. Perform file/directory operations for each item (DB file, `clip-images` dir, `clipboard-images` dir): |
| 48 | + * If "move": `fs::rename(source, destination)`. |
| 49 | + * If "copy": `fs::copy` for the file, and a recursive copy function for the directories. |
| 50 | + * If "none", do nothing. |
| 51 | + * Handle cases where source items might not exist (e.g., `clip-images` folder hasn't been created yet) by skipping them gracefully. |
| 52 | + 5. If successful, call `user_settings_service::set_custom_db_path(&new_parent_dir_path)`. |
| 53 | + 6. Return a success or error message. |
| 54 | + |
| 55 | +* **`cmd_revert_to_default_data_location() -> Result<String, String>`** (renamed and simplified) |
| 56 | + * **Updated Steps:** |
| 57 | + 1. Call `user_settings_service::remove_custom_db_path()` to clear the custom data path setting. |
| 58 | + 2. Return a success message indicating the setting has been removed. |
| 59 | + |
| 60 | +## 3. Frontend (React) |
| 61 | + |
| 62 | +* The UI has been updated to refer to "Custom Application Data Location" instead of "Custom Database Location". |
| 63 | +* A third radio button option, "Use new location", has been added. |
| 64 | +* The `handleBrowse` function now calls the `cmd_check_custom_data_path` command to analyze the selected directory and prompts the user accordingly. |
| 65 | +* The `settingsStore.ts` has been updated to support the "none" operation. |
| 66 | + |
| 67 | +## 4. User Interaction Flow (Mermaid Diagram) |
| 68 | + |
| 69 | +```mermaid |
| 70 | +graph TD |
| 71 | + subgraph User Flow |
| 72 | + A[User navigates to User Preferences] --> B{Custom Data Path Set?}; |
| 73 | + B -- Yes --> C[Display Current Custom Path]; |
| 74 | + B -- No --> D[Display Current Path: Default]; |
| 75 | +
|
| 76 | + C --> E[Show "Revert to Default" Button]; |
| 77 | + D --> F[User Selects New Parent Directory]; |
| 78 | + F --> G{Path Status?}; |
| 79 | + G -- Empty --> H[Set Path]; |
| 80 | + G -- Not Empty --> I{Confirm "pastebar-data" subfolder}; |
| 81 | + I -- Yes --> J[Append "pastebar-data" to path]; |
| 82 | + J --> H; |
| 83 | + I -- No --> H; |
| 84 | + G -- Is 'pastebar-data' and Not Empty --> K[Alert user existing data will be used]; |
| 85 | + K --> H; |
| 86 | + H --> L[User Selects Operation: Move/Copy/None]; |
| 87 | + L --> M[User Clicks "Apply and Restart"]; |
| 88 | + end |
| 89 | +
|
| 90 | + subgraph Backend Logic |
| 91 | + M --> N[Frontend calls `cmd_set_and_relocate_data`]; |
| 92 | + N -- Success --> O[1. Create new data dir if needed]; |
| 93 | + O --> P[2. Move/Copy/Skip data]; |
| 94 | + P --> Q[3. Update `custom_db_path` in settings]; |
| 95 | + Q --> R[Show Success Toast & Relaunch App]; |
| 96 | + N -- Error --> S[Show Error Toast]; |
| 97 | +
|
| 98 | + E --> T[User Clicks "Revert"]; |
| 99 | + T --> U[Frontend calls `cmd_revert_to_default_data_location`]; |
| 100 | + U -- Success --> V[Move/Copy data back to default app dir & clear setting]; |
| 101 | + V --> W[Show Success Toast & Relaunch App]; |
| 102 | + U -- Error --> X[Show Error Toast]; |
| 103 | + end |
| 104 | +
|
| 105 | + D -- "Browse..." --> F; |
| 106 | +``` |
| 107 | + |
| 108 | +## 5. Implementation Summary |
| 109 | + |
| 110 | +The following changes have been implemented: |
| 111 | + |
| 112 | +* **`packages/pastebar-app-ui/src/pages/settings/UserPreferences.tsx`**: |
| 113 | + * Renamed "Custom Database Location" to "Custom Application Data Location". |
| 114 | + * Added a third radio button for the "Use new location" option. |
| 115 | + * Updated the `handleBrowse` function to call the new `cmd_check_custom_data_path` command and handle the different path statuses with user prompts. |
| 116 | +* **`packages/pastebar-app-ui/src/store/settingsStore.ts`**: |
| 117 | + * Updated the `applyCustomDbPath` function to accept the "none" operation. |
| 118 | + * Updated the `revertToDefaultDbPath` function to call the renamed backend command. |
| 119 | +* **`src-tauri/src/commands/user_settings_command.rs`**: |
| 120 | + * Added the `cmd_check_custom_data_path` command. |
| 121 | + * Renamed `cmd_set_and_relocate_db` to `cmd_set_and_relocate_data` and updated its logic to handle the "none" operation and the new data directory structure. |
| 122 | + * Renamed `cmd_revert_to_default_db_location` to `cmd_revert_to_default_data_location` and updated its logic. |
| 123 | +* **`src-tauri/src/db.rs`**: |
| 124 | + * Refactored the `get_data_dir` function to no longer automatically append `pastebar-data`. |
| 125 | + * Added `get_clip_images_dir` and `get_clipboard_images_dir` helper functions. |
| 126 | +* **`src-tauri/src/main.rs`**: |
| 127 | + * Registered the new and renamed commands in the `invoke_handler`. |
| 128 | +* **`src-tauri/Cargo.toml`**: |
| 129 | + * Added the `fs_extra` dependency for recursive directory copying. |
| 130 | +* **`src-tauri/src/services/items_service.rs`** and **`src-tauri/src/services/history_service.rs`**: |
| 131 | + * Updated to use the new `get_clip_images_dir` and `get_clipboard_images_dir` helper functions. |
0 commit comments