refonte
This commit is contained in:
105
src/ipc.rs
105
src/ipc.rs
@ -2,64 +2,85 @@ use serde::{Deserialize, Serialize};
|
||||
use std::io::{Read, Write};
|
||||
use std::os::unix::net::UnixStream;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct HistoryItem {
|
||||
pub content: String,
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum IpcRequest {
|
||||
GetHistory { limit: usize },
|
||||
SetClipboard { content: String },
|
||||
DeleteEntry { content: String },
|
||||
GetHistory {
|
||||
limit: usize,
|
||||
},
|
||||
SetClipboard {
|
||||
content: String,
|
||||
},
|
||||
DeleteEntry {
|
||||
content: String,
|
||||
},
|
||||
UpdateEntry {
|
||||
old_content: String,
|
||||
new_content: String,
|
||||
},
|
||||
AddEntry {
|
||||
content: String,
|
||||
},
|
||||
ClearHistory,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum IpcResponse {
|
||||
History(Vec<String>),
|
||||
History(Vec<HistoryItem>),
|
||||
Ok,
|
||||
Error(String),
|
||||
}
|
||||
|
||||
pub fn fetch_history(limit: usize) -> Option<Vec<String>> {
|
||||
let base_dir = directories::ProjectDirs::from("com", "zefad", "rklipd")?
|
||||
fn send_request(req: &IpcRequest) -> Option<IpcResponse> {
|
||||
let dir = directories::ProjectDirs::from("com", "zefad", "rklipd")?
|
||||
.data_dir()
|
||||
.to_path_buf();
|
||||
let socket_path = base_dir.join("rklip.sock");
|
||||
let mut stream = UnixStream::connect(dir.join("rklip.sock")).ok()?;
|
||||
let json = serde_json::to_string(req).ok()?;
|
||||
stream.write_all(json.as_bytes()).ok()?;
|
||||
stream.shutdown(std::net::Shutdown::Write).ok()?;
|
||||
let mut buf = String::new();
|
||||
stream.read_to_string(&mut buf).ok()?;
|
||||
serde_json::from_str(&buf).ok()
|
||||
}
|
||||
|
||||
if let Ok(mut stream) = UnixStream::connect(&socket_path) {
|
||||
let req = IpcRequest::GetHistory { limit };
|
||||
let req_json = serde_json::to_string(&req).unwrap();
|
||||
|
||||
let _ = stream.write_all(req_json.as_bytes());
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
|
||||
let mut response_buffer = String::new();
|
||||
if stream.read_to_string(&mut response_buffer).is_ok() {
|
||||
if let Ok(IpcResponse::History(items)) = serde_json::from_str(&response_buffer) {
|
||||
return Some(items);
|
||||
}
|
||||
}
|
||||
pub fn fetch_history(limit: usize) -> Option<Vec<HistoryItem>> {
|
||||
match send_request(&IpcRequest::GetHistory { limit })? {
|
||||
IpcResponse::History(items) => Some(items),
|
||||
_ => None,
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn set_clipboard(content: String) {
|
||||
if let Some(base_dir) = directories::ProjectDirs::from("com", "zefad", "rklipd") {
|
||||
let socket_path = base_dir.data_dir().join("rklip.sock");
|
||||
if let Ok(mut stream) = UnixStream::connect(&socket_path) {
|
||||
let req = IpcRequest::SetClipboard { content };
|
||||
if let Ok(req_json) = serde_json::to_string(&req) {
|
||||
let _ = stream.write_all(req_json.as_bytes());
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = send_request(&IpcRequest::SetClipboard { content });
|
||||
}
|
||||
|
||||
pub fn delete_entry(content: String) {
|
||||
if let Some(base_dir) = directories::ProjectDirs::from("com", "zefad", "rklipd") {
|
||||
let socket_path = base_dir.data_dir().join("rklip.sock");
|
||||
if let Ok(mut stream) = UnixStream::connect(&socket_path) {
|
||||
let req = IpcRequest::DeleteEntry { content };
|
||||
if let Ok(req_json) = serde_json::to_string(&req) {
|
||||
let _ = stream.write_all(req_json.as_bytes());
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = send_request(&IpcRequest::DeleteEntry { content });
|
||||
}
|
||||
|
||||
pub fn update_entry(old_content: String, new_content: String) -> bool {
|
||||
matches!(
|
||||
send_request(&IpcRequest::UpdateEntry {
|
||||
old_content,
|
||||
new_content
|
||||
}),
|
||||
Some(IpcResponse::Ok)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn add_entry(content: String) {
|
||||
let _ = send_request(&IpcRequest::AddEntry { content });
|
||||
}
|
||||
|
||||
pub fn clear_history() -> bool {
|
||||
matches!(
|
||||
send_request(&IpcRequest::ClearHistory),
|
||||
Some(IpcResponse::Ok)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user