]> Creatis software - creaImageIO.git/blob - src2/creaImageIOGimmick.cpp
*** empty log message ***
[creaImageIO.git] / src2 / creaImageIOGimmick.cpp
1 #include <creaImageIOGimmick.h>
2
3 #include <creaImageIOSystem.h>
4
5 #include <boost/filesystem.hpp>
6 #include <boost/algorithm/string.hpp>
7
8 namespace creaImageIO
9 {
10
11
12   //==============================================================
13   Gimmick::Gimmick()
14     : mImageAdder(0)
15   {    
16     RegisterGimmickMessageTypes();
17   }
18   //==============================================================
19
20
21
22   //==============================================================
23   Gimmick::~Gimmick()
24   {
25     
26   }
27   //==============================================================
28   
29
30   //==============================================================
31   void Gimmick::Initialize()
32   {
33           std::string i_nameDB = "Local database";
34     // Create the UserSettings dir if does not exist
35     CreateUserSettingsDirectory();
36     // Sets the current directory to the home dir
37     mCurrentDirectory =  GetHomeDirectory();
38         mSynchronizer=0;
39
40         std::string dbpath = GetLocalDatabasePath();
41     // Create or open local database
42         mLocalDatabase = createDB(i_nameDB, mCurrentDirectory + "\\.gimmick\\localdatabase_Descriptor.txt", dbpath);
43     // Add it to the TreeHandlerMap
44     mTreeHandlerMap[i_nameDB] = mLocalDatabase;
45     
46         // Creates files and directories database
47     mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
48     // Create or open local database
49     if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
50       {
51         std::string mess = "Timestamp database '";
52         mess += GetTimestampDatabasePath();
53         mess += "' does not exist : creating it";
54         GimmickMessage(1,mess<<std::endl);
55         
56         if ( ! mTimestampDatabase->Create() )
57           {
58             GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
59           }
60         
61      }
62     else 
63       {
64         /// Open and test it
65         GimmickMessage(1,"Opening Timestamp database '"
66                        <<GetTimestampDatabasePath()<<"' "
67                        <<std::endl);
68         if ( ! mTimestampDatabase->Open() )
69           {
70             GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
71           }
72         
73       }
74
75   }
76
77    ///////////////////////////////////////////////////////////////////////
78    // add DB to TreeHandler Map                                                                                 //
79    // @param i_name : DB name                                                                               //
80    // @param i_location : DB location                                                               //
81    // return : -                                                                                                                //
82   ///////////////////////////////////////////////////////////////////////
83         void Gimmick::addDB(std::string &i_name, std::string &i_location)
84         {
85                 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
86         }
87
88  
89   ///////////////////////////////////////////////////////////////////////////
90   // create a DB from a attributes descriptor file for medical images      //
91   // @param i_name : DB name                                                                                       //
92   // @param i_locDesc : location of descriptor file                                                //
93   // @param i_locDB : location of DB                                                                       //
94   // return : the SQLiteTreeHandler object on DB                                                   //
95         /////////////////////////////////////////////////////////////////////////
96         SQLiteTreeHandler *Gimmick::createDB(std::string &i_name, std::string &i_locDesc, std::string &i_locDB)
97   {
98       SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
99     // Create or open local database
100     if (! boost::filesystem::exists(i_locDB) )
101      {
102          std::string mess = "Local database '";
103          mess += i_locDB;
104          mess += "' does not exist : creating it";
105          GimmickMessage(1,mess<<std::endl);
106          
107                  // CREATING DB STRUCTURE
108          sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
109          if ( ! sqlTreeH->Create(true) )
110                  {
111                         GimmickError("ERROR CREATING '"<<i_locDB<<"'");
112          }
113          sqlTreeH->SetAttribute(0,"Name",i_name);
114          }
115          else 
116          {
117                 /// Open and test it
118                 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
119         if ( !sqlTreeH->Open(true) )
120                 {
121                         GimmickError("ERROR OPENING '"<<i_locDB<<"'");
122                 }
123       }
124          return sqlTreeH;
125   }
126
127
128   //==============================================================
129   void Gimmick::Finalize()
130   {
131          
132           // delete SQLiteTreeHandler Object
133            for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
134            it!= mTreeHandlerMap.end(); ++it)
135            {
136                    delete it->second;
137            }
138         delete mTimestampDatabase;
139   }
140   //==============================================================
141
142   //================================================================
143   // file separator
144 #if defined(_WIN32)
145 #define VALID_FILE_SEPARATOR "\\"
146 #define INVALID_FILE_SEPARATOR "/"
147 #else
148 #define INVALID_FILE_SEPARATOR "\\"
149 #define VALID_FILE_SEPARATOR "/"
150 #endif
151   //================================================================
152
153   //================================================================
154   const std::string& Gimmick::GetHomeDirectory()
155   {
156     if (mHomeDirectory.size()==0) 
157       {
158 #if defined(__GNUC__)
159         mHomeDirectory = getenv("HOME");
160 #elif defined(_WIN32)
161         mHomeDirectory = getenv("USERPROFILE");
162 #endif
163       }
164     return mHomeDirectory;
165   }
166   //================================================================
167   const std::string& Gimmick::GetUserSettingsDirectory()
168   {
169     if (mUserSettingsDirectory.size()==0) 
170       {
171         mUserSettingsDirectory = GetHomeDirectory();
172         mUserSettingsDirectory += "/.gimmick/";
173         boost::algorithm::replace_all( mUserSettingsDirectory, 
174                                        INVALID_FILE_SEPARATOR , 
175                                        VALID_FILE_SEPARATOR);
176       }
177     return mUserSettingsDirectory;
178   }
179   //================================================================
180
181   //================================================================
182   const std::string& Gimmick::GetLocalDatabasePath()
183   {
184     if (mLocalDatabasePath.size()==0) 
185       {
186         mLocalDatabasePath = GetUserSettingsDirectory();
187         mLocalDatabasePath += "local_database.sqlite3";
188         boost::algorithm::replace_all( mLocalDatabasePath,
189                                        INVALID_FILE_SEPARATOR , 
190                                        VALID_FILE_SEPARATOR);
191       }
192     return mLocalDatabasePath;    
193   }
194
195   //================================================================
196
197   //================================================================
198   const std::string& Gimmick::GetTimestampDatabasePath()
199   {
200     if (mTimestampDatabasePath.size()==0) 
201       {
202         mTimestampDatabasePath = GetUserSettingsDirectory();
203         mTimestampDatabasePath += "timestamp_database.sqlite3";
204         boost::algorithm::replace_all( mTimestampDatabasePath,
205                                        INVALID_FILE_SEPARATOR , 
206                                        VALID_FILE_SEPARATOR);
207       }
208     return mTimestampDatabasePath;    
209   }
210   //========================================================================
211
212   //========================================================================
213   void Gimmick::CreateUserSettingsDirectory()
214   {
215     if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
216       {
217         GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
218                        << "does not exist : creating it"<<std::endl);
219         
220         if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
221           {
222             GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
223           }
224       }
225   }
226   //========================================================================
227
228
229   //========================================================================
230   /// Sets message level
231   void Gimmick::SetMessageLevel(int l)
232   {
233     SetGimmickMessageLevel(l);
234   }
235   //========================================================================
236
237   //========================================================================
238   /// Sets message level
239   void Gimmick::SetDebugMessageLevel(int l)
240   {
241     SetGimmickDebugMessageLevel(l);
242   }
243   //========================================================================
244
245   //========================================================================
246   /// Returns the tree handler with the given name
247   TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const 
248   {  
249     TreeHandlerMapType::const_iterator i;
250     i = GetTreeHandlerMap().find(name);
251     if ( i == GetTreeHandlerMap().end() )
252       {
253         GimmickError("TreeHandler '"<<name<<"' does not exist");
254       }
255     return i->second;
256   }
257
258   //========================================================================
259   ///Returns the timestamp database handler
260   TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const 
261   {  
262     return mTimestampDatabase;
263   }
264
265
266   //========================================================================
267   /// Add the files to the tree handler
268   void Gimmick::AddFiles(const std::string& d, 
269                         const std::vector<std::string>& filenames)
270   {
271     GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
272  
273     mImageAdder.SetTreeHandler(GetTreeHandler(d));
274         mImageAdder.SetTimestampHandler(mTimestampDatabase);
275     mImageAdder.AddFiles(filenames);
276
277   }
278   //========================================================================
279
280   //========================================================================
281   /// Add a dir to the local database
282   void Gimmick::AddDir(const std::string& d, const std::string& f, 
283                        bool recurse)
284   {
285     GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
286                    <<recurse<<std::endl);
287
288         TreeHandler * handler=GetTreeHandler(d);
289     mImageAdder.SetTreeHandler(handler);
290         mImageAdder.SetTimestampHandler(mTimestampDatabase);
291     mImageAdder.AddDirectory(f,recurse);
292         //Synchronize(true, handler);
293     
294   }
295
296   //========================================================================
297
298   //========================================================================
299  
300   void Gimmick::Synchronize(bool update, TreeHandler* handler)
301   {
302           GimmickMessage(4,"Synchronizing. Update:"<<update<<std::endl);
303           if(mSynchronizer==0)
304           {
305                   mSynchronizer=new Synchronizer(handler);
306           }
307           else
308           {
309                   mSynchronizer->SetTreeHandler(handler);
310           }
311           mSynchronizer->Synchronize(update);
312
313   }
314
315   //========================================================================
316   /// 
317   void Gimmick::Print(const std::string& d)
318   {
319     GetTreeHandler(d)->GetTree().Print();
320   }
321   //========================================================================
322
323
324 }