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 acc5c86
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
13 changes: 11 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,15 @@ 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 set_locked(&self, locked: bool) {
self.locked
.store(locked, std::sync::atomic::Ordering::Relaxed);
}

pub async fn dispatch_items(&self) -> Result<(), Error> {
let keyring_items = self.keyring.items().await;
let mut items = self.items.lock().await;
Expand Down
5 changes: 5 additions & 0 deletions server/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ impl Item {
pub fn path(&self) -> &OwnedObjectPath {
&self.path
}

pub async fn set_locked(&self, locked: bool) {
self.locked
.store(locked, std::sync::atomic::Ordering::Relaxed);
}
}
38 changes: 35 additions & 3 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,41 @@ 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;
let manager = self.manager.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;
let signal_emitter = manager.signal_emitter(item.path().clone())?;
item.locked_changed(&signal_emitter).await?;
}
let signal_emitter = manager.signal_emitter(collection.path().clone())?;
Collection::item_changed(&signal_emitter, item.path()).await?;
}
if !collection.is_locked().await {
collection.set_locked(true).await;
let signal_emitter = manager.signal_emitter(collection.path().clone())?;
collection.locked_changed(&signal_emitter).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 +214,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 acc5c86

Please sign in to comment.