2024-11-15 19:35:11 +11:00
|
|
|
/*
|
|
|
|
DrCr: Web-based double-entry bookkeeping framework
|
|
|
|
Copyright (C) 2022–2024 Lee Yingtong Li (RunasSudo)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-11-17 17:45:16 +11:00
|
|
|
use tauri::{AppHandle, Builder, Manager, State};
|
|
|
|
use tauri_plugin_store::StoreExt;
|
2024-11-15 19:35:11 +11:00
|
|
|
|
2024-11-17 17:45:16 +11:00
|
|
|
use std::fs;
|
2024-11-15 19:35:11 +11:00
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
struct AppState {
|
|
|
|
db_filename: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
fn get_open_filename(state: State<'_, Mutex<AppState>>) -> Option<String> {
|
|
|
|
let state = state.lock().unwrap();
|
|
|
|
state.db_filename.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tauri::command]
|
2024-11-17 17:45:16 +11:00
|
|
|
fn set_open_filename(state: State<'_, Mutex<AppState>>, app: AppHandle, filename: Option<String>) {
|
2024-11-15 19:35:11 +11:00
|
|
|
let mut state = state.lock().unwrap();
|
2024-11-17 17:45:16 +11:00
|
|
|
state.db_filename = filename.clone();
|
|
|
|
|
|
|
|
// Persist in store
|
|
|
|
let store = app.store("store.json").expect("Error opening store");
|
|
|
|
store.set("db_filename", filename);
|
2024-11-15 19:35:11 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
|
|
pub fn run() {
|
|
|
|
Builder::default()
|
|
|
|
.setup(|app| {
|
2024-11-17 17:45:16 +11:00
|
|
|
// Get open filename
|
|
|
|
let store = app.store("store.json")?;
|
|
|
|
let db_filename = match store.get("db_filename") {
|
|
|
|
None => None,
|
|
|
|
Some(serde_json::Value::String(s)) => {
|
|
|
|
if fs::exists(&s)? {
|
|
|
|
Some(s)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => panic!("Unexpected db_filename in store")
|
|
|
|
};
|
|
|
|
|
2024-11-15 19:35:11 +11:00
|
|
|
app.manage(Mutex::new(AppState {
|
2024-11-17 17:45:16 +11:00
|
|
|
db_filename: db_filename
|
2024-11-15 19:35:11 +11:00
|
|
|
}));
|
2024-11-17 17:45:16 +11:00
|
|
|
|
2024-11-15 19:35:11 +11:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.plugin(tauri_plugin_dialog::init())
|
|
|
|
.plugin(tauri_plugin_shell::init())
|
|
|
|
.plugin(tauri_plugin_sql::Builder::new().build())
|
2024-11-17 17:45:16 +11:00
|
|
|
.plugin(tauri_plugin_store::Builder::new().build())
|
2024-11-15 19:35:11 +11:00
|
|
|
.invoke_handler(tauri::generate_handler![get_open_filename, set_open_filename])
|
|
|
|
.run(tauri::generate_context!())
|
2024-11-17 17:45:16 +11:00
|
|
|
.expect("Error while running tauri application");
|
2024-11-15 19:35:11 +11:00
|
|
|
}
|