2 #include <creaImageIOGimmick.h>
4 #include <creaImageIOSystem.h>
5 #include <boost/filesystem.hpp>
6 #include <boost/algorithm/string.hpp>
8 #ifndef PATH_MAX // If not defined yet : do it
12 #ifdef _RELWITHDEBINFO
23 //==============================================================
27 RegisterGimmickMessageTypes();
30 mLocalDescpName = "localdatabase_Descriptor.dscp";
31 mLocalDBName = "Local database";
33 //==============================================================
36 //==============================================================
41 mSettings->writeSettingsFile();
49 //==============================================================
51 //==============================================================
52 void Gimmick::Initialize(const std::string& i_namedescp, const std::string i_namedb)
54 mLocalDescpName = i_namedescp;
55 mLocalDBName = i_namedb;
59 //==============================================================
60 void Gimmick::Initialize()
62 std::string i_nameDB = mLocalDBName;
63 // Create the UserSettings dir if does not exist
64 CreateUserSettingsDirectory();
65 // Sets the current directory to the home dir
66 mCurrentDirectory = GetHomeDirectory();
67 mSynchronizer= new Synchronizer(GetUserSettingsDirectory()+"Shared/gimmick/");
69 mSettings = new Settings(mCurrentDirectory);
71 std::string dbpath = GetLocalDatabasePath();
72 // Create or open local database
73 std::string dpath= mCurrentDirectory + "/.gimmick/Shared/gimmick/" + mLocalDescpName;
74 boost::algorithm::replace_all( dpath,
75 INVALID_FILE_SEPARATOR ,
76 VALID_FILE_SEPARATOR);
77 mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
78 // Add it to the TreeHandlerMap
79 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
81 //Add additional DB from user Settings
85 ///////////////////////////////////////////////////////////////////////
86 // add DB to TreeHandler Map //
87 // @param i_name : DB name //
88 // @param i_location : DB location //
90 ////////////////////////////////////////////////////////////////////////
91 void Gimmick::addDB(const std::string &i_name,
92 const std::string &i_location)
94 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
96 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
97 mTreeHandlerMap[i_name]->Open(true);
98 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<<"'");
142 //==============================================================
143 void Gimmick::Finalize()
145 if(mTreeHandlerMap.size() >0)
147 // delete SQLiteTreeHandler Object
148 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
149 it!= mTreeHandlerMap.end();
156 //==============================================================
158 //================================================================
161 #define VALID_FILE_SEPARATOR "\\"
162 #define INVALID_FILE_SEPARATOR "/"
164 #define INVALID_FILE_SEPARATOR "\\"
165 #define VALID_FILE_SEPARATOR "/"
167 //================================================================
169 //================================================================
170 const std::string& Gimmick::GetHomeDirectory()
172 if (mHomeDirectory.size()==0)
174 #if defined(__GNUC__)
175 mHomeDirectory = getenv("HOME");
176 #elif defined(_WIN32)
177 mHomeDirectory = getenv("USERPROFILE");
180 return mHomeDirectory;
182 //================================================================
183 const std::string& Gimmick::GetUserSettingsDirectory()
185 if (mUserSettingsDirectory.size()==0)
187 mUserSettingsDirectory = GetHomeDirectory();
188 mUserSettingsDirectory += "/.gimmick/";
189 boost::algorithm::replace_all( mUserSettingsDirectory,
190 INVALID_FILE_SEPARATOR ,
191 VALID_FILE_SEPARATOR);
193 return mUserSettingsDirectory;
195 //================================================================
198 //================================================================
199 const std::string& Gimmick::GetLocalDatabasePath()
201 if (mLocalDatabasePath.size()==0)
203 mLocalDatabasePath = GetUserSettingsDirectory();
204 mLocalDatabasePath += "Shared/gimmick/local_database.sqlite3";
205 boost::algorithm::replace_all( mLocalDatabasePath,
206 INVALID_FILE_SEPARATOR ,
207 VALID_FILE_SEPARATOR);
209 return mLocalDatabasePath;
212 //========================================================================
214 //========================================================================
215 void Gimmick::CreateUserSettingsDirectory()
217 //JCP settings dir 02/10/2009
218 const std::string settingsdirectory = GetUserSettingsDirectory();
219 bool isdir = boost::filesystem::is_directory( settingsdirectory );
222 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
223 << "does not exist : creating it"<<std::endl);
225 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
227 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
231 std::string setDir=GetUserSettingsDirectory();
232 boost::algorithm::replace_all( setDir,
233 INVALID_FILE_SEPARATOR ,
234 VALID_FILE_SEPARATOR);
236 boost::filesystem::create_directory( setDir );
238 boost::filesystem::create_directory( setDir );
239 setDir+=mLocalDescpName;
241 if(!boost::filesystem::is_regular(setDir))
244 //EED int err = GetBinaryDirectory(name, PATH_MAX);
245 crea::System::GetAppPath(name,PATH_MAX);
246 std::cout<<name<<std::endl;
248 std::string path=name;
249 path=path.substr(0,path.size()-1);
250 path=path.substr(0,path.find_last_of("/"));
251 //Creating directories
253 // The following stuff works on Linux, NOT CHECKED on Windows // JPR
256 path+="/bin/Shared/gimmick/";
257 path+=mLocalDescpName;
259 path+="/Shared/gimmick/";
260 path+= mLocalDescpName;
262 std::cout <<"From: " << path <<std::endl;
263 std::cout <<"To: " << setDir <<std::endl;
264 boost::algorithm::replace_all( path,
265 INVALID_FILE_SEPARATOR ,
266 VALID_FILE_SEPARATOR);
267 boost::filesystem::copy_file(path,setDir);
270 //========================================================================
273 //========================================================================
274 /// Sets message level
275 void Gimmick::SetMessageLevel(int l)
277 SetGimmickMessageLevel(l);
279 //========================================================================
281 //========================================================================
282 /// Sets message level
283 void Gimmick::SetDebugMessageLevel(int l)
285 SetGimmickDebugMessageLevel(l);
287 //========================================================================
289 //========================================================================
290 /// Returns the tree handler with the given name
291 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
293 TreeHandlerMapType::const_iterator i;
294 i = GetTreeHandlerMap().find(name);
295 if ( i == GetTreeHandlerMap().end() )
297 GimmickError("TreeHandler '"<<name<<"' does not exist");
302 //========================================================================
303 /// Add the files to the tree handler
304 void Gimmick::AddFiles(const std::string& d,
305 const std::vector<std::string>& filenames)
307 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
309 mImageAdder.SetCurrentDatabase(d);
310 mImageAdder.SetTreeHandler(GetTreeHandler(d));
311 mImageAdder.SetSynchronizer(mSynchronizer);
312 mImageAdder.AddFiles(filenames);
314 //========================================================================
316 //========================================================================
317 /// Add a dir to the local database
318 void Gimmick::AddDir(const std::string& d, const std::string& f,
321 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
322 <<recurse<<std::endl);
324 TreeHandler * handler=GetTreeHandler(d);
325 mImageAdder.SetCurrentDatabase(d);
326 mImageAdder.SetTreeHandler(handler);
327 mImageAdder.SetSynchronizer(mSynchronizer);
328 mImageAdder.AddDirectory(f,recurse);
331 //========================================================================
333 //========================================================================
334 void Gimmick::RemoveFile(const std::string& d,
337 mImageAdder.SetCurrentDatabase(d);
338 mImageAdder.SetSynchronizer(mSynchronizer);
339 mImageAdder.RemoveFile(node);
341 //========================================================================
343 //========================================================================
345 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
347 TreeHandler * handler=GetTreeHandler(d);
348 mImageAdder.SetCurrentDatabase(d);
349 mImageAdder.SetTreeHandler(handler);
350 mImageAdder.SetSynchronizer(mSynchronizer);
351 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
354 //========================================================================
356 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
358 TreeHandler * handler=GetTreeHandler(d);
359 mImageAdder.SetCurrentDatabase(d);
360 mImageAdder.SetTreeHandler(handler);
361 mImageAdder.SetSynchronizer(mSynchronizer);
362 return mImageAdder.Synchronize(repair, checkAttributes);
365 //========================================================================
367 void Gimmick::Print(const std::string& d)
369 GetTreeHandler(d)->GetTree().Print();
371 //========================================================================
373 void Gimmick::GetSetting(const std::string& name, std::string& value)
375 value = mSettings->getValue(name);
377 //========================================================================
379 //========================================================================
381 void Gimmick::GetAttributes(const std::string& d,
382 const std::string& filename,
383 const std::vector<std::string>& params,
384 std::vector<std::string>& results)
386 TreeHandler * handler=GetTreeHandler(d);
387 mImageAdder.SetCurrentDatabase(d);
388 mImageAdder.SetTreeHandler(handler);
389 mImageAdder.SetSynchronizer(mSynchronizer);
390 mImageAdder.GetAttributes(params, filename, results);
392 //========================================================================
394 //========================================================================
396 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
398 mSettings->updateSetting(name,value);
399 mSettings->writeSettingsFile();
401 //========================================================================
403 void Gimmick::DeleteDrive(const std::string& drive)
405 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
406 it!= mTreeHandlerMap.end();
409 mImageAdder.SetTreeHandler(it->second);
410 mImageAdder.DeleteDriveFromMainDB(drive);
412 mImageAdder.SetSynchronizer(mSynchronizer);
413 mImageAdder.DeleteDriveFromOtherDB(drive);
416 //========================================================================
417 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
419 TreeHandler * handler=GetTreeHandler(d);
420 mImageAdder.SetCurrentDatabase(d);
421 mImageAdder.SetTreeHandler(handler);
422 mImageAdder.EditField(node,name,key,val);
424 //========================================================================
426 ////////////////////////////////////////////////////////////////////////
427 // add DB from Settings file //
430 ////////////////////////////////////////////////////////////////////////
431 void Gimmick::addDBSettings()
434 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
436 // split to find all paths
437 std::vector<std::string> paths;
438 std::string separator = ";";
439 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
440 //find first separator
441 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
442 while(std::string::npos != pos || std::string::npos != last_pos)
444 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
445 last_pos = pathSettings.find_first_not_of(separator, pos);
446 pos = pathSettings.find_first_of(separator, last_pos);
449 std::vector<std::string>::iterator it_path = paths.begin();
450 for(; it_path != paths.end(); ++it_path)
452 pos = it_path->find_last_of("\\");
453 last_pos = it_path->find_last_of(".");
454 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
455 addDB(name, it_path->c_str());