| 1 |
//! # Examples |
| 2 |
//! |
| 3 |
//! ```rust,no_run |
| 4 |
//! use ashpd::desktop::location::{Accuracy, CreateSessionOptions, LocationProxy}; |
| 5 |
//! use futures_util::{FutureExt, StreamExt}; |
| 6 |
//! |
| 7 |
//! async fn run() -> ashpd::Result<()> { |
| 8 |
//! let proxy = LocationProxy::new().await?; |
| 9 |
//! let session = proxy |
| 10 |
//! .create_session(CreateSessionOptions::default().set_accuracy(Accuracy::Street)) |
| 11 |
//! .await?; |
| 12 |
//! let mut stream = proxy.receive_location_updated().await?; |
| 13 |
//! let (_, location) = futures_util::join!( |
| 14 |
//! proxy |
| 15 |
//! .start(&session, None, Default::default()) |
| 16 |
//! .map(|e| e.expect("Couldn't start session")), |
| 17 |
//! stream.next().map(|e| e.expect("Stream is exhausted")) |
| 18 |
//! ); |
| 19 |
//! println!("{}", location.accuracy()); |
| 20 |
//! println!("{}", location.longitude()); |
| 21 |
//! println!("{}", location.latitude()); |
| 22 |
//! session.close().await?; |
| 23 |
//! Ok(()) |
| 24 |
//! } |
| 25 |
//! ``` |
| 26 |
|
| 27 |
use std::fmt::Debug; |
| 28 |
|
| 29 |
use futures_util::Stream; |
| 30 |
use serde::{Deserialize, Serialize}; |
| 31 |
use serde_repr::Serialize_repr; |
| 32 |
use zbus::zvariant::{ |
| 33 |
ObjectPath, Optional, OwnedObjectPath, Type, |
| 34 |
as_value::{self, optional}, |
| 35 |
}; |
| 36 |
|
| 37 |
use super::{HandleToken, Request, Session, session::SessionPortal}; |
| 38 |
use crate::{Error, WindowIdentifier, proxy::Proxy}; |
| 39 |
|
| 40 |
#[cfg_attr(feature = "glib", derive(glib::Enum))] |
| 41 |
#[cfg_attr(feature = "glib", enum_type(name = "AshpdLocationAccuracy"))] |
| 42 |
#[derive(Serialize_repr, PartialEq, Eq, Clone, Copy, Debug, Type)] |
| 43 |
#[doc(alias = "XdpLocationAccuracy")] |
| 44 |
#[repr(u32)] |
| 45 |
/// The accuracy of the location. |
| 46 |
pub enum Accuracy { |
| 47 |
#[doc(alias = "XDP_LOCATION_ACCURACY_NONE")] |
| 48 |
/// None. |
| 49 |
None = 0, |
| 50 |
#[doc(alias = "XDP_LOCATION_ACCURACY_COUNTRY")] |
| 51 |
/// Country. |
| 52 |
Country = 1, |
| 53 |
#[doc(alias = "XDP_LOCATION_ACCURACY_CITY")] |
| 54 |
/// City. |
| 55 |
City = 2, |
| 56 |
#[doc(alias = "XDP_LOCATION_ACCURACY_NEIGHBORHOOD")] |
| 57 |
/// Neighborhood. |
| 58 |
Neighborhood = 3, |
| 59 |
#[doc(alias = "XDP_LOCATION_ACCURACY_STREET")] |
| 60 |
/// Street. |
| 61 |
Street = 4, |
| 62 |
#[doc(alias = "XDP_LOCATION_ACCURACY_EXACT")] |
| 63 |
/// The exact location. |
| 64 |
Exact = 5, |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Serialize, Type, Debug, Default)] |
| 68 |
/// Specified options for a [`LocationProxy::create_session`] request. |
| 69 |
#[zvariant(signature = "dict")] |
| 70 |
pub struct CreateSessionOptions { |
| 71 |
#[serde(with = "as_value")] |
| 72 |
session_handle_token: HandleToken, |
| 73 |
#[serde( |
| 74 |
rename = "distance-threshold", |
| 75 |
with = "optional", |
| 76 |
skip_serializing_if = "Option::is_none" |
| 77 |
)] |
| 78 |
distance_threshold: Option<u32>, |
| 79 |
#[serde( |
| 80 |
rename = "time-threshold", |
| 81 |
with = "optional", |
| 82 |
skip_serializing_if = "Option::is_none" |
| 83 |
)] |
| 84 |
time_threshold: Option<u32>, |
| 85 |
#[serde(with = "optional", skip_serializing_if = "Option::is_none")] |
| 86 |
accuracy: Option<Accuracy>, |
| 87 |
} |
| 88 |
|
| 89 |
impl CreateSessionOptions { |
| 90 |
/// Distance threshold in meters. Default is 0. |
| 91 |
pub fn set_distance_threshold(mut self, distance_threshold: impl Into<Option<u32>>) -> Self { |
| 92 |
self.distance_threshold = distance_threshold.into(); |
| 93 |
self |
| 94 |
} |
| 95 |
|
| 96 |
/// Time threshold in seconds. Default is 0. |
| 97 |
pub fn set_time_threshold(mut self, time_threshold: impl Into<Option<u32>>) -> Self { |
| 98 |
self.time_threshold = time_threshold.into(); |
| 99 |
self |
| 100 |
} |
| 101 |
|
| 102 |
/// Requested accuracy. Default is `Accuracy::Exact`. |
| 103 |
pub fn set_accuracy(mut self, accuracy: impl Into<Option<Accuracy>>) -> Self { |
| 104 |
self.accuracy = accuracy.into(); |
| 105 |
self |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
#[derive(Serialize, Type, Debug, Default)] |
| 110 |
/// Specified options for a [`LocationProxy::start`] request. |
| 111 |
#[zvariant(signature = "dict")] |
| 112 |
pub struct StartOptions { |
| 113 |
#[serde(with = "as_value")] |
| 114 |
handle_token: HandleToken, |
| 115 |
} |
| 116 |
|
| 117 |
#[derive(Deserialize, Type)] |
| 118 |
/// The response received on a `location_updated` signal. |
| 119 |
pub struct Location(OwnedObjectPath, LocationInner); |
| 120 |
|
| 121 |
impl Location { |
| 122 |
/// The associated session. |
| 123 |
pub fn session_handle(&self) -> ObjectPath<'_> { |
| 124 |
self.0.as_ref() |
| 125 |
} |
| 126 |
|
| 127 |
/// The accuracy, in meters. |
| 128 |
pub fn accuracy(&self) -> f64 { |
| 129 |
self.1.accuracy |
| 130 |
} |
| 131 |
|
| 132 |
/// The altitude, in meters. |
| 133 |
pub fn altitude(&self) -> Option<f64> { |
| 134 |
if self.1.altitude == -f64::MAX { |
| 135 |
None |
| 136 |
} else { |
| 137 |
Some(self.1.altitude) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/// The speed, in meters per second. |
| 142 |
pub fn speed(&self) -> Option<f64> { |
| 143 |
if self.1.speed == -1f64 { |
| 144 |
None |
| 145 |
} else { |
| 146 |
Some(self.1.speed) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/// The heading, in degrees, going clockwise. North 0, East 90, South 180, |
| 151 |
/// West 270. |
| 152 |
pub fn heading(&self) -> Option<f64> { |
| 153 |
if self.1.heading == -1f64 { |
| 154 |
None |
| 155 |
} else { |
| 156 |
Some(self.1.heading) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/// The location description. |
| 161 |
pub fn description(&self) -> Option<&str> { |
| 162 |
if self.1.description.is_empty() { |
| 163 |
None |
| 164 |
} else { |
| 165 |
Some(&self.1.description) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/// The latitude, in degrees. |
| 170 |
pub fn latitude(&self) -> f64 { |
| 171 |
self.1.latitude |
| 172 |
} |
| 173 |
|
| 174 |
/// The longitude, in degrees. |
| 175 |
pub fn longitude(&self) -> f64 { |
| 176 |
self.1.longitude |
| 177 |
} |
| 178 |
|
| 179 |
/// The timestamp when the location was retrieved. |
| 180 |
pub fn timestamp(&self) -> std::time::Duration { |
| 181 |
std::time::Duration::from_secs(self.1.timestamp.0) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
impl Debug for Location { |
| 186 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 187 |
f.debug_struct("Location") |
| 188 |
.field("accuracy", &self.accuracy()) |
| 189 |
.field("altitude", &self.altitude()) |
| 190 |
.field("speed", &self.speed()) |
| 191 |
.field("heading", &self.heading()) |
| 192 |
.field("description", &self.description()) |
| 193 |
.field("latitude", &self.latitude()) |
| 194 |
.field("longitude", &self.longitude()) |
| 195 |
.field("timestamp", &self.timestamp()) |
| 196 |
.finish() |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
#[derive(Debug, Serialize, Deserialize, Type)] |
| 201 |
#[zvariant(signature = "dict")] |
| 202 |
#[serde(rename_all = "PascalCase")] |
| 203 |
struct LocationInner { |
| 204 |
#[serde(with = "as_value")] |
| 205 |
accuracy: f64, |
| 206 |
#[serde(with = "as_value")] |
| 207 |
altitude: f64, |
| 208 |
#[serde(with = "as_value")] |
| 209 |
speed: f64, |
| 210 |
#[serde(with = "as_value")] |
| 211 |
heading: f64, |
| 212 |
#[serde(with = "as_value")] |
| 213 |
description: String, |
| 214 |
#[serde(with = "as_value")] |
| 215 |
latitude: f64, |
| 216 |
#[serde(with = "as_value")] |
| 217 |
longitude: f64, |
| 218 |
#[serde(with = "as_value")] |
| 219 |
timestamp: (u64, u64), |
| 220 |
} |
| 221 |
|
| 222 |
/// The interface lets sandboxed applications query basic information about the |
| 223 |
/// location. |
| 224 |
/// |
| 225 |
/// Wrapper of the DBus interface: [`org.freedesktop.portal.Location`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Location.html). |
| 226 |
#[derive(Debug, Clone)] |
| 227 |
#[doc(alias = "org.freedesktop.portal.Location")] |
| 228 |
pub struct LocationProxy(Proxy<'static>); |
| 229 |
|
| 230 |
impl LocationProxy { |
| 231 |
/// Create a new instance of [`LocationProxy`]. |
| 232 |
pub async fn new() -> Result<Self, Error> { |
| 233 |
let proxy = Proxy::new_desktop("org.freedesktop.portal.Location").await?; |
| 234 |
Ok(Self(proxy)) |
| 235 |
} |
| 236 |
|
| 237 |
/// Create a new instance of [`LocationProxy`]. |
| 238 |
pub async fn with_connection(connection: zbus::Connection) -> Result<Self, Error> { |
| 239 |
let proxy = |
| 240 |
Proxy::new_desktop_with_connection(connection, "org.freedesktop.portal.Location") |
| 241 |
.await?; |
| 242 |
Ok(Self(proxy)) |
| 243 |
} |
| 244 |
|
| 245 |
/// Returns the version of the portal interface. |
| 246 |
pub fn version(&self) -> u32 { |
| 247 |
self.0.version() |
| 248 |
} |
| 249 |
|
| 250 |
/// Signal emitted when the user location is updated. |
| 251 |
/// |
| 252 |
/// # Specifications |
| 253 |
/// |
| 254 |
/// See also [`LocationUpdated`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Location.html#org-freedesktop-portal-location-locationupdated). |
| 255 |
#[doc(alias = "LocationUpdated")] |
| 256 |
#[doc(alias = "XdpPortal::location-updated")] |
| 257 |
pub async fn receive_location_updated( |
| 258 |
&self, |
| 259 |
) -> Result<impl Stream<Item = Location> + use<>, Error> { |
| 260 |
self.0.signal("LocationUpdated").await |
| 261 |
} |
| 262 |
|
| 263 |
/// Create a location session. |
| 264 |
/// |
| 265 |
/// # Arguments |
| 266 |
/// |
| 267 |
/// * `distance_threshold` - Sets the distance threshold in meters, default |
| 268 |
/// to `0`. |
| 269 |
/// * `time_threshold` - Sets the time threshold in seconds, default to `0`. |
| 270 |
/// * `accuracy` - Sets the location accuracy, default to |
| 271 |
/// [`Accuracy::Exact`]. |
| 272 |
/// |
| 273 |
/// # Specifications |
| 274 |
/// |
| 275 |
/// See also [`CreateSession`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Location.html#org-freedesktop-portal-location-createsession). |
| 276 |
#[doc(alias = "CreateSession")] |
| 277 |
pub async fn create_session( |
| 278 |
&self, |
| 279 |
options: CreateSessionOptions, |
| 280 |
) -> Result<Session<Self>, Error> { |
| 281 |
let (path, proxy) = futures_util::try_join!( |
| 282 |
self.0.call::<OwnedObjectPath>("CreateSession", &(options)), |
| 283 |
Session::from_unique_name(self.0.connection().clone(), &options.session_handle_token), |
| 284 |
)?; |
| 285 |
assert_eq!(proxy.path(), &path.into_inner()); |
| 286 |
Ok(proxy) |
| 287 |
} |
| 288 |
|
| 289 |
/// Start the location session. |
| 290 |
/// An application can only attempt start a session once. |
| 291 |
/// |
| 292 |
/// # Arguments |
| 293 |
/// |
| 294 |
/// * `session` - A [`Session`], created with |
| 295 |
/// [`create_session()`][`LocationProxy::create_session`]. |
| 296 |
/// * `identifier` - Identifier for the application window. |
| 297 |
/// |
| 298 |
/// # Specifications |
| 299 |
/// |
| 300 |
/// See also [`Start`](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Location.html#org-freedesktop-portal-location-start). |
| 301 |
#[doc(alias = "Start")] |
| 302 |
#[doc(alias = "xdp_portal_location_monitor_start")] |
| 303 |
pub async fn start( |
| 304 |
&self, |
| 305 |
session: &Session<Self>, |
| 306 |
identifier: Option<&WindowIdentifier>, |
| 307 |
options: StartOptions, |
| 308 |
) -> Result<Request<()>, Error> { |
| 309 |
let identifier = Optional::from(identifier); |
| 310 |
self.0 |
| 311 |
.empty_request( |
| 312 |
&options.handle_token, |
| 313 |
"Start", |
| 314 |
&(session, identifier, &options), |
| 315 |
) |
| 316 |
.await |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
impl crate::Sealed for LocationProxy {} |
| 321 |
impl SessionPortal for LocationProxy {} |
| 322 |
|
| 323 |
impl std::ops::Deref for LocationProxy { |
| 324 |
type Target = zbus::Proxy<'static>; |
| 325 |
|
| 326 |
fn deref(&self) -> &Self::Target { |
| 327 |
&self.0 |
| 328 |
} |
| 329 |
} |