refonte
This commit is contained in:
@ -1,53 +1,72 @@
|
||||
use crate::{database::Database, models::ClipboardEntry};
|
||||
use arboard::Clipboard;
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
error::Error,
|
||||
sync::mpsc::channel,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use crate::database::Database;
|
||||
use crate::models::{ClipboardData, ClipboardEntry, Image};
|
||||
use std::error::Error;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::SystemTime;
|
||||
use uuid::Uuid;
|
||||
use wayland_clipboard_listener::{WlClipboardPasteStream, WlListenType};
|
||||
|
||||
pub fn start(db: Arc<Mutex<Database>>, mut clipboard: Clipboard) -> Result<(), Box<dyn Error>> {
|
||||
let (tx, rx) = channel();
|
||||
pub fn start(
|
||||
db: Arc<Mutex<Database>>,
|
||||
_clipboard: arboard::Clipboard,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let mut stream = WlClipboardPasteStream::init(WlListenType::ListenOnCopy)
|
||||
.map_err(|e| format!("Impossible d'initialiser Wayland : {e}"))?;
|
||||
|
||||
std::thread::spawn(
|
||||
move || match WlClipboardPasteStream::init(WlListenType::ListenOnCopy) {
|
||||
Ok(mut stream) => {
|
||||
for _ in stream.paste_stream().flatten() {
|
||||
std::thread::sleep(Duration::new(1, 0));
|
||||
if let Err(e) = tx.send(()) {
|
||||
eprintln!("{}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("{}", e);
|
||||
}
|
||||
},
|
||||
);
|
||||
println!("Écoute du presse-papier Wayland...");
|
||||
|
||||
for _ in rx {
|
||||
println!("Clipboard update!");
|
||||
if let Ok(entry) = ClipboardEntry::new(&mut clipboard) {
|
||||
let db_clone = Arc::clone(&db);
|
||||
for msg in stream.paste_stream().flatten() {
|
||||
let context = &msg.context;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let db_lock = db_clone.lock().unwrap();
|
||||
let data: &[u8] = context.context.as_slice();
|
||||
|
||||
if let Err(e) = db_lock.append(entry) {
|
||||
eprintln!("SQLite writing error: {}", e);
|
||||
} else {
|
||||
println!("SQLite edited!");
|
||||
}
|
||||
});
|
||||
if data.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// match ClipboardEntry::new(&mut clipboard) {
|
||||
// Ok(entry) => db.append(entry)?,
|
||||
// Err(e) => eprintln!("{}", e),
|
||||
// }
|
||||
let entry = if let Ok(text) = String::from_utf8(data.to_vec()) {
|
||||
let text = text.trim_end_matches('\n').to_string();
|
||||
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
ClipboardEntry {
|
||||
content: ClipboardData::Text(text),
|
||||
timestamp: SystemTime::now(),
|
||||
}
|
||||
} else {
|
||||
match image::load_from_memory(data) {
|
||||
Ok(img) => {
|
||||
let (width, height) = (img.width(), img.height());
|
||||
let rgba = img.into_rgba8();
|
||||
|
||||
ClipboardEntry {
|
||||
content: ClipboardData::Image(Image {
|
||||
raw_pixels: Some(rgba.into_raw()),
|
||||
width,
|
||||
height,
|
||||
id: Uuid::new_v4(),
|
||||
}),
|
||||
timestamp: SystemTime::now(),
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Clipboard ignoré : {e}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
println!("Clipboard update détecté");
|
||||
|
||||
let db_clone = Arc::clone(&db);
|
||||
std::thread::spawn(move || {
|
||||
let db_lock = db_clone.lock().unwrap();
|
||||
if let Err(e) = db_lock.append(entry) {
|
||||
eprintln!("SQLite error : {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user