]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeHandlerImageAdder.h
fe3c31da644ebe6f28219e1810e23df13c376915
[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 <wx/wx.h>
9 #include <wx/progdlg.h>
10 #include <creaWx.h>
11 // Signal/slot mechanism for progress events
12 #include <boost/signal.hpp>
13 #include <boost/bind.hpp>
14
15
16 namespace creaImageIO
17 {
18         /**
19         * \ingroup Model
20         */
21
22   //=======================================================================
23   /// 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
25   class TreeHandlerImageAdder
26   {
27           
28   public:
29     //====================================================================
30     /// Ctor
31     TreeHandlerImageAdder(TreeHandler* tree);
32     /// Dtor
33     ~TreeHandlerImageAdder();
34     /// Sets the TreeHandler
35     void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;}
36         /// Sets the TimestampDatabaseHandler
37     void SetTimestampHandler(TimestampDatabaseHandler* tdh) { mTimestampHandler = tdh;}
38         /// Sets the synchronizer
39         void SetSynchronizer(Synchronizer* s){mSynchronizer=s;}
40         /// Sets the synchronizer
41         void SetCurrentDatabase(std::string cur){mCurrentDB=cur;}
42     //====================================================================
43
44     //====================================================================
45     /// Structure which holds progress information
46     /// To stop the image adder use SetStop()
47     class Progress
48     {
49     public:
50       Progress() { Reset(); }
51       ~Progress() {}
52       
53       void Reset() 
54       {
55         mStop = false;
56         mNumberScannedFiles = 0;
57         mNumberScannedDirs = 0;
58         mNumberHandledFiles = 0;
59         mNumberAddedFiles = 0;
60       }
61
62       int GetNumberScannedFiles() const { return mNumberScannedFiles; }
63       int GetNumberScannedDirs() const { return mNumberScannedDirs; }
64       int GetNumberHandledFiles() const { return mNumberHandledFiles; }
65       int GetNumberAddedFiles() const { return mNumberAddedFiles; }
66
67       void IncNumberScannedFiles() { mNumberScannedFiles++; }
68       void IncNumberScannedDirs() { mNumberScannedDirs++; }
69       void IncNumberHandledFiles() { mNumberHandledFiles++; }
70       void IncNumberAddedFiles() { mNumberAddedFiles++; }
71
72       void SetStop() { mStop = true; }
73       bool GetStop() const { return mStop; }
74
75     private:
76       bool mStop;
77       int mNumberScannedFiles;
78       int mNumberScannedDirs;
79       int mNumberHandledFiles;
80       int mNumberAddedFiles;
81     };
82     //=============================================
83
84     //=============================================
85     const Progress& GetProgress() const { return mProgress; }
86     //=============================================
87
88     //=============================================
89     typedef boost::signal<void (Progress&)>  ProgressSignalType;
90     typedef ProgressSignalType::slot_function_type ProgressCallbackType;
91     //=============================================
92
93    //==================================================================
94     /// Adds the function f to the list of functions to call 
95     /// when the addition progresses.
96     /// f is of type ProgressCallbackType which is:
97     /// void (*ProgressCallbackType)(Progress&)
98     /// To pass a member function 'f' of an instance 'c' of a class 'C' 
99     /// as callback you have to 'bind' it, i.e. call:
100     /// ConnectProgressObserver ( boost::bind( &C::f , c, _1 ) );
101     void ConnectProgressObserver(ProgressCallbackType callback);
102    //==================================================================
103
104     //====================================================================
105     /// Returns if the file can be read or not
106     bool IsHandledFile( const std::string& filename);
107     /// Adds a list of files to the TreeHandler 
108     void AddFiles( const std::vector<std::string>& filename );
109     /// (Recursively) adds the files of a directory to the TreeHandler 
110     void AddDirectory( const std::string& directory, 
111                        bool recurse);
112         /// Removes a file from the databases
113         void RemoveFile(tree::Node* node);
114         /// Removes files from the databases
115         void RemoveFiles(const std::vector<tree::Node*>& nodes);
116         /// Synchronizes the DB and disk by repeating the operations the user has done and returns a report
117         std::string Synchronize(bool repair, bool checkAttributes);
118         ///Recursively checks if the directory is synchronized and optionally the state of the attributes
119         void CheckSyncDirectory(const std::string &dirpath, 
120                                                         bool recursive, 
121                                                         bool repair,
122                                                         bool checkAttributes,
123                                                         std::vector<std::string> &i_ignorefiles,
124                                                         std::vector<std::string> & attsModified,
125                                                         std::vector<std::string> & newfiles);
126         ///Copies the files indicated in the vector and updates all databases
127         void CopyFiles(const std::vector<std::string>& filenames, const std::string directory  );
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         ///Checks the attributes of the database against the ones in disk
134         void CheckAttributes(bool repair, std::string& file, std::vector<std::string>& attsModified);
135  
136     //====================================================================
137
138   private:
139
140     /// Adds a single file to the TreeHandler 
141     /// **WITHOUT** testing wether it is handled or not 
142     /// hence you have to call IsHandledFile before using AddFile!
143     void AddFile( const std::string& filename );
144
145     /// Recursive method which does the main job for AddDirectory
146     void AddDirectoryRecursor( const std::string& directory, 
147                                bool recurse,
148                            const std::string &addKey);
149
150     TreeHandler* mTreeHandler;
151         TimestampDatabaseHandler* mTimestampHandler;
152         Synchronizer* mSynchronizer;
153     ImageReader mReader;
154         std::string mCurrentDB;
155     
156     Progress mProgress;
157     ProgressSignalType mProgressSignal;
158
159
160   };
161   // EO class TreeHandlerImageAdder
162   //=======================================================================
163
164
165  
166
167
168 } // EO namespace creaImageIO
169
170 #include <iostream>
171 inline std::ostream& operator << ( std::ostream& o, 
172                             const creaImageIO::TreeHandlerImageAdder::Progress& p)
173 {
174   o << p.GetNumberScannedFiles() << " files - "
175     << p.GetNumberScannedDirs() << " dirs - "
176     << p.GetNumberHandledFiles() << " handled -"
177     << p.GetNumberAddedFiles() << " added";
178   return o;
179 }
180
181 // EOF
182 #endif  
183