]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOTreeHandlerImageAdder.cpp
Added Settings dialog, customize configuration options and the listener on an externa...
[creaImageIO.git] / src2 / creaImageIOTreeHandlerImageAdder.cpp
index 2d6ab3a40f0e6a7960d16d0174dd6d8ad57580a1..471c785317df2e0e0c4289d6dcdcedff6b50a5dd 100644 (file)
@@ -200,12 +200,14 @@ namespace creaImageIO
                        removedOn<<time(0);
                        mSynchronizer->InsertIgnoreFile(addKey, itr->string(),"0",removedOn.str(),mCurrentDB);
                        }
+                       
                        mProgressSignal(mProgress);
                        if (mProgress.GetStop()) 
                        {
                        //itr = end_itr;
                        break;
                        }
+                       
                }
                mTimestampHandler->SetAttribute("TopLevelNodeId",parent_id,"PATH",dirpath);
          }
@@ -445,7 +447,135 @@ namespace creaImageIO
   }
   
   //=======================================================================
+  void TreeHandlerImageAdder::FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node)
+  {
+         if(level>1)
+         {
+                 std::vector<tree::Node*>::iterator iter;
+                 for(iter=parent->GetChildrenList().begin();iter!=parent->GetChildrenList().end() && node==0 ;++iter)
+                 {
+                         FindNodePartial(*iter,level-1,searchParam,searchVal,node);
+                 }
+         }
+         else
+         {
+                 if(parent->GetAttribute(searchParam).find(searchVal)<9000)
+                 {
+                         node=parent;
+                         return;
+                 }
+
+         }
+  }
+  
+  //=======================================================================
+
+  void TreeHandlerImageAdder::CopyFiles(const std::vector<std::string>& filenames, const std::string directory  )
+  {
+         std::vector<std::string>::const_iterator i;
+         if(!boost::filesystem::exists(directory))
+         {
+         boost::filesystem::create_directory(boost::filesystem::path(directory));
+         mTimestampHandler->AddDirectory("",directory,fs::last_write_time(directory)+10, time(0),mCurrentDB);
+         mSynchronizer->InsertAddOp(directory,"0","0",mCurrentDB);
+         }
+         std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",directory,mCurrentDB);
+         std::string parent_id=mTimestampHandler->IsIndexed(directory,mCurrentDB);
+         size_t last;
+         std::vector<std::string> newNames;
+         for(i=filenames.begin();i!=filenames.end();++i)
+         {
+                 std::string dir=directory.c_str();
+                 mTimestampHandler->CleanPath(dir);
+                 if(boost::filesystem::exists(*i) && (*i).find(dir)==std::string::npos)
+                 {
+                 std::string path=*i;
+                 last=(*i).find_last_of('/');
+                 std::string f="\\"+(*i).substr(last+1);
+       
+                 int p=1;
+                 std::stringstream out;
+                 out<<directory<<f;
+                 while(boost::filesystem::exists(out.str()))
+                 {
+                         out.str("");
+                         out<<directory<<f.substr(0,f.size()-4)<<"("<<p<<")"<<f.substr(f.size()-4);
+                         p++;
+                 }
+                 std::string result=out.str();
+                 mTimestampHandler->CleanPath(result);
+                 boost::filesystem::copy_file((*i),result);
+
+                 //To update image database
+                 mTreeHandler->SetAttribute("Image","FullFileName",result,"FullFileName", (*i));
+                 
+                 //To update timestamp database
+                 mTimestampHandler->SetAttribute("PATH",result,"PATH",(*i));
+                 mTimestampHandler->SetAttribute("PARENT_ID",parent_id,"PATH",result);
+                 std::stringstream t;
+                 t<<fs::last_write_time(directory)+10;
+                 mTimestampHandler->SetAttribute("LastModified",t.str(),"PATH",result);
+
+                 //To update maintenance database
+                 //1.Add the new path and increase number of children on new operation.
+                 std::stringstream removedOn;
+                 removedOn<<time(0);
+                 //Inserts the file
+                 mSynchronizer->InsertIgnoreFile(addKey, result,"0",removedOn.str(),mCurrentDB);
+                 //Gets the number of files added
+                 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey,mCurrentDB)).c_str());
+                 files=files+1;
+                 std::stringstream fil;
+                 fil<<files;
+                 //Sets the new number of files
+                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",fil.str(),"ADD_KEY",addKey,mCurrentDB);
+                 fil.str("");
 
+                 //2.Set the old path as removed and decrease number of children on old operation.
+                 //Gets the old add key
+                 std::string oldAddKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path,mCurrentDB);
+                 //Sets the file as removed
+                 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH = '"+path+"' AND ADD_KEY",oldAddKey,mCurrentDB);
+                 //Gets the number of files added
+                 files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",oldAddKey,mCurrentDB)).c_str());
+                 files=files-1;
+                 fil<<files;
+                 //Sets the new number of files
+                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",fil.str(),"ADD_KEY",oldAddKey,mCurrentDB);
+                 
+                 }
+
+         }
+  }
+
+  //=======================================================================
+
+  void TreeHandlerImageAdder::DeleteDriveFromMainDB(const std::string& drive)
+  {
+         //Delete from local database and others
+         tree::Node* node=0;
+         mTreeHandler->LoadChildren(NULL,4);
+         FindNodePartial(mTreeHandler->GetTree().GetChildrenList()[0],3,"FullFileName",drive,node);
+         while(node!=0)
+         {
+         mTreeHandler->Remove(node);
+         node=0;
+         mTreeHandler->LoadChildren(NULL,4);
+         FindNodePartial(mTreeHandler->GetTree().GetChildrenList()[0],3,"FullFileName",drive,node); 
+         }
+  }
+
+   //=======================================================================
+
+  void TreeHandlerImageAdder::DeleteDriveFromOtherDB(const std::string& drive)
+  {
+         //Delete from timestamp
+         mTimestampHandler->RemoveEntries("FILES", "PATH", "LIKE", drive+"%");
+
+         //Delete from maintenance
+         mSynchronizer->RemoveEntries("ADD_OPS", "PATH", "LIKE", drive+"%");
+         mSynchronizer->RemoveEntries("IGNORED_FILES", "PATH", "LIKE", drive+"%");
+  }
 
  
 }