2023-08-24 22:34:12 +08:00
|
|
|
use hbb_common::{log, ResultType};
|
2023-06-20 00:50:01 +08:00
|
|
|
use std::sync::Mutex;
|
2022-10-26 22:37:45 +08:00
|
|
|
|
2023-08-24 22:34:12 +08:00
|
|
|
use crate::CliprdrServiceContext;
|
|
|
|
|
2023-06-29 20:41:33 +08:00
|
|
|
const CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS: u32 = 30;
|
2023-06-29 11:28:55 +08:00
|
|
|
|
2022-10-26 22:37:45 +08:00
|
|
|
lazy_static::lazy_static! {
|
2023-08-24 22:34:12 +08:00
|
|
|
static ref CONTEXT_SEND: ContextSend = ContextSend{addr: Mutex::new(None)};
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ContextSend {
|
2023-08-24 22:34:12 +08:00
|
|
|
addr: Mutex<Option<Box<dyn CliprdrServiceContext>>>,
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContextSend {
|
2023-06-19 21:44:32 +08:00
|
|
|
#[inline]
|
2023-06-20 00:50:01 +08:00
|
|
|
pub fn is_enabled() -> bool {
|
2023-08-24 22:34:12 +08:00
|
|
|
CONTEXT_SEND.addr.lock().unwrap().is_some()
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
|
2023-06-19 22:52:17 +08:00
|
|
|
pub fn set_is_stopped() {
|
2023-08-24 22:34:12 +08:00
|
|
|
let _res = Self::proc(|c| c.set_is_stopped().map_err(|e| e.into()));
|
2023-06-19 22:52:17 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 00:50:01 +08:00
|
|
|
pub fn enable(enabled: bool) {
|
|
|
|
let mut lock = CONTEXT_SEND.addr.lock().unwrap();
|
2022-10-26 22:37:45 +08:00
|
|
|
if enabled {
|
2023-08-24 22:34:12 +08:00
|
|
|
if lock.is_none() {
|
2023-06-29 11:28:55 +08:00
|
|
|
match crate::create_cliprdr_context(
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS,
|
|
|
|
) {
|
2022-10-26 22:37:45 +08:00
|
|
|
Ok(context) => {
|
|
|
|
log::info!("clipboard context for file transfer created.");
|
2023-08-24 22:34:12 +08:00
|
|
|
*lock = Some(context)
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::error!(
|
2023-10-07 17:26:20 +08:00
|
|
|
"create clipboard context for file transfer: {}",
|
2022-10-26 22:37:45 +08:00
|
|
|
err.to_string()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-04 15:38:53 +08:00
|
|
|
} else if let Some(_clp) = lock.take() {
|
|
|
|
*lock = None;
|
|
|
|
log::info!("clipboard context for file transfer destroyed.");
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:34:12 +08:00
|
|
|
pub fn proc<F: FnOnce(&mut Box<dyn CliprdrServiceContext>) -> ResultType<()>>(
|
|
|
|
f: F,
|
|
|
|
) -> ResultType<()> {
|
|
|
|
let mut lock = CONTEXT_SEND.addr.lock().unwrap();
|
|
|
|
match lock.as_mut() {
|
|
|
|
Some(context) => f(context),
|
|
|
|
None => Ok(()),
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|