2023-06-06 11:41:30 -06:00
|
|
|
package xgbsel
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
import "strings"
|
|
|
|
|
2024-07-05 21:27:53 -06:00
|
|
|
// Data represents X selection data.
|
2023-06-06 11:41:30 -06:00
|
|
|
type Data interface {
|
2024-07-05 21:27:53 -06:00
|
|
|
// Convert converts the data to the specified target and returns it. If
|
|
|
|
// the target is not supported, this behavior will return false for ok.
|
|
|
|
Convert (Target) (reader io.ReadSeekCloser, ok bool)
|
|
|
|
|
|
|
|
// Supported returns a slice of targets that Convert can accept. This
|
|
|
|
// can just be the result of MimeToTargets.
|
2023-06-06 11:41:30 -06:00
|
|
|
Supported () []Target
|
|
|
|
}
|
|
|
|
|
|
|
|
// Target represents an X selection target. It defines the type of data stored
|
2024-07-05 21:27:53 -06:00
|
|
|
// within an X selection. This data may be a MIME type, or a more specific name
|
2023-06-06 11:41:30 -06:00
|
|
|
// that is unique to X. A list of these names can be found here:
|
|
|
|
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.6.2
|
|
|
|
type Target string
|
|
|
|
|
2024-07-05 21:27:53 -06:00
|
|
|
// Confidence represents how accurate a conversion from a target to a MIME type
|
|
|
|
// is.
|
2023-06-06 11:41:30 -06:00
|
|
|
type Confidence int; const (
|
|
|
|
ConfidenceNone Confidence = iota
|
|
|
|
ConfidencePartial
|
|
|
|
ConfidenceFull
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToMime converts the specified target to a MIME type. Because a single MIME
|
|
|
|
// type may correspond to several targets, a confidence value is returned
|
2024-07-05 21:27:53 -06:00
|
|
|
// representing how one-to-one of a match it is. If some data is represented by
|
|
|
|
// multiple targets, they can each be checked individually and the one with the
|
|
|
|
// highest confidence value can be chosen. If a target cannot be converted to a
|
|
|
|
// MIME type, ("", ConfidenceNone) is returned.
|
2023-06-06 11:41:30 -06:00
|
|
|
func (target Target) ToMime () (string, Confidence) {
|
|
|
|
// TODO: add other stuff. reference this table:
|
|
|
|
// https://tronche.com/gui/x/icccm/sec-2.html#s-2.6.2
|
|
|
|
// perhaps we should also have parameters for mime types so we can
|
|
|
|
// return an encoding here for things like STRING?
|
|
|
|
switch target {
|
|
|
|
case "ADOBE_PORTABLE_DOCUMENT_FORMAT":
|
|
|
|
return "application/pdf", ConfidenceFull
|
|
|
|
case "APPLE_PICT":
|
|
|
|
return "image/pict", ConfidenceFull
|
|
|
|
case
|
|
|
|
"POSTSCRIPT",
|
|
|
|
"ENCAPSULATED_POSTSCRIPT",
|
|
|
|
"ENCAPSULATED_POSTSCRIPT_INTERCHANGE":
|
|
|
|
return "application/postscript", ConfidenceFull
|
|
|
|
case "FILE_NAME":
|
|
|
|
return "text/uri-list", ConfidenceFull
|
|
|
|
case "UTF8_STRING":
|
|
|
|
return "text/plain", ConfidenceFull
|
|
|
|
case "TEXT":
|
|
|
|
return "text/plain", ConfidencePartial
|
|
|
|
case "STRING":
|
|
|
|
return "text/plain", ConfidencePartial
|
|
|
|
default:
|
|
|
|
if strings.Count(string(target), "/") == 1 {
|
|
|
|
return string(target), ConfidenceFull
|
|
|
|
} else {
|
|
|
|
return "", ConfidenceNone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 21:27:53 -06:00
|
|
|
// MimeToTargets returns a slice of targets that correspond to a specified MIME
|
|
|
|
// type. The MIME type itself is always the first item of the slice. All targets
|
|
|
|
// returned by this function are guaranteed to convert to the given MIME type
|
|
|
|
// when ToMime is called on them.
|
2023-06-06 11:41:30 -06:00
|
|
|
func MimeToTargets (mime string) []Target {
|
|
|
|
targets := []Target { Target(mime) }
|
|
|
|
switch mime {
|
|
|
|
case "application/pdf":
|
|
|
|
targets = append(targets, "ADOBE_PORTABLE_DOCUMENT_FORMAT")
|
|
|
|
case "image/pict":
|
|
|
|
targets = append(targets, "APPLE_PICT")
|
|
|
|
case "application/postscript":
|
|
|
|
targets = append(targets, "POSTSCRIPT")
|
|
|
|
case "text/uri-list":
|
|
|
|
targets = append(targets, "FILE_NAME")
|
|
|
|
case "text/plain":
|
|
|
|
targets = append(targets, "UTF8_STRING", "TEXT", "STRING")
|
|
|
|
}
|
|
|
|
return targets
|
|
|
|
}
|