]> Creatis software - creaImageIO.git/blob - src2/creaImageIOGimmick.cpp
Added Add DataBase event.
[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( 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_locDesc : location of descriptor file                                                //
92   // @param i_locDB : location of DB                                                                       //
93   // return : the SQLiteTreeHandler object on DB                                                   //
94         /////////////////////////////////////////////////////////////////////////
95   SQLiteTreeHandler *Gimmick::createDB(std::string &i_locDesc, std::string &i_locDB)
96   {
97       SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
98     // Create or open local database
99     if (! boost::filesystem::exists(i_locDB) )
100      {
101          std::string mess = "Local database '";
102          mess += i_locDB;
103          mess += "' does not exist : creating it";
104          GimmickMessage(1,mess<<std::endl);
105          
106                  // CREATING DB STRUCTURE
107          sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
108          if ( ! sqlTreeH->Create(true) )
109                  {
110                         GimmickError("ERROR CREATING '"<<i_locDB<<"'");
111          }
112          sqlTreeH->SetAttribute(0,"Name","i_name");
113          }
114          else 
115          {
116                 /// Open and test it
117                 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
118         if ( !sqlTreeH->Open(true) )
119                 {
120                         GimmickError("ERROR OPENING '"<<i_locDB<<"'");
121                 }
122       }
123          return sqlTreeH;
124   }
125
126
127   //==============================================================
128   void Gimmick::Finalize()
129   {
130           // delete SQLiteTreeHandler Object
131            for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
132            it!= mTreeHandlerMap.end(); ++it)
133            {
134                    delete it->second;
135            }
136         delete mTimestampDatabase;
137   }
138   //==============================================================
139
140   //================================================================
141   // file separator
142 #if defined(_WIN32)
143 #define VALID_FILE_SEPARATOR "\\"
144 #define INVALID_FILE_SEPARATOR "/"
145 #else
146 #define INVALID_FILE_SEPARATOR "\\"
147 #define VALID_FILE_SEPARATOR "/"
148 #endif
149   //================================================================
150
151   //================================================================
152   const std::string& Gimmick::GetHomeDirectory()
153   {
154     if (mHomeDirectory.size()==0) 
155       {
156 #if defined(__GNUC__)
157         mHomeDirectory = getenv("HOME");
158 #elif defined(_WIN32)
159         mHomeDirectory = getenv("USERPROFILE");
160 #endif
161       }
162     return mHomeDirectory;
163   }
164   //================================================================
165   const std::string& Gimmick::GetUserSettingsDirectory()
166   {
167     if (mUserSettingsDirectory.size()==0) 
168       {
169         mUserSettingsDirectory = GetHomeDirectory();
170         mUserSettingsDirectory += "/.gimmick/";
171         boost::algorithm::replace_all( mUserSettingsDirectory, 
172                                        INVALID_FILE_SEPARATOR , 
173                                        VALID_FILE_SEPARATOR);
174       }
175     return mUserSettingsDirectory;
176   }
177   //================================================================
178
179   //================================================================
180   const std::string& Gimmick::GetLocalDatabasePath()
181   {
182     if (mLocalDatabasePath.size()==0) 
183       {
184         mLocalDatabasePath = GetUserSettingsDirectory();
185         mLocalDatabasePath += "local_database.sqlite3";
186         boost::algorithm::replace_all( mLocalDatabasePath,
187                                        INVALID_FILE_SEPARATOR , 
188                                        VALID_FILE_SEPARATOR);
189       }
190     return mLocalDatabasePath;    
191   }
192
193   //================================================================
194
195   //================================================================
196   const std::string& Gimmick::GetTimestampDatabasePath()
197   {
198     if (mTimestampDatabasePath.size()==0) 
199       {
200         mTimestampDatabasePath = GetUserSettingsDirectory();
201         mTimestampDatabasePath += "timestamp_database.sqlite3";
202         boost::algorithm::replace_all( mTimestampDatabasePath,
203                                        INVALID_FILE_SEPARATOR , 
204                                        VALID_FILE_SEPARATOR);
205       }
206     return mTimestampDatabasePath;    
207   }
208   //========================================================================
209
210   //========================================================================
211   void Gimmick::CreateUserSettingsDirectory()
212   {
213     if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
214       {
215         GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
216                        << "does not exist : creating it"<<std::endl);
217         
218         if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
219           {
220             GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
221           }
222       }
223   }
224   //========================================================================
225
226
227   //========================================================================
228   /// Sets message level
229   void Gimmick::SetMessageLevel(int l)
230   {
231     SetGimmickMessageLevel(l);
232   }
233   //========================================================================
234
235   //========================================================================
236   /// Sets message level
237   void Gimmick::SetDebugMessageLevel(int l)
238   {
239     SetGimmickDebugMessageLevel(l);
240   }
241   //========================================================================
242
243   //========================================================================
244   /// Returns the tree handler with the given name
245   TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const 
246   {  
247     TreeHandlerMapType::const_iterator i;
248     i = GetTreeHandlerMap().find(name);
249     if ( i == GetTreeHandlerMap().end() )
250       {
251         GimmickError("TreeHandler '"<<name<<"' does not exist");
252       }
253     return i->second;
254   }
255
256   //========================================================================
257   ///Returns the timestamp database handler
258   TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const 
259   {  
260     return mTimestampDatabase;
261   }
262
263
264   //========================================================================
265   /// Add the files to the tree handler
266   void Gimmick::AddFiles(const std::string& d, 
267                         const std::vector<std::string>& filenames)
268   {
269     GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
270  
271     mImageAdder.SetTreeHandler(GetTreeHandler(d));
272         mImageAdder.SetTimestampHandler(mTimestampDatabase);
273     mImageAdder.AddFiles(filenames);
274
275   }
276   //========================================================================
277
278   //========================================================================
279   /// Add a dir to the local database
280   void Gimmick::AddDir(const std::string& d, const std::string& f, 
281                        bool recurse)
282   {
283     GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
284                    <<recurse<<std::endl);
285
286         TreeHandler * handler=GetTreeHandler(d);
287     mImageAdder.SetTreeHandler(handler);
288         mImageAdder.SetTimestampHandler(mTimestampDatabase);
289     mImageAdder.AddDirectory(f,recurse);
290         //Synchronize(true, handler);
291     
292   }
293
294   //========================================================================
295
296   //========================================================================
297  
298   void Gimmick::Synchronize(bool update, TreeHandler* handler)
299   {
300           GimmickMessage(4,"Synchronizing. Update:"<<update<<std::endl);
301           if(mSynchronizer==0)
302           {
303                   mSynchronizer=new Synchronizer(handler);
304           }
305           else
306           {
307                   mSynchronizer->SetTreeHandler(handler);
308           }
309           mSynchronizer->Synchronize(update);
310
311   }
312
313   //========================================================================
314   /// 
315   void Gimmick::Print(const std::string& d)
316   {
317     GetTreeHandler(d)->GetTree().Print();
318   }
319   //========================================================================
320
321
322 }