]> Creatis software - creaImageIO.git/blob - src2/creaImageIOImageFinder.cpp
8a28144041dfb25f649d84d2fc39c5545f625f21
[creaImageIO.git] / src2 / creaImageIOImageFinder.cpp
1 #include <creaImageIOImageFinder.h>
2 #include <creaWx.h>
3 #include <wx/dir.h>
4 #include <wx/filename.h>
5
6 using namespace crea;
7
8 namespace creaImageIO
9 {
10   //====================================================================
11   // Ctor
12   ImageFinder::ImageFinder(TreeHandler* tree)
13     : mTreeHandler(tree)
14   {
15   }
16   // Dtor
17   ImageFinder::~ImageFinder()
18   {
19   }
20   //====================================================================
21
22   //=====================================================================
23   bool ImageFinder::IsHandledFile( const std::string& filename)
24   {
25     return (mReader.CanRead(filename,""));
26   }
27   //=====================================================================
28
29   //=====================================================================
30   bool ImageFinder::AddFiles( const std::vector<std::string>& filenames,
31                               wxProgressDialog* progress, 
32                               UpdateSummary& summary)
33   {
34     for (int swi=0;swi<10;swi++) 
35       {
36         msw[swi].Start(0);
37         msw[swi].Pause();
38       }
39     
40     // Parse directory
41     wxStopWatch sw; 
42
43
44     summary.added_images = 0;
45     unsigned int nbf = filenames.size(); 
46     std::vector<std::string>::const_iterator i;
47     for (i=filenames.begin();i!=filenames.end();++i)
48       {
49         summary.scanned_files++;
50         if (IsHandledFile(*i)) 
51           {
52             summary.handled_images++;
53             AddFile(*i,summary);
54              
55             if (progress)
56               {
57                 std::string mess("Adding ");
58                 mess += *i;
59                 if (!progress->Update( (int)(summary.added_images*999./nbf),
60                                        std2wx(mess)))
61                   {
62                     // Some file was added hence we must return true !
63                     summary.cancelled_by_user = true;
64                     break;
65                   }
66               }
67           }
68       }
69
70
71     sw.Pause();
72     msw[0].Pause();
73     msw[1].Pause();
74     msw[2].Pause();
75
76     summary.total_time = sw.Time();
77     summary.file_scan_time = msw[1].Time();
78     summary.update_database_time = msw[2].Time();
79     summary.update_structs_time = 
80       summary.total_time - 
81       summary.parse_time - 
82       summary.file_scan_time - 
83       summary.update_database_time;
84      
85     return true;
86   }
87   //=====================================================================
88
89   //=====================================================================
90   bool ImageFinder::AddFile( const std::string& filename,
91                              UpdateSummary& summary)
92   {
93     
94     std::map< std::string, std::string>  attr;
95     mTreeHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
96
97     msw[1].Resume();
98     mReader.ReadAttributes(filename,attr);
99     //     mReader.ReadDicomInfo(filename,image);
100     msw[1].Pause();
101
102     //     image->SetFieldValue("FullFileName",filename);
103      
104     int lev = mTreeHandler->AddBranch(attr);
105
106     // TO DO : update the summary according to lev
107
108     return true;
109   }
110   //=====================================================================
111
112   //=====================================================================
113   /**
114    * \brief   Explore a directory with possibility of recursion
115    *          return number of files read
116    * @param  dirpath   directory to explore
117    * @param  recursive whether we want recursion or not
118    */
119   void ImageFinder::ParseDirectory( const std::string &dirpath, 
120                                     std::vector<std::string> &Filenames,
121                                     bool recursive,
122                                     wxProgressDialog* progress, 
123                                     UpdateSummary& summary)
124     
125   {
126     if (progress) 
127       {
128         std::string mess("Parsing ");
129         mess += dirpath;
130         progress->Pulse(std2wx(mess));
131       }
132
133     wxStopWatch sw; 
134     sw.Start(0);
135     
136     std::string fileName;
137     std::string dirName = dirpath;
138
139     summary.scanned_dirs++;
140
141     wxDir dir( std2wx(dirpath) );
142
143     if ( !dir.IsOpened() )
144       {
145         // deal with the error here - wxDir would already log an error message
146         // explaining the exact reason of the failure
147         return;
148       }
149
150     wxString filename;
151
152     bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_FILES | wxDIR_HIDDEN );
153     while ( cont )
154       {
155         if ((progress)&&( sw.Time() >= 250 )) 
156           {
157             //                  std::cout << "PULSE"<<std::endl;
158             sw.Start(0);
159             if (!progress->Pulse()) 
160               {
161                 summary.cancelled_by_user = true;
162                 break;
163               }
164           }
165       
166         summary.scanned_files++;
167         wxFileName wxffn(dir.GetName(),filename);
168         std::string ffn = wx2std(wxffn.GetFullPath());
169         //              std::cout << ffn << std::endl;
170         if (mReader.CanRead(ffn,""))
171           {
172             Filenames.push_back( ffn );
173             summary.handled_images++;
174           }
175         cont = dir.GetNext(&filename);
176       }
177     
178     // Recurse into subdirs
179     if ( recursive )
180       {
181         cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN );
182         while ( cont )
183           {
184             
185             wxFileName wxffn(dir.GetName(),filename);
186             std::string ffn = wx2std(wxffn.GetFullPath());
187             
188             //                          std::cout << "dir="<< ffn<< std::endl;
189             
190             ParseDirectory( ffn, 
191                             Filenames,
192                             recursive,
193                             progress,
194                             summary);
195             if (summary.cancelled_by_user) break;
196             
197             cont = dir.GetNext(&filename);
198           }
199       }
200     
201   }
202   //=======================================================================
203
204
205   //=====================================================================
206   bool ImageFinder::AddDirectory( const std::string& directory,
207                                   bool recurse,
208                                   wxProgressDialog* progress, 
209                                   UpdateSummary& summary
210                                   )
211   {
212     //    std::cout << "** ImageFinder::AddDirectory"
213     //        << " '"<<directory<<"'"<<std::endl;
214     //    std::cout << "------ Parsing directory ------"<<std::endl;
215     
216     if (progress)
217       {
218         progress->Pulse();
219       }
220
221     for (int swi=0;swi<10;swi++) 
222       {
223         msw[swi].Start(0);
224         msw[swi].Pause();
225       }
226     
227     // Parse directory
228     wxStopWatch sw; 
229     
230     bool was_canceled_by_user(false);
231     std::vector<std::string> filenames;
232     ParseDirectory( directory, 
233                     filenames,
234                     recurse, 
235                     progress,
236                     summary);
237     
238     if ( summary.cancelled_by_user ) 
239       {
240         return false;
241       }
242     
243     summary.parse_time = sw.Time();
244
245
246     summary.added_images = 0;
247     unsigned int nbf = filenames.size(); // , nf = 0;
248     std::vector<std::string>::iterator i;
249     for (i=filenames.begin();i!=filenames.end();++i)
250       {
251         AddFile(*i,summary);
252
253         if (progress)
254           {
255             std::string mess("Adding ");
256             mess += *i;
257             if (!progress->Update( (int)(summary.added_images*999./nbf),
258                                    std2wx(mess)))
259               {
260                 // Some file was added hence we must return true !
261                 summary.cancelled_by_user = true;
262                 break;
263               }
264           }
265       }
266
267
268     sw.Pause();
269     msw[0].Pause();
270     msw[1].Pause();
271     msw[2].Pause();
272
273     summary.total_time = sw.Time();
274     summary.file_scan_time = msw[1].Time();
275     summary.update_database_time = msw[2].Time();
276     summary.update_structs_time = 
277       summary.total_time - 
278       summary.parse_time - 
279       summary.file_scan_time - 
280       summary.update_database_time;
281      
282     return true;
283   }
284   //=====================================================================
285
286
287
288 }