2021-03-29 15:59:14 +08:00
|
|
|
use super::*;
|
2024-07-01 02:14:58 +08:00
|
|
|
pub use crate::clipboard::{
|
2024-07-11 00:05:25 +08:00
|
|
|
check_clipboard, get_cache_msg, ClipboardContext, ClipboardSide,
|
|
|
|
CLIPBOARD_INTERVAL as INTERVAL, CLIPBOARD_NAME as NAME,
|
2021-03-29 15:59:14 +08:00
|
|
|
};
|
|
|
|
|
2024-07-01 00:24:23 +08:00
|
|
|
#[derive(Default)]
|
2022-05-12 17:35:25 +08:00
|
|
|
struct State {
|
|
|
|
ctx: Option<ClipboardContext>,
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
2024-07-01 00:24:23 +08:00
|
|
|
impl super::service::Reset for State {
|
|
|
|
fn reset(&mut self) {
|
2024-07-11 00:05:25 +08:00
|
|
|
crate::clipboard::reset_cache();
|
2024-07-01 00:24:23 +08:00
|
|
|
self.ctx = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init(&mut self) {
|
2024-06-30 21:57:32 +08:00
|
|
|
let ctx = match ClipboardContext::new(true) {
|
2022-05-12 17:35:25 +08:00
|
|
|
Ok(ctx) => Some(ctx),
|
2021-10-27 20:02:17 +08:00
|
|
|
Err(err) => {
|
|
|
|
log::error!("Failed to start {}: {}", NAME, err);
|
2022-05-12 17:35:25 +08:00
|
|
|
None
|
2021-10-26 16:59:31 +08:00
|
|
|
}
|
2021-10-27 20:02:17 +08:00
|
|
|
};
|
2024-07-01 00:24:23 +08:00
|
|
|
self.ctx = ctx;
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
2022-05-12 17:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new() -> GenericService {
|
2023-10-08 21:44:54 +08:00
|
|
|
let svc = EmptyExtraFieldService::new(NAME.to_owned(), true);
|
|
|
|
GenericService::repeat::<State, _, _>(&svc.clone(), INTERVAL, run);
|
|
|
|
svc.sp
|
2022-05-12 17:35:25 +08:00
|
|
|
}
|
2021-10-27 20:02:17 +08:00
|
|
|
|
2023-10-08 21:44:54 +08:00
|
|
|
fn run(sp: EmptyExtraFieldService, state: &mut State) -> ResultType<()> {
|
2024-07-11 00:05:25 +08:00
|
|
|
if let Some(msg) = check_clipboard(&mut state.ctx, ClipboardSide::Host) {
|
2024-02-21 22:05:27 +08:00
|
|
|
sp.send(msg);
|
2021-10-27 20:02:17 +08:00
|
|
|
}
|
2024-02-21 22:05:27 +08:00
|
|
|
sp.snapshot(|sps| {
|
2024-07-11 00:05:25 +08:00
|
|
|
// Just create a message with multi clipboards here
|
|
|
|
// The actual peer version and peer platform will be checked again before sending.
|
|
|
|
if let Some(msg) = get_cache_msg("1.2.7", "Windows") {
|
|
|
|
sps.send_shared(Arc::new(msg));
|
2024-02-21 22:05:27 +08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2022-05-12 17:35:25 +08:00
|
|
|
Ok(())
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|