use futures_util::Stream;
use serde_repr::{Deserialize_repr, Serialize_repr};
use zbus::zvariant::{DeserializeDict, ObjectPath, SerializeDict, Type};
use crate::{Error, WindowIdentifier, proxy::Proxy, window_identifier::MaybeWindowIdentifierExt};
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
struct UpdateOptions {}
#[derive(DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct UpdateInfo {
#[zvariant(rename = "running-commit")]
running_commit: String,
#[zvariant(rename = "local-commit")]
local_commit: String,
#[zvariant(rename = "remote-commit")]
remote_commit: String,
}
impl UpdateInfo {
pub fn running_commit(&self) -> &str {
&self.running_commit
}
pub fn local_commit(&self) -> &str {
&self.local_commit
}
pub fn remote_commit(&self) -> &str {
&self.remote_commit
}
}
#[cfg_attr(feature = "glib", derive(glib::Enum))]
#[cfg_attr(feature = "glib", enum_type(name = "AshpdUpdateStatus"))]
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Eq, Copy, Clone, Debug, Type)]
#[repr(u32)]
pub enum UpdateStatus {
#[doc(alias = "XDP_UPDATE_STATUS_RUNNING")]
Running = 0,
#[doc(alias = "XDP_UPDATE_STATUS_EMPTY")]
Empty = 1,
#[doc(alias = "XDP_UPDATE_STATUS_DONE")]
Done = 2,
#[doc(alias = "XDP_UPDATE_STATUS_FAILED")]
Failed = 3,
}
#[derive(DeserializeDict, Type, Debug)]
#[zvariant(signature = "dict")]
pub struct UpdateProgress {
pub n_ops: Option<u32>,
pub op: Option<u32>,
pub progress: Option<u32>,
pub status: Option<UpdateStatus>,
pub error: Option<String>,
pub error_message: Option<String>,
}
#[derive(Debug)]
#[doc(alias = "org.freedesktop.portal.Flatpak.UpdateMonitor")]
pub struct UpdateMonitor(Proxy<'static>);
impl UpdateMonitor {
pub(crate) async fn with_connection(
connection: zbus::Connection,
path: ObjectPath<'static>,
) -> Result<Self, Error> {
let proxy = Proxy::new_flatpak_with_path(
connection,
"org.freedesktop.portal.Flatpak.UpdateMonitor",
path,
)
.await?;
Ok(Self(proxy))
}
#[doc(alias = "Progress")]
#[doc(alias = "XdpPortal::update-progress")]
pub async fn receive_progress(&self) -> Result<impl Stream<Item = UpdateProgress>, Error> {
self.0.signal("Progress").await
}
#[doc(alias = "UpdateAvailable")]
#[doc(alias = "XdpPortal::update-available")]
pub async fn receive_update_available(&self) -> Result<impl Stream<Item = UpdateInfo>, Error> {
self.0.signal("UpdateAvailable").await
}
#[doc(alias = "Update")]
#[doc(alias = "xdp_portal_update_install")]
pub async fn update(&self, identifier: Option<&WindowIdentifier>) -> Result<(), Error> {
let options = UpdateOptions::default();
let identifier = identifier.to_string_or_empty();
self.0.call("Update", &(&identifier, options)).await
}
#[doc(alias = "Close")]
pub async fn close(&self) -> Result<(), Error> {
self.0.call("Close", &()).await
}
}
impl std::ops::Deref for UpdateMonitor {
type Target = zbus::Proxy<'static>;
fn deref(&self) -> &Self::Target {
&self.0
}
}