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 // Create the UserSettings dir if does not exist
34 CreateUserSettingsDirectory();
35 // Sets the current directory to the home dir
36 mCurrentDirectory = GetHomeDirectory();
39 // Create local database handler
40 mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath());
41 // Add it to the TreeHandlerMap
42 mTreeHandlerMap["Local database"] = mLocalDatabase;
43 // Create or open local database
44 if (! boost::filesystem::exists( GetLocalDatabasePath() ) )
46 std::string mess = "Local database '";
47 mess += GetLocalDatabasePath();
48 mess += "' does not exist : creating it";
49 GimmickMessage(1,mess<<std::endl);
51 // CREATING DEFAULT DB STRUCTURE
52 mLocalDatabase->GetTree().GetDescriptor().CreateDefault();
54 if ( ! mLocalDatabase->Create(true) )
56 GimmickError("ERROR CREATING '"<<GetLocalDatabasePath()<<"'");
58 mLocalDatabase->SetAttribute(0,"Name","Local database");
63 GimmickMessage(1,"Opening local database '"
64 <<GetLocalDatabasePath()<<"' "
66 if ( ! mLocalDatabase->Open(true) )
68 GimmickError("ERROR OPENING '"<<GetLocalDatabasePath()<<"'");
74 // Creates files and directories database
75 mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
76 // Create or open local database
77 if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
79 std::string mess = "Timestamp database '";
80 mess += GetTimestampDatabasePath();
81 mess += "' does not exist : creating it";
82 GimmickMessage(1,mess<<std::endl);
84 if ( ! mTimestampDatabase->Create() )
86 GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
93 GimmickMessage(1,"Opening Timestamp database '"
94 <<GetTimestampDatabasePath()<<"' "
96 if ( ! mTimestampDatabase->Open() )
98 GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
104 //================================================================
107 //==============================================================
108 void Gimmick::Finalize()
110 delete mLocalDatabase;
111 delete mTimestampDatabase;
113 //==============================================================
115 //================================================================
118 #define VALID_FILE_SEPARATOR "\\"
119 #define INVALID_FILE_SEPARATOR "/"
121 #define INVALID_FILE_SEPARATOR "\\"
122 #define VALID_FILE_SEPARATOR "/"
124 //================================================================
126 //================================================================
127 const std::string& Gimmick::GetHomeDirectory()
129 if (mHomeDirectory.size()==0)
131 #if defined(__GNUC__)
132 mHomeDirectory = getenv("HOME");
133 #elif defined(_WIN32)
134 mHomeDirectory = getenv("USERPROFILE");
137 return mHomeDirectory;
139 //================================================================
140 const std::string& Gimmick::GetUserSettingsDirectory()
142 if (mUserSettingsDirectory.size()==0)
144 mUserSettingsDirectory = GetHomeDirectory();
145 mUserSettingsDirectory += "/.gimmick/";
146 boost::algorithm::replace_all( mUserSettingsDirectory,
147 INVALID_FILE_SEPARATOR ,
148 VALID_FILE_SEPARATOR);
150 return mUserSettingsDirectory;
152 //================================================================
154 //================================================================
155 const std::string& Gimmick::GetLocalDatabasePath()
157 if (mLocalDatabasePath.size()==0)
159 mLocalDatabasePath = GetUserSettingsDirectory();
160 mLocalDatabasePath += "local_database.sqlite3";
161 boost::algorithm::replace_all( mLocalDatabasePath,
162 INVALID_FILE_SEPARATOR ,
163 VALID_FILE_SEPARATOR);
165 return mLocalDatabasePath;
168 //================================================================
170 //================================================================
171 const std::string& Gimmick::GetTimestampDatabasePath()
173 if (mTimestampDatabasePath.size()==0)
175 mTimestampDatabasePath = GetUserSettingsDirectory();
176 mTimestampDatabasePath += "timestamp_database.sqlite3";
177 boost::algorithm::replace_all( mTimestampDatabasePath,
178 INVALID_FILE_SEPARATOR ,
179 VALID_FILE_SEPARATOR);
181 return mTimestampDatabasePath;
183 //========================================================================
185 //========================================================================
186 void Gimmick::CreateUserSettingsDirectory()
188 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
190 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
191 << "does not exist : creating it"<<std::endl);
193 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
195 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
199 //========================================================================
202 //========================================================================
203 /// Sets message level
204 void Gimmick::SetMessageLevel(int l)
206 SetGimmickMessageLevel(l);
208 //========================================================================
210 //========================================================================
211 /// Sets message level
212 void Gimmick::SetDebugMessageLevel(int l)
214 SetGimmickDebugMessageLevel(l);
216 //========================================================================
218 //========================================================================
219 /// Returns the tree handler with the given name
220 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
222 TreeHandlerMapType::const_iterator i;
223 i = GetTreeHandlerMap().find(name);
224 if ( i == GetTreeHandlerMap().end() )
226 GimmickError("TreeHandler '"<<name<<"' does not exist");
231 //========================================================================
232 ///Returns the timestamp database handler
233 TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const
235 return mTimestampDatabase;
239 //========================================================================
240 /// Add the files to the tree handler
241 void Gimmick::AddFiles(const std::string& d,
242 const std::vector<std::string>& filenames)
244 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
246 mImageAdder.SetTreeHandler(GetTreeHandler(d));
247 mImageAdder.SetTimestampHandler(mTimestampDatabase);
248 mImageAdder.AddFiles(filenames);
251 //========================================================================
253 //========================================================================
254 /// Add a dir to the local database
255 void Gimmick::AddDir(const std::string& d, const std::string& f,
258 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
259 <<recurse<<std::endl);
261 TreeHandler * handler=GetTreeHandler(d);
262 mImageAdder.SetTreeHandler(handler);
263 mImageAdder.SetTimestampHandler(mTimestampDatabase);
264 mImageAdder.AddDirectory(f,recurse);
265 //Synchronize(true, handler);
269 //========================================================================
271 //========================================================================
273 void Gimmick::Synchronize(bool update, TreeHandler* handler)
275 GimmickMessage(4,"Synchronizing. Update:"<<update<<std::endl);
278 mSynchronizer=new Synchronizer(handler);
282 mSynchronizer->SetTreeHandler(handler);
284 mSynchronizer->Synchronize(update);
288 //========================================================================
290 void Gimmick::Print(const std::string& d)
292 GetTreeHandler(d)->GetTree().Print();
294 //========================================================================