1 #include <creaImageIOGimmick.h>
3 #include <creaImageIOSystem.h>
5 #include <boost/filesystem.hpp>
6 #include <boost/algorithm/string.hpp>
12 //==============================================================
16 RegisterGimmickMessageTypes();
18 //==============================================================
22 //==============================================================
27 //==============================================================
30 //==============================================================
31 void Gimmick::Initialize()
33 std::string i_nameDB = "Local database";
34 // Create the UserSettings dir if does not exist
35 CreateUserSettingsDirectory();
36 // Sets the current directory to the home dir
37 mCurrentDirectory = GetHomeDirectory();
40 std::string dbpath = GetLocalDatabasePath();
41 // Create or open local database
42 mLocalDatabase = createDB( mCurrentDirectory + "\\.gimmick\\localdatabase_Descriptor.txt", dbpath);
43 // Add it to the TreeHandlerMap
44 mTreeHandlerMap[i_nameDB] = mLocalDatabase;
46 // Creates files and directories database
47 mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
48 // Create or open local database
49 if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
51 std::string mess = "Timestamp database '";
52 mess += GetTimestampDatabasePath();
53 mess += "' does not exist : creating it";
54 GimmickMessage(1,mess<<std::endl);
56 if ( ! mTimestampDatabase->Create() )
58 GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
65 GimmickMessage(1,"Opening Timestamp database '"
66 <<GetTimestampDatabasePath()<<"' "
68 if ( ! mTimestampDatabase->Open() )
70 GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
77 ///////////////////////////////////////////////////////////////////////
78 // add DB to TreeHandler Map //
79 // @param i_name : DB name //
80 // @param i_location : DB location //
82 ///////////////////////////////////////////////////////////////////////
83 void Gimmick::addDB(std::string &i_name, std::string &i_location)
85 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
89 ///////////////////////////////////////////////////////////////////////////
90 // create a DB from a attributes descriptor file for medical images //
91 // @param i_locDesc : location of descriptor file //
92 // @param i_locDB : location of DB //
93 // return : the SQLiteTreeHandler object on DB //
94 /////////////////////////////////////////////////////////////////////////
95 SQLiteTreeHandler *Gimmick::createDB(std::string &i_locDesc, std::string &i_locDB)
97 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
98 // Create or open local database
99 if (! boost::filesystem::exists(i_locDB) )
101 std::string mess = "Local database '";
103 mess += "' does not exist : creating it";
104 GimmickMessage(1,mess<<std::endl);
106 // CREATING DB STRUCTURE
107 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
108 if ( ! sqlTreeH->Create(true) )
110 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
112 sqlTreeH->SetAttribute(0,"Name","i_name");
117 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
118 if ( !sqlTreeH->Open(true) )
120 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
127 //==============================================================
128 void Gimmick::Finalize()
130 // delete SQLiteTreeHandler Object
131 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
132 it!= mTreeHandlerMap.end(); ++it)
136 delete mTimestampDatabase;
138 //==============================================================
140 //================================================================
143 #define VALID_FILE_SEPARATOR "\\"
144 #define INVALID_FILE_SEPARATOR "/"
146 #define INVALID_FILE_SEPARATOR "\\"
147 #define VALID_FILE_SEPARATOR "/"
149 //================================================================
151 //================================================================
152 const std::string& Gimmick::GetHomeDirectory()
154 if (mHomeDirectory.size()==0)
156 #if defined(__GNUC__)
157 mHomeDirectory = getenv("HOME");
158 #elif defined(_WIN32)
159 mHomeDirectory = getenv("USERPROFILE");
162 return mHomeDirectory;
164 //================================================================
165 const std::string& Gimmick::GetUserSettingsDirectory()
167 if (mUserSettingsDirectory.size()==0)
169 mUserSettingsDirectory = GetHomeDirectory();
170 mUserSettingsDirectory += "/.gimmick/";
171 boost::algorithm::replace_all( mUserSettingsDirectory,
172 INVALID_FILE_SEPARATOR ,
173 VALID_FILE_SEPARATOR);
175 return mUserSettingsDirectory;
177 //================================================================
179 //================================================================
180 const std::string& Gimmick::GetLocalDatabasePath()
182 if (mLocalDatabasePath.size()==0)
184 mLocalDatabasePath = GetUserSettingsDirectory();
185 mLocalDatabasePath += "local_database.sqlite3";
186 boost::algorithm::replace_all( mLocalDatabasePath,
187 INVALID_FILE_SEPARATOR ,
188 VALID_FILE_SEPARATOR);
190 return mLocalDatabasePath;
193 //================================================================
195 //================================================================
196 const std::string& Gimmick::GetTimestampDatabasePath()
198 if (mTimestampDatabasePath.size()==0)
200 mTimestampDatabasePath = GetUserSettingsDirectory();
201 mTimestampDatabasePath += "timestamp_database.sqlite3";
202 boost::algorithm::replace_all( mTimestampDatabasePath,
203 INVALID_FILE_SEPARATOR ,
204 VALID_FILE_SEPARATOR);
206 return mTimestampDatabasePath;
208 //========================================================================
210 //========================================================================
211 void Gimmick::CreateUserSettingsDirectory()
213 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
215 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
216 << "does not exist : creating it"<<std::endl);
218 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
220 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
224 //========================================================================
227 //========================================================================
228 /// Sets message level
229 void Gimmick::SetMessageLevel(int l)
231 SetGimmickMessageLevel(l);
233 //========================================================================
235 //========================================================================
236 /// Sets message level
237 void Gimmick::SetDebugMessageLevel(int l)
239 SetGimmickDebugMessageLevel(l);
241 //========================================================================
243 //========================================================================
244 /// Returns the tree handler with the given name
245 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
247 TreeHandlerMapType::const_iterator i;
248 i = GetTreeHandlerMap().find(name);
249 if ( i == GetTreeHandlerMap().end() )
251 GimmickError("TreeHandler '"<<name<<"' does not exist");
256 //========================================================================
257 ///Returns the timestamp database handler
258 TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const
260 return mTimestampDatabase;
264 //========================================================================
265 /// Add the files to the tree handler
266 void Gimmick::AddFiles(const std::string& d,
267 const std::vector<std::string>& filenames)
269 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
271 mImageAdder.SetTreeHandler(GetTreeHandler(d));
272 mImageAdder.SetTimestampHandler(mTimestampDatabase);
273 mImageAdder.AddFiles(filenames);
276 //========================================================================
278 //========================================================================
279 /// Add a dir to the local database
280 void Gimmick::AddDir(const std::string& d, const std::string& f,
283 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
284 <<recurse<<std::endl);
286 TreeHandler * handler=GetTreeHandler(d);
287 mImageAdder.SetTreeHandler(handler);
288 mImageAdder.SetTimestampHandler(mTimestampDatabase);
289 mImageAdder.AddDirectory(f,recurse);
290 //Synchronize(true, handler);
294 //========================================================================
296 //========================================================================
298 void Gimmick::Synchronize(bool update, TreeHandler* handler)
300 GimmickMessage(4,"Synchronizing. Update:"<<update<<std::endl);
303 mSynchronizer=new Synchronizer(handler);
307 mSynchronizer->SetTreeHandler(handler);
309 mSynchronizer->Synchronize(update);
313 //========================================================================
315 void Gimmick::Print(const std::string& d)
317 GetTreeHandler(d)->GetTree().Print();
319 //========================================================================