1 #include <creaImageIOGimmick.h>
3 #include <creaImageIOSystem.h>
4 #include <creaImageIOImageFinder.h>
6 #include <boost/filesystem.hpp>
7 #include <boost/algorithm/string.hpp>
12 //==============================================================
15 crea::MessageManager::RegisterMessageType("Gimmick!",
17 crea::MessageManager::RegisterMessageType("Gimmick! DEBUG",
20 //==============================================================
24 //==============================================================
29 //==============================================================
32 //==============================================================
33 void Gimmick::Initialize()
35 // Create the UserSettings dir if does not exist
36 CreateUserSettingsDirectory();
37 // Sets the current directory to the home dir
38 mCurrentDirectory = GetHomeDirectory();
40 // Create local database handler
41 mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath());
42 // Create or open local database
43 if (! boost::filesystem::exists( GetLocalDatabasePath() ) )
45 std::string mess = "Local database '";
46 mess += GetLocalDatabasePath();
47 mess += "' does not exist : creating it";
48 GimmickMessage(1,mess<<std::endl);
50 // CREATING DEFAULT DB STRUCTURE
51 mLocalDatabase->GetTree().GetDescriptor().CreateDefault();
53 if ( ! mLocalDatabase->Create(true) )
55 GimmickError("ERROR CREATING '"<<GetLocalDatabasePath()<<"'");
57 mLocalDatabase->SetAttribute(0,"Name","Local database");
62 GimmickMessage(1,"Opening local database '"
63 <<GetLocalDatabasePath()<<"' "
65 if ( ! mLocalDatabase->Open(true) )
67 GimmickError("ERROR OPENING '"<<GetLocalDatabasePath()<<"'");
72 //================================================================
75 //==============================================================
76 void Gimmick::Finalize()
78 delete mLocalDatabase;
80 //==============================================================
82 //================================================================
85 #define VALID_FILE_SEPARATOR "\\"
86 #define INVALID_FILE_SEPARATOR "/"
88 #define INVALID_FILE_SEPARATOR "\\"
89 #define VALID_FILE_SEPARATOR "/"
91 //================================================================
93 //================================================================
94 const std::string& Gimmick::GetHomeDirectory()
96 if (mHomeDirectory.size()==0)
99 mHomeDirectory = getenv("HOME");
100 #elif defined(_WIN32)
101 mHomeDirectory = getenv("USERPROFILE");
104 return mHomeDirectory;
106 //================================================================
107 const std::string& Gimmick::GetUserSettingsDirectory()
109 if (mUserSettingsDirectory.size()==0)
111 mUserSettingsDirectory = GetHomeDirectory();
112 mUserSettingsDirectory += "/.gimmick/";
113 boost::algorithm::replace_all( mUserSettingsDirectory,
114 INVALID_FILE_SEPARATOR ,
115 VALID_FILE_SEPARATOR);
117 return mUserSettingsDirectory;
119 //================================================================
121 //================================================================
122 const std::string& Gimmick::GetLocalDatabasePath()
124 if (mLocalDatabasePath.size()==0)
126 mLocalDatabasePath = GetUserSettingsDirectory();
127 mLocalDatabasePath += "local_database.sqlite3";
128 boost::algorithm::replace_all( mLocalDatabasePath,
129 INVALID_FILE_SEPARATOR ,
130 VALID_FILE_SEPARATOR);
132 return mLocalDatabasePath;
134 //========================================================================
136 //========================================================================
137 void Gimmick::CreateUserSettingsDirectory()
139 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
141 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
142 << "does not exist : creating it"<<std::endl);
144 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
146 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
150 //========================================================================
153 //========================================================================
154 /// Sets message level
155 void Gimmick::SetMessageLevel(int l)
157 crea::MessageManager::SetMessageLevel("Gimmick!",l);
159 //========================================================================
161 //========================================================================
162 /// Sets message level
163 void Gimmick::SetDebugMessageLevel(int l)
165 crea::MessageManager::SetMessageLevel("Gimmick! DEBUG",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))
180 GimmickError("File '"<<f<<"' does not exist or is not handled");
183 //========================================================================
185 //========================================================================
186 /// Add a dir to the local database
187 void Gimmick::AddDirToLocalDatabase(const std::string& f, bool recurse)
189 ImageFinder finder(mLocalDatabase);
191 finder.AddDirectory(f,recurse);
194 //========================================================================
196 //========================================================================
198 void Gimmick::PrintLocalDatabase()
200 mLocalDatabase->GetTree().Print();
202 //========================================================================