Skip to content

Commit 4190908

Browse files
committed
Add set_collections() setter and update create_collection()
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: #73 (comment) Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
1 parent e0057d4 commit 4190908

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

server/src/daemon/collection.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ impl Collection {
105105
}
106106

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

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

@@ -153,4 +153,12 @@ impl Collection {
153153
self.locked
154154
.store(locked, std::sync::atomic::Ordering::Relaxed)
155155
}
156+
157+
pub fn created(&self) -> &Duration {
158+
&self.created
159+
}
160+
161+
pub fn modified(&self) -> &Duration {
162+
&self.modified
163+
}
156164
}

server/src/daemon/service.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use oo7::{
1414
portal::{Item, Keyring},
1515
Key,
1616
};
17+
use tokio::sync::RwLock;
1718
use zbus::{
1819
proxy::ProxyDefault,
1920
zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value},
@@ -26,7 +27,7 @@ use super::{
2627

2728
#[derive(Debug)]
2829
pub struct Service {
29-
collections: Vec<Collection>,
30+
collections: RwLock<Vec<Collection>>,
3031
keyring: Arc<Keyring>,
3132
cnx: Mutex<Option<zbus::Connection>>,
3233
}
@@ -66,6 +67,13 @@ impl Service {
6667
.unwrap(),
6768
Arc::clone(&self.keyring),
6869
);
70+
self.collections.write().await.push(Collection::new(
71+
collection.label(),
72+
alias,
73+
*collection.created(),
74+
Arc::clone(&self.keyring),
75+
));
76+
6977
let path = OwnedObjectPath::from(collection.path());
7078
object_server.at(&path, collection).await?;
7179
let prompt = Prompt::default(); // temp Prompt
@@ -113,7 +121,7 @@ impl Service {
113121
let mut unlocked: Vec<OwnedObjectPath> = Vec::new();
114122

115123
'main: for object in objects {
116-
for collection in self.collections.iter() {
124+
for collection in self.collections.read().await.iter() {
117125
if collection.path() == *object {
118126
if collection.locked() {
119127
collection.set_locked(false).await;
@@ -144,7 +152,7 @@ impl Service {
144152
let mut locked: Vec<OwnedObjectPath> = Vec::new();
145153

146154
for object in objects {
147-
for collection in self.collections.iter() {
155+
for collection in self.collections.read().await.iter() {
148156
if collection.path() == *object && !collection.locked() {
149157
collection.set_locked(true).await;
150158
locked.push(object.clone());
@@ -167,7 +175,7 @@ impl Service {
167175
session: ObjectPath<'_>,
168176
) -> Result<HashMap<OwnedObjectPath, SecretInner>> {
169177
let mut secrets = HashMap::with_capacity(paths.len());
170-
for collection in &self.collections {
178+
for collection in self.collections.read().await.iter() {
171179
let items = collection.items.read().await;
172180
for item in items.iter() {
173181
for path in paths.iter() {
@@ -194,13 +202,12 @@ impl Service {
194202
.unwrap_or_default()
195203
}
196204

197-
pub async fn set_alias(
205+
pub fn set_alias(
198206
&self,
199207
#[zbus(signal_context)] ctxt: SignalContext<'_>,
200208
alias: &str,
201209
path: ObjectPath<'_>,
202210
) -> Result<()> {
203-
// WIP: not complete:: handle alias & default path '/' to remove the alias
204211
match self.collections.iter().find(|c| c.path() == path) {
205212
Some(collection) => {
206213
collection.set_alias(&ctxt, alias).await?;
@@ -214,8 +221,10 @@ impl Service {
214221
}
215222

216223
#[zbus(property, name = "Collections")]
217-
pub fn collections(&self) -> Vec<ObjectPath> {
224+
pub async fn collections(&self) -> Vec<ObjectPath> {
218225
self.collections
226+
.read()
227+
.await
219228
.iter()
220229
.map(|collection| collection.path())
221230
.collect()
@@ -243,7 +252,7 @@ impl Service {
243252
impl Service {
244253
pub async fn new() -> Self {
245254
Self {
246-
collections: Vec::new(),
255+
collections: RwLock::new(Vec::new()),
247256
keyring: Arc::new(Keyring::load_default().await.unwrap()),
248257
cnx: Default::default(),
249258
}

0 commit comments

Comments
 (0)