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