Skip to content

Commit

Permalink
Add basic progress support
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Nov 10, 2023
1 parent 51302ee commit ee978f1
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
1 change: 1 addition & 0 deletions rust/agama-dbus-server/src/dbus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod interfaces;
1 change: 1 addition & 0 deletions rust/agama-dbus-server/src/dbus/interfaces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod progress;
108 changes: 108 additions & 0 deletions rust/agama-dbus-server/src/dbus/interfaces/progress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use zbus::{dbus_interface, Connection, InterfaceRef, SignalContext};
use crate::error::Error;

pub struct Progress {
iface_ref: Option<InterfaceRef<Progress>>,
total_steps: u8,
current_step: Option<Step>,
}

#[derive(Clone)]
struct Step(u8, String);

impl Default for Step {
fn default() -> Self {
Step(0, String::new())
}
}

type ProgressResult = Result<(), Error>;

impl Progress {
pub fn new() -> Self {
Self {
iface_ref: None,
total_steps: 0,
current_step: None
}
}

pub fn set_iface_ref(&mut self, iface_ref: InterfaceRef<Self>) {
self.iface_ref = Some(iface_ref);
}

pub async fn start(&mut self, total_steps: u8) -> ProgressResult {
self.set_total_steps(total_steps).await?;
self.set_current_step(None).await?;
Ok(())
}

pub async fn step(&mut self, description: String) -> ProgressResult {
let Step(index, _) = self.current_step.clone().unwrap_or_default();
self.set_current_step(Some(Step(index + 1, description))).await
}

pub async fn finish(&mut self) -> ProgressResult {
self.set_total_steps(0).await?;
self.set_current_step(None).await?;
Ok(())
}

async fn set_total_steps(&mut self, value: u8) -> ProgressResult {
self.total_steps = value;
self.total_steps_signal().await
}

async fn set_current_step(&mut self, step: Option<Step>) -> ProgressResult {
self.current_step = step;
self.current_step_signal().await
}

async fn total_steps_signal(&self) -> ProgressResult {
if let Some(signal_context) = self.signal_context() {
self.total_steps_changed(signal_context).await?;
}
Ok(())
}

async fn current_step_signal(&self) -> ProgressResult {
if let Some(signal_context) = self.signal_context() {
self.current_step_changed(signal_context).await?;
}
Ok(())
}

fn signal_context(&self) -> Option<&SignalContext> {
self.iface_ref
.as_ref()
.and_then(|iref| Some(iref.signal_context()))
}
}

#[dbus_interface(name = "org.opensuse.Agama1.Progress")]
impl Progress {
#[dbus_interface(property)]
fn total_steps(&self) -> u8 {
self.total_steps
}

#[dbus_interface(property)]
fn current_step(&self) -> (u8, String) {
let Step(index, description) = self.current_step.clone().unwrap_or_default();
(index, description)
}
}

pub async fn export_interface(
connection: &Connection,
path: &str,
) -> Result<InterfaceRef<Progress>, Box<dyn std::error::Error>> {
let progress = Progress::new();
connection.object_server().at(path, progress).await?;

let iface_ref = connection.object_server().interface::<_, Progress>(path).await?;
let mut iface = iface_ref.get_mut().await;
iface.set_iface_ref(iface_ref.clone());

Ok(iface_ref.clone())
}
2 changes: 2 additions & 0 deletions rust/agama-dbus-server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod dbus;
pub mod error;
pub mod locale;
pub mod network;
pub mod manager;
pub mod questions;
4 changes: 3 additions & 1 deletion rust/agama-dbus-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use agama_dbus_server::{locale, network, questions};
use agama_dbus_server::{manager, locale, network, questions};

use agama_lib::connection_to;
use anyhow::Context;
Expand Down Expand Up @@ -38,6 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
log::info!("Started locale interface");
network::export_dbus_objects(&connection).await?;
log::info!("Started network interface");
manager::export_dbus_objects(&connection).await?;
log::info!("Started manager interface");

connection
.request_name(SERVICE_NAME)
Expand Down
42 changes: 42 additions & 0 deletions rust/agama-dbus-server/src/manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::dbus::interfaces::progress::{export_interface, Progress};
use zbus::{dbus_interface, Connection, InterfaceRef};
use crate::error::Error;

pub struct Manager {
progress_ref: InterfaceRef<Progress>,
}

impl Manager {
pub fn new(progress_ref: InterfaceRef<Progress>) -> Self {
Self {
progress_ref,
}
}
}

#[dbus_interface(name = "org.opensuse.Agama1.Manager")]
impl Manager {
pub async fn probe(&mut self) -> Result<(), Error> {
let mut progress = self.progress_ref.get_mut().await;

// TODO
progress.start(2).await?;
progress.step("step 1".to_string()).await?;
progress.step("step 2".to_string()).await?;
progress.finish().await?;

Ok(())
}
}

pub async fn export_dbus_objects(
connection: &Connection,
) -> Result<(), Box<dyn std::error::Error>> {
const PATH: &str = "/org/opensuse/Agama1";

let progress_ref = export_interface(connection, PATH).await?;
let manager = Manager::new(progress_ref);
connection.object_server().at(PATH, manager).await?;

Ok(())
}

0 comments on commit ee978f1

Please sign in to comment.