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(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 mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
89 ///////////////////////////////////////////////////////////////////////////
90 // create a DB from a attributes descriptor file for medical images //
91 // @param i_name : DB name //
92 // @param i_locDesc : location of descriptor file //
93 // @param i_locDB : location of DB //
94 // return : the SQLiteTreeHandler object on DB //
95 /////////////////////////////////////////////////////////////////////////
96 SQLiteTreeHandler *Gimmick::createDB(std::string &i_name, std::string &i_locDesc, std::string &i_locDB)
98 SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
99 // Create or open local database
100 if (! boost::filesystem::exists(i_locDB) )
102 std::string mess = "Local database '";
104 mess += "' does not exist : creating it";
105 GimmickMessage(1,mess<<std::endl);
107 // CREATING DB STRUCTURE
108 sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
109 if ( ! sqlTreeH->Create(true) )
111 GimmickError("ERROR CREATING '"<<i_locDB<<"'");
113 sqlTreeH->SetAttribute(0,"Name",i_name);
118 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
119 if ( !sqlTreeH->Open(true) )
121 GimmickError("ERROR OPENING '"<<i_locDB<<"'");
128 //==============================================================
129 void Gimmick::Finalize()
132 // delete SQLiteTreeHandler Object
133 for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
134 it!= mTreeHandlerMap.end(); ++it)
138 delete mTimestampDatabase;
140 //==============================================================
142 //================================================================
145 #define VALID_FILE_SEPARATOR "\\"
146 #define INVALID_FILE_SEPARATOR "/"
148 #define INVALID_FILE_SEPARATOR "\\"
149 #define VALID_FILE_SEPARATOR "/"
151 //================================================================
153 //================================================================
154 const std::string& Gimmick::GetHomeDirectory()
156 if (mHomeDirectory.size()==0)
158 #if defined(__GNUC__)
159 mHomeDirectory = getenv("HOME");
160 #elif defined(_WIN32)
161 mHomeDirectory = getenv("USERPROFILE");
164 return mHomeDirectory;
166 //================================================================
167 const std::string& Gimmick::GetUserSettingsDirectory()
169 if (mUserSettingsDirectory.size()==0)
171 mUserSettingsDirectory = GetHomeDirectory();
172 mUserSettingsDirectory += "/.gimmick/";
173 boost::algorithm::replace_all( mUserSettingsDirectory,
174 INVALID_FILE_SEPARATOR ,
175 VALID_FILE_SEPARATOR);
177 return mUserSettingsDirectory;
179 //================================================================
181 //================================================================
182 const std::string& Gimmick::GetLocalDatabasePath()
184 if (mLocalDatabasePath.size()==0)
186 mLocalDatabasePath = GetUserSettingsDirectory();
187 mLocalDatabasePath += "local_database.sqlite3";
188 boost::algorithm::replace_all( mLocalDatabasePath,
189 INVALID_FILE_SEPARATOR ,
190 VALID_FILE_SEPARATOR);
192 return mLocalDatabasePath;
195 //================================================================
197 //================================================================
198 const std::string& Gimmick::GetTimestampDatabasePath()
200 if (mTimestampDatabasePath.size()==0)
202 mTimestampDatabasePath = GetUserSettingsDirectory();
203 mTimestampDatabasePath += "timestamp_database.sqlite3";
204 boost::algorithm::replace_all( mTimestampDatabasePath,
205 INVALID_FILE_SEPARATOR ,
206 VALID_FILE_SEPARATOR);
208 return mTimestampDatabasePath;
210 //========================================================================
212 //========================================================================
213 void Gimmick::CreateUserSettingsDirectory()
215 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
217 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
218 << "does not exist : creating it"<<std::endl);
220 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
222 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
226 //========================================================================
229 //========================================================================
230 /// Sets message level
231 void Gimmick::SetMessageLevel(int l)
233 SetGimmickMessageLevel(l);
235 //========================================================================
237 //========================================================================
238 /// Sets message level
239 void Gimmick::SetDebugMessageLevel(int l)
241 SetGimmickDebugMessageLevel(l);
243 //========================================================================
245 //========================================================================
246 /// Returns the tree handler with the given name
247 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
249 TreeHandlerMapType::const_iterator i;
250 i = GetTreeHandlerMap().find(name);
251 if ( i == GetTreeHandlerMap().end() )
253 GimmickError("TreeHandler '"<<name<<"' does not exist");
258 //========================================================================
259 ///Returns the timestamp database handler
260 TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const
262 return mTimestampDatabase;
266 //========================================================================
267 /// Add the files to the tree handler
268 void Gimmick::AddFiles(const std::string& d,
269 const std::vector<std::string>& filenames)
271 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
273 mImageAdder.SetTreeHandler(GetTreeHandler(d));
274 mImageAdder.SetTimestampHandler(mTimestampDatabase);
275 mImageAdder.AddFiles(filenames);
278 //========================================================================
280 //========================================================================
281 /// Add a dir to the local database
282 void Gimmick::AddDir(const std::string& d, const std::string& f,
285 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
286 <<recurse<<std::endl);
288 TreeHandler * handler=GetTreeHandler(d);
289 mImageAdder.SetTreeHandler(handler);
290 mImageAdder.SetTimestampHandler(mTimestampDatabase);
291 mImageAdder.AddDirectory(f,recurse);
292 //Synchronize(true, handler);
296 //========================================================================
298 //========================================================================
300 void Gimmick::Synchronize(bool update, TreeHandler* handler)
302 GimmickMessage(4,"Synchronizing. Update:"<<update<<std::endl);
305 mSynchronizer=new Synchronizer(handler);
309 mSynchronizer->SetTreeHandler(handler);
311 mSynchronizer->Synchronize(update);
315 //========================================================================
317 void Gimmick::Print(const std::string& d)
319 GetTreeHandler(d)->GetTree().Print();
321 //========================================================================