]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOGimmick.cpp
BUG doxygen images
[creaImageIO.git] / src2 / creaImageIOGimmick.cpp
index 21d9cd4d01a966e8dd5d60a23f309bfcff8d2763..5ae843801e4952e9d89ca7e6d7b81497fe9a1959 100644 (file)
@@ -1,7 +1,6 @@
 #include <creaImageIOGimmick.h>
 
 #include <creaImageIOSystem.h>
-#include <creaImageIOImageFinder.h>
 
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
@@ -12,6 +11,7 @@ namespace creaImageIO
 
   //==============================================================
   Gimmick::Gimmick()
+    : mImageAdder(0)
   {    
     RegisterGimmickMessageTypes();
   }
@@ -34,12 +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() ) )
       {
@@ -69,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()<<"'");
+         }
+       
+      }
+
   }
   //================================================================
 
@@ -77,6 +108,7 @@ namespace creaImageIO
   void Gimmick::Finalize()
   {
     delete mLocalDatabase;
+       delete mTimestampDatabase;
   }
   //==============================================================
 
@@ -132,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;    
+  }
   //========================================================================
 
   //========================================================================
@@ -168,7 +216,7 @@ namespace creaImageIO
   //========================================================================
 
   //========================================================================
-  /// Add a file to the local database
+  /// Returns the tree handler with the given name
   TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const 
   {  
     TreeHandlerMapType::const_iterator i;
@@ -180,22 +228,25 @@ namespace creaImageIO
     return i->second;
   }
 
+  //========================================================================
+  ///Returns the timestamp database handler
+  TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const 
+  {  
+    return mTimestampDatabase;
+  }
+
 
   //========================================================================
-  /// Add a file to the local database
-  void Gimmick::AddFile(const std::string& d, const std::string& f)
+  /// Add the files to the tree handler
+  void Gimmick::AddFiles(const std::string& d, 
+                       const std::vector<std::string>& filenames)
   {
-    GimmickMessage(2,"Adding file '"<<f<<"' to '"<<d<<"'"<<std::endl);
+    GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
  
-    ImageFinder finder(GetTreeHandler(d));
-    if (finder.IsHandledFile(f)) 
-      {
-       finder.AddFile(f);
-      }
-    else
-      {
-       GimmickError("File '"<<f<<"' does not exist or is not handled");
-      }    
+    mImageAdder.SetTreeHandler(GetTreeHandler(d));
+       mImageAdder.SetTimestampHandler(mTimestampDatabase);
+    mImageAdder.AddFiles(filenames);
+
   }
   //========================================================================
 
@@ -207,12 +258,32 @@ namespace creaImageIO
     GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
                   <<recurse<<std::endl);
 
-    ImageFinder finder(GetTreeHandler(d));
-    
-    finder.AddDirectory(f,recurse);
+       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);
+
+  }
 
   //========================================================================
   ///