1 #include <creaImageIOGimmick.h>
3 #include <creaImageIOSystem.h>
4 #include <boost/filesystem.hpp>
5 #include <boost/algorithm/string.hpp>
7 #ifndef PATH_MAX // If not defined yet : do it
15 //==============================================================
19 RegisterGimmickMessageTypes();
23 //==============================================================
27 //==============================================================
32 mSettings->writeSettingsFile();
40 //==============================================================
42 //==============================================================
43 void Gimmick::Initialize(const std::string& path)
48 //==============================================================
49 void Gimmick::Initialize()
51 std::string i_nameDB = "Local database";
52 // Create the UserSettings dir if does not exist
53 CreateUserSettingsDirectory();
54 // Sets the current directory to the home dir
55 mCurrentDirectory = GetHomeDirectory();
56 mSynchronizer= new Synchronizer(GetUserSettingsDirectory()+"Shared/gimmick/");
58 mSettings = new Settings(mCurrentDirectory);
61 std::string dbpath = GetLocalDatabasePath();
62 // Create or open local database
63 std::string dpath= mCurrentDirectory + "/.gimmick/Shared/gimmick/localdatabase_Descriptor.scp";
64 boost::algorithm::replace_all( dpath,
65 INVALID_FILE_SEPARATOR ,
66 VALID_FILE_SEPARATOR);
67 mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
68 // Add it to the TreeHandlerMap
69 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
71 //Add additional DB from user Settings
77 ///////////////////////////////////////////////////////////////////////
78 // add DB to TreeHandler Map //
79 // @param i_name : DB name //
80 // @param i_location : DB location //
82 ///////////////////////////////////////////////////////////////////////
83 void Gimmick::addDB(const std::string &i_name,
84 const std::string &i_location)
86 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
88 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
89 mTreeHandlerMap[i_name]->Open(true);
90 mSettings->addDB(i_location);
94 ///////////////////////////////////////////////////////////////////////////
95 // create a DB from a attributes descriptor file for medical images //
96 // @param i_name : DB name //
97 // @param i_locDesc : location of descriptor file //
98 // @param i_locDB : location of DB //
99 // return : the SQLiteTreeHandler object on DB //
100 /////////////////////////////////////////////////////////////////////////
101 SQLiteTreeHandler *Gimmick::createDB(const std::string &i_name,
102 const std::string &i_locDesc,
103 const std::string &i_locDB)
105 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
106 // Create or open local database
107 if (! boost::filesystem::exists(i_locDB) )
109 std::string mess = "Local database '";
111 mess += "' does not exist : creating it";
112 GimmickMessage(1,mess<<std::endl);
114 // CREATING DB STRUCTURE
115 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
116 if ( ! sqlTreeH->Create(true) )
118 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
120 sqlTreeH->SetAttribute(0,"Name",i_name);
125 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
126 if ( !sqlTreeH->Open(true) )
128 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
135 //==============================================================
136 void Gimmick::Finalize()
139 // delete SQLiteTreeHandler Object
140 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
141 it!= mTreeHandlerMap.end(); ++it)
146 //==============================================================
148 //================================================================
151 #define VALID_FILE_SEPARATOR "\\"
152 #define INVALID_FILE_SEPARATOR "/"
154 #define INVALID_FILE_SEPARATOR "\\"
155 #define VALID_FILE_SEPARATOR "/"
157 //================================================================
159 //================================================================
160 const std::string& Gimmick::GetHomeDirectory()
162 if (mHomeDirectory.size()==0)
164 #if defined(__GNUC__)
165 mHomeDirectory = getenv("HOME");
166 #elif defined(_WIN32)
167 mHomeDirectory = getenv("USERPROFILE");
170 return mHomeDirectory;
172 //================================================================
173 const std::string& Gimmick::GetUserSettingsDirectory()
175 if (mUserSettingsDirectory.size()==0)
177 mUserSettingsDirectory = GetHomeDirectory();
178 mUserSettingsDirectory += "/.gimmick/";
179 boost::algorithm::replace_all( mUserSettingsDirectory,
180 INVALID_FILE_SEPARATOR ,
181 VALID_FILE_SEPARATOR);
183 return mUserSettingsDirectory;
185 //================================================================
188 //================================================================
189 const std::string& Gimmick::GetLocalDatabasePath()
191 if (mLocalDatabasePath.size()==0)
193 mLocalDatabasePath = GetUserSettingsDirectory();
194 mLocalDatabasePath += "Shared/gimmick/local_database.sqlite3";
195 boost::algorithm::replace_all( mLocalDatabasePath,
196 INVALID_FILE_SEPARATOR ,
197 VALID_FILE_SEPARATOR);
199 return mLocalDatabasePath;
203 //========================================================================
205 //========================================================================
206 void Gimmick::CreateUserSettingsDirectory()
208 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
210 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
211 << "does not exist : creating it"<<std::endl);
213 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
215 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
219 std::string setDir=GetUserSettingsDirectory();
220 boost::algorithm::replace_all( setDir,
221 INVALID_FILE_SEPARATOR ,
222 VALID_FILE_SEPARATOR);
224 boost::filesystem::create_directory( setDir );
226 boost::filesystem::create_directory( setDir );
227 setDir+="localdatabase_Descriptor.txt";
229 if(!boost::filesystem::is_regular(setDir))
232 //EED int err = GetBinaryDirectory(name, PATH_MAX);
233 crea::System::GetAppPath(name,PATH_MAX);
234 std::cout<<name<<std::endl;
236 std::string path=name;
237 path=path.substr(0,path.size()-1);
238 path=path.substr(0,path.find_last_of("/"));
239 //Creating directories
240 path+="/bin/Shared/gimmick/localdatabase_Descriptor.dscp";
241 std::cout<<"From: "<<path<<std::endl;
242 std::cout<<"To: "<<setDir<<std::endl;
243 boost::algorithm::replace_all( path,
244 INVALID_FILE_SEPARATOR ,
245 VALID_FILE_SEPARATOR);
246 boost::filesystem::copy_file(path,setDir);
249 //========================================================================
252 //========================================================================
253 /// Sets message level
254 void Gimmick::SetMessageLevel(int l)
256 SetGimmickMessageLevel(l);
258 //========================================================================
260 //========================================================================
261 /// Sets message level
262 void Gimmick::SetDebugMessageLevel(int l)
264 SetGimmickDebugMessageLevel(l);
266 //========================================================================
268 //========================================================================
269 /// Returns the tree handler with the given name
270 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
272 TreeHandlerMapType::const_iterator i;
273 i = GetTreeHandlerMap().find(name);
274 if ( i == GetTreeHandlerMap().end() )
276 GimmickError("TreeHandler '"<<name<<"' does not exist");
283 //========================================================================
284 /// Add the files to the tree handler
285 void Gimmick::AddFiles(const std::string& d,
286 const std::vector<std::string>& filenames)
288 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
290 mImageAdder.SetCurrentDatabase(d);
291 mImageAdder.SetTreeHandler(GetTreeHandler(d));
292 mImageAdder.SetSynchronizer(mSynchronizer);
293 mImageAdder.AddFiles(filenames);
296 //========================================================================
298 //========================================================================
299 /// Add a dir to the local database
300 void Gimmick::AddDir(const std::string& d, const std::string& f,
303 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
304 <<recurse<<std::endl);
306 TreeHandler * handler=GetTreeHandler(d);
307 mImageAdder.SetCurrentDatabase(d);
308 mImageAdder.SetTreeHandler(handler);
309 mImageAdder.SetSynchronizer(mSynchronizer);
310 mImageAdder.AddDirectory(f,recurse);
313 //========================================================================
315 //========================================================================
316 void Gimmick::RemoveFile(const std::string& d,
319 mImageAdder.SetCurrentDatabase(d);
320 mImageAdder.SetSynchronizer(mSynchronizer);
321 mImageAdder.RemoveFile(node);
323 //========================================================================
325 //========================================================================
327 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
329 TreeHandler * handler=GetTreeHandler(d);
330 mImageAdder.SetCurrentDatabase(d);
331 mImageAdder.SetTreeHandler(handler);
332 mImageAdder.SetSynchronizer(mSynchronizer);
333 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
336 //========================================================================
338 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
340 TreeHandler * handler=GetTreeHandler(d);
341 mImageAdder.SetCurrentDatabase(d);
342 mImageAdder.SetTreeHandler(handler);
343 mImageAdder.SetSynchronizer(mSynchronizer);
344 return mImageAdder.Synchronize(repair, checkAttributes);
347 //========================================================================
349 void Gimmick::Print(const std::string& d)
351 GetTreeHandler(d)->GetTree().Print();
353 //========================================================================
355 void Gimmick::GetSetting(const std::string& name, std::string& value)
357 value = mSettings->getValue(name);
359 //========================================================================
361 //========================================================================
363 void Gimmick::GetAttributes(const std::string& d,
364 const std::string& filename,
365 const std::vector<std::string>& params,
366 std::vector<std::string>& results)
368 TreeHandler * handler=GetTreeHandler(d);
369 mImageAdder.SetCurrentDatabase(d);
370 mImageAdder.SetTreeHandler(handler);
371 mImageAdder.SetSynchronizer(mSynchronizer);
372 mImageAdder.GetAttributes(params, filename, results);
374 //========================================================================
376 //========================================================================
378 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
380 mSettings->updateSetting(name,value);
381 mSettings->writeSettingsFile();
383 //========================================================================
385 void Gimmick::DeleteDrive(const std::string& drive)
387 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
388 it!= mTreeHandlerMap.end(); ++it)
390 mImageAdder.SetTreeHandler(it->second);
391 mImageAdder.DeleteDriveFromMainDB(drive);
393 mImageAdder.SetSynchronizer(mSynchronizer);
394 mImageAdder.DeleteDriveFromOtherDB(drive);
397 //========================================================================
398 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
400 TreeHandler * handler=GetTreeHandler(d);
401 mImageAdder.SetCurrentDatabase(d);
402 mImageAdder.SetTreeHandler(handler);
403 mImageAdder.EditField(node,name,key,val);
405 //========================================================================
408 /////////////////////////////////////////////////////////////////////////
409 // add DB from Settings file //
412 /////////////////////////////////////////////////////////////////////////
413 void Gimmick::addDBSettings()
416 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
418 // split to find all paths
419 std::vector<std::string> paths;
420 std::string separator = ";";
421 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
422 //find first separator
423 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
424 while(std::string::npos != pos || std::string::npos != last_pos)
426 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
427 last_pos = pathSettings.find_first_not_of(separator, pos);
428 pos = pathSettings.find_first_of(separator, last_pos);
431 std::vector<std::string>::iterator it_path = paths.begin();
432 for(; it_path != paths.end(); ++it_path)
434 pos = it_path->find_last_of("\\");
435 last_pos = it_path->find_last_of(".");
436 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
437 addDB(name, it_path->c_str());