[niepce] LibFolder: handle parenting
- From: Hubert Figuière <hub src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [niepce] LibFolder: handle parenting
- Date: Fri, 27 Oct 2017 02:03:43 +0000 (UTC)
commit 7e51edff1ef1d15fc2d3c47b5af1558cc71ef339
Author: Hubert Figuière <hub figuiere net>
Date: Thu Oct 26 20:22:51 2017 -0400
LibFolder: handle parenting
src/engine/db/libfolder.rs | 13 ++++++++++++-
src/engine/db/library.rs | 22 +++++++++++++++++-----
2 files changed, 29 insertions(+), 6 deletions(-)
---
diff --git a/src/engine/db/libfolder.rs b/src/engine/db/libfolder.rs
index 9c196a7..5ad99fb 100644
--- a/src/engine/db/libfolder.rs
+++ b/src/engine/db/libfolder.rs
@@ -49,6 +49,7 @@ pub struct LibFolder {
locked: bool,
expanded: bool,
virt: FolderVirtualType,
+ parent: LibraryId,
cstr: CString,
}
@@ -59,6 +60,7 @@ impl LibFolder {
path: String::from(path),
locked: false,
expanded: false, virt: FolderVirtualType::NONE,
+ parent: 0,
cstr: CString::new("").unwrap(),
}
}
@@ -95,12 +97,19 @@ impl LibFolder {
self.virt = virt;
}
+ pub fn parent(&self) -> LibraryId {
+ self.parent
+ }
+
+ pub fn set_parent(&mut self, parent: LibraryId) {
+ self.parent = parent;
+ }
}
impl FromDb for LibFolder {
fn read_db_columns() -> &'static str {
- "id,name,virtual,locked,expanded,path"
+ "id,name,virtual,locked,expanded,path,parent_id"
}
fn read_db_tables() -> &'static str {
@@ -114,8 +123,10 @@ impl FromDb for LibFolder {
let locked = row.get(3);
let expanded = row.get(4);
let path: String = row.get_checked(5).unwrap_or(String::from(""));
+ let parent = row.get(6);
let mut libfolder = LibFolder::new(id, &name, &path);
+ libfolder.set_parent(parent);
libfolder.set_virtual_type(FolderVirtualType::from(virt_type));
libfolder.set_locked(locked);
libfolder.set_expanded(expanded);
diff --git a/src/engine/db/library.rs b/src/engine/db/library.rs
index cbff68a..06944ee 100644
--- a/src/engine/db/library.rs
+++ b/src/engine/db/library.rs
@@ -263,17 +263,25 @@ impl Library {
}
pub fn add_folder(&self, folder: &str) -> Option<LibFolder> {
+ self.add_folder_into(folder, 0)
+ }
+
+ /// Add folder into parent whose id is `into`.
+ /// A value of 0 means root.
+ pub fn add_folder_into(&self, folder: &str, into: LibraryId) -> Option<LibFolder> {
if let Some(foldername) = Self::leaf_name_for_pathname(folder) {
if let Some(ref conn) = self.dbconn {
if let Ok(c) = conn.execute(
- "INSERT INTO folders (path,name,vault_id,parent_id) VALUES(:1, :2, '0', '0')",
- &[&folder, &foldername]) {
+ "INSERT INTO folders (path,name,vault_id,parent_id) VALUES(:1, :2, '0', :3)",
+ &[&folder, &foldername, &into]) {
if c != 1 {
return None;
}
let id = conn.last_insert_rowid();
dbg_out!("last row inserted {}", id);
- return Some(LibFolder::new(id, &foldername, &folder));
+ let mut lf = LibFolder::new(id, &foldername, &folder);
+ lf.set_parent(into);
+ return Some(lf);
}
}
}
@@ -865,8 +873,12 @@ mod test {
let f = f.unwrap();
assert_eq!(folder_added.id(), f.id());
- lib.add_folder("bar");
- assert!(lib.get_folder("bar").is_some());
+ let id = f.id();
+ lib.add_folder_into("bar", id);
+ let f = lib.get_folder("bar");
+ assert!(f.is_some());
+ let f = f.unwrap();
+ assert_eq!(f.parent(), id);
let folders = lib.get_all_folders();
assert_eq!(folders.len(), 3);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]