Skip to content

Commit

Permalink
Add set_collections() setter and update create_collection()
Browse files Browse the repository at this point in the history
Since Collection is not using and cannot be implemented (easily)
Copy or Clone traits, the only way to clone a struct Collection
is call Collection::new() passing getter values.

Removed set_collections() setter.
See: bilelmoussaoui#73 (comment)

Signed-off-by: Dhanuka Warusadura <[email protected]>
  • Loading branch information
warusadura committed Oct 15, 2024
1 parent 79639af commit 05e3953
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
12 changes: 10 additions & 2 deletions server/src/daemon/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ impl Collection {
}

#[zbus(property, name = "Created")]
pub fn created(&self) -> u64 {
pub fn created_as_secs(&self) -> u64 {
self.created.as_secs()
}

#[zbus(property, name = "Modified")]
pub fn modified(&self) -> u64 {
pub fn modified_as_secs(&self) -> u64 {
self.modified.as_secs()
}

Expand Down Expand Up @@ -153,4 +153,12 @@ impl Collection {
self.locked
.store(locked, std::sync::atomic::Ordering::Relaxed)
}

pub fn created(&self) -> &Duration {
&self.created
}

pub fn modified(&self) -> &Duration {
&self.modified
}
}
25 changes: 17 additions & 8 deletions server/src/daemon/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use oo7::{
portal::{Item, Keyring},
Key,
};
use tokio::sync::RwLock;
use zbus::{
proxy::ProxyDefault,
zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value},
Expand All @@ -26,7 +27,7 @@ use super::{

#[derive(Debug)]
pub struct Service {
collections: Vec<Collection>,
collections: RwLock<Vec<Collection>>,
keyring: Arc<Keyring>,
cnx: Mutex<Option<zbus::Connection>>,
}
Expand Down Expand Up @@ -66,6 +67,13 @@ impl Service {
.unwrap(),
Arc::clone(&self.keyring),
);
self.collections.write().await.push(Collection::new(
collection.label(),
alias,
*collection.created(),
Arc::clone(&self.keyring),
));

let path = OwnedObjectPath::from(collection.path());
object_server.at(&path, collection).await?;
let prompt = Prompt::default(); // temp Prompt
Expand Down Expand Up @@ -113,7 +121,7 @@ impl Service {
let mut unlocked: Vec<OwnedObjectPath> = Vec::new();

'main: for object in objects {
for collection in self.collections.iter() {
for collection in self.collections.read().await.iter() {
if collection.path() == *object {
if collection.locked() {
collection.set_locked(false).await;
Expand Down Expand Up @@ -144,7 +152,7 @@ impl Service {
let mut locked: Vec<OwnedObjectPath> = Vec::new();

for object in objects {
for collection in self.collections.iter() {
for collection in self.collections.read().await.iter() {
if collection.path() == *object && !collection.locked() {
collection.set_locked(true).await;
locked.push(object.clone());
Expand All @@ -167,7 +175,7 @@ impl Service {
session: ObjectPath<'_>,
) -> Result<HashMap<OwnedObjectPath, SecretInner>> {
let mut secrets = HashMap::with_capacity(paths.len());
for collection in &self.collections {
for collection in self.collections.read().await.iter() {
let items = collection.items.read().await;
for item in items.iter() {
for path in paths.iter() {
Expand All @@ -194,13 +202,12 @@ impl Service {
.unwrap_or_default()
}

pub async fn set_alias(
pub fn set_alias(
&self,
#[zbus(signal_context)] ctxt: SignalContext<'_>,
alias: &str,
path: ObjectPath<'_>,
) -> Result<()> {
// WIP: not complete:: handle alias & default path '/' to remove the alias
match self.collections.iter().find(|c| c.path() == path) {
Some(collection) => {
collection.set_alias(&ctxt, alias).await?;
Expand All @@ -214,8 +221,10 @@ impl Service {
}

#[zbus(property, name = "Collections")]
pub fn collections(&self) -> Vec<ObjectPath> {
pub async fn collections(&self) -> Vec<ObjectPath> {
self.collections
.read()
.await
.iter()
.map(|collection| collection.path())
.collect()
Expand Down Expand Up @@ -243,7 +252,7 @@ impl Service {
impl Service {
pub async fn new() -> Self {
Self {
collections: Vec::new(),
collections: RwLock::new(Vec::new()),
keyring: Arc::new(Keyring::load_default().await.unwrap()),
cnx: Default::default(),
}
Expand Down

0 comments on commit 05e3953

Please sign in to comment.