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();
69 // Create or open local database
70 std::string dpath= mCurrentDirectory + "/.gimmick/Shared/gimmick/" + mLocalDescpName;
71 boost::algorithm::replace_all( dpath,
72 INVALID_FILE_SEPARATOR ,
73 VALID_FILE_SEPARATOR);
74 mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
75 // Add it to the TreeHandlerMap
76 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
78 //Add additional DB from user Settings
82 ///////////////////////////////////////////////////////////////////////
83 // add DB to TreeHandler Map //
84 // @param i_name : DB name //
85 // @param i_location : DB location //
87 ////////////////////////////////////////////////////////////////////////
88 void Gimmick::addDB(const std::string &i_name,
89 const std::string &i_location)
91 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
93 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
94 mTreeHandlerMap[i_name]->Open(true);
95 mSettings->addDB(i_location);
99 ///////////////////////////////////////////////////////////////////////////
100 // create a DB from a attributes descriptor file for medical images //
101 // @param i_name : DB name //
102 // @param i_locDesc : location of descriptor file //
103 // @param i_locDB : location of DB //
104 // return : the SQLiteTreeHandler object on DB //
105 /////////////////////////////////////////////////////////////////////////
106 SQLiteTreeHandler* Gimmick::createDB(const std::string &i_name,
107 const std::string &i_locDesc,
108 const std::string &i_locDB)
110 SQLiteTreeHandler* sqlTreeH( new SQLiteTreeHandler(i_locDB) );
111 // Create or open local database
112 if (! boost::filesystem::exists(i_locDB) )
114 std::string mess = "Local database '";
116 mess += "' does not exist : creating it";
117 GimmickMessage(1,mess<<std::endl);
119 // CREATING DB STRUCTURE
120 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
121 if ( ! sqlTreeH->Create(true) )
123 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
125 sqlTreeH->SetAttribute(0,"Name",i_name);
131 GimmickDebugMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
132 if ( !sqlTreeH->Open(true) )
134 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
140 //==============================================================
141 void Gimmick::Finalize()
143 if(mTreeHandlerMap.size() >0)
145 // delete SQLiteTreeHandler Object
146 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
147 it!= mTreeHandlerMap.end();
154 //==============================================================
156 //================================================================
159 #define VALID_FILE_SEPARATOR "\\"
160 #define INVALID_FILE_SEPARATOR "/"
162 #define INVALID_FILE_SEPARATOR "\\"
163 #define VALID_FILE_SEPARATOR "/"
165 //================================================================
167 //================================================================
168 const std::string& Gimmick::GetHomeDirectory()
170 if (mHomeDirectory.size()==0)
172 #if defined(__GNUC__)
173 mHomeDirectory = getenv("HOME");
174 #elif defined(_WIN32)
175 mHomeDirectory = getenv("USERPROFILE");
178 return mHomeDirectory;
180 //================================================================
181 const std::string& Gimmick::GetUserSettingsDirectory()
183 if (mUserSettingsDirectory.size()==0)
185 mUserSettingsDirectory = GetHomeDirectory();
186 mUserSettingsDirectory += "/.gimmick/";
187 boost::algorithm::replace_all( mUserSettingsDirectory,
188 INVALID_FILE_SEPARATOR ,
189 VALID_FILE_SEPARATOR);
191 return mUserSettingsDirectory;
193 //================================================================
196 //================================================================
197 const std::string& Gimmick::GetLocalDatabasePath()
199 if (mLocalDatabasePath.size()==0)
201 mLocalDatabasePath = GetUserSettingsDirectory();
202 mLocalDatabasePath += "Shared/gimmick/";
203 mLocalDatabasePath += mLocalDBName;
204 mLocalDatabasePath +=".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 // std::string st("C:/Documents and Settings/cervenansky/.gimmick/");
218 // boost::algorithm::replace_all( st,
219 // INVALID_FILE_SEPARATOR ,
220 // VALID_FILE_SEPARATOR);
221 //const boost::filesystem::path mpath(st);
222 //C:\Documents and Settings\cervenansky\.gimmick");
223 //if ( !boost::filesystem::exists( path ) ) return ;
224 // boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end
225 // for ( boost::filesystem::directory_iterator itr( path ); itr != end_itr; ++itr )
227 //// If is directory & recurse : do recurse
228 // if ( boost::filesystem::is_directory(itr->status()) )
232 //JCP settings dir 02/10/2009
233 const std::string settingsdirectory = GetUserSettingsDirectory();
234 //boost::algorithm::replace_all( mUserSettingsDirectory,
235 // INVALID_FILE_SEPARATOR ,
236 // VALID_FILE_SEPARATOR);
238 //("C:\\Documents and Settings\\cervenansky\\.gimmick\\"); // settingsdirectory );
240 isdir = boost::filesystem::is_directory(settingsdirectory); // settingsdirectory );
243 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
244 << "does not exist : creating it"<<std::endl);
246 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
248 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
252 std::string setDir=GetUserSettingsDirectory();
253 boost::algorithm::replace_all( setDir,
254 INVALID_FILE_SEPARATOR ,
255 VALID_FILE_SEPARATOR);
257 boost::filesystem::create_directory( setDir );
259 boost::filesystem::create_directory( setDir );
260 setDir+=mLocalDescpName;
262 if(!boost::filesystem::is_regular(setDir))
265 //EED int err = GetBinaryDirectory(name, PATH_MAX);
266 crea::System::GetAppPath(name,PATH_MAX);
267 std::cout<<name<<std::endl;
269 std::string path=name;
270 path=path.substr(0,path.size()-1);
271 path=path.substr(0,path.find_last_of("/"));
272 //Creating directories
274 // The following stuff works on Linux, NOT CHECKED on Windows // JPR
277 path+="/bin/Shared/gimmick/";
278 path+=mLocalDescpName;
280 path+="/Shared/gimmick/";
281 path+= mLocalDescpName;
283 std::cout <<"From: " << path <<std::endl;
284 std::cout <<"To: " << setDir <<std::endl;
285 boost::algorithm::replace_all( path,
286 INVALID_FILE_SEPARATOR ,
287 VALID_FILE_SEPARATOR);
288 boost::filesystem::copy_file(path,setDir);
291 //========================================================================
294 //========================================================================
295 /// Sets message level
296 void Gimmick::SetMessageLevel(int l)
298 SetGimmickMessageLevel(l);
300 //========================================================================
302 //========================================================================
303 /// Sets message level
304 void Gimmick::SetDebugMessageLevel(int l)
306 SetGimmickDebugMessageLevel(l);
308 //========================================================================
310 //========================================================================
311 /// Returns the tree handler with the given name
312 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
314 TreeHandlerMapType::const_iterator i;
315 i = GetTreeHandlerMap().find(name);
316 if ( i == GetTreeHandlerMap().end() )
318 GimmickError("TreeHandler '"<<name<<"' does not exist");
323 //========================================================================
324 /// Add the files to the tree handler
325 void Gimmick::AddFiles(const std::string& d,
326 const std::vector<std::string>& filenames)
328 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
330 mImageAdder.SetCurrentDatabase(d);
331 mImageAdder.SetTreeHandler(GetTreeHandler(d));
332 mImageAdder.SetSynchronizer(mSynchronizer);
333 mImageAdder.AddFiles(filenames);
335 //========================================================================
337 //========================================================================
338 /// Add a dir to the local database
339 void Gimmick::AddDir(const std::string& d, const std::string& f,
342 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
343 <<recurse<<std::endl);
345 TreeHandler * handler=GetTreeHandler(d);
346 mImageAdder.SetCurrentDatabase(d);
347 mImageAdder.SetTreeHandler(handler);
348 mImageAdder.SetSynchronizer(mSynchronizer);
349 mImageAdder.AddDirectory(f,recurse);
352 //========================================================================
354 //========================================================================
355 void Gimmick::RemoveFile(const std::string& d,
358 mImageAdder.SetCurrentDatabase(d);
359 mImageAdder.SetSynchronizer(mSynchronizer);
360 mImageAdder.RemoveFile(node);
362 //========================================================================
364 //========================================================================
366 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
368 TreeHandler * handler=GetTreeHandler(d);
369 mImageAdder.SetCurrentDatabase(d);
370 mImageAdder.SetTreeHandler(handler);
371 mImageAdder.SetSynchronizer(mSynchronizer);
372 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
375 //========================================================================
377 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
379 TreeHandler * handler=GetTreeHandler(d);
380 mImageAdder.SetCurrentDatabase(d);
381 mImageAdder.SetTreeHandler(handler);
382 mImageAdder.SetSynchronizer(mSynchronizer);
383 return mImageAdder.Synchronize(repair, checkAttributes);
386 //========================================================================
388 void Gimmick::Print(const std::string& d)
390 GetTreeHandler(d)->GetTree().Print();
392 //========================================================================
394 void Gimmick::GetSetting(const std::string& name, std::string& value)
396 value = mSettings->getValue(name);
398 //========================================================================
400 //========================================================================
402 void Gimmick::GetAttributes(const std::string& d,
403 const std::string& filename,
404 const std::vector<std::string>& params,
405 std::vector<std::string>& results)
407 TreeHandler * handler=GetTreeHandler(d);
408 mImageAdder.SetCurrentDatabase(d);
409 mImageAdder.SetTreeHandler(handler);
410 mImageAdder.SetSynchronizer(mSynchronizer);
411 mImageAdder.GetAttributes(params, filename, results);
413 //========================================================================
415 //========================================================================
417 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
419 mSettings->updateSetting(name,value);
420 mSettings->writeSettingsFile();
422 //========================================================================
424 void Gimmick::DeleteDrive(const std::string& drive)
426 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
427 it!= mTreeHandlerMap.end();
430 mImageAdder.SetTreeHandler(it->second);
431 mImageAdder.DeleteDriveFromMainDB(drive);
433 mImageAdder.SetSynchronizer(mSynchronizer);
434 mImageAdder.DeleteDriveFromOtherDB(drive);
437 //========================================================================
438 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
440 TreeHandler * handler=GetTreeHandler(d);
441 mImageAdder.SetCurrentDatabase(d);
442 mImageAdder.SetTreeHandler(handler);
443 mImageAdder.EditField(node,name,key,val);
445 //========================================================================
447 ////////////////////////////////////////////////////////////////////////
448 // add DB from Settings file //
451 ////////////////////////////////////////////////////////////////////////
452 void Gimmick::addDBSettings()
455 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
457 // split to find all paths
458 std::vector<std::string> paths;
459 std::string separator = ";";
460 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
461 //find first separator
462 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
463 while(std::string::npos != pos || std::string::npos != last_pos)
465 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
466 last_pos = pathSettings.find_first_not_of(separator, pos);
467 pos = pathSettings.find_first_of(separator, last_pos);
470 std::vector<std::string>::iterator it_path = paths.begin();
471 for(; it_path != paths.end(); ++it_path)
473 pos = it_path->find_last_of("\\");
474 last_pos = it_path->find_last_of(".");
475 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
476 addDB(name, it_path->c_str());