[niepce] library: folder now have optional path



commit 72a582b97389a58ef408025ed012787c93cf2dc8
Author: Hubert Figuière <hub figuiere net>
Date:   Wed Nov 15 23:26:49 2017 -0500

    library: folder now have optional path

 src/engine/db/libfolder.rs            |   12 +++++++-----
 src/engine/db/library.rs              |   21 +++++++++++----------
 src/engine/library/commands.rs        |   25 +++++++++++++++++--------
 src/libraryclient/clientimpl.rs       |    4 ++--
 src/libraryclient/clientinterface.rs  |    2 +-
 src/libraryclient/libraryclient.rs    |   14 ++++++++++----
 src/niepce/ui/workspacecontroller.cpp |    2 +-
 7 files changed, 49 insertions(+), 31 deletions(-)
---
diff --git a/src/engine/db/libfolder.rs b/src/engine/db/libfolder.rs
index 5ad99fb..7c58a6b 100644
--- a/src/engine/db/libfolder.rs
+++ b/src/engine/db/libfolder.rs
@@ -44,8 +44,10 @@ impl From<i32> for FolderVirtualType {
 #[derive(Clone)]
 pub struct LibFolder {
     id: LibraryId,
+    /// Name of the folder
     name: String,
-    path: String,
+    /// Path of the folder.
+    path: Option<String>,
     locked: bool,
     expanded: bool,
     virt: FolderVirtualType,
@@ -54,10 +56,10 @@ pub struct LibFolder {
 }
 
 impl LibFolder {
-    pub fn new(id: LibraryId, name: &str, path: &str) -> LibFolder {
+    pub fn new(id: LibraryId, name: &str, path: Option<String>) -> LibFolder {
         LibFolder {
             id: id, name: String::from(name),
-            path: String::from(path),
+            path: path,
             locked: false,
             expanded: false, virt: FolderVirtualType::NONE,
             parent: 0,
@@ -122,10 +124,10 @@ impl FromDb for LibFolder {
         let virt_type: i32 = row.get(2);
         let locked = row.get(3);
         let expanded = row.get(4);
-        let path: String = row.get_checked(5).unwrap_or(String::from(""));
+        let path: Option<String> = row.get_checked(5).ok();
         let parent = row.get(6);
 
-        let mut libfolder = LibFolder::new(id, &name, &path);
+        let mut libfolder = LibFolder::new(id, &name, path);
         libfolder.set_parent(parent);
         libfolder.set_virtual_type(FolderVirtualType::from(virt_type));
         libfolder.set_locked(locked);
diff --git a/src/engine/db/library.rs b/src/engine/db/library.rs
index 2dd645e..5a6e768 100644
--- a/src/engine/db/library.rs
+++ b/src/engine/db/library.rs
@@ -229,7 +229,7 @@ impl Library {
         false
     }
 
-    fn leaf_name_for_pathname(pathname: &str) -> Option<String> {
+    pub fn leaf_name_for_pathname(pathname: &str) -> Option<String> {
         let name = try_opt!(Path::new(pathname).file_name());
         Some(String::from(try_opt!(name.to_str())))
     }
@@ -251,24 +251,25 @@ impl Library {
         Some(files)
     }
 
-    pub fn add_folder(&self, folder: &str) -> Option<LibFolder> {
-        self.add_folder_into(folder, 0)
+    /// Add a folder at the root.
+    pub fn add_folder(&self, name: &str, path: Option<String>) -> Option<LibFolder> {
+        self.add_folder_into(name, path, 0)
     }
 
-    /// Add folder into parent whose id is `into`.
+    /// Add folder with name into parent whose id is `into`.
     /// A value of 0 means root.
-    pub fn add_folder_into(&self, folder: &str, into: LibraryId) -> Option<LibFolder> {
-        let foldername = try_opt!(Self::leaf_name_for_pathname(folder));
+    pub fn add_folder_into(&self, name: &str, path: Option<String>,
+                           into: LibraryId) -> Option<LibFolder> {
         let conn = try_opt!(self.dbconn.as_ref());
         let c = try_opt!(conn.execute(
             "INSERT INTO folders (path,name,vault_id,parent_id) VALUES(:1, :2, '0', :3)",
-            &[&folder, &foldername, &into]).ok());
+            &[&path, &name, &into]).ok());
         if c != 1 {
             return None;
         }
         let id = conn.last_insert_rowid();
         dbg_out!("last row inserted {}", id);
-        let mut lf = LibFolder::new(id, &foldername, &folder);
+        let mut lf = LibFolder::new(id, &name, path);
         lf.set_parent(into);
         return Some(lf);
     }
@@ -819,7 +820,7 @@ mod test {
         assert!(lib.is_ok());
         assert!(lib.check_database_version() == super::DB_SCHEMA_VERSION);
 
-        let folder_added = lib.add_folder("foo");
+        let folder_added = lib.add_folder("foo", Some(String::from("foo")));
         assert!(folder_added.is_some());
         let folder_added = folder_added.unwrap();
         assert!(folder_added.id() > 0);
@@ -830,7 +831,7 @@ mod test {
         assert_eq!(folder_added.id(), f.id());
 
         let id = f.id();
-        lib.add_folder_into("bar", id);
+        lib.add_folder_into("bar", Some(String::from("bar")), id);
         let f = lib.get_folder("bar");
         assert!(f.is_some());
         let f = f.unwrap();
diff --git a/src/engine/library/commands.rs b/src/engine/library/commands.rs
index d349aa4..972febd 100644
--- a/src/engine/library/commands.rs
+++ b/src/engine/library/commands.rs
@@ -74,9 +74,14 @@ pub fn cmd_import_file(lib: &Library, path: &str, manage: Managed) -> bool {
         Some(lf) =>
             libfolder = lf,
         _ => {
-            if let Some(lf) = lib.add_folder(&*folder.to_string_lossy()) {
-                libfolder = lf.clone();
-                lib.notify(Box::new(LibNotification::AddedFolder(lf)));
+            let folder = &*folder.to_string_lossy();
+            if let Some(name) = Library::leaf_name_for_pathname(folder) {
+                if let Some(lf) = lib.add_folder(&name, Some(String::from(folder))) {
+                    libfolder = lf.clone();
+                    lib.notify(Box::new(LibNotification::AddedFolder(lf)));
+                } else {
+                    return false;
+                }
             } else {
                 return false;
             }
@@ -98,9 +103,13 @@ pub fn cmd_import_files(lib: &Library, folder: &str, files: &Vec<String>,
         Some(lf) =>
             libfolder = lf,
         _ => {
-            if let Some(lf) = lib.add_folder(folder) {
-                libfolder = lf.clone();
-                lib.notify(Box::new(LibNotification::AddedFolder(lf)));
+            if let Some(name) = Library::leaf_name_for_pathname(folder) {
+                if let Some(lf) = lib.add_folder(&name, Some(String::from(folder))) {
+                    libfolder = lf.clone();
+                    lib.notify(Box::new(LibNotification::AddedFolder(lf)));
+                } else {
+                    return false;
+                }
             } else {
                 return false;
             }
@@ -114,8 +123,8 @@ pub fn cmd_import_files(lib: &Library, folder: &str, files: &Vec<String>,
     true
 }
 
-pub fn cmd_create_folder(lib: &Library, path: &String) -> LibraryId {
-    if let Some(lf) = lib.add_folder(path) {
+pub fn cmd_create_folder(lib: &Library, name: &String, path: Option<String>) -> LibraryId {
+    if let Some(lf) = lib.add_folder(name, path) {
         let id = lf.id();
         lib.notify(Box::new(LibNotification::AddedFolder(lf)));
         return id;
diff --git a/src/libraryclient/clientimpl.rs b/src/libraryclient/clientimpl.rs
index f48a763..65d1ec4 100644
--- a/src/libraryclient/clientimpl.rs
+++ b/src/libraryclient/clientimpl.rs
@@ -241,12 +241,12 @@ impl ClientInterfaceSync for ClientImpl {
         rx.recv().unwrap()
     }
 
-    fn create_folder_sync(&mut self, path: String) -> LibraryId {
+    fn create_folder_sync(&mut self, name: String, path: Option<String>) -> LibraryId {
         // can't use futures::sync::oneshot
         let (tx, rx) = mpsc::sync_channel::<LibraryId>(1);
 
         self.schedule_op(move |lib| {
-            tx.send(commands::cmd_create_folder(&lib, &path)).unwrap();
+            tx.send(commands::cmd_create_folder(&lib, &name, path.clone())).unwrap();
             true
         });
 
diff --git a/src/libraryclient/clientinterface.rs b/src/libraryclient/clientinterface.rs
index 107a68e..e58e8c1 100644
--- a/src/libraryclient/clientinterface.rs
+++ b/src/libraryclient/clientinterface.rs
@@ -72,5 +72,5 @@ pub trait ClientInterfaceSync {
     fn create_label_sync(&mut self, name: String, colour: String) -> LibraryId;
 
     /// Create a folder. Return the id of the newly created folder.
-    fn create_folder_sync(&mut self, path: String) -> LibraryId;
+    fn create_folder_sync(&mut self, name: String, path: Option<String>) -> LibraryId;
 }
diff --git a/src/libraryclient/libraryclient.rs b/src/libraryclient/libraryclient.rs
index abd5bfa..5de76e4 100644
--- a/src/libraryclient/libraryclient.rs
+++ b/src/libraryclient/libraryclient.rs
@@ -153,8 +153,8 @@ impl ClientInterfaceSync for LibraryClient {
         self.pimpl.create_label_sync(name, colour)
     }
 
-    fn create_folder_sync(&mut self, path: String) -> LibraryId {
-        self.pimpl.create_folder_sync(path)
+    fn create_folder_sync(&mut self, name: String, path: Option<String>) -> LibraryId {
+        self.pimpl.create_folder_sync(name, path)
     }
 }
 
@@ -197,9 +197,15 @@ pub extern "C" fn libraryclient_query_folder_content(client: &mut LibraryClientW
 
 #[no_mangle]
 pub extern "C" fn libraryclient_create_folder_sync(client: &mut LibraryClientWrapper,
+                                                   n: *const c_char,
                                                    p: *const c_char) -> LibraryId {
-    let path = unsafe { CStr::from_ptr(p) }.to_string_lossy();
-    client.unwrap_mut().create_folder_sync(String::from(path))
+    let name = String::from(unsafe { CStr::from_ptr(n) }.to_string_lossy());
+    let path = if p.is_null() {
+        None
+    } else {
+        Some(String::from(unsafe { CStr::from_ptr(p) }.to_string_lossy()))
+    };
+    client.unwrap_mut().create_folder_sync(name, path)
 }
 
 #[no_mangle]
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index 02f0bd9..15b2a36 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -85,7 +85,7 @@ fwk::Configuration::Ptr WorkspaceController::getLibraryConfig() const
 void WorkspaceController::action_new_folder()
 {
     // XXX get a unique name
-    auto id = ffi::libraryclient_create_folder_sync(getLibraryClient()->client(), "foobar");
+    auto id = ffi::libraryclient_create_folder_sync(getLibraryClient()->client(), "foobar", nullptr);
     // select folder in tree
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]