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