]> Creatis software - creaImageIO.git/blob - src2/creaImageIOGimmick.cpp
Impemented Remove Function
[creaImageIO.git] / src2 / creaImageIOGimmick.cpp
1 #include <creaImageIOGimmick.h>
2
3 #include <creaImageIOSystem.h>
4
5 #include <boost/filesystem.hpp>
6 #include <boost/algorithm/string.hpp>
7
8 namespace creaImageIO
9 {
10
11
12   //==============================================================
13   Gimmick::Gimmick()
14     : mImageAdder(0)
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 the files to the tree handler
186   void Gimmick::AddFiles(const std::string& d, 
187                         const std::vector<std::string>& filenames)
188   {
189     GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
190  
191     mImageAdder.SetTreeHandler(GetTreeHandler(d));
192     mImageAdder.AddFiles(filenames);
193
194   }
195   //========================================================================
196
197   //========================================================================
198   /// Add a dir to the local database
199   void Gimmick::AddDir(const std::string& d, const std::string& f, 
200                        bool recurse)
201   {
202     GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
203                    <<recurse<<std::endl);
204
205     mImageAdder.SetTreeHandler(GetTreeHandler(d));
206     mImageAdder.AddDirectory(f,recurse);
207     
208   }
209
210   //========================================================================
211
212   //========================================================================
213   /// 
214   void Gimmick::Print(const std::string& d)
215   {
216     GetTreeHandler(d)->GetTree().Print();
217   }
218   //========================================================================
219
220
221 }