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