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
16 //==============================================================
20 RegisterGimmickMessageTypes();
23 mLocalDescpName = "localdatabase_Descriptor.dscp";
24 mLocalDBName = "Local database";
26 //==============================================================
29 //==============================================================
34 mSettings->writeSettingsFile();
42 //==============================================================
44 //==============================================================
45 void Gimmick::Initialize(const std::string i_namedescp, const std::string i_namedb)
47 mLocalDescpName = i_namedescp;
48 mLocalDBName = i_namedb;
52 //==============================================================
53 void Gimmick::Initialize()
55 std::string i_nameDB = mLocalDBName;
56 // Create the UserSettings dir if does not exist
57 CreateUserSettingsDirectory();
58 // Sets the current directory to the home dir
59 mCurrentDirectory = GetHomeDirectory();
60 mSynchronizer= new Synchronizer(GetUserSettingsDirectory()+"Shared/gimmick/");
62 mSettings = new Settings(mCurrentDirectory);
64 std::string dbpath = GetLocalDatabasePath();
65 // Create or open local database
66 std::string dpath= mCurrentDirectory + "/.gimmick/Shared/gimmick/" + mLocalDescpName;
67 boost::algorithm::replace_all( dpath,
68 INVALID_FILE_SEPARATOR ,
69 VALID_FILE_SEPARATOR);
70 mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
71 // Add it to the TreeHandlerMap
72 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
74 //Add additional DB from user Settings
78 ///////////////////////////////////////////////////////////////////////
79 // add DB to TreeHandler Map //
80 // @param i_name : DB name //
81 // @param i_location : DB location //
83 ////////////////////////////////////////////////////////////////////////
84 void Gimmick::addDB(const std::string &i_name,
85 const std::string &i_location)
87 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
89 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
90 mTreeHandlerMap[i_name]->Open(true);
91 mSettings->addDB(i_location);
95 ///////////////////////////////////////////////////////////////////////////
96 // create a DB from a attributes descriptor file for medical images //
97 // @param i_name : DB name //
98 // @param i_locDesc : location of descriptor file //
99 // @param i_locDB : location of DB //
100 // return : the SQLiteTreeHandler object on DB //
101 /////////////////////////////////////////////////////////////////////////
102 SQLiteTreeHandler* Gimmick::createDB(const std::string &i_name,
103 const std::string &i_locDesc,
104 const std::string &i_locDB)
106 SQLiteTreeHandler* sqlTreeH( new SQLiteTreeHandler(i_locDB) );
107 // Create or open local database
108 if (! boost::filesystem::exists(i_locDB) )
110 std::string mess = "Local database '";
112 mess += "' does not exist : creating it";
113 GimmickMessage(1,mess<<std::endl);
115 // CREATING DB STRUCTURE
116 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
117 if ( ! sqlTreeH->Create(true) )
119 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
121 sqlTreeH->SetAttribute(0,"Name",i_name);
127 GimmickDebugMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
128 if ( !sqlTreeH->Open(true) )
130 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
136 //==============================================================
137 void Gimmick::Finalize()
139 if(mTreeHandlerMap.size() >0)
141 // delete SQLiteTreeHandler Object
142 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
143 it!= mTreeHandlerMap.end();
150 //==============================================================
152 //================================================================
155 #define VALID_FILE_SEPARATOR "\\"
156 #define INVALID_FILE_SEPARATOR "/"
158 #define INVALID_FILE_SEPARATOR "\\"
159 #define VALID_FILE_SEPARATOR "/"
161 //================================================================
163 //================================================================
164 const std::string& Gimmick::GetHomeDirectory()
166 if (mHomeDirectory.size()==0)
168 #if defined(__GNUC__)
169 mHomeDirectory = getenv("HOME");
170 #elif defined(_WIN32)
171 mHomeDirectory = getenv("USERPROFILE");
174 return mHomeDirectory;
176 //================================================================
177 const std::string& Gimmick::GetUserSettingsDirectory()
179 if (mUserSettingsDirectory.size()==0)
181 mUserSettingsDirectory = GetHomeDirectory();
182 mUserSettingsDirectory += "/.gimmick/";
183 boost::algorithm::replace_all( mUserSettingsDirectory,
184 INVALID_FILE_SEPARATOR ,
185 VALID_FILE_SEPARATOR);
187 return mUserSettingsDirectory;
189 //================================================================
192 //================================================================
193 const std::string& Gimmick::GetLocalDatabasePath()
195 if (mLocalDatabasePath.size()==0)
197 mLocalDatabasePath = GetUserSettingsDirectory();
198 mLocalDatabasePath += "Shared/gimmick/";
199 mLocalDatabasePath += mLocalDBName;
200 mLocalDatabasePath +=".sqlite3";
201 boost::algorithm::replace_all( mLocalDatabasePath,
202 INVALID_FILE_SEPARATOR ,
203 VALID_FILE_SEPARATOR);
205 return mLocalDatabasePath;
208 //========================================================================
210 //========================================================================
211 void Gimmick::CreateUserSettingsDirectory()
213 // std::string st("C:/Documents and Settings/cervenansky/.gimmick/");
214 // boost::algorithm::replace_all( st,
215 // INVALID_FILE_SEPARATOR ,
216 // VALID_FILE_SEPARATOR);
217 //const boost::filesystem::path mpath(st);
218 //C:\Documents and Settings\cervenansky\.gimmick");
219 //if ( !boost::filesystem::exists( path ) ) return ;
220 // boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end
221 // for ( boost::filesystem::directory_iterator itr( path ); itr != end_itr; ++itr )
223 //// If is directory & recurse : do recurse
224 // if ( boost::filesystem::is_directory(itr->status()) )
228 //JCP settings dir 02/10/2009
229 const std::string settingsdirectory = GetUserSettingsDirectory();
230 //boost::algorithm::replace_all( mUserSettingsDirectory,
231 // INVALID_FILE_SEPARATOR ,
232 // VALID_FILE_SEPARATOR);
234 //("C:\\Documents and Settings\\cervenansky\\.gimmick\\"); // settingsdirectory );
236 isdir = boost::filesystem::is_directory(settingsdirectory); // settingsdirectory );
239 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
240 << "does not exist : creating it"<<std::endl);
242 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
244 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
248 std::string setDir=GetUserSettingsDirectory();
249 boost::algorithm::replace_all( setDir,
250 INVALID_FILE_SEPARATOR ,
251 VALID_FILE_SEPARATOR);
253 boost::filesystem::create_directory( setDir );
255 boost::filesystem::create_directory( setDir );
256 setDir+=mLocalDescpName;
258 if(!boost::filesystem::is_regular(setDir))
261 //EED int err = GetBinaryDirectory(name, PATH_MAX);
262 crea::System::GetAppPath(name,PATH_MAX);
263 std::cout<<name<<std::endl;
265 std::string path=name;
266 path=path.substr(0,path.size()-1);
267 path=path.substr(0,path.find_last_of("/"));
268 //Creating directories
270 // The following stuff works on Linux, NOT CHECKED on Windows // JPR
273 path+="/bin/Shared/gimmick/";
274 path+=mLocalDescpName;
276 path+="/Shared/gimmick/";
277 path+= mLocalDescpName;
279 std::cout <<"From: " << path <<std::endl;
280 std::cout <<"To: " << setDir <<std::endl;
281 boost::algorithm::replace_all( path,
282 INVALID_FILE_SEPARATOR ,
283 VALID_FILE_SEPARATOR);
284 boost::filesystem::copy_file(path,setDir);
287 //========================================================================
290 //========================================================================
291 /// Sets message level
292 void Gimmick::SetMessageLevel(int l)
294 SetGimmickMessageLevel(l);
296 //========================================================================
298 //========================================================================
299 /// Sets message level
300 void Gimmick::SetDebugMessageLevel(int l)
302 SetGimmickDebugMessageLevel(l);
304 //========================================================================
306 //========================================================================
307 /// Returns the tree handler with the given name
308 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
310 TreeHandlerMapType::const_iterator i;
311 i = GetTreeHandlerMap().find(name);
312 if ( i == GetTreeHandlerMap().end() )
314 GimmickError("TreeHandler '"<<name<<"' does not exist");
319 //========================================================================
320 /// Add the files to the tree handler
321 void Gimmick::AddFiles(const std::string& d,
322 const std::vector<std::string>& filenames)
324 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
326 mImageAdder.SetCurrentDatabase(d);
327 mImageAdder.SetTreeHandler(GetTreeHandler(d));
328 mImageAdder.SetSynchronizer(mSynchronizer);
329 mImageAdder.AddFiles(filenames);
331 //========================================================================
333 //========================================================================
334 /// Add a dir to the local database
335 void Gimmick::AddDir(const std::string& d, const std::string& f,
338 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
339 <<recurse<<std::endl);
341 TreeHandler * handler=GetTreeHandler(d);
342 mImageAdder.SetCurrentDatabase(d);
343 mImageAdder.SetTreeHandler(handler);
344 mImageAdder.SetSynchronizer(mSynchronizer);
345 mImageAdder.AddDirectory(f,recurse);
348 //========================================================================
350 //========================================================================
351 void Gimmick::RemoveFile(const std::string& d,
354 mImageAdder.SetCurrentDatabase(d);
355 mImageAdder.SetSynchronizer(mSynchronizer);
356 mImageAdder.RemoveFile(node);
358 //========================================================================
360 //========================================================================
362 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
364 TreeHandler * handler=GetTreeHandler(d);
365 mImageAdder.SetCurrentDatabase(d);
366 mImageAdder.SetTreeHandler(handler);
367 mImageAdder.SetSynchronizer(mSynchronizer);
368 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
371 //========================================================================
373 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
375 TreeHandler * handler=GetTreeHandler(d);
376 mImageAdder.SetCurrentDatabase(d);
377 mImageAdder.SetTreeHandler(handler);
378 mImageAdder.SetSynchronizer(mSynchronizer);
379 return mImageAdder.Synchronize(repair, checkAttributes);
382 //========================================================================
384 void Gimmick::Print(const std::string& d)
386 GetTreeHandler(d)->GetTree().Print();
388 //========================================================================
390 void Gimmick::GetSetting(const std::string& name, std::string& value)
392 value = mSettings->getValue(name);
394 //========================================================================
396 //========================================================================
398 void Gimmick::GetAttributes(const std::string& d,
399 const std::string& filename,
400 const std::vector<std::string>& params,
401 std::vector<std::string>& results)
403 TreeHandler * handler=GetTreeHandler(d);
404 mImageAdder.SetCurrentDatabase(d);
405 mImageAdder.SetTreeHandler(handler);
406 mImageAdder.SetSynchronizer(mSynchronizer);
407 mImageAdder.GetAttributes(params, filename, results);
409 //========================================================================
411 //========================================================================
413 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
415 mSettings->updateSetting(name,value);
416 mSettings->writeSettingsFile();
418 //========================================================================
420 void Gimmick::DeleteDrive(const std::string& drive)
422 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
423 it!= mTreeHandlerMap.end();
426 mImageAdder.SetTreeHandler(it->second);
427 mImageAdder.DeleteDriveFromMainDB(drive);
429 mImageAdder.SetSynchronizer(mSynchronizer);
430 mImageAdder.DeleteDriveFromOtherDB(drive);
433 //========================================================================
434 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
436 TreeHandler * handler=GetTreeHandler(d);
437 mImageAdder.SetCurrentDatabase(d);
438 mImageAdder.SetTreeHandler(handler);
439 mImageAdder.EditField(node,name,key,val);
441 //========================================================================
443 ////////////////////////////////////////////////////////////////////////
444 // add DB from Settings file //
447 ////////////////////////////////////////////////////////////////////////
448 void Gimmick::addDBSettings()
451 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
453 // split to find all paths
454 std::vector<std::string> paths;
455 std::string separator = ";";
456 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
457 //find first separator
458 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
459 while(std::string::npos != pos || std::string::npos != last_pos)
461 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
462 last_pos = pathSettings.find_first_not_of(separator, pos);
463 pos = pathSettings.find_first_of(separator, last_pos);
466 std::vector<std::string>::iterator it_path = paths.begin();
467 for(; it_path != paths.end(); ++it_path)
469 pos = it_path->find_last_of("\\");
470 last_pos = it_path->find_last_of(".");
471 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
472 addDB(name, it_path->c_str());