]> Creatis software - creaImageIO.git/blob - src2/creaImageIOGimmick.cpp
64099e589c651e2bbf37fe4fefa1667cfcc7ef0a
[creaImageIO.git] / src2 / creaImageIOGimmick.cpp
1 #include <creaImageIOGimmick.h>
2
3 #include <creaImageIOSystem.h>
4 #include <creaImageIOImageFinder.h>
5
6 #include <boost/filesystem.hpp>
7 #include <boost/algorithm/string.hpp>
8
9 namespace creaImageIO
10 {
11   
12   //==============================================================
13   Gimmick::Gimmick()
14   {    
15     crea::MessageManager::RegisterMessageType("Gimmick!",
16                                               "Gimmick",1);
17     crea::MessageManager::RegisterMessageType("Gimmick! DEBUG",
18                                               "Gimmick",0);
19   }
20   //==============================================================
21
22
23
24   //==============================================================
25   Gimmick::~Gimmick()
26   {
27     
28   }
29   //==============================================================
30   
31
32   //==============================================================
33   void Gimmick::Initialize()
34   {
35     // Create the UserSettings dir if does not exist
36     CreateUserSettingsDirectory();
37     // Sets the current directory to the home dir
38     mCurrentDirectory =  GetHomeDirectory();
39
40     // Create local database handler
41     mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath());
42     // Create or open local database
43     if (! boost::filesystem::exists( GetLocalDatabasePath() ) )
44       {
45         std::string mess = "Local database '";
46         mess += GetLocalDatabasePath();
47         mess += "' does not exist : creating it";
48         GimmickMessage(1,mess<<std::endl);
49         
50         // CREATING DEFAULT DB STRUCTURE
51         mLocalDatabase->GetTree().GetDescriptor().CreateDefault();
52         
53         if ( ! mLocalDatabase->Create(true) )
54           {
55             GimmickError("ERROR CREATING '"<<GetLocalDatabasePath()<<"'");
56           }
57         mLocalDatabase->SetAttribute(0,"Name","Local database");
58       }
59     else 
60       {
61         /// Open and test it
62         GimmickMessage(1,"Opening local database '"
63                        <<GetLocalDatabasePath()<<"' "
64                        <<std::endl);
65         if ( ! mLocalDatabase->Open(true) )
66           {
67             GimmickError("ERROR OPENING '"<<GetLocalDatabasePath()<<"'");
68           }
69         
70       }
71   }
72   //================================================================
73
74
75   //==============================================================
76   void Gimmick::Finalize()
77   {
78     delete mLocalDatabase;
79   }
80   //==============================================================
81
82   //================================================================
83   // file separator
84 #if defined(_WIN32)
85 #define VALID_FILE_SEPARATOR "\\"
86 #define INVALID_FILE_SEPARATOR "/"
87 #else
88 #define INVALID_FILE_SEPARATOR "\\"
89 #define VALID_FILE_SEPARATOR "/"
90 #endif
91   //================================================================
92
93   //================================================================
94   const std::string& Gimmick::GetHomeDirectory()
95   {
96     if (mHomeDirectory.size()==0) 
97       {
98 #if defined(__GNUC__)
99         mHomeDirectory = getenv("HOME");
100 #elif defined(_WIN32)
101         mHomeDirectory = getenv("USERPROFILE");
102 #endif
103       }
104     return mHomeDirectory;
105   }
106   //================================================================
107   const std::string& Gimmick::GetUserSettingsDirectory()
108   {
109     if (mUserSettingsDirectory.size()==0) 
110       {
111         mUserSettingsDirectory = GetHomeDirectory();
112         mUserSettingsDirectory += "/.gimmick/";
113         boost::algorithm::replace_all( mUserSettingsDirectory, 
114                                        INVALID_FILE_SEPARATOR , 
115                                        VALID_FILE_SEPARATOR);
116       }
117     return mUserSettingsDirectory;
118   }
119   //================================================================
120
121   //================================================================
122   const std::string& Gimmick::GetLocalDatabasePath()
123   {
124     if (mLocalDatabasePath.size()==0) 
125       {
126         mLocalDatabasePath = GetUserSettingsDirectory();
127         mLocalDatabasePath += "local_database.sqlite3";
128         boost::algorithm::replace_all( mLocalDatabasePath,
129                                        INVALID_FILE_SEPARATOR , 
130                                        VALID_FILE_SEPARATOR);
131       }
132     return mLocalDatabasePath;    
133   }
134   //========================================================================
135
136   //========================================================================
137   void Gimmick::CreateUserSettingsDirectory()
138   {
139     if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
140       {
141         GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
142                        << "does not exist : creating it"<<std::endl);
143         
144         if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
145           {
146             GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
147           }
148       }
149   }
150   //========================================================================
151
152
153   //========================================================================
154   /// Sets message level
155   void Gimmick::SetMessageLevel(int l)
156   {
157     crea::MessageManager::SetMessageLevel("Gimmick!",l);
158   }
159   //========================================================================
160
161   //========================================================================
162   /// Sets message level
163   void Gimmick::SetDebugMessageLevel(int l)
164   {
165     crea::MessageManager::SetMessageLevel("Gimmick! DEBUG",l);
166   }
167   //========================================================================
168
169   //========================================================================
170   /// Add a file to the local database
171   void Gimmick::AddFileToLocalDatabase(const std::string& f)
172   {
173     ImageFinder finder(mLocalDatabase);
174     if (finder.IsHandledFile(f)) 
175       {
176         finder.AddFile(f);
177       }
178     else
179       {
180         GimmickError("File '"<<f<<"' does not exist or is not handled");
181       }    
182   }
183   //========================================================================
184
185   //========================================================================
186   /// Add a dir to the local database
187   void Gimmick::AddDirToLocalDatabase(const std::string& f, bool recurse)
188   {
189     ImageFinder finder(mLocalDatabase);
190     
191     finder.AddDirectory(f,recurse);
192     
193   }
194   //========================================================================
195
196   //========================================================================
197   /// 
198   void Gimmick::PrintLocalDatabase()
199   {
200     mLocalDatabase->GetTree().Print();
201   }
202   //========================================================================
203
204
205 }