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
11 #include <creaImageIOGimmick.h>
19 //==============================================================
23 RegisterGimmickMessageTypes();
26 mLocalDescpName = "localdatabase_Descriptor.dscp";
27 mLocalDBName = "Local database";
29 //==============================================================
32 //==============================================================
38 mSettings->writeSettingsFile();
46 //==============================================================
48 //==============================================================
49 void Gimmick::Initialize(const std::string i_namedescp, const std::string i_namedb)
51 mLocalDescpName = i_namedescp;
52 mLocalDBName = i_namedb;
56 //==============================================================
57 void Gimmick::Initialize()
59 std::string i_nameDB = mLocalDBName;
60 // Create the UserSettings dir if does not exist
61 CreateUserSettingsDirectory();
62 // Sets the current directory to the home dir
63 mCurrentDirectory = GetHomeDirectory();
64 mSynchronizer= new Synchronizer(GetUserSettingsDirectory()+"Shared/gimmick/");
66 mSettings = new Settings(mCurrentDirectory);
68 std::string dbpath = GetLocalDatabasePath();
70 // Create or open local database
71 std::string dpath= mCurrentDirectory + "/.gimmick/Shared/gimmick/" + mLocalDescpName;
73 boost::algorithm::replace_all( dpath,
74 INVALID_FILE_SEPARATOR ,
75 VALID_FILE_SEPARATOR);
76 mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
77 // Add it to the TreeHandlerMap
78 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
80 //Add additional DB from user Settings
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);
101 ///////////////////////////////////////////////////////////////////////////
102 // create a DB from a attributes descriptor file for medical images //
103 // @param i_name : DB name //
104 // @param i_locDesc : location of descriptor file //
105 // @param i_locDB : location of DB //
106 // return : the SQLiteTreeHandler object on DB //
107 /////////////////////////////////////////////////////////////////////////
108 SQLiteTreeHandler* Gimmick::createDB(const std::string &i_name,
109 const std::string &i_locDesc,
110 const std::string &i_locDB)
112 SQLiteTreeHandler* sqlTreeH( new SQLiteTreeHandler(i_locDB) );
113 // Create or open local database
114 if (! boost::filesystem::exists(i_locDB) )
116 std::string mess = "Local database '";
118 mess += "' does not exist : creating it";
119 GimmickMessage(1,mess<<std::endl);
121 // CREATING DB STRUCTURE
122 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
123 if ( ! sqlTreeH->Create(true) )
125 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
127 sqlTreeH->SetAttribute(0,"Name",i_name);
133 GimmickDebugMessage(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/";
205 mLocalDatabasePath += mLocalDBName;
206 mLocalDatabasePath +=".sqlite3";
207 boost::algorithm::replace_all( mLocalDatabasePath,
208 INVALID_FILE_SEPARATOR ,
209 VALID_FILE_SEPARATOR);
211 return mLocalDatabasePath;
214 //========================================================================
216 //========================================================================
217 void Gimmick::CreateUserSettingsDirectory()
220 // std::string st("C:/Documents and Settings/cervenansky/.gimmick/");
221 // boost::algorithm::replace_all( st,
222 // INVALID_FILE_SEPARATOR ,
223 // VALID_FILE_SEPARATOR);
224 //const boost::filesystem::path mpath(st);
225 //C:\Documents and Settings\cervenansky\.gimmick");
226 //if ( !boost::filesystem::exists( path ) ) return ;
227 // boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end
228 // for ( boost::filesystem::directory_iterator itr( path ); itr != end_itr; ++itr )
230 //// If is directory & recurse : do recurse
231 // if ( boost::filesystem::is_directory(itr->status()) )
235 //JCP settings dir 02/10/2009
236 const std::string settingsdirectory = GetUserSettingsDirectory();
237 //boost::algorithm::replace_all( mUserSettingsDirectory,
238 // INVALID_FILE_SEPARATOR ,
239 // VALID_FILE_SEPARATOR);
241 //("C:\\Documents and Settings\\cervenansky\\.gimmick\\"); // settingsdirectory );
243 isdir = boost::filesystem::is_directory(settingsdirectory); // settingsdirectory );
246 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
247 << "does not exist : creating it"<<std::endl);
249 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
251 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
255 std::string setDir=GetUserSettingsDirectory();
256 boost::algorithm::replace_all( setDir,
257 INVALID_FILE_SEPARATOR ,
258 VALID_FILE_SEPARATOR);
260 boost::filesystem::create_directory( setDir );
262 boost::filesystem::create_directory( setDir );
263 setDir+=mLocalDescpName;
265 if(!boost::filesystem::is_regular(setDir))
268 crea::System::GetAppPath(name,PATH_MAX);
269 std::cout<<name<<std::endl;
271 std::string path=name;
272 path=path.substr(0,path.size()-1);
273 path=path.substr(0,path.find_last_of("/"));
274 //Creating directories
276 // The following stuff works on Linux, NOT CHECKED on Windows // JPR
279 path+="/bin/Shared/gimmick/";
283 path+="/../share/gimmick/";
285 #if defined(__APPLE__)
286 path+="/../../../../share/gimmick/";
290 path+= mLocalDescpName;
292 std::cout <<"From: " << path <<std::endl;
293 std::cout <<"To: " << setDir <<std::endl;
294 boost::algorithm::replace_all( path,
295 INVALID_FILE_SEPARATOR ,
296 VALID_FILE_SEPARATOR);
297 boost::filesystem::copy_file(path,setDir);
301 //========================================================================
304 //========================================================================
305 /// Sets message level
306 void Gimmick::SetMessageLevel(int l)
308 SetGimmickMessageLevel(l);
310 //========================================================================
312 //========================================================================
313 /// Sets message level
314 void Gimmick::SetDebugMessageLevel(int l)
316 SetGimmickDebugMessageLevel(l);
318 //========================================================================
320 //========================================================================
321 /// Returns the tree handler with the given name
322 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
324 TreeHandlerMapType::const_iterator i;
325 i = GetTreeHandlerMap().find(name);
326 if ( i == GetTreeHandlerMap().end() )
328 GimmickError("TreeHandler '"<<name<<"' does not exist");
333 //========================================================================
334 /// Add the files to the tree handler
335 void Gimmick::AddFiles(const std::string& d,
336 const std::vector<std::string>& filenames)
338 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
340 mImageAdder.SetCurrentDatabase(d);
341 mImageAdder.SetTreeHandler(GetTreeHandler(d));
342 mImageAdder.SetSynchronizer(mSynchronizer);
343 mImageAdder.AddFiles(filenames);
345 //========================================================================
347 //========================================================================
348 /// Add a dir to the local database
349 void Gimmick::AddDir(const std::string& d, const std::string& f,
352 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
353 <<recurse<<std::endl);
355 TreeHandler * handler=GetTreeHandler(d);
356 mImageAdder.SetCurrentDatabase(d);
357 mImageAdder.SetTreeHandler(handler);
358 mImageAdder.SetSynchronizer(mSynchronizer);
359 mImageAdder.AddDirectory(f,recurse);
362 //========================================================================
364 //========================================================================
365 void Gimmick::RemoveFile(const std::string& d,
368 mImageAdder.SetCurrentDatabase(d);
369 mImageAdder.SetSynchronizer(mSynchronizer);
370 mImageAdder.RemoveFile(node);
372 //========================================================================
374 //========================================================================
376 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
378 TreeHandler * handler=GetTreeHandler(d);
379 mImageAdder.SetCurrentDatabase(d);
380 mImageAdder.SetTreeHandler(handler);
381 mImageAdder.SetSynchronizer(mSynchronizer);
382 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
385 //========================================================================
387 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
389 TreeHandler * handler=GetTreeHandler(d);
390 mImageAdder.SetCurrentDatabase(d);
391 mImageAdder.SetTreeHandler(handler);
392 mImageAdder.SetSynchronizer(mSynchronizer);
393 return mImageAdder.Synchronize(repair, checkAttributes);
396 //========================================================================
398 void Gimmick::Print(const std::string& d)
400 GetTreeHandler(d)->GetTree().Print();
402 //========================================================================
404 void Gimmick::GetSetting(const std::string& name, std::string& value)
406 value = mSettings->getValue(name);
408 //========================================================================
410 //========================================================================
412 void Gimmick::GetAttributes(const std::string& d,
413 const std::string& filename,
414 const std::vector<std::string>& params,
415 std::vector<std::string>& results)
417 TreeHandler * handler=GetTreeHandler(d);
418 mImageAdder.SetCurrentDatabase(d);
419 mImageAdder.SetTreeHandler(handler);
420 mImageAdder.SetSynchronizer(mSynchronizer);
421 mImageAdder.GetAttributes(params, filename, results);
423 //========================================================================
425 //========================================================================
427 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
429 mSettings->updateSetting(name,value);
430 mSettings->writeSettingsFile();
432 //========================================================================
434 void Gimmick::DeleteDrive(const std::string& drive)
436 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
437 it!= mTreeHandlerMap.end();
440 mImageAdder.SetTreeHandler(it->second);
441 mImageAdder.DeleteDriveFromMainDB(drive);
443 mImageAdder.SetSynchronizer(mSynchronizer);
444 mImageAdder.DeleteDriveFromOtherDB(drive);
447 //========================================================================
448 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
450 TreeHandler * handler=GetTreeHandler(d);
451 mImageAdder.SetCurrentDatabase(d);
452 mImageAdder.SetTreeHandler(handler);
453 mImageAdder.EditField(node,name,key,val);
455 //========================================================================
457 ////////////////////////////////////////////////////////////////////////
458 // add DB from Settings file //
461 ////////////////////////////////////////////////////////////////////////
462 void Gimmick::addDBSettings()
465 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
467 // split to find all paths
468 std::vector<std::string> paths;
469 std::string separator = ";";
470 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
471 //find first separator
472 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
473 while(std::string::npos != pos || std::string::npos != last_pos)
475 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
476 last_pos = pathSettings.find_first_not_of(separator, pos);
477 pos = pathSettings.find_first_of(separator, last_pos);
480 std::vector<std::string>::iterator it_path = paths.begin();
481 for(; it_path != paths.end(); ++it_path)
483 pos = it_path->find_last_of("\\");
484 last_pos = it_path->find_last_of(".");
485 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
486 addDB(name, it_path->c_str());