1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use std::ptr;
use glib::Error;
use glib::object::{Object, Upcast, Wrapper, Ref};
use glib::types::{StaticType, Type};
use glib::translate::*;
use glib::glib_container::GlibContainer;
use secret_service::SecretService;
use secret_item::SecretItem;
use SecretResult;
use util::{lock_object, unlock_object};
use Lock;
use ffi;

/// SecretCollection represents a collection of secret items stored in the
/// Secret Service.
/// A collection can be in a locked or unlocked state. Use `Lock::lock()` or 
/// `Lock::unlock()` to lock or unlock the collection.
/// Use `get_items()` to lookup the items in the collection. There may not be 
/// any items exposed when the collection is locked.
pub struct SecretCollection(Ref);

impl SecretCollection {

    /// Lookup which collection is assigned to this alias.
    /// Aliases help determine well known collections, such as 'default'.
    /// Returns the collection, or NULL if none assigned to the alias.
    pub fn for_alias(alias: &str) -> SecretResult<SecretCollection>{
        let mut err = ptr::null_mut();
        let ptr = unsafe {
            ffi::secret_collection_for_alias_sync(
                ptr::null_mut(),
                alias.to_glib_none().0,
                SECRET_COLLECTION_LOAD_ITEMS,
                ptr::null_mut(),
                &mut err
                )
        };
        if err.is_null(){
            Ok(
                unsafe {
                    from_glib_full(ptr)
                }
              )
        } else {
            Err(Error::wrap(err))
        }
    }

    /// Create a new collection in the secret service.
    /// If you specify an alias and a collection with that alias already exists,
    /// then a new collection will not be created. The previous one will be 
    /// returned instead.
    /// Returns the created Collection.
    pub fn create(label: &str, alias: Option<&str>) -> SecretResult<SecretCollection> {
        let mut err = ptr::null_mut();
        let ptr = unsafe {
            ffi::secret_collection_create_sync(
                ptr::null_mut(),
                label.to_glib_none().0,
                alias.to_glib_none().0,
                0,
                ptr::null_mut(),
                &mut err
                )
        };
        if err.is_null(){
            Ok(
                unsafe {
                    from_glib_full(ptr)
                }
              )
        } else {
            Err(Error::wrap(err))
        }
    }

    /// Delete this collection.
    /// The Secret Service may prompt the user.
    pub fn delete(&self) -> SecretResult<()>{
        let mut err = ptr::null_mut();
        unsafe {
            ffi::secret_collection_delete_sync(
                self.to_glib_none().0,
                ptr::null_mut(),
                &mut err
                )
        };
        if err.is_null(){
            Ok(())
        } else {
            Err(Error::wrap(err))
        }
    }

    /// Get the created date and time of the collection.
    /// The return value is the number of seconds since the unix epoch, 
    /// January 1st 1970.
    pub fn get_created(&self) -> u64 {
        unsafe {
            ffi::secret_collection_get_created(
                self.to_glib_none().0
                )
        }
    }

    /// Get the modified date and time of the collection.
    /// The return value is the number of seconds since the unix epoch, 
    /// January 1st 1970.
    pub fn get_modified(&self) -> u64 {
        unsafe {
            ffi::secret_collection_get_modified(
                self.to_glib_none().0
                )
        }
    }

    /// Get the Secret Service object that this collection was created with.
    pub fn get_service(&self) -> SecretService {
        unsafe {
            let ptr = ffi::secret_collection_get_service(
                self.to_glib_none().0
                );
            from_glib_none(ptr)
        }
    }

    /// Get if the items are currently loaded. Use `load_items()` to load them.
    pub fn are_items_loaded(&self) -> bool {
        let flags = unsafe {
            ffi::secret_collection_get_flags(
                self.to_glib_none().0
                )
        };
        flags & SECRET_COLLECTION_LOAD_ITEMS != 0
    }

    /// Get the label of this collection.
    pub fn get_label(&self) -> String {
        unsafe{
            let ptr = ffi::secret_collection_get_label(
                self.to_glib_none().0
                );
            from_glib_full(ptr)
        }
    }

    /// Get the SecretItems of the collection
    pub fn get_items(&self) -> Vec<SecretItem> {
        unsafe {
            let glist = ffi::secret_collection_get_items(
                self.to_glib_none().0
                );
            Vec::from_glib_full(glist)
        }
    }

    /// Ensure that the SecretCollection proxy has loaded all the items present
    /// in the Secret Service.
    pub fn load_items(&self) -> SecretResult<()>{
        unsafe {
            let mut err = ptr::null_mut();
            ffi::secret_collection_load_items_sync(
                self.to_glib_none().0,
                ptr::null_mut(),
                &mut err
                );
            if err.is_null() {
                Ok(())
            } else {
                Err(Error::wrap(err))
            }
        }
    }

    /// Assign the collection to this alias. Aliases help determine well known
    /// collections, such as 'default'.
    pub fn set_alias(&self, alias: &str) -> SecretResult<()>{
        unsafe {
            let mut err = ptr::null_mut();
            ffi::secret_service_set_alias_sync(
                ptr::null_mut(),
                alias.to_glib_none().0,
                self.to_glib_none().0,
                ptr::null_mut(),
                &mut err
                );
            if err.is_null() {
                Ok(())
            } else {
                Err(Error::wrap(err))
            }
        }
    }

    /// Check if the collection is currently locked.
    pub fn is_locked(&self) -> bool {
        let gbool = unsafe {
            ffi::secret_collection_get_locked(
                self.to_glib_none().0
                )
        };
        from_glib(gbool)
    }
}

impl StaticType for SecretCollection {
    fn static_type() -> Type{
        unsafe {
            from_glib(ffi::secret_collection_get_type())
        }
    }
}

unsafe impl Upcast<Object> for SecretCollection { }

impl Wrapper for SecretCollection {
    type GlibType = ffi::SecretCollection;
    unsafe fn wrap(r: Ref) -> Self{
        SecretCollection(r)
    }

    fn as_ref(&self) -> &Ref{
        &self.0
    }

    fn unwrap(self) -> Ref{
        self.0
    }
}

impl Lock<SecretCollection> for SecretCollection {

    fn lock(&self) -> SecretResult<Vec<SecretCollection>>{
        lock_object::<SecretCollection>(self)
    }

    fn unlock(&self) -> SecretResult<Vec<SecretCollection>>{
        unlock_object::<SecretCollection>(self)
    }
}

#[allow(dead_code)]
const SECRET_COLLECTION_NONE: i32        = 0;
const SECRET_COLLECTION_LOAD_ITEMS: i32  = 1 << 1;


#[cfg(test)]
mod test {
    use glib::types::{StaticType, Type};
    use super::SecretCollection;

    /*
    #[test]
    fn test_sc_create_delete() {
        let sc = SecretCollection::create("cool_label", None).unwrap();
        assert!(!sc.is_locked());
        assert!(sc.are_items_loaded());
        assert_eq!(sc.get_label(), "cool_label");
        assert_eq!(sc.get_items().len(), 0);
        sc.delete().ok().unwrap();
    } */

    #[test]
    pub fn test_sc_static_type() {
        match SecretCollection::static_type() {
            Type::Other(_) => {},
            _ => panic!("Expected Type::Other")
        }
    }
}