Skip to content

Commit

Permalink
server: Add service method Lock implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
  • Loading branch information
warusadura committed Nov 2, 2024
1 parent 37198e7 commit b414c91
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
24 changes: 22 additions & 2 deletions server/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl Collection {
) -> zbus::Result<()>;

#[zbus(signal, name = "ItemChanged")]
async fn item_changed(
pub async fn item_changed(
signal_emitter: &SignalEmitter<'_>,
item: OwnedObjectPath,
item: &OwnedObjectPath,
) -> zbus::Result<()>;
}

Expand Down Expand Up @@ -157,6 +157,26 @@ impl Collection {
self.alias.lock().await.clone()
}

pub async fn inner_items(&self) -> Vec<item::Item> {
self.items.lock().await.to_vec()
}

pub async fn locked_updated(&self) -> Result<(), ServiceError> {
let manager = self.manager.lock().await;
let signal_emitter = manager.signal_emitter(self.path.clone())?;
self.locked_changed(&signal_emitter).await?;

Ok(())
}

pub async fn set_locked(&self, locked: bool) -> Result<(), ServiceError> {
self.locked
.store(locked, std::sync::atomic::Ordering::Relaxed);
self.locked_updated().await?;

Ok(())
}

pub async fn dispatch_items(&self) -> Result<(), Error> {
let keyring_items = self.keyring.items().await;
let mut items = self.items.lock().await;
Expand Down
26 changes: 23 additions & 3 deletions server/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ use oo7::{
use tokio::sync::Mutex;
use zbus::zvariant::{ObjectPath, OwnedObjectPath};

use crate::service_manager::ServiceManager;
use crate::{collection::Collection, service_manager::ServiceManager};

#[derive(Debug, Clone)]
pub struct Item {
// Properties
locked: Arc<AtomicBool>,
inner: Arc<Mutex<oo7::portal::Item>>,
// Other attributes
_manager: Arc<Mutex<ServiceManager>>,
manager: Arc<Mutex<ServiceManager>>,
collection_path: OwnedObjectPath,
path: OwnedObjectPath,
}

Expand Down Expand Up @@ -93,12 +94,31 @@ impl Item {
Self {
locked: Arc::new(AtomicBool::new(locked)),
inner: Arc::new(Mutex::new(item)),
collection_path: collection_path.clone(),
path: OwnedObjectPath::try_from(format!("{}/{}", collection_path, item_index)).unwrap(),
_manager: manager,
manager,
}
}

pub fn path(&self) -> &OwnedObjectPath {
&self.path
}

pub async fn locked_updated(&self) -> Result<(), ServiceError> {
let manager = self.manager.lock().await;
let signal_emitter = manager.signal_emitter(self.path.clone())?;
self.locked_changed(&signal_emitter).await?;
let signal_emitter = manager.signal_emitter(self.collection_path.clone())?;
Collection::item_changed(&signal_emitter, &self.path).await?;

Ok(())
}

pub async fn set_locked(&self, locked: bool) -> Result<(), ServiceError> {
self.locked
.store(locked, std::sync::atomic::Ordering::Relaxed);
self.locked_updated().await?;

Ok(())
}
}
31 changes: 28 additions & 3 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,34 @@ impl Service {
#[zbus(out_args("locked", "prompt"))]
pub async fn lock(
&mut self,
_objects: Vec<OwnedObjectPath>,
objects: Vec<OwnedObjectPath>,
#[zbus(signal_emitter)] signal_emitter: SignalEmitter<'_>,
) -> Result<(Vec<OwnedObjectPath>, ObjectPath), ServiceError> {
todo!()
let mut locked: Vec<OwnedObjectPath> = Vec::new();
let prompt = ObjectPath::default();
let collections = self.collections.lock().await;

for object in &objects {
for collection in collections.iter() {
if object == collection.path() || collection.items().await.contains(object) {
for item in collection.inner_items().await {
if !item.is_locked().await {
item.set_locked(true).await?;
}
}
if !collection.is_locked().await {
collection.set_locked(true).await?;
}
locked.push(object.clone());
Self::collection_changed(&signal_emitter, collection.path()).await?;
tracing::info!("Object: {} is locked.", object);
break;
}
tracing::info!("Object: {} does not exist.", object);
}
}

Ok((locked, prompt))
}

#[zbus(out_args("secrets"))]
Expand Down Expand Up @@ -182,7 +207,7 @@ impl Service {
#[zbus(signal, name = "CollectionChanged")]
async fn collection_changed(
signal_emitter: &SignalEmitter<'_>,
collection: OwnedObjectPath,
collection: &OwnedObjectPath,
) -> zbus::Result<()>;
}

Expand Down
9 changes: 9 additions & 0 deletions server/src/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ impl ServiceManager {
self.connection.object_server()
}

pub fn signal_emitter(
&self,
path: OwnedObjectPath,
) -> Result<zbus::object_server::SignalEmitter, oo7::dbus::ServiceError> {
let signal_emitter = zbus::object_server::SignalEmitter::new(&self.connection, path)?;

Ok(signal_emitter)
}

pub fn n_sessions(&self) -> usize {
self.sessions.len()
}
Expand Down

0 comments on commit b414c91

Please sign in to comment.