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