]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOGimmick.cpp
BUG doxygen images
[creaImageIO.git] / src2 / creaImageIOGimmick.cpp
index 64099e589c651e2bbf37fe4fefa1667cfcc7ef0a..5ae843801e4952e9d89ca7e6d7b81497fe9a1959 100644 (file)
@@ -1,21 +1,19 @@
 #include <creaImageIOGimmick.h>
 
 #include <creaImageIOSystem.h>
-#include <creaImageIOImageFinder.h>
 
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 
 namespace creaImageIO
 {
-  
+
+
   //==============================================================
   Gimmick::Gimmick()
+    : mImageAdder(0)
   {    
-    crea::MessageManager::RegisterMessageType("Gimmick!",
-                                             "Gimmick",1);
-    crea::MessageManager::RegisterMessageType("Gimmick! DEBUG",
-                                             "Gimmick",0);
+    RegisterGimmickMessageTypes();
   }
   //==============================================================
 
@@ -36,9 +34,12 @@ namespace creaImageIO
     CreateUserSettingsDirectory();
     // Sets the current directory to the home dir
     mCurrentDirectory =  GetHomeDirectory();
+       mSynchronizer=0;
 
     // Create local database handler
     mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath());
+    // Add it to the TreeHandlerMap
+    mTreeHandlerMap["Local database"] = mLocalDatabase;
     // Create or open local database
     if (! boost::filesystem::exists( GetLocalDatabasePath() ) )
       {
@@ -68,6 +69,37 @@ namespace creaImageIO
          }
        
       }
+
+
+       // Creates files and directories database
+    mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
+    // Create or open local database
+    if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
+      {
+       std::string mess = "Timestamp database '";
+       mess += GetTimestampDatabasePath();
+       mess += "' does not exist : creating it";
+       GimmickMessage(1,mess<<std::endl);
+       
+       if ( ! mTimestampDatabase->Create() )
+         {
+           GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
+         }
+       
+     }
+    else 
+      {
+       /// Open and test it
+       GimmickMessage(1,"Opening Timestamp database '"
+                      <<GetTimestampDatabasePath()<<"' "
+                      <<std::endl);
+       if ( ! mTimestampDatabase->Open() )
+         {
+           GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
+         }
+       
+      }
+
   }
   //================================================================
 
@@ -76,6 +108,7 @@ namespace creaImageIO
   void Gimmick::Finalize()
   {
     delete mLocalDatabase;
+       delete mTimestampDatabase;
   }
   //==============================================================
 
@@ -131,6 +164,22 @@ namespace creaImageIO
       }
     return mLocalDatabasePath;    
   }
+
+  //================================================================
+
+  //================================================================
+  const std::string& Gimmick::GetTimestampDatabasePath()
+  {
+    if (mTimestampDatabasePath.size()==0) 
+      {
+       mTimestampDatabasePath = GetUserSettingsDirectory();
+       mTimestampDatabasePath += "timestamp_database.sqlite3";
+       boost::algorithm::replace_all( mTimestampDatabasePath,
+                                      INVALID_FILE_SEPARATOR , 
+                                      VALID_FILE_SEPARATOR);
+      }
+    return mTimestampDatabasePath;    
+  }
   //========================================================================
 
   //========================================================================
@@ -154,7 +203,7 @@ namespace creaImageIO
   /// Sets message level
   void Gimmick::SetMessageLevel(int l)
   {
-    crea::MessageManager::SetMessageLevel("Gimmick!",l);
+    SetGimmickMessageLevel(l);
   }
   //========================================================================
 
@@ -162,42 +211,85 @@ namespace creaImageIO
   /// Sets message level
   void Gimmick::SetDebugMessageLevel(int l)
   {
-    crea::MessageManager::SetMessageLevel("Gimmick! DEBUG",l);
+    SetGimmickDebugMessageLevel(l);
   }
   //========================================================================
 
   //========================================================================
-  /// Add a file to the local database
-  void Gimmick::AddFileToLocalDatabase(const std::string& f)
-  {
-    ImageFinder finder(mLocalDatabase);
-    if (finder.IsHandledFile(f)) 
+  /// Returns the tree handler with the given name
+  TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const 
+  {  
+    TreeHandlerMapType::const_iterator i;
+    i = GetTreeHandlerMap().find(name);
+    if ( i == GetTreeHandlerMap().end() )
       {
-       finder.AddFile(f);
+       GimmickError("TreeHandler '"<<name<<"' does not exist");
       }
-    else
-      {
-       GimmickError("File '"<<f<<"' does not exist or is not handled");
-      }    
+    return i->second;
+  }
+
+  //========================================================================
+  ///Returns the timestamp database handler
+  TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const 
+  {  
+    return mTimestampDatabase;
+  }
+
+
+  //========================================================================
+  /// Add the files to the tree handler
+  void Gimmick::AddFiles(const std::string& d, 
+                       const std::vector<std::string>& filenames)
+  {
+    GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
+    mImageAdder.SetTreeHandler(GetTreeHandler(d));
+       mImageAdder.SetTimestampHandler(mTimestampDatabase);
+    mImageAdder.AddFiles(filenames);
+
   }
   //========================================================================
 
   //========================================================================
   /// Add a dir to the local database
-  void Gimmick::AddDirToLocalDatabase(const std::string& f, bool recurse)
+  void Gimmick::AddDir(const std::string& d, const std::string& f, 
+                      bool recurse)
   {
-    ImageFinder finder(mLocalDatabase);
-    
-    finder.AddDirectory(f,recurse);
+    GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
+                  <<recurse<<std::endl);
+
+       TreeHandler * handler=GetTreeHandler(d);
+    mImageAdder.SetTreeHandler(handler);
+       mImageAdder.SetTimestampHandler(mTimestampDatabase);
+    mImageAdder.AddDirectory(f,recurse);
+       //Synchronize(true, handler);
     
   }
+
   //========================================================================
 
+  //========================================================================
+  void Gimmick::Synchronize(bool update, TreeHandler* handler)
+  {
+         GimmickMessage(4,"Synchronizing. Update:"<<update<<std::endl);
+         if(mSynchronizer==0)
+         {
+                 mSynchronizer=new Synchronizer(handler);
+         }
+         else
+         {
+                 mSynchronizer->SetTreeHandler(handler);
+         }
+         mSynchronizer->Synchronize(update);
+
+  }
+
   //========================================================================
   /// 
-  void Gimmick::PrintLocalDatabase()
+  void Gimmick::Print(const std::string& d)
   {
-    mLocalDatabase->GetTree().Print();
+    GetTreeHandler(d)->GetTree().Print();
   }
   //========================================================================