1 #include <creaImageIOGimmick.h>
3 #include <creaImageIOSystem.h>
4 #include <boost/filesystem.hpp>
5 #include <boost/algorithm/string.hpp>
11 //==============================================================
15 RegisterGimmickMessageTypes();
17 //==============================================================
21 //==============================================================
24 mSettings->writeSettingsFile();
28 //==============================================================
31 //==============================================================
32 void Gimmick::Initialize()
34 std::string i_nameDB = "Local database";
35 // Create the UserSettings dir if does not exist
36 CreateUserSettingsDirectory();
37 // Sets the current directory to the home dir
38 mCurrentDirectory = GetHomeDirectory();
39 mSynchronizer= new Synchronizer(GetUserSettingsDirectory());
41 mSettings = new Settings(mCurrentDirectory);
44 std::string dbpath = GetLocalDatabasePath();
45 // Create or open local database
46 mLocalDatabase = createDB(i_nameDB, mCurrentDirectory + "\\.gimmick\\localdatabase_Descriptor.txt", dbpath);
47 // Add it to the TreeHandlerMap
48 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
50 //Add additional DB from user Settings
53 // Creates files and directories database
54 mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
55 // Create or open local database
56 if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
58 std::string mess = "Timestamp database '";
59 mess += GetTimestampDatabasePath();
60 mess += "' does not exist : creating it";
61 GimmickMessage(1,mess<<std::endl);
63 if ( ! mTimestampDatabase->Create() )
65 GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
72 GimmickMessage(1,"Opening Timestamp database '"
73 <<GetTimestampDatabasePath()<<"' "
75 if ( ! mTimestampDatabase->Open() )
77 GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
84 ///////////////////////////////////////////////////////////////////////
85 // add DB to TreeHandler Map //
86 // @param i_name : DB name //
87 // @param i_location : DB location //
89 ///////////////////////////////////////////////////////////////////////
90 void Gimmick::addDB(const std::string &i_name,
91 const std::string &i_location)
93 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
95 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
96 mTreeHandlerMap[i_name]->Open(true);
97 mSettings->addDB(i_location);
102 ///////////////////////////////////////////////////////////////////////////
103 // create a DB from a attributes descriptor file for medical images //
104 // @param i_name : DB name //
105 // @param i_locDesc : location of descriptor file //
106 // @param i_locDB : location of DB //
107 // return : the SQLiteTreeHandler object on DB //
108 /////////////////////////////////////////////////////////////////////////
109 SQLiteTreeHandler *Gimmick::createDB(const std::string &i_name,
110 const std::string &i_locDesc,
111 const std::string &i_locDB)
113 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
114 // Create or open local database
115 if (! boost::filesystem::exists(i_locDB) )
117 std::string mess = "Local database '";
119 mess += "' does not exist : creating it";
120 GimmickMessage(1,mess<<std::endl);
122 // CREATING DB STRUCTURE
123 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
124 if ( ! sqlTreeH->Create(true) )
126 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
128 sqlTreeH->SetAttribute(0,"Name",i_name);
133 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
134 if ( !sqlTreeH->Open(true) )
136 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
143 //==============================================================
144 void Gimmick::Finalize()
147 // delete SQLiteTreeHandler Object
148 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
149 it!= mTreeHandlerMap.end(); ++it)
153 delete mTimestampDatabase;
155 //==============================================================
157 //================================================================
160 #define VALID_FILE_SEPARATOR "\\"
161 #define INVALID_FILE_SEPARATOR "/"
163 #define INVALID_FILE_SEPARATOR "\\"
164 #define VALID_FILE_SEPARATOR "/"
166 //================================================================
168 //================================================================
169 const std::string& Gimmick::GetHomeDirectory()
171 if (mHomeDirectory.size()==0)
173 #if defined(__GNUC__)
174 mHomeDirectory = getenv("HOME");
175 #elif defined(_WIN32)
176 mHomeDirectory = getenv("USERPROFILE");
179 return mHomeDirectory;
181 //================================================================
182 const std::string& Gimmick::GetUserSettingsDirectory()
184 if (mUserSettingsDirectory.size()==0)
186 mUserSettingsDirectory = GetHomeDirectory();
187 mUserSettingsDirectory += "/.gimmick/";
188 boost::algorithm::replace_all( mUserSettingsDirectory,
189 INVALID_FILE_SEPARATOR ,
190 VALID_FILE_SEPARATOR);
192 return mUserSettingsDirectory;
194 //================================================================
196 //================================================================
197 const std::string& Gimmick::GetLocalDatabasePath()
199 if (mLocalDatabasePath.size()==0)
201 mLocalDatabasePath = GetUserSettingsDirectory();
202 mLocalDatabasePath += "local_database.sqlite3";
203 boost::algorithm::replace_all( mLocalDatabasePath,
204 INVALID_FILE_SEPARATOR ,
205 VALID_FILE_SEPARATOR);
207 return mLocalDatabasePath;
210 //================================================================
212 //================================================================
213 const std::string& Gimmick::GetTimestampDatabasePath()
215 if (mTimestampDatabasePath.size()==0)
217 mTimestampDatabasePath = GetUserSettingsDirectory();
218 mTimestampDatabasePath += "timestamp_database.sqlite3";
219 boost::algorithm::replace_all( mTimestampDatabasePath,
220 INVALID_FILE_SEPARATOR ,
221 VALID_FILE_SEPARATOR);
223 return mTimestampDatabasePath;
225 //========================================================================
227 //========================================================================
228 void Gimmick::CreateUserSettingsDirectory()
230 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
232 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
233 << "does not exist : creating it"<<std::endl);
235 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
237 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
241 //========================================================================
244 //========================================================================
245 /// Sets message level
246 void Gimmick::SetMessageLevel(int l)
248 SetGimmickMessageLevel(l);
250 //========================================================================
252 //========================================================================
253 /// Sets message level
254 void Gimmick::SetDebugMessageLevel(int l)
256 SetGimmickDebugMessageLevel(l);
258 //========================================================================
260 //========================================================================
261 /// Returns the tree handler with the given name
262 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
264 TreeHandlerMapType::const_iterator i;
265 i = GetTreeHandlerMap().find(name);
266 if ( i == GetTreeHandlerMap().end() )
268 GimmickError("TreeHandler '"<<name<<"' does not exist");
273 //========================================================================
274 ///Returns the timestamp database handler
275 TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const
277 return mTimestampDatabase;
281 //========================================================================
282 /// Add the files to the tree handler
283 void Gimmick::AddFiles(const std::string& d,
284 const std::vector<std::string>& filenames)
286 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
288 mImageAdder.SetCurrentDatabase(d);
289 mImageAdder.SetTreeHandler(GetTreeHandler(d));
290 mImageAdder.SetTimestampHandler(mTimestampDatabase);
291 mImageAdder.SetSynchronizer(mSynchronizer);
292 mImageAdder.AddFiles(filenames);
295 //========================================================================
297 //========================================================================
298 /// Add a dir to the local database
299 void Gimmick::AddDir(const std::string& d, const std::string& f,
302 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
303 <<recurse<<std::endl);
305 TreeHandler * handler=GetTreeHandler(d);
306 mImageAdder.SetCurrentDatabase(d);
307 mImageAdder.SetTreeHandler(handler);
308 mImageAdder.SetTimestampHandler(mTimestampDatabase);
309 mImageAdder.SetSynchronizer(mSynchronizer);
310 mImageAdder.AddDirectory(f,recurse);
313 //========================================================================
315 //========================================================================
316 void Gimmick::RemoveFile(const std::string& d,
319 mImageAdder.SetCurrentDatabase(d);
320 mImageAdder.SetSynchronizer(mSynchronizer);
321 mTimestampDatabase->RemoveNode("PATH",node,d);
322 mImageAdder.RemoveFile(node);
324 //========================================================================
326 //========================================================================
328 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
330 TreeHandler * handler=GetTreeHandler(d);
331 mImageAdder.SetCurrentDatabase(d);
332 mImageAdder.SetTreeHandler(handler);
333 mImageAdder.SetTimestampHandler(mTimestampDatabase);
334 mImageAdder.SetSynchronizer(mSynchronizer);
335 return mImageAdder.Synchronize(repair, checkAttributes);
338 //========================================================================
340 void Gimmick::Print(const std::string& d)
342 GetTreeHandler(d)->GetTree().Print();
344 //========================================================================
345 /////////////////////////////////////////////////////////////////////////
346 // add DB from Settings file //
349 /////////////////////////////////////////////////////////////////////////
350 void Gimmick::addDBSettings()
353 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
355 // split to find all paths
356 std::vector<std::string> paths;
357 std::string separator = ";";
358 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
359 //find first separator
360 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
361 while(std::string::npos != pos || std::string::npos != last_pos)
363 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
364 last_pos = pathSettings.find_first_not_of(separator, pos);
365 pos = pathSettings.find_first_of(separator, last_pos);
368 std::vector<std::string>::iterator it_path = paths.begin();
369 for(; it_path != paths.end(); ++it_path)
371 pos = it_path->find_last_of("\\");
372 last_pos = it_path->find_last_of(".");
373 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
374 addDB(name, it_path->c_str());