1 #ifndef __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
2 #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
4 #include <creaImageIOTreeHandler.h>
5 #include <creaImageIOSynchron.h>
6 #include <creaImageIOImageReader.h>
7 //#include <creaImageIOImageWriter.h>
9 // Signal/slot mechanism for progress events
10 #include <boost/signal.hpp>
11 #include <boost/bind.hpp>
20 //=======================================================================
21 /// Object which can add images files to a TreeHandler. Is able to parse (recursively) a part of a filesystem to look for known images and load their attributes in order to add the images to a Tree (submission via a TreeHandler::AddBranch)
23 class TreeHandlerImageAdder
27 //====================================================================
29 TreeHandlerImageAdder(TreeHandler* tree);
31 ~TreeHandlerImageAdder();
32 /// Sets the TreeHandler
33 void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;}
35 /// Sets the synchronizer
36 void SetSynchronizer(Synchronizer* s){mSynchronizer=s;}
37 /// Sets the synchronizer
38 void SetCurrentDatabase(std::string cur){mCurrentDB=cur;}
39 //====================================================================
41 //====================================================================
42 /// Structure which holds progress information
43 /// To stop the image adder use SetStop()
47 Progress() { Reset(); }
53 mNumberScannedFiles = 0;
54 mNumberScannedDirs = 0;
55 mNumberHandledFiles = 0;
56 mNumberAddedFiles = 0;
59 int GetNumberScannedFiles() const { return mNumberScannedFiles; }
60 int GetNumberScannedDirs() const { return mNumberScannedDirs; }
61 int GetNumberHandledFiles() const { return mNumberHandledFiles; }
62 int GetNumberAddedFiles() const { return mNumberAddedFiles; }
64 void IncNumberScannedFiles() { mNumberScannedFiles++; }
65 void IncNumberScannedDirs() { mNumberScannedDirs++; }
66 void IncNumberHandledFiles() { mNumberHandledFiles++; }
67 void IncNumberAddedFiles() { mNumberAddedFiles++; }
69 void SetStop() { mStop = true; }
70 bool GetStop() const { return mStop; }
74 int mNumberScannedFiles;
75 int mNumberScannedDirs;
76 int mNumberHandledFiles;
77 int mNumberAddedFiles;
79 //=============================================
81 //=============================================
82 const Progress& GetProgress() const { return mProgress; }
83 //=============================================
85 //=============================================
86 typedef boost::signal<void (Progress&)> ProgressSignalType;
87 typedef ProgressSignalType::slot_function_type ProgressCallbackType;
88 //=============================================
90 //==================================================================
91 /// Adds the function f to the list of functions to call
92 /// when the addition progresses.
93 /// f is of type ProgressCallbackType which is:
94 /// void (*ProgressCallbackType)(Progress&)
95 /// To pass a member function 'f' of an instance 'c' of a class 'C'
96 /// as callback you have to 'bind' it, i.e. call:
97 /// ConnectProgressObserver ( boost::bind( &C::f , c, _1 ) );
98 void ConnectProgressObserver(ProgressCallbackType callback);
99 //==================================================================
101 //====================================================================
102 /// Returns if the file can be read or not
103 bool IsHandledFile( const std::string& filename);
104 /// Adds a list of files to the TreeHandler
105 void AddFiles( const std::vector<std::string>& filename );
106 /// (Recursively) adds the files of a directory to the TreeHandler
107 void AddDirectory( const std::string& directory,
109 /// Removes a file from the databases
110 void RemoveFile(tree::Node* node);
111 /// Removes files from the databases
112 void RemoveFiles(const std::vector<tree::Node*>& nodes);
113 /// Synchronizes the DB and disk by repeating the operations the user has done and returns a report
114 std::string Synchronize(bool repair, bool checkAttributes);
115 ///Recursively checks if the directory is synchronized and optionally the state of the attributes
116 void CheckSyncDirectory(const std::string &dirpath,
119 bool checkAttributes,
120 std::vector<std::string> &i_ignorefiles,
121 std::vector<std::string> & attsModified,
122 std::vector<std::string> & newfiles);
123 ///Copies the files indicated in the vector and updates all databases
124 void CopyFiles(const std::vector<std::string>& filenames, const std::string directory );
126 ///Saves as the files indicated in the vector in a specific directory
127 void SaveAs(const std::vector<std::string>& filenames, std::vector<vtkImageData *> i_images);
128 ///Finds the node that matches the specified parameters
129 void FindNode(tree::Node* parent, int level,
130 const std::string& searchParam,
131 const std::string& searchVal,
134 ///Finds the nodes that partially match the searchVal
135 void FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node);
137 ///Checks the attributes of the database against the ones in disk
138 void CheckAttributes(bool repair, std::string& file, std::vector<std::string>& attsModified);
139 ///Deletes the drive with the given name (use for all databases except maintenance and timestamp)
140 void DeleteDriveFromMainDB(const std::string& drive);
141 ///Deletes the drive with the given name (use for maintenance and timestamp databases)
142 void DeleteDriveFromOtherDB(const std::string& drive);
143 ///Edits the given field and sets the new parameters
144 void EditField(tree::Node* node, const std::string& name, const std::string& key, const std::string& val);
145 ///Returns the demanded attributes for the given file
146 void GetAttributes(const std::vector<std::string>& params,
147 const std::string& filename,
148 std::vector<std::string>& results);
153 //====================================================================
157 /// Adds a single file to the TreeHandler
158 /// **WITHOUT** testing wether it is handled or not
159 /// hence you have to call IsHandledFile before using AddFile!
160 void AddFile( const std::string& filename );
162 /// Recursive method which does the main job for AddDirectory
163 void AddDirectoryRecursor( const std::string& directory,
165 const std::string &addKey);
167 TreeHandler* mTreeHandler;
168 Synchronizer* mSynchronizer;
170 //ImageWriter mWriter;
171 std::string mCurrentDB;
174 ProgressSignalType mProgressSignal;
178 // EO class TreeHandlerImageAdder
179 //=======================================================================
185 } // EO namespace creaImageIO
188 inline std::ostream& operator << ( std::ostream& o,
189 const creaImageIO::TreeHandlerImageAdder::Progress& p)
191 o << p.GetNumberScannedFiles() << " files - "
192 << p.GetNumberScannedDirs() << " dirs - "
193 << p.GetNumberHandledFiles() << " handled -"
194 << p.GetNumberAddedFiles() << " added";