]> git.mxchange.org Git - simgear.git/blob - simgear/package/Root.cxx
Lots more work on package support.
[simgear.git] / simgear / package / Root.cxx
1 // Copyright (C) 2013  James Turner - zakalawe@mac.com
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #include <simgear/package/Root.hxx>
19
20 #include <boost/foreach.hpp>
21 #include <cstring>
22
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/props/props_io.hxx>
25 #include <simgear/io/HTTPRequest.hxx>
26 #include <simgear/io/HTTPClient.hxx>
27 #include <simgear/misc/sg_dir.hxx>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/package/Package.hxx>
30 #include <simgear/package/Install.hxx>
31 #include <simgear/package/Catalog.hxx>
32
33 namespace simgear {
34     
35 namespace pkg {
36
37 void Root::setMaxAgeSeconds(int seconds)
38 {
39     m_maxAgeSeconds = seconds;
40 }
41
42 void Root::setHTTPClient(HTTP::Client* aHTTP)
43 {
44     m_http = aHTTP;
45 }
46
47 HTTP::Client* Root::getHTTPClient() const
48 {
49     return m_http;
50 }
51
52 Root::Root(const SGPath& aPath, const std::string& aVersion) :
53     m_path(aPath),
54     m_http(NULL),
55     m_maxAgeSeconds(60 * 60 * 24),
56     m_delegate(NULL),
57     m_version(aVersion)
58 {
59     if (getenv("LOCALE")) {
60         m_locale = getenv("LOCALE");
61     }
62     
63     Dir d(aPath);
64     if (!d.exists()) {
65         d.create(0755);
66         return;
67     }
68     
69     BOOST_FOREACH(SGPath c, d.children(Dir::TYPE_DIR)) {
70         Catalog* cat = Catalog::createFromPath(this, c);
71         if (cat) {
72            m_catalogs[cat->id()] = cat;     
73         }
74     } // of child directories iteration
75 }
76
77 Root::~Root()
78 {
79     
80 }
81     
82 std::string Root::catalogVersion() const
83 {
84     return m_version;
85 }
86     
87 Catalog* Root::getCatalogById(const std::string& aId) const
88 {
89     CatalogDict::const_iterator it = m_catalogs.find(aId);
90     if (it == m_catalogs.end()) {
91         return NULL;
92     }
93     
94     return it->second;
95 }
96
97 Package* Root::getPackageById(const std::string& aName) const
98 {
99     size_t lastDot = aName.rfind('.');
100     
101     Package* pkg = NULL;
102     if (lastDot == std::string::npos) {
103         // naked package ID
104         CatalogDict::const_iterator it = m_catalogs.begin();
105         for (; it != m_catalogs.end(); ++it) {
106             pkg = it->second->getPackageById(aName);
107             if (pkg) {
108                 return pkg;
109             }
110         }
111         
112         return NULL;
113     }
114     
115     std::string catalogId = aName.substr(0, lastDot);
116     std::string id = aName.substr(lastDot + 1);    
117     Catalog* catalog = getCatalogById(catalogId);
118     if (!catalog) {
119         return NULL;
120     }
121             
122     return catalog->getPackageById(id);
123 }
124
125 CatalogList Root::catalogs() const
126 {
127     CatalogList r;
128     CatalogDict::const_iterator it = m_catalogs.begin();
129     for (; it != m_catalogs.end(); ++it) {
130         r.push_back(it->second);
131     }
132     
133     return r;
134 }
135
136 PackageList
137 Root::packagesMatching(const SGPropertyNode* aFilter) const
138 {
139     PackageList r;
140     
141     CatalogDict::const_iterator it = m_catalogs.begin();
142     for (; it != m_catalogs.end(); ++it) {
143         PackageList r2(it->second->packagesMatching(aFilter));
144         r.insert(r.end(), r2.begin(), r2.end());
145     }
146     
147     return r;
148 }
149
150 PackageList
151 Root::packagesNeedingUpdate() const
152 {
153     PackageList r;
154     
155     CatalogDict::const_iterator it = m_catalogs.begin();
156     for (; it != m_catalogs.end(); ++it) {
157         PackageList r2(it->second->packagesNeedingUpdate());
158         r.insert(r.end(), r2.begin(), r2.end());
159     }
160     
161     return r;
162 }
163
164 void Root::refresh(bool aForce)
165 {
166     CatalogDict::iterator it = m_catalogs.begin();
167     for (; it != m_catalogs.end(); ++it) {
168         if (aForce || (it->second->ageInSeconds() > m_maxAgeSeconds)) {
169             it->second->refresh();
170         }
171     }
172 }
173
174 void Root::setDelegate(simgear::pkg::Delegate *aDelegate)
175 {
176     m_delegate = aDelegate;
177 }
178     
179 void Root::setLocale(const std::string& aLocale)
180 {
181     m_locale = aLocale;
182 }
183
184 std::string Root::getLocale() const
185 {
186     return m_locale;
187 }
188
189 void Root::scheduleToUpdate(Install* aInstall)
190 {
191     if (!aInstall) {
192         sg_exception("missing argument to scheduleToUpdate");
193     }
194     
195     PackageList deps = aInstall->package()->dependencies();
196     BOOST_FOREACH(Package* dep, deps) {
197         // will internally schedule for update if required
198         // hence be careful, this method is re-entered in here!
199         dep->install();
200     }
201
202     bool wasEmpty = m_updateDeque.empty();    
203     m_updateDeque.push_back(aInstall);
204     
205     if (wasEmpty) {
206         aInstall->startUpdate();
207     }
208 }
209
210 void Root::startInstall(Install* aInstall)
211 {
212     if (m_delegate) {
213         m_delegate->startInstall(aInstall);
214     }
215 }
216
217 void Root::installProgress(Install* aInstall, unsigned int aBytes, unsigned int aTotal)
218 {
219     if (m_delegate) {
220         m_delegate->installProgress(aInstall, aBytes, aTotal);
221     }
222 }
223
224 void Root::startNext(Install* aCurrent)
225 {
226     if (m_updateDeque.front() != aCurrent) {
227         SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
228     } else {
229         m_updateDeque.pop_front();
230     }
231     
232     if (!m_updateDeque.empty()) {
233         m_updateDeque.front()->startUpdate();
234     }
235 }
236
237 void Root::finishInstall(Install* aInstall)
238 {
239     if (m_delegate) {
240         m_delegate->finishInstall(aInstall);
241     }
242     
243     startNext(aInstall);
244 }
245
246 void Root::failedInstall(Install* aInstall, Delegate::FailureCode aReason)
247 {
248     SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:" 
249         << aInstall->package()->id() << ":" << aReason);
250     if (m_delegate) {
251         m_delegate->failedInstall(aInstall, aReason);
252     }
253     
254     startNext(aInstall);
255 }
256
257 void Root::catalogRefreshBegin(Catalog* aCat)
258 {
259     m_refreshing.insert(aCat);
260 }
261
262 void Root::catalogRefreshComplete(Catalog* aCat, Delegate::FailureCode aReason)
263 {
264     CatalogDict::iterator catIt = m_catalogs.find(aCat->id());
265     if (aReason != Delegate::FAIL_SUCCESS) {
266         if (m_delegate) {
267             m_delegate->failedRefresh(aCat, aReason);
268         }
269         
270         // if the failure is permanent, delete the catalog from our
271         // list (don't touch it on disk)
272         bool isPermanentFailure = (aReason == Delegate::FAIL_VERSION);
273         if (isPermanentFailure) {
274             SG_LOG(SG_GENERAL, SG_WARN, "permanent failure for catalog:" << aCat->id());
275             m_catalogs.erase(catIt);
276         }
277     } else if (catIt == m_catalogs.end()) {
278         // first fresh, add to our storage now
279         m_catalogs.insert(catIt, CatalogDict::value_type(aCat->id(), aCat));
280     }
281     
282     m_refreshing.erase(aCat);
283     if (m_refreshing.empty()) {
284         if (m_delegate) {
285             m_delegate->refreshComplete();
286         }
287     }
288 }
289
290 } // of namespace pkg
291
292 } // of namespace simgear