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/";
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/";
280 path+=mLocalDescpName;
282 path+="/Shared/gimmick/";
283 path+= mLocalDescpName;
285 std::cout <<"From: " << path <<std::endl;
286 std::cout <<"To: " << setDir <<std::endl;
287 boost::algorithm::replace_all( path,
288 INVALID_FILE_SEPARATOR ,
289 VALID_FILE_SEPARATOR);
290 boost::filesystem::copy_file(path,setDir);
293 //========================================================================
296 //========================================================================
297 /// Sets message level
298 void Gimmick::SetMessageLevel(int l)
300 SetGimmickMessageLevel(l);
302 //========================================================================
304 //========================================================================
305 /// Sets message level
306 void Gimmick::SetDebugMessageLevel(int l)
308 SetGimmickDebugMessageLevel(l);
310 //========================================================================
312 //========================================================================
313 /// Returns the tree handler with the given name
314 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
316 TreeHandlerMapType::const_iterator i;
317 i = GetTreeHandlerMap().find(name);
318 if ( i == GetTreeHandlerMap().end() )
320 GimmickError("TreeHandler '"<<name<<"' does not exist");
325 //========================================================================
326 /// Add the files to the tree handler
327 void Gimmick::AddFiles(const std::string& d,
328 const std::vector<std::string>& filenames)
330 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
332 mImageAdder.SetCurrentDatabase(d);
333 mImageAdder.SetTreeHandler(GetTreeHandler(d));
334 mImageAdder.SetSynchronizer(mSynchronizer);
335 mImageAdder.AddFiles(filenames);
337 //========================================================================
339 //========================================================================
340 /// Add a dir to the local database
341 void Gimmick::AddDir(const std::string& d, const std::string& f,
344 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
345 <<recurse<<std::endl);
347 TreeHandler * handler=GetTreeHandler(d);
348 mImageAdder.SetCurrentDatabase(d);
349 mImageAdder.SetTreeHandler(handler);
350 mImageAdder.SetSynchronizer(mSynchronizer);
351 mImageAdder.AddDirectory(f,recurse);
354 //========================================================================
356 //========================================================================
357 void Gimmick::RemoveFile(const std::string& d,
360 mImageAdder.SetCurrentDatabase(d);
361 mImageAdder.SetSynchronizer(mSynchronizer);
362 mImageAdder.RemoveFile(node);
364 //========================================================================
366 //========================================================================
368 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
370 TreeHandler * handler=GetTreeHandler(d);
371 mImageAdder.SetCurrentDatabase(d);
372 mImageAdder.SetTreeHandler(handler);
373 mImageAdder.SetSynchronizer(mSynchronizer);
374 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
377 //========================================================================
379 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
381 TreeHandler * handler=GetTreeHandler(d);
382 mImageAdder.SetCurrentDatabase(d);
383 mImageAdder.SetTreeHandler(handler);
384 mImageAdder.SetSynchronizer(mSynchronizer);
385 return mImageAdder.Synchronize(repair, checkAttributes);
388 //========================================================================
390 void Gimmick::Print(const std::string& d)
392 GetTreeHandler(d)->GetTree().Print();
394 //========================================================================
396 void Gimmick::GetSetting(const std::string& name, std::string& value)
398 value = mSettings->getValue(name);
400 //========================================================================
402 //========================================================================
404 void Gimmick::GetAttributes(const std::string& d,
405 const std::string& filename,
406 const std::vector<std::string>& params,
407 std::vector<std::string>& results)
409 TreeHandler * handler=GetTreeHandler(d);
410 mImageAdder.SetCurrentDatabase(d);
411 mImageAdder.SetTreeHandler(handler);
412 mImageAdder.SetSynchronizer(mSynchronizer);
413 mImageAdder.GetAttributes(params, filename, results);
415 //========================================================================
417 //========================================================================
419 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
421 mSettings->updateSetting(name,value);
422 mSettings->writeSettingsFile();
424 //========================================================================
426 void Gimmick::DeleteDrive(const std::string& drive)
428 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
429 it!= mTreeHandlerMap.end();
432 mImageAdder.SetTreeHandler(it->second);
433 mImageAdder.DeleteDriveFromMainDB(drive);
435 mImageAdder.SetSynchronizer(mSynchronizer);
436 mImageAdder.DeleteDriveFromOtherDB(drive);
439 //========================================================================
440 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
442 TreeHandler * handler=GetTreeHandler(d);
443 mImageAdder.SetCurrentDatabase(d);
444 mImageAdder.SetTreeHandler(handler);
445 mImageAdder.EditField(node,name,key,val);
447 //========================================================================
449 ////////////////////////////////////////////////////////////////////////
450 // add DB from Settings file //
453 ////////////////////////////////////////////////////////////////////////
454 void Gimmick::addDBSettings()
457 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
459 // split to find all paths
460 std::vector<std::string> paths;
461 std::string separator = ";";
462 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
463 //find first separator
464 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
465 while(std::string::npos != pos || std::string::npos != last_pos)
467 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
468 last_pos = pathSettings.find_first_not_of(separator, pos);
469 pos = pathSettings.find_first_of(separator, last_pos);
472 std::vector<std::string>::iterator it_path = paths.begin();
473 for(; it_path != paths.end(); ++it_path)
475 pos = it_path->find_last_of("\\");
476 last_pos = it_path->find_last_of(".");
477 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
478 addDB(name, it_path->c_str());