1 #include <creaImageIOGimmick.h>
3 #include <creaImageIOSystem.h>
4 #include <boost/filesystem.hpp>
5 #include <boost/algorithm/string.hpp>
11 //==============================================================
15 RegisterGimmickMessageTypes();
19 //==============================================================
23 //==============================================================
28 mSettings->writeSettingsFile();
36 //==============================================================
39 //==============================================================
40 void Gimmick::Initialize()
42 std::string i_nameDB = "Local database";
43 // Create the UserSettings dir if does not exist
44 CreateUserSettingsDirectory();
45 // Sets the current directory to the home dir
46 mCurrentDirectory = GetHomeDirectory();
47 mSynchronizer= new Synchronizer(GetUserSettingsDirectory());
49 mSettings = new Settings(mCurrentDirectory);
52 std::string dbpath = GetLocalDatabasePath();
53 // Create or open local database
54 mLocalDatabase = createDB(i_nameDB, mCurrentDirectory + "\\.gimmick\\localdatabase_Descriptor.txt", dbpath);
55 // Add it to the TreeHandlerMap
56 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
58 //Add additional DB from user Settings
61 // Creates files and directories database
62 mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
63 // Create or open local database
64 if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
66 std::string mess = "Timestamp database '";
67 mess += GetTimestampDatabasePath();
68 mess += "' does not exist : creating it";
69 GimmickMessage(1,mess<<std::endl);
71 if ( ! mTimestampDatabase->Create() )
73 GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
80 GimmickMessage(1,"Opening Timestamp database '"
81 <<GetTimestampDatabasePath()<<"' "
83 if ( ! mTimestampDatabase->Open() )
85 GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
92 ///////////////////////////////////////////////////////////////////////
93 // add DB to TreeHandler Map //
94 // @param i_name : DB name //
95 // @param i_location : DB location //
97 ///////////////////////////////////////////////////////////////////////
98 void Gimmick::addDB(const std::string &i_name,
99 const std::string &i_location)
101 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
103 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
104 mTreeHandlerMap[i_name]->Open(true);
105 mSettings->addDB(i_location);
110 ///////////////////////////////////////////////////////////////////////////
111 // create a DB from a attributes descriptor file for medical images //
112 // @param i_name : DB name //
113 // @param i_locDesc : location of descriptor file //
114 // @param i_locDB : location of DB //
115 // return : the SQLiteTreeHandler object on DB //
116 /////////////////////////////////////////////////////////////////////////
117 SQLiteTreeHandler *Gimmick::createDB(const std::string &i_name,
118 const std::string &i_locDesc,
119 const std::string &i_locDB)
121 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
122 // Create or open local database
123 if (! boost::filesystem::exists(i_locDB) )
125 std::string mess = "Local database '";
127 mess += "' does not exist : creating it";
128 GimmickMessage(1,mess<<std::endl);
130 // CREATING DB STRUCTURE
131 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
132 if ( ! sqlTreeH->Create(true) )
134 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
136 sqlTreeH->SetAttribute(0,"Name",i_name);
141 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
142 if ( !sqlTreeH->Open(true) )
144 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
151 //==============================================================
152 void Gimmick::Finalize()
155 // delete SQLiteTreeHandler Object
156 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
157 it!= mTreeHandlerMap.end(); ++it)
161 delete mTimestampDatabase;
163 //==============================================================
165 //================================================================
168 #define VALID_FILE_SEPARATOR "\\"
169 #define INVALID_FILE_SEPARATOR "/"
171 #define INVALID_FILE_SEPARATOR "\\"
172 #define VALID_FILE_SEPARATOR "/"
174 //================================================================
176 //================================================================
177 const std::string& Gimmick::GetHomeDirectory()
179 if (mHomeDirectory.size()==0)
181 #if defined(__GNUC__)
182 mHomeDirectory = getenv("HOME");
183 #elif defined(_WIN32)
184 mHomeDirectory = getenv("USERPROFILE");
187 return mHomeDirectory;
189 //================================================================
190 const std::string& Gimmick::GetUserSettingsDirectory()
192 if (mUserSettingsDirectory.size()==0)
194 mUserSettingsDirectory = GetHomeDirectory();
195 mUserSettingsDirectory += "/.gimmick/";
196 boost::algorithm::replace_all( mUserSettingsDirectory,
197 INVALID_FILE_SEPARATOR ,
198 VALID_FILE_SEPARATOR);
200 return mUserSettingsDirectory;
202 //================================================================
204 //================================================================
205 const std::string& Gimmick::GetLocalDatabasePath()
207 if (mLocalDatabasePath.size()==0)
209 mLocalDatabasePath = GetUserSettingsDirectory();
210 mLocalDatabasePath += "local_database.sqlite3";
211 boost::algorithm::replace_all( mLocalDatabasePath,
212 INVALID_FILE_SEPARATOR ,
213 VALID_FILE_SEPARATOR);
215 return mLocalDatabasePath;
218 //================================================================
220 //================================================================
221 const std::string& Gimmick::GetTimestampDatabasePath()
223 if (mTimestampDatabasePath.size()==0)
225 mTimestampDatabasePath = GetUserSettingsDirectory();
226 mTimestampDatabasePath += "timestamp_database.sqlite3";
227 boost::algorithm::replace_all( mTimestampDatabasePath,
228 INVALID_FILE_SEPARATOR ,
229 VALID_FILE_SEPARATOR);
231 return mTimestampDatabasePath;
233 //========================================================================
235 //========================================================================
236 void Gimmick::CreateUserSettingsDirectory()
238 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
240 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
241 << "does not exist : creating it"<<std::endl);
243 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
245 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
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");
281 //========================================================================
282 ///Returns the timestamp database handler
283 TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const
285 return mTimestampDatabase;
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.SetTimestampHandler(mTimestampDatabase);
299 mImageAdder.SetSynchronizer(mSynchronizer);
300 mImageAdder.AddFiles(filenames);
303 //========================================================================
305 //========================================================================
306 /// Add a dir to the local database
307 void Gimmick::AddDir(const std::string& d, const std::string& f,
310 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
311 <<recurse<<std::endl);
313 TreeHandler * handler=GetTreeHandler(d);
314 mImageAdder.SetCurrentDatabase(d);
315 mImageAdder.SetTreeHandler(handler);
316 mImageAdder.SetTimestampHandler(mTimestampDatabase);
317 mImageAdder.SetSynchronizer(mSynchronizer);
318 mImageAdder.AddDirectory(f,recurse);
321 //========================================================================
323 //========================================================================
324 void Gimmick::RemoveFile(const std::string& d,
327 mImageAdder.SetCurrentDatabase(d);
328 mImageAdder.SetSynchronizer(mSynchronizer);
329 mTimestampDatabase->RemoveNode("PATH",node,d);
330 mImageAdder.RemoveFile(node);
332 //========================================================================
334 //========================================================================
336 void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
338 TreeHandler * handler=GetTreeHandler(d);
339 mImageAdder.SetCurrentDatabase(d);
340 mImageAdder.SetTreeHandler(handler);
341 mImageAdder.SetTimestampHandler(mTimestampDatabase);
342 mImageAdder.SetSynchronizer(mSynchronizer);
343 mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
346 //========================================================================
348 std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
350 TreeHandler * handler=GetTreeHandler(d);
351 mImageAdder.SetCurrentDatabase(d);
352 mImageAdder.SetTreeHandler(handler);
353 mImageAdder.SetTimestampHandler(mTimestampDatabase);
354 mImageAdder.SetSynchronizer(mSynchronizer);
355 return mImageAdder.Synchronize(repair, checkAttributes);
358 //========================================================================
360 void Gimmick::Print(const std::string& d)
362 GetTreeHandler(d)->GetTree().Print();
364 //========================================================================
366 void Gimmick::GetSetting(const std::string& name, std::string& value)
368 value = mSettings->getValue(name);
370 //========================================================================
372 //========================================================================
374 void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
376 mSettings->updateSetting(name,value);
377 mSettings->writeSettingsFile();
379 //========================================================================
381 void Gimmick::DeleteDrive(const std::string& drive)
383 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
384 it!= mTreeHandlerMap.end(); ++it)
386 mImageAdder.SetTreeHandler(it->second);
387 mImageAdder.DeleteDriveFromMainDB(drive);
389 mImageAdder.SetTimestampHandler(mTimestampDatabase);
390 mImageAdder.SetSynchronizer(mSynchronizer);
391 mImageAdder.DeleteDriveFromOtherDB(drive);
394 //========================================================================
395 void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
397 TreeHandler * handler=GetTreeHandler(d);
398 mImageAdder.SetCurrentDatabase(d);
399 mImageAdder.SetTreeHandler(handler);
400 mImageAdder.EditField(node,name,key,val);
402 //========================================================================
405 /////////////////////////////////////////////////////////////////////////
406 // add DB from Settings file //
409 /////////////////////////////////////////////////////////////////////////
410 void Gimmick::addDBSettings()
413 std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
415 // split to find all paths
416 std::vector<std::string> paths;
417 std::string separator = ";";
418 std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
419 //find first separator
420 std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
421 while(std::string::npos != pos || std::string::npos != last_pos)
423 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
424 last_pos = pathSettings.find_first_not_of(separator, pos);
425 pos = pathSettings.find_first_of(separator, last_pos);
428 std::vector<std::string>::iterator it_path = paths.begin();
429 for(; it_path != paths.end(); ++it_path)
431 pos = it_path->find_last_of("\\");
432 last_pos = it_path->find_last_of(".");
433 std::string name = it_path->substr(pos +1, last_pos -pos-1 );
434 addDB(name, it_path->c_str());