]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeHandlerImageAdder.h
a9666eac07d6e93b94a78e8b61fc022cd76d0d53
[creaImageIO.git] / src2 / creaImageIOTreeHandlerImageAdder.h
1 #ifndef __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
2 #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
3
4 #include <creaImageIOTreeHandler.h>
5 #include <creaImageIOTimestampDatabaseHandler.h>
6 #include <creaImageIOSynchron.h>
7 #include <creaImageIOImageReader.h>
8 //#include <creaImageIOImageWriter.h>
9
10 // Signal/slot mechanism for progress events
11 #include <boost/signal.hpp>
12 #include <boost/bind.hpp>
13
14
15 namespace creaImageIO
16 {
17         /**
18         * \ingroup Model
19         */
20
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)
23
24   class TreeHandlerImageAdder
25   {
26           
27   public:
28     //====================================================================
29     /// Ctor
30     TreeHandlerImageAdder(TreeHandler* tree);
31     /// Dtor
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     //====================================================================
42
43     //====================================================================
44     /// Structure which holds progress information
45     /// To stop the image adder use SetStop()
46     class Progress
47     {
48     public:
49       Progress() { Reset(); }
50       ~Progress() {}
51       
52       void Reset() 
53       {
54         mStop = false;
55         mNumberScannedFiles = 0;
56         mNumberScannedDirs = 0;
57         mNumberHandledFiles = 0;
58         mNumberAddedFiles = 0;
59       }
60
61       int GetNumberScannedFiles() const { return mNumberScannedFiles; }
62       int GetNumberScannedDirs() const { return mNumberScannedDirs; }
63       int GetNumberHandledFiles() const { return mNumberHandledFiles; }
64       int GetNumberAddedFiles() const { return mNumberAddedFiles; }
65
66       void IncNumberScannedFiles() { mNumberScannedFiles++; }
67       void IncNumberScannedDirs() { mNumberScannedDirs++; }
68       void IncNumberHandledFiles() { mNumberHandledFiles++; }
69       void IncNumberAddedFiles() { mNumberAddedFiles++; }
70
71       void SetStop() { mStop = true; }
72       bool GetStop() const { return mStop; }
73
74     private:
75       bool mStop;
76       int mNumberScannedFiles;
77       int mNumberScannedDirs;
78       int mNumberHandledFiles;
79       int mNumberAddedFiles;
80     };
81     //=============================================
82
83     //=============================================
84     const Progress& GetProgress() const { return mProgress; }
85     //=============================================
86
87     //=============================================
88     typedef boost::signal<void (Progress&)>  ProgressSignalType;
89     typedef ProgressSignalType::slot_function_type ProgressCallbackType;
90     //=============================================
91
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    //==================================================================
102
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, 
110                        bool recurse);
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, 
119                                                         bool recursive, 
120                                                         bool repair,
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  );
127         
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, 
134                 tree::Node*& node);
135
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);
138  
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);
151   
152   
153   
154  
155     //====================================================================
156
157   private:
158
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 );
163
164     /// Recursive method which does the main job for AddDirectory
165     void AddDirectoryRecursor( const std::string& directory, 
166                                bool recurse,
167                            const std::string &addKey);
168
169     TreeHandler* mTreeHandler;
170         TimestampDatabaseHandler* mTimestampHandler;
171         Synchronizer* mSynchronizer;
172     ImageReader mReader;
173         //ImageWriter mWriter;
174         std::string mCurrentDB;
175     
176     Progress mProgress;
177     ProgressSignalType mProgressSignal;
178
179
180   };
181   // EO class TreeHandlerImageAdder
182   //=======================================================================
183
184
185  
186
187
188 } // EO namespace creaImageIO
189
190 #include <iostream>
191 inline std::ostream& operator << ( std::ostream& o, 
192                             const creaImageIO::TreeHandlerImageAdder::Progress& p)
193 {
194   o << p.GetNumberScannedFiles() << " files - "
195     << p.GetNumberScannedDirs() << " dirs - "
196     << p.GetNumberHandledFiles() << " handled -"
197     << p.GetNumberAddedFiles() << " added";
198   return o;
199 }
200
201 // EOF
202 #endif  
203