1 #include <creaImageIOGimmick.h>
3 #include <creaMessageManager.h>
4 #include <creaImageIOImageFinder.h>
6 #include <boost/filesystem.hpp>
7 #include <boost/algorithm/string.hpp>
12 //==============================================================
15 crea::MessageManager::RegisterMessageType("Gimmick!",
18 //==============================================================
22 //==============================================================
27 //==============================================================
30 //==============================================================
31 bool Gimmick::Initialize()
33 // Create the UserSettings dir if does not exist
34 if (!CreateUserSettingsDirectory()) return false;
35 // Sets the current directory to the home dir
36 mCurrentDirectory = GetHomeDirectory();
38 // Create local database handler
39 mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath());
40 // Create or open local database
41 if (! boost::filesystem::exists( GetLocalDatabasePath() ) )
43 creaMessage("Gimmick!",1,
44 "[Gimmick!] Local database '"<<GetLocalDatabasePath()<<"' "
45 << "does not exist : creating it"<<std::endl);
47 // CREATING DEFAULT DB STRUCTURE
48 mLocalDatabase->GetTree().GetDescriptor().CreateDefault();
50 if ( ! mLocalDatabase->Create(true) )
52 creaMessage("Gimmick!",1,
53 "[Gimmick!] !! ERROR CREATING '"<<GetLocalDatabasePath()<<"'");
56 mLocalDatabase->SetAttribute(0,"Name","Local database");
61 creaMessage("Gimmick!",1,
62 "[Gimmick!] Opening local database '"
63 <<GetLocalDatabasePath()<<"' "
65 if ( ! mLocalDatabase->Open(true) )
67 creaMessage("Gimmick!",1,
68 "[Gimmick!] !! ERROR OPENING '"<<GetLocalDatabasePath()<<"'");
76 //================================================================
79 //==============================================================
80 bool Gimmick::Finalize()
82 delete mLocalDatabase;
84 //==============================================================
86 //================================================================
89 #define VALID_FILE_SEPARATOR "\\"
90 #define INVALID_FILE_SEPARATOR "/"
92 #define INVALID_FILE_SEPARATOR "\\"
93 #define VALID_FILE_SEPARATOR "/"
95 //================================================================
97 //================================================================
98 const std::string& Gimmick::GetHomeDirectory()
100 if (mHomeDirectory.size()==0)
102 #if defined(__GNUC__)
103 mHomeDirectory = getenv("HOME");
104 #elif defined(_WIN32)
105 mHomeDirectory = getenv("USERPROFILE");
108 return mHomeDirectory;
110 //================================================================
111 const std::string& Gimmick::GetUserSettingsDirectory()
113 if (mUserSettingsDirectory.size()==0)
115 mUserSettingsDirectory = GetHomeDirectory();
116 mUserSettingsDirectory += "/.gimmick/";
117 boost::algorithm::replace_all( mUserSettingsDirectory,
118 INVALID_FILE_SEPARATOR ,
119 VALID_FILE_SEPARATOR);
121 return mUserSettingsDirectory;
123 //================================================================
125 //================================================================
126 const std::string& Gimmick::GetLocalDatabasePath()
128 if (mLocalDatabasePath.size()==0)
130 mLocalDatabasePath = GetUserSettingsDirectory();
131 mLocalDatabasePath += "local_database.sqlite3";
132 boost::algorithm::replace_all( mLocalDatabasePath,
133 INVALID_FILE_SEPARATOR ,
134 VALID_FILE_SEPARATOR);
136 return mLocalDatabasePath;
138 //========================================================================
140 //========================================================================
141 bool Gimmick::CreateUserSettingsDirectory()
143 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
145 creaMessage("Gimmick!",1,
146 "[Gimmick!] Directory '"<<GetUserSettingsDirectory()<<"' "
147 << "does not exist : creating it"<<std::endl);
149 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
151 creaMessage("Gimmick!",1,
152 "[Gimmick!] !! ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
158 //========================================================================
161 //========================================================================
162 /// Sets message level
163 void Gimmick::SetMessageLevel(int l)
165 crea::MessageManager::SetMessageLevel("Gimmick!",l);
167 //========================================================================
169 //========================================================================
170 /// Add a file to the local database
171 void Gimmick::AddFileToLocalDatabase(const std::string& f)
173 ImageFinder finder(mLocalDatabase);
174 if (finder.IsHandledFile(f)) finder.AddFile(f);
177 //========================================================================
179 //========================================================================
180 /// Add a dir to the local database
181 void Gimmick::AddDirToLocalDatabase(const std::string& f, bool recurse)
183 ImageFinder finder(mLocalDatabase);
185 finder.AddDirectory(f,recurse);
188 //========================================================================
190 //========================================================================
192 void Gimmick::PrintLocalDatabase()
194 mLocalDatabase->GetTree().Print();
196 //========================================================================