Skip to content

Commit

Permalink
mime: use text/plain as a fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
kchibisov committed Oct 10, 2023
1 parent 3139220 commit a1779ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Update SCTK to 0.18
- Fix active polling of the clipboard each 50ms
- Fix freeze when copying data larger than the pipe buffer size
- Accept text/plain mime type as a fallback

## 0.6.6 -- 2022-06-20

Expand Down
13 changes: 11 additions & 2 deletions src/mime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// List of allowed mimes.
pub static ALLOWED_MIME_TYPES: [&str; 2] = ["text/plain;charset=utf-8", "UTF8_STRING"];
pub static ALLOWED_MIME_TYPES: [&str; 3] =
["text/plain;charset=utf-8", "UTF8_STRING", "text/plain"];

/// Mime type supported by clipboard.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
Expand All @@ -13,6 +14,10 @@ pub enum MimeType {
/// Some X11 clients are using only this mime type, so we
/// should have it as a fallback just in case.
Utf8String = 1,
/// text/plain mime type.
///
/// Fallback without charset parameter.
TextPlain = 2,
}

impl MimeType {
Expand All @@ -21,15 +26,19 @@ impl MimeType {
/// `find_allowed()` searches for mime type clipboard supports, if we have a
/// match, returns `Some(MimeType)`, otherwise `None`.
pub fn find_allowed(offered_mime_types: &[String]) -> Option<Self> {
let mut fallback = None;
for offered_mime_type in offered_mime_types.iter() {
if offered_mime_type == ALLOWED_MIME_TYPES[Self::TextPlainUtf8 as usize] {
return Some(Self::TextPlainUtf8);
} else if offered_mime_type == ALLOWED_MIME_TYPES[Self::Utf8String as usize] {
return Some(Self::Utf8String);
} else if offered_mime_type == ALLOWED_MIME_TYPES[Self::TextPlain as usize] {
// Only use this mime type as a fallback.
fallback = Some(Self::TextPlain);
}
}

None
fallback
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ impl State {

// Post-process the content according to mime type.
let content = match mime_type {
MimeType::TextPlainUtf8 => normalize_to_lf(content),
MimeType::TextPlainUtf8 | MimeType::TextPlain => {
normalize_to_lf(content)
},
MimeType::Utf8String => content,
};

Expand Down

0 comments on commit a1779ce

Please sign in to comment.