From 9b19229069b38b9b14563f427554fa7c8fd9ac6f Mon Sep 17 00:00:00 2001 From: caballero Date: Tue, 5 May 2009 14:53:38 +0000 Subject: [PATCH] Added local copy functionality. --- appli/gimmick/main.cxx | 21 +++-- src2/creaImageIOGimmick.cpp | 12 +++ src2/creaImageIOGimmick.h | 4 + src2/creaImageIOGimmickView.h | 3 + src2/creaImageIOSettings.cpp | 2 + src2/creaImageIOSettings.h | 1 + src2/creaImageIOTimestampDatabaseHandler.cpp | 4 +- src2/creaImageIOTreeHandlerImageAdder.cpp | 79 +++++++++++++++++++ src2/creaImageIOTreeHandlerImageAdder.h | 2 + src2/creaImageIOWxGimmickView.cpp | 7 ++ src2/creaImageIOWxGimmickView.h | 2 + src2/creaImageIOWxTreeView.cpp | 81 ++++++++++++++++++-- src2/creaImageIOWxTreeView.h | 7 +- 13 files changed, 211 insertions(+), 14 deletions(-) diff --git a/appli/gimmick/main.cxx b/appli/gimmick/main.cxx index ab08f62..b064a25 100644 --- a/appli/gimmick/main.cxx +++ b/appli/gimmick/main.cxx @@ -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 >(),"Adds the file to the local database") + ("files,f",po::value< vector >(),"Adds the file(s) to the local database") ("dir,d",po::value< vector >(),"Adds the directory to the local database") ("sync,s",po::value< vector >(),"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 >(),"Copies the files into a local directory"); - //Describes third group of options po::options_description option("OPTIONS"); option.add_options() ("verbose,v",po::value(&verb),"Verbosity level (default=`1')") - ("debug,D",po::value(&deb),"Debug messages level (default=`0')"); + ("debug,D",po::value(&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(&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< name=vm["copy"].as< vector >(); + g.CopyFiles(name,handler); + } g.Finalize(); } catch (crea::Exception e) { e.Print(); } + } return 0; diff --git a/src2/creaImageIOGimmick.cpp b/src2/creaImageIOGimmick.cpp index 3213aca..da76e9d 100644 --- a/src2/creaImageIOGimmick.cpp +++ b/src2/creaImageIOGimmick.cpp @@ -325,6 +325,18 @@ namespace creaImageIO //======================================================================== + void Gimmick::CopyFiles(const std::vector& 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); diff --git a/src2/creaImageIOGimmick.h b/src2/creaImageIOGimmick.h index b97df6f..6c3b1cc 100644 --- a/src2/creaImageIOGimmick.h +++ b/src2/creaImageIOGimmick.h @@ -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& 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); diff --git a/src2/creaImageIOGimmickView.h b/src2/creaImageIOGimmickView.h index ef435c1..c0e31d3 100644 --- a/src2/creaImageIOGimmickView.h +++ b/src2/creaImageIOGimmickView.h @@ -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& 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); diff --git a/src2/creaImageIOSettings.cpp b/src2/creaImageIOSettings.cpp index 88ca918..43d879e 100644 --- a/src2/creaImageIOSettings.cpp +++ b/src2/creaImageIOSettings.cpp @@ -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(); } diff --git a/src2/creaImageIOSettings.h b/src2/creaImageIOSettings.h index 76f8214..44c6e8c 100644 --- a/src2/creaImageIOSettings.h +++ b/src2/creaImageIOSettings.h @@ -5,6 +5,7 @@ #define SETTINGS_SYNC_EVENT "" #define SETTINGS_SYNC_FREQ "" #define SETTINGS_DBPATH "" +#define SETTINGS_COPY_PATH "" namespace creaImageIO diff --git a/src2/creaImageIOTimestampDatabaseHandler.cpp b/src2/creaImageIOTimestampDatabaseHandler.cpp index 167224b..7556785 100644 --- a/src2/creaImageIOTimestampDatabaseHandler.cpp +++ b/src2/creaImageIOTimestampDatabaseHandler.cpp @@ -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='"<& filenames, const std::string directory ) + { + std::vector::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 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<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<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<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<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<SetAttribute("FILES_ADDED","ADD_OPS",fil.str(),"ADD_KEY",oldAddKey,mCurrentDB); + + } + + } + } + + //======================================================================= } diff --git a/src2/creaImageIOTreeHandlerImageAdder.h b/src2/creaImageIOTreeHandlerImageAdder.h index 8311fdd..fe3c31d 100644 --- a/src2/creaImageIOTreeHandlerImageAdder.h +++ b/src2/creaImageIOTreeHandlerImageAdder.h @@ -123,6 +123,8 @@ namespace creaImageIO std::vector &i_ignorefiles, std::vector & attsModified, std::vector & newfiles); + ///Copies the files indicated in the vector and updates all databases + void CopyFiles(const std::vector& filenames, const std::string directory ); ///Finds the node that matches the specified parameters void FindNode(tree::Node* parent, int level, const std::string& searchParam, diff --git a/src2/creaImageIOWxGimmickView.cpp b/src2/creaImageIOWxGimmickView.cpp index 4381a6e..0664caf 100644 --- a/src2/creaImageIOWxGimmickView.cpp +++ b/src2/creaImageIOWxGimmickView.cpp @@ -611,6 +611,13 @@ namespace creaImageIO GetTreeViewMap()[crea::wx2std(mNotebook->GetPageText(mNotebook->GetSelection()))]->UpdateLevel(1); } + //================================================= + void WxGimmickView::CopyFiles(const std::vector& 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) { diff --git a/src2/creaImageIOWxGimmickView.h b/src2/creaImageIOWxGimmickView.h index fd22e48..3881c85 100644 --- a/src2/creaImageIOWxGimmickView.h +++ b/src2/creaImageIOWxGimmickView.h @@ -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& filenames); ///Sends a request to read the currently selected node and the ones that surround it. diff --git a/src2/creaImageIOWxTreeView.cpp b/src2/creaImageIOWxTreeView.cpp index 0f23632..3289962 100644 --- a/src2/creaImageIOWxTreeView.cpp +++ b/src2/creaImageIOWxTreeView.cpp @@ -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& WxTreeView::GetSelected(int level) + const std::vector& WxTreeView::GetSelected(int level) { // if (GetSelectedUpToDate(level)) int l = level - 1; // the selection of upper level - std::vector& sel(mLevelList[level-1].Selected); + std::vector& 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& 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<1&&levelName.at(levelName.size()-1)!='s') + { + out<<" "<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 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& nodes, std::vector&s) + { + std::vector::const_iterator i; + + for (i=nodes.begin(); i!=nodes.end(); ++i) + { + if((*i)->GetLevel()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) { diff --git a/src2/creaImageIOWxTreeView.h b/src2/creaImageIOWxTreeView.h index c941606..9d8c34a 100644 --- a/src2/creaImageIOWxTreeView.h +++ b/src2/creaImageIOWxTreeView.h @@ -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&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& nodes, std::vector&s); + /// Gets the next nodes on the list, be it up(true) or down(false). void GetNodes(std::vector& nodes, bool direction); /// Updates the view of a level given the selected items of upper level /// Recursive method -- 2.45.1