diff --git a/wallitor-gui/src-tauri/src/handler/apply.rs b/wallitor-gui/src-tauri/src/handler/apply.rs new file mode 100644 index 0000000..97b4272 --- /dev/null +++ b/wallitor-gui/src-tauri/src/handler/apply.rs @@ -0,0 +1,17 @@ +use std::ffi::CString; +use libloading::{Library, Symbol}; + +#[tauri::command] +pub async fn set_wallpaper(title: String) -> bool { + let lib = unsafe { Library::new("wallitor-core.dll").unwrap() }; + type SetFn = unsafe extern "C" fn(*const i8) -> i8; + let set: Symbol = unsafe { lib.get(b"set_wallpaper\0").unwrap() }; + let title = CString::new(title.to_string()).unwrap(); + unsafe { + let res = set(title.as_ptr()); + if res == 0 { + return false; + }; + } + return true; +} \ No newline at end of file diff --git a/wallitor-gui/src-tauri/src/handler/file.rs b/wallitor-gui/src-tauri/src/handler/file.rs new file mode 100644 index 0000000..c39e7cf --- /dev/null +++ b/wallitor-gui/src-tauri/src/handler/file.rs @@ -0,0 +1,42 @@ +use crate::reader; +use std::path::Path; +use std::fs; +use tauri::ipc::Response; + +#[tauri::command] +pub async fn get_file(path: String) -> Response { + let p = Path::new(&path); + if let Ok(true) = fs::exists(p) { + let data: Vec = fs::read(p).unwrap(); + return tauri::ipc::Response::new(data); + } + tauri::ipc::Response::new(String::from("")) +} + +#[tauri::command] +pub async fn read_resource_dir() -> String { + let mut file_map = reader::FileMap::new(); + let path = Path::new(".\\resource"); + if let Ok(false) = fs::exists(path) { + fs::create_dir(path).expect("Can't create dir"); + } + file_map + .read_resourse_directory(path) + .expect("Can't read dir"); + serde_json::to_string(&file_map).unwrap() +} + +#[tauri::command] +pub async fn del_folder(path: String) -> bool { + let p = Path::new(&path); + if p.starts_with(".\\") { + if p.is_dir() { + if let Ok(true) = fs::exists(p) { + if fs::remove_dir_all(p).is_ok() { + return true; + } + } + } + } + false +} \ No newline at end of file diff --git a/wallitor-gui/src-tauri/src/handler/mod.rs b/wallitor-gui/src-tauri/src/handler/mod.rs new file mode 100644 index 0000000..4346ffb --- /dev/null +++ b/wallitor-gui/src-tauri/src/handler/mod.rs @@ -0,0 +1,3 @@ +pub mod file; +pub mod wallpaper; +pub mod apply; \ No newline at end of file diff --git a/wallitor-gui/src-tauri/src/handler/wallpaper.rs b/wallitor-gui/src-tauri/src/handler/wallpaper.rs new file mode 100644 index 0000000..9180249 --- /dev/null +++ b/wallitor-gui/src-tauri/src/handler/wallpaper.rs @@ -0,0 +1,110 @@ +use std::path::Path; +use std::fs; +use chrono::Local; +use serde::{Deserialize, Serialize}; +use serde_json::{self, json}; + + +#[derive(Deserialize)] +pub struct AddInfo { + name: String, + preview: String, + media: String, + description: String, +} + +#[derive(Serialize,Deserialize)] +struct Info { + media_type: String, + description: String, + created: i64, + entry_point: String, +} + +#[derive(Serialize,Deserialize)] +struct Opt { + mute: bool, +} + +#[derive(Serialize,Deserialize)] +pub struct Config { + name: String, + info: Info, + option: Opt, +} + +#[tauri::command] +pub async fn new_wallpaper(info: AddInfo) -> String { + let base_url = String::from("./resource"); + let current_time: i64 = Local::now().timestamp(); + let folder = format!("{}/{}", base_url, current_time); + if fs::create_dir_all(Path::new(&folder)).is_err() { + return String::from("Error creating folder."); + } + if !info.preview.is_empty() { + if let Ok(false) = fs::exists(Path::new(&info.preview)) { + return String::from("Source Image doesn't exist."); + } + let preview = Path::new(&info.preview); + if let Some(ext) = preview.extension().and_then(|f| f.to_str()) { + if fs::copy( + Path::new(&info.preview), + Path::new(&format!("{}/preview.{}", folder, ext)), + ) + .is_err() + { + return String::from("Error copy image."); + } + } else { + return String::from("Invalid Image Path."); + } + } + if fs::create_dir(Path::new(&format!("{}/res", folder))).is_err() { + return String::from("Error creating res folder."); + } + let media = Path::new(&info.media); + if let Some(filename) = media.file_name().and_then(|f| f.to_str()) { + if fs::copy( + Path::new(&info.media), + Path::new(&format!("{}/res/{}", folder, filename)), + ) + .is_err() + { + return String::from("Error copy media."); + } + let config = json!(Config { + name: info.name, + info: Info { + media_type: String::from("video"), + description: info.description, + created: current_time, + entry_point: String::from(filename) + }, + option: Opt { mute: true } + }); + if fs::write( + Path::new(&format!("{}/config.json", folder)), + config.to_string(), + ) + .is_err() + { + return String::from("Error write config."); + } + } else { + return String::from("Invalid media path."); + } + String::from("Success") +} + +#[derive(Deserialize)] +pub struct EditInfo { + path: String, + name: String, + preview: String, + description: String, +} + +#[tauri::command] +pub async fn edit_wallpaper(title: String) -> bool { + true +} \ No newline at end of file diff --git a/wallitor-gui/src-tauri/src/lib.rs b/wallitor-gui/src-tauri/src/lib.rs index 3a2f994..4a019c6 100644 --- a/wallitor-gui/src-tauri/src/lib.rs +++ b/wallitor-gui/src-tauri/src/lib.rs @@ -1,14 +1,6 @@ -mod reader; mod setup; - -use chrono::Local; -use libloading::{Library, Symbol}; -use serde::{Deserialize, Serialize}; -use serde_json::{self, json}; -use std::ffi::CString; -use std::path::Path; -use std::{fs}; -use tauri::ipc::Response; +mod handler; +mod reader; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { @@ -17,157 +9,13 @@ pub fn run() { .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_dialog::init()) .invoke_handler(tauri::generate_handler![ - read_resource_dir, - get_file, - new_wallpaper, - set_wallpaper, - del_folder + handler::file::get_file, + handler::file::read_resource_dir, + handler::file::del_folder, + handler::wallpaper::new_wallpaper, + handler::apply::set_wallpaper, + handler::wallpaper::edit_wallpaper, ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } - -#[tauri::command] -async fn read_resource_dir() -> String { - let mut file_map = reader::FileMap::new(); - let path = Path::new(".\\resource"); - if let Ok(false) = fs::exists(path) { - fs::create_dir(path).expect("Can't create dir"); - } - file_map - .read_resourse_directory(path) - .expect("Can't read dir"); - serde_json::to_string(&file_map).unwrap() -} - -// remember to call `.manage(MyState::default())` -#[tauri::command] -async fn get_file(path: String) -> Response { - let p = Path::new(&path); - if let Ok(true) = fs::exists(p) { - let data: Vec = fs::read(p).unwrap(); - return tauri::ipc::Response::new(data); - } - tauri::ipc::Response::new(String::from("")) -} - -#[tauri::command] -async fn del_folder(path: String) -> bool { - let p = Path::new(&path); - if p.starts_with(".\\") { - if p.is_dir() { - if let Ok(true) = fs::exists(p) { - if fs::remove_dir_all(p).is_ok() { - return true; - } - } - } - } - false -} - -#[derive(Deserialize)] -struct AddInfo { - name: String, - preview: String, - media: String, - description: String, -} - -#[derive(Serialize)] -struct Info { - media_type: String, - description: String, - created: i64, - entry_point: String, -} - -#[derive(Serialize)] -struct Opt { - mute: bool, -} - -#[derive(Serialize)] -struct Config { - name: String, - info: Info, - option: Opt, -} - -#[tauri::command] -async fn new_wallpaper(info: AddInfo) -> String { - let base_url = String::from("./resource"); - let current_time: i64 = Local::now().timestamp(); - let folder = format!("{}/{}", base_url, current_time); - if fs::create_dir_all(Path::new(&folder)).is_err() { - return String::from("Error creating folder."); - } - if !info.preview.is_empty() { - if let Ok(false) = fs::exists(Path::new(&info.preview)) { - return String::from("Source Image doesn't exist."); - } - let preview = Path::new(&info.preview); - if let Some(ext) = preview.extension().and_then(|f| f.to_str()) { - if fs::copy( - Path::new(&info.preview), - Path::new(&format!("{}/preview.{}", folder, ext)), - ) - .is_err() - { - return String::from("Error copy image."); - } - } else { - return String::from("Invalid Image Path."); - } - } - if fs::create_dir(Path::new(&format!("{}/res", folder))).is_err() { - return String::from("Error creating res folder."); - } - let media = Path::new(&info.media); - if let Some(filename) = media.file_name().and_then(|f| f.to_str()) { - if fs::copy( - Path::new(&info.media), - Path::new(&format!("{}/res/{}", folder, filename)), - ) - .is_err() - { - return String::from("Error copy media."); - } - let config = json!(Config { - name: info.name, - info: Info { - media_type: String::from("video"), - description: info.description, - created: current_time, - entry_point: String::from(filename) - }, - option: Opt { mute: true } - }); - if fs::write( - Path::new(&format!("{}/config.json", folder)), - config.to_string(), - ) - .is_err() - { - return String::from("Error write config."); - } - } else { - return String::from("Invalid media path."); - } - String::from("Success") -} - -#[tauri::command] -async fn set_wallpaper(title: String) -> bool { - let lib = unsafe { Library::new("wallitor-core.dll").unwrap() }; - type SetFn = unsafe extern "C" fn(*const i8) -> i8; - let set: Symbol = unsafe { lib.get(b"set_wallpaper\0").unwrap() }; - let title = CString::new(title.to_string()).unwrap(); - unsafe { - let res = set(title.as_ptr()); - if res == 0 { - return false; - }; - } - return true; -}