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-10-27 20:40:23 +08:00
|
|
|
if lock.is_some() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match crate::create_cliprdr_context(true, false, CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS) {
|
|
|
|
Ok(context) => {
|
|
|
|
log::info!("clipboard context for file transfer created.");
|
|
|
|
*lock = Some(context)
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
log::error!(
|
|
|
|
"create clipboard context for file transfer: {}",
|
|
|
|
err.to_string()
|
|
|
|
);
|
2022-10-26 22:37:45 +08:00
|
|
|
}
|
|
|
|
}
|
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-10-30 12:00:44 +08:00
|
|
|
/// make sure the clipboard context is enabled.
|
|
|
|
pub fn make_sure_enabled() -> ResultType<()> {
|
|
|
|
let mut lock = CONTEXT_SEND.addr.lock().unwrap();
|
|
|
|
if lock.is_some() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let ctx = crate::create_cliprdr_context(true, false, CLIPBOARD_RESPONSE_WAIT_TIMEOUT_SECS)?;
|
|
|
|
*lock = Some(ctx);
|
|
|
|
log::info!("clipboard context for file transfer recreated.");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|