| 1 | use crate::{ |
| 2 | error::AtspiError, |
| 3 | events::{BusProperties, EventBodyOwned, HasMatchRule, HasRegistryEventString, ObjectRef}, |
| 4 | Event, EventProperties, EventTypeProperties, |
| 5 | }; |
| 6 | use zbus_names::UniqueName; |
| 7 | use zvariant::{ObjectPath, OwnedValue}; |
| 8 | |
| 9 | #[derive (Clone, Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash)] |
| 10 | pub enum DocumentEvents { |
| 11 | /// See: [`LoadCompleteEvent`]. |
| 12 | LoadComplete(LoadCompleteEvent), |
| 13 | /// See: [`ReloadEvent`]. |
| 14 | Reload(ReloadEvent), |
| 15 | /// See: [`LoadStoppedEvent`]. |
| 16 | LoadStopped(LoadStoppedEvent), |
| 17 | /// See: [`ContentChangedEvent`]. |
| 18 | ContentChanged(ContentChangedEvent), |
| 19 | /// See: [`AttributesChangedEvent`]. |
| 20 | AttributesChanged(AttributesChangedEvent), |
| 21 | /// See: [`PageChangedEvent`]. |
| 22 | PageChanged(PageChangedEvent), |
| 23 | } |
| 24 | |
| 25 | impl EventTypeProperties for DocumentEvents { |
| 26 | fn member(&self) -> &'static str { |
| 27 | match self { |
| 28 | Self::LoadComplete(inner) => inner.member(), |
| 29 | Self::Reload(inner) => inner.member(), |
| 30 | Self::LoadStopped(inner) => inner.member(), |
| 31 | Self::ContentChanged(inner) => inner.member(), |
| 32 | Self::AttributesChanged(inner) => inner.member(), |
| 33 | Self::PageChanged(inner) => inner.member(), |
| 34 | } |
| 35 | } |
| 36 | fn interface(&self) -> &'static str { |
| 37 | match self { |
| 38 | Self::LoadComplete(inner) => inner.interface(), |
| 39 | Self::Reload(inner) => inner.interface(), |
| 40 | Self::LoadStopped(inner) => inner.interface(), |
| 41 | Self::ContentChanged(inner) => inner.interface(), |
| 42 | Self::AttributesChanged(inner) => inner.interface(), |
| 43 | Self::PageChanged(inner) => inner.interface(), |
| 44 | } |
| 45 | } |
| 46 | fn match_rule(&self) -> &'static str { |
| 47 | match self { |
| 48 | Self::LoadComplete(inner) => inner.match_rule(), |
| 49 | Self::Reload(inner) => inner.match_rule(), |
| 50 | Self::LoadStopped(inner) => inner.match_rule(), |
| 51 | Self::ContentChanged(inner) => inner.match_rule(), |
| 52 | Self::AttributesChanged(inner) => inner.match_rule(), |
| 53 | Self::PageChanged(inner) => inner.match_rule(), |
| 54 | } |
| 55 | } |
| 56 | fn registry_string(&self) -> &'static str { |
| 57 | match self { |
| 58 | Self::LoadComplete(inner) => inner.registry_string(), |
| 59 | Self::Reload(inner) => inner.registry_string(), |
| 60 | Self::LoadStopped(inner) => inner.registry_string(), |
| 61 | Self::ContentChanged(inner) => inner.registry_string(), |
| 62 | Self::AttributesChanged(inner) => inner.registry_string(), |
| 63 | Self::PageChanged(inner) => inner.registry_string(), |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl EventProperties for DocumentEvents { |
| 69 | fn path(&self) -> ObjectPath<'_> { |
| 70 | match self { |
| 71 | Self::LoadComplete(inner: &LoadCompleteEvent) => inner.path(), |
| 72 | Self::Reload(inner: &ReloadEvent) => inner.path(), |
| 73 | Self::LoadStopped(inner: &LoadStoppedEvent) => inner.path(), |
| 74 | Self::ContentChanged(inner: &ContentChangedEvent) => inner.path(), |
| 75 | Self::AttributesChanged(inner: &AttributesChangedEvent) => inner.path(), |
| 76 | Self::PageChanged(inner: &PageChangedEvent) => inner.path(), |
| 77 | } |
| 78 | } |
| 79 | fn sender(&self) -> UniqueName<'_> { |
| 80 | match self { |
| 81 | Self::LoadComplete(inner: &LoadCompleteEvent) => inner.sender(), |
| 82 | Self::Reload(inner: &ReloadEvent) => inner.sender(), |
| 83 | Self::LoadStopped(inner: &LoadStoppedEvent) => inner.sender(), |
| 84 | Self::ContentChanged(inner: &ContentChangedEvent) => inner.sender(), |
| 85 | Self::AttributesChanged(inner: &AttributesChangedEvent) => inner.sender(), |
| 86 | Self::PageChanged(inner: &PageChangedEvent) => inner.sender(), |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl_from_interface_event_enum_for_event!(DocumentEvents, Event::Document); |
| 92 | impl_try_from_event_for_user_facing_event_type!(DocumentEvents, Event::Document); |
| 93 | event_wrapper_test_cases!(DocumentEvents, LoadCompleteEvent); |
| 94 | |
| 95 | impl HasMatchRule for DocumentEvents { |
| 96 | const MATCH_RULE_STRING: &'static str = |
| 97 | "type='signal',interface='org.a11y.atspi.Event.Document'" ; |
| 98 | } |
| 99 | |
| 100 | /// An event triggered by the completion of a document load action. |
| 101 | /// For example: a web page has finished loading its initial payload, or |
| 102 | /// `LibreOffice` has loaded a document from disk. |
| 103 | #[derive (Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] |
| 104 | pub struct LoadCompleteEvent { |
| 105 | /// The [`ObjectRef`] which the event applies to. |
| 106 | pub item: crate::events::ObjectRef, |
| 107 | } |
| 108 | |
| 109 | /// An event triggered by a reloading of a document. |
| 110 | /// For example: pressing F5, or `Control + r` will reload a page in a web browser. |
| 111 | #[derive (Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] |
| 112 | pub struct ReloadEvent { |
| 113 | /// The [`ObjectRef`] which the event applies to. |
| 114 | pub item: crate::events::ObjectRef, |
| 115 | } |
| 116 | |
| 117 | /// An event triggered by the cancelling of a document load. |
| 118 | /// For example: during the loading of a large web page, a user may press `Escape` to stop loading the page. |
| 119 | #[derive (Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] |
| 120 | pub struct LoadStoppedEvent { |
| 121 | /// The [`ObjectRef`] which the event applies to. |
| 122 | pub item: crate::events::ObjectRef, |
| 123 | } |
| 124 | |
| 125 | #[derive (Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] |
| 126 | pub struct ContentChangedEvent { |
| 127 | /// The [`ObjectRef`] which the event applies to. |
| 128 | pub item: crate::events::ObjectRef, |
| 129 | } |
| 130 | |
| 131 | #[derive (Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] |
| 132 | pub struct AttributesChangedEvent { |
| 133 | /// The [`ObjectRef`] which the event applies to. |
| 134 | pub item: crate::events::ObjectRef, |
| 135 | } |
| 136 | |
| 137 | #[derive (Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] |
| 138 | pub struct PageChangedEvent { |
| 139 | /// The [`ObjectRef`] which the event applies to. |
| 140 | pub item: crate::events::ObjectRef, |
| 141 | } |
| 142 | |
| 143 | impl BusProperties for LoadCompleteEvent { |
| 144 | const DBUS_MEMBER: &'static str = "LoadComplete" ; |
| 145 | const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Document" ; |
| 146 | const MATCH_RULE_STRING: &'static str = |
| 147 | "type='signal',interface='org.a11y.atspi.Event.Document',member='LoadComplete'" ; |
| 148 | const REGISTRY_EVENT_STRING: &'static str = "Document:" ; |
| 149 | |
| 150 | type Body = EventBodyOwned; |
| 151 | |
| 152 | fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> { |
| 153 | Ok(Self { item }) |
| 154 | } |
| 155 | fn body(&self) -> Self::Body { |
| 156 | let copy: LoadCompleteEvent = self.clone(); |
| 157 | copy.into() |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | impl BusProperties for ReloadEvent { |
| 162 | const DBUS_MEMBER: &'static str = "Reload" ; |
| 163 | const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Document" ; |
| 164 | const MATCH_RULE_STRING: &'static str = |
| 165 | "type='signal',interface='org.a11y.atspi.Event.Document',member='Reload'" ; |
| 166 | const REGISTRY_EVENT_STRING: &'static str = "Document:" ; |
| 167 | |
| 168 | type Body = EventBodyOwned; |
| 169 | |
| 170 | fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> { |
| 171 | Ok(Self { item }) |
| 172 | } |
| 173 | fn body(&self) -> Self::Body { |
| 174 | let copy: ReloadEvent = self.clone(); |
| 175 | copy.into() |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | impl BusProperties for LoadStoppedEvent { |
| 180 | const DBUS_MEMBER: &'static str = "LoadStopped" ; |
| 181 | const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Document" ; |
| 182 | const MATCH_RULE_STRING: &'static str = |
| 183 | "type='signal',interface='org.a11y.atspi.Event.Document',member='LoadStopped'" ; |
| 184 | const REGISTRY_EVENT_STRING: &'static str = "Document:" ; |
| 185 | |
| 186 | type Body = EventBodyOwned; |
| 187 | |
| 188 | fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> { |
| 189 | Ok(Self { item }) |
| 190 | } |
| 191 | fn body(&self) -> Self::Body { |
| 192 | let copy: LoadStoppedEvent = self.clone(); |
| 193 | copy.into() |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | impl BusProperties for ContentChangedEvent { |
| 198 | const DBUS_MEMBER: &'static str = "ContentChanged" ; |
| 199 | const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Document" ; |
| 200 | const MATCH_RULE_STRING: &'static str = |
| 201 | "type='signal',interface='org.a11y.atspi.Event.Document',member='ContentChanged'" ; |
| 202 | const REGISTRY_EVENT_STRING: &'static str = "Document:" ; |
| 203 | |
| 204 | type Body = EventBodyOwned; |
| 205 | |
| 206 | fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> { |
| 207 | Ok(Self { item }) |
| 208 | } |
| 209 | fn body(&self) -> Self::Body { |
| 210 | let copy: ContentChangedEvent = self.clone(); |
| 211 | copy.into() |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | impl BusProperties for AttributesChangedEvent { |
| 216 | const DBUS_MEMBER: &'static str = "AttributesChanged" ; |
| 217 | const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Document" ; |
| 218 | const MATCH_RULE_STRING: &'static str = |
| 219 | "type='signal',interface='org.a11y.atspi.Event.Document',member='AttributesChanged'" ; |
| 220 | const REGISTRY_EVENT_STRING: &'static str = "Document:" ; |
| 221 | |
| 222 | type Body = EventBodyOwned; |
| 223 | |
| 224 | fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> { |
| 225 | Ok(Self { item }) |
| 226 | } |
| 227 | fn body(&self) -> Self::Body { |
| 228 | let copy: AttributesChangedEvent = self.clone(); |
| 229 | copy.into() |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | impl BusProperties for PageChangedEvent { |
| 234 | const DBUS_MEMBER: &'static str = "PageChanged" ; |
| 235 | const DBUS_INTERFACE: &'static str = "org.a11y.atspi.Event.Document" ; |
| 236 | const MATCH_RULE_STRING: &'static str = |
| 237 | "type='signal',interface='org.a11y.atspi.Event.Document',member='PageChanged'" ; |
| 238 | const REGISTRY_EVENT_STRING: &'static str = "Document:" ; |
| 239 | |
| 240 | type Body = EventBodyOwned; |
| 241 | |
| 242 | fn from_message_parts(item: ObjectRef, _body: Self::Body) -> Result<Self, AtspiError> { |
| 243 | Ok(Self { item }) |
| 244 | } |
| 245 | fn body(&self) -> Self::Body { |
| 246 | let copy: PageChangedEvent = self.clone(); |
| 247 | copy.into() |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | #[cfg (feature = "zbus" )] |
| 252 | impl TryFrom<&zbus::Message> for DocumentEvents { |
| 253 | type Error = AtspiError; |
| 254 | fn try_from(ev: &zbus::Message) -> Result<Self, Self::Error> { |
| 255 | let header: Header<'_> = ev.header(); |
| 256 | let member: &MemberName<'_> = header |
| 257 | .member() |
| 258 | .ok_or(err:AtspiError::MemberMatch("Event without member" .into()))?; |
| 259 | match member.as_str() { |
| 260 | "LoadComplete" => Ok(DocumentEvents::LoadComplete(ev.try_into()?)), |
| 261 | "Reload" => Ok(DocumentEvents::Reload(ev.try_into()?)), |
| 262 | "LoadStopped" => Ok(DocumentEvents::LoadStopped(ev.try_into()?)), |
| 263 | "ContentChanged" => Ok(DocumentEvents::ContentChanged(ev.try_into()?)), |
| 264 | "AttributesChanged" => Ok(DocumentEvents::AttributesChanged(ev.try_into()?)), |
| 265 | "PageChanged" => Ok(DocumentEvents::PageChanged(ev.try_into()?)), |
| 266 | _ => Err(AtspiError::MemberMatch("No matching member for Document" .into())), |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | impl_from_user_facing_event_for_interface_event_enum!( |
| 272 | LoadCompleteEvent, |
| 273 | DocumentEvents, |
| 274 | DocumentEvents::LoadComplete |
| 275 | ); |
| 276 | impl_from_user_facing_type_for_event_enum!(LoadCompleteEvent, Event::Document); |
| 277 | impl_try_from_event_for_user_facing_type!( |
| 278 | LoadCompleteEvent, |
| 279 | DocumentEvents::LoadComplete, |
| 280 | Event::Document |
| 281 | ); |
| 282 | |
| 283 | event_test_cases!(LoadCompleteEvent); |
| 284 | impl_to_dbus_message!(LoadCompleteEvent); |
| 285 | impl_from_dbus_message!(LoadCompleteEvent); |
| 286 | impl_event_properties!(LoadCompleteEvent); |
| 287 | impl From<LoadCompleteEvent> for EventBodyOwned { |
| 288 | fn from(_event: LoadCompleteEvent) -> Self { |
| 289 | EventBodyOwned { |
| 290 | properties: std::collections::HashMap::new(), |
| 291 | kind: String::default(), |
| 292 | detail1: i32::default(), |
| 293 | detail2: i32::default(), |
| 294 | any_data: OwnedValue::from(0u8), |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | impl_from_user_facing_event_for_interface_event_enum!( |
| 300 | ReloadEvent, |
| 301 | DocumentEvents, |
| 302 | DocumentEvents::Reload |
| 303 | ); |
| 304 | impl_from_user_facing_type_for_event_enum!(ReloadEvent, Event::Document); |
| 305 | impl_try_from_event_for_user_facing_type!(ReloadEvent, DocumentEvents::Reload, Event::Document); |
| 306 | |
| 307 | event_test_cases!(ReloadEvent); |
| 308 | impl_to_dbus_message!(ReloadEvent); |
| 309 | impl_from_dbus_message!(ReloadEvent); |
| 310 | impl_event_properties!(ReloadEvent); |
| 311 | impl From<ReloadEvent> for EventBodyOwned { |
| 312 | fn from(_event: ReloadEvent) -> Self { |
| 313 | EventBodyOwned { |
| 314 | properties: std::collections::HashMap::new(), |
| 315 | kind: String::default(), |
| 316 | detail1: i32::default(), |
| 317 | detail2: i32::default(), |
| 318 | any_data: OwnedValue::from(0u8), |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | impl_from_user_facing_event_for_interface_event_enum!( |
| 324 | LoadStoppedEvent, |
| 325 | DocumentEvents, |
| 326 | DocumentEvents::LoadStopped |
| 327 | ); |
| 328 | impl_from_user_facing_type_for_event_enum!(LoadStoppedEvent, Event::Document); |
| 329 | impl_try_from_event_for_user_facing_type!( |
| 330 | LoadStoppedEvent, |
| 331 | DocumentEvents::LoadStopped, |
| 332 | Event::Document |
| 333 | ); |
| 334 | |
| 335 | event_test_cases!(LoadStoppedEvent); |
| 336 | impl_to_dbus_message!(LoadStoppedEvent); |
| 337 | impl_from_dbus_message!(LoadStoppedEvent); |
| 338 | impl_event_properties!(LoadStoppedEvent); |
| 339 | impl From<LoadStoppedEvent> for EventBodyOwned { |
| 340 | fn from(_event: LoadStoppedEvent) -> Self { |
| 341 | EventBodyOwned { |
| 342 | properties: std::collections::HashMap::new(), |
| 343 | kind: String::default(), |
| 344 | detail1: i32::default(), |
| 345 | detail2: i32::default(), |
| 346 | any_data: OwnedValue::from(0u8), |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | impl_from_user_facing_event_for_interface_event_enum!( |
| 352 | ContentChangedEvent, |
| 353 | DocumentEvents, |
| 354 | DocumentEvents::ContentChanged |
| 355 | ); |
| 356 | impl_from_user_facing_type_for_event_enum!(ContentChangedEvent, Event::Document); |
| 357 | impl_try_from_event_for_user_facing_type!( |
| 358 | ContentChangedEvent, |
| 359 | DocumentEvents::ContentChanged, |
| 360 | Event::Document |
| 361 | ); |
| 362 | |
| 363 | event_test_cases!(ContentChangedEvent); |
| 364 | impl_to_dbus_message!(ContentChangedEvent); |
| 365 | impl_from_dbus_message!(ContentChangedEvent); |
| 366 | impl_event_properties!(ContentChangedEvent); |
| 367 | impl From<ContentChangedEvent> for EventBodyOwned { |
| 368 | fn from(_event: ContentChangedEvent) -> Self { |
| 369 | EventBodyOwned { |
| 370 | properties: std::collections::HashMap::new(), |
| 371 | kind: String::default(), |
| 372 | detail1: i32::default(), |
| 373 | detail2: i32::default(), |
| 374 | any_data: OwnedValue::from(0u8), |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | impl_from_user_facing_event_for_interface_event_enum!( |
| 380 | AttributesChangedEvent, |
| 381 | DocumentEvents, |
| 382 | DocumentEvents::AttributesChanged |
| 383 | ); |
| 384 | impl_from_user_facing_type_for_event_enum!(AttributesChangedEvent, Event::Document); |
| 385 | impl_try_from_event_for_user_facing_type!( |
| 386 | AttributesChangedEvent, |
| 387 | DocumentEvents::AttributesChanged, |
| 388 | Event::Document |
| 389 | ); |
| 390 | |
| 391 | event_test_cases!(AttributesChangedEvent); |
| 392 | impl_to_dbus_message!(AttributesChangedEvent); |
| 393 | impl_from_dbus_message!(AttributesChangedEvent); |
| 394 | impl_event_properties!(AttributesChangedEvent); |
| 395 | impl From<AttributesChangedEvent> for EventBodyOwned { |
| 396 | fn from(_event: AttributesChangedEvent) -> Self { |
| 397 | EventBodyOwned { |
| 398 | properties: std::collections::HashMap::new(), |
| 399 | kind: String::default(), |
| 400 | detail1: i32::default(), |
| 401 | detail2: i32::default(), |
| 402 | any_data: OwnedValue::from(0u8), |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | impl_from_user_facing_event_for_interface_event_enum!( |
| 408 | PageChangedEvent, |
| 409 | DocumentEvents, |
| 410 | DocumentEvents::PageChanged |
| 411 | ); |
| 412 | impl_from_user_facing_type_for_event_enum!(PageChangedEvent, Event::Document); |
| 413 | impl_try_from_event_for_user_facing_type!( |
| 414 | PageChangedEvent, |
| 415 | DocumentEvents::PageChanged, |
| 416 | Event::Document |
| 417 | ); |
| 418 | |
| 419 | event_test_cases!(PageChangedEvent); |
| 420 | impl_to_dbus_message!(PageChangedEvent); |
| 421 | impl_from_dbus_message!(PageChangedEvent); |
| 422 | impl_event_properties!(PageChangedEvent); |
| 423 | impl From<PageChangedEvent> for EventBodyOwned { |
| 424 | fn from(_event: PageChangedEvent) -> Self { |
| 425 | EventBodyOwned { |
| 426 | properties: std::collections::HashMap::new(), |
| 427 | kind: String::default(), |
| 428 | detail1: i32::default(), |
| 429 | detail2: i32::default(), |
| 430 | any_data: OwnedValue::from(0u8), |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | impl HasRegistryEventString for DocumentEvents { |
| 436 | const REGISTRY_EVENT_STRING: &'static str = "Document:" ; |
| 437 | } |
| 438 | |