1 #include <creaImageIOGimmick.h>
3 #include <creaImageIOSystem.h>
4 #include <boost/filesystem.hpp>
5 #include <boost/algorithm/string.hpp>
7 #ifndef PATH_MAX // If not defined yet : do it
15 //==============================================================
19 RegisterGimmickMessageTypes();
23 //==============================================================
27 //==============================================================
32 mSettings->writeSettingsFile();
40 //==============================================================
42 //==============================================================
43 void Gimmick::Initialize(const std::string& path)
48 //==============================================================
49 void Gimmick::Initialize()
51 std::string i_nameDB = "Local database";
52 // Create the UserSettings dir if does not exist
53 CreateUserSettingsDirectory();
54 // Sets the current directory to the home dir
55 mCurrentDirectory = GetHomeDirectory();
56 mSynchronizer= new Synchronizer(GetUserSettingsDirectory()+"Shared/gimmick/");
58 mSettings = new Settings(mCurrentDirectory);
61 std::string dbpath = GetLocalDatabasePath();
62 // Create or open local database
63 std::string dpath= mCurrentDirectory + "/.gimmick/Shared/gimmick/localdatabase_Descriptor.txt";
64 boost::algorithm::replace_all( dpath,
65 INVALID_FILE_SEPARATOR ,
66 VALID_FILE_SEPARATOR);
67 mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
68 // Add it to the TreeHandlerMap
69 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
71 //Add additional DB from user Settings
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);
90 mSettings->addDB(i_location);
95 ///////////////////////////////////////////////////////////////////////////
96 // create a DB from a attributes descriptor file for medical images //
97 // @param i_name : DB name //
98 // @param i_locDesc : location of descriptor file //
99 // @param i_locDB : location of DB //
100 // return : the SQLiteTreeHandler object on DB //
101 /////////////////////////////////////////////////////////////////////////
102 SQLiteTreeHandler *Gimmick::createDB(const std::string &i_name,
103 const std::string &i_locDesc,
104 const std::string &i_locDB)
106 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
107 // Create or open local database
108 if (! boost::filesystem::exists(i_locDB) )
110 std::string mess = "Local database '";
112 mess += "' does not exist : creating it";
113 GimmickMessage(1,mess<<std::endl);
115 // CREATING DB STRUCTURE
116 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
117 if ( ! sqlTreeH->Create(true) )
119 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
121 sqlTreeH->SetAttribute(0,"Name",i_name);
126 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
127 if ( !sqlTreeH->Open(true) )
129 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
136 //==============================================================
137 void Gimmick::Finalize()
140 // delete SQLiteTreeHandler Object
141 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
142 it!= mTreeHandlerMap.end(); ++it)
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 //================================================================
189 //================================================================
190 const std::string& Gimmick::GetLocalDatabasePath()
192 if (mLocalDatabasePath.size()==0)
194 mLocalDatabasePath = GetUserSettingsDirectory();
195 mLocalDatabasePath += "Shared/gimmick/local_database.sqlite3";
196 boost::algorithm::replace_all( mLocalDatabasePath,
197 INVALID_FILE_SEPARATOR ,
198 VALID_FILE_SEPARATOR);
200 return mLocalDatabasePath;
204 //========================================================================
206 //========================================================================
207 void Gimmick::CreateUserSettingsDirectory()
209 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
211 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
212 << "does not exist : creating it"<<std::endl);
214 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
216 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
220 std::string setDir=GetUserSettingsDirectory();
221 boost::algorithm::replace_all( setDir,
222 INVALID_FILE_SEPARATOR ,
223 VALID_FILE_SEPARATOR);
225 boost::filesystem::create_directory( setDir );
227 boost::filesystem::create_directory( setDir );
228 setDir+="localdatabase_Descriptor.txt";
230 if(!boost::filesystem::is_regular(setDir))
233 //EED int err = GetBinaryDirectory(name, PATH_MAX);
234 crea::System::GetAppPath(name,PATH_MAX);
235 std::cout<<name<<std::endl;
237 std::string path=name;
238 path=path.substr(0,path.size()-1);
239 path=path.substr(0,path.find_last_of("/"));
240 //Creating directories
241 path+="/bin/Shared/gimmick/localdatabase_Descriptor.txt";
242 std::cout<<"From: "<<path<<std::endl;
243 std::cout<<"To: "<<setDir<<std::endl;
244 boost::algorithm::replace_all( path,
245 INVALID_FILE_SEPARATOR ,
246 VALID_FILE_SEPARATOR);
247 boost::filesystem::copy_file(path,setDir);
250 //========================================================================
253 //========================================================================
254 /// Sets message level
255 void Gimmick::SetMessageLevel(int l)
257 SetGimmickMessageLevel(l);
259 //========================================================================
261 //========================================================================
262 /// Sets message level
263 void Gimmick::SetDebugMessageLevel(int l)
265 SetGimmickDebugMessageLevel(l);
267 //========================================================================
269 //========================================================================
270 /// Returns the tree handler with the given name
271 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
273 TreeHandlerMapType::const_iterator i;
274 i = GetTreeHandlerMap().find(name);
275 if ( i == GetTreeHandlerMap().end() )
277 GimmickError("TreeHandler '"<<name<<"' does not exist");
284 //========================================================================
285 /// Add the files to the tree handler
286 void Gimmick::AddFiles(const std::string& d,
287 const std::vector<std::string>& filenames)
289 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
291 mImageAdder.SetCurrentDatabase(d);
292 mImageAdder.SetTreeHandler(GetTreeHandler(d));
293 mImageAdder.SetSynchronizer(mSynchronizer);
294 mImageAdder.AddFiles(filenames);
297 //========================================================================
299 //========================================================================
300 /// Add a dir to the local database
301 void Gimmick::AddDir(const std::string& d, const std::string& f,
304 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
305 <<recurse<<std::endl);
307 TreeHandler * handler=GetTreeHandler(d);
308 mImageAdder.SetCurrentDatabase(d);
309 mImageAdder.SetTreeHandler(handler);
310 mImageAdder.SetSynchronizer(mSynchronizer);
311 mImageAdder.AddDirectory(f,recurse);
314 //========================================================================
316 //========================================================================
317 void Gimmick::RemoveFile(const std::string& d,
320 mImageAdder.SetCurrentDatabase(d);
321 mImageAdder.SetSynchronizer(mSynchronizer);
322 mImageAdder.RemoveFile(node);
324 //========================================================================
326 //========================================================================
328 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
330 TreeHandler * handler=GetTreeHandler(d);
331 mImageAdder.SetCurrentDatabase(d);
332 mImageAdder.SetTreeHandler(handler);
333 mImageAdder.SetSynchronizer(mSynchronizer);
334 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
337 //========================================================================
339 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
341 TreeHandler * handler=GetTreeHandler(d);
342 mImageAdder.SetCurrentDatabase(d);
343 mImageAdder.SetTreeHandler(handler);
344 mImageAdder.SetSynchronizer(mSynchronizer);
345 return mImageAdder.Synchronize(repair, checkAttributes);
348 //========================================================================
350 void Gimmick::Print(const std::string& d)
352 GetTreeHandler(d)->GetTree().Print();
354 //========================================================================
356 void Gimmick::GetSetting(const std::string& name, std::string& value)
358 value = mSettings->getValue(name);
360 //========================================================================
362 //========================================================================
364 void Gimmick::GetAttributes(const std::string& d,
365 const std::string& filename,
366 const std::vector<std::string>& params,
367 std::vector<std::string>& results)
369 TreeHandler * handler=GetTreeHandler(d);
370 mImageAdder.SetCurrentDatabase(d);
371 mImageAdder.SetTreeHandler(handler);
372 mImageAdder.SetSynchronizer(mSynchronizer);
373 mImageAdder.GetAttributes(params, filename, results);
375 //========================================================================
377 //========================================================================
379 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
381 mSettings->updateSetting(name,value);
382 mSettings->writeSettingsFile();
384 //========================================================================
386 void Gimmick::DeleteDrive(const std::string& drive)
388 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
389 it!= mTreeHandlerMap.end(); ++it)
391 mImageAdder.SetTreeHandler(it->second);
392 mImageAdder.DeleteDriveFromMainDB(drive);
394 mImageAdder.SetSynchronizer(mSynchronizer);
395 mImageAdder.DeleteDriveFromOtherDB(drive);
398 //========================================================================
399 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
401 TreeHandler * handler=GetTreeHandler(d);
402 mImageAdder.SetCurrentDatabase(d);
403 mImageAdder.SetTreeHandler(handler);
404 mImageAdder.EditField(node,name,key,val);
406 //========================================================================
409 /////////////////////////////////////////////////////////////////////////
410 // add DB from Settings file //
413 /////////////////////////////////////////////////////////////////////////
414 void Gimmick::addDBSettings()
417 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
419 // split to find all paths
420 std::vector<std::string> paths;
421 std::string separator = ";";
422 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
423 //find first separator
424 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
425 while(std::string::npos != pos || std::string::npos != last_pos)
427 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
428 last_pos = pathSettings.find_first_not_of(separator, pos);
429 pos = pathSettings.find_first_of(separator, last_pos);
432 std::vector<std::string>::iterator it_path = paths.begin();
433 for(; it_path != paths.end(); ++it_path)
435 pos = it_path->find_last_of("\\");
436 last_pos = it_path->find_last_of(".");
437 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
438 addDB(name, it_path->c_str());