mirror of
https://github.com/NotAShelf/stash.git
synced 2026-05-18 21:17:35 +00:00
watch: add some logging to get_contents
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I31da28f4da619acb0c442d52d1a8990a6a6a6964
This commit is contained in:
parent
340d02d09a
commit
f0ff94a939
1 changed files with 81 additions and 62 deletions
|
|
@ -52,96 +52,115 @@ fn get_contents(
|
||||||
types_preferred: &[String],
|
types_preferred: &[String],
|
||||||
detection_smart: bool,
|
detection_smart: bool,
|
||||||
) -> Result<(Box<dyn std::io::Read>, String), Box<dyn std::error::Error>> {
|
) -> Result<(Box<dyn std::io::Read>, String), Box<dyn std::error::Error>> {
|
||||||
|
log::debug!(
|
||||||
|
"attempted to get clipboard contents with \
|
||||||
|
smart_detection={detection_smart}, preferred_types={types_preferred:?}"
|
||||||
|
);
|
||||||
|
|
||||||
if !types_preferred.is_empty() && detection_smart {
|
if !types_preferred.is_empty() && detection_smart {
|
||||||
match get_mime_types(clipboard, seat) {
|
log::debug!("querying available mime types with user preferences");
|
||||||
Ok(types) => {
|
if let Ok(types) = get_mime_types(clipboard, seat) {
|
||||||
for preferred in types_preferred {
|
log::debug!("Available MIME types: {types:?}");
|
||||||
// Handle wildcards (e.g., "image/*")
|
log::debug!("trying user preferred types in order: {types_preferred:?}");
|
||||||
if preferred.ends_with("/*") {
|
|
||||||
let prefix = &preferred[..preferred.len() - 2];
|
for preferred in types_preferred {
|
||||||
for mime_type in &types {
|
// Handle wildcards (e.g., "image/*")
|
||||||
if mime_type.starts_with(prefix) {
|
if preferred.ends_with("/*") {
|
||||||
let mime_str = mime_type.clone();
|
let prefix = &preferred[..preferred.len() - 2];
|
||||||
let (reader, _) = wl_get_contents(
|
for mime_type in &types {
|
||||||
clipboard,
|
if mime_type.starts_with(prefix) {
|
||||||
seat,
|
let mime_str = mime_type.clone();
|
||||||
MimeType::Specific(&mime_str),
|
|
||||||
)?;
|
|
||||||
return Ok((
|
|
||||||
Box::new(reader) as Box<dyn std::io::Read>,
|
|
||||||
mime_str,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Exact match
|
|
||||||
if types.contains(preferred) {
|
|
||||||
let (reader, _) = wl_get_contents(
|
let (reader, _) = wl_get_contents(
|
||||||
clipboard,
|
clipboard,
|
||||||
seat,
|
seat,
|
||||||
MimeType::Specific(preferred),
|
MimeType::Specific(&mime_str),
|
||||||
)?;
|
)?;
|
||||||
return Ok((
|
return Ok((
|
||||||
Box::new(reader) as Box<dyn std::io::Read>,
|
Box::new(reader) as Box<dyn std::io::Read>,
|
||||||
preferred.clone(),
|
mime_str,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log::warn!("no matches found for wildcard pattern '{preferred}'");
|
||||||
|
} else {
|
||||||
|
// Exact match
|
||||||
|
if types.contains(preferred) {
|
||||||
|
log::debug!("selected MIME type '{preferred}' (exact match)");
|
||||||
|
let (reader, _) =
|
||||||
|
wl_get_contents(clipboard, seat, MimeType::Specific(preferred))?;
|
||||||
|
return Ok((
|
||||||
|
Box::new(reader) as Box<dyn std::io::Read>,
|
||||||
|
preferred.clone(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
log::info!("exact match '{preferred}' not found in available types");
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(_) => {
|
log::warn!(
|
||||||
// Fall back to regular behavior if mime type query fails
|
"none of the preferred types matched available types, falling back to \
|
||||||
},
|
default priority"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Fall back to regular behavior if mime type query fails
|
||||||
|
log::warn!("failed to query available MIME types, falling back to Any");
|
||||||
}
|
}
|
||||||
} else if detection_smart {
|
} else if detection_smart {
|
||||||
// Default for "smart" detection:
|
// Default for "smart" detection:
|
||||||
// prioritize images > text/plain > other text > other
|
// prioritize images > text/plain > other text > other
|
||||||
// It is as smart as I am, and to be honest, that's not very smart
|
// It is as smart as I am, and to be honest, that's not very smart
|
||||||
match get_mime_types(clipboard, seat) {
|
if let Ok(types) = get_mime_types(clipboard, seat) {
|
||||||
Ok(types) => {
|
log::debug!("available MIME types: {types:?}");
|
||||||
// Priority order: images > text/plain > other text > other
|
|
||||||
for mime_type in &types {
|
|
||||||
if mime_type.starts_with("image/") {
|
|
||||||
let mime_str = mime_type.clone();
|
|
||||||
let (reader, _) =
|
|
||||||
wl_get_contents(clipboard, seat, MimeType::Specific(&mime_str))?;
|
|
||||||
return Ok((Box::new(reader) as Box<dyn std::io::Read>, mime_str));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if types.contains("text/plain") {
|
// Priority order: images > text/plain > other text > other
|
||||||
let (reader, _) = wl_get_contents(clipboard, seat, MimeType::Text)?;
|
for mime_type in &types {
|
||||||
return Ok((
|
if mime_type.starts_with("image/") {
|
||||||
Box::new(reader) as Box<dyn std::io::Read>,
|
let mime_str = mime_type.clone();
|
||||||
"text/plain".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
for mime_type in &types {
|
|
||||||
if mime_type.starts_with("text/") {
|
|
||||||
let mime_str = mime_type.clone();
|
|
||||||
let (reader, _) =
|
|
||||||
wl_get_contents(clipboard, seat, MimeType::Specific(&mime_str))?;
|
|
||||||
return Ok((Box::new(reader) as Box<dyn std::io::Read>, mime_str));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback to first available
|
|
||||||
if let Some(first_type) = types.iter().next() {
|
|
||||||
let mime_str = first_type.clone();
|
|
||||||
let (reader, _) =
|
let (reader, _) =
|
||||||
wl_get_contents(clipboard, seat, MimeType::Specific(&mime_str))?;
|
wl_get_contents(clipboard, seat, MimeType::Specific(&mime_str))?;
|
||||||
return Ok((Box::new(reader) as Box<dyn std::io::Read>, mime_str));
|
return Ok((Box::new(reader) as Box<dyn std::io::Read>, mime_str));
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(_) => {
|
|
||||||
// Fall back to regular behavior if mime type query fails
|
log::debug!("no image formats found, checking for text/plain");
|
||||||
},
|
if types.contains("text/plain") {
|
||||||
|
let (reader, _) = wl_get_contents(clipboard, seat, MimeType::Text)?;
|
||||||
|
return Ok((
|
||||||
|
Box::new(reader) as Box<dyn std::io::Read>,
|
||||||
|
"text/plain".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
log::debug!("no text/plain found, checking for other text formats");
|
||||||
|
for mime_type in &types {
|
||||||
|
if mime_type.starts_with("text/") {
|
||||||
|
let mime_str = mime_type.clone();
|
||||||
|
let (reader, _) =
|
||||||
|
wl_get_contents(clipboard, seat, MimeType::Specific(&mime_str))?;
|
||||||
|
return Ok((Box::new(reader) as Box<dyn std::io::Read>, mime_str));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to first available
|
||||||
|
log::info!("no preferred formats found, using first available type");
|
||||||
|
if let Some(first_type) = types.iter().next() {
|
||||||
|
let mime_str = first_type.clone();
|
||||||
|
let (reader, _) =
|
||||||
|
wl_get_contents(clipboard, seat, MimeType::Specific(&mime_str))?;
|
||||||
|
return Ok((Box::new(reader) as Box<dyn std::io::Read>, mime_str));
|
||||||
|
}
|
||||||
|
|
||||||
|
log::warn!("no MIME types available from clipboard");
|
||||||
|
} else {
|
||||||
|
// Fall back to regular behavior if mime type query fails
|
||||||
|
log::warn!("failed to query available MIME types, falling back to Any");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log::debug!("smart MIME detection is not enabled, using MimeType::Any");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to `Any` if smart detection is disabled or fails
|
// Fallback to Any if smart detection is disabled or fails
|
||||||
let (reader, _) = wl_get_contents(clipboard, seat, MimeType::Any)?;
|
let (reader, _) = wl_get_contents(clipboard, seat, MimeType::Any)?;
|
||||||
|
log::info!("selected MIME type 'application/octet-stream'");
|
||||||
Ok((
|
Ok((
|
||||||
Box::new(reader) as Box<dyn std::io::Read>,
|
Box::new(reader) as Box<dyn std::io::Read>,
|
||||||
"application/octet-stream".to_string(),
|
"application/octet-stream".to_string(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue