[niepce] implement moveFileToFolder in the library



commit 17ca035835064337b78f4bb09c76bfff9d00f2fe
Author: Hub Figuiere <hub figuiere net>
Date:   Mon Nov 14 22:48:16 2011 -0800

    implement moveFileToFolder in the library

 src/engine/db/library.cpp           |   23 +++++++++++++++++++++++
 src/engine/db/library.hpp           |    5 ++++-
 src/engine/db/test_library.cpp      |   13 ++++++++++++-
 src/engine/library/commands.cpp     |   19 +++++++++++++++----
 src/engine/library/commands.hpp     |    2 ++
 src/libraryclient/clientimpl.cpp    |    9 +++++++++
 src/libraryclient/clientimpl.hpp    |    1 +
 src/libraryclient/libraryclient.cpp |    5 +++++
 src/libraryclient/libraryclient.hpp |    1 +
 9 files changed, 72 insertions(+), 6 deletions(-)
---
diff --git a/src/engine/db/library.cpp b/src/engine/db/library.cpp
index 53609a3..485ad34 100644
--- a/src/engine/db/library.cpp
+++ b/src/engine/db/library.cpp
@@ -773,6 +773,29 @@ bool Library::setMetaData(library_id_t file_id, fwk::PropertyIndex meta,
     return retval;
 }
 
+
+bool Library::moveFileToFolder(library_id_t file_id, library_id_t folder_id)
+{
+    SQLStatement sql(boost::format("SELECT id FROM folders WHERE id = %1%;") % folder_id);
+    try {
+        if(m_dbdrv->execute_statement(sql)) {
+            if(m_dbdrv->read_next_row()) {
+                // we have the destination folder
+                SQLStatement sql2(boost::format("UPDATE files SET parent_id = %1% "
+                                     " WHERE id = %2%;") % folder_id % file_id);
+                if(m_dbdrv->execute_statement(sql2)) {
+                    return true;
+                }
+            }
+        }
+    }
+    catch(fwk::Exception & e)
+    {
+        DBG_OUT("db exception %s", e.what());
+    }
+    return false;
+}
+
 void Library::getAllLabels(const Label::ListPtr & l)
 {
     SQLStatement sql("SELECT id,name,color FROM labels ORDER BY id");
diff --git a/src/engine/db/library.hpp b/src/engine/db/library.hpp
index 30d9d3d..eaad5bf 100644
--- a/src/engine/db/library.hpp
+++ b/src/engine/db/library.hpp
@@ -68,7 +68,8 @@ public:
         NOTIFY_LABEL_CHANGED,
         NOTIFY_LABEL_DELETED,
         NOTIFY_XMP_NEEDS_UPDATE,
-        NOTIFY_FOLDER_COUNTED
+        NOTIFY_FOLDER_COUNTED,
+        NOTIFY_FILE_MOVED
 		} NotifyType;
 
 		Library(const std::string & dir, const fwk::NotificationCenter::Ptr & nc);
@@ -164,6 +165,8 @@ public:
     bool setMetaData(library_id_t file_id, const LibMetadata::Ptr & );
     bool setMetaData(library_id_t file_id, fwk::PropertyIndex meta, const fwk::PropertyValue & value);
 
+    bool moveFileToFolder(library_id_t file_id, library_id_t folder_id);
+
 		void getAllLabels(const eng::Label::ListPtr & l);
     library_id_t addLabel(const std::string & name, const std::string & color);
     library_id_t addLabel(const std::string & name, const fwk::RgbColor & c);
diff --git a/src/engine/db/test_library.cpp b/src/engine/db/test_library.cpp
index a77cc09..fe3de4e 100644
--- a/src/engine/db/test_library.cpp
+++ b/src/engine/db/test_library.cpp
@@ -56,7 +56,18 @@ int test_main(int, char *[])
     
     int file_id = lib.addFile(folder_added->id(), "foo/myfile", false);
     BOOST_CHECK(file_id > 0);
-    
+
+    BOOST_CHECK(lib.moveFileToFolder(file_id, 100) == false);
+    BOOST_CHECK(lib.moveFileToFolder(file_id, folder_added->id()));
+
+    int count = lib.countFolder(folder_added->id());
+    BOOST_CHECK(count == 1);
+
+    const eng::LibFile::ListPtr fl(new eng::LibFile::List);
+    lib.getFolderContent(folder_added->id(), fl);
+    BOOST_CHECK(fl->size() == (size_t)count);
+    BOOST_CHECK(fl->front()->id() == file_id);
+
     BOOST_CHECK(unlink(lib.dbName().c_str()) != -1);
     return 0;
 }
diff --git a/src/engine/library/commands.cpp b/src/engine/library/commands.cpp
index 87fc89e..6034d60 100644
--- a/src/engine/library/commands.cpp
+++ b/src/engine/library/commands.cpp
@@ -118,14 +118,25 @@ void Commands::cmdSetMetadata(const Library::Ptr & lib,
                               library_id_t file_id, fwk::PropertyIndex meta, 
                               const fwk::PropertyValue & value)
 {
-	metadata_desc_t m;
-	m.id = file_id;
-	m.meta = meta;
-	m.value = value;
+    metadata_desc_t m;
+    m.id = file_id;
+    m.meta = meta;
+    m.value = value;
     lib->setMetaData(file_id, meta, value);
     lib->notify(Library::NOTIFY_METADATA_CHANGED, boost::any(m));
 }
 
+void Commands::cmdMoveFileToFolder(const Library::Ptr & lib, 
+                                   library_id_t file_id, library_id_t folder_id)
+{
+    if(lib->moveFileToFolder(file_id, folder_id)) {
+        std::pair<library_id_t, library_id_t> move;
+        move.first = file_id;
+        move.second = folder_id;
+        lib->notify(Library::NOTIFY_FILE_MOVED, boost::any(move));
+    }
+}
+
 void Commands::cmdListAllLabels(const Library::Ptr & lib)
 {
     eng::Label::ListPtr l(new eng::Label::List);
diff --git a/src/engine/library/commands.hpp b/src/engine/library/commands.hpp
index e621668..fc676a6 100644
--- a/src/engine/library/commands.hpp
+++ b/src/engine/library/commands.hpp
@@ -54,6 +54,8 @@ public:
     static void cmdSetMetadata(const Library::Ptr & lib,
                                library_id_t file_id, fwk::PropertyIndex meta, 
                                const fwk::PropertyValue & value);
+    static void cmdMoveFileToFolder(const Library::Ptr & lib, 
+                                    library_id_t file_id, library_id_t folder_id);
     static void cmdListAllLabels(const Library::Ptr & lib);
     static void cmdCreateLabel(const Library::Ptr & lib, const std::string & s, 
                                const std::string & color);
diff --git a/src/libraryclient/clientimpl.cpp b/src/libraryclient/clientimpl.cpp
index 7ff16dc..cd0a9a9 100644
--- a/src/libraryclient/clientimpl.cpp
+++ b/src/libraryclient/clientimpl.cpp
@@ -120,6 +120,15 @@ tid_t ClientImpl::setMetadata(eng::library_id_t file_id, int meta, const fwk::Pr
     return id;
 }
 
+tid_t ClientImpl::moveFileToFolder(eng::library_id_t file_id, eng::library_id_t folder_id)
+{
+    tid_t id = LibraryClient::newTid();
+    Op::Ptr op(new Op(id, boost::bind(&Commands::cmdMoveFileToFolder, _1,
+                                      file_id, folder_id)));
+    m_localLibrary->schedule(op);
+    return id;    
+}
+
 
 tid_t ClientImpl::getAllLabels()
 {
diff --git a/src/libraryclient/clientimpl.hpp b/src/libraryclient/clientimpl.hpp
index 385615a..811a5bc 100644
--- a/src/libraryclient/clientimpl.hpp
+++ b/src/libraryclient/clientimpl.hpp
@@ -47,6 +47,7 @@ public:
 	eng::tid_t countFolder(eng::library_id_t id);
 	eng::tid_t requestMetadata(eng::library_id_t id);
     eng::tid_t setMetadata(eng::library_id_t id, int meta, const fwk::PropertyValue & value);
+    eng::tid_t moveFileToFolder(eng::library_id_t file_id, eng::library_id_t folder_id);
 
     eng::tid_t getAllLabels();
     eng::tid_t createLabel(const std::string & s, const std::string & color);
diff --git a/src/libraryclient/libraryclient.cpp b/src/libraryclient/libraryclient.cpp
index c550b77..a3541fe 100644
--- a/src/libraryclient/libraryclient.cpp
+++ b/src/libraryclient/libraryclient.cpp
@@ -90,6 +90,11 @@ eng::tid_t LibraryClient::setMetadata(library_id_t id, fwk::PropertyIndex meta,
     return m_pImpl->setMetadata(id, meta, value);
 }
 
+eng::tid_t LibraryClient::moveFileToFolder(eng::library_id_t file_id, eng::library_id_t folder_id)
+{
+    return m_pImpl->moveFileToFolder(file_id, folder_id);
+}
+
 eng::tid_t LibraryClient::getAllLabels()
 {
     return m_pImpl->getAllLabels();
diff --git a/src/libraryclient/libraryclient.hpp b/src/libraryclient/libraryclient.hpp
index 91b7ff4..289d442 100644
--- a/src/libraryclient/libraryclient.hpp
+++ b/src/libraryclient/libraryclient.hpp
@@ -66,6 +66,7 @@ namespace libraryclient {
 
     /** set the metadata */
 	  eng::tid_t setMetadata(eng::library_id_t id, fwk::PropertyIndex meta, const fwk::PropertyValue & value);
+	  eng::tid_t moveFileToFolder(eng::library_id_t file_id, eng::library_id_t folder_id);
 
     /** get all the labels */
     eng::tid_t getAllLabels();



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