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();
38 // Create local database handler
39 mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath());
40 // Add it to the TreeHandlerMap
41 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()<<"'");
73 //================================================================
76 //==============================================================
77 void Gimmick::Finalize()
79 delete mLocalDatabase;
81 //==============================================================
83 //================================================================
86 #define VALID_FILE_SEPARATOR "\\"
87 #define INVALID_FILE_SEPARATOR "/"
89 #define INVALID_FILE_SEPARATOR "\\"
90 #define VALID_FILE_SEPARATOR "/"
92 //================================================================
94 //================================================================
95 const std::string& Gimmick::GetHomeDirectory()
97 if (mHomeDirectory.size()==0)
100 mHomeDirectory = getenv("HOME");
101 #elif defined(_WIN32)
102 mHomeDirectory = getenv("USERPROFILE");
105 return mHomeDirectory;
107 //================================================================
108 const std::string& Gimmick::GetUserSettingsDirectory()
110 if (mUserSettingsDirectory.size()==0)
112 mUserSettingsDirectory = GetHomeDirectory();
113 mUserSettingsDirectory += "/.gimmick/";
114 boost::algorithm::replace_all( mUserSettingsDirectory,
115 INVALID_FILE_SEPARATOR ,
116 VALID_FILE_SEPARATOR);
118 return mUserSettingsDirectory;
120 //================================================================
122 //================================================================
123 const std::string& Gimmick::GetLocalDatabasePath()
125 if (mLocalDatabasePath.size()==0)
127 mLocalDatabasePath = GetUserSettingsDirectory();
128 mLocalDatabasePath += "local_database.sqlite3";
129 boost::algorithm::replace_all( mLocalDatabasePath,
130 INVALID_FILE_SEPARATOR ,
131 VALID_FILE_SEPARATOR);
133 return mLocalDatabasePath;
135 //========================================================================
137 //========================================================================
138 void Gimmick::CreateUserSettingsDirectory()
140 if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
142 GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
143 << "does not exist : creating it"<<std::endl);
145 if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
147 GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
151 //========================================================================
154 //========================================================================
155 /// Sets message level
156 void Gimmick::SetMessageLevel(int l)
158 SetGimmickMessageLevel(l);
160 //========================================================================
162 //========================================================================
163 /// Sets message level
164 void Gimmick::SetDebugMessageLevel(int l)
166 SetGimmickDebugMessageLevel(l);
168 //========================================================================
170 //========================================================================
171 /// Add a file to the local database
172 TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const
174 TreeHandlerMapType::const_iterator i;
175 i = GetTreeHandlerMap().find(name);
176 if ( i == GetTreeHandlerMap().end() )
178 GimmickError("TreeHandler '"<<name<<"' does not exist");
184 //========================================================================
185 /// Add the files to the tree handler
186 void Gimmick::AddFiles(const std::string& d,
187 const std::vector<std::string>& filenames)
189 GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
191 mImageAdder.SetTreeHandler(GetTreeHandler(d));
192 mImageAdder.AddFiles(filenames);
195 //========================================================================
197 //========================================================================
198 /// Add a dir to the local database
199 void Gimmick::AddDir(const std::string& d, const std::string& f,
202 GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
203 <<recurse<<std::endl);
205 mImageAdder.SetTreeHandler(GetTreeHandler(d));
206 mImageAdder.AddDirectory(f,recurse);
210 //========================================================================
212 //========================================================================
214 void Gimmick::Print(const std::string& d)
216 GetTreeHandler(d)->GetTree().Print();
218 //========================================================================