1 #ifndef __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
2 #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
4 #include <creaImageIOTreeHandler.h>
5 #include <creaImageIOTimestampDatabaseHandler.h>
6 #include <creaImageIOSynchron.h>
7 #include <creaImageIOImageReader.h>
8 //#include <creaImageIOImageWriter.h>
10 // Signal/slot mechanism for progress events
11 #include <boost/signal.hpp>
12 #include <boost/bind.hpp>
21 //=======================================================================
22 /// 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)
24 class TreeHandlerImageAdder
28 //====================================================================
30 TreeHandlerImageAdder(TreeHandler* tree);
32 ~TreeHandlerImageAdder();
33 /// Sets the TreeHandler
34 void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;}
35 /// Sets the TimestampDatabaseHandler
36 void SetTimestampHandler(TimestampDatabaseHandler* tdh) { mTimestampHandler = tdh;}
37 /// Sets the synchronizer
38 void SetSynchronizer(Synchronizer* s){mSynchronizer=s;}
39 /// Sets the synchronizer
40 void SetCurrentDatabase(std::string cur){mCurrentDB=cur;}
41 //====================================================================
43 //====================================================================
44 /// Structure which holds progress information
45 /// To stop the image adder use SetStop()
49 Progress() { Reset(); }
55 mNumberScannedFiles = 0;
56 mNumberScannedDirs = 0;
57 mNumberHandledFiles = 0;
58 mNumberAddedFiles = 0;
61 int GetNumberScannedFiles() const { return mNumberScannedFiles; }
62 int GetNumberScannedDirs() const { return mNumberScannedDirs; }
63 int GetNumberHandledFiles() const { return mNumberHandledFiles; }
64 int GetNumberAddedFiles() const { return mNumberAddedFiles; }
66 void IncNumberScannedFiles() { mNumberScannedFiles++; }
67 void IncNumberScannedDirs() { mNumberScannedDirs++; }
68 void IncNumberHandledFiles() { mNumberHandledFiles++; }
69 void IncNumberAddedFiles() { mNumberAddedFiles++; }
71 void SetStop() { mStop = true; }
72 bool GetStop() const { return mStop; }
76 int mNumberScannedFiles;
77 int mNumberScannedDirs;
78 int mNumberHandledFiles;
79 int mNumberAddedFiles;
81 //=============================================
83 //=============================================
84 const Progress& GetProgress() const { return mProgress; }
85 //=============================================
87 //=============================================
88 typedef boost::signal<void (Progress&)> ProgressSignalType;
89 typedef ProgressSignalType::slot_function_type ProgressCallbackType;
90 //=============================================
92 //==================================================================
93 /// Adds the function f to the list of functions to call
94 /// when the addition progresses.
95 /// f is of type ProgressCallbackType which is:
96 /// void (*ProgressCallbackType)(Progress&)
97 /// To pass a member function 'f' of an instance 'c' of a class 'C'
98 /// as callback you have to 'bind' it, i.e. call:
99 /// ConnectProgressObserver ( boost::bind( &C::f , c, _1 ) );
100 void ConnectProgressObserver(ProgressCallbackType callback);
101 //==================================================================
103 //====================================================================
104 /// Returns if the file can be read or not
105 bool IsHandledFile( const std::string& filename);
106 /// Adds a list of files to the TreeHandler
107 void AddFiles( const std::vector<std::string>& filename );
108 /// (Recursively) adds the files of a directory to the TreeHandler
109 void AddDirectory( const std::string& directory,
111 /// Removes a file from the databases
112 void RemoveFile(tree::Node* node);
113 /// Removes files from the databases
114 void RemoveFiles(const std::vector<tree::Node*>& nodes);
115 /// Synchronizes the DB and disk by repeating the operations the user has done and returns a report
116 std::string Synchronize(bool repair, bool checkAttributes);
117 ///Recursively checks if the directory is synchronized and optionally the state of the attributes
118 void CheckSyncDirectory(const std::string &dirpath,
121 bool checkAttributes,
122 std::vector<std::string> &i_ignorefiles,
123 std::vector<std::string> & attsModified,
124 std::vector<std::string> & newfiles);
125 ///Copies the files indicated in the vector and updates all databases
126 void CopyFiles(const std::vector<std::string>& filenames, const std::string directory );
128 ///Saves as the files indicated in the vector in a specific directory
129 void SaveAs(const std::vector<std::string>& filenames, std::vector<vtkImageData *> i_images);
130 ///Finds the node that matches the specified parameters
131 void FindNode(tree::Node* parent, int level,
132 const std::string& searchParam,
133 const std::string& searchVal,
136 ///Finds the nodes that partially match the searchVal
137 void FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node);
139 ///Checks the attributes of the database against the ones in disk
140 void CheckAttributes(bool repair, std::string& file, std::vector<std::string>& attsModified);
141 ///Deletes the drive with the given name (use for all databases except maintenance and timestamp)
142 void DeleteDriveFromMainDB(const std::string& drive);
143 ///Deletes the drive with the given name (use for maintenance and timestamp databases)
144 void DeleteDriveFromOtherDB(const std::string& drive);
145 ///Edits the given field and sets the new parameters
146 void EditField(tree::Node* node, const std::string& name, const std::string& key, const std::string& val);
147 ///Returns the demanded attributes for the given file
148 void GetAttributes(const std::vector<std::string>& params,
149 const std::string& filename,
150 std::vector<std::string>& results);
155 //====================================================================
159 /// Adds a single file to the TreeHandler
160 /// **WITHOUT** testing wether it is handled or not
161 /// hence you have to call IsHandledFile before using AddFile!
162 void AddFile( const std::string& filename );
164 /// Recursive method which does the main job for AddDirectory
165 void AddDirectoryRecursor( const std::string& directory,
167 const std::string &addKey);
169 TreeHandler* mTreeHandler;
170 TimestampDatabaseHandler* mTimestampHandler;
171 Synchronizer* mSynchronizer;
173 //ImageWriter mWriter;
174 std::string mCurrentDB;
177 ProgressSignalType mProgressSignal;
181 // EO class TreeHandlerImageAdder
182 //=======================================================================
188 } // EO namespace creaImageIO
191 inline std::ostream& operator << ( std::ostream& o,
192 const creaImageIO::TreeHandlerImageAdder::Progress& p)
194 o << p.GetNumberScannedFiles() << " files - "
195 << p.GetNumberScannedDirs() << " dirs - "
196 << p.GetNumberHandledFiles() << " handled -"
197 << p.GetNumberAddedFiles() << " added";