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();
38 mSynchronizer= new Synchronizer(GetUserSettingsDirectory());
40 std::string dbpath = GetLocalDatabasePath();
41 // Create or open local database
42 mLocalDatabase = createDB(i_nameDB, 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 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
87 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
88 mTreeHandlerMap[i_name]->Open(true);
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(std::string &i_name, std::string &i_locDesc, std::string &i_locDB)
102 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
103 // Create or open local database
104 if (! boost::filesystem::exists(i_locDB) )
106 std::string mess = "Local database '";
108 mess += "' does not exist : creating it";
109 GimmickMessage(1,mess<<std::endl);
111 // CREATING DB STRUCTURE
112 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
113 if ( ! sqlTreeH->Create(true) )
115 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
117 sqlTreeH->SetAttribute(0,"Name",i_name);
122 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
123 if ( !sqlTreeH->Open(true) )
125 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
132 //==============================================================
133 void Gimmick::Finalize()
136 // delete SQLiteTreeHandler Object
137 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
138 it!= mTreeHandlerMap.end(); ++it)
142 delete mTimestampDatabase;
144 //==============================================================
146 //================================================================
149 #define VALID_FILE_SEPARATOR "\\"
150 #define INVALID_FILE_SEPARATOR "/"
152 #define INVALID_FILE_SEPARATOR "\\"
153 #define VALID_FILE_SEPARATOR "/"
155 //================================================================
157 //================================================================
158 const std::string& Gimmick::GetHomeDirectory()
160 if (mHomeDirectory.size()==0)
162 #if defined(__GNUC__)
163 mHomeDirectory = getenv("HOME");
164 #elif defined(_WIN32)
165 mHomeDirectory = getenv("USERPROFILE");
168 return mHomeDirectory;
170 //================================================================
171 const std::string& Gimmick::GetUserSettingsDirectory()
173 if (mUserSettingsDirectory.size()==0)
175 mUserSettingsDirectory = GetHomeDirectory();
176 mUserSettingsDirectory += "/.gimmick/";
177 boost::algorithm::replace_all( mUserSettingsDirectory,
178 INVALID_FILE_SEPARATOR ,
179 VALID_FILE_SEPARATOR);
181 return mUserSettingsDirectory;
183 //================================================================
185 //================================================================
186 const std::string& Gimmick::GetLocalDatabasePath()
188 if (mLocalDatabasePath.size()==0)
190 mLocalDatabasePath = GetUserSettingsDirectory();
191 mLocalDatabasePath += "local_database.sqlite3";
192 boost::algorithm::replace_all( mLocalDatabasePath,
193 INVALID_FILE_SEPARATOR ,
194 VALID_FILE_SEPARATOR);
196 return mLocalDatabasePath;
199 //================================================================
201 //================================================================
202 const std::string& Gimmick::GetTimestampDatabasePath()
204 if (mTimestampDatabasePath.size()==0)
206 mTimestampDatabasePath = GetUserSettingsDirectory();
207 mTimestampDatabasePath += "timestamp_database.sqlite3";
208 boost::algorithm::replace_all( mTimestampDatabasePath,
209 INVALID_FILE_SEPARATOR ,
210 VALID_FILE_SEPARATOR);
212 return mTimestampDatabasePath;
214 //========================================================================
216 //========================================================================
217 void Gimmick::CreateUserSettingsDirectory()
219 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
221 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
222 << "does not exist : creating it"<<std::endl);
224 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
226 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
230 //========================================================================
233 //========================================================================
234 /// Sets message level
235 void Gimmick::SetMessageLevel(int l)
237 SetGimmickMessageLevel(l);
239 //========================================================================
241 //========================================================================
242 /// Sets message level
243 void Gimmick::SetDebugMessageLevel(int l)
245 SetGimmickDebugMessageLevel(l);
247 //========================================================================
249 //========================================================================
250 /// Returns the tree handler with the given name
251 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
253 TreeHandlerMapType::const_iterator i;
254 i = GetTreeHandlerMap().find(name);
255 if ( i == GetTreeHandlerMap().end() )
257 GimmickError("TreeHandler '"<<name<<"' does not exist");
262 //========================================================================
263 ///Returns the timestamp database handler
264 TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const
266 return mTimestampDatabase;
270 //========================================================================
271 /// Add the files to the tree handler
272 void Gimmick::AddFiles(const std::string& d,
273 const std::vector<std::string>& filenames)
275 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
277 mImageAdder.SetTreeHandler(GetTreeHandler(d));
278 mImageAdder.SetTimestampHandler(mTimestampDatabase);
279 mImageAdder.SetSynchronizer(mSynchronizer);
280 mImageAdder.AddFiles(filenames);
283 //========================================================================
285 //========================================================================
286 /// Add a dir to the local database
287 void Gimmick::AddDir(const std::string& d, const std::string& f,
290 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
291 <<recurse<<std::endl);
293 TreeHandler * handler=GetTreeHandler(d);
294 mImageAdder.SetTreeHandler(handler);
295 mImageAdder.SetTimestampHandler(mTimestampDatabase);
296 mImageAdder.SetSynchronizer(mSynchronizer);
297 mImageAdder.AddDirectory(f,recurse);
300 //========================================================================
302 //========================================================================
303 void Gimmick::RemoveFile(const std::string& d,
304 const tree::Node* & node)
306 mImageAdder.SetSynchronizer(mSynchronizer);
307 mTimestampDatabase->RemoveNode("PATH",node);
308 mImageAdder.RemoveFile(node);
310 //========================================================================
312 //========================================================================
314 std::string Gimmick::Synchronize(bool repair, bool checkAttributes)
316 TreeHandler * handler=GetTreeHandler("Local database");
317 mImageAdder.SetTreeHandler(handler);
318 mImageAdder.SetTimestampHandler(mTimestampDatabase);
319 mImageAdder.SetSynchronizer(mSynchronizer);
320 return mImageAdder.Synchronize(repair, checkAttributes);
323 //========================================================================
325 void Gimmick::Print(const std::string& d)
327 GetTreeHandler(d)->GetTree().Print();
329 //========================================================================