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()
219 // std::string st("C:/Documents and Settings/cervenansky/.gimmick/");
220 // boost::algorithm::replace_all( st,
221 // INVALID_FILE_SEPARATOR ,
222 // VALID_FILE_SEPARATOR);
223 //const boost::filesystem::path mpath(st);
224 //C:\Documents and Settings\cervenansky\.gimmick");
225 //if ( !boost::filesystem::exists( path ) ) return ;
226 // boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end
227 // for ( boost::filesystem::directory_iterator itr( path ); itr != end_itr; ++itr )
229 //// If is directory & recurse : do recurse
230 // if ( boost::filesystem::is_directory(itr->status()) )
234 //JCP settings dir 02/10/2009
235 const std::string settingsdirectory = GetUserSettingsDirectory();
236 //boost::algorithm::replace_all( mUserSettingsDirectory,
237 // INVALID_FILE_SEPARATOR ,
238 // VALID_FILE_SEPARATOR);
240 //("C:\\Documents and Settings\\cervenansky\\.gimmick\\"); // settingsdirectory );
242 isdir = boost::filesystem::is_directory(settingsdirectory); // settingsdirectory );
245 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
246 << "does not exist : creating it"<<std::endl);
248 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
250 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
254 std::string setDir=GetUserSettingsDirectory();
255 boost::algorithm::replace_all( setDir,
256 INVALID_FILE_SEPARATOR ,
257 VALID_FILE_SEPARATOR);
259 boost::filesystem::create_directory( setDir );
261 boost::filesystem::create_directory( setDir );
262 setDir+=mLocalDescpName;
264 if(!boost::filesystem::is_regular(setDir))
267 //EED int err = GetBinaryDirectory(name, PATH_MAX);
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/";
282 #if defined(__APPLE__)
283 path+="../../../../shared/gimmick/";
287 path+="../shared/gimmick/";
289 path+= mLocalDescpName;
290 std::cout <<"From: " << path <<std::endl;
291 std::cout <<"To: " << setDir <<std::endl;
292 boost::algorithm::replace_all( path,
293 INVALID_FILE_SEPARATOR ,
294 VALID_FILE_SEPARATOR);
295 boost::filesystem::copy_file(path,setDir);
298 //========================================================================
301 //========================================================================
302 /// Sets message level
303 void Gimmick::SetMessageLevel(int l)
305 SetGimmickMessageLevel(l);
307 //========================================================================
309 //========================================================================
310 /// Sets message level
311 void Gimmick::SetDebugMessageLevel(int l)
313 SetGimmickDebugMessageLevel(l);
315 //========================================================================
317 //========================================================================
318 /// Returns the tree handler with the given name
319 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
321 TreeHandlerMapType::const_iterator i;
322 i = GetTreeHandlerMap().find(name);
323 if ( i == GetTreeHandlerMap().end() )
325 GimmickError("TreeHandler '"<<name<<"' does not exist");
330 //========================================================================
331 /// Add the files to the tree handler
332 void Gimmick::AddFiles(const std::string& d,
333 const std::vector<std::string>& filenames)
335 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
337 mImageAdder.SetCurrentDatabase(d);
338 mImageAdder.SetTreeHandler(GetTreeHandler(d));
339 mImageAdder.SetSynchronizer(mSynchronizer);
340 mImageAdder.AddFiles(filenames);
342 //========================================================================
344 //========================================================================
345 /// Add a dir to the local database
346 void Gimmick::AddDir(const std::string& d, const std::string& f,
349 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
350 <<recurse<<std::endl);
352 TreeHandler * handler=GetTreeHandler(d);
353 mImageAdder.SetCurrentDatabase(d);
354 mImageAdder.SetTreeHandler(handler);
355 mImageAdder.SetSynchronizer(mSynchronizer);
356 mImageAdder.AddDirectory(f,recurse);
359 //========================================================================
361 //========================================================================
362 void Gimmick::RemoveFile(const std::string& d,
365 mImageAdder.SetCurrentDatabase(d);
366 mImageAdder.SetSynchronizer(mSynchronizer);
367 mImageAdder.RemoveFile(node);
369 //========================================================================
371 //========================================================================
373 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
375 TreeHandler * handler=GetTreeHandler(d);
376 mImageAdder.SetCurrentDatabase(d);
377 mImageAdder.SetTreeHandler(handler);
378 mImageAdder.SetSynchronizer(mSynchronizer);
379 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
382 //========================================================================
384 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
386 TreeHandler * handler=GetTreeHandler(d);
387 mImageAdder.SetCurrentDatabase(d);
388 mImageAdder.SetTreeHandler(handler);
389 mImageAdder.SetSynchronizer(mSynchronizer);
390 return mImageAdder.Synchronize(repair, checkAttributes);
393 //========================================================================
395 void Gimmick::Print(const std::string& d)
397 GetTreeHandler(d)->GetTree().Print();
399 //========================================================================
401 void Gimmick::GetSetting(const std::string& name, std::string& value)
403 value = mSettings->getValue(name);
405 //========================================================================
407 //========================================================================
409 void Gimmick::GetAttributes(const std::string& d,
410 const std::string& filename,
411 const std::vector<std::string>& params,
412 std::vector<std::string>& results)
414 TreeHandler * handler=GetTreeHandler(d);
415 mImageAdder.SetCurrentDatabase(d);
416 mImageAdder.SetTreeHandler(handler);
417 mImageAdder.SetSynchronizer(mSynchronizer);
418 mImageAdder.GetAttributes(params, filename, results);
420 //========================================================================
422 //========================================================================
424 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
426 mSettings->updateSetting(name,value);
427 mSettings->writeSettingsFile();
429 //========================================================================
431 void Gimmick::DeleteDrive(const std::string& drive)
433 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
434 it!= mTreeHandlerMap.end();
437 mImageAdder.SetTreeHandler(it->second);
438 mImageAdder.DeleteDriveFromMainDB(drive);
440 mImageAdder.SetSynchronizer(mSynchronizer);
441 mImageAdder.DeleteDriveFromOtherDB(drive);
444 //========================================================================
445 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
447 TreeHandler * handler=GetTreeHandler(d);
448 mImageAdder.SetCurrentDatabase(d);
449 mImageAdder.SetTreeHandler(handler);
450 mImageAdder.EditField(node,name,key,val);
452 //========================================================================
454 ////////////////////////////////////////////////////////////////////////
455 // add DB from Settings file //
458 ////////////////////////////////////////////////////////////////////////
459 void Gimmick::addDBSettings()
462 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
464 // split to find all paths
465 std::vector<std::string> paths;
466 std::string separator = ";";
467 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
468 //find first separator
469 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
470 while(std::string::npos != pos || std::string::npos != last_pos)
472 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
473 last_pos = pathSettings.find_first_not_of(separator, pos);
474 pos = pathSettings.find_first_of(separator, last_pos);
477 std::vector<std::string>::iterator it_path = paths.begin();
478 for(; it_path != paths.end(); ++it_path)
480 pos = it_path->find_last_of("\\");
481 last_pos = it_path->find_last_of(".");
482 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
483 addDB(name, it_path->c_str());