use std::fmt::Debug;
use futures_util::Stream;
use serde::Deserialize;
use serde_repr::Serialize_repr;
use zbus::zvariant::{DeserializeDict, ObjectPath, OwnedObjectPath, SerializeDict, Type};
use super::{HandleToken, Request, Session, session::SessionPortal};
use crate::{Error, WindowIdentifier, proxy::Proxy, window_identifier::MaybeWindowIdentifierExt};
#[cfg_attr(feature = "glib", derive(glib::Enum))]
#[cfg_attr(feature = "glib", enum_type(name = "AshpdLocationAccuracy"))]
#[derive(Serialize_repr, PartialEq, Eq, Clone, Copy, Debug, Type)]
#[doc(alias = "XdpLocationAccuracy")]
#[repr(u32)]
pub enum Accuracy {
#[doc(alias = "XDP_LOCATION_ACCURACY_NONE")]
None = 0,
#[doc(alias = "XDP_LOCATION_ACCURACY_COUNTRY")]
Country = 1,
#[doc(alias = "XDP_LOCATION_ACCURACY_CITY")]
City = 2,
#[doc(alias = "XDP_LOCATION_ACCURACY_NEIGHBORHOOD")]
Neighborhood = 3,
#[doc(alias = "XDP_LOCATION_ACCURACY_STREET")]
Street = 4,
#[doc(alias = "XDP_LOCATION_ACCURACY_EXACT")]
Exact = 5,
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct CreateSessionOptions {
session_handle_token: HandleToken,
#[zvariant(rename = "distance-threshold")]
distance_threshold: Option<u32>,
#[zvariant(rename = "time-threshold")]
time_threshold: Option<u32>,
accuracy: Option<Accuracy>,
}
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct SessionStartOptions {
handle_token: HandleToken,
}
#[derive(Deserialize, Type)]
pub struct Location(OwnedObjectPath, LocationInner);
impl Location {
pub fn session_handle(&self) -> ObjectPath<'_> {
self.0.as_ref()
}
pub fn accuracy(&self) -> f64 {
self.1.accuracy
}
pub fn altitude(&self) -> Option<f64> {
if self.1.altitude == -f64::MAX {
None
} else {
Some(self.1.altitude)
}
}
pub fn speed(&self) -> Option<f64> {
if self.1.speed == -1f64 {
None
} else {
Some(self.1.speed)
}
}
pub fn heading(&self) -> Option<f64> {
if self.1.heading == -1f64 {
None
} else {
Some(self.1.heading)
}
}
pub fn description(&self) -> Option<&str> {
if self.1.description.is_empty() {
None
} else {
Some(&self.1.description)
}
}
pub fn latitude(&self) -> f64 {
self.1.latitude
}
pub fn longitude(&self) -> f64 {
self.1.longitude
}
pub fn timestamp(&self) -> std::time::Duration {
std::time::Duration::from_secs(self.1.timestamp.0)
}
}
impl Debug for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Location")
.field("accuracy", &self.accuracy())
.field("altitude", &self.altitude())
.field("speed", &self.speed())
.field("heading", &self.heading())
.field("description", &self.description())
.field("latitude", &self.latitude())
.field("longitude", &self.longitude())
.field("timestamp", &self.timestamp())
.finish()
}
}
#[derive(Debug, SerializeDict, DeserializeDict, Type)]
#[zvariant(signature = "dict")]
struct LocationInner {
#[zvariant(rename = "Accuracy")]
accuracy: f64,
#[zvariant(rename = "Altitude")]
altitude: f64,
#[zvariant(rename = "Speed")]
speed: f64,
#[zvariant(rename = "Heading")]
heading: f64,
#[zvariant(rename = "Description")]
description: String,
#[zvariant(rename = "Latitude")]
latitude: f64,
#[zvariant(rename = "Longitude")]
longitude: f64,
#[zvariant(rename = "Timestamp")]
timestamp: (u64, u64),
}
#[derive(Debug, Clone)]
#[doc(alias = "org.freedesktop.portal.Location")]
pub struct LocationProxy(Proxy<'static>);
impl LocationProxy {
pub async fn new() -> Result<Self, Error> {
let proxy = Proxy::new_desktop("org.freedesktop.portal.Location").await?;
Ok(Self(proxy))
}
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> {
let proxy =
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Location")
.await?;
Ok(Self(proxy))
}
#[doc(alias = "LocationUpdated")]
#[doc(alias = "XdpPortal::location-updated")]
pub async fn receive_location_updated(
&self,
) -> Result<impl Stream<Item = Location> + use<'_>, Error> {
self.0.signal("LocationUpdated").await
}
#[doc(alias = "CreateSession")]
pub async fn create_session(
&self,
distance_threshold: Option<u32>,
time_threshold: Option<u32>,
accuracy: Option<Accuracy>,
) -> Result<Session<Self>, Error> {
let options = CreateSessionOptions {
distance_threshold,
time_threshold,
accuracy,
..Default::default()
};
let (path, proxy) = futures_util::try_join!(
self.0.call::<OwnedObjectPath>("CreateSession", &(options)),
Session::from_unique_name(self.0.connection().clone(), &options.session_handle_token),
)?;
assert_eq!(proxy.path(), &path.into_inner());
Ok(proxy)
}
#[doc(alias = "Start")]
#[doc(alias = "xdp_portal_location_monitor_start")]
pub async fn start(
&self,
session: &Session<Self>,
identifier: Option<&WindowIdentifier>,
) -> Result<Request<()>, Error> {
let options = SessionStartOptions::default();
let identifier = identifier.to_string_or_empty();
self.0
.empty_request(
&options.handle_token,
"Start",
&(session, &identifier, &options),
)
.await
}
}
impl crate::Sealed for LocationProxy {}
impl SessionPortal for LocationProxy {}
impl std::ops::Deref for LocationProxy {
type Target = zbus::Proxy<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}