1 #include <creaImageIOGimmick.h>
3 #include <creaImageIOSystem.h>
5 #include <boost/filesystem.hpp>
6 #include <boost/algorithm/string.hpp>
12 //==============================================================
16 RegisterGimmickMessageTypes();
18 //==============================================================
22 //==============================================================
27 //==============================================================
30 //==============================================================
31 void Gimmick::Initialize()
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());
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;
46 // Creates files and directories database
47 mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
48 // Create or open local database
49 if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
51 std::string mess = "Timestamp database '";
52 mess += GetTimestampDatabasePath();
53 mess += "' does not exist : creating it";
54 GimmickMessage(1,mess<<std::endl);
56 if ( ! mTimestampDatabase->Create() )
58 GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
65 GimmickMessage(1,"Opening Timestamp database '"
66 <<GetTimestampDatabasePath()<<"' "
68 if ( ! mTimestampDatabase->Open() )
70 GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
77 ///////////////////////////////////////////////////////////////////////
78 // add DB to TreeHandler Map //
79 // @param i_name : DB name //
80 // @param i_location : DB location //
82 ///////////////////////////////////////////////////////////////////////
83 void Gimmick::addDB(const std::string &i_name,
84 const std::string &i_location)
86 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
88 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
89 mTreeHandlerMap[i_name]->Open(true);
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)
105 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
106 // Create or open local database
107 if (! boost::filesystem::exists(i_locDB) )
109 std::string mess = "Local database '";
111 mess += "' does not exist : creating it";
112 GimmickMessage(1,mess<<std::endl);
114 // CREATING DB STRUCTURE
115 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
116 if ( ! sqlTreeH->Create(true) )
118 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
120 sqlTreeH->SetAttribute(0,"Name",i_name);
125 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
126 if ( !sqlTreeH->Open(true) )
128 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
135 //==============================================================
136 void Gimmick::Finalize()
139 // delete SQLiteTreeHandler Object
140 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
141 it!= mTreeHandlerMap.end(); ++it)
145 delete mTimestampDatabase;
147 //==============================================================
149 //================================================================
152 #define VALID_FILE_SEPARATOR "\\"
153 #define INVALID_FILE_SEPARATOR "/"
155 #define INVALID_FILE_SEPARATOR "\\"
156 #define VALID_FILE_SEPARATOR "/"
158 //================================================================
160 //================================================================
161 const std::string& Gimmick::GetHomeDirectory()
163 if (mHomeDirectory.size()==0)
165 #if defined(__GNUC__)
166 mHomeDirectory = getenv("HOME");
167 #elif defined(_WIN32)
168 mHomeDirectory = getenv("USERPROFILE");
171 return mHomeDirectory;
173 //================================================================
174 const std::string& Gimmick::GetUserSettingsDirectory()
176 if (mUserSettingsDirectory.size()==0)
178 mUserSettingsDirectory = GetHomeDirectory();
179 mUserSettingsDirectory += "/.gimmick/";
180 boost::algorithm::replace_all( mUserSettingsDirectory,
181 INVALID_FILE_SEPARATOR ,
182 VALID_FILE_SEPARATOR);
184 return mUserSettingsDirectory;
186 //================================================================
188 //================================================================
189 const std::string& Gimmick::GetLocalDatabasePath()
191 if (mLocalDatabasePath.size()==0)
193 mLocalDatabasePath = GetUserSettingsDirectory();
194 mLocalDatabasePath += "local_database.sqlite3";
195 boost::algorithm::replace_all( mLocalDatabasePath,
196 INVALID_FILE_SEPARATOR ,
197 VALID_FILE_SEPARATOR);
199 return mLocalDatabasePath;
202 //================================================================
204 //================================================================
205 const std::string& Gimmick::GetTimestampDatabasePath()
207 if (mTimestampDatabasePath.size()==0)
209 mTimestampDatabasePath = GetUserSettingsDirectory();
210 mTimestampDatabasePath += "timestamp_database.sqlite3";
211 boost::algorithm::replace_all( mTimestampDatabasePath,
212 INVALID_FILE_SEPARATOR ,
213 VALID_FILE_SEPARATOR);
215 return mTimestampDatabasePath;
217 //========================================================================
219 //========================================================================
220 void Gimmick::CreateUserSettingsDirectory()
222 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
224 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
225 << "does not exist : creating it"<<std::endl);
227 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
229 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
233 //========================================================================
236 //========================================================================
237 /// Sets message level
238 void Gimmick::SetMessageLevel(int l)
240 SetGimmickMessageLevel(l);
242 //========================================================================
244 //========================================================================
245 /// Sets message level
246 void Gimmick::SetDebugMessageLevel(int l)
248 SetGimmickDebugMessageLevel(l);
250 //========================================================================
252 //========================================================================
253 /// Returns the tree handler with the given name
254 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
256 TreeHandlerMapType::const_iterator i;
257 i = GetTreeHandlerMap().find(name);
258 if ( i == GetTreeHandlerMap().end() )
260 GimmickError("TreeHandler '"<<name<<"' does not exist");
265 //========================================================================
266 ///Returns the timestamp database handler
267 TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const
269 return mTimestampDatabase;
273 //========================================================================
274 /// Add the files to the tree handler
275 void Gimmick::AddFiles(const std::string& d,
276 const std::vector<std::string>& filenames)
278 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
280 mImageAdder.SetTreeHandler(GetTreeHandler(d));
281 mImageAdder.SetTimestampHandler(mTimestampDatabase);
282 mImageAdder.SetSynchronizer(mSynchronizer);
283 mImageAdder.AddFiles(filenames);
286 //========================================================================
288 //========================================================================
289 /// Add a dir to the local database
290 void Gimmick::AddDir(const std::string& d, const std::string& f,
293 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
294 <<recurse<<std::endl);
296 TreeHandler * handler=GetTreeHandler(d);
297 mImageAdder.SetTreeHandler(handler);
298 mImageAdder.SetTimestampHandler(mTimestampDatabase);
299 mImageAdder.SetSynchronizer(mSynchronizer);
300 mImageAdder.AddDirectory(f,recurse);
303 //========================================================================
305 //========================================================================
306 void Gimmick::RemoveFile(const std::string& d,
309 mImageAdder.SetSynchronizer(mSynchronizer);
310 mTimestampDatabase->RemoveNode("PATH",node);
311 mImageAdder.RemoveFile(node);
313 //========================================================================
315 //========================================================================
317 std::string Gimmick::Synchronize(bool repair, bool checkAttributes)
319 TreeHandler * handler=GetTreeHandler("Local database");
320 mImageAdder.SetTreeHandler(handler);
321 mImageAdder.SetTimestampHandler(mTimestampDatabase);
322 mImageAdder.SetSynchronizer(mSynchronizer);
323 return mImageAdder.Synchronize(repair, checkAttributes);
326 //========================================================================
328 void Gimmick::Print(const std::string& d)
330 GetTreeHandler(d)->GetTree().Print();
332 //========================================================================