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