| 1 |
use std::os::fd::AsFd; |
| 2 |
|
| 3 |
use serde::{ |
| 4 |
Deserialize, de, |
| 5 |
ser::{Serialize, SerializeTuple}, |
| 6 |
}; |
| 7 |
use zbus::zvariant::{self, OwnedValue, Type, Value}; |
| 8 |
|
| 9 |
use crate::{Error, Uri}; |
| 10 |
|
| 11 |
#[derive(Debug, Type)] |
| 12 |
#[zvariant(signature = "(sv)")] |
| 13 |
/// A representation of an icon. |
| 14 |
/// |
| 15 |
/// Used by both the Notification & Dynamic launcher portals. |
| 16 |
pub enum Icon { |
| 17 |
/// An icon URI. |
| 18 |
Uri(Uri), |
| 19 |
/// A list of icon names. |
| 20 |
Names(Vec<String>), |
| 21 |
/// Icon bytes. |
| 22 |
Bytes(Vec<u8>), |
| 23 |
/// A file descriptor. |
| 24 |
FileDescriptor(std::os::fd::OwnedFd), |
| 25 |
} |
| 26 |
|
| 27 |
impl Icon { |
| 28 |
/// Create an icon from a list of names. |
| 29 |
pub fn with_names<N>(names: impl IntoIterator<Item = N>) -> Self |
| 30 |
where |
| 31 |
N: ToString, |
| 32 |
{ |
| 33 |
Self::Names(names.into_iter().map(|name| name.to_string()).collect()) |
| 34 |
} |
| 35 |
|
| 36 |
pub(crate) fn is_bytes(&self) -> bool { |
| 37 |
matches!(self, Self::Bytes(_)) |
| 38 |
} |
| 39 |
|
| 40 |
pub(crate) fn inner_bytes<'a>(&'a self) -> Value<'a> { |
| 41 |
match self { |
| 42 |
Self::Bytes(bytes) => { |
| 43 |
let mut array = zvariant::Array::new(u8::SIGNATURE); |
| 44 |
for byte in bytes.iter() { |
| 45 |
// Safe to unwrap because we are sure it is of the correct type |
| 46 |
array.append(Value::from(*byte)).unwrap(); |
| 47 |
} |
| 48 |
Value::from(array) |
| 49 |
} |
| 50 |
_ => panic!("Only bytes icons can be converted to a bytes variant"), |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
pub(crate) fn as_value<'a>(&'a self) -> Value<'a> { |
| 55 |
let tuple = match self { |
| 56 |
Self::Uri(uri) => ("file", Value::from(uri.as_str())), |
| 57 |
Self::Names(names) => { |
| 58 |
let mut array = zvariant::Array::new(String::SIGNATURE); |
| 59 |
for name in names.iter() { |
| 60 |
// Safe to unwrap because we are sure it is of the correct type |
| 61 |
array.append(Value::from(name)).unwrap(); |
| 62 |
} |
| 63 |
|
| 64 |
("themed", Value::from(array)) |
| 65 |
} |
| 66 |
Self::Bytes(_) => ("bytes", self.inner_bytes()), |
| 67 |
Self::FileDescriptor(fd) => ("file-descriptor", zvariant::Fd::from(fd).into()), |
| 68 |
}; |
| 69 |
Value::new(tuple) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl Serialize for Icon { |
| 74 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 75 |
where |
| 76 |
S: serde::Serializer, |
| 77 |
{ |
| 78 |
let mut tuple = serializer.serialize_tuple(2)?; |
| 79 |
match self { |
| 80 |
Self::Uri(uri) => { |
| 81 |
tuple.serialize_element("file")?; |
| 82 |
tuple.serialize_element(&Value::from(uri.as_str()))?; |
| 83 |
} |
| 84 |
Self::Names(names) => { |
| 85 |
tuple.serialize_element("themed")?; |
| 86 |
let mut array = zvariant::Array::new(String::SIGNATURE); |
| 87 |
for name in names.iter() { |
| 88 |
// Safe to unwrap because we are sure it is of the correct type |
| 89 |
array.append(Value::from(name)).unwrap(); |
| 90 |
} |
| 91 |
tuple.serialize_element(&Value::from(array))?; |
| 92 |
} |
| 93 |
Self::Bytes(_) => { |
| 94 |
tuple.serialize_element("bytes")?; |
| 95 |
tuple.serialize_element(&self.inner_bytes())?; |
| 96 |
} |
| 97 |
Self::FileDescriptor(fd) => { |
| 98 |
tuple.serialize_element("file-descriptor")?; |
| 99 |
tuple.serialize_element(&Value::from(zvariant::Fd::from(fd)))?; |
| 100 |
} |
| 101 |
} |
| 102 |
tuple.end() |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl<'de> Deserialize<'de> for Icon { |
| 107 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 108 |
where |
| 109 |
D: serde::Deserializer<'de>, |
| 110 |
{ |
| 111 |
let (type_, data) = <(String, OwnedValue)>::deserialize(deserializer)?; |
| 112 |
match type_.as_str() { |
| 113 |
"file" => { |
| 114 |
let uri_str = data.downcast_ref::<zvariant::Str>().unwrap(); |
| 115 |
let uri = Uri::parse(uri_str.as_str()) |
| 116 |
.map_err(|e| serde::de::Error::custom(format!("Invalid URI: {}", e)))?; |
| 117 |
Ok(Self::Uri(uri)) |
| 118 |
} |
| 119 |
"bytes" => { |
| 120 |
let array = data.downcast_ref::<zvariant::Array>().unwrap(); |
| 121 |
let mut bytes = Vec::with_capacity(array.len()); |
| 122 |
for byte in array.inner() { |
| 123 |
bytes.push(byte.downcast_ref::<u8>().unwrap()); |
| 124 |
} |
| 125 |
Ok(Self::Bytes(bytes)) |
| 126 |
} |
| 127 |
"themed" => { |
| 128 |
let array = data.downcast_ref::<zvariant::Array>().unwrap(); |
| 129 |
let mut names = Vec::with_capacity(array.len()); |
| 130 |
for value in array.inner() { |
| 131 |
let name = value.downcast_ref::<zvariant::Str>().unwrap(); |
| 132 |
names.push(name.as_str().to_owned()); |
| 133 |
} |
| 134 |
Ok(Self::Names(names)) |
| 135 |
} |
| 136 |
"file-descriptor" => { |
| 137 |
let fd = data.downcast_ref::<zvariant::Fd>().unwrap(); |
| 138 |
Ok(Self::FileDescriptor( |
| 139 |
fd.as_fd() |
| 140 |
.try_clone_to_owned() |
| 141 |
.expect("Failed to clone file descriptor"), |
| 142 |
)) |
| 143 |
} |
| 144 |
_ => Err(de::Error::custom("Invalid Icon type")), |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
impl TryFrom<&OwnedValue> for Icon { |
| 150 |
type Error = crate::Error; |
| 151 |
fn try_from(value: &OwnedValue) -> Result<Self, Self::Error> { |
| 152 |
let structure = value.downcast_ref::<zvariant::Structure>().unwrap(); |
| 153 |
let fields = structure.fields(); |
| 154 |
let type_ = fields[0].downcast_ref::<zvariant::Str>().unwrap(); |
| 155 |
match type_.as_str() { |
| 156 |
"file" => { |
| 157 |
let uri_str = fields[1] |
| 158 |
.downcast_ref::<zvariant::Str>() |
| 159 |
.unwrap() |
| 160 |
.to_owned(); |
| 161 |
let uri = Uri::parse(uri_str.as_str()).map_err(crate::Error::Uri)?; |
| 162 |
Ok(Self::Uri(uri)) |
| 163 |
} |
| 164 |
"bytes" => { |
| 165 |
let array = fields[1].downcast_ref::<zvariant::Array>().unwrap(); |
| 166 |
let mut bytes = Vec::with_capacity(array.len()); |
| 167 |
for byte in array.inner() { |
| 168 |
bytes.push(byte.downcast_ref::<u8>().unwrap()); |
| 169 |
} |
| 170 |
Ok(Self::Bytes(bytes)) |
| 171 |
} |
| 172 |
"themed" => { |
| 173 |
let array = fields[1].downcast_ref::<zvariant::Array>().unwrap(); |
| 174 |
let mut names = Vec::with_capacity(array.len()); |
| 175 |
for value in array.inner() { |
| 176 |
let name = value.downcast_ref::<zvariant::Str>().unwrap(); |
| 177 |
names.push(name.as_str().to_owned()); |
| 178 |
} |
| 179 |
Ok(Self::Names(names)) |
| 180 |
} |
| 181 |
"file-descriptor" => { |
| 182 |
let fd = fields[1].downcast_ref::<zvariant::Fd>().unwrap(); |
| 183 |
Ok(Self::FileDescriptor( |
| 184 |
fd.as_fd() |
| 185 |
.try_clone_to_owned() |
| 186 |
.expect("Failed to clone file descriptor"), |
| 187 |
)) |
| 188 |
} |
| 189 |
_ => Err(Error::ParseError("Invalid Icon type")), |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
impl TryFrom<OwnedValue> for Icon { |
| 195 |
type Error = crate::Error; |
| 196 |
fn try_from(value: OwnedValue) -> Result<Self, Self::Error> { |
| 197 |
Self::try_from(&value) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
impl TryFrom<Value<'_>> for Icon { |
| 202 |
type Error = crate::Error; |
| 203 |
fn try_from(value: Value<'_>) -> Result<Self, Self::Error> { |
| 204 |
Self::try_from(&value) |
| 205 |
} |
| 206 |
} |
| 207 |
impl TryFrom<&Value<'_>> for Icon { |
| 208 |
type Error = crate::Error; |
| 209 |
fn try_from(value: &Value<'_>) -> Result<Self, Self::Error> { |
| 210 |
Self::try_from(value.try_to_owned()?) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
#[cfg(test)] |
| 215 |
mod test { |
| 216 |
use zbus::zvariant::{Endian, serialized::Context, to_bytes}; |
| 217 |
|
| 218 |
use super::*; |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn check_icon_signature() { |
| 222 |
assert_eq!(Icon::SIGNATURE, "(sv)"); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn serialize_deserialize() { |
| 227 |
let ctxt = Context::new_dbus(Endian::Little, 0); |
| 228 |
|
| 229 |
let icon = Icon::with_names(["dialog-symbolic"]); |
| 230 |
|
| 231 |
let encoded = to_bytes(ctxt, &icon).unwrap(); |
| 232 |
let decoded: Icon = encoded.deserialize().unwrap().0; |
| 233 |
assert!(matches!(decoded, Icon::Names(_))); |
| 234 |
|
| 235 |
let icon = Icon::Uri(Uri::parse("file://some/icon.png").unwrap()); |
| 236 |
let encoded = to_bytes(ctxt, &icon).unwrap(); |
| 237 |
let decoded: Icon = encoded.deserialize().unwrap().0; |
| 238 |
assert!(matches!(decoded, Icon::Uri(_))); |
| 239 |
|
| 240 |
let icon = Icon::Bytes(vec![1, 0, 1, 0]); |
| 241 |
let encoded = to_bytes(ctxt, &icon).unwrap(); |
| 242 |
let decoded: Icon = encoded.deserialize().unwrap().0; |
| 243 |
assert!(matches!(decoded, Icon::Bytes(_))); |
| 244 |
|
| 245 |
let fd = std::fs::File::open("/tmp").unwrap(); |
| 246 |
let icon = Icon::FileDescriptor(fd.as_fd().try_clone_to_owned().unwrap()); |
| 247 |
let encoded = to_bytes(ctxt, &icon).unwrap(); |
| 248 |
let decoded: Icon = encoded.deserialize().unwrap().0; |
| 249 |
assert!(matches!(decoded, Icon::FileDescriptor(_))); |
| 250 |
} |
| 251 |
} |