1 #include <creaImageIOTreeHandlerImageAdder.h>
2 #include <creaImageIOSystem.h>
3 #include "boost/filesystem.hpp"
4 #include <boost/filesystem/operations.hpp>
5 #include <boost/utility.hpp>
8 namespace fs = boost::filesystem;
9 using boost::filesystem::path;
18 //====================================================================
20 TreeHandlerImageAdder::TreeHandlerImageAdder(TreeHandler* tree)
25 TreeHandlerImageAdder::~TreeHandlerImageAdder()
28 //====================================================================
30 //====================================================================
31 void TreeHandlerImageAdder::ConnectProgressObserver(ProgressCallbackType callback)
33 mProgressSignal.connect(callback);
35 //====================================================================
37 //=====================================================================
38 bool TreeHandlerImageAdder::IsHandledFile( const std::string& filename)
40 return (mReader.CanRead(filename));
42 //=====================================================================
44 //=====================================================================
45 void TreeHandlerImageAdder::AddFiles( const std::vector<std::string>& filenames)
49 unsigned int nbf = filenames.size();
50 std::vector<std::string>::const_iterator i;
51 mSynchronizer->GetList();
52 for (i=filenames.begin();i!=filenames.end();++i)
55 mProgress.IncNumberScannedFiles();
56 if (IsHandledFile(*i))
58 mProgress.IncNumberHandledFiles();
59 if(mSynchronizer->isIndexed(*i))
61 mSynchronizer->InsertAddOp((*i),"0","1",mCurrentDB);
62 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",(*i),mCurrentDB);
63 std::stringstream removedOn;
65 mSynchronizer->InsertIgnoreFile(addKey,(*i),"0",removedOn.str(),mCurrentDB);
69 mProgressSignal(mProgress);
70 if (mProgress.GetStop()) break;
73 //=====================================================================
75 //=====================================================================
76 void TreeHandlerImageAdder::AddDirectory( const std::string& directory,
80 std::stringstream files;
82 std::stringstream rec;
84 mSynchronizer->InsertAddOp(directory,rec.str(),"0",mCurrentDB);
85 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",directory,mCurrentDB);
86 mTreeHandler->BeginTransaction();
87 mSynchronizer->GetList();
88 AddDirectoryRecursor( directory, recurse, addKey );
90 int nFiles=GetProgress().GetNumberAddedFiles();
92 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",files.str(),"ADD_KEY",addKey,mCurrentDB);
93 mTreeHandler->EndTransaction();
94 GimmickDebugMessage(3,mProgress<<std::endl);
97 //=====================================================================
98 void TreeHandlerImageAdder::AddFile( const std::string& filename )
100 GimmickDebugMessage(4,"Adding '"<<filename<<"'"<<std::endl);
101 std::map< std::string, std::string> attr;
102 mTreeHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
104 mReader.ReadAttributes(filename,attr);
106 int lev = mTreeHandler->AddBranch(attr);
108 // update the progress according to lev
109 if (lev<mTreeHandler->GetTree().GetNumberOfLevels())
110 mProgress.IncNumberAddedFiles();
112 //=====================================================================
114 void TreeHandlerImageAdder::RemoveFile( tree::Node* node)
116 int n=node->GetNumberOfChildren();
119 RemoveFiles(node->GetChildrenList());
123 std::string path=node->GetAttribute("FullFileName");
125 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path,mCurrentDB);
126 //Gets the number of files added
127 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey,mCurrentDB)).c_str());
129 std::stringstream out;
131 //Sets the new number of files
132 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",addKey,mCurrentDB);
133 //Sets the file as removed
134 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH = '"+path+"' AND ADD_KEY",addKey,mCurrentDB);
138 //=====================================================================
140 void TreeHandlerImageAdder::RemoveFiles(const std::vector<tree::Node*>& nodes)
142 std::vector<tree::Node*>::const_iterator it;
143 for(it=nodes.begin();it!=nodes.end();++it)
145 int n=(*it)->GetNumberOfChildren();
148 RemoveFiles((*it)->GetChildrenList());
152 std::string path=(*it)->GetAttribute("FullFileName");
154 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path,mCurrentDB);
155 //Gets the number of files added
156 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey,mCurrentDB)).c_str());
158 std::stringstream out;
160 //Sets the new number of files
161 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",addKey,mCurrentDB);
162 //Sets the file as removed
163 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH = '"+path+"' AND ADD_KEY",addKey,mCurrentDB);
170 //=====================================================================
171 void TreeHandlerImageAdder::AddDirectoryRecursor(const std::string &dirpath,
173 const std::string &addKey)
175 GimmickDebugMessage(4,"Scanning '"<<dirpath<<"'"<<std::endl);
176 mProgress.IncNumberScannedDirs();
178 if ( !fs::exists( dirpath ) ) return;
179 time_t lastModif=fs::last_write_time(dirpath);
182 fs::directory_iterator end_itr; // default construction yields past-the-end
183 for ( fs::directory_iterator itr( dirpath );
187 // If is directory & recurse : do recurse
188 if ( fs::is_directory(itr->status()) )
192 AddDirectoryRecursor( itr->string(), recursive, addKey);
197 std::string parent_id;
198 // tTest if directory (and only it) exists or not.
199 bool valid = mSynchronizer->isIndexed(itr->string());//true;//=mTimestampHandler->AddDirectory(dirpath, itr->string(), lastModif, time(0),mCurrentDB);
202 mProgress.IncNumberScannedFiles();
203 if (IsHandledFile(itr->string()))
205 mProgress.IncNumberHandledFiles();
206 AddFile( itr->string() );
207 mTreeHandler->GetTopLevelNodeId("FullFileName",itr->string(),parent_id);
208 std::stringstream removedOn;
210 mSynchronizer->InsertIgnoreFile(addKey, itr->string(),"0",removedOn.str(),mCurrentDB);
213 mProgressSignal(mProgress);
214 if (mProgress.GetStop())
225 //=======================================================================
227 //=======================================================================
229 void TreeHandlerImageAdder::CheckSyncDirectory(const std::string &dirpath,
232 bool checkAttributes,
233 std::vector<std::string> &i_ignorefiles,
234 std::vector<std::string> & attsModified,
235 std::vector<std::string> & newfiles)
237 if ( !fs::exists( dirpath ) ) return;
238 fs::directory_iterator end_itr; // default construction yields past-the-end
240 for ( fs::directory_iterator itr( dirpath ); itr != end_itr; ++itr )
242 // If is directory & recurse : do recurse
243 if ( fs::is_directory(itr->status()) )
247 CheckSyncDirectory( itr->string(), recursive, repair, checkAttributes, i_ignorefiles, attsModified, newfiles);
252 if (IsHandledFile(itr->string()))
255 for(std::vector<std::string>::iterator it_new = i_ignorefiles.begin(); it_new < i_ignorefiles.end(); ++it_new)
257 if((*it_new) == itr->string())
260 //Additional checking of attributes
263 CheckAttributes(repair,(*it_new),attsModified);
265 i_ignorefiles.erase(it_new);
269 if(!bfound && i_ignorefiles.size()>0 )
271 newfiles.push_back( itr->string() );
278 //=======================================================================
280 //=======================================================================
282 std::string TreeHandlerImageAdder::Synchronize(bool repair, bool checkAttributes)
284 std::vector<AddList> fileList;
285 std::vector<std::string> ignoreList;
286 std::vector<std::string> newFiles;
287 std::vector<std::string> attsModified;
288 std::stringstream mess;
289 std::vector<AddList>::iterator iter;
291 //Gets the list of added files
292 mSynchronizer->GetFileList(fileList,mCurrentDB);
294 std::vector<std::string>::iterator i;
295 //Actions to take if the user doesn't want to repair
298 //Iterates to see if they are in sync
299 for(iter=fileList.begin();iter!=fileList.end();++iter)
301 mSynchronizer->GetIgnoredFiles((*iter).key,ignoreList);
303 if((*iter).recursive=="0"){rec=false;}
304 CheckSyncDirectory((*iter).path,rec,repair,checkAttributes,ignoreList,attsModified,newFiles);
307 //Add to message the result of new found files
308 mess<<"New Files Found: "<<newFiles.size()<<std::endl;
309 if(newFiles.size()>0)
311 mess<<"Filenames: "<<std::endl;
312 for(i=newFiles.begin();i!=newFiles.end();++i)
318 //Add to message the result of missing files
319 mess<<"Missing Files: "<<ignoreList.size()<<std::endl;
320 if(ignoreList.size()>0)
322 mess<<"Filenames: "<<std::endl;
323 for(i=ignoreList.begin();i!=ignoreList.end();++i)
329 //In the case that the user wants to check the attributes...
332 //... add to message the result of files that have been changed.
333 mess<<"Files with different attributes: "<<attsModified.size()<<std::endl;
334 if(attsModified.size()>0)
336 mess<<"Filenames: "<<std::endl;
337 for(i=attsModified.begin();i!=attsModified.end();++i)
346 //Actions to take if the user wants to repair
350 //Iterates to see if they are in sync
351 for(iter=fileList.begin();iter!=fileList.end();++iter)
353 mSynchronizer->GetIgnoredFiles((*iter).key,ignoreList);
355 if((*iter).recursive=="0"){rec=false;}
356 CheckSyncDirectory((*iter).path,rec,repair,checkAttributes,ignoreList,attsModified,newFiles);
358 //For the new files, add them
359 for (i=newFiles.begin();i!=newFiles.end();++i)
361 if (IsHandledFile(*i))
363 std::stringstream removedOn;
365 mSynchronizer->InsertIgnoreFile((*iter).key,(*i),"0",removedOn.str(),mCurrentDB);
366 //Gets the number of files added
367 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",(*iter).key,mCurrentDB)).c_str());
369 std::stringstream out;
371 //Sets the new number of files
372 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",(*iter).key,mCurrentDB);
380 //Reports number of added files
381 mess<<"Files Added: "<<nf<<std::endl;
383 //Removes the necessary files and reports the results
384 if(ignoreList.size()>0)
387 mTreeHandler->LoadChildren(NULL,4);
388 for(i=ignoreList.begin();i!=ignoreList.end();++i)
390 FindNode(mTreeHandler->GetTree().GetChildrenList()[0],3,"FullFileName",*i,node);
392 mTreeHandler->Remove(node);
395 mess<<"Files Removed: "<<ignoreList.size()<<std::endl;
396 //In the case that the user wants to check the attributes...
399 //... add to message the result of files that were modified.
400 mess<<"Files Modified: "<<attsModified.size()<<std::endl;
406 //=======================================================================
408 void TreeHandlerImageAdder::CheckAttributes(bool repair, std::string& file, std::vector<std::string>& attsModified)
410 std::map< std::string, std::string> attr;
411 mTreeHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
412 mReader.ReadAttributes(file,attr);
413 tree::LevelDescriptor::AttributeDescriptorListType adl= mTreeHandler->GetTree().GetAttributeDescriptorList(mTreeHandler->GetTree().GetNumberOfLevels()-1);
414 tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
415 for (a = adl.begin();a!=adl.end();++a)
417 std::string databaseVal;
418 mTreeHandler->GetAttribute("Image","FullFileName",file,a->GetKey(),databaseVal);
419 std::string fileVal=attr.find(a->GetKey())->second;
420 if ( a->GetFlags()==0 && databaseVal == fileVal)
424 mTreeHandler->SetAttribute("Image",a->GetKey(),fileVal,"FullFileName", file);
426 attsModified.push_back(file);
433 //=======================================================================
436 void TreeHandlerImageAdder::FindNode(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node)
440 std::vector<tree::Node*>::iterator iter;
441 for(iter=parent->GetChildrenList().begin();iter!=parent->GetChildrenList().end();++iter)
443 FindNode(*iter,level-1,searchParam,searchVal,node);
448 if(parent->GetAttribute(searchParam).compare(searchVal)==0)
456 void TreeHandlerImageAdder::SaveAs(const std::vector<std::string>& filenames, std::vector<vtkImageData *> i_images)
458 std::vector<std::string>::const_iterator it_file = filenames.begin();
459 std::vector<vtkImageData *>::iterator it_image = i_images.begin();
460 /* mWriter.CanWrite(".jpeg");
461 for(; it_file != filenames.end(); ++it_file, ++it_image)
462 mWriter.WriteImage(it_file->c_str(), (vtkImageData &)it_image);*/
465 //=======================================================================
466 void TreeHandlerImageAdder::FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node)
470 std::vector<tree::Node*>::iterator iter;
471 for(iter=parent->GetChildrenList().begin();iter!=parent->GetChildrenList().end() && node==0 ;++iter)
473 FindNodePartial(*iter,level-1,searchParam,searchVal,node);
478 if(parent->GetAttribute(searchParam).find(searchVal)<9000)
487 //=======================================================================
489 void TreeHandlerImageAdder::CopyFiles(const std::vector<std::string>& filenames, const std::string directory )
491 std::vector<std::string>::const_iterator i;
492 if(!boost::filesystem::exists(directory))
494 boost::filesystem::create_directory(boost::filesystem::path(directory));
495 mSynchronizer->InsertAddOp(directory,"0","0",mCurrentDB);
497 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",directory,mCurrentDB);
499 std::vector<std::string> newNames;
500 for(i=filenames.begin();i!=filenames.end();++i)
502 std::string dir=directory.c_str();
503 if(boost::filesystem::exists(*i) && (*i).find(dir)==std::string::npos)
506 last=(*i).find_last_of('/');
507 std::string f="\\"+(*i).substr(last+1);
510 std::stringstream out;
512 while(boost::filesystem::exists(out.str()))
515 out<<directory<<f.substr(0,f.size()-4)<<"("<<p<<")"<<f.substr(f.size()-4);
518 std::string result=out.str();
519 boost::filesystem::copy_file((*i),result);
521 //To update image database
522 mTreeHandler->SetAttribute("Image","FullFileName",result,"FullFileName", (*i));
524 //To update maintenance database
525 //1.Add the new path and increase number of children on new operation.
526 std::stringstream removedOn;
529 mSynchronizer->InsertIgnoreFile(addKey, result,"0",removedOn.str(),mCurrentDB);
530 //Gets the number of files added
531 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey,mCurrentDB)).c_str());
533 std::stringstream fil;
535 //Sets the new number of files
536 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",fil.str(),"ADD_KEY",addKey,mCurrentDB);
539 //2.Set the old path as removed and decrease number of children on old operation.
540 //Gets the old add key
541 std::string oldAddKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path,mCurrentDB);
542 //Sets the file as removed
543 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH = '"+path+"' AND ADD_KEY",oldAddKey,mCurrentDB);
544 //Gets the number of files added
545 files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",oldAddKey,mCurrentDB)).c_str());
548 //Sets the new number of files
549 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",fil.str(),"ADD_KEY",oldAddKey,mCurrentDB);
556 //=======================================================================
558 void TreeHandlerImageAdder::DeleteDriveFromMainDB(const std::string& drive)
560 //Delete from local database and others
562 mTreeHandler->LoadChildren(NULL,4);
563 FindNodePartial(mTreeHandler->GetTree().GetChildrenList()[0],3,"FullFileName",drive,node);
566 mTreeHandler->Remove(node);
568 mTreeHandler->LoadChildren(NULL,4);
569 FindNodePartial(mTreeHandler->GetTree().GetChildrenList()[0],3,"FullFileName",drive,node);
573 //=======================================================================
575 void TreeHandlerImageAdder::DeleteDriveFromOtherDB(const std::string& drive)
577 //Delete from maintenance
578 mSynchronizer->RemoveEntries("ADD_OPS", "PATH", "LIKE", drive+"%");
579 mSynchronizer->RemoveEntries("IGNORED_FILES", "PATH", "LIKE", drive+"%");
582 //=======================================================================
583 void TreeHandlerImageAdder::EditField(tree::Node* node, const std::string& name, const std::string& key, const std::string& val)
585 node->SetAttribute(key,val);
586 mTreeHandler->SetAttribute(node,key,val);
589 //=======================================================================
590 void TreeHandlerImageAdder::GetAttributes(const std::vector<std::string>& params,
591 const std::string& filename,
592 std::vector<std::string>& results)
594 std::vector<std::string>::const_iterator i;
596 for(i=params.begin();i!=params.end();i++)
598 mTreeHandler->GetAttribute("Image","FullFileName",filename,*i,result);
599 results.push_back(result);