[niepce] npc-engine: QueriedContent is now in Rust



commit 89484b7115aa8eaa292b0d5e382b52adf63971a3
Author: Hubert Figuière <hub figuiere net>
Date:   Wed Jan 1 12:17:07 2020 -0500

    npc-engine: QueriedContent is now in Rust
    
    - engine/notification.hpp is no more

 crates/npc-engine/build.rs                      |  2 -
 crates/npc-engine/src/db/fsfile.rs              |  3 +-
 crates/npc-engine/src/db/libfile.rs             |  3 +-
 crates/npc-engine/src/library/commands.rs       | 24 +++++------
 crates/npc-engine/src/library/mod.rs            |  1 +
 crates/npc-engine/src/library/notification.rs   | 46 +++-----------------
 crates/npc-engine/src/library/queriedcontent.rs | 56 +++++++++++++++++++++++++
 src/Makefile.am                                 |  1 +
 src/engine/Makefile.am                          |  1 -
 src/engine/db/bindings.hpp                      |  1 -
 src/engine/db/libfile.cpp                       | 28 +------------
 src/engine/db/libfile.hpp                       |  2 -
 src/engine/library/notification.hpp             | 53 -----------------------
 src/niepce/notificationcenter.cpp               |  1 -
 src/niepce/notificationcenter.hpp               |  1 -
 src/niepce/ui/imageliststore.cpp                | 20 +++++----
 src/niepce/ui/imageliststore.hpp                |  1 -
 src/niepce/ui/niepcewindow.hpp                  |  1 -
 src/niepce/ui/workspacecontroller.cpp           |  2 -
 src/rust_bindings.hpp                           |  5 ---
 20 files changed, 90 insertions(+), 162 deletions(-)
---
diff --git a/crates/npc-engine/build.rs b/crates/npc-engine/build.rs
index 533c3d2..245c7d1 100644
--- a/crates/npc-engine/build.rs
+++ b/crates/npc-engine/build.rs
@@ -20,8 +20,6 @@ fn main() {
         // bindings for.
         .whitelist_type("eng::NiepceProperties")
         .rustified_enum("eng::NiepceProperties")
-        .whitelist_type("eng::QueriedContent")
-        .opaque_type("eng::QueriedContent")
         .opaque_type("std::.*")
         .whitelist_type("fwk::FileList")
         .whitelist_type("eng::IndexToXmp")
diff --git a/crates/npc-engine/src/db/fsfile.rs b/crates/npc-engine/src/db/fsfile.rs
index e213205..4dcfa47 100644
--- a/crates/npc-engine/src/db/fsfile.rs
+++ b/crates/npc-engine/src/db/fsfile.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - engine/db/fsfile.rs
  *
- * Copyright (C) 2017 Hubert Figuière
+ * Copyright (C) 2017-2020 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@ use std::path::{Path, PathBuf};
 use super::LibraryId;
 
 /// Describe a file on the file system
+#[derive(Clone)]
 pub struct FsFile {
     id: LibraryId,
     path: PathBuf,
diff --git a/crates/npc-engine/src/db/libfile.rs b/crates/npc-engine/src/db/libfile.rs
index 0fd4039..04d94b6 100644
--- a/crates/npc-engine/src/db/libfile.rs
+++ b/crates/npc-engine/src/db/libfile.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - eng/db/libfile.rs
  *
- * Copyright (C) 2017-2018 Hubert Figuière
+ * Copyright (C) 2017-2020 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -77,6 +77,7 @@ impl Into<i32> for FileType {
     }
 }
 
+#[derive(Clone)]
 pub struct LibFile {
     id: LibraryId,
     folder_id: LibraryId,
diff --git a/crates/npc-engine/src/library/commands.rs b/crates/npc-engine/src/library/commands.rs
index 9362b32..c5d6efb 100644
--- a/crates/npc-engine/src/library/commands.rs
+++ b/crates/npc-engine/src/library/commands.rs
@@ -1,7 +1,7 @@
 /*
- * niepce - engine/library/commands.rs
+ * niepce - npc-engine/library/commands.rs
  *
- * Copyright (C) 2017-2019 Hubert Figuière
+ * Copyright (C) 2017-2020 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,10 +17,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-use std::os::raw::c_void;
-
 use super::notification::LibNotification;
-use super::notification::{Content, Count, FileMove, MetadataChange};
+use super::notification::{Count, FileMove, MetadataChange};
+use super::queriedcontent::QueriedContent;
 use crate::db::filebundle::FileBundle;
 use crate::db::keyword::Keyword;
 use crate::db::label::Label;
@@ -185,13 +184,12 @@ pub fn cmd_request_metadata(lib: &Library, file_id: LibraryId) -> bool {
 pub fn cmd_query_folder_content(lib: &Library, folder_id: LibraryId) -> bool {
     match lib.get_folder_content(folder_id) {
         Ok(fl) => {
-            let mut value =
-                LibNotification::FolderContentQueried(unsafe { Content::new(folder_id) });
-            if let LibNotification::FolderContentQueried(ref mut content) = value {
-                for f in fl {
-                    unsafe { content.push(Box::into_raw(Box::new(f)) as *mut c_void) };
-                }
+            let mut content = QueriedContent::new(folder_id);
+            for f in fl {
+                content.push(f);
             }
+            let value = LibNotification::FolderContentQueried(content);
+
             // This time it's a fatal error since the purpose of this comand
             // is to retrieve.
             match lib.notify(value) {
@@ -273,9 +271,9 @@ pub fn cmd_add_keyword(lib: &Library, keyword: &str) -> LibraryId {
 pub fn cmd_query_keyword_content(lib: &Library, keyword_id: LibraryId) -> bool {
     match lib.get_keyword_content(keyword_id) {
         Ok(fl) => {
-            let mut content = unsafe { Content::new(keyword_id) };
+            let mut content = QueriedContent::new(keyword_id);
             for f in fl {
-                unsafe { content.push(Box::into_raw(Box::new(f)) as *mut c_void) };
+                content.push(f);
             }
             // This time it's a fatal error since the purpose of this comand
             // is to retrieve.
diff --git a/crates/npc-engine/src/library/mod.rs b/crates/npc-engine/src/library/mod.rs
index a6fb41b..ead2f6c 100644
--- a/crates/npc-engine/src/library/mod.rs
+++ b/crates/npc-engine/src/library/mod.rs
@@ -1,3 +1,4 @@
 pub mod commands;
 pub mod notification;
 pub mod op;
+pub mod queriedcontent;
diff --git a/crates/npc-engine/src/library/notification.rs b/crates/npc-engine/src/library/notification.rs
index 5e0a0c1..0348559 100644
--- a/crates/npc-engine/src/library/notification.rs
+++ b/crates/npc-engine/src/library/notification.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - engine/library/notification.rs
  *
- * Copyright (C) 2017-2019 Hubert Figuière
+ * Copyright (C) 2017-2020 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,23 +19,13 @@
 
 use crate::db::libfile::FileStatus;
 use crate::db::{Keyword, Label, LibFolder, LibMetadata, LibraryId};
+use super::queriedcontent::QueriedContent;
 use npc_fwk::base::PropertyIndex;
 use npc_fwk::toolkit::PortableChannel;
 use npc_fwk::PropertyValue;
 
-#[cfg(not(test))]
-use crate::root::eng::QueriedContent;
-#[cfg(not(test))]
-pub type Content = QueriedContent;
-
 pub type LcChannel = PortableChannel<LibNotification>;
 
-// Content need to be stubbed for the test as it is a FFI struct
-// and is missing the proper C++ code.
-#[cfg(test)]
-#[derive(Clone, Copy)]
-pub struct Content {}
-
 #[repr(i32)]
 #[allow(non_camel_case_types)]
 pub enum NotificationType {
@@ -123,11 +113,11 @@ pub enum LibNotification {
     AddedLabel(Label),
     FileMoved(FileMove),
     FileStatusChanged(FileStatusChange),
-    FolderContentQueried(Content),
+    FolderContentQueried(QueriedContent),
     FolderCounted(Count),
     FolderCountChanged(Count),
     FolderDeleted(LibraryId),
-    KeywordContentQueried(Content),
+    KeywordContentQueried(QueriedContent),
     KeywordCounted(Count),
     KeywordCountChanged(Count),
     LabelChanged(Label),
@@ -138,18 +128,6 @@ pub enum LibNotification {
     XmpNeedsUpdate,
 }
 
-impl Drop for LibNotification {
-    fn drop(&mut self) {
-        match *self {
-            LibNotification::FolderContentQueried(mut c)
-            | LibNotification::KeywordContentQueried(mut c) => unsafe {
-                c.destruct();
-            },
-            _ => (),
-        }
-    }
-}
-
 /// Send a notification for the file status change.
 /// Return `false` if sending failed.
 #[no_mangle]
@@ -309,24 +287,10 @@ pub unsafe extern "C" fn engine_library_notification_get_keyword(
 #[no_mangle]
 pub unsafe extern "C" fn engine_library_notification_get_content(
     n: *const LibNotification,
-) -> *const Content {
+) -> *const QueriedContent {
     match n.as_ref() {
         Some(&LibNotification::FolderContentQueried(ref c))
         | Some(&LibNotification::KeywordContentQueried(ref c)) => c,
         _ => unreachable!(),
     }
 }
-
-#[cfg(test)]
-use libc::c_void;
-
-#[cfg(test)]
-impl Content {
-    pub unsafe fn new(_: LibraryId) -> Self {
-        Content {}
-    }
-
-    pub unsafe fn push(&mut self, _: *mut c_void) {}
-
-    pub unsafe fn destruct(&self) {}
-}
diff --git a/crates/npc-engine/src/library/queriedcontent.rs b/crates/npc-engine/src/library/queriedcontent.rs
new file mode 100644
index 0000000..09ddfed
--- /dev/null
+++ b/crates/npc-engine/src/library/queriedcontent.rs
@@ -0,0 +1,56 @@
+/*
+ * niepce - npc-engine/library/queriedcontent.rs
+ *
+ * Copyright (C) 2020 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+use crate::db::libfile::LibFile;
+use crate::db::LibraryId;
+
+/// Queried content to pass a list of LibFile and the id of the container.
+#[derive(Clone)]
+pub struct QueriedContent {
+    id: LibraryId,
+    content: Vec<LibFile>,
+}
+
+impl QueriedContent {
+    pub fn new(id: LibraryId) -> Self {
+        QueriedContent {
+            id,
+            content: vec![],
+        }
+    }
+
+    pub fn push(&mut self, f: LibFile) {
+        self.content.push(f);
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn engine_queried_content_id(obj: &QueriedContent) -> LibraryId {
+    obj.id
+}
+
+#[no_mangle]
+pub extern "C" fn engine_queried_content_size(obj: &QueriedContent) -> u64 {
+    obj.content.len() as u64
+}
+
+#[no_mangle]
+pub extern "C" fn engine_queried_content_get(obj: &QueriedContent, idx: usize) -> *mut LibFile {
+    Box::into_raw(Box::new(obj.content[idx].clone()))
+}
diff --git a/src/Makefile.am b/src/Makefile.am
index a3a8259..64fa431 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,6 +22,7 @@ RUST_SOURCES = \
        @top_srcdir@/crates/npc-engine/src/library/commands.rs \
        @top_srcdir@/crates/npc-engine/src/library/mod.rs \
        @top_srcdir@/crates/npc-engine/src/library/notification.rs \
+       @top_srcdir@/crates/npc-engine/src/library/queriedcontent.rs \
        @top_srcdir@/crates/npc-engine/src/library/op.rs \
        @top_srcdir@/crates/npc-engine/src/lib.rs \
        @top_srcdir@/crates/npc-fwk/Cargo.toml \
diff --git a/src/engine/Makefile.am b/src/engine/Makefile.am
index 95749c1..fbbd293 100644
--- a/src/engine/Makefile.am
+++ b/src/engine/Makefile.am
@@ -28,7 +28,6 @@ libniepceengine_a_SOURCES = \
        db/properties.hpp db/properties.cpp \
        db/properties-def.hpp \
        library/clienttypes.hpp \
-       library/notification.hpp \
        library/thumbnailcache.hpp library/thumbnailcache.cpp \
        library/thumbnailnotification.hpp \
        importer/iimporter.hpp \
diff --git a/src/engine/db/bindings.hpp b/src/engine/db/bindings.hpp
index b27d1ca..12f058c 100644
--- a/src/engine/db/bindings.hpp
+++ b/src/engine/db/bindings.hpp
@@ -3,5 +3,4 @@
 #include "properties-enum.hpp"
 
 #include "engine/db/libmetadata.hpp"
-#include "engine/library/notification.hpp"
 #include "fwk/utils/files.hpp"
diff --git a/src/engine/db/libfile.cpp b/src/engine/db/libfile.cpp
index 700ee9b..0fe9973 100644
--- a/src/engine/db/libfile.cpp
+++ b/src/engine/db/libfile.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - db/libfile.cpp
  *
- * Copyright (C) 2007-2009 Hubert Figuiere
+ * Copyright (C) 2007-2020 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,40 +18,14 @@
  */
 
 #include "libfile.hpp"
-#include "engine/library/notification.hpp"
 
 namespace eng {
 
-// some glue for rust
-QueriedContent::QueriedContent(library_id_t _container)
-    : container(_container)
-    , files(new LibFileList)
-{
-}
-
-QueriedContent::~QueriedContent()
-{
-}
-
-void QueriedContent::push(void* f)
-{
-    files->push_back(eng::libfile_wrap(static_cast<LibFile*>(f)));
-}
-
 LibFilePtr libfile_wrap(eng::LibFile *lf)
 {
     return LibFilePtr(lf, &ffi::engine_db_libfile_delete);
 }
 
-
-LibFilePtr libfile_new(library_id_t id, library_id_t folder_id,
-                       library_id_t fs_file_id, const char *path,
-                       const char *name)
-{
-    return libfile_wrap(
-        ffi::engine_db_libfile_new(id, folder_id, fs_file_id, path, name));
-}
-
 /**
  * Converts a mimetype, which is expensive to calculate, into a FileType.
  * @param mime The mimetype we want to know as a filetype
diff --git a/src/engine/db/libfile.hpp b/src/engine/db/libfile.hpp
index 1df88d0..30d8928 100644
--- a/src/engine/db/libfile.hpp
+++ b/src/engine/db/libfile.hpp
@@ -43,8 +43,6 @@ typedef std::weak_ptr<LibFile> LibFileWeakPtr;
 typedef std::list<LibFilePtr> LibFileList;
 typedef std::shared_ptr<LibFileList> LibFileListPtr;
 
-LibFilePtr libfile_new(library_id_t, library_id_t, library_id_t, const char *,
-                       const char *);
 LibFilePtr libfile_wrap(LibFile *);
 }
 
diff --git a/src/niepce/notificationcenter.cpp b/src/niepce/notificationcenter.cpp
index abbdcd6..c1fcee4 100644
--- a/src/niepce/notificationcenter.cpp
+++ b/src/niepce/notificationcenter.cpp
@@ -20,7 +20,6 @@
 #include <boost/any.hpp>
 
 #include "fwk/base/debug.hpp"
-#include "engine/library/notification.hpp"
 #include "niepce/notifications.hpp"
 #include "niepce/notificationcenter.hpp"
 
diff --git a/src/niepce/notificationcenter.hpp b/src/niepce/notificationcenter.hpp
index 44f4541..69ffa19 100644
--- a/src/niepce/notificationcenter.hpp
+++ b/src/niepce/notificationcenter.hpp
@@ -23,7 +23,6 @@
 #include <sigc++/signal.h>
 
 #include "fwk/toolkit/notificationcenter.hpp"
-#include "engine/library/notification.hpp"
 #include "engine/library/thumbnailnotification.hpp"
 
 namespace niepce {
diff --git a/src/niepce/ui/imageliststore.cpp b/src/niepce/ui/imageliststore.cpp
index 813686b..00261b3 100644
--- a/src/niepce/ui/imageliststore.cpp
+++ b/src/niepce/ui/imageliststore.cpp
@@ -1,7 +1,7 @@
 /*
  * niepce - ui/imageliststore.cpp
  *
- * Copyright (C) 2008-2018 Hubert Figuiere
+ * Copyright (C) 2008-2020 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -116,20 +116,22 @@ void ImageListStore::on_lib_notification(const eng::LibNotification &ln)
     case eng::NotificationType::FOLDER_CONTENT_QUERIED:
     case eng::NotificationType::KEYWORD_CONTENT_QUERIED:
     {
-        auto param = engine_library_notification_get_content(&ln);
-        const auto& l = param->files;
+        auto content = engine_library_notification_get_content(&ln);
         if (type == eng::NotificationType::FOLDER_CONTENT_QUERIED) {
-            m_current_folder = param->container;
+            m_current_folder = ffi::engine_queried_content_id(content);
             m_current_keyword = 0;
         } else if (type == eng::NotificationType::KEYWORD_CONTENT_QUERIED) {
             m_current_folder = 0;
-            m_current_keyword = param->container;
+            m_current_keyword = ffi::engine_queried_content_id(content);
         }
         clear_content();
-        DBG_OUT("received folder content file # %lu", l->size());
-        for_each(l->cbegin(), l->cend(), [this] (const eng::LibFilePtr & f) {
-                this->add_libfile(f);
-            });
+        DBG_OUT("received folder content file # %lu", engine_queried_content_size(content));
+        eng::LibFileListPtr l(new eng::LibFileList);
+        for (uint64_t i = 0; i < engine_queried_content_size(content); i++) {
+            auto f = eng::libfile_wrap(engine_queried_content_get(content, i));
+            add_libfile(f);
+            l->push_back(f);
+        }
         // at that point clear the cache because the icon view is populated.
         getLibraryClient()->thumbnailCache().request(*l);
         break;
diff --git a/src/niepce/ui/imageliststore.hpp b/src/niepce/ui/imageliststore.hpp
index 8aed829..f46b3d8 100644
--- a/src/niepce/ui/imageliststore.hpp
+++ b/src/niepce/ui/imageliststore.hpp
@@ -29,7 +29,6 @@
 #include "fwk/base/propertybag.hpp"
 #include "fwk/toolkit/controller.hpp"
 #include "engine/db/libfile.hpp"
-#include "engine/library/notification.hpp"
 #include "engine/library/thumbnailnotification.hpp"
 #include "libraryclient/libraryclient.hpp"
 
diff --git a/src/niepce/ui/niepcewindow.hpp b/src/niepce/ui/niepcewindow.hpp
index 6c0b6af..e2ee763 100644
--- a/src/niepce/ui/niepcewindow.hpp
+++ b/src/niepce/ui/niepcewindow.hpp
@@ -31,7 +31,6 @@
 
 #include "fwk/toolkit/appframe.hpp"
 #include "fwk/toolkit/configdatabinder.hpp"
-#include "engine/library/notification.hpp"
 #include "libraryclient/libraryclient.hpp"
 #include "ui/moduleshell.hpp"
 #include "ui/workspacecontroller.hpp"
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index 95c5e5f..ec0b433 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -28,9 +28,7 @@
 #include "fwk/toolkit/application.hpp"
 #include "fwk/toolkit/configuration.hpp"
 #include "fwk/toolkit/gtkutils.hpp"
-#include "niepce/notifications.hpp"
 #include "engine/importer/iimporter.hpp"
-#include "engine/library/notification.hpp"
 #include "libraryclient/libraryclient.hpp"
 #include "dialogs/importdialog.hpp"
 #include "niepcewindow.hpp"
diff --git a/src/rust_bindings.hpp b/src/rust_bindings.hpp
index 5c6def7..278a65b 100644
--- a/src/rust_bindings.hpp
+++ b/src/rust_bindings.hpp
@@ -28,10 +28,6 @@
 namespace fwk {
 class FileList;
 }
-namespace eng {
-struct QueriedContent;
-}
-
 
 namespace ffi {
 class rust_str;
@@ -39,7 +35,6 @@ struct Utc;
 template <class T>
 struct DateTime;
 typedef rust_str str;
-typedef eng::QueriedContent QueriedContent;
 typedef eng::NiepceProperties Np;
 typedef fwk::FileList FileList;
 }


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