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
18 //==============================================================
22 RegisterGimmickMessageTypes();
26 //==============================================================
29 //==============================================================
34 mSettings->writeSettingsFile();
42 //==============================================================
44 //==============================================================
45 void Gimmick::Initialize(const std::string& path)
50 //==============================================================
51 void Gimmick::Initialize()
53 std::string i_nameDB = "Local database";
54 // Create the UserSettings dir if does not exist
55 CreateUserSettingsDirectory();
56 // Sets the current directory to the home dir
57 mCurrentDirectory = GetHomeDirectory();
58 mSynchronizer= new Synchronizer(GetUserSettingsDirectory()+"Shared/gimmick/");
60 mSettings = new Settings(mCurrentDirectory);
62 std::string dbpath = GetLocalDatabasePath();
63 // Create or open local database
64 std::string dpath= mCurrentDirectory + "/.gimmick/Shared/gimmick/localdatabase_Descriptor.dscp";
65 boost::algorithm::replace_all( dpath,
66 INVALID_FILE_SEPARATOR ,
67 VALID_FILE_SEPARATOR);
68 mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
69 // Add it to the TreeHandlerMap
70 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
72 //Add additional DB from user Settings
76 ///////////////////////////////////////////////////////////////////////
77 // add DB to TreeHandler Map //
78 // @param i_name : DB name //
79 // @param i_location : DB location //
81 ////////////////////////////////////////////////////////////////////////
82 void Gimmick::addDB(const std::string &i_name,
83 const std::string &i_location)
85 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
87 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
88 mTreeHandlerMap[i_name]->Open(true);
89 mSettings->addDB(i_location);
93 ///////////////////////////////////////////////////////////////////////////
94 // create a DB from a attributes descriptor file for medical images //
95 // @param i_name : DB name //
96 // @param i_locDesc : location of descriptor file //
97 // @param i_locDB : location of DB //
98 // return : the SQLiteTreeHandler object on DB //
99 /////////////////////////////////////////////////////////////////////////
100 SQLiteTreeHandler* Gimmick::createDB(const std::string &i_name,
101 const std::string &i_locDesc,
102 const std::string &i_locDB)
104 SQLiteTreeHandler* sqlTreeH( new SQLiteTreeHandler(i_locDB) );
105 // Create or open local database
106 if (! boost::filesystem::exists(i_locDB) )
108 std::string mess = "Local database '";
110 mess += "' does not exist : creating it";
111 GimmickMessage(1,mess<<std::endl);
113 // CREATING DB STRUCTURE
114 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
115 if ( ! sqlTreeH->Create(true) )
117 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
119 sqlTreeH->SetAttribute(0,"Name",i_name);
124 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
125 if ( !sqlTreeH->Open(true) )
127 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
133 //==============================================================
134 void Gimmick::Finalize()
137 // delete SQLiteTreeHandler Object
138 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
139 it!= mTreeHandlerMap.end();
145 //==============================================================
147 //================================================================
150 #define VALID_FILE_SEPARATOR "\\"
151 #define INVALID_FILE_SEPARATOR "/"
153 #define INVALID_FILE_SEPARATOR "\\"
154 #define VALID_FILE_SEPARATOR "/"
156 //================================================================
158 //================================================================
159 const std::string& Gimmick::GetHomeDirectory()
161 if (mHomeDirectory.size()==0)
163 #if defined(__GNUC__)
164 mHomeDirectory = getenv("HOME");
165 #elif defined(_WIN32)
166 mHomeDirectory = getenv("USERPROFILE");
169 return mHomeDirectory;
171 //================================================================
172 const std::string& Gimmick::GetUserSettingsDirectory()
174 if (mUserSettingsDirectory.size()==0)
176 mUserSettingsDirectory = GetHomeDirectory();
177 mUserSettingsDirectory += "/.gimmick/";
178 boost::algorithm::replace_all( mUserSettingsDirectory,
179 INVALID_FILE_SEPARATOR ,
180 VALID_FILE_SEPARATOR);
182 return mUserSettingsDirectory;
184 //================================================================
187 //================================================================
188 const std::string& Gimmick::GetLocalDatabasePath()
190 if (mLocalDatabasePath.size()==0)
192 mLocalDatabasePath = GetUserSettingsDirectory();
193 mLocalDatabasePath += "Shared/gimmick/local_database.sqlite3";
194 boost::algorithm::replace_all( mLocalDatabasePath,
195 INVALID_FILE_SEPARATOR ,
196 VALID_FILE_SEPARATOR);
198 return mLocalDatabasePath;
201 //========================================================================
203 //========================================================================
204 void Gimmick::CreateUserSettingsDirectory()
206 //JCP settings dir 02/10/2009
207 const std::string settingsdirectory = GetUserSettingsDirectory();
208 bool isdir = boost::filesystem::is_directory( settingsdirectory );
211 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
212 << "does not exist : creating it"<<std::endl);
214 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
216 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
220 std::string setDir=GetUserSettingsDirectory();
221 boost::algorithm::replace_all( setDir,
222 INVALID_FILE_SEPARATOR ,
223 VALID_FILE_SEPARATOR);
225 boost::filesystem::create_directory( setDir );
227 boost::filesystem::create_directory( setDir );
228 setDir+="localdatabase_Descriptor.dscp";
230 if(!boost::filesystem::is_regular(setDir))
233 //EED int err = GetBinaryDirectory(name, PATH_MAX);
234 crea::System::GetAppPath(name,PATH_MAX);
235 std::cout<<name<<std::endl;
237 std::string path=name;
238 path=path.substr(0,path.size()-1);
239 path=path.substr(0,path.find_last_of("/"));
240 //Creating directories
242 // The following stuff works on Linux, NOT CHECKED on Windows // JPR
245 path+="/bin/Shared/gimmick/localdatabase_Descriptor.dscp";
247 path+="/Shared/gimmick/localdatabase_Descriptor.dscp";
249 std::cout <<"From: " << path <<std::endl;
250 std::cout <<"To: " << setDir <<std::endl;
251 boost::algorithm::replace_all( path,
252 INVALID_FILE_SEPARATOR ,
253 VALID_FILE_SEPARATOR);
254 boost::filesystem::copy_file(path,setDir);
257 //========================================================================
260 //========================================================================
261 /// Sets message level
262 void Gimmick::SetMessageLevel(int l)
264 SetGimmickMessageLevel(l);
266 //========================================================================
268 //========================================================================
269 /// Sets message level
270 void Gimmick::SetDebugMessageLevel(int l)
272 SetGimmickDebugMessageLevel(l);
274 //========================================================================
276 //========================================================================
277 /// Returns the tree handler with the given name
278 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
280 TreeHandlerMapType::const_iterator i;
281 i = GetTreeHandlerMap().find(name);
282 if ( i == GetTreeHandlerMap().end() )
284 GimmickError("TreeHandler '"<<name<<"' does not exist");
289 //========================================================================
290 /// Add the files to the tree handler
291 void Gimmick::AddFiles(const std::string& d,
292 const std::vector<std::string>& filenames)
294 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
296 mImageAdder.SetCurrentDatabase(d);
297 mImageAdder.SetTreeHandler(GetTreeHandler(d));
298 mImageAdder.SetSynchronizer(mSynchronizer);
299 mImageAdder.AddFiles(filenames);
301 //========================================================================
303 //========================================================================
304 /// Add a dir to the local database
305 void Gimmick::AddDir(const std::string& d, const std::string& f,
308 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
309 <<recurse<<std::endl);
311 TreeHandler * handler=GetTreeHandler(d);
312 mImageAdder.SetCurrentDatabase(d);
313 mImageAdder.SetTreeHandler(handler);
314 mImageAdder.SetSynchronizer(mSynchronizer);
315 mImageAdder.AddDirectory(f,recurse);
318 //========================================================================
320 //========================================================================
321 void Gimmick::RemoveFile(const std::string& d,
324 mImageAdder.SetCurrentDatabase(d);
325 mImageAdder.SetSynchronizer(mSynchronizer);
326 mImageAdder.RemoveFile(node);
328 //========================================================================
330 //========================================================================
332 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
334 TreeHandler * handler=GetTreeHandler(d);
335 mImageAdder.SetCurrentDatabase(d);
336 mImageAdder.SetTreeHandler(handler);
337 mImageAdder.SetSynchronizer(mSynchronizer);
338 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
341 //========================================================================
343 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
345 TreeHandler * handler=GetTreeHandler(d);
346 mImageAdder.SetCurrentDatabase(d);
347 mImageAdder.SetTreeHandler(handler);
348 mImageAdder.SetSynchronizer(mSynchronizer);
349 return mImageAdder.Synchronize(repair, checkAttributes);
352 //========================================================================
354 void Gimmick::Print(const std::string& d)
356 GetTreeHandler(d)->GetTree().Print();
358 //========================================================================
360 void Gimmick::GetSetting(const std::string& name, std::string& value)
362 value = mSettings->getValue(name);
364 //========================================================================
366 //========================================================================
368 void Gimmick::GetAttributes(const std::string& d,
369 const std::string& filename,
370 const std::vector<std::string>& params,
371 std::vector<std::string>& results)
373 TreeHandler * handler=GetTreeHandler(d);
374 mImageAdder.SetCurrentDatabase(d);
375 mImageAdder.SetTreeHandler(handler);
376 mImageAdder.SetSynchronizer(mSynchronizer);
377 mImageAdder.GetAttributes(params, filename, results);
379 //========================================================================
381 //========================================================================
383 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
385 mSettings->updateSetting(name,value);
386 mSettings->writeSettingsFile();
388 //========================================================================
390 void Gimmick::DeleteDrive(const std::string& drive)
392 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
393 it!= mTreeHandlerMap.end();
396 mImageAdder.SetTreeHandler(it->second);
397 mImageAdder.DeleteDriveFromMainDB(drive);
399 mImageAdder.SetSynchronizer(mSynchronizer);
400 mImageAdder.DeleteDriveFromOtherDB(drive);
403 //========================================================================
404 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
406 TreeHandler * handler=GetTreeHandler(d);
407 mImageAdder.SetCurrentDatabase(d);
408 mImageAdder.SetTreeHandler(handler);
409 mImageAdder.EditField(node,name,key,val);
411 //========================================================================
413 ////////////////////////////////////////////////////////////////////////
414 // add DB from Settings file //
417 ////////////////////////////////////////////////////////////////////////
418 void Gimmick::addDBSettings()
421 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
423 // split to find all paths
424 std::vector<std::string> paths;
425 std::string separator = ";";
426 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
427 //find first separator
428 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
429 while(std::string::npos != pos || std::string::npos != last_pos)
431 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
432 last_pos = pathSettings.find_first_not_of(separator, pos);
433 pos = pathSettings.find_first_of(separator, last_pos);
436 std::vector<std::string>::iterator it_path = paths.begin();
437 for(; it_path != paths.end(); ++it_path)
439 pos = it_path->find_last_of("\\");
440 last_pos = it_path->find_last_of(".");
441 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
442 addDB(name, it_path->c_str());