]> Creatis software - creaImageIO.git/commitdiff
Added local copy functionality.
authorcaballero <caballero>
Tue, 5 May 2009 14:53:38 +0000 (14:53 +0000)
committercaballero <caballero>
Tue, 5 May 2009 14:53:38 +0000 (14:53 +0000)
13 files changed:
appli/gimmick/main.cxx
src2/creaImageIOGimmick.cpp
src2/creaImageIOGimmick.h
src2/creaImageIOGimmickView.h
src2/creaImageIOSettings.cpp
src2/creaImageIOSettings.h
src2/creaImageIOTimestampDatabaseHandler.cpp
src2/creaImageIOTreeHandlerImageAdder.cpp
src2/creaImageIOTreeHandlerImageAdder.h
src2/creaImageIOWxGimmickView.cpp
src2/creaImageIOWxGimmickView.h
src2/creaImageIOWxTreeView.cpp
src2/creaImageIOWxTreeView.h

index ab08f622a10ec27ea2bcfb5816233eb8531bf878..b064a25829d19c22f0706e81a9681f42ddff3e6d 100644 (file)
@@ -25,19 +25,20 @@ int main(int ac, char* av[])
   po::options_description command("COMMANDS");
   command.add_options()
     ("print,p","Prints the local database tree  (default=off)")
-    ("file,f",po::value< vector<string> >(),"Adds the file to the local database")
+    ("files,f",po::value< vector<string> >(),"Adds the file(s) to the local database")
     ("dir,d",po::value< vector<string> >(),"Adds the directory to the local database")
     ("sync,s",po::value< vector<string> >(),"Synchronizes the local database with the files")
-    ("recurse,r","Recurse into sub-directories  (default=off)")
-       ("repair,R","Repair the database (on synchronization) (default=off)")
-       ("check,c","Check for attribute differences (on synchronization) (default=off)");
+       ("copy,c",po::value< vector<string> >(),"Copies the files into a local directory");
 
-  
   //Describes third group of options
   po::options_description option("OPTIONS");
   option.add_options()
     ("verbose,v",po::value<int>(&verb),"Verbosity level (default=`1')")
-    ("debug,D",po::value<int>(&deb),"Debug messages level (default=`0')");
+    ("debug,D",po::value<int>(&deb),"Debug messages level (default=`0')")
+       ("recurse,r","Recurse into sub-directories  (default=off)")
+       ("repair,R","Repair the database (on synchronization) (default=off)")
+       ("check,C","Check for attribute differences (on synchronization) (default=off)")
+       ("handler,H",po::value<string>(&handler),"Handler name (default=`Local database')");
  
   //Adds the groups into a big one
   po::options_description cmdline_options;
@@ -48,6 +49,7 @@ int main(int ac, char* av[])
   po::store(po::parse_command_line(ac, av, cmdline_options), vm);
   po::notify(vm);    
   
+  
   //Does something on each option
   //GENERIC
   if (vm.count("help")) {
@@ -73,7 +75,9 @@ int main(int ac, char* av[])
        ||vm.count("file")
        ||vm.count("dir")
        ||vm.count("sync")
+       ||vm.count("copy")
        )
+  {
 
     try
       {
@@ -106,12 +110,17 @@ int main(int ac, char* av[])
          if(out2.str().compare("1")==0){chk=true;}
          cout<<g.Synchronize(name.front(),rep,chk)<<"\n";
        }       
+       if (vm.count("copy")) {
+         std::vector<std::string> name=vm["copy"].as< vector<string> >();
+         g.CopyFiles(name,handler);
+       }
        g.Finalize();
       }
     catch (crea::Exception e)
       {
        e.Print();
       }
+  }
          
   
   return 0;
index 3213acabe760a9cd60259249f1820d18a86f163f..da76e9d848a253f8a8c203090348ea82f6c4e092 100644 (file)
@@ -325,6 +325,18 @@ namespace creaImageIO
 
   //========================================================================
  
+  void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
+  {
+         TreeHandler * handler=GetTreeHandler(d);
+         mImageAdder.SetCurrentDatabase(d);
+         mImageAdder.SetTreeHandler(handler);
+         mImageAdder.SetTimestampHandler(mTimestampDatabase);
+         mImageAdder.SetSynchronizer(mSynchronizer);
+         mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
+  }
+
+  //========================================================================
   std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
   {
          TreeHandler * handler=GetTreeHandler(d);
index b97df6f9f64adb6d6b5c27ac3ca4c5059470fcd7..6c3b1cc6e231754cced691044bd385d7009da19a 100644 (file)
@@ -96,6 +96,10 @@ namespace creaImageIO
        void RemoveFile(const std::string& d, 
                        tree::Node* filename);
 
+       /// Copies the files into the local directory
+       void CopyFiles(const std::vector<std::string>& filenames, const std::string& d );
+  
+
        ///Synchronizes the loaded data with the database d. If repair is true the database will be updated, otherwise 
        ///only a warning sign will be issued
        std::string Synchronize(const std::string& d, bool repair, bool checkAttributes);
index ef435c17ae9302d85fd1dab27d2c6f4578b0d884..c0e31d39e0d6c4f91f81906f175c7f7e79135ea8 100644 (file)
@@ -83,6 +83,9 @@ namespace creaImageIO
          virtual void AddIgnoreFile(tree::Node* toRemove)
          { GimmickError("INTERNAL ERROR : AddIgnoreFile not implemented"); }
       
+         ///Copies selected files
+         virtual void CopyFiles(const std::vector<std::string>& filenames)
+         { GimmickError("INTERNAL ERROR : CopyFiles not implemented"); }
       
       ///Validates the dimension compliance of the images with the maximum and minimum given, and between their sizes
       bool ValidateSelected (tree::Node* sel, int min_dim, int max_dim);
index 88ca918db48b35b2135e57a5458c97a97d42dddb..43d879ebba13b7aa377234591313d9cf51f55311 100644 (file)
@@ -34,6 +34,7 @@ namespace creaImageIO
                Keys.push_back(SETTINGS_SYNC_EVENT);
                Keys.push_back(SETTINGS_DBPATH);
                Keys.push_back(SETTINGS_SYNC_FREQ);
+               Keys.push_back(SETTINGS_COPY_PATH);
                readSettings(Keys, sets);
 
     }
@@ -55,6 +56,7 @@ namespace creaImageIO
        m_SettingsMap[SETTINGS_SYNC_EVENT] = "end";
        m_SettingsMap[SETTINGS_DBPATH] = "";
        m_SettingsMap[SETTINGS_DICOM_LIBRARY] = "gdcm";
+          m_SettingsMap[SETTINGS_COPY_PATH] = m_SettingsFileName.substr(0,m_SettingsFileName.find_last_of('\\')+1)+"Copied files";
        writeSettingsFile();
    }
 
index 76f8214f616fc51fe6af62477ff35104c09aa028..44c6e8c79561385bd59552be3e98e18324205c2e 100644 (file)
@@ -5,6 +5,7 @@
 #define SETTINGS_SYNC_EVENT     "<syncro_event>"
 #define SETTINGS_SYNC_FREQ       "<syncro_frequency>"
 #define SETTINGS_DBPATH       "<dbpath>"
+#define SETTINGS_COPY_PATH       "<copy_path>"
 
 
 namespace creaImageIO
index 167224be19496f29074a278b5c30343f542a8fb2..755678564966bb00b78299a9286f17fa3e3c681e 100644 (file)
@@ -279,9 +279,11 @@ namespace creaImageIO
   //=====================================================================
   std::string TimestampDatabaseHandler::IsIndexed(const std::string& path, const std::string& refdb)
   {
+       std::string pat=path.c_str();
+       CleanPath(pat);
        std::stringstream out;
        std::stringstream result;
-       out<<"SELECT ID FROM FILES WHERE PATH='"<<path<<"' AND REFERENCEDDB='"<<refdb<<"';";
+       out<<"SELECT ID FROM FILES WHERE PATH='"<<pat<<"' AND REFERENCEDDB='"<<refdb<<"';";
                
        CppSQLite3Query q;
        QUERYTIMESTAMPDB(out.str(),q);
index 2d6ab3a40f0e6a7960d16d0174dd6d8ad57580a1..6fb907a2490ee2162e8b8962b5b3daaa076b1965 100644 (file)
@@ -446,6 +446,85 @@ namespace creaImageIO
   
   //=======================================================================
 
+  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);
+                 
+                 }
+
+         }
+  }
+
+  //=======================================================================
 
  
 }
index 8311fdd59d318c9c785e8da5ebc9e0f7d8b0f176..fe3c31da644ebe6f28219e1810e23df13c376915 100644 (file)
@@ -123,6 +123,8 @@ namespace creaImageIO
                                                        std::vector<std::string> &i_ignorefiles,
                                                        std::vector<std::string> & attsModified,
                                                        std::vector<std::string> & newfiles);
+       ///Copies the files indicated in the vector and updates all databases
+       void CopyFiles(const std::vector<std::string>& filenames, const std::string directory  );
        ///Finds the node that matches the specified parameters
        void FindNode(tree::Node* parent, int level, 
                const std::string& searchParam, 
index 4381a6e1a38e422714d0d6fa19c93fd093555108..0664caf2269cd0c170f60800172d23c40ce66621 100644 (file)
@@ -611,6 +611,13 @@ namespace creaImageIO
          GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1);
   }
 
+  //=================================================
+  void WxGimmickView::CopyFiles(const std::vector<std::string>& filenames)
+  {
+         mGimmick->CopyFiles(filenames, crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection())));
+         wxMessageBox(std2wx("The selected files have been copied"),_T("Copy files"),wxOK,this);
+  }
+
    //=================================================
   void WxGimmickView::OnSynchronize(wxCommandEvent& event)
   {       
index fd22e481f08372c4c3879bebae8875d65fd8c2f1..3881c8543c1e07f1be260a0d14fe9c50134bd4e7 100644 (file)
@@ -63,6 +63,8 @@ namespace creaImageIO
     void AddIgnoreFile(tree::Node* toRemove);
     ///Resets the default image
     void ClearSelection();
+       ///Copies selected files
+       void CopyFiles(const std::vector<std::string>& filenames);
     
     
     ///Sends a request to read the currently selected node and the ones that surround it.
index 0f2363227ffda5ecaa3f036c153a7045f4f05a7d..3289962d950f8333c85c91ea189aca3cd6ef3a58 100644 (file)
@@ -231,12 +231,10 @@ namespace creaImageIO
        mAnonymizingID=m1Item->GetId();
        mLocalCopyID=m2Item->GetId();
        //Connect( mAnonymizingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnAnonymize) );
-       //Connect( mLocalCopyID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnLocalCopy) );
+       Connect( mLocalCopyID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnLocalCopy) );
        
        
 #endif // wxUSE_MENUS
-
-
        /// Initialize the first level splitter
          
        sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW  ,0);
@@ -269,19 +267,19 @@ namespace creaImageIO
   
   
   //=====================================================================
-   const std::vector<tree::Node*>& WxTreeView::GetSelected(int level)
+  const std::vector<tree::Node*>& WxTreeView::GetSelected(int level)
   {
     //  if (GetSelectedUpToDate(level)) 
     
     int l = level - 1;
     // the selection of upper level
-    std::vector<tree::Node*>& sel(mLevelList[level-1].Selected);
+       std::vector<tree::Node*>& sel(mLevelList[level-1].Selected);
     sel.clear();
          if (level == 1) 
       {
        sel.push_back(GetTreeHandler()->GetTree().GetTree());
       }
-    else if (level < 5
+         else if (level < mLevelList.size()+2 
     {
                long item = -1;
                for ( ;; )
@@ -508,8 +506,11 @@ namespace creaImageIO
              {
                std::string val;
                //  Temporary correction : it works but no explanation about the problem FCY
+               
                if(k==0 && level <3)
+               {
                  val = (*j)->GetAttribute("NumberOfChildren");
+               }
                else
                  val = (*j)->GetAttribute(mLevelList[l].key[k]);
                if(((*j)->GetAttributeDescriptor(mLevelList[l].key[k])).isDateEntry()) // Date
@@ -777,6 +778,53 @@ namespace creaImageIO
   }
   //================================================================
 
+  //================================================================
+  void WxTreeView::OnLocalCopy(wxCommandEvent& event)
+  {
+    wxBusyCursor busy;
+    
+       unsigned int tempLevel = mLastLevel;
+    mLastLevel+=1;
+    const std::vector<tree::Node*>& sel=GetSelected(mLastLevel+1);
+       
+    if(sel.size() != 0)
+       {
+           bool copy=false;
+           std::stringstream out;
+           std::string levelName=GetTreeHandler()->GetTree().GetLevelDescriptor(mLastLevel).GetName();
+           out<<"Copy ";
+           out<<sel.size();
+           if(sel.size()>1&&levelName.at(levelName.size()-1)!='s')
+             {
+               out<<" "<<levelName;
+               out<<"s to .gimmick?";
+             }
+           else
+             {
+               out<<" "<<GetTreeHandler()->GetTree().GetLevelDescriptor(mLastLevel).GetName()<<" to .gimmick?";
+             }
+           if (wxMessageBox(crea::std2wx(out.str()),
+                            _T("Remove Files"),
+                            wxYES_NO,this ) == wxYES)
+             {
+               copy = true;
+             }
+           if(copy)
+                 {
+                       std::vector<std::string> s;
+                       GetFilenamesAsString(sel,s);
+            GetGimmickView()->CopyFiles(s);
+                 }
+       }
+       else
+       {
+               mLastLevel = tempLevel;
+       }
+    
+    
+  }
+  //================================================================
+
   //================================================================
   void WxTreeView::SortLevel(int level)
   {      
@@ -945,6 +993,7 @@ namespace creaImageIO
                }
                if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0 )
                {
+
                        adr = GetCtrl(level)->GetItemData(item);
                        nod = ((ItemData*)adr)->node;
                        nodes.push_back(nod);
@@ -982,6 +1031,26 @@ namespace creaImageIO
       }
   }
 
+  //================================================================
+  void WxTreeView::GetFilenamesAsString(const std::vector<tree::Node*>& nodes, std::vector<std::string>&s)
+  {
+    std::vector<tree::Node*>::const_iterator i;
+    
+    for (i=nodes.begin(); i!=nodes.end(); ++i)
+      {
+                 if((*i)->GetLevel()<mLevelList.size())
+                 {
+                        GetTreeHandler()->LoadChildren(*i,0);
+                        GetFilenamesAsString((*i)->GetChildrenList(),s);
+                 }
+                 else
+                 {
+                       std::string filename=(*i)->GetAttribute("FullFileName");
+                       s.push_back(filename);
+                 }
+      }
+  }
+
    //================================================================
   void WxTreeView::SetColor(int l, int item)
   {
index c941606768f05410e18eda9417c9ef6b36595402..9d8c34afdbc9de07c6b4f4e29c01d77420835bd3 100644 (file)
@@ -53,6 +53,9 @@ namespace creaImageIO
       
       ///Callback when the user need the items filtered
       void OnPopupFilter(wxCommandEvent& event);
+
+         ///Callback when the user need the items filtered
+      void OnLocalCopy(wxCommandEvent& event);
       
       ///Callback on mouse click
       void OnMouseClick(wxMouseEvent& event);
@@ -104,7 +107,9 @@ namespace creaImageIO
       void ValidateSelectedImages(bool isSelection);   
       ///Gets selected filenames
       void GetSelectedAsString(std::vector<std::string>&s);
-      /// Gets the next nodes on the list, be it up(true) or down(false).
+          ///Gets the filenames of the given nodes and returns them on the given vector. Is recursive.
+         void GetFilenamesAsString(const std::vector<tree::Node*>& nodes, std::vector<std::string>&s);
+        /// Gets the next nodes on the list, be it up(true) or down(false).
       void GetNodes(std::vector<tree::Node*>& nodes, bool direction);
       /// Updates the view of a level given the selected items of upper level
       /// Recursive method