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